/src/mozilla-central/dom/base/nsNodeInfoManager.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | /* |
8 | | * A class for handing out nodeinfos and ensuring sharing of them as needed. |
9 | | */ |
10 | | |
11 | | #ifndef nsNodeInfoManager_h___ |
12 | | #define nsNodeInfoManager_h___ |
13 | | |
14 | | #include "mozilla/Attributes.h" // for final |
15 | | #include "mozilla/dom/NodeInfo.h" |
16 | | #include "mozilla/MruCache.h" |
17 | | #include "nsCOMPtr.h" // for member |
18 | | #include "nsCycleCollectionParticipant.h" // for NS_DECL_CYCLE_* |
19 | | #include "nsDataHashtable.h" |
20 | | #include "nsStringFwd.h" |
21 | | |
22 | | class nsBindingManager; |
23 | | class nsAtom; |
24 | | class nsIDocument; |
25 | | class nsIPrincipal; |
26 | | class nsWindowSizes; |
27 | | template<class T> struct already_AddRefed; |
28 | | |
29 | | class nsNodeInfoManager final |
30 | | { |
31 | | private: |
32 | | ~nsNodeInfoManager(); |
33 | | |
34 | | public: |
35 | | nsNodeInfoManager(); |
36 | | |
37 | | NS_DECL_CYCLE_COLLECTION_SKIPPABLE_NATIVE_CLASS(nsNodeInfoManager) |
38 | | |
39 | | NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(nsNodeInfoManager) |
40 | | |
41 | | /** |
42 | | * Initialize the nodeinfo manager with a document. |
43 | | */ |
44 | | nsresult Init(nsIDocument *aDocument); |
45 | | |
46 | | /** |
47 | | * Release the reference to the document, this will be called when |
48 | | * the document is going away. |
49 | | */ |
50 | | void DropDocumentReference(); |
51 | | |
52 | | /** |
53 | | * Methods for creating nodeinfo's from atoms and/or strings. |
54 | | */ |
55 | | already_AddRefed<mozilla::dom::NodeInfo> |
56 | | GetNodeInfo(nsAtom *aName, nsAtom *aPrefix, int32_t aNamespaceID, |
57 | | uint16_t aNodeType, nsAtom* aExtraName = nullptr); |
58 | | nsresult GetNodeInfo(const nsAString& aName, nsAtom *aPrefix, |
59 | | int32_t aNamespaceID, uint16_t aNodeType, |
60 | | mozilla::dom::NodeInfo** aNodeInfo); |
61 | | nsresult GetNodeInfo(const nsAString& aName, nsAtom *aPrefix, |
62 | | const nsAString& aNamespaceURI, uint16_t aNodeType, |
63 | | mozilla::dom::NodeInfo** aNodeInfo); |
64 | | |
65 | | /** |
66 | | * Returns the nodeinfo for text nodes. Can return null if OOM. |
67 | | */ |
68 | | already_AddRefed<mozilla::dom::NodeInfo> GetTextNodeInfo(); |
69 | | |
70 | | /** |
71 | | * Returns the nodeinfo for comment nodes. Can return null if OOM. |
72 | | */ |
73 | | already_AddRefed<mozilla::dom::NodeInfo> GetCommentNodeInfo(); |
74 | | |
75 | | /** |
76 | | * Returns the nodeinfo for the document node. Can return null if OOM. |
77 | | */ |
78 | | already_AddRefed<mozilla::dom::NodeInfo> GetDocumentNodeInfo(); |
79 | | |
80 | | /** |
81 | | * Retrieve a pointer to the document that owns this node info |
82 | | * manager. |
83 | | */ |
84 | | nsIDocument* GetDocument() const |
85 | 0 | { |
86 | 0 | return mDocument; |
87 | 0 | } |
88 | | |
89 | | /** |
90 | | * Gets the principal of the document this nodeinfo manager belongs to. |
91 | | */ |
92 | 0 | nsIPrincipal *DocumentPrincipal() const { |
93 | 0 | NS_ASSERTION(mPrincipal, "How'd that happen?"); |
94 | 0 | return mPrincipal; |
95 | 0 | } |
96 | | |
97 | | void RemoveNodeInfo(mozilla::dom::NodeInfo *aNodeInfo); |
98 | | |
99 | | nsBindingManager* GetBindingManager() const |
100 | 0 | { |
101 | 0 | return mBindingManager; |
102 | 0 | } |
103 | | |
104 | | enum Tri |
105 | | { |
106 | | eTriUnset = 0, |
107 | | eTriFalse, |
108 | | eTriTrue |
109 | | }; |
110 | | |
111 | | /** |
112 | | * Returns true if SVG nodes in this document have real SVG semantics. |
113 | | */ |
114 | | bool SVGEnabled() |
115 | 0 | { |
116 | 0 | return mSVGEnabled == eTriTrue |
117 | 0 | ? true |
118 | 0 | : mSVGEnabled == eTriFalse ? false : InternalSVGEnabled(); |
119 | 0 | } |
120 | | |
121 | | /** |
122 | | * Returns true if MathML nodes in this document have real MathML semantics. |
123 | | */ |
124 | | bool MathMLEnabled() |
125 | 0 | { |
126 | 0 | return mMathMLEnabled == eTriTrue |
127 | 0 | ? true |
128 | 0 | : mMathMLEnabled == eTriFalse ? false : InternalMathMLEnabled(); |
129 | 0 | } |
130 | | |
131 | | void AddSizeOfIncludingThis(nsWindowSizes& aSizes) const; |
132 | | |
133 | | protected: |
134 | | friend class nsIDocument; |
135 | | friend class nsXULPrototypeDocument; |
136 | | |
137 | | /** |
138 | | * Sets the principal of the document this nodeinfo manager belongs to. |
139 | | */ |
140 | | void SetDocumentPrincipal(nsIPrincipal *aPrincipal); |
141 | | |
142 | | private: |
143 | | bool InternalSVGEnabled(); |
144 | | bool InternalMathMLEnabled(); |
145 | | |
146 | | class NodeInfoInnerKey : |
147 | | public nsPtrHashKey<mozilla::dom::NodeInfo::NodeInfoInner> |
148 | | { |
149 | | public: |
150 | 0 | explicit NodeInfoInnerKey(KeyTypePointer aKey) : nsPtrHashKey(aKey) {} |
151 | | NodeInfoInnerKey(NodeInfoInnerKey&&) = default; |
152 | | ~NodeInfoInnerKey() = default; |
153 | 0 | bool KeyEquals(KeyTypePointer aKey) const { return *mKey == *aKey; } |
154 | 0 | static PLDHashNumber HashKey(KeyTypePointer aKey) { return aKey->Hash(); } |
155 | | }; |
156 | | |
157 | | struct NodeInfoCache : public mozilla::MruCache< |
158 | | mozilla::dom::NodeInfo::NodeInfoInner, |
159 | | mozilla::dom::NodeInfo*, |
160 | | NodeInfoCache> |
161 | | { |
162 | | static mozilla::HashNumber Hash( |
163 | | const mozilla::dom::NodeInfo::NodeInfoInner& aKey) |
164 | 0 | { |
165 | 0 | return aKey.Hash(); |
166 | 0 | } |
167 | | static bool Match(const mozilla::dom::NodeInfo::NodeInfoInner& aKey, |
168 | | const mozilla::dom::NodeInfo* aVal) |
169 | 0 | { |
170 | 0 | return aKey == aVal->mInner; |
171 | 0 | } |
172 | | }; |
173 | | |
174 | | nsDataHashtable<NodeInfoInnerKey, mozilla::dom::NodeInfo*> mNodeInfoHash; |
175 | | nsIDocument * MOZ_NON_OWNING_REF mDocument; // WEAK |
176 | | uint32_t mNonDocumentNodeInfos; |
177 | | nsCOMPtr<nsIPrincipal> mPrincipal; // Never null after Init() succeeds. |
178 | | nsCOMPtr<nsIPrincipal> mDefaultPrincipal; // Never null after Init() succeeds |
179 | | mozilla::dom::NodeInfo * MOZ_NON_OWNING_REF mTextNodeInfo; // WEAK to avoid circular ownership |
180 | | mozilla::dom::NodeInfo * MOZ_NON_OWNING_REF mCommentNodeInfo; // WEAK to avoid circular ownership |
181 | | mozilla::dom::NodeInfo * MOZ_NON_OWNING_REF mDocumentNodeInfo; // WEAK to avoid circular ownership |
182 | | RefPtr<nsBindingManager> mBindingManager; |
183 | | NodeInfoCache mRecentlyUsedNodeInfos; |
184 | | Tri mSVGEnabled; |
185 | | Tri mMathMLEnabled; |
186 | | }; |
187 | | |
188 | | #endif /* nsNodeInfoManager_h___ */ |