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 #include "CacheLog.h"
6 #include "CacheIndexIterator.h"
7 #include "CacheIndex.h"
8 #include "nsString.h"
9 #include "mozilla/DebugOnly.h"
10
11 namespace mozilla::net {
12
CacheIndexIterator(CacheIndex * aIndex,bool aAddNew)13 CacheIndexIterator::CacheIndexIterator(CacheIndex* aIndex, bool aAddNew)
14 : mStatus(NS_OK), mIndex(aIndex), mAddNew(aAddNew) {
15 LOG(("CacheIndexIterator::CacheIndexIterator() [this=%p]", this));
16 }
17
~CacheIndexIterator()18 CacheIndexIterator::~CacheIndexIterator() {
19 LOG(("CacheIndexIterator::~CacheIndexIterator() [this=%p]", this));
20
21 StaticMutexAutoLock lock(CacheIndex::sLock);
22 ClearRecords(lock);
23 CloseInternal(NS_ERROR_NOT_AVAILABLE);
24 }
25
GetNextHash(SHA1Sum::Hash * aHash)26 nsresult CacheIndexIterator::GetNextHash(SHA1Sum::Hash* aHash) {
27 LOG(("CacheIndexIterator::GetNextHash() [this=%p]", this));
28
29 StaticMutexAutoLock lock(CacheIndex::sLock);
30
31 if (NS_FAILED(mStatus)) {
32 return mStatus;
33 }
34
35 if (!mRecords.Length()) {
36 CloseInternal(NS_ERROR_NOT_AVAILABLE);
37 return mStatus;
38 }
39
40 memcpy(aHash, mRecords.PopLastElement()->Get()->mHash, sizeof(SHA1Sum::Hash));
41
42 return NS_OK;
43 }
44
Close()45 nsresult CacheIndexIterator::Close() {
46 LOG(("CacheIndexIterator::Close() [this=%p]", this));
47
48 StaticMutexAutoLock lock(CacheIndex::sLock);
49
50 return CloseInternal(NS_ERROR_NOT_AVAILABLE);
51 }
52
CloseInternal(nsresult aStatus)53 nsresult CacheIndexIterator::CloseInternal(nsresult aStatus) {
54 LOG(("CacheIndexIterator::CloseInternal() [this=%p, status=0x%08" PRIx32 "]",
55 this, static_cast<uint32_t>(aStatus)));
56
57 // Make sure status will be a failure
58 MOZ_ASSERT(NS_FAILED(aStatus));
59 if (NS_SUCCEEDED(aStatus)) {
60 aStatus = NS_ERROR_UNEXPECTED;
61 }
62
63 if (NS_FAILED(mStatus)) {
64 return NS_ERROR_NOT_AVAILABLE;
65 }
66
67 DebugOnly<bool> removed = mIndex->mIterators.RemoveElement(this);
68 MOZ_ASSERT(removed);
69 mStatus = aStatus;
70
71 return NS_OK;
72 }
73
ClearRecords(const StaticMutexAutoLock & aProofOfLock)74 void CacheIndexIterator::ClearRecords(const StaticMutexAutoLock& aProofOfLock) {
75 mRecords.Clear();
76 }
77
AddRecord(CacheIndexRecordWrapper * aRecord,const StaticMutexAutoLock & aProofOfLock)78 void CacheIndexIterator::AddRecord(CacheIndexRecordWrapper* aRecord,
79 const StaticMutexAutoLock& aProofOfLock) {
80 LOG(("CacheIndexIterator::AddRecord() [this=%p, record=%p]", this, aRecord));
81
82 mRecords.AppendElement(aRecord);
83 }
84
RemoveRecord(CacheIndexRecordWrapper * aRecord,const StaticMutexAutoLock & aProofOfLock)85 bool CacheIndexIterator::RemoveRecord(CacheIndexRecordWrapper* aRecord,
86 const StaticMutexAutoLock& aProofOfLock) {
87 LOG(("CacheIndexIterator::RemoveRecord() [this=%p, record=%p]", this,
88 aRecord));
89
90 return mRecords.RemoveElement(aRecord);
91 }
92
ReplaceRecord(CacheIndexRecordWrapper * aOldRecord,CacheIndexRecordWrapper * aNewRecord,const StaticMutexAutoLock & aProofOfLock)93 bool CacheIndexIterator::ReplaceRecord(
94 CacheIndexRecordWrapper* aOldRecord, CacheIndexRecordWrapper* aNewRecord,
95 const StaticMutexAutoLock& aProofOfLock) {
96 LOG(
97 ("CacheIndexIterator::ReplaceRecord() [this=%p, oldRecord=%p, "
98 "newRecord=%p]",
99 this, aOldRecord, aNewRecord));
100
101 if (RemoveRecord(aOldRecord, aProofOfLock)) {
102 AddRecord(aNewRecord, aProofOfLock);
103 return true;
104 }
105
106 return false;
107 }
108
109 } // namespace mozilla::net
110