tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nsHtml5AtomTable.h (2231B)


      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 #define RECENTLY_USED_PARSER_ATOMS_SIZE 37
     14 
     15 /*
     16 * nsHtml5AtomTable provides an atom cache for nsHtml5Parser and
     17 * nsHtml5StreamParser.
     18 *
     19 * An instance of nsHtml5AtomTable that belongs to an nsHtml5Parser is only
     20 * accessed from the main thread. An instance of nsHtml5AtomTable that belongs
     21 * to an nsHtml5StreamParser is accessed both from the main thread and from the
     22 * thread that executes the runnables of the nsHtml5StreamParser instance.
     23 * However, the threads never access the nsHtml5AtomTable instance concurrently
     24 * in the nsHtml5StreamParser case.
     25 *
     26 * Methods on the atoms obtained from nsHtml5AtomTable may be called on any
     27 * thread, although they only need to be called on the main thread or on the
     28 * thread working for the nsHtml5StreamParser when nsHtml5AtomTable belongs to
     29 * an nsHtml5StreamParser.
     30 *
     31 * Atoms cached by nsHtml5AtomTable are released when Clear() is called or when
     32 * the nsHtml5AtomTable itself is destructed, which happens when the owner
     33 * nsHtml5Parser or nsHtml5StreamParser is destructed.
     34 */
     35 class nsHtml5AtomTable {
     36 public:
     37  nsHtml5AtomTable();
     38  ~nsHtml5AtomTable();
     39 
     40  // NOTE: We rely on mRecentlyUsedParserAtoms keeping alive the returned atom,
     41  // but the caller is responsible to take a reference before calling GetAtom
     42  // again.
     43  nsAtom* GetAtom(const nsAString& aKey);
     44 
     45  /**
     46   * Empties the table.
     47   */
     48  void Clear() {
     49    for (uint32_t i = 0; i < RECENTLY_USED_PARSER_ATOMS_SIZE; ++i) {
     50      mRecentlyUsedParserAtoms[i] = nullptr;
     51    }
     52  }
     53 
     54 #ifdef DEBUG
     55  void SetPermittedLookupEventTarget(nsISerialEventTarget* aEventTarget) {
     56    mPermittedLookupEventTarget = aEventTarget;
     57  }
     58 #endif
     59 
     60 private:
     61  RefPtr<nsAtom> mRecentlyUsedParserAtoms[RECENTLY_USED_PARSER_ATOMS_SIZE];
     62 #ifdef DEBUG
     63  nsCOMPtr<nsISerialEventTarget> mPermittedLookupEventTarget;
     64 #endif
     65 };
     66 
     67 #endif  // nsHtml5AtomTable_h