michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "CacheLog.h" michael@0: #include "CacheIndexIterator.h" michael@0: #include "CacheIndex.h" michael@0: #include "nsString.h" michael@0: #include "mozilla/DebugOnly.h" michael@0: michael@0: michael@0: namespace mozilla { michael@0: namespace net { michael@0: michael@0: CacheIndexIterator::CacheIndexIterator(CacheIndex *aIndex, bool aAddNew) michael@0: : mStatus(NS_OK) michael@0: , mIndex(aIndex) michael@0: , mAddNew(aAddNew) michael@0: { michael@0: LOG(("CacheIndexIterator::CacheIndexIterator() [this=%p]", this)); michael@0: } michael@0: michael@0: CacheIndexIterator::~CacheIndexIterator() michael@0: { michael@0: LOG(("CacheIndexIterator::~CacheIndexIterator() [this=%p]", this)); michael@0: michael@0: Close(); michael@0: } michael@0: michael@0: nsresult michael@0: CacheIndexIterator::GetNextHash(SHA1Sum::Hash *aHash) michael@0: { michael@0: LOG(("CacheIndexIterator::GetNextHash() [this=%p]", this)); michael@0: michael@0: CacheIndexAutoLock lock(mIndex); michael@0: michael@0: if (NS_FAILED(mStatus)) { michael@0: return mStatus; michael@0: } michael@0: michael@0: if (!mRecords.Length()) { michael@0: CloseInternal(NS_ERROR_NOT_AVAILABLE); michael@0: return mStatus; michael@0: } michael@0: michael@0: memcpy(aHash, mRecords[mRecords.Length() - 1]->mHash, sizeof(SHA1Sum::Hash)); michael@0: mRecords.RemoveElementAt(mRecords.Length() - 1); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: CacheIndexIterator::Close() michael@0: { michael@0: LOG(("CacheIndexIterator::Close() [this=%p]", this)); michael@0: michael@0: CacheIndexAutoLock lock(mIndex); michael@0: michael@0: return CloseInternal(NS_ERROR_NOT_AVAILABLE); michael@0: } michael@0: michael@0: nsresult michael@0: CacheIndexIterator::CloseInternal(nsresult aStatus) michael@0: { michael@0: LOG(("CacheIndexIterator::CloseInternal() [this=%p, status=0x%08x]", this, michael@0: aStatus)); michael@0: michael@0: // Make sure status will be a failure michael@0: MOZ_ASSERT(NS_FAILED(aStatus)); michael@0: if (NS_SUCCEEDED(aStatus)) { michael@0: aStatus = NS_ERROR_UNEXPECTED; michael@0: } michael@0: michael@0: if (NS_FAILED(mStatus)) { michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: } michael@0: michael@0: DebugOnly removed = mIndex->mIterators.RemoveElement(this); michael@0: MOZ_ASSERT(removed); michael@0: mStatus = aStatus; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: CacheIndexIterator::AddRecord(CacheIndexRecord *aRecord) michael@0: { michael@0: LOG(("CacheIndexIterator::AddRecord() [this=%p, record=%p]", this, aRecord)); michael@0: michael@0: mRecords.AppendElement(aRecord); michael@0: } michael@0: michael@0: void michael@0: CacheIndexIterator::AddRecords(const nsTArray &aRecords) michael@0: { michael@0: LOG(("CacheIndexIterator::AddRecords() [this=%p]", this)); michael@0: michael@0: mRecords.AppendElements(aRecords); michael@0: } michael@0: michael@0: bool michael@0: CacheIndexIterator::RemoveRecord(CacheIndexRecord *aRecord) michael@0: { michael@0: LOG(("CacheIndexIterator::RemoveRecord() [this=%p, record=%p]", this, michael@0: aRecord)); michael@0: michael@0: return mRecords.RemoveElement(aRecord); michael@0: } michael@0: michael@0: bool michael@0: CacheIndexIterator::ReplaceRecord(CacheIndexRecord *aOldRecord, michael@0: CacheIndexRecord *aNewRecord) michael@0: { michael@0: LOG(("CacheIndexIterator::ReplaceRecord() [this=%p, oldRecord=%p, " michael@0: "newRecord=%p]", this, aOldRecord, aNewRecord)); michael@0: michael@0: if (RemoveRecord(aOldRecord)) { michael@0: AddRecord(aNewRecord); michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: } // net michael@0: } // mozilla