2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 G* * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef SVGAnimatedProperty_h
32 #define SVGAnimatedProperty_h
34 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
35 #include "bindings/core/v8/ScriptWrappable.h"
36 #include "core/dom/ExceptionCode.h"
37 #include "core/svg/SVGParsingError.h"
38 #include "core/svg/properties/SVGPropertyInfo.h"
39 #include "core/svg/properties/SVGPropertyTearOff.h"
40 #include "wtf/Noncopyable.h"
41 #include "wtf/PassRefPtr.h"
42 #include "wtf/RefCounted.h"
48 class SVGAnimatedPropertyBase : public RefCounted<SVGAnimatedPropertyBase>, public ScriptWrappableBase {
50 virtual ~SVGAnimatedPropertyBase();
52 virtual SVGPropertyBase* currentValueBase() = 0;
53 virtual bool isAnimating() const = 0;
55 virtual PassRefPtr<SVGPropertyBase> createAnimatedValue() = 0;
56 virtual void setAnimatedValue(PassRefPtr<SVGPropertyBase>) = 0;
57 virtual void animationEnded();
59 virtual void setBaseValueAsString(const String& value, SVGParsingError& parseError) = 0;
60 virtual bool needsSynchronizeAttribute() = 0;
61 virtual void synchronizeAttribute();
63 AnimatedPropertyType type() const
68 SVGElement* contextElement() const
70 return m_contextElement;
73 const QualifiedName& attributeName() const
75 return m_attributeName;
78 bool isReadOnly() const
88 bool isSpecified() const;
91 SVGAnimatedPropertyBase(AnimatedPropertyType, SVGElement*, const QualifiedName& attributeName);
94 const AnimatedPropertyType m_type;
97 // This reference is kept alive from V8 wrapper
98 SVGElement* m_contextElement;
100 const QualifiedName& m_attributeName;
102 WTF_MAKE_NONCOPYABLE(SVGAnimatedPropertyBase);
105 template <typename Property>
106 class SVGAnimatedPropertyCommon : public SVGAnimatedPropertyBase {
108 Property* baseValue()
110 return m_baseValue.get();
113 Property* currentValue()
115 return m_currentValue ? m_currentValue.get() : m_baseValue.get();
118 const Property* currentValue() const
120 return const_cast<SVGAnimatedPropertyCommon*>(this)->currentValue();
123 virtual SVGPropertyBase* currentValueBase() override
125 return currentValue();
128 virtual bool isAnimating() const override
130 return m_currentValue;
133 void setBaseValueAsString(const String& value, SVGParsingError& parseError) override
135 TrackExceptionState es;
137 m_baseValue->setValueAsString(value, es);
139 if (es.hadException())
140 parseError = ParsingAttributeFailedError;
143 virtual PassRefPtr<SVGPropertyBase> createAnimatedValue() override
145 return m_baseValue->clone();
148 virtual void setAnimatedValue(PassRefPtr<SVGPropertyBase> passValue) override
150 RefPtr<SVGPropertyBase> value = passValue;
151 ASSERT(value->type() == Property::classType());
152 m_currentValue = static_pointer_cast<Property>(value.release());
155 virtual void animationEnded() override
157 m_currentValue.clear();
159 SVGAnimatedPropertyBase::animationEnded();
163 SVGAnimatedPropertyCommon(SVGElement* contextElement, const QualifiedName& attributeName, PassRefPtr<Property> initialValue)
164 : SVGAnimatedPropertyBase(Property::classType(), contextElement, attributeName)
165 , m_baseValue(initialValue)
170 RefPtr<Property> m_baseValue;
171 RefPtr<Property> m_currentValue;
174 // Implementation of SVGAnimatedProperty which uses primitive types.
175 // This is for classes which return primitive type for its "animVal".
176 // Examples are SVGAnimatedBoolean, SVGAnimatedNumber, etc.
177 template <typename Property, typename TearOffType = typename Property::TearOffType, typename PrimitiveType = typename Property::PrimitiveType>
178 class SVGAnimatedProperty : public SVGAnimatedPropertyCommon<Property> {
180 virtual bool needsSynchronizeAttribute() override
182 // DOM attribute synchronization is only needed if tear-off is being touched from javascript or the property is being animated.
183 // This prevents unnecessary attribute creation on target element.
184 return m_baseValueUpdated || this->isAnimating();
187 virtual void synchronizeAttribute() override
189 SVGAnimatedPropertyBase::synchronizeAttribute();
190 m_baseValueUpdated = false;
193 // SVGAnimated* DOM Spec implementations:
195 // baseVal()/setBaseVal()/animVal() are only to be used from SVG DOM implementation.
196 // Use currentValue() from C++ code.
197 PrimitiveType baseVal()
199 return this->baseValue()->value();
202 void setBaseVal(PrimitiveType value, blink::ExceptionState& exceptionState)
204 if (this->isReadOnly()) {
205 exceptionState.throwDOMException(NoModificationAllowedError, "The attribute is read-only.");
209 this->baseValue()->setValue(value);
210 m_baseValueUpdated = true;
212 ASSERT(this->attributeName() != QualifiedName::null());
213 this->contextElement()->invalidateSVGAttributes();
214 this->contextElement()->svgAttributeChanged(this->attributeName());
217 PrimitiveType animVal()
219 return this->currentValue()->value();
223 SVGAnimatedProperty(SVGElement* contextElement, const QualifiedName& attributeName, PassRefPtr<Property> initialValue)
224 : SVGAnimatedPropertyCommon<Property>(contextElement, attributeName, initialValue)
225 , m_baseValueUpdated(false)
229 bool m_baseValueUpdated;
232 // Implementation of SVGAnimatedProperty which uses tear-off value types.
233 // These classes has "void" for its PrimitiveType.
234 // This is for classes which return special type for its "animVal".
235 // Examples are SVGAnimatedLength, SVGAnimatedRect, SVGAnimated*List, etc.
236 template <typename Property, typename TearOffType>
237 class SVGAnimatedProperty<Property, TearOffType, void> : public SVGAnimatedPropertyCommon<Property> {
239 static PassRefPtr<SVGAnimatedProperty<Property> > create(SVGElement* contextElement, const QualifiedName& attributeName, PassRefPtr<Property> initialValue)
241 return adoptRef(new SVGAnimatedProperty<Property>(contextElement, attributeName, initialValue));
244 virtual void setAnimatedValue(PassRefPtr<SVGPropertyBase> value) override
246 SVGAnimatedPropertyCommon<Property>::setAnimatedValue(value);
247 updateAnimValTearOffIfNeeded();
250 virtual void animationEnded() override
252 SVGAnimatedPropertyCommon<Property>::animationEnded();
253 updateAnimValTearOffIfNeeded();
256 virtual bool needsSynchronizeAttribute() override
258 // DOM attribute synchronization is only needed if tear-off is being touched from javascript or the property is being animated.
259 // This prevents unnecessary attribute creation on target element.
260 return m_baseValTearOff || this->isAnimating();
263 // SVGAnimated* DOM Spec implementations:
265 // baseVal()/animVal() are only to be used from SVG DOM implementation.
266 // Use currentValue() from C++ code.
267 virtual TearOffType* baseVal()
269 if (!m_baseValTearOff) {
270 m_baseValTearOff = TearOffType::create(this->baseValue(), this->contextElement(), PropertyIsNotAnimVal, this->attributeName());
271 if (this->isReadOnly())
272 m_baseValTearOff->setIsReadOnlyProperty();
275 return m_baseValTearOff.get();
278 TearOffType* animVal()
280 if (!m_animValTearOff)
281 m_animValTearOff = TearOffType::create(this->currentValue(), this->contextElement(), PropertyIsAnimVal, this->attributeName());
283 return m_animValTearOff.get();
287 SVGAnimatedProperty(SVGElement* contextElement, const QualifiedName& attributeName, PassRefPtr<Property> initialValue)
288 : SVGAnimatedPropertyCommon<Property>(contextElement, attributeName, initialValue)
293 void updateAnimValTearOffIfNeeded()
295 if (m_animValTearOff)
296 m_animValTearOff->setTarget(this->currentValue());
299 // When still (not animated):
300 // Both m_animValTearOff and m_baseValTearOff target m_baseValue.
302 // m_animValTearOff targets m_currentValue.
303 // m_baseValTearOff targets m_baseValue.
304 RefPtr<TearOffType> m_baseValTearOff;
305 RefPtr<TearOffType> m_animValTearOff;
310 #endif // SVGAnimatedProperty_h