2014-04-10 14:47:20 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "CacheLog.h"
|
|
|
|
#include "CacheIndexIterator.h"
|
|
|
|
#include "CacheIndex.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
|
|
|
CacheIndexIterator::CacheIndexIterator(CacheIndex *aIndex, bool aAddNew)
|
|
|
|
: mStatus(NS_OK)
|
|
|
|
, mIndex(aIndex)
|
|
|
|
, mAddNew(aAddNew)
|
|
|
|
{
|
|
|
|
LOG(("CacheIndexIterator::CacheIndexIterator() [this=%p]", this));
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheIndexIterator::~CacheIndexIterator()
|
|
|
|
{
|
|
|
|
LOG(("CacheIndexIterator::~CacheIndexIterator() [this=%p]", this));
|
|
|
|
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CacheIndexIterator::GetNextHash(SHA1Sum::Hash *aHash)
|
|
|
|
{
|
|
|
|
LOG(("CacheIndexIterator::GetNextHash() [this=%p]", this));
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoLock lock(CacheIndex::sLock);
|
2014-04-10 14:47:20 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(mStatus)) {
|
|
|
|
return mStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mRecords.Length()) {
|
|
|
|
CloseInternal(NS_ERROR_NOT_AVAILABLE);
|
|
|
|
return mStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(aHash, mRecords[mRecords.Length() - 1]->mHash, sizeof(SHA1Sum::Hash));
|
|
|
|
mRecords.RemoveElementAt(mRecords.Length() - 1);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CacheIndexIterator::Close()
|
|
|
|
{
|
|
|
|
LOG(("CacheIndexIterator::Close() [this=%p]", this));
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoLock lock(CacheIndex::sLock);
|
2014-04-10 14:47:20 +04:00
|
|
|
|
|
|
|
return CloseInternal(NS_ERROR_NOT_AVAILABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CacheIndexIterator::CloseInternal(nsresult aStatus)
|
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("CacheIndexIterator::CloseInternal() [this=%p, status=0x%08" PRIx32 "]", this,
|
|
|
|
static_cast<uint32_t>(aStatus)));
|
2014-04-10 14:47:20 +04:00
|
|
|
|
|
|
|
// Make sure status will be a failure
|
|
|
|
MOZ_ASSERT(NS_FAILED(aStatus));
|
|
|
|
if (NS_SUCCEEDED(aStatus)) {
|
|
|
|
aStatus = NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(mStatus)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
DebugOnly<bool> removed = mIndex->mIterators.RemoveElement(this);
|
|
|
|
MOZ_ASSERT(removed);
|
|
|
|
mStatus = aStatus;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CacheIndexIterator::AddRecord(CacheIndexRecord *aRecord)
|
|
|
|
{
|
|
|
|
LOG(("CacheIndexIterator::AddRecord() [this=%p, record=%p]", this, aRecord));
|
|
|
|
|
|
|
|
mRecords.AppendElement(aRecord);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CacheIndexIterator::RemoveRecord(CacheIndexRecord *aRecord)
|
|
|
|
{
|
|
|
|
LOG(("CacheIndexIterator::RemoveRecord() [this=%p, record=%p]", this,
|
|
|
|
aRecord));
|
|
|
|
|
|
|
|
return mRecords.RemoveElement(aRecord);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CacheIndexIterator::ReplaceRecord(CacheIndexRecord *aOldRecord,
|
|
|
|
CacheIndexRecord *aNewRecord)
|
|
|
|
{
|
|
|
|
LOG(("CacheIndexIterator::ReplaceRecord() [this=%p, oldRecord=%p, "
|
|
|
|
"newRecord=%p]", this, aOldRecord, aNewRecord));
|
|
|
|
|
|
|
|
if (RemoveRecord(aOldRecord)) {
|
|
|
|
AddRecord(aNewRecord);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|