michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsHtml5AtomTable_h michael@0: #define nsHtml5AtomTable_h michael@0: michael@0: #include "nsHashKeys.h" michael@0: #include "nsTHashtable.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsIAtom.h" michael@0: #include "nsIThread.h" michael@0: michael@0: class nsHtml5Atom; michael@0: michael@0: class nsHtml5AtomEntry : public nsStringHashKey michael@0: { michael@0: public: michael@0: nsHtml5AtomEntry(KeyTypePointer aStr); michael@0: nsHtml5AtomEntry(const nsHtml5AtomEntry& aOther); michael@0: ~nsHtml5AtomEntry(); michael@0: inline nsHtml5Atom* GetAtom() michael@0: { michael@0: return mAtom; michael@0: } michael@0: private: michael@0: nsAutoPtr mAtom; michael@0: }; michael@0: michael@0: /** michael@0: * nsHtml5AtomTable provides non-locking lookup and creation of atoms for michael@0: * nsHtml5Parser or nsHtml5StreamParser. michael@0: * michael@0: * The hashtable holds dynamically allocated atoms that are private to an michael@0: * instance of nsHtml5Parser or nsHtml5StreamParser. (Static atoms are used on michael@0: * interned nsHtml5ElementNames and interned nsHtml5AttributeNames. Also, when michael@0: * the doctype name is 'html', that identifier needs to be represented as a michael@0: * static atom.) michael@0: * michael@0: * Each instance of nsHtml5Parser has a single instance of nsHtml5AtomTable, michael@0: * and each instance of nsHtml5StreamParser has a single instance of michael@0: * nsHtml5AtomTable. Dynamic atoms obtained from an nsHtml5AtomTable are valid michael@0: * for == comparison with each other or with atoms declared in nsHtml5Atoms michael@0: * within the nsHtml5Tokenizer and the nsHtml5TreeBuilder instances owned by michael@0: * the same nsHtml5Parser/nsHtml5StreamParser instance that owns the michael@0: * nsHtml5AtomTable instance. michael@0: * michael@0: * Dynamic atoms (atoms whose IsStaticAtom() returns false) obtained from michael@0: * nsHtml5AtomTable must be re-obtained from another atom table when there's a michael@0: * need to migrate atoms from an nsHtml5Parser to its nsHtml5StreamParser michael@0: * (re-obtain from the other nsHtml5AtomTable), from an nsHtml5Parser to its michael@0: * owner nsHtml5Parser (re-obtain from the other nsHtml5AtomTable) or from the michael@0: * parser to the DOM (re-obtain from the application-wide atom table). To michael@0: * re-obtain an atom from another atom table, obtain a string from the atom michael@0: * using ToString(nsAString&) and look up an atom in the other table using that michael@0: * string. michael@0: * michael@0: * An instance of nsHtml5AtomTable that belongs to an nsHtml5Parser is only michael@0: * accessed from the main thread. An instance of nsHtml5AtomTable that belongs michael@0: * to an nsHtml5StreamParser is accessed both from the main thread and from the michael@0: * thread that executes the runnables of the nsHtml5StreamParser instance. michael@0: * However, the threads never access the nsHtml5AtomTable instance concurrently michael@0: * in the nsHtml5StreamParser case. michael@0: * michael@0: * Methods on the atoms obtained from nsHtml5AtomTable may be called on any michael@0: * thread, although they only need to be called on the main thread or on the michael@0: * thread working for the nsHtml5StreamParser when nsHtml5AtomTable belongs to michael@0: * an nsHtml5StreamParser. michael@0: * michael@0: * Dynamic atoms obtained from nsHtml5AtomTable are deleted when the michael@0: * nsHtml5AtomTable itself is destructed, which happens when the owner michael@0: * nsHtml5Parser or nsHtml5StreamParser is destructed. michael@0: */ michael@0: class nsHtml5AtomTable michael@0: { michael@0: public: michael@0: nsHtml5AtomTable(); michael@0: ~nsHtml5AtomTable(); michael@0: michael@0: /** michael@0: * Obtains the atom for the given string in the scope of this atom table. michael@0: */ michael@0: nsIAtom* GetAtom(const nsAString& aKey); michael@0: michael@0: /** michael@0: * Empties the table. michael@0: */ michael@0: void Clear() michael@0: { michael@0: mTable.Clear(); michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: void SetPermittedLookupThread(nsIThread* aThread) michael@0: { michael@0: mPermittedLookupThread = aThread; michael@0: } michael@0: #endif michael@0: michael@0: private: michael@0: nsTHashtable mTable; michael@0: #ifdef DEBUG michael@0: nsCOMPtr mPermittedLookupThread; michael@0: #endif michael@0: }; michael@0: michael@0: #endif // nsHtml5AtomTable_h