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/v8/ExceptionStatePlaceholder.h"
35 #include "bindings/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> {
50 virtual ~SVGAnimatedPropertyBase();
52 virtual SVGPropertyBase* currentValueBase() = 0;
54 virtual void animationStarted();
55 virtual PassRefPtr<SVGPropertyBase> createAnimatedValue() = 0;
56 virtual void setAnimatedValue(PassRefPtr<SVGPropertyBase>) = 0;
57 virtual void animationEnded();
59 virtual bool needsSynchronizeAttribute() = 0;
60 virtual void synchronizeAttribute();
62 AnimatedPropertyType type() const
67 SVGElement* contextElement() const
69 return m_contextElement;
72 const QualifiedName& attributeName() const
74 return m_attributeName;
77 bool isAnimating() const
82 bool isReadOnly() const
92 bool isSpecified() const;
95 SVGAnimatedPropertyBase(AnimatedPropertyType, SVGElement*, const QualifiedName& attributeName);
98 const AnimatedPropertyType m_type;
102 // This reference is kept alive from V8 wrapper
103 SVGElement* m_contextElement;
105 const QualifiedName& m_attributeName;
107 WTF_MAKE_NONCOPYABLE(SVGAnimatedPropertyBase);
110 template <typename Property>
111 class SVGAnimatedPropertyCommon : public SVGAnimatedPropertyBase {
113 Property* baseValue()
115 return m_baseValue.get();
118 Property* currentValue()
120 return m_currentValue ? m_currentValue.get() : m_baseValue.get();
123 const Property* currentValue() const
125 return const_cast<SVGAnimatedPropertyCommon*>(this)->currentValue();
128 virtual SVGPropertyBase* currentValueBase() OVERRIDE
130 return currentValue();
133 void setBaseValueAsString(const String& value, SVGParsingError& parseError)
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 ASSERT(isAnimating());
152 RefPtr<SVGPropertyBase> value = passValue;
153 ASSERT(value->type() == Property::classType());
154 m_currentValue = static_pointer_cast<Property>(value.release());
157 virtual void animationEnded() OVERRIDE
159 SVGAnimatedPropertyBase::animationEnded();
161 ASSERT(m_currentValue);
162 m_currentValue.clear();
166 SVGAnimatedPropertyCommon(SVGElement* contextElement, const QualifiedName& attributeName, PassRefPtr<Property> initialValue)
167 : SVGAnimatedPropertyBase(Property::classType(), contextElement, attributeName)
168 , m_baseValue(initialValue)
173 RefPtr<Property> m_baseValue;
174 RefPtr<Property> m_currentValue;
177 // Implementation of SVGAnimatedProperty which uses primitive types.
178 // This is for classes which return primitive type for its "animVal".
179 // Examples are SVGAnimatedBoolean, SVGAnimatedNumber, etc.
180 template <typename Property, typename TearOffType = typename Property::TearOffType, typename PrimitiveType = typename Property::PrimitiveType>
181 class SVGAnimatedProperty : public SVGAnimatedPropertyCommon<Property> {
183 static PassRefPtr<SVGAnimatedProperty<Property> > create(SVGElement* contextElement, const QualifiedName& attributeName, PassRefPtr<Property> initialValue)
185 return adoptRef(new SVGAnimatedProperty<Property>(contextElement, attributeName, initialValue));
188 virtual bool needsSynchronizeAttribute() OVERRIDE
190 // DOM attribute synchronization is only needed if tear-off is being touched from javascript or the property is being animated.
191 // This prevents unnecessary attribute creation on target element.
192 return m_baseValueUpdated || this->isAnimating();
195 virtual void synchronizeAttribute() OVERRIDE
197 SVGAnimatedPropertyBase::synchronizeAttribute();
198 m_baseValueUpdated = false;
201 // SVGAnimated* DOM Spec implementations:
203 // baseVal()/setBaseVal()/animVal() are only to be used from SVG DOM implementation.
204 // Use currentValue() from C++ code.
205 PrimitiveType baseVal()
207 return this->baseValue()->value();
210 void setBaseVal(PrimitiveType value, WebCore::ExceptionState& exceptionState)
212 if (this->isReadOnly()) {
213 exceptionState.throwDOMException(NoModificationAllowedError, "The attribute is read-only.");
217 this->baseValue()->setValue(value);
218 m_baseValueUpdated = true;
220 ASSERT(this->attributeName() != nullQName());
221 this->contextElement()->invalidateSVGAttributes();
222 this->contextElement()->svgAttributeChanged(this->attributeName());
225 PrimitiveType animVal()
227 return this->currentValue()->value();
231 SVGAnimatedProperty(SVGElement* contextElement, const QualifiedName& attributeName, PassRefPtr<Property> initialValue)
232 : SVGAnimatedPropertyCommon<Property>(contextElement, attributeName, initialValue)
233 , m_baseValueUpdated(false)
237 bool m_baseValueUpdated;
240 // Implementation of SVGAnimatedProperty which uses tear-off value types.
241 // These classes has "void" for its PrimitiveType.
242 // This is for classes which return special type for its "animVal".
243 // Examples are SVGAnimatedLength, SVGAnimatedRect, SVGAnimated*List, etc.
244 template <typename Property, typename TearOffType>
245 class SVGAnimatedProperty<Property, TearOffType, void> : public SVGAnimatedPropertyCommon<Property> {
247 static PassRefPtr<SVGAnimatedProperty<Property> > create(SVGElement* contextElement, const QualifiedName& attributeName, PassRefPtr<Property> initialValue)
249 return adoptRef(new SVGAnimatedProperty<Property>(contextElement, attributeName, initialValue));
252 virtual void setAnimatedValue(PassRefPtr<SVGPropertyBase> value) OVERRIDE
254 SVGAnimatedPropertyCommon<Property>::setAnimatedValue(value);
255 updateAnimValTearOffIfNeeded();
258 virtual void animationEnded() OVERRIDE
260 SVGAnimatedPropertyCommon<Property>::animationEnded();
261 updateAnimValTearOffIfNeeded();
264 virtual bool needsSynchronizeAttribute() OVERRIDE
266 // DOM attribute synchronization is only needed if tear-off is being touched from javascript or the property is being animated.
267 // This prevents unnecessary attribute creation on target element.
268 return m_baseValTearOff || this->isAnimating();
271 // SVGAnimated* DOM Spec implementations:
273 // baseVal()/animVal() are only to be used from SVG DOM implementation.
274 // Use currentValue() from C++ code.
275 virtual TearOffType* baseVal()
277 if (!m_baseValTearOff) {
278 m_baseValTearOff = TearOffType::create(this->baseValue(), this->contextElement(), PropertyIsNotAnimVal, this->attributeName());
279 if (this->isReadOnly())
280 m_baseValTearOff->setIsReadOnlyProperty();
283 return m_baseValTearOff.get();
286 TearOffType* animVal()
288 if (!m_animValTearOff)
289 m_animValTearOff = TearOffType::create(this->currentValue(), this->contextElement(), PropertyIsAnimVal, this->attributeName());
291 return m_animValTearOff.get();
295 SVGAnimatedProperty(SVGElement* contextElement, const QualifiedName& attributeName, PassRefPtr<Property> initialValue)
296 : SVGAnimatedPropertyCommon<Property>(contextElement, attributeName, initialValue)
301 void updateAnimValTearOffIfNeeded()
303 if (m_animValTearOff)
304 m_animValTearOff->setTarget(this->currentValue());
307 // When still (not animated):
308 // Both m_animValTearOff and m_baseValTearOff target m_baseValue.
310 // m_animValTearOff targets m_currentValue.
311 // m_baseValTearOff targets m_baseValue.
312 RefPtr<TearOffType> m_baseValTearOff;
313 RefPtr<TearOffType> m_animValTearOff;
318 #endif // SVGAnimatedProperty_h