Bug 1265584 [wpt PR 11167] - [Gecko Bug 1265584] Move wptrunner marionette usage...
[gecko.git] / dom / svg / DOMSVGNumber.cpp
blob60ccb2b05169eea9370b71222b2ea28f5c64139d
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 #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"
16 // See the architecture comment in DOMSVGAnimatedNumberList.h.
18 namespace mozilla {
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)
26 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(DOMSVGNumber)
27 // We may not belong to a list, so we must null check tmp->mList.
28 if (tmp->mList) {
29 tmp->mList->mItems[tmp->mListIndex] = nullptr;
31 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
32 NS_IMPL_CYCLE_COLLECTION_UNLINK(mList)
33 NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent)
34 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
35 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(DOMSVGNumber)
36 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mList)
37 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent)
38 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
39 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(DOMSVGNumber)
40 NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
41 NS_IMPL_CYCLE_COLLECTION_TRACE_END
43 NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMSVGNumber)
44 NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMSVGNumber)
46 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMSVGNumber)
47 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
48 NS_INTERFACE_MAP_ENTRY(nsISupports)
49 NS_INTERFACE_MAP_END
51 //----------------------------------------------------------------------
52 // Helper class: AutoChangeNumberNotifier
53 // Stack-based helper class to pair calls to WillChangeNumberList and
54 // DidChangeNumberList.
55 class MOZ_RAII AutoChangeNumberNotifier
57 public:
58 explicit AutoChangeNumberNotifier(DOMSVGNumber* aNumber MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
59 : mNumber(aNumber)
61 MOZ_GUARD_OBJECT_NOTIFIER_INIT;
62 MOZ_ASSERT(mNumber, "Expecting non-null number");
63 MOZ_ASSERT(mNumber->HasOwner(),
64 "Expecting list to have an owner for notification");
65 mEmptyOrOldValue =
66 mNumber->Element()->WillChangeNumberList(mNumber->mAttrEnum);
69 ~AutoChangeNumberNotifier()
71 mNumber->Element()->DidChangeNumberList(mNumber->mAttrEnum,
72 mEmptyOrOldValue);
73 if (mNumber->mList->IsAnimating()) {
74 mNumber->Element()->AnimationNeedsResample();
78 private:
79 DOMSVGNumber* const mNumber;
80 nsAttrValue mEmptyOrOldValue;
81 MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
84 DOMSVGNumber::DOMSVGNumber(DOMSVGNumberList *aList,
85 uint8_t aAttrEnum,
86 uint32_t aListIndex,
87 bool aIsAnimValItem)
88 : mList(aList)
89 , mParent(aList)
90 , mListIndex(aListIndex)
91 , mAttrEnum(aAttrEnum)
92 , mIsAnimValItem(aIsAnimValItem)
93 , mValue(0.0f)
95 // These shifts are in sync with the members in the header.
96 MOZ_ASSERT(aList &&
97 aAttrEnum < (1 << 4) &&
98 aListIndex <= MaxListIndex(),
99 "bad arg");
101 MOZ_ASSERT(IndexIsValid(), "Bad index for DOMSVGNumber!");
104 DOMSVGNumber::DOMSVGNumber(nsISupports* aParent)
105 : mList(nullptr)
106 , mParent(aParent)
107 , mListIndex(0)
108 , mAttrEnum(0)
109 , mIsAnimValItem(false)
110 , mValue(0.0f)
114 float
115 DOMSVGNumber::Value()
117 if (mIsAnimValItem && HasOwner()) {
118 Element()->FlushAnimations(); // May make HasOwner() == false
120 return HasOwner() ? InternalItem() : mValue;
123 void
124 DOMSVGNumber::SetValue(float aValue, ErrorResult& aRv)
126 if (mIsAnimValItem) {
127 aRv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
128 return;
131 if (HasOwner()) {
132 if (InternalItem() == aValue) {
133 return;
135 AutoChangeNumberNotifier notifier(this);
136 InternalItem() = aValue;
137 return;
140 mValue = aValue;
143 void
144 DOMSVGNumber::InsertingIntoList(DOMSVGNumberList *aList,
145 uint8_t aAttrEnum,
146 uint32_t aListIndex,
147 bool aIsAnimValItem)
149 NS_ASSERTION(!HasOwner(), "Inserting item that is already in a list");
151 mList = aList;
152 mAttrEnum = aAttrEnum;
153 mListIndex = aListIndex;
154 mIsAnimValItem = aIsAnimValItem;
156 MOZ_ASSERT(IndexIsValid(), "Bad index for DOMSVGNumber!");
159 void
160 DOMSVGNumber::RemovingFromList()
162 mValue = InternalItem();
163 mList = nullptr;
164 mIsAnimValItem = false;
167 float
168 DOMSVGNumber::ToSVGNumber()
170 return HasOwner() ? InternalItem() : mValue;
173 float&
174 DOMSVGNumber::InternalItem()
176 SVGAnimatedNumberList *alist = Element()->GetAnimatedNumberList(mAttrEnum);
177 return mIsAnimValItem && alist->mAnimVal ?
178 (*alist->mAnimVal)[mListIndex] :
179 alist->mBaseVal[mListIndex];
182 #ifdef DEBUG
183 bool
184 DOMSVGNumber::IndexIsValid()
186 SVGAnimatedNumberList *alist = Element()->GetAnimatedNumberList(mAttrEnum);
187 return (mIsAnimValItem &&
188 mListIndex < alist->GetAnimValue().Length()) ||
189 (!mIsAnimValItem &&
190 mListIndex < alist->GetBaseValue().Length());
192 #endif
194 JSObject*
195 DOMSVGNumber::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
197 return dom::SVGNumber_Binding::Wrap(aCx, this, aGivenProto);
200 } // namespace mozilla