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/. */
5 #ifndef nsHtml5AtomTable_h
6 #define nsHtml5AtomTable_h
8 #include "nsHashKeys.h"
9 #include "nsTHashtable.h"
11 #include "nsISerialEventTarget.h"
13 #define RECENTLY_USED_PARSER_ATOMS_SIZE 31
15 class nsHtml5AtomEntry
: public nsStringHashKey
18 explicit nsHtml5AtomEntry(KeyTypePointer aStr
);
19 nsHtml5AtomEntry(const nsHtml5AtomEntry
& aOther
);
21 inline nsAtom
* GetAtom() { return mAtom
; }
28 * nsHtml5AtomTable provides non-locking lookup and creation of atoms for
29 * nsHtml5Parser or nsHtml5StreamParser.
31 * The hashtable holds dynamically allocated atoms that are private to an
32 * instance of nsHtml5Parser or nsHtml5StreamParser. (Static atoms are used on
33 * interned nsHtml5ElementNames and interned nsHtml5AttributeNames. Also, when
34 * the doctype name is 'html', that identifier needs to be represented as a
37 * Each instance of nsHtml5Parser has a single instance of nsHtml5AtomTable,
38 * and each instance of nsHtml5StreamParser has a single instance of
39 * nsHtml5AtomTable. Dynamic atoms obtained from an nsHtml5AtomTable are valid
40 * for == comparison with each other or with atoms declared in nsHtml5Atoms
41 * within the nsHtml5Tokenizer and the nsHtml5TreeBuilder instances owned by
42 * the same nsHtml5Parser/nsHtml5StreamParser instance that owns the
43 * nsHtml5AtomTable instance.
45 * Dynamic atoms (atoms whose IsStatic() returns false) obtained from
46 * nsHtml5AtomTable must be re-obtained from another atom table when there's a
47 * need to migrate atoms from an nsHtml5Parser to its nsHtml5StreamParser
48 * (re-obtain from the other nsHtml5AtomTable), from an nsHtml5Parser to its
49 * owner nsHtml5Parser (re-obtain from the other nsHtml5AtomTable) or from the
50 * parser to the DOM (re-obtain from the application-wide atom table). To
51 * re-obtain an atom from another atom table, obtain a string from the atom
52 * using ToString(nsAString&) and look up an atom in the other table using that
55 * An instance of nsHtml5AtomTable that belongs to an nsHtml5Parser is only
56 * accessed from the main thread. An instance of nsHtml5AtomTable that belongs
57 * to an nsHtml5StreamParser is accessed both from the main thread and from the
58 * thread that executes the runnables of the nsHtml5StreamParser instance.
59 * However, the threads never access the nsHtml5AtomTable instance concurrently
60 * in the nsHtml5StreamParser case.
62 * Methods on the atoms obtained from nsHtml5AtomTable may be called on any
63 * thread, although they only need to be called on the main thread or on the
64 * thread working for the nsHtml5StreamParser when nsHtml5AtomTable belongs to
65 * an nsHtml5StreamParser.
67 * Dynamic atoms obtained from nsHtml5AtomTable are deleted when the
68 * nsHtml5AtomTable itself is destructed, which happens when the owner
69 * nsHtml5Parser or nsHtml5StreamParser is destructed.
71 class nsHtml5AtomTable
78 * Obtains the atom for the given string in the scope of this atom table.
80 nsAtom
* GetAtom(const nsAString
& aKey
);
87 for (uint32_t i
= 0; i
< RECENTLY_USED_PARSER_ATOMS_SIZE
; ++i
) {
88 mRecentlyUsedParserAtoms
[i
] = nullptr;
94 void SetPermittedLookupEventTarget(nsISerialEventTarget
* aEventTarget
)
96 mPermittedLookupEventTarget
= aEventTarget
;
101 nsTHashtable
<nsHtml5AtomEntry
> mTable
;
102 nsAtom
* mRecentlyUsedParserAtoms
[RECENTLY_USED_PARSER_ATOMS_SIZE
];
104 nsCOMPtr
<nsISerialEventTarget
> mPermittedLookupEventTarget
;
108 #endif // nsHtml5AtomTable_h