1/*
2 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB.  If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#ifndef SVGPropertyTearOff_h
21#define SVGPropertyTearOff_h
22
23#if ENABLE(SVG)
24#include "SVGAnimatedProperty.h"
25#include "SVGElement.h"
26#include "SVGProperty.h"
27#include <wtf/WeakPtr.h>
28
29namespace WebCore {
30
31class SVGPropertyTearOffBase : public SVGProperty {
32public:
33    virtual void detachWrapper() = 0;
34};
35
36template<typename PropertyType>
37class SVGPropertyTearOff : public SVGPropertyTearOffBase {
38public:
39    typedef SVGPropertyTearOff<PropertyType> Self;
40
41    // Used for child types (baseVal/animVal) of a SVGAnimated* property (for example: SVGAnimatedLength::baseVal()).
42    // Also used for list tear offs (for example: text.x.baseVal.getItem(0)).
43    static PassRefPtr<Self> create(SVGAnimatedProperty* animatedProperty, SVGPropertyRole role, PropertyType& value)
44    {
45        ASSERT(animatedProperty);
46        return adoptRef(new Self(animatedProperty, role, value));
47    }
48
49    // Used for non-animated POD types (for example: SVGSVGElement::createSVGLength()).
50    static PassRefPtr<Self> create(const PropertyType& initialValue)
51    {
52        return adoptRef(new Self(initialValue));
53    }
54
55    PropertyType& propertyReference() { return *m_value; }
56    SVGAnimatedProperty* animatedProperty() const { return m_animatedProperty; }
57
58    void setValue(PropertyType& value)
59    {
60        if (m_valueIsCopy) {
61            detachChildren();
62            delete m_value;
63        }
64        m_valueIsCopy = false;
65        m_value = &value;
66    }
67
68    void setAnimatedProperty(SVGAnimatedProperty* animatedProperty)
69    {
70        m_animatedProperty = animatedProperty;
71
72        if (m_animatedProperty)
73            m_contextElement = m_animatedProperty->contextElement();
74    }
75
76    SVGElement* contextElement() const
77    {
78        if (!m_animatedProperty || m_valueIsCopy)
79            return 0;
80        return m_contextElement.get();
81    }
82
83    void addChild(WeakPtr<SVGPropertyTearOffBase> child)
84    {
85        m_childTearOffs.append(child);
86    }
87
88    virtual void detachWrapper() override
89    {
90        if (m_valueIsCopy)
91            return;
92
93        detachChildren();
94
95        // Switch from a live value, to a non-live value.
96        // For example: <text x="50"/>
97        // var item = text.x.baseVal.getItem(0);
98        // text.setAttribute("x", "100");
99        // item.value still has to report '50' and it has to be possible to modify 'item'
100        // w/o changing the "new item" (with x=100) in the text element.
101        // Whenever the XML DOM modifies the "x" attribute, all existing wrappers are detached, using this function.
102        m_value = new PropertyType(*m_value);
103        m_valueIsCopy = true;
104        m_animatedProperty = 0;
105    }
106
107    virtual void commitChange()
108    {
109        if (!m_animatedProperty || m_valueIsCopy)
110            return;
111        m_animatedProperty->commitChange();
112    }
113
114    virtual bool isReadOnly() const
115    {
116        if (m_role == AnimValRole)
117            return true;
118        if (m_animatedProperty && m_animatedProperty->isReadOnly())
119            return true;
120        return false;
121    }
122
123protected:
124    SVGPropertyTearOff(SVGAnimatedProperty* animatedProperty, SVGPropertyRole role, PropertyType& value)
125        : m_animatedProperty(animatedProperty)
126        , m_role(role)
127        , m_value(&value)
128        , m_valueIsCopy(false)
129    {
130        // Using operator & is completely fine, as SVGAnimatedProperty owns this reference,
131        // and we're guaranteed to live as long as SVGAnimatedProperty does.
132
133        if (m_animatedProperty)
134            m_contextElement = m_animatedProperty->contextElement();
135    }
136
137    SVGPropertyTearOff(const PropertyType& initialValue)
138        : m_animatedProperty(0)
139        , m_role(UndefinedRole)
140        , m_value(new PropertyType(initialValue))
141        , m_valueIsCopy(true)
142    {
143    }
144
145    virtual ~SVGPropertyTearOff()
146    {
147        if (m_valueIsCopy) {
148            detachChildren();
149            delete m_value;
150        }
151    }
152
153    void detachChildren()
154    {
155        for (const auto& childTearOff : m_childTearOffs) {
156            if (childTearOff.get())
157                childTearOff.get()->detachWrapper();
158        }
159        m_childTearOffs.clear();
160    }
161
162    RefPtr<SVGElement> m_contextElement;
163    SVGAnimatedProperty* m_animatedProperty;
164    SVGPropertyRole m_role;
165    PropertyType* m_value;
166    Vector<WeakPtr<SVGPropertyTearOffBase>> m_childTearOffs;
167    bool m_valueIsCopy : 1;
168};
169
170}
171
172#endif // ENABLE(SVG)
173#endif // SVGPropertyTearOff_h
174