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