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 "SVGAnimatedNumberList.h"
8
9 #include "DOMSVGAnimatedNumberList.h"
10 #include "mozilla/Move.h"
11 #include "nsSVGElement.h"
12 #include "nsSVGAttrTearoffTable.h"
13 #include "nsSMILValue.h"
14 #include "SVGNumberListSMILType.h"
15
16 namespace mozilla {
17
18 nsresult
SetBaseValueString(const nsAString & aValue)19 SVGAnimatedNumberList::SetBaseValueString(const nsAString& aValue)
20 {
21 SVGNumberList newBaseValue;
22 nsresult rv = newBaseValue.SetValueFromString(aValue);
23 if (NS_FAILED(rv)) {
24 return rv;
25 }
26
27 DOMSVGAnimatedNumberList *domWrapper =
28 DOMSVGAnimatedNumberList::GetDOMWrapperIfExists(this);
29 if (domWrapper) {
30 // We must send this notification *before* changing mBaseVal! If the length
31 // of our baseVal is being reduced, our baseVal's DOM wrapper list may have
32 // to remove DOM items from itself, and any removed DOM items need to copy
33 // their internal counterpart values *before* we change them.
34 //
35 domWrapper->InternalBaseValListWillChangeTo(newBaseValue);
36 }
37
38 // We don't need to call DidChange* here - we're only called by
39 // nsSVGElement::ParseAttribute under Element::SetAttr,
40 // which takes care of notifying.
41
42 mIsBaseSet = true;
43 rv = mBaseVal.CopyFrom(newBaseValue);
44 if (NS_FAILED(rv) && domWrapper) {
45 // Attempting to increase mBaseVal's length failed - reduce domWrapper
46 // back to the same length:
47 domWrapper->InternalBaseValListWillChangeTo(mBaseVal);
48 }
49 return rv;
50 }
51
52 void
ClearBaseValue(uint32_t aAttrEnum)53 SVGAnimatedNumberList::ClearBaseValue(uint32_t aAttrEnum)
54 {
55 DOMSVGAnimatedNumberList *domWrapper =
56 DOMSVGAnimatedNumberList::GetDOMWrapperIfExists(this);
57 if (domWrapper) {
58 // We must send this notification *before* changing mBaseVal! (See above.)
59 domWrapper->InternalBaseValListWillChangeTo(SVGNumberList());
60 }
61 mBaseVal.Clear();
62 mIsBaseSet = false;
63 // Caller notifies
64 }
65
66 nsresult
SetAnimValue(const SVGNumberList & aNewAnimValue,nsSVGElement * aElement,uint32_t aAttrEnum)67 SVGAnimatedNumberList::SetAnimValue(const SVGNumberList& aNewAnimValue,
68 nsSVGElement *aElement,
69 uint32_t aAttrEnum)
70 {
71 DOMSVGAnimatedNumberList *domWrapper =
72 DOMSVGAnimatedNumberList::GetDOMWrapperIfExists(this);
73 if (domWrapper) {
74 // A new animation may totally change the number of items in the animVal
75 // list, replacing what was essentially a mirror of the baseVal list, or
76 // else replacing and overriding an existing animation. When this happens
77 // we must try and keep our animVal's DOM wrapper in sync (see the comment
78 // in DOMSVGAnimatedNumberList::InternalBaseValListWillChangeTo).
79 //
80 // It's not possible for us to reliably distinguish between calls to this
81 // method that are setting a new sample for an existing animation, and
82 // calls that are setting the first sample of an animation that will
83 // override an existing animation. Happily it's cheap to just blindly
84 // notify our animVal's DOM wrapper of its internal counterpart's new value
85 // each time this method is called, so that's what we do.
86 //
87 // Note that we must send this notification *before* setting or changing
88 // mAnimVal! (See the comment in SetBaseValueString above.)
89 //
90 domWrapper->InternalAnimValListWillChangeTo(aNewAnimValue);
91 }
92 if (!mAnimVal) {
93 mAnimVal = new SVGNumberList();
94 }
95 nsresult rv = mAnimVal->CopyFrom(aNewAnimValue);
96 if (NS_FAILED(rv)) {
97 // OOM. We clear the animation, and, importantly, ClearAnimValue() ensures
98 // that mAnimVal and its DOM wrapper (if any) will have the same length!
99 ClearAnimValue(aElement, aAttrEnum);
100 return rv;
101 }
102 aElement->DidAnimateNumberList(aAttrEnum);
103 return NS_OK;
104 }
105
106 void
ClearAnimValue(nsSVGElement * aElement,uint32_t aAttrEnum)107 SVGAnimatedNumberList::ClearAnimValue(nsSVGElement *aElement,
108 uint32_t aAttrEnum)
109 {
110 DOMSVGAnimatedNumberList *domWrapper =
111 DOMSVGAnimatedNumberList::GetDOMWrapperIfExists(this);
112 if (domWrapper) {
113 // When all animation ends, animVal simply mirrors baseVal, which may have
114 // a different number of items to the last active animated value. We must
115 // keep the length of our animVal's DOM wrapper list in sync, and again we
116 // must do that before touching mAnimVal. See comments above.
117 //
118 domWrapper->InternalAnimValListWillChangeTo(mBaseVal);
119 }
120 mAnimVal = nullptr;
121 aElement->DidAnimateNumberList(aAttrEnum);
122 }
123
124 nsISMILAttr*
ToSMILAttr(nsSVGElement * aSVGElement,uint8_t aAttrEnum)125 SVGAnimatedNumberList::ToSMILAttr(nsSVGElement *aSVGElement,
126 uint8_t aAttrEnum)
127 {
128 return new SMILAnimatedNumberList(this, aSVGElement, aAttrEnum);
129 }
130
131 nsresult
132 SVGAnimatedNumberList::
ValueFromString(const nsAString & aStr,const dom::SVGAnimationElement *,nsSMILValue & aValue,bool & aPreventCachingOfSandwich) const133 SMILAnimatedNumberList::ValueFromString(const nsAString& aStr,
134 const dom::SVGAnimationElement* /*aSrcElement*/,
135 nsSMILValue& aValue,
136 bool& aPreventCachingOfSandwich) const
137 {
138 nsSMILValue val(&SVGNumberListSMILType::sSingleton);
139 SVGNumberListAndInfo *nlai = static_cast<SVGNumberListAndInfo*>(val.mU.mPtr);
140 nsresult rv = nlai->SetValueFromString(aStr);
141 if (NS_SUCCEEDED(rv)) {
142 nlai->SetInfo(mElement);
143 aValue = Move(val);
144 }
145 aPreventCachingOfSandwich = false;
146 return rv;
147 }
148
149 nsSMILValue
GetBaseValue() const150 SVGAnimatedNumberList::SMILAnimatedNumberList::GetBaseValue() const
151 {
152 // To benefit from Return Value Optimization and avoid copy constructor calls
153 // due to our use of return-by-value, we must return the exact same object
154 // from ALL return points. This function must only return THIS variable:
155 nsSMILValue val;
156
157 nsSMILValue tmp(&SVGNumberListSMILType::sSingleton);
158 SVGNumberListAndInfo *nlai = static_cast<SVGNumberListAndInfo*>(tmp.mU.mPtr);
159 nsresult rv = nlai->CopyFrom(mVal->mBaseVal);
160 if (NS_SUCCEEDED(rv)) {
161 nlai->SetInfo(mElement);
162 Swap(val, tmp);
163 }
164 return val;
165 }
166
167 nsresult
SetAnimValue(const nsSMILValue & aValue)168 SVGAnimatedNumberList::SMILAnimatedNumberList::SetAnimValue(const nsSMILValue& aValue)
169 {
170 NS_ASSERTION(aValue.mType == &SVGNumberListSMILType::sSingleton,
171 "Unexpected type to assign animated value");
172 if (aValue.mType == &SVGNumberListSMILType::sSingleton) {
173 mVal->SetAnimValue(*static_cast<SVGNumberListAndInfo*>(aValue.mU.mPtr),
174 mElement,
175 mAttrEnum);
176 }
177 return NS_OK;
178 }
179
180 void
ClearAnimValue()181 SVGAnimatedNumberList::SMILAnimatedNumberList::ClearAnimValue()
182 {
183 if (mVal->mAnimVal) {
184 mVal->ClearAnimValue(mElement, mAttrEnum);
185 }
186 }
187
188 } // namespace mozilla
189