Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / NodeRareData.h
index 1de8431..69e65d2 100644 (file)
 #ifndef NodeRareData_h
 #define NodeRareData_h
 
-#include "core/dom/ChildNodeList.h"
-#include "core/dom/EmptyNodeList.h"
-#include "core/dom/LiveNodeList.h"
 #include "core/dom/MutationObserverRegistration.h"
-#include "core/dom/QualifiedName.h"
-#include "core/dom/TagCollection.h"
-#include "core/page/Page.h"
+#include "core/dom/NodeListsNodeData.h"
+#include "platform/heap/Handle.h"
 #include "wtf/HashSet.h"
 #include "wtf/OwnPtr.h"
 #include "wtf/PassOwnPtr.h"
-#include "wtf/text/AtomicString.h"
-#include "wtf/text/StringHash.h"
 
-namespace WebCore {
+namespace blink {
 
-class LabelsNodeList;
-class RadioNodeList;
-class TreeScope;
-
-class NodeListsNodeData {
-    WTF_MAKE_NONCOPYABLE(NodeListsNodeData); WTF_MAKE_FAST_ALLOCATED;
+class NodeMutationObserverData FINAL : public NoBaseWillBeGarbageCollected<NodeMutationObserverData> {
+    WTF_MAKE_NONCOPYABLE(NodeMutationObserverData);
+    WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
 public:
-    void clearChildNodeListCache()
-    {
-        if (m_childNodeList && m_childNodeList->isChildNodeList())
-            toChildNodeList(m_childNodeList)->invalidateCache();
-    }
-
-    PassRefPtr<ChildNodeList> ensureChildNodeList(ContainerNode& node)
-    {
-        if (m_childNodeList)
-            return toChildNodeList(m_childNodeList);
-        RefPtr<ChildNodeList> list = ChildNodeList::create(node);
-        m_childNodeList = list.get();
-        return list.release();
-    }
-
-    PassRefPtr<EmptyNodeList> ensureEmptyChildNodeList(Node& node)
-    {
-        if (m_childNodeList)
-            return toEmptyNodeList(m_childNodeList);
-        RefPtr<EmptyNodeList> list = EmptyNodeList::create(node);
-        m_childNodeList = list.get();
-        return list.release();
-    }
-
-    void removeChildNodeList(ChildNodeList* list)
-    {
-        ASSERT(m_childNodeList == list);
-        if (deleteThisAndUpdateNodeRareDataIfAboutToRemoveLastList(list->ownerNode()))
-            return;
-        m_childNodeList = 0;
-    }
-
-    void removeEmptyChildNodeList(EmptyNodeList* list)
-    {
-        ASSERT(m_childNodeList == list);
-        if (deleteThisAndUpdateNodeRareDataIfAboutToRemoveLastList(list->ownerNode()))
-            return;
-        m_childNodeList = 0;
-    }
-
-    struct NodeListAtomicCacheMapEntryHash {
-        static unsigned hash(const std::pair<unsigned char, StringImpl*>& entry)
-        {
-            return DefaultHash<StringImpl*>::Hash::hash(entry.second) + entry.first;
-        }
-        static bool equal(const std::pair<unsigned char, StringImpl*>& a, const std::pair<unsigned char, StringImpl*>& b) { return a == b; }
-        static const bool safeToCompareToEmptyOrDeleted = DefaultHash<StringImpl*>::Hash::safeToCompareToEmptyOrDeleted;
-    };
-
-    typedef HashMap<std::pair<unsigned char, StringImpl*>, LiveNodeListBase*, NodeListAtomicCacheMapEntryHash> NodeListAtomicNameCacheMap;
-    typedef HashMap<QualifiedName, TagCollection*> TagCollectionCacheNS;
-
-    template<typename T>
-    PassRefPtr<T> addCache(ContainerNode& node, CollectionType collectionType, const AtomicString& name)
-    {
-        NodeListAtomicNameCacheMap::AddResult result = m_atomicNameCaches.add(namedNodeListKey(collectionType, name), 0);
-        if (!result.isNewEntry)
-            return static_cast<T*>(result.storedValue->value);
-
-        RefPtr<T> list = T::create(node, collectionType, name);
-        result.storedValue->value = list.get();
-        return list.release();
-    }
-
-    template<typename T>
-    PassRefPtr<T> addCache(ContainerNode& node, CollectionType collectionType)
-    {
-        NodeListAtomicNameCacheMap::AddResult result = m_atomicNameCaches.add(namedNodeListKey(collectionType, starAtom), 0);
-        if (!result.isNewEntry)
-            return static_cast<T*>(result.storedValue->value);
-
-        RefPtr<T> list = T::create(node, collectionType);
-        result.storedValue->value = list.get();
-        return list.release();
-    }
-
-    template<typename T>
-    T* cached(CollectionType collectionType)
-    {
-        return static_cast<T*>(m_atomicNameCaches.get(namedNodeListKey(collectionType, starAtom)));
-    }
-
-    PassRefPtr<TagCollection> addCache(ContainerNode& node, const AtomicString& namespaceURI, const AtomicString& localName)
-    {
-        QualifiedName name(nullAtom, localName, namespaceURI);
-        TagCollectionCacheNS::AddResult result = m_tagCollectionCacheNS.add(name, 0);
-        if (!result.isNewEntry)
-            return result.storedValue->value;
-
-        RefPtr<TagCollection> list = TagCollection::create(node, namespaceURI, localName);
-        result.storedValue->value = list.get();
-        return list.release();
-    }
+    WillBeHeapVector<OwnPtrWillBeMember<MutationObserverRegistration> > registry;
+    WillBeHeapHashSet<RawPtrWillBeMember<MutationObserverRegistration> > transientRegistry;
 
-    void removeCache(LiveNodeListBase* list, CollectionType collectionType, const AtomicString& name = starAtom)
+    static PassOwnPtrWillBeRawPtr<NodeMutationObserverData> create()
     {
-        ASSERT(list == m_atomicNameCaches.get(namedNodeListKey(collectionType, name)));
-        if (deleteThisAndUpdateNodeRareDataIfAboutToRemoveLastList(list->ownerNode()))
-            return;
-        m_atomicNameCaches.remove(namedNodeListKey(collectionType, name));
+        return adoptPtrWillBeNoop(new NodeMutationObserverData);
     }
 
-    void removeCache(LiveNodeListBase* list, const AtomicString& namespaceURI, const AtomicString& localName)
+    void trace(Visitor* visitor)
     {
-        QualifiedName name(nullAtom, localName, namespaceURI);
-        ASSERT(list == m_tagCollectionCacheNS.get(name));
-        if (deleteThisAndUpdateNodeRareDataIfAboutToRemoveLastList(list->ownerNode()))
-            return;
-        m_tagCollectionCacheNS.remove(name);
+#if ENABLE(OILPAN)
+        visitor->trace(registry);
+        visitor->trace(transientRegistry);
+#endif
     }
 
-    static PassOwnPtr<NodeListsNodeData> create()
-    {
-        return adoptPtr(new NodeListsNodeData);
-    }
-
-    void invalidateCaches(const QualifiedName* attrName = 0);
-    bool isEmpty() const
-    {
-        return m_atomicNameCaches.isEmpty() && m_tagCollectionCacheNS.isEmpty();
-    }
-
-    void adoptTreeScope()
-    {
-        invalidateCaches();
-    }
-
-    void adoptDocument(Document& oldDocument, Document& newDocument)
-    {
-        ASSERT(oldDocument != newDocument);
-
-        NodeListAtomicNameCacheMap::const_iterator atomicNameCacheEnd = m_atomicNameCaches.end();
-        for (NodeListAtomicNameCacheMap::const_iterator it = m_atomicNameCaches.begin(); it != atomicNameCacheEnd; ++it) {
-            LiveNodeListBase* list = it->value;
-            list->didMoveToDocument(oldDocument, newDocument);
-        }
-
-        TagCollectionCacheNS::const_iterator tagEnd = m_tagCollectionCacheNS.end();
-        for (TagCollectionCacheNS::const_iterator it = m_tagCollectionCacheNS.begin(); it != tagEnd; ++it) {
-            LiveNodeListBase* list = it->value;
-            ASSERT(!list->isRootedAtDocument());
-            list->didMoveToDocument(oldDocument, newDocument);
-        }
-    }
-
-private:
-    NodeListsNodeData()
-        : m_childNodeList(0)
-    { }
-
-    std::pair<unsigned char, StringImpl*> namedNodeListKey(CollectionType type, const AtomicString& name)
-    {
-        // Holding the raw StringImpl is safe because |name| is retained by the NodeList and the NodeList
-        // is reponsible for removing itself from the cache on deletion.
-        return std::pair<unsigned char, StringImpl*>(type, name.impl());
-    }
-
-    bool deleteThisAndUpdateNodeRareDataIfAboutToRemoveLastList(Node&);
-
-    // Can be a ChildNodeList or an EmptyNodeList.
-    NodeList* m_childNodeList;
-    NodeListAtomicNameCacheMap m_atomicNameCaches;
-    TagCollectionCacheNS m_tagCollectionCacheNS;
-};
-
-class NodeMutationObserverData {
-    WTF_MAKE_NONCOPYABLE(NodeMutationObserverData); WTF_MAKE_FAST_ALLOCATED;
-public:
-    Vector<OwnPtr<MutationObserverRegistration> > registry;
-    HashSet<MutationObserverRegistration*> transientRegistry;
-
-    static PassOwnPtr<NodeMutationObserverData> create() { return adoptPtr(new NodeMutationObserverData); }
-
 private:
     NodeMutationObserverData() { }
 };
 
-class NodeRareData : public NodeRareDataBase {
-    WTF_MAKE_NONCOPYABLE(NodeRareData); WTF_MAKE_FAST_ALLOCATED;
+class NodeRareData : public NoBaseWillBeGarbageCollectedFinalized<NodeRareData>, public NodeRareDataBase {
+    WTF_MAKE_NONCOPYABLE(NodeRareData);
+    WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
 public:
-    static PassOwnPtr<NodeRareData> create(RenderObject* renderer) { return adoptPtr(new NodeRareData(renderer)); }
+    static NodeRareData* create(RenderObject* renderer)
+    {
+        return new NodeRareData(renderer);
+    }
 
     void clearNodeLists() { m_nodeLists.clear(); }
     NodeListsNodeData* nodeLists() const { return m_nodeLists.get(); }
@@ -268,32 +106,31 @@ public:
         ConnectedFrameCountBits = 10, // Must fit Page::maxNumberOfFrames.
     };
 
+    void trace(Visitor*);
+
+    void traceAfterDispatch(Visitor*);
+    void finalizeGarbageCollectedObject();
+
 protected:
-    NodeRareData(RenderObject* renderer)
+    explicit NodeRareData(RenderObject* renderer)
         : NodeRareDataBase(renderer)
         , m_connectedFrameCount(0)
         , m_elementFlags(0)
         , m_restyleFlags(0)
+        , m_isElementRareData(false)
     { }
 
 private:
-    OwnPtr<NodeListsNodeData> m_nodeLists;
-    OwnPtr<NodeMutationObserverData> m_mutationObserverData;
+    OwnPtrWillBeMember<NodeListsNodeData> m_nodeLists;
+    OwnPtrWillBeMember<NodeMutationObserverData> m_mutationObserverData;
 
     unsigned m_connectedFrameCount : ConnectedFrameCountBits;
     unsigned m_elementFlags : NumberOfElementFlags;
     unsigned m_restyleFlags : NumberOfDynamicRestyleFlags;
+protected:
+    unsigned m_isElementRareData : 1;
 };
 
-inline bool NodeListsNodeData::deleteThisAndUpdateNodeRareDataIfAboutToRemoveLastList(Node& ownerNode)
-{
-    ASSERT(ownerNode.nodeLists() == this);
-    if ((m_childNodeList ? 1 : 0) + m_atomicNameCaches.size() + m_tagCollectionCacheNS.size() != 1)
-        return false;
-    ownerNode.clearNodeLists();
-    return true;
-}
-
-} // namespace WebCore
+} // namespace blink
 
 #endif // NodeRareData_h