DOMSVGAnimatedLengthList.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "DOMSVGAnimatedLengthList.h"
  6. #include "DOMSVGLengthList.h"
  7. #include "SVGAnimatedLengthList.h"
  8. #include "nsSVGElement.h"
  9. #include "nsCOMPtr.h"
  10. #include "nsSVGAttrTearoffTable.h"
  11. #include "mozilla/dom/SVGAnimatedLengthListBinding.h"
  12. // See the architecture comment in this file's header.
  13. namespace mozilla {
  14. static inline
  15. nsSVGAttrTearoffTable<SVGAnimatedLengthList, DOMSVGAnimatedLengthList>&
  16. SVGAnimatedLengthListTearoffTable()
  17. {
  18. static nsSVGAttrTearoffTable<SVGAnimatedLengthList, DOMSVGAnimatedLengthList>
  19. sSVGAnimatedLengthListTearoffTable;
  20. return sSVGAnimatedLengthListTearoffTable;
  21. }
  22. NS_SVG_VAL_IMPL_CYCLE_COLLECTION_WRAPPERCACHED(DOMSVGAnimatedLengthList, mElement)
  23. NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(DOMSVGAnimatedLengthList, AddRef)
  24. NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(DOMSVGAnimatedLengthList, Release)
  25. JSObject*
  26. DOMSVGAnimatedLengthList::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  27. {
  28. return dom::SVGAnimatedLengthListBinding::Wrap(aCx, this, aGivenProto);
  29. }
  30. already_AddRefed<DOMSVGLengthList>
  31. DOMSVGAnimatedLengthList::BaseVal()
  32. {
  33. if (!mBaseVal) {
  34. mBaseVal = new DOMSVGLengthList(this, InternalAList().GetBaseValue());
  35. }
  36. RefPtr<DOMSVGLengthList> baseVal = mBaseVal;
  37. return baseVal.forget();
  38. }
  39. already_AddRefed<DOMSVGLengthList>
  40. DOMSVGAnimatedLengthList::AnimVal()
  41. {
  42. if (!mAnimVal) {
  43. mAnimVal = new DOMSVGLengthList(this, InternalAList().GetAnimValue());
  44. }
  45. RefPtr<DOMSVGLengthList> animVal = mAnimVal;
  46. return animVal.forget();
  47. }
  48. /* static */ already_AddRefed<DOMSVGAnimatedLengthList>
  49. DOMSVGAnimatedLengthList::GetDOMWrapper(SVGAnimatedLengthList *aList,
  50. nsSVGElement *aElement,
  51. uint8_t aAttrEnum,
  52. uint8_t aAxis)
  53. {
  54. RefPtr<DOMSVGAnimatedLengthList> wrapper =
  55. SVGAnimatedLengthListTearoffTable().GetTearoff(aList);
  56. if (!wrapper) {
  57. wrapper = new DOMSVGAnimatedLengthList(aElement, aAttrEnum, aAxis);
  58. SVGAnimatedLengthListTearoffTable().AddTearoff(aList, wrapper);
  59. }
  60. return wrapper.forget();
  61. }
  62. /* static */ DOMSVGAnimatedLengthList*
  63. DOMSVGAnimatedLengthList::GetDOMWrapperIfExists(SVGAnimatedLengthList *aList)
  64. {
  65. return SVGAnimatedLengthListTearoffTable().GetTearoff(aList);
  66. }
  67. DOMSVGAnimatedLengthList::~DOMSVGAnimatedLengthList()
  68. {
  69. // Script no longer has any references to us, to our base/animVal objects, or
  70. // to any of their list items.
  71. SVGAnimatedLengthListTearoffTable().RemoveTearoff(&InternalAList());
  72. }
  73. void
  74. DOMSVGAnimatedLengthList::InternalBaseValListWillChangeTo(const SVGLengthList& aNewValue)
  75. {
  76. // When the number of items in our internal counterpart's baseVal changes,
  77. // we MUST keep our baseVal in sync. If we don't, script will either see a
  78. // list that is too short and be unable to access indexes that should be
  79. // valid, or else, MUCH WORSE, script will see a list that is too long and be
  80. // able to access "items" at indexes that are out of bounds (read/write to
  81. // bad memory)!!
  82. RefPtr<DOMSVGAnimatedLengthList> kungFuDeathGrip;
  83. if (mBaseVal) {
  84. if (aNewValue.Length() < mBaseVal->LengthNoFlush()) {
  85. // InternalListLengthWillChange might clear last reference to |this|.
  86. // Retain a temporary reference to keep from dying before returning.
  87. kungFuDeathGrip = this;
  88. }
  89. mBaseVal->InternalListLengthWillChange(aNewValue.Length());
  90. }
  91. // If our attribute is not animating, then our animVal mirrors our baseVal
  92. // and we must sync its length too. (If our attribute is animating, then the
  93. // SMIL engine takes care of calling InternalAnimValListWillChangeTo() if
  94. // necessary.)
  95. if (!IsAnimating()) {
  96. InternalAnimValListWillChangeTo(aNewValue);
  97. }
  98. }
  99. void
  100. DOMSVGAnimatedLengthList::InternalAnimValListWillChangeTo(const SVGLengthList& aNewValue)
  101. {
  102. if (mAnimVal) {
  103. mAnimVal->InternalListLengthWillChange(aNewValue.Length());
  104. }
  105. }
  106. bool
  107. DOMSVGAnimatedLengthList::IsAnimating() const
  108. {
  109. return InternalAList().IsAnimating();
  110. }
  111. SVGAnimatedLengthList&
  112. DOMSVGAnimatedLengthList::InternalAList()
  113. {
  114. return *mElement->GetAnimatedLengthList(mAttrEnum);
  115. }
  116. const SVGAnimatedLengthList&
  117. DOMSVGAnimatedLengthList::InternalAList() const
  118. {
  119. return *mElement->GetAnimatedLengthList(mAttrEnum);
  120. }
  121. } // namespace mozilla