/src/mozilla-central/parser/html/nsHtml5AtomTable.h
Line | Count | Source (jump to first uncovered line) |
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 | 0 | #define RECENTLY_USED_PARSER_ATOMS_SIZE 31 |
14 | | |
15 | | class nsHtml5AtomEntry : public nsStringHashKey |
16 | | { |
17 | | public: |
18 | | explicit nsHtml5AtomEntry(KeyTypePointer aStr); |
19 | | nsHtml5AtomEntry(nsHtml5AtomEntry&& aOther); |
20 | | ~nsHtml5AtomEntry(); |
21 | 0 | inline nsAtom* GetAtom() { return mAtom; } |
22 | | |
23 | | private: |
24 | | nsDynamicAtom* mAtom; |
25 | | }; |
26 | | |
27 | | /** |
28 | | * nsHtml5AtomTable provides non-locking lookup and creation of atoms for |
29 | | * nsHtml5Parser or nsHtml5StreamParser. |
30 | | * |
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 |
35 | | * static atom.) |
36 | | * |
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. |
44 | | * |
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 |
53 | | * string. |
54 | | * |
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. |
61 | | * |
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. |
66 | | * |
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. |
70 | | */ |
71 | | class nsHtml5AtomTable |
72 | | { |
73 | | public: |
74 | | nsHtml5AtomTable(); |
75 | | ~nsHtml5AtomTable(); |
76 | | |
77 | | /** |
78 | | * Obtains the atom for the given string in the scope of this atom table. |
79 | | */ |
80 | | nsAtom* GetAtom(const nsAString& aKey); |
81 | | |
82 | | /** |
83 | | * Empties the table. |
84 | | */ |
85 | | void Clear() |
86 | 0 | { |
87 | 0 | for (uint32_t i = 0; i < RECENTLY_USED_PARSER_ATOMS_SIZE; ++i) { |
88 | 0 | mRecentlyUsedParserAtoms[i] = nullptr; |
89 | 0 | } |
90 | 0 | mTable.Clear(); |
91 | 0 | } |
92 | | |
93 | | #ifdef DEBUG |
94 | | void SetPermittedLookupEventTarget(nsISerialEventTarget* aEventTarget) |
95 | | { |
96 | | mPermittedLookupEventTarget = aEventTarget; |
97 | | } |
98 | | #endif |
99 | | |
100 | | private: |
101 | | nsTHashtable<nsHtml5AtomEntry> mTable; |
102 | | nsAtom* mRecentlyUsedParserAtoms[RECENTLY_USED_PARSER_ATOMS_SIZE]; |
103 | | #ifdef DEBUG |
104 | | nsCOMPtr<nsISerialEventTarget> mPermittedLookupEventTarget; |
105 | | #endif |
106 | | }; |
107 | | |
108 | | #endif // nsHtml5AtomTable_h |