|
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 * This Original Code has been modified by IBM Corporation. |
|
9 * Modifications made by IBM described herein are |
|
10 * Copyright (c) International Business Machines |
|
11 * Corporation, 2000 |
|
12 * |
|
13 * Modifications to Mozilla code or documentation |
|
14 * identified per MPL Section 3.3 |
|
15 * |
|
16 * Date Modified by Description of modification |
|
17 * 03/27/2000 IBM Corp. Added PR_CALLBACK for Optlink |
|
18 * use in OS2 |
|
19 */ |
|
20 |
|
21 #ifndef nsScriptNameSpaceManager_h__ |
|
22 #define nsScriptNameSpaceManager_h__ |
|
23 |
|
24 #include "mozilla/MemoryReporting.h" |
|
25 #include "nsIMemoryReporter.h" |
|
26 #include "nsIScriptNameSpaceManager.h" |
|
27 #include "nsString.h" |
|
28 #include "nsID.h" |
|
29 #include "pldhash.h" |
|
30 #include "nsDOMClassInfo.h" |
|
31 #include "nsIObserver.h" |
|
32 #include "nsWeakReference.h" |
|
33 #include "xpcpublic.h" |
|
34 |
|
35 |
|
36 struct nsGlobalNameStruct |
|
37 { |
|
38 struct ConstructorAlias |
|
39 { |
|
40 nsCID mCID; |
|
41 nsString mProtoName; |
|
42 nsGlobalNameStruct* mProto; |
|
43 }; |
|
44 |
|
45 enum nametype { |
|
46 eTypeNotInitialized, |
|
47 eTypeNewDOMBinding, |
|
48 eTypeInterface, |
|
49 eTypeProperty, |
|
50 eTypeNavigatorProperty, |
|
51 eTypeExternalConstructor, |
|
52 eTypeStaticNameSet, |
|
53 eTypeClassConstructor, |
|
54 eTypeClassProto, |
|
55 eTypeExternalClassInfoCreator, |
|
56 eTypeExternalClassInfo, |
|
57 eTypeExternalConstructorAlias |
|
58 } mType; |
|
59 |
|
60 // mChromeOnly is only used for structs that define non-WebIDL things |
|
61 // (possibly in addition to WebIDL ones). In particular, it's not even |
|
62 // initialized for eTypeNewDOMBinding structs. |
|
63 bool mChromeOnly : 1; |
|
64 bool mAllowXBL : 1; |
|
65 |
|
66 union { |
|
67 int32_t mDOMClassInfoID; // eTypeClassConstructor |
|
68 nsIID mIID; // eTypeInterface, eTypeClassProto |
|
69 nsExternalDOMClassInfoData* mData; // eTypeExternalClassInfo |
|
70 ConstructorAlias* mAlias; // eTypeExternalConstructorAlias |
|
71 nsCID mCID; // All other types except eTypeNewDOMBinding |
|
72 }; |
|
73 |
|
74 // For new style DOM bindings. |
|
75 union { |
|
76 mozilla::dom::DefineInterface mDefineDOMInterface; // for window |
|
77 mozilla::dom::ConstructNavigatorProperty mConstructNavigatorProperty; // for navigator |
|
78 }; |
|
79 // May be null if enabled unconditionally |
|
80 mozilla::dom::ConstructorEnabled* mConstructorEnabled; |
|
81 }; |
|
82 |
|
83 |
|
84 class nsIScriptContext; |
|
85 class nsICategoryManager; |
|
86 class GlobalNameMapEntry; |
|
87 |
|
88 |
|
89 class nsScriptNameSpaceManager : public nsIObserver, |
|
90 public nsSupportsWeakReference, |
|
91 public nsIMemoryReporter |
|
92 { |
|
93 public: |
|
94 NS_DECL_ISUPPORTS |
|
95 NS_DECL_NSIOBSERVER |
|
96 NS_DECL_NSIMEMORYREPORTER |
|
97 |
|
98 nsScriptNameSpaceManager(); |
|
99 virtual ~nsScriptNameSpaceManager(); |
|
100 |
|
101 nsresult Init(); |
|
102 nsresult InitForContext(nsIScriptContext *aContext); |
|
103 |
|
104 // Returns a nsGlobalNameStruct for aName, or null if one is not |
|
105 // found. The returned nsGlobalNameStruct is only guaranteed to be |
|
106 // valid until the next call to any of the methods in this class. |
|
107 // It also returns a pointer to the string buffer of the classname |
|
108 // in the nsGlobalNameStruct. |
|
109 const nsGlobalNameStruct* LookupName(const nsAString& aName, |
|
110 const char16_t **aClassName = nullptr) |
|
111 { |
|
112 return LookupNameInternal(aName, aClassName); |
|
113 } |
|
114 |
|
115 // Returns a nsGlobalNameStruct for the navigator property aName, or |
|
116 // null if one is not found. The returned nsGlobalNameStruct is only |
|
117 // guaranteed to be valid until the next call to any of the methods |
|
118 // in this class. |
|
119 const nsGlobalNameStruct* LookupNavigatorName(const nsAString& aName); |
|
120 |
|
121 nsresult RegisterClassName(const char *aClassName, |
|
122 int32_t aDOMClassInfoID, |
|
123 bool aPrivileged, |
|
124 bool aXBLAllowed, |
|
125 const char16_t **aResult); |
|
126 |
|
127 nsresult RegisterClassProto(const char *aClassName, |
|
128 const nsIID *aConstructorProtoIID, |
|
129 bool *aFoundOld); |
|
130 |
|
131 nsresult RegisterExternalInterfaces(bool aAsProto); |
|
132 |
|
133 nsresult RegisterExternalClassName(const char *aClassName, |
|
134 nsCID& aCID); |
|
135 |
|
136 // Register the info for an external class. aName must be static |
|
137 // data, it will not be deleted by the DOM code. |
|
138 nsresult RegisterDOMCIData(const char *aName, |
|
139 nsDOMClassInfoExternalConstructorFnc aConstructorFptr, |
|
140 const nsIID *aProtoChainInterface, |
|
141 const nsIID **aInterfaces, |
|
142 uint32_t aScriptableFlags, |
|
143 bool aHasClassInterface, |
|
144 const nsCID *aConstructorCID); |
|
145 |
|
146 nsGlobalNameStruct* GetConstructorProto(const nsGlobalNameStruct* aStruct); |
|
147 |
|
148 void RegisterDefineDOMInterface(const nsAFlatString& aName, |
|
149 mozilla::dom::DefineInterface aDefineDOMInterface, |
|
150 mozilla::dom::ConstructorEnabled* aConstructorEnabled); |
|
151 template<size_t N> |
|
152 void RegisterDefineDOMInterface(const char16_t (&aKey)[N], |
|
153 mozilla::dom::DefineInterface aDefineDOMInterface, |
|
154 mozilla::dom::ConstructorEnabled* aConstructorEnabled) |
|
155 { |
|
156 nsLiteralString key(aKey); |
|
157 return RegisterDefineDOMInterface(key, aDefineDOMInterface, |
|
158 aConstructorEnabled); |
|
159 } |
|
160 |
|
161 void RegisterNavigatorDOMConstructor(const nsAFlatString& aName, |
|
162 mozilla::dom::ConstructNavigatorProperty aNavConstructor, |
|
163 mozilla::dom::ConstructorEnabled* aConstructorEnabled); |
|
164 template<size_t N> |
|
165 void RegisterNavigatorDOMConstructor(const char16_t (&aKey)[N], |
|
166 mozilla::dom::ConstructNavigatorProperty aNavConstructor, |
|
167 mozilla::dom::ConstructorEnabled* aConstructorEnabled) |
|
168 { |
|
169 nsLiteralString key(aKey); |
|
170 return RegisterNavigatorDOMConstructor(key, aNavConstructor, |
|
171 aConstructorEnabled); |
|
172 } |
|
173 |
|
174 typedef PLDHashOperator |
|
175 (* NameEnumerator)(const nsAString& aGlobalName, |
|
176 const nsGlobalNameStruct& aGlobalNameStruct, |
|
177 void* aClosure); |
|
178 |
|
179 void EnumerateGlobalNames(NameEnumerator aEnumerator, |
|
180 void* aClosure); |
|
181 void EnumerateNavigatorNames(NameEnumerator aEnumerator, |
|
182 void* aClosure); |
|
183 |
|
184 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf); |
|
185 |
|
186 private: |
|
187 // Adds a new entry to the hash and returns the nsGlobalNameStruct |
|
188 // that aKey will be mapped to. If mType in the returned |
|
189 // nsGlobalNameStruct is != eTypeNotInitialized, an entry for aKey |
|
190 // already existed. |
|
191 nsGlobalNameStruct *AddToHash(PLDHashTable *aTable, const nsAString *aKey, |
|
192 const char16_t **aClassName = nullptr); |
|
193 nsGlobalNameStruct *AddToHash(PLDHashTable *aTable, const char *aKey, |
|
194 const char16_t **aClassName = nullptr) |
|
195 { |
|
196 NS_ConvertASCIItoUTF16 key(aKey); |
|
197 return AddToHash(aTable, &key, aClassName); |
|
198 } |
|
199 // Removes an existing entry from the hash. |
|
200 void RemoveFromHash(PLDHashTable *aTable, const nsAString *aKey); |
|
201 |
|
202 nsresult FillHash(nsICategoryManager *aCategoryManager, |
|
203 const char *aCategory); |
|
204 nsresult RegisterInterface(const char* aIfName, |
|
205 const nsIID *aIfIID, |
|
206 bool* aFoundOld); |
|
207 |
|
208 /** |
|
209 * Add a new category entry into the hash table. |
|
210 * Only some categories can be added (see the beginning of the definition). |
|
211 * The other ones will be ignored. |
|
212 * |
|
213 * @aCategoryManager Instance of the category manager service. |
|
214 * @aCategory Category where the entry comes from. |
|
215 * @aEntry The entry that should be added. |
|
216 */ |
|
217 nsresult AddCategoryEntryToHash(nsICategoryManager* aCategoryManager, |
|
218 const char* aCategory, |
|
219 nsISupports* aEntry); |
|
220 |
|
221 /** |
|
222 * Remove an existing category entry from the hash table. |
|
223 * Only some categories can be removed (see the beginning of the definition). |
|
224 * The other ones will be ignored. |
|
225 * |
|
226 * @aCategory Category where the entry will be removed from. |
|
227 * @aEntry The entry that should be removed. |
|
228 */ |
|
229 nsresult RemoveCategoryEntryFromHash(nsICategoryManager* aCategoryManager, |
|
230 const char* aCategory, |
|
231 nsISupports* aEntry); |
|
232 |
|
233 // common helper for AddCategoryEntryToHash and RemoveCategoryEntryFromHash |
|
234 nsresult OperateCategoryEntryHash(nsICategoryManager* aCategoryManager, |
|
235 const char* aCategory, |
|
236 nsISupports* aEntry, |
|
237 bool aRemove); |
|
238 |
|
239 nsGlobalNameStruct* LookupNameInternal(const nsAString& aName, |
|
240 const char16_t **aClassName = nullptr); |
|
241 |
|
242 PLDHashTable mGlobalNames; |
|
243 PLDHashTable mNavigatorNames; |
|
244 |
|
245 bool mIsInitialized; |
|
246 }; |
|
247 |
|
248 #endif /* nsScriptNameSpaceManager_h__ */ |