2 * Copyright (C) 2008, 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 David Smith <catfish.man@gmail.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
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
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 #ifndef NodeListsNodeData_h
23 #define NodeListsNodeData_h
25 #include "core/dom/ChildNodeList.h"
26 #include "core/dom/EmptyNodeList.h"
27 #include "core/dom/QualifiedName.h"
28 #include "core/dom/TagCollection.h"
29 #include "core/html/CollectionType.h"
30 #include "platform/heap/Handle.h"
31 #include "wtf/text/AtomicString.h"
32 #include "wtf/text/StringHash.h"
36 class NodeListsNodeData final : public NoBaseWillBeGarbageCollectedFinalized<NodeListsNodeData> {
37 WTF_MAKE_NONCOPYABLE(NodeListsNodeData);
38 WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
40 ChildNodeList* childNodeList(ContainerNode& node)
42 ASSERT_UNUSED(node, !m_childNodeList || node == m_childNodeList->virtualOwnerNode());
43 return toChildNodeList(m_childNodeList);
46 PassRefPtrWillBeRawPtr<ChildNodeList> ensureChildNodeList(ContainerNode& node)
49 return toChildNodeList(m_childNodeList);
50 RefPtrWillBeRawPtr<ChildNodeList> list = ChildNodeList::create(node);
51 m_childNodeList = list.get();
52 return list.release();
55 PassRefPtrWillBeRawPtr<EmptyNodeList> ensureEmptyChildNodeList(Node& node)
58 return toEmptyNodeList(m_childNodeList);
59 RefPtrWillBeRawPtr<EmptyNodeList> list = EmptyNodeList::create(node);
60 m_childNodeList = list.get();
61 return list.release();
65 void removeChildNodeList(ChildNodeList* list)
67 ASSERT(m_childNodeList == list);
68 if (deleteThisAndUpdateNodeRareDataIfAboutToRemoveLastList(list->ownerNode()))
70 m_childNodeList = nullptr;
73 void removeEmptyChildNodeList(EmptyNodeList* list)
75 ASSERT(m_childNodeList == list);
76 if (deleteThisAndUpdateNodeRareDataIfAboutToRemoveLastList(list->ownerNode()))
78 m_childNodeList = nullptr;
82 struct NodeListAtomicCacheMapEntryHash {
83 static unsigned hash(const std::pair<unsigned char, StringImpl*>& entry)
85 return DefaultHash<StringImpl*>::Hash::hash(entry.second) + entry.first;
87 static bool equal(const std::pair<unsigned char, StringImpl*>& a, const std::pair<unsigned char, StringImpl*>& b) { return a == b; }
88 static const bool safeToCompareToEmptyOrDeleted = DefaultHash<StringImpl*>::Hash::safeToCompareToEmptyOrDeleted;
91 // Oilpan: keep a weak reference to the collection objects.
92 // Explicit object unregistration in a non-Oilpan setting
93 // on object destruction is replaced by the garbage collector
94 // clearing out their weak reference.
95 typedef WillBeHeapHashMap<std::pair<unsigned char, StringImpl*>, RawPtrWillBeWeakMember<LiveNodeListBase>, NodeListAtomicCacheMapEntryHash> NodeListAtomicNameCacheMap;
96 typedef WillBeHeapHashMap<QualifiedName, RawPtrWillBeWeakMember<TagCollection> > TagCollectionCacheNS;
99 PassRefPtrWillBeRawPtr<T> addCache(ContainerNode& node, CollectionType collectionType, const AtomicString& name)
101 NodeListAtomicNameCacheMap::AddResult result = m_atomicNameCaches.add(namedNodeListKey(collectionType, name), nullptr);
102 if (!result.isNewEntry) {
104 return static_cast<T*>(result.storedValue->value.get());
106 return static_cast<T*>(result.storedValue->value);
110 RefPtrWillBeRawPtr<T> list = T::create(node, collectionType, name);
111 result.storedValue->value = list.get();
112 return list.release();
116 PassRefPtrWillBeRawPtr<T> addCache(ContainerNode& node, CollectionType collectionType)
118 NodeListAtomicNameCacheMap::AddResult result = m_atomicNameCaches.add(namedNodeListKey(collectionType, starAtom), nullptr);
119 if (!result.isNewEntry) {
121 return static_cast<T*>(result.storedValue->value.get());
123 return static_cast<T*>(result.storedValue->value);
127 RefPtrWillBeRawPtr<T> list = T::create(node, collectionType);
128 result.storedValue->value = list.get();
129 return list.release();
133 T* cached(CollectionType collectionType)
135 return static_cast<T*>(m_atomicNameCaches.get(namedNodeListKey(collectionType, starAtom)));
138 PassRefPtrWillBeRawPtr<TagCollection> addCache(ContainerNode& node, const AtomicString& namespaceURI, const AtomicString& localName)
140 QualifiedName name(nullAtom, localName, namespaceURI);
141 TagCollectionCacheNS::AddResult result = m_tagCollectionCacheNS.add(name, nullptr);
142 if (!result.isNewEntry)
143 return result.storedValue->value;
145 RefPtrWillBeRawPtr<TagCollection> list = TagCollection::create(node, namespaceURI, localName);
146 result.storedValue->value = list.get();
147 return list.release();
151 void removeCache(LiveNodeListBase* list, CollectionType collectionType, const AtomicString& name = starAtom)
153 ASSERT(list == m_atomicNameCaches.get(namedNodeListKey(collectionType, name)));
154 if (deleteThisAndUpdateNodeRareDataIfAboutToRemoveLastList(list->ownerNode()))
156 m_atomicNameCaches.remove(namedNodeListKey(collectionType, name));
159 void removeCache(LiveNodeListBase* list, const AtomicString& namespaceURI, const AtomicString& localName)
161 QualifiedName name(nullAtom, localName, namespaceURI);
162 ASSERT(list == m_tagCollectionCacheNS.get(name));
163 if (deleteThisAndUpdateNodeRareDataIfAboutToRemoveLastList(list->ownerNode()))
165 m_tagCollectionCacheNS.remove(name);
169 static PassOwnPtrWillBeRawPtr<NodeListsNodeData> create()
171 return adoptPtrWillBeNoop(new NodeListsNodeData);
174 void invalidateCaches(const QualifiedName* attrName = 0);
178 return !m_childNodeList && m_atomicNameCaches.isEmpty() && m_tagCollectionCacheNS.isEmpty();
181 void adoptTreeScope()
186 void adoptDocument(Document& oldDocument, Document& newDocument)
188 ASSERT(oldDocument != newDocument);
190 NodeListAtomicNameCacheMap::const_iterator atomicNameCacheEnd = m_atomicNameCaches.end();
191 for (NodeListAtomicNameCacheMap::const_iterator it = m_atomicNameCaches.begin(); it != atomicNameCacheEnd; ++it) {
192 LiveNodeListBase* list = it->value;
193 list->didMoveToDocument(oldDocument, newDocument);
196 TagCollectionCacheNS::const_iterator tagEnd = m_tagCollectionCacheNS.end();
197 for (TagCollectionCacheNS::const_iterator it = m_tagCollectionCacheNS.begin(); it != tagEnd; ++it) {
198 LiveNodeListBase* list = it->value;
199 ASSERT(!list->isRootedAtDocument());
200 list->didMoveToDocument(oldDocument, newDocument);
204 void trace(Visitor*);
208 : m_childNodeList(nullptr)
211 std::pair<unsigned char, StringImpl*> namedNodeListKey(CollectionType type, const AtomicString& name)
213 // Holding the raw StringImpl is safe because |name| is retained by the NodeList and the NodeList
214 // is reponsible for removing itself from the cache on deletion.
215 return std::pair<unsigned char, StringImpl*>(type, name.impl());
219 bool deleteThisAndUpdateNodeRareDataIfAboutToRemoveLastList(Node&);
222 // Can be a ChildNodeList or an EmptyNodeList.
223 RawPtrWillBeWeakMember<NodeList> m_childNodeList;
224 NodeListAtomicNameCacheMap m_atomicNameCaches;
225 TagCollectionCacheNS m_tagCollectionCacheNS;
229 inline bool NodeListsNodeData::deleteThisAndUpdateNodeRareDataIfAboutToRemoveLastList(Node& ownerNode)
231 ASSERT(ownerNode.nodeLists() == this);
232 if ((m_childNodeList ? 1 : 0) + m_atomicNameCaches.size() + m_tagCollectionCacheNS.size() != 1)
234 ownerNode.clearNodeLists();
239 template <typename Collection>
240 inline PassRefPtrWillBeRawPtr<Collection> ContainerNode::ensureCachedCollection(CollectionType type)
242 return ensureNodeLists().addCache<Collection>(*this, type);
245 template <typename Collection>
246 inline PassRefPtrWillBeRawPtr<Collection> ContainerNode::ensureCachedCollection(CollectionType type, const AtomicString& name)
248 return ensureNodeLists().addCache<Collection>(*this, type, name);
251 template <typename Collection>
252 inline PassRefPtrWillBeRawPtr<Collection> ContainerNode::ensureCachedCollection(CollectionType type, const AtomicString& namespaceURI, const AtomicString& localName)
254 ASSERT_UNUSED(type, type == TagCollectionType);
255 return ensureNodeLists().addCache(*this, namespaceURI, localName);
258 template <typename Collection>
259 inline Collection* ContainerNode::cachedCollection(CollectionType type)
261 NodeListsNodeData* nodeLists = this->nodeLists();
262 return nodeLists ? nodeLists->cached<Collection>(type) : 0;
267 #endif // NodeListsNodeData_h