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