DOMSVGNumberList.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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_DOMSVGNUMBERLIST_H__
  6. #define MOZILLA_DOMSVGNUMBERLIST_H__
  7. #include "DOMSVGAnimatedNumberList.h"
  8. #include "nsCycleCollectionParticipant.h"
  9. #include "nsDebug.h"
  10. #include "nsTArray.h"
  11. #include "SVGNumberList.h"
  12. #include "mozilla/Attributes.h"
  13. #include "mozilla/ErrorResult.h"
  14. class nsSVGElement;
  15. namespace mozilla {
  16. class DOMSVGNumber;
  17. /**
  18. * Class DOMSVGNumberList
  19. *
  20. * This class is used to create the DOM tearoff objects that wrap internal
  21. * SVGNumberList objects.
  22. *
  23. * See the architecture comment in DOMSVGAnimatedNumberList.h.
  24. *
  25. * This class is strongly intertwined with DOMSVGAnimatedNumberList and
  26. * DOMSVGNumber. We are a friend of DOMSVGAnimatedNumberList, and are
  27. * responsible for nulling out our DOMSVGAnimatedNumberList's pointer to us
  28. * when we die, essentially making its pointer to us a weak pointer. Similarly,
  29. * our DOMSVGNumber 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 DOMSVGNumberList final : public nsISupports,
  35. public nsWrapperCache
  36. {
  37. friend class AutoChangeNumberListNotifier;
  38. friend class DOMSVGNumber;
  39. ~DOMSVGNumberList() {
  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(DOMSVGNumberList)
  50. DOMSVGNumberList(DOMSVGAnimatedNumberList *aAList,
  51. const SVGNumberList &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& error);
  98. already_AddRefed<DOMSVGNumber> Initialize(DOMSVGNumber& newItem,
  99. ErrorResult& error);
  100. already_AddRefed<DOMSVGNumber> GetItem(uint32_t index, ErrorResult& error);
  101. already_AddRefed<DOMSVGNumber> IndexedGetter(uint32_t index, bool& found,
  102. ErrorResult& error);
  103. already_AddRefed<DOMSVGNumber> InsertItemBefore(DOMSVGNumber& newItem,
  104. uint32_t index, ErrorResult& error);
  105. already_AddRefed<DOMSVGNumber> ReplaceItem(DOMSVGNumber& newItem, uint32_t index,
  106. ErrorResult& error);
  107. already_AddRefed<DOMSVGNumber> RemoveItem(uint32_t index,
  108. ErrorResult& error);
  109. already_AddRefed<DOMSVGNumber> AppendItem(DOMSVGNumber& newItem,
  110. ErrorResult& error)
  111. {
  112. return InsertItemBefore(newItem, LengthNoFlush(), error);
  113. }
  114. uint32_t Length() const
  115. {
  116. return NumberOfItems();
  117. }
  118. private:
  119. nsSVGElement* Element() const {
  120. return mAList->mElement;
  121. }
  122. uint8_t AttrEnum() const {
  123. return mAList->mAttrEnum;
  124. }
  125. /// Used to determine if this list is the baseVal or animVal list.
  126. bool IsAnimValList() const {
  127. MOZ_ASSERT(this == mAList->mBaseVal || this == mAList->mAnimVal,
  128. "Calling IsAnimValList() too early?!");
  129. return this == mAList->mAnimVal;
  130. }
  131. /**
  132. * Get a reference to this object's corresponding internal SVGNumberList.
  133. *
  134. * To simplify the code we just have this one method for obtaining both
  135. * baseVal and animVal internal lists. This means that animVal lists don't
  136. * get const protection, but our setter methods guard against changing
  137. * animVal lists.
  138. */
  139. SVGNumberList& InternalList() const;
  140. /// Returns the DOMSVGNumber at aIndex, creating it if necessary.
  141. already_AddRefed<DOMSVGNumber> GetItemAt(uint32_t aIndex);
  142. void MaybeInsertNullInAnimValListAt(uint32_t aIndex);
  143. void MaybeRemoveItemFromAnimValListAt(uint32_t aIndex);
  144. // Weak refs to our DOMSVGNumber items. The items are friends and take care
  145. // of clearing our pointer to them when they die.
  146. FallibleTArray<DOMSVGNumber*> mItems;
  147. RefPtr<DOMSVGAnimatedNumberList> mAList;
  148. };
  149. } // namespace mozilla
  150. #endif // MOZILLA_DOMSVGNUMBERLIST_H__