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"
15 CacheIndexIterator::CacheIndexIterator(CacheIndex
*aIndex
, bool aAddNew
)
20 LOG(("CacheIndexIterator::CacheIndexIterator() [this=%p]", this));
23 CacheIndexIterator::~CacheIndexIterator()
25 LOG(("CacheIndexIterator::~CacheIndexIterator() [this=%p]", this));
31 CacheIndexIterator::GetNextHash(SHA1Sum::Hash
*aHash
)
33 LOG(("CacheIndexIterator::GetNextHash() [this=%p]", this));
35 StaticMutexAutoLock
lock(CacheIndex::sLock
);
37 if (NS_FAILED(mStatus
)) {
41 if (!mRecords
.Length()) {
42 CloseInternal(NS_ERROR_NOT_AVAILABLE
);
46 memcpy(aHash
, mRecords
[mRecords
.Length() - 1]->mHash
, sizeof(SHA1Sum::Hash
));
47 mRecords
.RemoveLastElement();
53 CacheIndexIterator::Close()
55 LOG(("CacheIndexIterator::Close() [this=%p]", this));
57 StaticMutexAutoLock
lock(CacheIndex::sLock
);
59 return CloseInternal(NS_ERROR_NOT_AVAILABLE
);
63 CacheIndexIterator::CloseInternal(nsresult aStatus
)
65 LOG(("CacheIndexIterator::CloseInternal() [this=%p, status=0x%08" PRIx32
"]", this,
66 static_cast<uint32_t>(aStatus
)));
68 // Make sure status will be a failure
69 MOZ_ASSERT(NS_FAILED(aStatus
));
70 if (NS_SUCCEEDED(aStatus
)) {
71 aStatus
= NS_ERROR_UNEXPECTED
;
74 if (NS_FAILED(mStatus
)) {
75 return NS_ERROR_NOT_AVAILABLE
;
78 DebugOnly
<bool> removed
= mIndex
->mIterators
.RemoveElement(this);
86 CacheIndexIterator::AddRecord(CacheIndexRecord
*aRecord
)
88 LOG(("CacheIndexIterator::AddRecord() [this=%p, record=%p]", this, aRecord
));
90 mRecords
.AppendElement(aRecord
);
94 CacheIndexIterator::RemoveRecord(CacheIndexRecord
*aRecord
)
96 LOG(("CacheIndexIterator::RemoveRecord() [this=%p, record=%p]", this,
99 return mRecords
.RemoveElement(aRecord
);
103 CacheIndexIterator::ReplaceRecord(CacheIndexRecord
*aOldRecord
,
104 CacheIndexRecord
*aNewRecord
)
106 LOG(("CacheIndexIterator::ReplaceRecord() [this=%p, oldRecord=%p, "
107 "newRecord=%p]", this, aOldRecord
, aNewRecord
));
109 if (RemoveRecord(aOldRecord
)) {
110 AddRecord(aNewRecord
);
118 } // namespace mozilla