Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "CacheLog.h" |
michael@0 | 6 | #include "CacheIndexIterator.h" |
michael@0 | 7 | #include "CacheIndex.h" |
michael@0 | 8 | #include "nsString.h" |
michael@0 | 9 | #include "mozilla/DebugOnly.h" |
michael@0 | 10 | |
michael@0 | 11 | |
michael@0 | 12 | namespace mozilla { |
michael@0 | 13 | namespace net { |
michael@0 | 14 | |
michael@0 | 15 | CacheIndexIterator::CacheIndexIterator(CacheIndex *aIndex, bool aAddNew) |
michael@0 | 16 | : mStatus(NS_OK) |
michael@0 | 17 | , mIndex(aIndex) |
michael@0 | 18 | , mAddNew(aAddNew) |
michael@0 | 19 | { |
michael@0 | 20 | LOG(("CacheIndexIterator::CacheIndexIterator() [this=%p]", this)); |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | CacheIndexIterator::~CacheIndexIterator() |
michael@0 | 24 | { |
michael@0 | 25 | LOG(("CacheIndexIterator::~CacheIndexIterator() [this=%p]", this)); |
michael@0 | 26 | |
michael@0 | 27 | Close(); |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | nsresult |
michael@0 | 31 | CacheIndexIterator::GetNextHash(SHA1Sum::Hash *aHash) |
michael@0 | 32 | { |
michael@0 | 33 | LOG(("CacheIndexIterator::GetNextHash() [this=%p]", this)); |
michael@0 | 34 | |
michael@0 | 35 | CacheIndexAutoLock lock(mIndex); |
michael@0 | 36 | |
michael@0 | 37 | if (NS_FAILED(mStatus)) { |
michael@0 | 38 | return mStatus; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | if (!mRecords.Length()) { |
michael@0 | 42 | CloseInternal(NS_ERROR_NOT_AVAILABLE); |
michael@0 | 43 | return mStatus; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | memcpy(aHash, mRecords[mRecords.Length() - 1]->mHash, sizeof(SHA1Sum::Hash)); |
michael@0 | 47 | mRecords.RemoveElementAt(mRecords.Length() - 1); |
michael@0 | 48 | |
michael@0 | 49 | return NS_OK; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | nsresult |
michael@0 | 53 | CacheIndexIterator::Close() |
michael@0 | 54 | { |
michael@0 | 55 | LOG(("CacheIndexIterator::Close() [this=%p]", this)); |
michael@0 | 56 | |
michael@0 | 57 | CacheIndexAutoLock lock(mIndex); |
michael@0 | 58 | |
michael@0 | 59 | return CloseInternal(NS_ERROR_NOT_AVAILABLE); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | nsresult |
michael@0 | 63 | CacheIndexIterator::CloseInternal(nsresult aStatus) |
michael@0 | 64 | { |
michael@0 | 65 | LOG(("CacheIndexIterator::CloseInternal() [this=%p, status=0x%08x]", this, |
michael@0 | 66 | aStatus)); |
michael@0 | 67 | |
michael@0 | 68 | // Make sure status will be a failure |
michael@0 | 69 | MOZ_ASSERT(NS_FAILED(aStatus)); |
michael@0 | 70 | if (NS_SUCCEEDED(aStatus)) { |
michael@0 | 71 | aStatus = NS_ERROR_UNEXPECTED; |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | if (NS_FAILED(mStatus)) { |
michael@0 | 75 | return NS_ERROR_NOT_AVAILABLE; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | DebugOnly<bool> removed = mIndex->mIterators.RemoveElement(this); |
michael@0 | 79 | MOZ_ASSERT(removed); |
michael@0 | 80 | mStatus = aStatus; |
michael@0 | 81 | |
michael@0 | 82 | return NS_OK; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | void |
michael@0 | 86 | CacheIndexIterator::AddRecord(CacheIndexRecord *aRecord) |
michael@0 | 87 | { |
michael@0 | 88 | LOG(("CacheIndexIterator::AddRecord() [this=%p, record=%p]", this, aRecord)); |
michael@0 | 89 | |
michael@0 | 90 | mRecords.AppendElement(aRecord); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | void |
michael@0 | 94 | CacheIndexIterator::AddRecords(const nsTArray<CacheIndexRecord *> &aRecords) |
michael@0 | 95 | { |
michael@0 | 96 | LOG(("CacheIndexIterator::AddRecords() [this=%p]", this)); |
michael@0 | 97 | |
michael@0 | 98 | mRecords.AppendElements(aRecords); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | bool |
michael@0 | 102 | CacheIndexIterator::RemoveRecord(CacheIndexRecord *aRecord) |
michael@0 | 103 | { |
michael@0 | 104 | LOG(("CacheIndexIterator::RemoveRecord() [this=%p, record=%p]", this, |
michael@0 | 105 | aRecord)); |
michael@0 | 106 | |
michael@0 | 107 | return mRecords.RemoveElement(aRecord); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | bool |
michael@0 | 111 | CacheIndexIterator::ReplaceRecord(CacheIndexRecord *aOldRecord, |
michael@0 | 112 | CacheIndexRecord *aNewRecord) |
michael@0 | 113 | { |
michael@0 | 114 | LOG(("CacheIndexIterator::ReplaceRecord() [this=%p, oldRecord=%p, " |
michael@0 | 115 | "newRecord=%p]", this, aOldRecord, aNewRecord)); |
michael@0 | 116 | |
michael@0 | 117 | if (RemoveRecord(aOldRecord)) { |
michael@0 | 118 | AddRecord(aNewRecord); |
michael@0 | 119 | return true; |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | return false; |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | } // net |
michael@0 | 126 | } // mozilla |