1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #ifndef nsHtml5AtomTable_h
6 #define nsHtml5AtomTable_h
7 
8 #include "nsHashKeys.h"
9 #include "nsTHashtable.h"
10 #include "nsAtom.h"
11 #include "nsISerialEventTarget.h"
12 
13 #define RECENTLY_USED_PARSER_ATOMS_SIZE 31
14 
15 /**
16  * nsHtml5AtomTable provides non-locking lookup and creation of atoms for
17  * nsHtml5Parser or nsHtml5StreamParser.
18  *
19  * The hashtable holds dynamically allocated atoms that are private to an
20  * instance of nsHtml5Parser or nsHtml5StreamParser. (Static atoms are used on
21  * interned nsHtml5ElementNames and interned nsHtml5AttributeNames. Also, when
22  * the doctype name is 'html', that identifier needs to be represented as a
23  * static atom.)
24  *
25  * Each instance of nsHtml5Parser has a single instance of nsHtml5AtomTable,
26  * and each instance of nsHtml5StreamParser has a single instance of
27  * nsHtml5AtomTable. Dynamic atoms obtained from an nsHtml5AtomTable are valid
28  * for == comparison with each other or with atoms declared in nsHtml5Atoms
29  * within the nsHtml5Tokenizer and the nsHtml5TreeBuilder instances owned by
30  * the same nsHtml5Parser/nsHtml5StreamParser instance that owns the
31  * nsHtml5AtomTable instance.
32  *
33  * Dynamic atoms (atoms whose IsStatic() returns false) obtained from
34  * nsHtml5AtomTable must be re-obtained from another atom table when there's a
35  * need to migrate atoms from an nsHtml5Parser to its nsHtml5StreamParser
36  * (re-obtain from the other nsHtml5AtomTable), from an nsHtml5Parser to its
37  * owner nsHtml5Parser (re-obtain from the other nsHtml5AtomTable) or from the
38  * parser to the DOM (re-obtain from the application-wide atom table). To
39  * re-obtain an atom from another atom table, obtain a string from the atom
40  * using ToString(nsAString&) and look up an atom in the other table using that
41  * string.
42  *
43  * An instance of nsHtml5AtomTable that belongs to an nsHtml5Parser is only
44  * accessed from the main thread. An instance of nsHtml5AtomTable that belongs
45  * to an nsHtml5StreamParser is accessed both from the main thread and from the
46  * thread that executes the runnables of the nsHtml5StreamParser instance.
47  * However, the threads never access the nsHtml5AtomTable instance concurrently
48  * in the nsHtml5StreamParser case.
49  *
50  * Methods on the atoms obtained from nsHtml5AtomTable may be called on any
51  * thread, although they only need to be called on the main thread or on the
52  * thread working for the nsHtml5StreamParser when nsHtml5AtomTable belongs to
53  * an nsHtml5StreamParser.
54  *
55  * Dynamic atoms obtained from nsHtml5AtomTable are deleted when the
56  * nsHtml5AtomTable itself is destructed, which happens when the owner
57  * nsHtml5Parser or nsHtml5StreamParser is destructed.
58  */
59 class nsHtml5AtomTable {
60  public:
61   nsHtml5AtomTable();
62   ~nsHtml5AtomTable();
63 
64   // NOTE: We rely on mRecentlyUsedParserAtoms keeping alive the returned atom,
65   // but the caller is responsible to take a reference before calling GetAtom
66   // again.
67   nsAtom* GetAtom(const nsAString& aKey);
68 
69   /**
70    * Empties the table.
71    */
Clear()72   void Clear() {
73     for (uint32_t i = 0; i < RECENTLY_USED_PARSER_ATOMS_SIZE; ++i) {
74       mRecentlyUsedParserAtoms[i] = nullptr;
75     }
76   }
77 
78 #ifdef DEBUG
SetPermittedLookupEventTarget(nsISerialEventTarget * aEventTarget)79   void SetPermittedLookupEventTarget(nsISerialEventTarget* aEventTarget) {
80     mPermittedLookupEventTarget = aEventTarget;
81   }
82 #endif
83 
84  private:
85   RefPtr<nsAtom> mRecentlyUsedParserAtoms[RECENTLY_USED_PARSER_ATOMS_SIZE];
86 #ifdef DEBUG
87   nsCOMPtr<nsISerialEventTarget> mPermittedLookupEventTarget;
88 #endif
89 };
90 
91 #endif  // nsHtml5AtomTable_h
92