Line data Source code
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 "nsDataHashtable.h"
11 : #include "nsHashKeys.h"
12 : #include "nsIAtom.h"
13 : #include "nsIDocument.h"
14 : #include "nsIObserver.h"
15 : #include "nsTArray.h"
16 :
17 : #include "mozilla/StaticPtr.h"
18 :
19 : class nsAString;
20 :
21 : /**
22 : * The Name Space Manager tracks the association between a NameSpace
23 : * URI and the int32_t runtime id. Mappings between NameSpaces and
24 : * NameSpace prefixes are managed by nsINameSpaces.
25 : *
26 : * All NameSpace URIs are stored in a global table so that IDs are
27 : * consistent accross the app. NameSpace IDs are only consistent at runtime
28 : * ie: they are not guaranteed to be consistent accross app sessions.
29 : *
30 : * The nsNameSpaceManager needs to have a live reference for as long as
31 : * the NameSpace IDs are needed.
32 : *
33 : */
34 :
35 3 : class nsNameSpaceManager final : public nsIObserver
36 : {
37 : public:
38 : NS_DECL_ISUPPORTS
39 : NS_DECL_NSIOBSERVER
40 : virtual nsresult RegisterNameSpace(const nsAString& aURI,
41 : int32_t& aNameSpaceID);
42 :
43 : virtual nsresult GetNameSpaceURI(int32_t aNameSpaceID, nsAString& aURI);
44 :
45 : // Returns the atom for the namespace URI associated with the given ID. The
46 : // ID must be within range and not be kNameSpaceID_None (i.e. zero);
47 : nsIAtom* NameSpaceURIAtom(int32_t aNameSpaceID) {
48 : MOZ_ASSERT(aNameSpaceID > 0);
49 : return NameSpaceURIAtomForServo(aNameSpaceID);
50 : }
51 :
52 : // NB: This function should only be called by Servo code (and the above
53 : // accessor), which uses the empty atom to represent kNameSpaceID_None.
54 0 : nsIAtom* NameSpaceURIAtomForServo(int32_t aNameSpaceID) {
55 0 : MOZ_ASSERT(aNameSpaceID >= 0);
56 0 : MOZ_ASSERT((int64_t) aNameSpaceID < (int64_t) mURIArray.Length());
57 0 : return mURIArray.ElementAt(aNameSpaceID);
58 : }
59 :
60 : int32_t GetNameSpaceID(const nsAString& aURI,
61 : bool aInChromeDoc);
62 : int32_t GetNameSpaceID(nsIAtom* aURI,
63 : bool aInChromeDoc);
64 :
65 : bool HasElementCreator(int32_t aNameSpaceID);
66 :
67 : static nsNameSpaceManager* GetInstance();
68 : bool mMathMLDisabled;
69 : bool mSVGDisabled;
70 :
71 : private:
72 : bool Init();
73 : nsresult AddNameSpace(already_AddRefed<nsIAtom> aURI, const int32_t aNameSpaceID);
74 : nsresult AddDisabledNameSpace(already_AddRefed<nsIAtom> aURI, const int32_t aNameSpaceID);
75 0 : ~nsNameSpaceManager() {};
76 :
77 : nsDataHashtable<nsISupportsHashKey, int32_t> mURIToIDTable;
78 : nsDataHashtable<nsISupportsHashKey, int32_t> mDisabledURIToIDTable;
79 : nsTArray<nsCOMPtr<nsIAtom>> mURIArray;
80 :
81 : static mozilla::StaticRefPtr<nsNameSpaceManager> sInstance;
82 : };
83 :
84 : #endif // nsNameSpaceManager_h___
|