DOMSVGAnimatedNumberList.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "DOMSVGAnimatedNumberList.h"
  6. #include "DOMSVGNumberList.h"
  7. #include "SVGAnimatedNumberList.h"
  8. #include "nsSVGElement.h"
  9. #include "nsCOMPtr.h"
  10. #include "nsSVGAttrTearoffTable.h"
  11. #include "mozilla/dom/SVGAnimatedNumberListBinding.h"
  12. // See the architecture comment in this file's header.
  13. namespace mozilla {
  14. static inline
  15. nsSVGAttrTearoffTable<SVGAnimatedNumberList, DOMSVGAnimatedNumberList>&
  16. SVGAnimatedNumberListTearoffTable()
  17. {
  18. static nsSVGAttrTearoffTable<SVGAnimatedNumberList, DOMSVGAnimatedNumberList>
  19. sSVGAnimatedNumberListTearoffTable;
  20. return sSVGAnimatedNumberListTearoffTable;
  21. }
  22. NS_SVG_VAL_IMPL_CYCLE_COLLECTION_WRAPPERCACHED(DOMSVGAnimatedNumberList, mElement)
  23. NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMSVGAnimatedNumberList)
  24. NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMSVGAnimatedNumberList)
  25. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMSVGAnimatedNumberList)
  26. NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  27. NS_INTERFACE_MAP_ENTRY(nsISupports)
  28. NS_INTERFACE_MAP_END
  29. JSObject*
  30. DOMSVGAnimatedNumberList::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  31. {
  32. return mozilla::dom::SVGAnimatedNumberListBinding::Wrap(aCx, this, aGivenProto);
  33. }
  34. already_AddRefed<DOMSVGNumberList>
  35. DOMSVGAnimatedNumberList::BaseVal()
  36. {
  37. if (!mBaseVal) {
  38. mBaseVal = new DOMSVGNumberList(this, InternalAList().GetBaseValue());
  39. }
  40. RefPtr<DOMSVGNumberList> baseVal = mBaseVal;
  41. return baseVal.forget();
  42. }
  43. already_AddRefed<DOMSVGNumberList>
  44. DOMSVGAnimatedNumberList::AnimVal()
  45. {
  46. if (!mAnimVal) {
  47. mAnimVal = new DOMSVGNumberList(this, InternalAList().GetAnimValue());
  48. }
  49. RefPtr<DOMSVGNumberList> animVal = mAnimVal;
  50. return animVal.forget();
  51. }
  52. /* static */ already_AddRefed<DOMSVGAnimatedNumberList>
  53. DOMSVGAnimatedNumberList::GetDOMWrapper(SVGAnimatedNumberList *aList,
  54. nsSVGElement *aElement,
  55. uint8_t aAttrEnum)
  56. {
  57. RefPtr<DOMSVGAnimatedNumberList> wrapper =
  58. SVGAnimatedNumberListTearoffTable().GetTearoff(aList);
  59. if (!wrapper) {
  60. wrapper = new DOMSVGAnimatedNumberList(aElement, aAttrEnum);
  61. SVGAnimatedNumberListTearoffTable().AddTearoff(aList, wrapper);
  62. }
  63. return wrapper.forget();
  64. }
  65. /* static */ DOMSVGAnimatedNumberList*
  66. DOMSVGAnimatedNumberList::GetDOMWrapperIfExists(SVGAnimatedNumberList *aList)
  67. {
  68. return SVGAnimatedNumberListTearoffTable().GetTearoff(aList);
  69. }
  70. DOMSVGAnimatedNumberList::~DOMSVGAnimatedNumberList()
  71. {
  72. // Script no longer has any references to us, to our base/animVal objects, or
  73. // to any of their list items.
  74. SVGAnimatedNumberListTearoffTable().RemoveTearoff(&InternalAList());
  75. }
  76. void
  77. DOMSVGAnimatedNumberList::InternalBaseValListWillChangeTo(const SVGNumberList& aNewValue)
  78. {
  79. // When the number of items in our internal counterpart's baseVal changes,
  80. // we MUST keep our baseVal in sync. If we don't, script will either see a
  81. // list that is too short and be unable to access indexes that should be
  82. // valid, or else, MUCH WORSE, script will see a list that is too long and be
  83. // able to access "items" at indexes that are out of bounds (read/write to
  84. // bad memory)!!
  85. RefPtr<DOMSVGAnimatedNumberList> kungFuDeathGrip;
  86. if (mBaseVal) {
  87. if (aNewValue.Length() < mBaseVal->LengthNoFlush()) {
  88. // InternalListLengthWillChange might clear last reference to |this|.
  89. // Retain a temporary reference to keep from dying before returning.
  90. kungFuDeathGrip = this;
  91. }
  92. mBaseVal->InternalListLengthWillChange(aNewValue.Length());
  93. }
  94. // If our attribute is not animating, then our animVal mirrors our baseVal
  95. // and we must sync its length too. (If our attribute is animating, then the
  96. // SMIL engine takes care of calling InternalAnimValListWillChangeTo() if
  97. // necessary.)
  98. if (!IsAnimating()) {
  99. InternalAnimValListWillChangeTo(aNewValue);
  100. }
  101. }
  102. void
  103. DOMSVGAnimatedNumberList::InternalAnimValListWillChangeTo(const SVGNumberList& aNewValue)
  104. {
  105. if (mAnimVal) {
  106. mAnimVal->InternalListLengthWillChange(aNewValue.Length());
  107. }
  108. }
  109. bool
  110. DOMSVGAnimatedNumberList::IsAnimating() const
  111. {
  112. return InternalAList().IsAnimating();
  113. }
  114. SVGAnimatedNumberList&
  115. DOMSVGAnimatedNumberList::InternalAList()
  116. {
  117. return *mElement->GetAnimatedNumberList(mAttrEnum);
  118. }
  119. const SVGAnimatedNumberList&
  120. DOMSVGAnimatedNumberList::InternalAList() const
  121. {
  122. return *mElement->GetAnimatedNumberList(mAttrEnum);
  123. }
  124. } // namespace mozilla