1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is HTML Parser C++ Translator code.
15 : *
16 : * The Initial Developer of the Original Code is
17 : * Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2009
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Henri Sivonen <hsivonen@iki.fi>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef nsHtml5AtomTable_h_
39 : #define nsHtml5AtomTable_h_
40 :
41 : #include "nsHashKeys.h"
42 : #include "nsTHashtable.h"
43 : #include "nsAutoPtr.h"
44 : #include "nsIAtom.h"
45 : #include "nsIThread.h"
46 :
47 : class nsHtml5Atom;
48 :
49 : class nsHtml5AtomEntry : public nsStringHashKey
50 : {
51 : public:
52 : nsHtml5AtomEntry(KeyTypePointer aStr);
53 : nsHtml5AtomEntry(const nsHtml5AtomEntry& aOther);
54 : ~nsHtml5AtomEntry();
55 : inline nsHtml5Atom* GetAtom() {
56 : return mAtom;
57 : }
58 : private:
59 : nsAutoPtr<nsHtml5Atom> mAtom;
60 : };
61 :
62 : /**
63 : * nsHtml5AtomTable provides non-locking lookup and creation of atoms for
64 : * nsHtml5Parser or nsHtml5StreamParser.
65 : *
66 : * The hashtable holds dynamically allocated atoms that are private to an
67 : * instance of nsHtml5Parser or nsHtml5StreamParser. (Static atoms are used on
68 : * interned nsHtml5ElementNames and interned nsHtml5AttributeNames. Also, when
69 : * the doctype name is 'html', that identifier needs to be represented as a
70 : * static atom.)
71 : *
72 : * Each instance of nsHtml5Parser has a single instance of nsHtml5AtomTable,
73 : * and each instance of nsHtml5StreamParser has a single instance of
74 : * nsHtml5AtomTable. Dynamic atoms obtained from an nsHtml5AtomTable are valid
75 : * for == comparison with each other or with atoms declared in nsHtml5Atoms
76 : * within the nsHtml5Tokenizer and the nsHtml5TreeBuilder instances owned by
77 : * the same nsHtml5Parser/nsHtml5StreamParser instance that owns the
78 : * nsHtml5AtomTable instance.
79 : *
80 : * Dynamic atoms (atoms whose IsStaticAtom() returns false) obtained from
81 : * nsHtml5AtomTable must be re-obtained from another atom table when there's a
82 : * need to migrate atoms from an nsHtml5Parser to its nsHtml5StreamParser
83 : * (re-obtain from the other nsHtml5AtomTable), from an nsHtml5Parser to its
84 : * owner nsHtml5Parser (re-obtain from the other nsHtml5AtomTable) or from the
85 : * parser to the DOM (re-obtain from the application-wide atom table). To
86 : * re-obtain an atom from another atom table, obtain a string from the atom
87 : * using ToString(nsAString&) and look up an atom in the other table using that
88 : * string.
89 : *
90 : * An instance of nsHtml5AtomTable that belongs to an nsHtml5Parser is only
91 : * accessed from the main thread. An instance of nsHtml5AtomTable that belongs
92 : * to an nsHtml5StreamParser is accessed both from the main thread and from the
93 : * thread that executes the runnables of the nsHtml5StreamParser instance.
94 : * However, the threads never access the nsHtml5AtomTable instance concurrently
95 : * in the nsHtml5StreamParser case.
96 : *
97 : * Methods on the atoms obtained from nsHtml5AtomTable may be called on any
98 : * thread, although they only need to be called on the main thread or on the
99 : * thread working for the nsHtml5StreamParser when nsHtml5AtomTable belongs to
100 : * an nsHtml5StreamParser.
101 : *
102 : * Dynamic atoms obtained from nsHtml5AtomTable are deleted when the
103 : * nsHtml5AtomTable itself is destructed, which happens when the owner
104 : * nsHtml5Parser or nsHtml5StreamParser is destructed.
105 : */
106 : class nsHtml5AtomTable
107 : {
108 : public:
109 : nsHtml5AtomTable();
110 : ~nsHtml5AtomTable();
111 :
112 : /**
113 : * Must be called after the constructor before use. Returns true
114 : * when successful and false on OOM failure.
115 : */
116 12 : inline bool Init() {
117 12 : return mTable.Init();
118 : }
119 :
120 : /**
121 : * Obtains the atom for the given string in the scope of this atom table.
122 : */
123 : nsIAtom* GetAtom(const nsAString& aKey);
124 :
125 : /**
126 : * Empties the table.
127 : */
128 : void Clear() {
129 : mTable.Clear();
130 : }
131 :
132 : #ifdef DEBUG
133 : void SetPermittedLookupThread(nsIThread* aThread) {
134 : mPermittedLookupThread = aThread;
135 : }
136 : #endif
137 :
138 : private:
139 : nsTHashtable<nsHtml5AtomEntry> mTable;
140 : #ifdef DEBUG
141 : nsCOMPtr<nsIThread> mPermittedLookupThread;
142 : #endif
143 : };
144 :
145 : #endif // nsHtml5AtomTable_h_
|