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