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 CacheIndexIterator__h__
6 #define CacheIndexIterator__h__
7 
8 #include "nsTArray.h"
9 #include "nsCOMPtr.h"
10 #include "mozilla/SHA1.h"
11 #include "mozilla/StaticMutex.h"
12 
13 namespace mozilla {
14 namespace net {
15 
16 class CacheIndex;
17 class CacheIndexRecordWrapper;
18 
19 class CacheIndexIterator {
20  public:
21   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CacheIndexIterator)
22 
23   CacheIndexIterator(CacheIndex* aIndex, bool aAddNew);
24 
25  protected:
26   virtual ~CacheIndexIterator();
27 
28  public:
29   // Returns a hash of a next entry. If there is no entry NS_ERROR_NOT_AVAILABLE
30   // is returned and the iterator is closed. Other error is returned when the
31   // iterator is closed for other reason, e.g. shutdown.
32   nsresult GetNextHash(SHA1Sum::Hash* aHash);
33 
34   // Closes the iterator. This means the iterator is removed from the list of
35   // iterators in CacheIndex.
36   nsresult Close();
37 
38  protected:
39   friend class CacheIndex;
40 
41   nsresult CloseInternal(nsresult aStatus);
42 
ShouldBeNewAdded()43   bool ShouldBeNewAdded() { return mAddNew; }
44   virtual void AddRecord(CacheIndexRecordWrapper* aRecord,
45                          const StaticMutexAutoLock& aProofOfLock);
46   bool RemoveRecord(CacheIndexRecordWrapper* aRecord,
47                     const StaticMutexAutoLock& aProofOfLock);
48   bool ReplaceRecord(CacheIndexRecordWrapper* aOldRecord,
49                      CacheIndexRecordWrapper* aNewRecord,
50                      const StaticMutexAutoLock& aProofOfLock);
51   void ClearRecords(const StaticMutexAutoLock& aProofOfLock);
52 
53   nsresult mStatus;
54   RefPtr<CacheIndex> mIndex;
55   nsTArray<RefPtr<CacheIndexRecordWrapper>> mRecords;
56   bool mAddNew;
57 };
58 
59 }  // namespace net
60 }  // namespace mozilla
61 
62 #endif
63