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 : #include "DOMSVGNumber.h"
8 : #include "DOMSVGNumberList.h"
9 : #include "DOMSVGAnimatedNumberList.h"
10 : #include "SVGAnimatedNumberList.h"
11 : #include "nsSVGElement.h"
12 : #include "nsError.h"
13 : #include "nsContentUtils.h" // for NS_ENSURE_FINITE
14 : #include "mozilla/dom/SVGNumberBinding.h"
15 :
16 : // See the architecture comment in DOMSVGAnimatedNumberList.h.
17 :
18 : namespace mozilla {
19 :
20 : // We could use NS_IMPL_CYCLE_COLLECTION(, except that in Unlink() we need to
21 : // clear our list's weak ref to us to be safe. (The other option would be to
22 : // not unlink and rely on the breaking of the other edges in the cycle, as
23 : // NS_SVG_VAL_IMPL_CYCLE_COLLECTION does.)
24 : NS_IMPL_CYCLE_COLLECTION_CLASS(DOMSVGNumber)
25 :
26 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(DOMSVGNumber)
27 : // We may not belong to a list, so we must null check tmp->mList.
28 0 : if (tmp->mList) {
29 0 : tmp->mList->mItems[tmp->mListIndex] = nullptr;
30 : }
31 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
32 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK(mList)
33 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent)
34 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_END
35 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(DOMSVGNumber)
36 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mList)
37 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent)
38 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
39 0 : NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(DOMSVGNumber)
40 0 : NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
41 0 : NS_IMPL_CYCLE_COLLECTION_TRACE_END
42 :
43 0 : NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMSVGNumber)
44 0 : NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMSVGNumber)
45 :
46 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMSVGNumber)
47 0 : NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
48 0 : NS_INTERFACE_MAP_ENTRY(nsISupports)
49 0 : NS_INTERFACE_MAP_END
50 :
51 : //----------------------------------------------------------------------
52 : // Helper class: AutoChangeNumberNotifier
53 : // Stack-based helper class to pair calls to WillChangeNumberList and
54 : // DidChangeNumberList.
55 : class MOZ_RAII AutoChangeNumberNotifier
56 : {
57 : public:
58 0 : explicit AutoChangeNumberNotifier(DOMSVGNumber* aNumber MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
59 0 : : mNumber(aNumber)
60 : {
61 0 : MOZ_GUARD_OBJECT_NOTIFIER_INIT;
62 0 : MOZ_ASSERT(mNumber, "Expecting non-null number");
63 0 : MOZ_ASSERT(mNumber->HasOwner(),
64 : "Expecting list to have an owner for notification");
65 : mEmptyOrOldValue =
66 0 : mNumber->Element()->WillChangeNumberList(mNumber->mAttrEnum);
67 0 : }
68 :
69 0 : ~AutoChangeNumberNotifier()
70 0 : {
71 0 : mNumber->Element()->DidChangeNumberList(mNumber->mAttrEnum,
72 0 : mEmptyOrOldValue);
73 0 : if (mNumber->mList->IsAnimating()) {
74 0 : mNumber->Element()->AnimationNeedsResample();
75 : }
76 0 : }
77 :
78 : private:
79 : DOMSVGNumber* const mNumber;
80 : nsAttrValue mEmptyOrOldValue;
81 : MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
82 : };
83 :
84 0 : DOMSVGNumber::DOMSVGNumber(DOMSVGNumberList *aList,
85 : uint8_t aAttrEnum,
86 : uint32_t aListIndex,
87 0 : bool aIsAnimValItem)
88 : : mList(aList)
89 : , mParent(aList)
90 : , mListIndex(aListIndex)
91 : , mAttrEnum(aAttrEnum)
92 : , mIsAnimValItem(aIsAnimValItem)
93 0 : , mValue(0.0f)
94 : {
95 : // These shifts are in sync with the members in the header.
96 0 : MOZ_ASSERT(aList &&
97 : aAttrEnum < (1 << 4) &&
98 : aListIndex <= MaxListIndex(),
99 : "bad arg");
100 :
101 0 : MOZ_ASSERT(IndexIsValid(), "Bad index for DOMSVGNumber!");
102 0 : }
103 :
104 0 : DOMSVGNumber::DOMSVGNumber(nsISupports* aParent)
105 : : mList(nullptr)
106 : , mParent(aParent)
107 : , mListIndex(0)
108 : , mAttrEnum(0)
109 : , mIsAnimValItem(false)
110 0 : , mValue(0.0f)
111 : {
112 0 : }
113 :
114 : /* static */ already_AddRefed<DOMSVGNumber>
115 0 : DOMSVGNumber::Constructor(const dom::GlobalObject& aGlobal, ErrorResult& aRv)
116 : {
117 : nsCOMPtr<nsPIDOMWindowInner> window =
118 0 : do_QueryInterface(aGlobal.GetAsSupports());
119 0 : if (!window) {
120 0 : aRv.Throw(NS_ERROR_UNEXPECTED);
121 0 : return nullptr;
122 : }
123 :
124 0 : RefPtr<DOMSVGNumber> number = new DOMSVGNumber(window);
125 0 : return number.forget();
126 : }
127 :
128 : /* static */ already_AddRefed<DOMSVGNumber>
129 0 : DOMSVGNumber::Constructor(const dom::GlobalObject& aGlobal, float aValue,
130 : ErrorResult& aRv)
131 : {
132 : nsCOMPtr<nsPIDOMWindowInner> window =
133 0 : do_QueryInterface(aGlobal.GetAsSupports());
134 0 : if (!window) {
135 0 : aRv.Throw(NS_ERROR_UNEXPECTED);
136 0 : return nullptr;
137 : }
138 :
139 0 : RefPtr<DOMSVGNumber> number = new DOMSVGNumber(window);
140 0 : number->SetValue(aValue, aRv);
141 0 : return number.forget();
142 : }
143 :
144 : float
145 0 : DOMSVGNumber::Value()
146 : {
147 0 : if (mIsAnimValItem && HasOwner()) {
148 0 : Element()->FlushAnimations(); // May make HasOwner() == false
149 : }
150 0 : return HasOwner() ? InternalItem() : mValue;
151 : }
152 :
153 : void
154 0 : DOMSVGNumber::SetValue(float aValue, ErrorResult& aRv)
155 : {
156 0 : if (mIsAnimValItem) {
157 0 : aRv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
158 0 : return;
159 : }
160 :
161 0 : if (HasOwner()) {
162 0 : if (InternalItem() == aValue) {
163 0 : return;
164 : }
165 0 : AutoChangeNumberNotifier notifier(this);
166 0 : InternalItem() = aValue;
167 0 : return;
168 : }
169 :
170 0 : mValue = aValue;
171 : }
172 :
173 : void
174 0 : DOMSVGNumber::InsertingIntoList(DOMSVGNumberList *aList,
175 : uint8_t aAttrEnum,
176 : uint32_t aListIndex,
177 : bool aIsAnimValItem)
178 : {
179 0 : NS_ASSERTION(!HasOwner(), "Inserting item that is already in a list");
180 :
181 0 : mList = aList;
182 0 : mAttrEnum = aAttrEnum;
183 0 : mListIndex = aListIndex;
184 0 : mIsAnimValItem = aIsAnimValItem;
185 :
186 0 : MOZ_ASSERT(IndexIsValid(), "Bad index for DOMSVGNumber!");
187 0 : }
188 :
189 : void
190 0 : DOMSVGNumber::RemovingFromList()
191 : {
192 0 : mValue = InternalItem();
193 0 : mList = nullptr;
194 0 : mIsAnimValItem = false;
195 0 : }
196 :
197 : float
198 0 : DOMSVGNumber::ToSVGNumber()
199 : {
200 0 : return HasOwner() ? InternalItem() : mValue;
201 : }
202 :
203 : float&
204 0 : DOMSVGNumber::InternalItem()
205 : {
206 0 : SVGAnimatedNumberList *alist = Element()->GetAnimatedNumberList(mAttrEnum);
207 0 : return mIsAnimValItem && alist->mAnimVal ?
208 0 : (*alist->mAnimVal)[mListIndex] :
209 0 : alist->mBaseVal[mListIndex];
210 : }
211 :
212 : #ifdef DEBUG
213 : bool
214 0 : DOMSVGNumber::IndexIsValid()
215 : {
216 0 : SVGAnimatedNumberList *alist = Element()->GetAnimatedNumberList(mAttrEnum);
217 0 : return (mIsAnimValItem &&
218 0 : mListIndex < alist->GetAnimValue().Length()) ||
219 0 : (!mIsAnimValItem &&
220 0 : mListIndex < alist->GetBaseValue().Length());
221 : }
222 : #endif
223 :
224 : JSObject*
225 0 : DOMSVGNumber::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
226 : {
227 0 : return dom::SVGNumberBinding::Wrap(aCx, this, aGivenProto);
228 : }
229 :
230 : } // namespace mozilla
|