Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / properties / SVGAnimatedProperty.h
1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #ifndef SVGAnimatedProperty_h
32 #define SVGAnimatedProperty_h
33
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"
43
44 namespace WebCore {
45
46 class SVGElement;
47
48 class SVGAnimatedPropertyBase : public RefCounted<SVGAnimatedPropertyBase> {
49 public:
50     virtual ~SVGAnimatedPropertyBase();
51
52     virtual SVGPropertyBase* currentValueBase() = 0;
53
54     virtual void animationStarted();
55     virtual PassRefPtr<SVGPropertyBase> createAnimatedValue() = 0;
56     virtual void setAnimatedValue(PassRefPtr<SVGPropertyBase>) = 0;
57     virtual void animationEnded();
58
59     virtual bool needsSynchronizeAttribute() = 0;
60     virtual void synchronizeAttribute();
61
62     AnimatedPropertyType type() const
63     {
64         return m_type;
65     }
66
67     SVGElement* contextElement() const
68     {
69         return m_contextElement;
70     }
71
72     const QualifiedName& attributeName() const
73     {
74         return m_attributeName;
75     }
76
77     bool isAnimating() const
78     {
79         return m_isAnimating;
80     }
81
82     bool isReadOnly() const
83     {
84         return m_isReadOnly;
85     }
86
87     void setReadOnly()
88     {
89         m_isReadOnly = true;
90     }
91
92     bool isSpecified() const;
93
94 protected:
95     SVGAnimatedPropertyBase(AnimatedPropertyType, SVGElement*, const QualifiedName& attributeName);
96
97 private:
98     const AnimatedPropertyType m_type;
99     bool m_isReadOnly;
100     bool m_isAnimating;
101
102     // This reference is kept alive from V8 wrapper
103     SVGElement* m_contextElement;
104
105     const QualifiedName& m_attributeName;
106
107     WTF_MAKE_NONCOPYABLE(SVGAnimatedPropertyBase);
108 };
109
110 template <typename Property>
111 class SVGAnimatedPropertyCommon : public SVGAnimatedPropertyBase {
112 public:
113     Property* baseValue()
114     {
115         return m_baseValue.get();
116     }
117
118     Property* currentValue()
119     {
120         return m_currentValue ? m_currentValue.get() : m_baseValue.get();
121     }
122
123     const Property* currentValue() const
124     {
125         return const_cast<SVGAnimatedPropertyCommon*>(this)->currentValue();
126     }
127
128     virtual SVGPropertyBase* currentValueBase() OVERRIDE
129     {
130         return currentValue();
131     }
132
133     void setBaseValueAsString(const String& value, SVGParsingError& parseError)
134     {
135         TrackExceptionState es;
136
137         m_baseValue->setValueAsString(value, es);
138
139         if (es.hadException())
140             parseError = ParsingAttributeFailedError;
141     }
142
143     virtual PassRefPtr<SVGPropertyBase> createAnimatedValue() OVERRIDE
144     {
145         return m_baseValue->clone();
146     }
147
148     virtual void setAnimatedValue(PassRefPtr<SVGPropertyBase> passValue) OVERRIDE
149     {
150         ASSERT(isAnimating());
151
152         RefPtr<SVGPropertyBase> value = passValue;
153         ASSERT(value->type() == Property::classType());
154         m_currentValue = static_pointer_cast<Property>(value.release());
155     }
156
157     virtual void animationEnded() OVERRIDE
158     {
159         SVGAnimatedPropertyBase::animationEnded();
160
161         ASSERT(m_currentValue);
162         m_currentValue.clear();
163     }
164
165 protected:
166     SVGAnimatedPropertyCommon(SVGElement* contextElement, const QualifiedName& attributeName, PassRefPtr<Property> initialValue)
167         : SVGAnimatedPropertyBase(Property::classType(), contextElement, attributeName)
168         , m_baseValue(initialValue)
169     {
170     }
171
172 private:
173     RefPtr<Property> m_baseValue;
174     RefPtr<Property> m_currentValue;
175 };
176
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> {
182 public:
183     static PassRefPtr<SVGAnimatedProperty<Property> > create(SVGElement* contextElement, const QualifiedName& attributeName, PassRefPtr<Property> initialValue)
184     {
185         return adoptRef(new SVGAnimatedProperty<Property>(contextElement, attributeName, initialValue));
186     }
187
188     virtual bool needsSynchronizeAttribute() OVERRIDE
189     {
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();
193     }
194
195     virtual void synchronizeAttribute() OVERRIDE
196     {
197         SVGAnimatedPropertyBase::synchronizeAttribute();
198         m_baseValueUpdated = false;
199     }
200
201     // SVGAnimated* DOM Spec implementations:
202
203     // baseVal()/setBaseVal()/animVal() are only to be used from SVG DOM implementation.
204     // Use currentValue() from C++ code.
205     PrimitiveType baseVal()
206     {
207         return this->baseValue()->value();
208     }
209
210     void setBaseVal(PrimitiveType value, WebCore::ExceptionState& exceptionState)
211     {
212         if (this->isReadOnly()) {
213             exceptionState.throwDOMException(NoModificationAllowedError, "The attribute is read-only.");
214             return;
215         }
216
217         this->baseValue()->setValue(value);
218         m_baseValueUpdated = true;
219
220         ASSERT(this->attributeName() != nullQName());
221         this->contextElement()->invalidateSVGAttributes();
222         this->contextElement()->svgAttributeChanged(this->attributeName());
223     }
224
225     PrimitiveType animVal()
226     {
227         return this->currentValue()->value();
228     }
229
230 protected:
231     SVGAnimatedProperty(SVGElement* contextElement, const QualifiedName& attributeName, PassRefPtr<Property> initialValue)
232         : SVGAnimatedPropertyCommon<Property>(contextElement, attributeName, initialValue)
233         , m_baseValueUpdated(false)
234     {
235     }
236
237     bool m_baseValueUpdated;
238 };
239
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> {
246 public:
247     static PassRefPtr<SVGAnimatedProperty<Property> > create(SVGElement* contextElement, const QualifiedName& attributeName, PassRefPtr<Property> initialValue)
248     {
249         return adoptRef(new SVGAnimatedProperty<Property>(contextElement, attributeName, initialValue));
250     }
251
252     virtual void setAnimatedValue(PassRefPtr<SVGPropertyBase> value) OVERRIDE
253     {
254         SVGAnimatedPropertyCommon<Property>::setAnimatedValue(value);
255         updateAnimValTearOffIfNeeded();
256     }
257
258     virtual void animationEnded() OVERRIDE
259     {
260         SVGAnimatedPropertyCommon<Property>::animationEnded();
261         updateAnimValTearOffIfNeeded();
262     }
263
264     virtual bool needsSynchronizeAttribute() OVERRIDE
265     {
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();
269     }
270
271     // SVGAnimated* DOM Spec implementations:
272
273     // baseVal()/animVal() are only to be used from SVG DOM implementation.
274     // Use currentValue() from C++ code.
275     virtual TearOffType* baseVal()
276     {
277         if (!m_baseValTearOff) {
278             m_baseValTearOff = TearOffType::create(this->baseValue(), this->contextElement(), PropertyIsNotAnimVal, this->attributeName());
279             if (this->isReadOnly())
280                 m_baseValTearOff->setIsReadOnlyProperty();
281         }
282
283         return m_baseValTearOff.get();
284     }
285
286     TearOffType* animVal()
287     {
288         if (!m_animValTearOff)
289             m_animValTearOff = TearOffType::create(this->currentValue(), this->contextElement(), PropertyIsAnimVal, this->attributeName());
290
291         return m_animValTearOff.get();
292     }
293
294 protected:
295     SVGAnimatedProperty(SVGElement* contextElement, const QualifiedName& attributeName, PassRefPtr<Property> initialValue)
296         : SVGAnimatedPropertyCommon<Property>(contextElement, attributeName, initialValue)
297     {
298     }
299
300 private:
301     void updateAnimValTearOffIfNeeded()
302     {
303         if (m_animValTearOff)
304             m_animValTearOff->setTarget(this->currentValue());
305     }
306
307     // When still (not animated):
308     //     Both m_animValTearOff and m_baseValTearOff target m_baseValue.
309     // When animated:
310     //     m_animValTearOff targets m_currentValue.
311     //     m_baseValTearOff targets m_baseValue.
312     RefPtr<TearOffType> m_baseValTearOff;
313     RefPtr<TearOffType> m_animValTearOff;
314 };
315
316 }
317
318 #endif // SVGAnimatedProperty_h