Merge mozilla-central to autoland on a CLOSED TREE
[gecko.git] / dom / svg / SVGAnimatedPathSegList.cpp
blob0333173e82e25eb46b836b928f9995e644227393
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 "SVGAnimatedPathSegList.h"
9 #include <utility>
11 #include "DOMSVGPathSegList.h"
12 #include "SVGPathSegListSMILType.h"
13 #include "mozilla/SMILValue.h"
14 #include "mozilla/dom/SVGElement.h"
16 using namespace mozilla::dom;
18 // See the comments in this file's header!
20 namespace mozilla {
22 nsresult SVGAnimatedPathSegList::SetBaseValueString(const nsAString& aValue) {
23 SVGPathData newBaseValue;
25 // The spec says that the path data is parsed and accepted up to the first
26 // error encountered, so we don't return early if an error occurs. However,
27 // we do want to throw any error code from setAttribute if there's a problem.
28 nsresult rv = newBaseValue.SetValueFromString(aValue);
30 // We must send these notifications *before* changing mBaseVal! Our baseVal's
31 // DOM wrapper list may have to remove DOM items from itself, and any removed
32 // DOM items need to copy their internal counterpart's values *before* we
33 // change them. See the comments in
34 // DOMSVGPathSegList::InternalListWillChangeTo().
35 DOMSVGPathSegList* baseValWrapper = nullptr;
36 DOMSVGPathSegList* animValWrapper = nullptr;
37 if (StaticPrefs::dom_svg_pathSeg_enabled()) {
38 baseValWrapper = DOMSVGPathSegList::GetDOMWrapperIfExists(GetBaseValKey());
39 if (baseValWrapper) {
40 baseValWrapper->InternalListWillChangeTo(newBaseValue);
43 if (!IsAnimating()) { // DOM anim val wraps our base val too!
44 animValWrapper =
45 DOMSVGPathSegList::GetDOMWrapperIfExists(GetAnimValKey());
46 if (animValWrapper) {
47 animValWrapper->InternalListWillChangeTo(newBaseValue);
52 // Only now may we modify mBaseVal!
54 // We don't need to call DidChange* here - we're only called by
55 // SVGElement::ParseAttribute under Element::SetAttr,
56 // which takes care of notifying.
58 mBaseVal.SwapWith(newBaseValue);
59 return rv;
62 void SVGAnimatedPathSegList::ClearBaseValue() {
63 if (StaticPrefs::dom_svg_pathSeg_enabled()) {
64 // We must send these notifications *before* changing mBaseVal! (See above.)
66 DOMSVGPathSegList* baseValWrapper =
67 DOMSVGPathSegList::GetDOMWrapperIfExists(GetBaseValKey());
68 if (baseValWrapper) {
69 baseValWrapper->InternalListWillChangeTo(SVGPathData());
72 if (!IsAnimating()) { // DOM anim val wraps our base val too!
73 DOMSVGPathSegList* animValWrapper =
74 DOMSVGPathSegList::GetDOMWrapperIfExists(GetAnimValKey());
75 if (animValWrapper) {
76 animValWrapper->InternalListWillChangeTo(SVGPathData());
81 mBaseVal.Clear();
82 // Caller notifies
85 nsresult SVGAnimatedPathSegList::SetAnimValue(const SVGPathData& aNewAnimValue,
86 SVGElement* aElement) {
87 // Note that a new animation may totally change the number of items in the
88 // animVal list, either replacing what was essentially a mirror of the
89 // baseVal list, or else replacing and overriding an existing animation.
90 // Unfortunately it is not possible for us to reliably distinguish between
91 // calls to this method that are setting a new sample for an existing
92 // animation, and calls that are setting the first sample of an animation
93 // that will override an existing animation. In the case of DOMSVGPathSegList
94 // the InternalListWillChangeTo method is not virtually free as it is for the
95 // other DOM list classes, so this is a shame. We'd quite like to be able to
96 // skip the call if possible.
98 if (StaticPrefs::dom_svg_pathSeg_enabled()) {
99 // We must send these notifications *before* changing mAnimVal! (See above.)
101 DOMSVGPathSegList* domWrapper =
102 DOMSVGPathSegList::GetDOMWrapperIfExists(GetAnimValKey());
103 if (domWrapper) {
104 domWrapper->InternalListWillChangeTo(aNewAnimValue);
107 if (!mAnimVal) {
108 mAnimVal = MakeUnique<SVGPathData>();
110 nsresult rv = mAnimVal->CopyFrom(aNewAnimValue);
111 if (NS_FAILED(rv)) {
112 // OOM. We clear the animation and, importantly, ClearAnimValue() ensures
113 // that mAnimVal's DOM wrapper (if any) is kept in sync!
114 ClearAnimValue(aElement);
116 aElement->DidAnimatePathSegList();
117 return rv;
120 void SVGAnimatedPathSegList::ClearAnimValue(SVGElement* aElement) {
121 if (StaticPrefs::dom_svg_pathSeg_enabled()) {
122 // We must send these notifications *before* changing mAnimVal! (See above.)
124 DOMSVGPathSegList* domWrapper =
125 DOMSVGPathSegList::GetDOMWrapperIfExists(GetAnimValKey());
126 if (domWrapper) {
127 // When all animation ends, animVal simply mirrors baseVal, which may have
128 // a different number of items to the last active animated value.
130 domWrapper->InternalListWillChangeTo(mBaseVal);
133 mAnimVal = nullptr;
134 aElement->DidAnimatePathSegList();
137 bool SVGAnimatedPathSegList::IsRendered() const {
138 return mAnimVal ? !mAnimVal->IsEmpty() : !mBaseVal.IsEmpty();
141 UniquePtr<SMILAttr> SVGAnimatedPathSegList::ToSMILAttr(SVGElement* aElement) {
142 return MakeUnique<SMILAnimatedPathSegList>(this, aElement);
145 nsresult SVGAnimatedPathSegList::SMILAnimatedPathSegList::ValueFromString(
146 const nsAString& aStr, const dom::SVGAnimationElement* /*aSrcElement*/,
147 SMILValue& aValue, bool& aPreventCachingOfSandwich) const {
148 SMILValue val(SVGPathSegListSMILType::Singleton());
149 SVGPathDataAndInfo* list = static_cast<SVGPathDataAndInfo*>(val.mU.mPtr);
150 nsresult rv = list->SetValueFromString(aStr);
151 if (NS_SUCCEEDED(rv)) {
152 list->SetElement(mElement);
153 aValue = std::move(val);
155 return rv;
158 SMILValue SVGAnimatedPathSegList::SMILAnimatedPathSegList::GetBaseValue()
159 const {
160 // To benefit from Return Value Optimization and avoid copy constructor calls
161 // due to our use of return-by-value, we must return the exact same object
162 // from ALL return points. This function must only return THIS variable:
163 SMILValue val;
165 SMILValue tmp(SVGPathSegListSMILType::Singleton());
166 auto* list = static_cast<SVGPathDataAndInfo*>(tmp.mU.mPtr);
167 nsresult rv = list->CopyFrom(mVal->mBaseVal);
168 if (NS_SUCCEEDED(rv)) {
169 list->SetElement(mElement);
170 val = std::move(tmp);
172 return val;
175 nsresult SVGAnimatedPathSegList::SMILAnimatedPathSegList::SetAnimValue(
176 const SMILValue& aValue) {
177 NS_ASSERTION(aValue.mType == SVGPathSegListSMILType::Singleton(),
178 "Unexpected type to assign animated value");
179 if (aValue.mType == SVGPathSegListSMILType::Singleton()) {
180 mVal->SetAnimValue(*static_cast<SVGPathDataAndInfo*>(aValue.mU.mPtr),
181 mElement);
183 return NS_OK;
186 void SVGAnimatedPathSegList::SMILAnimatedPathSegList::ClearAnimValue() {
187 if (mVal->mAnimVal) {
188 mVal->ClearAnimValue(mElement);
192 size_t SVGAnimatedPathSegList::SizeOfExcludingThis(
193 MallocSizeOf aMallocSizeOf) const {
194 size_t total = mBaseVal.SizeOfExcludingThis(aMallocSizeOf);
195 if (mAnimVal) {
196 mAnimVal->SizeOfIncludingThis(aMallocSizeOf);
198 return total;
201 } // namespace mozilla