DOMSVGLengthList.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #ifndef MOZILLA_DOMSVGLENGTHLIST_H__
  6. #define MOZILLA_DOMSVGLENGTHLIST_H__
  7. #include "DOMSVGAnimatedLengthList.h"
  8. #include "nsCycleCollectionParticipant.h"
  9. #include "nsDebug.h"
  10. #include "nsTArray.h"
  11. #include "SVGLengthList.h"
  12. #include "mozilla/Attributes.h"
  13. #include "mozilla/ErrorResult.h"
  14. class nsSVGElement;
  15. namespace mozilla {
  16. class DOMSVGLength;
  17. /**
  18. * Class DOMSVGLengthList
  19. *
  20. * This class is used to create the DOM tearoff objects that wrap internal
  21. * SVGLengthList objects.
  22. *
  23. * See the architecture comment in DOMSVGAnimatedLengthList.h.
  24. *
  25. * This class is strongly intertwined with DOMSVGAnimatedLengthList and
  26. * DOMSVGLength. We are a friend of DOMSVGAnimatedLengthList, and are
  27. * responsible for nulling out our DOMSVGAnimatedLengthList's pointer to us
  28. * when we die, essentially making its pointer to us a weak pointer. Similarly,
  29. * our DOMSVGLength items are friends of us and responsible for nulling out our
  30. * pointers to them.
  31. *
  32. * Our DOM items are created lazily on demand as and when script requests them.
  33. */
  34. class DOMSVGLengthList final : public nsISupports,
  35. public nsWrapperCache
  36. {
  37. friend class AutoChangeLengthListNotifier;
  38. friend class DOMSVGLength;
  39. ~DOMSVGLengthList() {
  40. // Our mAList's weak ref to us must be nulled out when we die. If GC has
  41. // unlinked us using the cycle collector code, then that has already
  42. // happened, and mAList is null.
  43. if (mAList) {
  44. ( IsAnimValList() ? mAList->mAnimVal : mAList->mBaseVal ) = nullptr;
  45. }
  46. }
  47. public:
  48. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  49. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMSVGLengthList)
  50. DOMSVGLengthList(DOMSVGAnimatedLengthList *aAList,
  51. const SVGLengthList &aInternalList)
  52. : mAList(aAList)
  53. {
  54. // aInternalList must be passed in explicitly because we can't use
  55. // InternalList() here. (Because it depends on IsAnimValList, which depends
  56. // on this object having been assigned to aAList's mBaseVal or mAnimVal,
  57. // which hasn't happend yet.)
  58. InternalListLengthWillChange(aInternalList.Length()); // Sync mItems
  59. }
  60. virtual JSObject* WrapObject(JSContext *cx, JS::Handle<JSObject*> aGivenProto) override;
  61. nsISupports* GetParentObject()
  62. {
  63. return static_cast<nsIContent*>(Element());
  64. }
  65. /**
  66. * This will normally be the same as InternalList().Length(), except if we've
  67. * hit OOM in which case our length will be zero.
  68. */
  69. uint32_t LengthNoFlush() const {
  70. MOZ_ASSERT(mItems.Length() == 0 ||
  71. mItems.Length() == InternalList().Length(),
  72. "DOM wrapper's list length is out of sync");
  73. return mItems.Length();
  74. }
  75. /// Called to notify us to syncronize our length and detach excess items.
  76. void InternalListLengthWillChange(uint32_t aNewLength);
  77. /**
  78. * Returns true if our attribute is animating (in which case our animVal is
  79. * not simply a mirror of our baseVal).
  80. */
  81. bool IsAnimating() const {
  82. return mAList->IsAnimating();
  83. }
  84. /**
  85. * Returns true if there is an animated list mirroring the base list.
  86. */
  87. bool AnimListMirrorsBaseList() const {
  88. return mAList->mAnimVal && !mAList->IsAnimating();
  89. }
  90. uint32_t NumberOfItems() const
  91. {
  92. if (IsAnimValList()) {
  93. Element()->FlushAnimations();
  94. }
  95. return LengthNoFlush();
  96. }
  97. void Clear(ErrorResult& aError);
  98. already_AddRefed<DOMSVGLength> Initialize(DOMSVGLength& newItem,
  99. ErrorResult& error);
  100. already_AddRefed<DOMSVGLength> GetItem(uint32_t index,
  101. ErrorResult& error);
  102. already_AddRefed<DOMSVGLength> IndexedGetter(uint32_t index, bool& found,
  103. ErrorResult& error);
  104. already_AddRefed<DOMSVGLength> InsertItemBefore(DOMSVGLength& newItem,
  105. uint32_t index,
  106. ErrorResult& error);
  107. already_AddRefed<DOMSVGLength> ReplaceItem(DOMSVGLength& newItem,
  108. uint32_t index,
  109. ErrorResult& error);
  110. already_AddRefed<DOMSVGLength> RemoveItem(uint32_t index,
  111. ErrorResult& error);
  112. already_AddRefed<DOMSVGLength> AppendItem(DOMSVGLength& newItem,
  113. ErrorResult& error)
  114. {
  115. return InsertItemBefore(newItem, LengthNoFlush(), error);
  116. }
  117. uint32_t Length() const
  118. {
  119. return NumberOfItems();
  120. }
  121. private:
  122. nsSVGElement* Element() const {
  123. return mAList->mElement;
  124. }
  125. uint8_t AttrEnum() const {
  126. return mAList->mAttrEnum;
  127. }
  128. uint8_t Axis() const {
  129. return mAList->mAxis;
  130. }
  131. /// Used to determine if this list is the baseVal or animVal list.
  132. bool IsAnimValList() const {
  133. MOZ_ASSERT(this == mAList->mBaseVal || this == mAList->mAnimVal,
  134. "Calling IsAnimValList() too early?!");
  135. return this == mAList->mAnimVal;
  136. }
  137. /**
  138. * Get a reference to this object's corresponding internal SVGLengthList.
  139. *
  140. * To simplify the code we just have this one method for obtaining both
  141. * baseVal and animVal internal lists. This means that animVal lists don't
  142. * get const protection, but our setter methods guard against changing
  143. * animVal lists.
  144. */
  145. SVGLengthList& InternalList() const;
  146. /// Returns the DOMSVGLength at aIndex, creating it if necessary.
  147. already_AddRefed<DOMSVGLength> GetItemAt(uint32_t aIndex);
  148. void MaybeInsertNullInAnimValListAt(uint32_t aIndex);
  149. void MaybeRemoveItemFromAnimValListAt(uint32_t aIndex);
  150. // Weak refs to our DOMSVGLength items. The items are friends and take care
  151. // of clearing our pointer to them when they die.
  152. FallibleTArray<DOMSVGLength*> mItems;
  153. RefPtr<DOMSVGAnimatedLengthList> mAList;
  154. };
  155. } // namespace mozilla
  156. #endif // MOZILLA_DOMSVGLENGTHLIST_H__