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 *     * 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 SVGPropertyTearOff_h
32#define SVGPropertyTearOff_h
33
34#include "core/dom/QualifiedName.h"
35#include "core/svg/properties/SVGProperty.h"
36#include "wtf/RefCounted.h"
37
38namespace blink {
39
40enum PropertyIsAnimValType {
41    PropertyIsNotAnimVal,
42    PropertyIsAnimVal
43};
44
45class SVGPropertyTearOffBase : public RefCounted<SVGPropertyTearOffBase> {
46public:
47    virtual ~SVGPropertyTearOffBase() { }
48
49    PropertyIsAnimValType propertyIsAnimVal() const
50    {
51        return m_propertyIsAnimVal;
52    }
53
54    bool isAnimVal() const
55    {
56        return m_propertyIsAnimVal == PropertyIsAnimVal;
57    }
58
59    bool isReadOnlyProperty() const
60    {
61        return m_isReadOnlyProperty;
62    }
63
64    void setIsReadOnlyProperty()
65    {
66        m_isReadOnlyProperty = true;
67    }
68
69    bool isImmutable() const
70    {
71        return isReadOnlyProperty() || isAnimVal();
72    }
73
74    virtual void commitChange();
75
76    SVGElement* contextElement()
77    {
78        return m_contextElement;
79    }
80
81    const QualifiedName& attributeName()
82    {
83        return m_attributeName;
84    }
85
86    void attachToSVGElementAttribute(SVGElement* contextElement, const QualifiedName& attributeName)
87    {
88        ASSERT(!isImmutable());
89        ASSERT(contextElement);
90        ASSERT(attributeName != QualifiedName::null());
91        m_contextElement = contextElement;
92        m_attributeName = attributeName;
93    }
94
95    virtual AnimatedPropertyType type() const = 0;
96
97protected:
98    SVGPropertyTearOffBase(SVGElement* contextElement, PropertyIsAnimValType propertyIsAnimVal, const QualifiedName& attributeName = QualifiedName::null())
99        : m_contextElement(contextElement)
100        , m_propertyIsAnimVal(propertyIsAnimVal)
101        , m_isReadOnlyProperty(false)
102        , m_attributeName(attributeName)
103    {
104    }
105
106private:
107    // These references are kept alive from V8 wrapper to prevent reference cycles
108    SVGElement* m_contextElement;
109
110    PropertyIsAnimValType m_propertyIsAnimVal;
111    bool m_isReadOnlyProperty;
112    QualifiedName m_attributeName;
113};
114
115template <typename Property>
116class SVGPropertyTearOff : public SVGPropertyTearOffBase {
117public:
118    Property* target()
119    {
120        return m_target.get();
121    }
122
123    void setTarget(PassRefPtr<Property> target)
124    {
125        m_target = target;
126    }
127
128    virtual AnimatedPropertyType type() const OVERRIDE
129    {
130        return Property::classType();
131    }
132
133protected:
134    SVGPropertyTearOff(PassRefPtr<Property> target, SVGElement* contextElement, PropertyIsAnimValType propertyIsAnimVal, const QualifiedName& attributeName = QualifiedName::null())
135        : SVGPropertyTearOffBase(contextElement, propertyIsAnimVal, attributeName)
136        , m_target(target)
137    {
138        ASSERT(m_target);
139    }
140
141private:
142    RefPtr<Property> m_target;
143};
144
145}
146
147#endif // SVGPropertyTearOff_h
148