tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

CacheIndexIterator.cpp (3122B)


      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/. */
      4 
      5 #include "CacheLog.h"
      6 #include "CacheIndexIterator.h"
      7 #include "CacheIndex.h"
      8 #include "nsString.h"
      9 #include "mozilla/DebugOnly.h"
     10 
     11 namespace mozilla::net {
     12 
     13 CacheIndexIterator::CacheIndexIterator(CacheIndex* aIndex, bool aAddNew)
     14    : mStatus(NS_OK), mIndex(aIndex), mAddNew(aAddNew) {
     15  LOG(("CacheIndexIterator::CacheIndexIterator() [this=%p]", this));
     16 }
     17 
     18 CacheIndexIterator::~CacheIndexIterator() {
     19  LOG(("CacheIndexIterator::~CacheIndexIterator() [this=%p]", this));
     20 
     21  StaticMutexAutoLock lock(CacheIndex::sLock);
     22  ClearRecords(lock);
     23  CloseInternal(NS_ERROR_NOT_AVAILABLE);
     24 }
     25 
     26 nsresult CacheIndexIterator::GetNextHash(SHA1Sum::Hash* aHash) {
     27  LOG(("CacheIndexIterator::GetNextHash() [this=%p]", this));
     28 
     29  StaticMutexAutoLock lock(CacheIndex::sLock);
     30 
     31  if (NS_FAILED(mStatus)) {
     32    return mStatus;
     33  }
     34 
     35  if (!mRecords.Length()) {
     36    CloseInternal(NS_ERROR_NOT_AVAILABLE);
     37    return mStatus;
     38  }
     39 
     40  memcpy(aHash, mRecords.PopLastElement()->Get()->mHash, sizeof(SHA1Sum::Hash));
     41 
     42  return NS_OK;
     43 }
     44 
     45 nsresult CacheIndexIterator::Close() {
     46  LOG(("CacheIndexIterator::Close() [this=%p]", this));
     47 
     48  StaticMutexAutoLock lock(CacheIndex::sLock);
     49 
     50  return CloseInternal(NS_ERROR_NOT_AVAILABLE);
     51 }
     52 
     53 nsresult CacheIndexIterator::CloseInternal(nsresult aStatus) {
     54  LOG(("CacheIndexIterator::CloseInternal() [this=%p, status=0x%08" PRIx32 "]",
     55       this, static_cast<uint32_t>(aStatus)));
     56 
     57  // Make sure status will be a failure
     58  MOZ_ASSERT(NS_FAILED(aStatus));
     59  if (NS_SUCCEEDED(aStatus)) {
     60    aStatus = NS_ERROR_UNEXPECTED;
     61  }
     62 
     63  if (NS_FAILED(mStatus)) {
     64    return NS_ERROR_NOT_AVAILABLE;
     65  }
     66 
     67  CacheIndex::sLock.AssertCurrentThreadOwns();
     68  DebugOnly<bool> removed = mIndex->mIterators.RemoveElement(this);
     69  MOZ_ASSERT(removed);
     70  mStatus = aStatus;
     71 
     72  return NS_OK;
     73 }
     74 
     75 void CacheIndexIterator::ClearRecords(const StaticMutexAutoLock& aProofOfLock) {
     76  mRecords.Clear();
     77 }
     78 
     79 void CacheIndexIterator::AddRecord(CacheIndexRecordWrapper* aRecord,
     80                                   const StaticMutexAutoLock& aProofOfLock) {
     81  LOG(("CacheIndexIterator::AddRecord() [this=%p, record=%p]", this, aRecord));
     82 
     83  mRecords.AppendElement(aRecord);
     84 }
     85 
     86 bool CacheIndexIterator::RemoveRecord(CacheIndexRecordWrapper* aRecord,
     87                                      const StaticMutexAutoLock& aProofOfLock) {
     88  LOG(("CacheIndexIterator::RemoveRecord() [this=%p, record=%p]", this,
     89       aRecord));
     90 
     91  return mRecords.RemoveElement(aRecord);
     92 }
     93 
     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));
    101 
    102  if (RemoveRecord(aOldRecord, aProofOfLock)) {
    103    AddRecord(aNewRecord, aProofOfLock);
    104    return true;
    105  }
    106 
    107  return false;
    108 }
    109 
    110 }  // namespace mozilla::net