648d9ce8a700fae10234b17e1acac31d19768caf
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGElementRareData.h
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 SVGElementRareData_h
21 #define SVGElementRareData_h
22
23 #include "core/css/StylePropertySet.h"
24 #include "core/css/resolver/StyleResolver.h"
25 #include "wtf/HashSet.h"
26 #include "wtf/Noncopyable.h"
27 #include "wtf/StdLibExtras.h"
28
29 namespace WebCore {
30
31 class CSSCursorImageValue;
32 class SVGCursorElement;
33 class SVGElement;
34
35 class SVGElementRareData : public NoBaseWillBeGarbageCollectedFinalized<SVGElementRareData> {
36     WTF_MAKE_NONCOPYABLE(SVGElementRareData); WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
37 public:
38     SVGElementRareData(SVGElement* owner)
39         : m_owner(owner)
40         , m_cursorElement(nullptr)
41         , m_cursorImageValue(nullptr)
42         , m_correspondingElement(0)
43         , m_instancesUpdatesBlocked(false)
44         , m_useOverrideComputedStyle(false)
45         , m_needsOverrideComputedStyleUpdate(false)
46     {
47     }
48
49     typedef WillBeHeapHashMap<RawPtrWillBeWeakMember<const SVGElement>, RawPtrWillBeMember<SVGElementRareData> > SVGElementRareDataMap;
50
51     static SVGElementRareDataMap& rareDataMap()
52     {
53 #if ENABLE(OILPAN)
54         DEFINE_STATIC_LOCAL(Persistent<SVGElementRareDataMap>, rareDataMap, (new SVGElementRareDataMap));
55         return *rareDataMap;
56 #else
57         DEFINE_STATIC_LOCAL(SVGElementRareDataMap, rareDataMap, ());
58         return rareDataMap;
59 #endif
60     }
61
62     static SVGElementRareData* rareDataFromMap(const SVGElement* element)
63     {
64         return rareDataMap().get(element);
65     }
66
67     WillBeHeapHashSet<RawPtrWillBeWeakMember<SVGElement> >& elementInstances() { return m_elementInstances; }
68     const WillBeHeapHashSet<RawPtrWillBeWeakMember<SVGElement> >& elementInstances() const { return m_elementInstances; }
69
70     bool instanceUpdatesBlocked() const { return m_instancesUpdatesBlocked; }
71     void setInstanceUpdatesBlocked(bool value) { m_instancesUpdatesBlocked = value; }
72
73     SVGCursorElement* cursorElement() const { return m_cursorElement; }
74     void setCursorElement(SVGCursorElement* cursorElement) { m_cursorElement = cursorElement; }
75
76     SVGElement* correspondingElement() { return m_correspondingElement; }
77     void setCorrespondingElement(SVGElement* correspondingElement) { m_correspondingElement = correspondingElement; }
78
79     CSSCursorImageValue* cursorImageValue() const { return m_cursorImageValue; }
80     void setCursorImageValue(CSSCursorImageValue* cursorImageValue) { m_cursorImageValue = cursorImageValue; }
81
82     MutableStylePropertySet* animatedSMILStyleProperties() const { return m_animatedSMILStyleProperties.get(); }
83     MutableStylePropertySet* ensureAnimatedSMILStyleProperties()
84     {
85         if (!m_animatedSMILStyleProperties)
86             m_animatedSMILStyleProperties = MutableStylePropertySet::create(SVGAttributeMode);
87         return m_animatedSMILStyleProperties.get();
88     }
89
90     RenderStyle* overrideComputedStyle(Element* element, RenderStyle* parentStyle)
91     {
92         ASSERT(element);
93         if (!m_useOverrideComputedStyle)
94             return 0;
95         if (!m_overrideComputedStyle || m_needsOverrideComputedStyleUpdate) {
96             // The style computed here contains no CSS Animations/Transitions or SMIL induced rules - this is needed to compute the "base value" for the SMIL animation sandwhich model.
97             m_overrideComputedStyle = element->document().ensureStyleResolver().styleForElement(element, parentStyle, DisallowStyleSharing, MatchAllRulesExcludingSMIL);
98             m_needsOverrideComputedStyleUpdate = false;
99         }
100         ASSERT(m_overrideComputedStyle);
101         return m_overrideComputedStyle.get();
102     }
103
104     bool useOverrideComputedStyle() const { return m_useOverrideComputedStyle; }
105     void setUseOverrideComputedStyle(bool value) { m_useOverrideComputedStyle = value; }
106     void setNeedsOverrideComputedStyleUpdate() { m_needsOverrideComputedStyleUpdate = true; }
107
108     void trace(Visitor* visitor)
109     {
110         visitor->trace(m_animatedSMILStyleProperties);
111         visitor->trace(m_elementInstances);
112         visitor->registerWeakMembers<SVGElementRareData, &SVGElementRareData::processWeakMembers>(this);
113     }
114
115     void processWeakMembers(Visitor* visitor)
116     {
117 #if ENABLE(OILPAN)
118         if (!visitor->isAlive(m_owner)) {
119             // If the owning SVGElement is dead this raraData element will be collected ASAP.
120             // The owning SVGElement will also be automatically removed from the SVGCursorElement's
121             // HashSet so no need to call out and clear anything.
122             // It should not be necessary, but just in case we clear the internal members to
123             // ensure we don't have a stale pointer.
124             m_owner = nullptr;
125             m_cursorElement = nullptr;
126             m_cursorImageValue = nullptr;
127             return;
128         }
129         ASSERT(m_owner);
130         if (!visitor->isAlive(m_cursorElement))
131             m_cursorElement = nullptr;
132
133         if (!visitor->isAlive(m_cursorImageValue)) {
134             // If the owning SVGElement is still alive and it is pointing to an SVGCursorElement
135             // we unregister it when the CSSCursorImageValue dies.
136             if (m_cursorElement) {
137                 m_cursorElement->removeReferencedElement(m_owner);
138                 m_cursorElement = nullptr;
139             }
140             m_cursorImageValue = nullptr;
141         }
142         ASSERT(!m_cursorElement || visitor->isAlive(m_cursorElement));
143         ASSERT(!m_cursorImageValue || visitor->isAlive(m_cursorImageValue));
144 #endif
145     }
146
147 private:
148     RawPtrWillBeWeakMember<SVGElement> m_owner;
149     WillBeHeapHashSet<RawPtrWillBeWeakMember<SVGElement> > m_elementInstances;
150     RawPtrWillBeWeakMember<SVGCursorElement> m_cursorElement;
151     RawPtrWillBeWeakMember<CSSCursorImageValue> m_cursorImageValue;
152     SVGElement* m_correspondingElement;
153     bool m_instancesUpdatesBlocked : 1;
154     bool m_useOverrideComputedStyle : 1;
155     bool m_needsOverrideComputedStyleUpdate : 1;
156     RefPtrWillBeMember<MutableStylePropertySet> m_animatedSMILStyleProperties;
157     RefPtr<RenderStyle> m_overrideComputedStyle;
158 };
159
160 }
161
162 #endif