| OLD | NEW |
| (Empty) |
| 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 NewSVGAnimatedProperty_h | |
| 32 #define NewSVGAnimatedProperty_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/NewSVGPropertyTearOff.h" | |
| 39 #include "core/svg/properties/SVGPropertyInfo.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 NewSVGAnimatedPropertyBase : public RefCounted<NewSVGAnimatedPropertyBase>
{ | |
| 49 public: | |
| 50 virtual ~NewSVGAnimatedPropertyBase(); | |
| 51 | |
| 52 virtual NewSVGPropertyBase* currentValueBase() = 0; | |
| 53 | |
| 54 virtual void animationStarted(); | |
| 55 virtual PassRefPtr<NewSVGPropertyBase> createAnimatedValue() = 0; | |
| 56 virtual void setAnimatedValue(PassRefPtr<NewSVGPropertyBase>) = 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 NewSVGAnimatedPropertyBase(AnimatedPropertyType, SVGElement*, const Qualifie
dName& attributeName); | |
| 96 void commitChange(); | |
| 97 | |
| 98 private: | |
| 99 const AnimatedPropertyType m_type; | |
| 100 bool m_isReadOnly; | |
| 101 bool m_isAnimating; | |
| 102 | |
| 103 // This reference is kept alive from V8 wrapper | |
| 104 SVGElement* m_contextElement; | |
| 105 | |
| 106 const QualifiedName& m_attributeName; | |
| 107 | |
| 108 WTF_MAKE_NONCOPYABLE(NewSVGAnimatedPropertyBase); | |
| 109 }; | |
| 110 | |
| 111 template <typename Property> | |
| 112 class NewSVGAnimatedPropertyCommon : public NewSVGAnimatedPropertyBase { | |
| 113 public: | |
| 114 Property* baseValue() | |
| 115 { | |
| 116 return m_baseValue.get(); | |
| 117 } | |
| 118 | |
| 119 Property* currentValue() | |
| 120 { | |
| 121 return m_currentValue ? m_currentValue.get() : m_baseValue.get(); | |
| 122 } | |
| 123 | |
| 124 const Property* currentValue() const | |
| 125 { | |
| 126 return const_cast<NewSVGAnimatedPropertyCommon*>(this)->currentValue(); | |
| 127 } | |
| 128 | |
| 129 virtual NewSVGPropertyBase* currentValueBase() OVERRIDE | |
| 130 { | |
| 131 return currentValue(); | |
| 132 } | |
| 133 | |
| 134 void setBaseValueAsString(const String& value, SVGParsingError& parseError) | |
| 135 { | |
| 136 TrackExceptionState es; | |
| 137 | |
| 138 m_baseValue->setValueAsString(value, es); | |
| 139 | |
| 140 if (es.hadException()) | |
| 141 parseError = ParsingAttributeFailedError; | |
| 142 } | |
| 143 | |
| 144 virtual PassRefPtr<NewSVGPropertyBase> createAnimatedValue() OVERRIDE | |
| 145 { | |
| 146 return m_baseValue->clone(); | |
| 147 } | |
| 148 | |
| 149 virtual void setAnimatedValue(PassRefPtr<NewSVGPropertyBase> passValue) OVER
RIDE | |
| 150 { | |
| 151 ASSERT(isAnimating()); | |
| 152 | |
| 153 RefPtr<NewSVGPropertyBase> value = passValue; | |
| 154 ASSERT(value->type() == Property::classType()); | |
| 155 m_currentValue = static_pointer_cast<Property>(value.release()); | |
| 156 } | |
| 157 | |
| 158 virtual void animationEnded() OVERRIDE | |
| 159 { | |
| 160 NewSVGAnimatedPropertyBase::animationEnded(); | |
| 161 | |
| 162 ASSERT(m_currentValue); | |
| 163 m_currentValue.clear(); | |
| 164 } | |
| 165 | |
| 166 protected: | |
| 167 NewSVGAnimatedPropertyCommon(SVGElement* contextElement, const QualifiedName
& attributeName, PassRefPtr<Property> initialValue) | |
| 168 : NewSVGAnimatedPropertyBase(Property::classType(), contextElement, attr
ibuteName) | |
| 169 , m_baseValue(initialValue) | |
| 170 { | |
| 171 } | |
| 172 | |
| 173 private: | |
| 174 RefPtr<Property> m_baseValue; | |
| 175 RefPtr<Property> m_currentValue; | |
| 176 }; | |
| 177 | |
| 178 // Implementation of SVGAnimatedProperty which uses primitive types. | |
| 179 // This is for classes which return primitive type for its "animVal". | |
| 180 // Examples are SVGAnimatedBoolean, SVGAnimatedNumber, etc. | |
| 181 template <typename Property, typename TearOffType = typename Property::TearOffTy
pe, typename PrimitiveType = typename Property::PrimitiveType> | |
| 182 class NewSVGAnimatedProperty : public NewSVGAnimatedPropertyCommon<Property> { | |
| 183 public: | |
| 184 static PassRefPtr<NewSVGAnimatedProperty<Property> > create(SVGElement* cont
extElement, const QualifiedName& attributeName, PassRefPtr<Property> initialValu
e) | |
| 185 { | |
| 186 return adoptRef(new NewSVGAnimatedProperty<Property>(contextElement, att
ributeName, initialValue)); | |
| 187 } | |
| 188 | |
| 189 virtual bool needsSynchronizeAttribute() OVERRIDE | |
| 190 { | |
| 191 // DOM attribute synchronization is only needed if tear-off is being tou
ched from javascript or the property is being animated. | |
| 192 // This prevents unnecessary attribute creation on target element. | |
| 193 return m_baseValueUpdated || this->isAnimating(); | |
| 194 } | |
| 195 | |
| 196 virtual void synchronizeAttribute() OVERRIDE | |
| 197 { | |
| 198 NewSVGAnimatedPropertyBase::synchronizeAttribute(); | |
| 199 m_baseValueUpdated = false; | |
| 200 } | |
| 201 | |
| 202 // SVGAnimated* DOM Spec implementations: | |
| 203 | |
| 204 // baseVal()/setBaseVal()/animVal() are only to be used from SVG DOM impleme
ntation. | |
| 205 // Use currentValue() from C++ code. | |
| 206 PrimitiveType baseVal() | |
| 207 { | |
| 208 return this->baseValue()->value(); | |
| 209 } | |
| 210 | |
| 211 void setBaseVal(PrimitiveType value, WebCore::ExceptionState& exceptionState
) | |
| 212 { | |
| 213 if (this->isReadOnly()) { | |
| 214 exceptionState.throwDOMException(NoModificationAllowedError, "The at
tribute is read-only."); | |
| 215 return; | |
| 216 } | |
| 217 | |
| 218 this->baseValue()->setValue(value); | |
| 219 m_baseValueUpdated = true; | |
| 220 | |
| 221 ASSERT(this->attributeName() != nullQName()); | |
| 222 this->contextElement()->invalidateSVGAttributes(); | |
| 223 this->contextElement()->svgAttributeChanged(this->attributeName()); | |
| 224 } | |
| 225 | |
| 226 PrimitiveType animVal() | |
| 227 { | |
| 228 return this->currentValue()->value(); | |
| 229 } | |
| 230 | |
| 231 protected: | |
| 232 NewSVGAnimatedProperty(SVGElement* contextElement, const QualifiedName& attr
ibuteName, PassRefPtr<Property> initialValue) | |
| 233 : NewSVGAnimatedPropertyCommon<Property>(contextElement, attributeName,
initialValue) | |
| 234 , m_baseValueUpdated(false) | |
| 235 { | |
| 236 } | |
| 237 | |
| 238 bool m_baseValueUpdated; | |
| 239 }; | |
| 240 | |
| 241 // Implementation of SVGAnimatedProperty which uses tear-off value types. | |
| 242 // These classes has "void" for its PrimitiveType. | |
| 243 // This is for classes which return special type for its "animVal". | |
| 244 // Examples are SVGAnimatedLength, SVGAnimatedRect, SVGAnimated*List, etc. | |
| 245 template <typename Property, typename TearOffType> | |
| 246 class NewSVGAnimatedProperty<Property, TearOffType, void> : public NewSVGAnimate
dPropertyCommon<Property> { | |
| 247 public: | |
| 248 static PassRefPtr<NewSVGAnimatedProperty<Property> > create(SVGElement* cont
extElement, const QualifiedName& attributeName, PassRefPtr<Property> initialValu
e) | |
| 249 { | |
| 250 return adoptRef(new NewSVGAnimatedProperty<Property>(contextElement, att
ributeName, initialValue)); | |
| 251 } | |
| 252 | |
| 253 virtual void setAnimatedValue(PassRefPtr<NewSVGPropertyBase> value) OVERRIDE | |
| 254 { | |
| 255 NewSVGAnimatedPropertyCommon<Property>::setAnimatedValue(value); | |
| 256 updateAnimValTearOffIfNeeded(); | |
| 257 } | |
| 258 | |
| 259 virtual void animationEnded() OVERRIDE | |
| 260 { | |
| 261 NewSVGAnimatedPropertyCommon<Property>::animationEnded(); | |
| 262 updateAnimValTearOffIfNeeded(); | |
| 263 } | |
| 264 | |
| 265 virtual bool needsSynchronizeAttribute() OVERRIDE | |
| 266 { | |
| 267 // DOM attribute synchronization is only needed if tear-off is being tou
ched from javascript or the property is being animated. | |
| 268 // This prevents unnecessary attribute creation on target element. | |
| 269 return m_baseValTearOff || this->isAnimating(); | |
| 270 } | |
| 271 | |
| 272 // SVGAnimated* DOM Spec implementations: | |
| 273 | |
| 274 // baseVal()/animVal() are only to be used from SVG DOM implementation. | |
| 275 // Use currentValue() from C++ code. | |
| 276 virtual TearOffType* baseVal() | |
| 277 { | |
| 278 if (!m_baseValTearOff) { | |
| 279 m_baseValTearOff = TearOffType::create(this->baseValue(), this->cont
extElement(), PropertyIsNotAnimVal, this->attributeName()); | |
| 280 if (this->isReadOnly()) | |
| 281 m_baseValTearOff->setIsReadOnlyProperty(); | |
| 282 } | |
| 283 | |
| 284 return m_baseValTearOff.get(); | |
| 285 } | |
| 286 | |
| 287 TearOffType* animVal() | |
| 288 { | |
| 289 if (!m_animValTearOff) | |
| 290 m_animValTearOff = TearOffType::create(this->currentValue(), this->c
ontextElement(), PropertyIsAnimVal, this->attributeName()); | |
| 291 | |
| 292 return m_animValTearOff.get(); | |
| 293 } | |
| 294 | |
| 295 protected: | |
| 296 NewSVGAnimatedProperty(SVGElement* contextElement, const QualifiedName& attr
ibuteName, PassRefPtr<Property> initialValue) | |
| 297 : NewSVGAnimatedPropertyCommon<Property>(contextElement, attributeName,
initialValue) | |
| 298 { | |
| 299 } | |
| 300 | |
| 301 private: | |
| 302 void updateAnimValTearOffIfNeeded() | |
| 303 { | |
| 304 if (m_animValTearOff) | |
| 305 m_animValTearOff->setTarget(this->currentValue()); | |
| 306 } | |
| 307 | |
| 308 // When still (not animated): | |
| 309 // Both m_animValTearOff and m_baseValTearOff target m_baseValue. | |
| 310 // When animated: | |
| 311 // m_animValTearOff targets m_currentValue. | |
| 312 // m_baseValTearOff targets m_baseValue. | |
| 313 RefPtr<TearOffType> m_baseValTearOff; | |
| 314 RefPtr<TearOffType> m_animValTearOff; | |
| 315 }; | |
| 316 | |
| 317 } | |
| 318 | |
| 319 #endif // NewSVGAnimatedProperty_h | |
| OLD | NEW |