Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / netwerk / cache2 / CacheIndexIterator.cpp
blob5f1a78470cb78f94bc7c78ec97b2b83126603414
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/. */
5 #include "CacheLog.h"
6 #include "CacheIndexIterator.h"
7 #include "CacheIndex.h"
8 #include "nsString.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);
22 ClearRecords(lock);
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)) {
32 return mStatus;
35 if (!mRecords.Length()) {
36 CloseInternal(NS_ERROR_NOT_AVAILABLE);
37 return mStatus;
40 memcpy(aHash, mRecords.PopLastElement()->Get()->mHash, sizeof(SHA1Sum::Hash));
42 return NS_OK;
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);
69 MOZ_ASSERT(removed);
70 mStatus = aStatus;
72 return NS_OK;
75 void CacheIndexIterator::ClearRecords(const StaticMutexAutoLock& aProofOfLock) {
76 mRecords.Clear();
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,
89 aRecord));
91 return mRecords.RemoveElement(aRecord);
94 bool CacheIndexIterator::ReplaceRecord(
95 CacheIndexRecordWrapper* aOldRecord, CacheIndexRecordWrapper* aNewRecord,
96 const StaticMutexAutoLock& aProofOfLock) {
97 LOG(
98 ("CacheIndexIterator::ReplaceRecord() [this=%p, oldRecord=%p, "
99 "newRecord=%p]",
100 this, aOldRecord, aNewRecord));
102 if (RemoveRecord(aOldRecord, aProofOfLock)) {
103 AddRecord(aNewRecord, aProofOfLock);
104 return true;
107 return false;
110 } // namespace mozilla::net