Line data Source code
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 "nsAutoPtr.h"
11 : #include "nsIAtom.h"
12 : #include "nsISerialEventTarget.h"
13 :
14 : #define RECENTLY_USED_PARSER_ATOMS_SIZE 31
15 :
16 : class nsHtml5Atom;
17 :
18 : class nsHtml5AtomEntry : public nsStringHashKey
19 : {
20 : public:
21 : explicit nsHtml5AtomEntry(KeyTypePointer aStr);
22 : nsHtml5AtomEntry(const nsHtml5AtomEntry& aOther);
23 : ~nsHtml5AtomEntry();
24 0 : inline nsHtml5Atom* GetAtom()
25 : {
26 0 : return mAtom;
27 : }
28 : private:
29 : nsAutoPtr<nsHtml5Atom> mAtom;
30 : };
31 :
32 : /**
33 : * nsHtml5AtomTable provides non-locking lookup and creation of atoms for
34 : * nsHtml5Parser or nsHtml5StreamParser.
35 : *
36 : * The hashtable holds dynamically allocated atoms that are private to an
37 : * instance of nsHtml5Parser or nsHtml5StreamParser. (Static atoms are used on
38 : * interned nsHtml5ElementNames and interned nsHtml5AttributeNames. Also, when
39 : * the doctype name is 'html', that identifier needs to be represented as a
40 : * static atom.)
41 : *
42 : * Each instance of nsHtml5Parser has a single instance of nsHtml5AtomTable,
43 : * and each instance of nsHtml5StreamParser has a single instance of
44 : * nsHtml5AtomTable. Dynamic atoms obtained from an nsHtml5AtomTable are valid
45 : * for == comparison with each other or with atoms declared in nsHtml5Atoms
46 : * within the nsHtml5Tokenizer and the nsHtml5TreeBuilder instances owned by
47 : * the same nsHtml5Parser/nsHtml5StreamParser instance that owns the
48 : * nsHtml5AtomTable instance.
49 : *
50 : * Dynamic atoms (atoms whose IsStaticAtom() returns false) obtained from
51 : * nsHtml5AtomTable must be re-obtained from another atom table when there's a
52 : * need to migrate atoms from an nsHtml5Parser to its nsHtml5StreamParser
53 : * (re-obtain from the other nsHtml5AtomTable), from an nsHtml5Parser to its
54 : * owner nsHtml5Parser (re-obtain from the other nsHtml5AtomTable) or from the
55 : * parser to the DOM (re-obtain from the application-wide atom table). To
56 : * re-obtain an atom from another atom table, obtain a string from the atom
57 : * using ToString(nsAString&) and look up an atom in the other table using that
58 : * string.
59 : *
60 : * An instance of nsHtml5AtomTable that belongs to an nsHtml5Parser is only
61 : * accessed from the main thread. An instance of nsHtml5AtomTable that belongs
62 : * to an nsHtml5StreamParser is accessed both from the main thread and from the
63 : * thread that executes the runnables of the nsHtml5StreamParser instance.
64 : * However, the threads never access the nsHtml5AtomTable instance concurrently
65 : * in the nsHtml5StreamParser case.
66 : *
67 : * Methods on the atoms obtained from nsHtml5AtomTable may be called on any
68 : * thread, although they only need to be called on the main thread or on the
69 : * thread working for the nsHtml5StreamParser when nsHtml5AtomTable belongs to
70 : * an nsHtml5StreamParser.
71 : *
72 : * Dynamic atoms obtained from nsHtml5AtomTable are deleted when the
73 : * nsHtml5AtomTable itself is destructed, which happens when the owner
74 : * nsHtml5Parser or nsHtml5StreamParser is destructed.
75 : */
76 : class nsHtml5AtomTable
77 : {
78 : public:
79 : nsHtml5AtomTable();
80 : ~nsHtml5AtomTable();
81 :
82 : /**
83 : * Obtains the atom for the given string in the scope of this atom table.
84 : */
85 : nsIAtom* GetAtom(const nsAString& aKey);
86 :
87 : /**
88 : * Empties the table.
89 : */
90 0 : void Clear()
91 : {
92 0 : for (uint32_t i = 0; i < RECENTLY_USED_PARSER_ATOMS_SIZE; ++i) {
93 0 : mRecentlyUsedParserAtoms[i] = nullptr;
94 : }
95 0 : mTable.Clear();
96 0 : }
97 :
98 : #ifdef DEBUG
99 4 : void SetPermittedLookupEventTarget(nsISerialEventTarget* aEventTarget)
100 : {
101 4 : mPermittedLookupEventTarget = aEventTarget;
102 4 : }
103 : #endif
104 :
105 : private:
106 : nsTHashtable<nsHtml5AtomEntry> mTable;
107 : nsIAtom* mRecentlyUsedParserAtoms[RECENTLY_USED_PARSER_ATOMS_SIZE];
108 : #ifdef DEBUG
109 : nsCOMPtr<nsISerialEventTarget> mPermittedLookupEventTarget;
110 : #endif
111 : };
112 :
113 : #endif // nsHtml5AtomTable_h
|