Bug 1869043 allow a device to be specified with MediaTrackGraph::NotifyWhenDeviceStar...
[gecko.git] / netwerk / dns / HostRecordQueue.h
blobdfa56d1c8870fb6340ca21d597bfc745561e9683
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/. */
6 #ifndef HostRecordQueue_h__
7 #define HostRecordQueue_h__
9 #include <functional>
10 #include "mozilla/Mutex.h"
11 #include "nsHostRecord.h"
12 #include "nsRefPtrHashtable.h"
14 namespace mozilla {
15 namespace net {
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;
24 uint32_t PendingCount() const { return mPendingCount; }
25 uint32_t EvictionQSize() const { return mEvictionQSize; }
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 // Called for removing the record from mEvictionQ. When this function is
38 // called, the record should be either in mEvictionQ or not in any queue.
39 void MaybeRenewHostRecord(nsHostRecord* aRec,
40 const MutexAutoLock& aProofOfLock);
41 // Called for clearing mEvictionQ.
42 void FlushEvictionQ(
43 nsRefPtrHashtable<nsGenericHashKey<nsHostKey>, nsHostRecord>& aDB,
44 const MutexAutoLock& aProofOfLock);
45 // Remove the record from the queue that contains it.
46 void MaybeRemoveFromQ(nsHostRecord* aRec, const MutexAutoLock& aProofOfLock);
47 // When the record's priority changes, move the record between pending queues.
48 void MoveToAnotherPendingQ(nsHostRecord* aRec, nsIDNSService::DNSFlags aFlags,
49 const MutexAutoLock& aProofOfLock);
50 // Returning the first record from one of the pending queue. When |aHighQOnly|
51 // is true, returning the record from mHighQ only. When false, return the
52 // record from mMediumQ or mLowQ.
53 already_AddRefed<nsHostRecord> Dequeue(bool aHighQOnly,
54 const MutexAutoLock& aProofOfLock);
55 // Clear all queues and is called only during shutdown. |aCallback| is invoked
56 // when a record is removed from a queue.
57 void ClearAll(const std::function<void(nsHostRecord*)>& aCallback,
58 const MutexAutoLock& aProofOfLock);
60 private:
61 Atomic<uint32_t> mPendingCount{0};
62 Atomic<uint32_t> mEvictionQSize{0};
63 LinkedList<RefPtr<nsHostRecord>> mHighQ;
64 LinkedList<RefPtr<nsHostRecord>> mMediumQ;
65 LinkedList<RefPtr<nsHostRecord>> mLowQ;
66 LinkedList<RefPtr<nsHostRecord>> mEvictionQ;
69 } // namespace net
70 } // namespace mozilla
72 #endif // HostRecordQueue_h__