1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef DOM_SVG_DOMSVGLENGTHLIST_H_
8 #define DOM_SVG_DOMSVGLENGTHLIST_H_
10 #include "DOMSVGAnimatedLengthList.h"
11 #include "mozAutoDocUpdate.h"
12 #include "nsCycleCollectionParticipant.h"
15 #include "SVGLengthList.h"
16 #include "mozilla/Attributes.h"
17 #include "mozilla/Unused.h"
19 // {cbecb7a4-d6f3-47b5-b5a3-3e5bdbf5b2f9}
20 #define MOZILLA_DOMSVGLENGTHLIST_IID \
22 0xcbecb7a4, 0xd6f3, 0xd6f3, { \
23 0xb5, 0xa3, 0x3e, 0x5b, 0xdb, 0xf5, 0xb2, 0xf9 \
34 //----------------------------------------------------------------------
35 // Helper class: AutoChangeLengthListNotifier
36 // Stack-based helper class to pair calls to WillChangeLengthList and
37 // DidChangeLengthList. Used by DOMSVGLength and DOMSVGLengthList.
39 class MOZ_RAII AutoChangeLengthListNotifier
: public mozAutoDocUpdate
{
41 explicit AutoChangeLengthListNotifier(T
* aValue
)
42 : mozAutoDocUpdate(aValue
->Element()->GetComposedDoc(), true),
44 MOZ_ASSERT(aValue
, "Expecting non-null value");
46 mValue
->Element()->WillChangeLengthList(mValue
->AttrEnum(), *this);
49 ~AutoChangeLengthListNotifier() {
50 mValue
->Element()->DidChangeLengthList(mValue
->AttrEnum(), mEmptyOrOldValue
,
52 if (mValue
->IsAnimating()) {
53 mValue
->Element()->AnimationNeedsResample();
59 nsAttrValue mEmptyOrOldValue
;
63 * Class DOMSVGLengthList
65 * This class is used to create the DOM tearoff objects that wrap internal
66 * SVGLengthList objects.
68 * See the architecture comment in DOMSVGAnimatedLengthList.h.
70 * This class is strongly intertwined with DOMSVGAnimatedLengthList and
71 * DOMSVGLength. We are a friend of DOMSVGAnimatedLengthList, and are
72 * responsible for nulling out our DOMSVGAnimatedLengthList's pointer to us
73 * when we die, essentially making its pointer to us a weak pointer. Similarly,
74 * our DOMSVGLength items are friends of us and responsible for nulling out our
77 * Our DOM items are created lazily on demand as and when script requests them.
79 class DOMSVGLengthList final
: public nsISupports
, public nsWrapperCache
{
81 friend class AutoChangeLengthListNotifier
;
82 friend class DOMSVGLength
;
85 // Our mAList's weak ref to us must be nulled out when we die. If GC has
86 // unlinked us using the cycle collector code, then that has already
87 // happened, and mAList is null.
89 (IsAnimValList() ? mAList
->mAnimVal
: mAList
->mBaseVal
) = nullptr;
94 NS_DECLARE_STATIC_IID_ACCESSOR(MOZILLA_DOMSVGLENGTHLIST_IID
)
95 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
96 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMSVGLengthList
)
98 DOMSVGLengthList(DOMSVGAnimatedLengthList
* aAList
,
99 const SVGLengthList
& aInternalList
)
101 // aInternalList must be passed in explicitly because we can't use
102 // InternalList() here. (Because it depends on IsAnimValList, which depends
103 // on this object having been assigned to aAList's mBaseVal or mAnimVal,
104 // which hasn't happened yet.)
106 InternalListLengthWillChange(aInternalList
.Length()); // Sync mItems
109 JSObject
* WrapObject(JSContext
* cx
,
110 JS::Handle
<JSObject
*> aGivenProto
) override
;
112 nsISupports
* GetParentObject() { return static_cast<nsIContent
*>(Element()); }
115 * This will normally be the same as InternalList().Length(), except if we've
116 * hit OOM in which case our length will be zero.
118 uint32_t LengthNoFlush() const {
120 mItems
.Length() == 0 || mItems
.Length() == InternalList().Length(),
121 "DOM wrapper's list length is out of sync");
122 return mItems
.Length();
125 /// Called to notify us to synchronize our length and detach excess items.
126 void InternalListLengthWillChange(uint32_t aNewLength
);
129 * Returns true if our attribute is animating (in which case our animVal is
130 * not simply a mirror of our baseVal).
132 bool IsAnimating() const { return mAList
->IsAnimating(); }
134 * Returns true if there is an animated list mirroring the base list.
136 bool AnimListMirrorsBaseList() const {
137 return mAList
->mAnimVal
&& !mAList
->IsAnimating();
140 uint32_t NumberOfItems() const {
141 if (IsAnimValList()) {
142 Element()->FlushAnimations();
144 return LengthNoFlush();
146 void Clear(ErrorResult
& aError
);
147 already_AddRefed
<DOMSVGLength
> Initialize(DOMSVGLength
& newItem
,
149 already_AddRefed
<DOMSVGLength
> GetItem(uint32_t index
, ErrorResult
& error
);
150 already_AddRefed
<DOMSVGLength
> IndexedGetter(uint32_t index
, bool& found
,
152 already_AddRefed
<DOMSVGLength
> InsertItemBefore(DOMSVGLength
& newItem
,
155 already_AddRefed
<DOMSVGLength
> ReplaceItem(DOMSVGLength
& newItem
,
158 already_AddRefed
<DOMSVGLength
> RemoveItem(uint32_t index
, ErrorResult
& error
);
159 already_AddRefed
<DOMSVGLength
> AppendItem(DOMSVGLength
& newItem
,
160 ErrorResult
& error
) {
161 return InsertItemBefore(newItem
, LengthNoFlush(), error
);
163 void IndexedSetter(uint32_t index
, DOMSVGLength
& newValue
,
165 uint32_t Length() const { return NumberOfItems(); }
168 dom::SVGElement
* Element() const { return mAList
->mElement
; }
170 uint8_t AttrEnum() const { return mAList
->mAttrEnum
; }
172 uint8_t Axis() const { return mAList
->mAxis
; }
174 /// Used to determine if this list is the baseVal or animVal list.
175 bool IsAnimValList() const {
176 MOZ_ASSERT(this == mAList
->mBaseVal
|| this == mAList
->mAnimVal
,
177 "Calling IsAnimValList() too early?!");
178 return this == mAList
->mAnimVal
;
182 * Get a reference to this object's corresponding internal SVGLengthList.
184 * To simplify the code we just have this one method for obtaining both
185 * baseVal and animVal internal lists. This means that animVal lists don't
186 * get const protection, but our setter methods guard against changing
189 SVGLengthList
& InternalList() const;
191 /// Returns the DOMSVGLength at aIndex, creating it if necessary.
192 already_AddRefed
<DOMSVGLength
> GetItemAt(uint32_t aIndex
);
194 void MaybeInsertNullInAnimValListAt(uint32_t aIndex
);
195 void MaybeRemoveItemFromAnimValListAt(uint32_t aIndex
);
197 // Weak refs to our DOMSVGLength items. The items are friends and take care
198 // of clearing our pointer to them when they die.
199 FallibleTArray
<DOMSVGLength
*> mItems
;
201 RefPtr
<DOMSVGAnimatedLengthList
> mAList
;
204 NS_DEFINE_STATIC_IID_ACCESSOR(DOMSVGLengthList
, MOZILLA_DOMSVGLENGTHLIST_IID
)
207 } // namespace mozilla
209 #endif // DOM_SVG_DOMSVGLENGTHLIST_H_