#ifndef SVGAnimatedProperty_h
#define SVGAnimatedProperty_h
#include "bindings/v8/ExceptionStatePlaceholder.h"
#include "bindings/v8/ScriptWrappable.h"
#include "core/dom/ExceptionCode.h"
#include "core/svg/SVGParsingError.h"
#include "core/svg/properties/SVGPropertyInfo.h"
#include "core/svg/properties/SVGPropertyTearOff.h"
#include "wtf/Noncopyable.h"
#include "wtf/PassRefPtr.h"
#include "wtf/RefCounted.h"
namespace WebCore {
class SVGElement;
class SVGAnimatedPropertyBase : public RefCounted<SVGAnimatedPropertyBase> {
public:
virtual ~SVGAnimatedPropertyBase();
virtual SVGPropertyBase* currentValueBase() = 0;
virtual void animationStarted();
virtual PassRefPtr<SVGPropertyBase> createAnimatedValue() = 0;
virtual void setAnimatedValue(PassRefPtr<SVGPropertyBase>) = 0;
virtual void animationEnded();
virtual bool needsSynchronizeAttribute() = 0;
virtual void synchronizeAttribute();
AnimatedPropertyType type() const
{
return m_type;
}
SVGElement* contextElement() const
{
return m_contextElement;
}
const QualifiedName& attributeName() const
{
return m_attributeName;
}
bool isAnimating() const
{
return m_isAnimating;
}
bool isReadOnly() const
{
return m_isReadOnly;
}
void setReadOnly()
{
m_isReadOnly = true;
}
bool isSpecified() const;
protected:
SVGAnimatedPropertyBase(AnimatedPropertyType, SVGElement*, const QualifiedName& attributeName);
private:
const AnimatedPropertyType m_type;
bool m_isReadOnly;
bool m_isAnimating;
SVGElement* m_contextElement;
const QualifiedName& m_attributeName;
WTF_MAKE_NONCOPYABLE(SVGAnimatedPropertyBase);
};
template <typename Property>
class SVGAnimatedPropertyCommon : public SVGAnimatedPropertyBase {
public:
Property* baseValue()
{
return m_baseValue.get();
}
Property* currentValue()
{
return m_currentValue ? m_currentValue.get() : m_baseValue.get();
}
const Property* currentValue() const
{
return const_cast<SVGAnimatedPropertyCommon*>(this)->currentValue();
}
virtual SVGPropertyBase* currentValueBase() OVERRIDE
{
return currentValue();
}
void setBaseValueAsString(const String& value, SVGParsingError& parseError)
{
TrackExceptionState es;
m_baseValue->setValueAsString(value, es);
if (es.hadException())
parseError = ParsingAttributeFailedError;
}
virtual PassRefPtr<SVGPropertyBase> createAnimatedValue() OVERRIDE
{
return m_baseValue->clone();
}
virtual void setAnimatedValue(PassRefPtr<SVGPropertyBase> passValue) OVERRIDE
{
ASSERT(isAnimating());
RefPtr<SVGPropertyBase> value = passValue;
ASSERT(value->type() == Property::classType());
m_currentValue = static_pointer_cast<Property>(value.release());
}
virtual void animationEnded() OVERRIDE
{
SVGAnimatedPropertyBase::animationEnded();
ASSERT(m_currentValue);
m_currentValue.clear();
}
protected:
SVGAnimatedPropertyCommon(SVGElement* contextElement, const QualifiedName& attributeName, PassRefPtr<Property> initialValue)
: SVGAnimatedPropertyBase(Property::classType(), contextElement, attributeName)
, m_baseValue(initialValue)
{
}
private:
RefPtr<Property> m_baseValue;
RefPtr<Property> m_currentValue;
};
template <typename Property, typename TearOffType = typename Property::TearOffType, typename PrimitiveType = typename Property::PrimitiveType>
class SVGAnimatedProperty : public SVGAnimatedPropertyCommon<Property> {
public:
static PassRefPtr<SVGAnimatedProperty<Property> > create(SVGElement* contextElement, const QualifiedName& attributeName, PassRefPtr<Property> initialValue)
{
return adoptRef(new SVGAnimatedProperty<Property>(contextElement, attributeName, initialValue));
}
virtual bool needsSynchronizeAttribute() OVERRIDE
{
return m_baseValueUpdated || this->isAnimating();
}
virtual void synchronizeAttribute() OVERRIDE
{
SVGAnimatedPropertyBase::synchronizeAttribute();
m_baseValueUpdated = false;
}
PrimitiveType baseVal()
{
return this->baseValue()->value();
}
void setBaseVal(PrimitiveType value, WebCore::ExceptionState& exceptionState)
{
if (this->isReadOnly()) {
exceptionState.throwDOMException(NoModificationAllowedError, "The attribute is read-only.");
return;
}
this->baseValue()->setValue(value);
m_baseValueUpdated = true;
ASSERT(this->attributeName() != nullQName());
this->contextElement()->invalidateSVGAttributes();
this->contextElement()->svgAttributeChanged(this->attributeName());
}
PrimitiveType animVal()
{
return this->currentValue()->value();
}
protected:
SVGAnimatedProperty(SVGElement* contextElement, const QualifiedName& attributeName, PassRefPtr<Property> initialValue)
: SVGAnimatedPropertyCommon<Property>(contextElement, attributeName, initialValue)
, m_baseValueUpdated(false)
{
}
bool m_baseValueUpdated;
};
template <typename Property, typename TearOffType>
class SVGAnimatedProperty<Property, TearOffType, void> : public SVGAnimatedPropertyCommon<Property> {
public:
static PassRefPtr<SVGAnimatedProperty<Property> > create(SVGElement* contextElement, const QualifiedName& attributeName, PassRefPtr<Property> initialValue)
{
return adoptRef(new SVGAnimatedProperty<Property>(contextElement, attributeName, initialValue));
}
virtual void setAnimatedValue(PassRefPtr<SVGPropertyBase> value) OVERRIDE
{
SVGAnimatedPropertyCommon<Property>::setAnimatedValue(value);
updateAnimValTearOffIfNeeded();
}
virtual void animationEnded() OVERRIDE
{
SVGAnimatedPropertyCommon<Property>::animationEnded();
updateAnimValTearOffIfNeeded();
}
virtual bool needsSynchronizeAttribute() OVERRIDE
{
return m_baseValTearOff || this->isAnimating();
}
virtual TearOffType* baseVal()
{
if (!m_baseValTearOff) {
m_baseValTearOff = TearOffType::create(this->baseValue(), this->contextElement(), PropertyIsNotAnimVal, this->attributeName());
if (this->isReadOnly())
m_baseValTearOff->setIsReadOnlyProperty();
}
return m_baseValTearOff.get();
}
TearOffType* animVal()
{
if (!m_animValTearOff)
m_animValTearOff = TearOffType::create(this->currentValue(), this->contextElement(), PropertyIsAnimVal, this->attributeName());
return m_animValTearOff.get();
}
protected:
SVGAnimatedProperty(SVGElement* contextElement, const QualifiedName& attributeName, PassRefPtr<Property> initialValue)
: SVGAnimatedPropertyCommon<Property>(contextElement, attributeName, initialValue)
{
}
private:
void updateAnimValTearOffIfNeeded()
{
if (m_animValTearOff)
m_animValTearOff->setTarget(this->currentValue());
}
RefPtr<TearOffType> m_baseValTearOff;
RefPtr<TearOffType> m_animValTearOff;
};
}
#endif