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/. */
6 #include "CacheIndexIterator.h"
7 #include "CacheIndex.h"
9 #include "mozilla/DebugOnly.h"
11 namespace mozilla::net
{
13 CacheIndexIterator::CacheIndexIterator(CacheIndex
* aIndex
, bool aAddNew
)
14 : mStatus(NS_OK
), mIndex(aIndex
), mAddNew(aAddNew
) {
15 LOG(("CacheIndexIterator::CacheIndexIterator() [this=%p]", this));
18 CacheIndexIterator::~CacheIndexIterator() {
19 LOG(("CacheIndexIterator::~CacheIndexIterator() [this=%p]", this));
21 StaticMutexAutoLock
lock(CacheIndex::sLock
);
23 CloseInternal(NS_ERROR_NOT_AVAILABLE
);
26 nsresult
CacheIndexIterator::GetNextHash(SHA1Sum::Hash
* aHash
) {
27 LOG(("CacheIndexIterator::GetNextHash() [this=%p]", this));
29 StaticMutexAutoLock
lock(CacheIndex::sLock
);
31 if (NS_FAILED(mStatus
)) {
35 if (!mRecords
.Length()) {
36 CloseInternal(NS_ERROR_NOT_AVAILABLE
);
40 memcpy(aHash
, mRecords
.PopLastElement()->Get()->mHash
, sizeof(SHA1Sum::Hash
));
45 nsresult
CacheIndexIterator::Close() {
46 LOG(("CacheIndexIterator::Close() [this=%p]", this));
48 StaticMutexAutoLock
lock(CacheIndex::sLock
);
50 return CloseInternal(NS_ERROR_NOT_AVAILABLE
);
53 nsresult
CacheIndexIterator::CloseInternal(nsresult aStatus
) {
54 LOG(("CacheIndexIterator::CloseInternal() [this=%p, status=0x%08" PRIx32
"]",
55 this, static_cast<uint32_t>(aStatus
)));
57 // Make sure status will be a failure
58 MOZ_ASSERT(NS_FAILED(aStatus
));
59 if (NS_SUCCEEDED(aStatus
)) {
60 aStatus
= NS_ERROR_UNEXPECTED
;
63 if (NS_FAILED(mStatus
)) {
64 return NS_ERROR_NOT_AVAILABLE
;
67 CacheIndex::sLock
.AssertCurrentThreadOwns();
68 DebugOnly
<bool> removed
= mIndex
->mIterators
.RemoveElement(this);
75 void CacheIndexIterator::ClearRecords(const StaticMutexAutoLock
& aProofOfLock
) {
79 void CacheIndexIterator::AddRecord(CacheIndexRecordWrapper
* aRecord
,
80 const StaticMutexAutoLock
& aProofOfLock
) {
81 LOG(("CacheIndexIterator::AddRecord() [this=%p, record=%p]", this, aRecord
));
83 mRecords
.AppendElement(aRecord
);
86 bool CacheIndexIterator::RemoveRecord(CacheIndexRecordWrapper
* aRecord
,
87 const StaticMutexAutoLock
& aProofOfLock
) {
88 LOG(("CacheIndexIterator::RemoveRecord() [this=%p, record=%p]", this,
91 return mRecords
.RemoveElement(aRecord
);
94 bool CacheIndexIterator::ReplaceRecord(
95 CacheIndexRecordWrapper
* aOldRecord
, CacheIndexRecordWrapper
* aNewRecord
,
96 const StaticMutexAutoLock
& aProofOfLock
) {
98 ("CacheIndexIterator::ReplaceRecord() [this=%p, oldRecord=%p, "
100 this, aOldRecord
, aNewRecord
));
102 if (RemoveRecord(aOldRecord
, aProofOfLock
)) {
103 AddRecord(aNewRecord
, aProofOfLock
);
110 } // namespace mozilla::net