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