Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(523)

Side by Side Diff: Source/core/dom/NodeRareData.h

Issue 390043003: Move NodeListsNodeData class out of NodeRareData.h (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2008, 2010 Apple Inc. All rights reserved. 2 * Copyright (C) 2008, 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 David Smith <catfish.man@gmail.com> 3 * Copyright (C) 2008 David Smith <catfish.man@gmail.com>
4 * 4 *
5 * This library is free software; you can redistribute it and/or 5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public 6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either 7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version. 8 * version 2 of the License, or (at your option) any later version.
9 * 9 *
10 * This library is distributed in the hope that it will be useful, 10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details. 13 * Library General Public License for more details.
14 * 14 *
15 * You should have received a copy of the GNU Library General Public License 15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to 16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA. 18 * Boston, MA 02110-1301, USA.
19 * 19 *
20 */ 20 */
21 21
22 #ifndef NodeRareData_h 22 #ifndef NodeRareData_h
23 #define NodeRareData_h 23 #define NodeRareData_h
24 24
25 #include "core/dom/ChildNodeList.h"
26 #include "core/dom/EmptyNodeList.h"
27 #include "core/dom/LiveNodeList.h"
28 #include "core/dom/MutationObserverRegistration.h" 25 #include "core/dom/MutationObserverRegistration.h"
29 #include "core/dom/QualifiedName.h" 26 #include "core/dom/NodeListsNodeData.h"
30 #include "core/dom/TagCollection.h"
31 #include "platform/heap/Handle.h" 27 #include "platform/heap/Handle.h"
32 #include "wtf/HashSet.h" 28 #include "wtf/HashSet.h"
33 #include "wtf/OwnPtr.h" 29 #include "wtf/OwnPtr.h"
34 #include "wtf/PassOwnPtr.h" 30 #include "wtf/PassOwnPtr.h"
35 #include "wtf/text/AtomicString.h"
36 #include "wtf/text/StringHash.h"
37 31
38 namespace WebCore { 32 namespace WebCore {
39 33
40 class NodeListsNodeData FINAL : public NoBaseWillBeGarbageCollectedFinalized<Nod eListsNodeData> {
41 WTF_MAKE_NONCOPYABLE(NodeListsNodeData);
42 WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
43 public:
44 void clearChildNodeListCache()
45 {
46 if (m_childNodeList && m_childNodeList->isChildNodeList())
47 toChildNodeList(m_childNodeList)->invalidateCache();
48 }
49
50 PassRefPtrWillBeRawPtr<ChildNodeList> ensureChildNodeList(ContainerNode& nod e)
51 {
52 if (m_childNodeList)
53 return toChildNodeList(m_childNodeList);
54 RefPtrWillBeRawPtr<ChildNodeList> list = ChildNodeList::create(node);
55 m_childNodeList = list.get();
56 return list.release();
57 }
58
59 PassRefPtrWillBeRawPtr<EmptyNodeList> ensureEmptyChildNodeList(Node& node)
60 {
61 if (m_childNodeList)
62 return toEmptyNodeList(m_childNodeList);
63 RefPtrWillBeRawPtr<EmptyNodeList> list = EmptyNodeList::create(node);
64 m_childNodeList = list.get();
65 return list.release();
66 }
67
68 #if !ENABLE(OILPAN)
69 void removeChildNodeList(ChildNodeList* list)
70 {
71 ASSERT(m_childNodeList == list);
72 if (deleteThisAndUpdateNodeRareDataIfAboutToRemoveLastList(list->ownerNo de()))
73 return;
74 m_childNodeList = nullptr;
75 }
76
77 void removeEmptyChildNodeList(EmptyNodeList* list)
78 {
79 ASSERT(m_childNodeList == list);
80 if (deleteThisAndUpdateNodeRareDataIfAboutToRemoveLastList(list->ownerNo de()))
81 return;
82 m_childNodeList = nullptr;
83 }
84 #endif
85
86 struct NodeListAtomicCacheMapEntryHash {
87 static unsigned hash(const std::pair<unsigned char, StringImpl*>& entry)
88 {
89 return DefaultHash<StringImpl*>::Hash::hash(entry.second) + entry.fi rst;
90 }
91 static bool equal(const std::pair<unsigned char, StringImpl*>& a, const std::pair<unsigned char, StringImpl*>& b) { return a == b; }
92 static const bool safeToCompareToEmptyOrDeleted = DefaultHash<StringImpl *>::Hash::safeToCompareToEmptyOrDeleted;
93 };
94
95 // Oilpan: keep a weak reference to the collection objects.
96 // Explicit object unregistration in a non-Oilpan setting
97 // on object destruction is replaced by the garbage collector
98 // clearing out their weak reference.
99 typedef WillBeHeapHashMap<std::pair<unsigned char, StringImpl*>, RawPtrWillB eWeakMember<LiveNodeListBase>, NodeListAtomicCacheMapEntryHash> NodeListAtomicNa meCacheMap;
100 typedef WillBeHeapHashMap<QualifiedName, RawPtrWillBeWeakMember<TagCollectio n> > TagCollectionCacheNS;
101
102 template<typename T>
103 PassRefPtrWillBeRawPtr<T> addCache(ContainerNode& node, CollectionType colle ctionType, const AtomicString& name)
104 {
105 NodeListAtomicNameCacheMap::AddResult result = m_atomicNameCaches.add(na medNodeListKey(collectionType, name), nullptr);
106 if (!result.isNewEntry) {
107 #if ENABLE(OILPAN)
108 return static_cast<T*>(result.storedValue->value.get());
109 #else
110 return static_cast<T*>(result.storedValue->value);
111 #endif
112 }
113
114 RefPtrWillBeRawPtr<T> list = T::create(node, collectionType, name);
115 result.storedValue->value = list.get();
116 return list.release();
117 }
118
119 template<typename T>
120 PassRefPtrWillBeRawPtr<T> addCache(ContainerNode& node, CollectionType colle ctionType)
121 {
122 NodeListAtomicNameCacheMap::AddResult result = m_atomicNameCaches.add(na medNodeListKey(collectionType, starAtom), nullptr);
123 if (!result.isNewEntry) {
124 #if ENABLE(OILPAN)
125 return static_cast<T*>(result.storedValue->value.get());
126 #else
127 return static_cast<T*>(result.storedValue->value);
128 #endif
129 }
130
131 RefPtrWillBeRawPtr<T> list = T::create(node, collectionType);
132 result.storedValue->value = list.get();
133 return list.release();
134 }
135
136 template<typename T>
137 T* cached(CollectionType collectionType)
138 {
139 return static_cast<T*>(m_atomicNameCaches.get(namedNodeListKey(collectio nType, starAtom)));
140 }
141
142 PassRefPtrWillBeRawPtr<TagCollection> addCache(ContainerNode& node, const At omicString& namespaceURI, const AtomicString& localName)
143 {
144 QualifiedName name(nullAtom, localName, namespaceURI);
145 TagCollectionCacheNS::AddResult result = m_tagCollectionCacheNS.add(name , nullptr);
146 if (!result.isNewEntry)
147 return result.storedValue->value;
148
149 RefPtrWillBeRawPtr<TagCollection> list = TagCollection::create(node, nam espaceURI, localName);
150 result.storedValue->value = list.get();
151 return list.release();
152 }
153
154 #if !ENABLE(OILPAN)
155 void removeCache(LiveNodeListBase* list, CollectionType collectionType, cons t AtomicString& name = starAtom)
156 {
157 ASSERT(list == m_atomicNameCaches.get(namedNodeListKey(collectionType, n ame)));
158 if (deleteThisAndUpdateNodeRareDataIfAboutToRemoveLastList(list->ownerNo de()))
159 return;
160 m_atomicNameCaches.remove(namedNodeListKey(collectionType, name));
161 }
162
163 void removeCache(LiveNodeListBase* list, const AtomicString& namespaceURI, c onst AtomicString& localName)
164 {
165 QualifiedName name(nullAtom, localName, namespaceURI);
166 ASSERT(list == m_tagCollectionCacheNS.get(name));
167 if (deleteThisAndUpdateNodeRareDataIfAboutToRemoveLastList(list->ownerNo de()))
168 return;
169 m_tagCollectionCacheNS.remove(name);
170 }
171 #endif
172
173 static PassOwnPtrWillBeRawPtr<NodeListsNodeData> create()
174 {
175 return adoptPtrWillBeNoop(new NodeListsNodeData);
176 }
177
178 void invalidateCaches(const QualifiedName* attrName = 0);
179
180 bool isEmpty() const
181 {
182 return !m_childNodeList && m_atomicNameCaches.isEmpty() && m_tagCollecti onCacheNS.isEmpty();
183 }
184
185 void adoptTreeScope()
186 {
187 invalidateCaches();
188 }
189
190 void adoptDocument(Document& oldDocument, Document& newDocument)
191 {
192 ASSERT(oldDocument != newDocument);
193
194 NodeListAtomicNameCacheMap::const_iterator atomicNameCacheEnd = m_atomic NameCaches.end();
195 for (NodeListAtomicNameCacheMap::const_iterator it = m_atomicNameCaches. begin(); it != atomicNameCacheEnd; ++it) {
196 LiveNodeListBase* list = it->value;
197 list->didMoveToDocument(oldDocument, newDocument);
198 }
199
200 TagCollectionCacheNS::const_iterator tagEnd = m_tagCollectionCacheNS.end ();
201 for (TagCollectionCacheNS::const_iterator it = m_tagCollectionCacheNS.be gin(); it != tagEnd; ++it) {
202 LiveNodeListBase* list = it->value;
203 ASSERT(!list->isRootedAtDocument());
204 list->didMoveToDocument(oldDocument, newDocument);
205 }
206 }
207
208 void trace(Visitor*);
209
210 private:
211 NodeListsNodeData()
212 : m_childNodeList(nullptr)
213 { }
214
215 std::pair<unsigned char, StringImpl*> namedNodeListKey(CollectionType type, const AtomicString& name)
216 {
217 // Holding the raw StringImpl is safe because |name| is retained by the NodeList and the NodeList
218 // is reponsible for removing itself from the cache on deletion.
219 return std::pair<unsigned char, StringImpl*>(type, name.impl());
220 }
221
222 #if !ENABLE(OILPAN)
223 bool deleteThisAndUpdateNodeRareDataIfAboutToRemoveLastList(Node&);
224 #endif
225
226 // Can be a ChildNodeList or an EmptyNodeList.
227 RawPtrWillBeWeakMember<NodeList> m_childNodeList;
228 NodeListAtomicNameCacheMap m_atomicNameCaches;
229 TagCollectionCacheNS m_tagCollectionCacheNS;
230 };
231
232 class NodeMutationObserverData FINAL : public NoBaseWillBeGarbageCollected<NodeM utationObserverData> { 34 class NodeMutationObserverData FINAL : public NoBaseWillBeGarbageCollected<NodeM utationObserverData> {
233 WTF_MAKE_NONCOPYABLE(NodeMutationObserverData); 35 WTF_MAKE_NONCOPYABLE(NodeMutationObserverData);
234 WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED; 36 WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
235 public: 37 public:
236 WillBeHeapVector<OwnPtrWillBeMember<MutationObserverRegistration> > registry ; 38 WillBeHeapVector<OwnPtrWillBeMember<MutationObserverRegistration> > registry ;
237 WillBeHeapHashSet<RawPtrWillBeMember<MutationObserverRegistration> > transie ntRegistry; 39 WillBeHeapHashSet<RawPtrWillBeMember<MutationObserverRegistration> > transie ntRegistry;
238 40
239 static PassOwnPtrWillBeRawPtr<NodeMutationObserverData> create() 41 static PassOwnPtrWillBeRawPtr<NodeMutationObserverData> create()
240 { 42 {
241 return adoptPtrWillBeNoop(new NodeMutationObserverData); 43 return adoptPtrWillBeNoop(new NodeMutationObserverData);
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 OwnPtrWillBeMember<NodeListsNodeData> m_nodeLists; 122 OwnPtrWillBeMember<NodeListsNodeData> m_nodeLists;
321 OwnPtrWillBeMember<NodeMutationObserverData> m_mutationObserverData; 123 OwnPtrWillBeMember<NodeMutationObserverData> m_mutationObserverData;
322 124
323 unsigned m_connectedFrameCount : ConnectedFrameCountBits; 125 unsigned m_connectedFrameCount : ConnectedFrameCountBits;
324 unsigned m_elementFlags : NumberOfElementFlags; 126 unsigned m_elementFlags : NumberOfElementFlags;
325 unsigned m_restyleFlags : NumberOfDynamicRestyleFlags; 127 unsigned m_restyleFlags : NumberOfDynamicRestyleFlags;
326 protected: 128 protected:
327 unsigned m_isElementRareData : 1; 129 unsigned m_isElementRareData : 1;
328 }; 130 };
329 131
330 #if !ENABLE(OILPAN)
331 inline bool NodeListsNodeData::deleteThisAndUpdateNodeRareDataIfAboutToRemoveLas tList(Node& ownerNode)
332 {
333 ASSERT(ownerNode.nodeLists() == this);
334 if ((m_childNodeList ? 1 : 0) + m_atomicNameCaches.size() + m_tagCollectionC acheNS.size() != 1)
335 return false;
336 ownerNode.clearNodeLists();
337 return true;
338 }
339 #endif
340
341 } // namespace WebCore 132 } // namespace WebCore
342 133
343 #endif // NodeRareData_h 134 #endif // NodeRareData_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698