tor-browser

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

HostRecordQueue.h (3235B)


      1 /* vim:set ts=4 sw=2 sts=2 et cin: */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #ifndef HostRecordQueue_h__
      7 #define HostRecordQueue_h__
      8 
      9 #include <functional>
     10 #include "mozilla/Mutex.h"
     11 #include "nsHostRecord.h"
     12 #include "nsRefPtrHashtable.h"
     13 
     14 namespace mozilla {
     15 namespace net {
     16 
     17 class HostRecordQueue final {
     18 public:
     19  HostRecordQueue() = default;
     20  ~HostRecordQueue() = default;
     21  HostRecordQueue(const HostRecordQueue& aCopy) = delete;
     22  HostRecordQueue& operator=(const HostRecordQueue& aCopy) = delete;
     23 
     24  uint32_t PendingCount() const { return mPendingCount; }
     25  uint32_t EvictionQSize() const { return mEvictionQSize; }
     26 
     27  // Insert the record to mHighQ or mMediumQ or mLowQ based on the record's
     28  // priority.
     29  void InsertRecord(nsHostRecord* aRec, nsIDNSService::DNSFlags aFlags,
     30                    const MutexAutoLock& aProofOfLock);
     31  // Insert the record to mEvictionQ. In theory, this function should be called
     32  // when the record is not in any queue.
     33  void AddToEvictionQ(
     34      nsHostRecord* aRec, uint32_t aMaxCacheEntries,
     35      nsRefPtrHashtable<nsGenericHashKey<nsHostKey>, nsHostRecord>& aDB,
     36      const MutexAutoLock& aProofOfLock);
     37 
     38  // Move aRec to the tail of mEvictionQ (the most-recently-used end).
     39  void MoveToEvictionQueueTail(nsHostRecord* aRec,
     40                               const MutexAutoLock& aProofOfLock);
     41 
     42  // Called for removing the record from mEvictionQ. When this function is
     43  // called, the record should be either in mEvictionQ or not in any queue.
     44  void MaybeRenewHostRecord(nsHostRecord* aRec,
     45                            const MutexAutoLock& aProofOfLock);
     46  // Called for clearing mEvictionQ.
     47  void FlushEvictionQ(
     48      nsRefPtrHashtable<nsGenericHashKey<nsHostKey>, nsHostRecord>& aDB,
     49      const MutexAutoLock& aProofOfLock);
     50  // Remove the record from the queue that contains it.
     51  void MaybeRemoveFromQ(nsHostRecord* aRec, const MutexAutoLock& aProofOfLock);
     52  // When the record's priority changes, move the record between pending queues.
     53  void MoveToAnotherPendingQ(nsHostRecord* aRec, nsIDNSService::DNSFlags aFlags,
     54                             const MutexAutoLock& aProofOfLock);
     55  // Returning the first record from one of the pending queue. When |aHighQOnly|
     56  // is true, returning the record from mHighQ only. When false, return the
     57  // record from mMediumQ or mLowQ.
     58  already_AddRefed<nsHostRecord> Dequeue(bool aHighQOnly,
     59                                         const MutexAutoLock& aProofOfLock);
     60  // Clear all queues and is called only during shutdown. |aCallback| is invoked
     61  // when a record is removed from a queue.
     62  void ClearAll(const std::function<void(nsHostRecord*)>& aCallback,
     63                const MutexAutoLock& aProofOfLock);
     64 
     65 private:
     66  Atomic<uint32_t> mPendingCount{0};
     67  Atomic<uint32_t> mEvictionQSize{0};
     68  LinkedList<RefPtr<nsHostRecord>> mHighQ;
     69  LinkedList<RefPtr<nsHostRecord>> mMediumQ;
     70  LinkedList<RefPtr<nsHostRecord>> mLowQ;
     71  LinkedList<RefPtr<nsHostRecord>> mEvictionQ;
     72 };
     73 
     74 }  // namespace net
     75 }  // namespace mozilla
     76 
     77 #endif  // HostRecordQueue_h__