1/*
2 * Copyright (C) 2008, 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 David Smith <catfish.man@gmail.com>
4 *
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.
9 *
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.
14 *
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.
19 *
20 */
21
22#ifndef NodeRareData_h
23#define NodeRareData_h
24
25#include "core/dom/MutationObserverRegistration.h"
26#include "core/dom/NodeListsNodeData.h"
27#include "platform/heap/Handle.h"
28#include "wtf/HashSet.h"
29#include "wtf/OwnPtr.h"
30#include "wtf/PassOwnPtr.h"
31
32namespace blink {
33
34class NodeMutationObserverData FINAL : public NoBaseWillBeGarbageCollected<NodeMutationObserverData> {
35    WTF_MAKE_NONCOPYABLE(NodeMutationObserverData);
36    WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
37public:
38    WillBeHeapVector<OwnPtrWillBeMember<MutationObserverRegistration> > registry;
39    WillBeHeapHashSet<RawPtrWillBeMember<MutationObserverRegistration> > transientRegistry;
40
41    static PassOwnPtrWillBeRawPtr<NodeMutationObserverData> create()
42    {
43        return adoptPtrWillBeNoop(new NodeMutationObserverData);
44    }
45
46    void trace(Visitor* visitor)
47    {
48#if ENABLE(OILPAN)
49        visitor->trace(registry);
50        visitor->trace(transientRegistry);
51#endif
52    }
53
54private:
55    NodeMutationObserverData() { }
56};
57
58class NodeRareData : public NoBaseWillBeGarbageCollectedFinalized<NodeRareData>, public NodeRareDataBase {
59    WTF_MAKE_NONCOPYABLE(NodeRareData);
60    WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
61public:
62    static NodeRareData* create(RenderObject* renderer)
63    {
64        return new NodeRareData(renderer);
65    }
66
67    void clearNodeLists() { m_nodeLists.clear(); }
68    NodeListsNodeData* nodeLists() const { return m_nodeLists.get(); }
69    NodeListsNodeData& ensureNodeLists()
70    {
71        if (!m_nodeLists)
72            m_nodeLists = NodeListsNodeData::create();
73        return *m_nodeLists;
74    }
75
76    NodeMutationObserverData* mutationObserverData() { return m_mutationObserverData.get(); }
77    NodeMutationObserverData& ensureMutationObserverData()
78    {
79        if (!m_mutationObserverData)
80            m_mutationObserverData = NodeMutationObserverData::create();
81        return *m_mutationObserverData;
82    }
83
84    unsigned connectedSubframeCount() const { return m_connectedFrameCount; }
85    void incrementConnectedSubframeCount(unsigned amount)
86    {
87        m_connectedFrameCount += amount;
88    }
89    void decrementConnectedSubframeCount(unsigned amount)
90    {
91        ASSERT(m_connectedFrameCount);
92        ASSERT(amount <= m_connectedFrameCount);
93        m_connectedFrameCount -= amount;
94    }
95
96    bool hasElementFlag(ElementFlags mask) const { return m_elementFlags & mask; }
97    void setElementFlag(ElementFlags mask, bool value) { m_elementFlags = (m_elementFlags & ~mask) | (-(int32_t)value & mask); }
98    void clearElementFlag(ElementFlags mask) { m_elementFlags &= ~mask; }
99
100    bool hasRestyleFlag(DynamicRestyleFlags mask) const { return m_restyleFlags & mask; }
101    void setRestyleFlag(DynamicRestyleFlags mask) { m_restyleFlags |= mask; RELEASE_ASSERT(m_restyleFlags); }
102    bool hasRestyleFlags() const { return m_restyleFlags; }
103    void clearRestyleFlags() { m_restyleFlags = 0; }
104
105    enum {
106        ConnectedFrameCountBits = 10, // Must fit Page::maxNumberOfFrames.
107    };
108
109    void trace(Visitor*);
110
111    void traceAfterDispatch(Visitor*);
112    void finalizeGarbageCollectedObject();
113
114protected:
115    explicit NodeRareData(RenderObject* renderer)
116        : NodeRareDataBase(renderer)
117        , m_connectedFrameCount(0)
118        , m_elementFlags(0)
119        , m_restyleFlags(0)
120        , m_isElementRareData(false)
121    { }
122
123private:
124    OwnPtrWillBeMember<NodeListsNodeData> m_nodeLists;
125    OwnPtrWillBeMember<NodeMutationObserverData> m_mutationObserverData;
126
127    unsigned m_connectedFrameCount : ConnectedFrameCountBits;
128    unsigned m_elementFlags : NumberOfElementFlags;
129    unsigned m_restyleFlags : NumberOfDynamicRestyleFlags;
130protected:
131    unsigned m_isElementRareData : 1;
132};
133
134} // namespace blink
135
136#endif // NodeRareData_h
137