1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/parser/html/nsHtml5AtomTable.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,107 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#ifndef nsHtml5AtomTable_h 1.9 +#define nsHtml5AtomTable_h 1.10 + 1.11 +#include "nsHashKeys.h" 1.12 +#include "nsTHashtable.h" 1.13 +#include "nsAutoPtr.h" 1.14 +#include "nsIAtom.h" 1.15 +#include "nsIThread.h" 1.16 + 1.17 +class nsHtml5Atom; 1.18 + 1.19 +class nsHtml5AtomEntry : public nsStringHashKey 1.20 +{ 1.21 + public: 1.22 + nsHtml5AtomEntry(KeyTypePointer aStr); 1.23 + nsHtml5AtomEntry(const nsHtml5AtomEntry& aOther); 1.24 + ~nsHtml5AtomEntry(); 1.25 + inline nsHtml5Atom* GetAtom() 1.26 + { 1.27 + return mAtom; 1.28 + } 1.29 + private: 1.30 + nsAutoPtr<nsHtml5Atom> mAtom; 1.31 +}; 1.32 + 1.33 +/** 1.34 + * nsHtml5AtomTable provides non-locking lookup and creation of atoms for 1.35 + * nsHtml5Parser or nsHtml5StreamParser. 1.36 + * 1.37 + * The hashtable holds dynamically allocated atoms that are private to an 1.38 + * instance of nsHtml5Parser or nsHtml5StreamParser. (Static atoms are used on 1.39 + * interned nsHtml5ElementNames and interned nsHtml5AttributeNames. Also, when 1.40 + * the doctype name is 'html', that identifier needs to be represented as a 1.41 + * static atom.) 1.42 + * 1.43 + * Each instance of nsHtml5Parser has a single instance of nsHtml5AtomTable, 1.44 + * and each instance of nsHtml5StreamParser has a single instance of 1.45 + * nsHtml5AtomTable. Dynamic atoms obtained from an nsHtml5AtomTable are valid 1.46 + * for == comparison with each other or with atoms declared in nsHtml5Atoms 1.47 + * within the nsHtml5Tokenizer and the nsHtml5TreeBuilder instances owned by 1.48 + * the same nsHtml5Parser/nsHtml5StreamParser instance that owns the 1.49 + * nsHtml5AtomTable instance. 1.50 + * 1.51 + * Dynamic atoms (atoms whose IsStaticAtom() returns false) obtained from 1.52 + * nsHtml5AtomTable must be re-obtained from another atom table when there's a 1.53 + * need to migrate atoms from an nsHtml5Parser to its nsHtml5StreamParser 1.54 + * (re-obtain from the other nsHtml5AtomTable), from an nsHtml5Parser to its 1.55 + * owner nsHtml5Parser (re-obtain from the other nsHtml5AtomTable) or from the 1.56 + * parser to the DOM (re-obtain from the application-wide atom table). To 1.57 + * re-obtain an atom from another atom table, obtain a string from the atom 1.58 + * using ToString(nsAString&) and look up an atom in the other table using that 1.59 + * string. 1.60 + * 1.61 + * An instance of nsHtml5AtomTable that belongs to an nsHtml5Parser is only 1.62 + * accessed from the main thread. An instance of nsHtml5AtomTable that belongs 1.63 + * to an nsHtml5StreamParser is accessed both from the main thread and from the 1.64 + * thread that executes the runnables of the nsHtml5StreamParser instance. 1.65 + * However, the threads never access the nsHtml5AtomTable instance concurrently 1.66 + * in the nsHtml5StreamParser case. 1.67 + * 1.68 + * Methods on the atoms obtained from nsHtml5AtomTable may be called on any 1.69 + * thread, although they only need to be called on the main thread or on the 1.70 + * thread working for the nsHtml5StreamParser when nsHtml5AtomTable belongs to 1.71 + * an nsHtml5StreamParser. 1.72 + * 1.73 + * Dynamic atoms obtained from nsHtml5AtomTable are deleted when the 1.74 + * nsHtml5AtomTable itself is destructed, which happens when the owner 1.75 + * nsHtml5Parser or nsHtml5StreamParser is destructed. 1.76 + */ 1.77 +class nsHtml5AtomTable 1.78 +{ 1.79 + public: 1.80 + nsHtml5AtomTable(); 1.81 + ~nsHtml5AtomTable(); 1.82 + 1.83 + /** 1.84 + * Obtains the atom for the given string in the scope of this atom table. 1.85 + */ 1.86 + nsIAtom* GetAtom(const nsAString& aKey); 1.87 + 1.88 + /** 1.89 + * Empties the table. 1.90 + */ 1.91 + void Clear() 1.92 + { 1.93 + mTable.Clear(); 1.94 + } 1.95 + 1.96 +#ifdef DEBUG 1.97 + void SetPermittedLookupThread(nsIThread* aThread) 1.98 + { 1.99 + mPermittedLookupThread = aThread; 1.100 + } 1.101 +#endif 1.102 + 1.103 + private: 1.104 + nsTHashtable<nsHtml5AtomEntry> mTable; 1.105 +#ifdef DEBUG 1.106 + nsCOMPtr<nsIThread> mPermittedLookupThread; 1.107 +#endif 1.108 +}; 1.109 + 1.110 +#endif // nsHtml5AtomTable_h