1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/cache2/CacheIndexIterator.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,126 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "CacheLog.h" 1.9 +#include "CacheIndexIterator.h" 1.10 +#include "CacheIndex.h" 1.11 +#include "nsString.h" 1.12 +#include "mozilla/DebugOnly.h" 1.13 + 1.14 + 1.15 +namespace mozilla { 1.16 +namespace net { 1.17 + 1.18 +CacheIndexIterator::CacheIndexIterator(CacheIndex *aIndex, bool aAddNew) 1.19 + : mStatus(NS_OK) 1.20 + , mIndex(aIndex) 1.21 + , mAddNew(aAddNew) 1.22 +{ 1.23 + LOG(("CacheIndexIterator::CacheIndexIterator() [this=%p]", this)); 1.24 +} 1.25 + 1.26 +CacheIndexIterator::~CacheIndexIterator() 1.27 +{ 1.28 + LOG(("CacheIndexIterator::~CacheIndexIterator() [this=%p]", this)); 1.29 + 1.30 + Close(); 1.31 +} 1.32 + 1.33 +nsresult 1.34 +CacheIndexIterator::GetNextHash(SHA1Sum::Hash *aHash) 1.35 +{ 1.36 + LOG(("CacheIndexIterator::GetNextHash() [this=%p]", this)); 1.37 + 1.38 + CacheIndexAutoLock lock(mIndex); 1.39 + 1.40 + if (NS_FAILED(mStatus)) { 1.41 + return mStatus; 1.42 + } 1.43 + 1.44 + if (!mRecords.Length()) { 1.45 + CloseInternal(NS_ERROR_NOT_AVAILABLE); 1.46 + return mStatus; 1.47 + } 1.48 + 1.49 + memcpy(aHash, mRecords[mRecords.Length() - 1]->mHash, sizeof(SHA1Sum::Hash)); 1.50 + mRecords.RemoveElementAt(mRecords.Length() - 1); 1.51 + 1.52 + return NS_OK; 1.53 +} 1.54 + 1.55 +nsresult 1.56 +CacheIndexIterator::Close() 1.57 +{ 1.58 + LOG(("CacheIndexIterator::Close() [this=%p]", this)); 1.59 + 1.60 + CacheIndexAutoLock lock(mIndex); 1.61 + 1.62 + return CloseInternal(NS_ERROR_NOT_AVAILABLE); 1.63 +} 1.64 + 1.65 +nsresult 1.66 +CacheIndexIterator::CloseInternal(nsresult aStatus) 1.67 +{ 1.68 + LOG(("CacheIndexIterator::CloseInternal() [this=%p, status=0x%08x]", this, 1.69 + aStatus)); 1.70 + 1.71 + // Make sure status will be a failure 1.72 + MOZ_ASSERT(NS_FAILED(aStatus)); 1.73 + if (NS_SUCCEEDED(aStatus)) { 1.74 + aStatus = NS_ERROR_UNEXPECTED; 1.75 + } 1.76 + 1.77 + if (NS_FAILED(mStatus)) { 1.78 + return NS_ERROR_NOT_AVAILABLE; 1.79 + } 1.80 + 1.81 + DebugOnly<bool> removed = mIndex->mIterators.RemoveElement(this); 1.82 + MOZ_ASSERT(removed); 1.83 + mStatus = aStatus; 1.84 + 1.85 + return NS_OK; 1.86 +} 1.87 + 1.88 +void 1.89 +CacheIndexIterator::AddRecord(CacheIndexRecord *aRecord) 1.90 +{ 1.91 + LOG(("CacheIndexIterator::AddRecord() [this=%p, record=%p]", this, aRecord)); 1.92 + 1.93 + mRecords.AppendElement(aRecord); 1.94 +} 1.95 + 1.96 +void 1.97 +CacheIndexIterator::AddRecords(const nsTArray<CacheIndexRecord *> &aRecords) 1.98 +{ 1.99 + LOG(("CacheIndexIterator::AddRecords() [this=%p]", this)); 1.100 + 1.101 + mRecords.AppendElements(aRecords); 1.102 +} 1.103 + 1.104 +bool 1.105 +CacheIndexIterator::RemoveRecord(CacheIndexRecord *aRecord) 1.106 +{ 1.107 + LOG(("CacheIndexIterator::RemoveRecord() [this=%p, record=%p]", this, 1.108 + aRecord)); 1.109 + 1.110 + return mRecords.RemoveElement(aRecord); 1.111 +} 1.112 + 1.113 +bool 1.114 +CacheIndexIterator::ReplaceRecord(CacheIndexRecord *aOldRecord, 1.115 + CacheIndexRecord *aNewRecord) 1.116 +{ 1.117 + LOG(("CacheIndexIterator::ReplaceRecord() [this=%p, oldRecord=%p, " 1.118 + "newRecord=%p]", this, aOldRecord, aNewRecord)); 1.119 + 1.120 + if (RemoveRecord(aOldRecord)) { 1.121 + AddRecord(aNewRecord); 1.122 + return true; 1.123 + } 1.124 + 1.125 + return false; 1.126 +} 1.127 + 1.128 +} // net 1.129 +} // mozilla