nsNameSpaceManager.h (2905B)
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 #ifndef nsNameSpaceManager_h___ 8 #define nsNameSpaceManager_h___ 9 10 #include "mozilla/StaticPtr.h" 11 #include "nsStringFwd.h" 12 #include "nsTArray.h" 13 #include "nsTHashMap.h" 14 15 /** 16 * The Name Space Manager tracks the association between a NameSpace 17 * URI and the int32_t runtime id. Mappings between NameSpaces and 18 * NameSpace prefixes are managed by nsINameSpaces. 19 * 20 * All NameSpace URIs are stored in a global table so that IDs are 21 * consistent accross the app. NameSpace IDs are only consistent at runtime 22 * ie: they are not guaranteed to be consistent accross app sessions. 23 * 24 * The nsNameSpaceManager needs to have a live reference for as long as 25 * the NameSpace IDs are needed. 26 * 27 */ 28 29 class nsNameSpaceManager final { 30 public: 31 NS_INLINE_DECL_REFCOUNTING(nsNameSpaceManager) 32 33 nsresult RegisterNameSpace(const nsAString& aURI, int32_t& aNameSpaceID); 34 nsresult RegisterNameSpace(already_AddRefed<nsAtom> aURI, 35 int32_t& aNameSpaceID); 36 37 nsresult GetNameSpaceURI(int32_t aNameSpaceID, nsAString& aURI); 38 39 // Returns the atom for the namespace URI associated with the given ID. The 40 // ID must be within range and not be kNameSpaceID_None (i.e. zero); 41 // 42 // NB: The requirement of mapping from the first entry to the empty atom is 43 // necessary for Servo, though it can be removed if needed adding a branch in 44 // GeckoElement::get_namespace(). 45 nsAtom* NameSpaceURIAtom(int32_t aNameSpaceID) { 46 MOZ_ASSERT(aNameSpaceID > 0); 47 MOZ_ASSERT((int64_t)aNameSpaceID < (int64_t)mURIArray.Length()); 48 return mURIArray.ElementAt(aNameSpaceID); 49 } 50 51 int32_t GetNameSpaceID(const nsAString& aURI, bool aInChromeDoc); 52 int32_t GetNameSpaceID(nsAtom* aURI, bool aInChromeDoc); 53 54 static const char* GetNameSpaceDisplayName(uint32_t aNameSpaceID); 55 56 bool HasElementCreator(int32_t aNameSpaceID); 57 58 static nsNameSpaceManager* GetInstance(); 59 bool mMathMLDisabled; 60 bool mSVGDisabled; 61 62 private: 63 static void PrefChanged(const char* aPref, void* aSelf); 64 void PrefChanged(const char* aPref); 65 66 bool Init(); 67 nsresult AddNameSpace(already_AddRefed<nsAtom> aURI, 68 const int32_t aNameSpaceID); 69 nsresult AddDisabledNameSpace(already_AddRefed<nsAtom> aURI, 70 const int32_t aNameSpaceID); 71 ~nsNameSpaceManager() = default; 72 73 nsTHashMap<RefPtr<nsAtom>, int32_t> mURIToIDTable; 74 nsTHashMap<RefPtr<nsAtom>, int32_t> mDisabledURIToIDTable; 75 nsTArray<RefPtr<nsAtom>> mURIArray; 76 77 static mozilla::StaticRefPtr<nsNameSpaceManager> sInstance; 78 }; 79 80 #endif // nsNameSpaceManager_h___