Line data Source code
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 "nsCOMPtr.h" // for member
16 : #include "nsCycleCollectionParticipant.h" // for NS_DECL_CYCLE_*
17 : #include "plhash.h" // for typedef PLHashNumber
18 :
19 : class nsAString;
20 : class nsBindingManager;
21 : class nsIAtom;
22 : class nsIDocument;
23 : class nsIDOMDocumentType;
24 : class nsIPrincipal;
25 : struct PLHashEntry;
26 : struct PLHashTable;
27 : template<class T> struct already_AddRefed;
28 :
29 : namespace mozilla {
30 : namespace dom {
31 : class NodeInfo;
32 : } // namespace dom
33 : } // namespace mozilla
34 :
35 : #define RECENTLY_USED_NODEINFOS_SIZE 31
36 :
37 : class nsNodeInfoManager final
38 : {
39 : private:
40 : ~nsNodeInfoManager();
41 :
42 : public:
43 : nsNodeInfoManager();
44 :
45 1907 : NS_DECL_CYCLE_COLLECTION_SKIPPABLE_NATIVE_CLASS(nsNodeInfoManager)
46 :
47 1907 : NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(nsNodeInfoManager)
48 :
49 : /**
50 : * Initialize the nodeinfo manager with a document.
51 : */
52 : nsresult Init(nsIDocument *aDocument);
53 :
54 : /**
55 : * Release the reference to the document, this will be called when
56 : * the document is going away.
57 : */
58 : void DropDocumentReference();
59 :
60 : /**
61 : * Methods for creating nodeinfo's from atoms and/or strings.
62 : */
63 : already_AddRefed<mozilla::dom::NodeInfo>
64 : GetNodeInfo(nsIAtom *aName, nsIAtom *aPrefix, int32_t aNamespaceID,
65 : uint16_t aNodeType, nsIAtom* aExtraName = nullptr);
66 : nsresult GetNodeInfo(const nsAString& aName, nsIAtom *aPrefix,
67 : int32_t aNamespaceID, uint16_t aNodeType,
68 : mozilla::dom::NodeInfo** aNodeInfo);
69 : nsresult GetNodeInfo(const nsAString& aName, nsIAtom *aPrefix,
70 : const nsAString& aNamespaceURI, uint16_t aNodeType,
71 : mozilla::dom::NodeInfo** aNodeInfo);
72 :
73 : /**
74 : * Returns the nodeinfo for text nodes. Can return null if OOM.
75 : */
76 : already_AddRefed<mozilla::dom::NodeInfo> GetTextNodeInfo();
77 :
78 : /**
79 : * Returns the nodeinfo for comment nodes. Can return null if OOM.
80 : */
81 : already_AddRefed<mozilla::dom::NodeInfo> GetCommentNodeInfo();
82 :
83 : /**
84 : * Returns the nodeinfo for the document node. Can return null if OOM.
85 : */
86 : already_AddRefed<mozilla::dom::NodeInfo> GetDocumentNodeInfo();
87 :
88 : /**
89 : * Retrieve a pointer to the document that owns this node info
90 : * manager.
91 : */
92 1920 : nsIDocument* GetDocument() const
93 : {
94 1920 : return mDocument;
95 : }
96 :
97 : /**
98 : * Gets the principal of the document this nodeinfo manager belongs to.
99 : */
100 4553 : nsIPrincipal *DocumentPrincipal() const {
101 4553 : NS_ASSERTION(mPrincipal, "How'd that happen?");
102 4553 : return mPrincipal;
103 : }
104 :
105 : void RemoveNodeInfo(mozilla::dom::NodeInfo *aNodeInfo);
106 :
107 5773 : nsBindingManager* GetBindingManager() const
108 : {
109 5773 : return mBindingManager;
110 : }
111 :
112 : protected:
113 : friend class nsDocument;
114 : friend class nsXULPrototypeDocument;
115 : friend nsresult NS_NewDOMDocumentType(nsIDOMDocumentType** ,
116 : nsNodeInfoManager *,
117 : nsIAtom *,
118 : const nsAString& ,
119 : const nsAString& ,
120 : const nsAString& );
121 :
122 : /**
123 : * Sets the principal of the document this nodeinfo manager belongs to.
124 : */
125 : void SetDocumentPrincipal(nsIPrincipal *aPrincipal);
126 :
127 : private:
128 : static int NodeInfoInnerKeyCompare(const void *key1, const void *key2);
129 : static PLHashNumber GetNodeInfoInnerHashValue(const void *key);
130 : static int DropNodeInfoDocument(PLHashEntry *he, int hashIndex,
131 : void *arg);
132 :
133 : PLHashTable *mNodeInfoHash;
134 : nsIDocument * MOZ_NON_OWNING_REF mDocument; // WEAK
135 : uint32_t mNonDocumentNodeInfos;
136 : nsCOMPtr<nsIPrincipal> mPrincipal; // Never null after Init() succeeds.
137 : nsCOMPtr<nsIPrincipal> mDefaultPrincipal; // Never null after Init() succeeds
138 : mozilla::dom::NodeInfo * MOZ_NON_OWNING_REF mTextNodeInfo; // WEAK to avoid circular ownership
139 : mozilla::dom::NodeInfo * MOZ_NON_OWNING_REF mCommentNodeInfo; // WEAK to avoid circular ownership
140 : mozilla::dom::NodeInfo * MOZ_NON_OWNING_REF mDocumentNodeInfo; // WEAK to avoid circular ownership
141 : RefPtr<nsBindingManager> mBindingManager;
142 : mozilla::dom::NodeInfo* mRecentlyUsedNodeInfos[RECENTLY_USED_NODEINFOS_SIZE];
143 : };
144 :
145 : #endif /* nsNodeInfoManager_h___ */
|