/src/mozilla-central/netwerk/cache2/CacheIndexIterator.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
12 | | namespace mozilla { |
13 | | namespace net { |
14 | | |
15 | | CacheIndexIterator::CacheIndexIterator(CacheIndex *aIndex, bool aAddNew) |
16 | | : mStatus(NS_OK) |
17 | | , mIndex(aIndex) |
18 | | , mAddNew(aAddNew) |
19 | 0 | { |
20 | 0 | LOG(("CacheIndexIterator::CacheIndexIterator() [this=%p]", this)); |
21 | 0 | } |
22 | | |
23 | | CacheIndexIterator::~CacheIndexIterator() |
24 | 0 | { |
25 | 0 | LOG(("CacheIndexIterator::~CacheIndexIterator() [this=%p]", this)); |
26 | 0 |
|
27 | 0 | Close(); |
28 | 0 | } |
29 | | |
30 | | nsresult |
31 | | CacheIndexIterator::GetNextHash(SHA1Sum::Hash *aHash) |
32 | 0 | { |
33 | 0 | LOG(("CacheIndexIterator::GetNextHash() [this=%p]", this)); |
34 | 0 |
|
35 | 0 | StaticMutexAutoLock lock(CacheIndex::sLock); |
36 | 0 |
|
37 | 0 | if (NS_FAILED(mStatus)) { |
38 | 0 | return mStatus; |
39 | 0 | } |
40 | 0 | |
41 | 0 | if (!mRecords.Length()) { |
42 | 0 | CloseInternal(NS_ERROR_NOT_AVAILABLE); |
43 | 0 | return mStatus; |
44 | 0 | } |
45 | 0 | |
46 | 0 | memcpy(aHash, mRecords[mRecords.Length() - 1]->mHash, sizeof(SHA1Sum::Hash)); |
47 | 0 | mRecords.RemoveLastElement(); |
48 | 0 |
|
49 | 0 | return NS_OK; |
50 | 0 | } |
51 | | |
52 | | nsresult |
53 | | CacheIndexIterator::Close() |
54 | 0 | { |
55 | 0 | LOG(("CacheIndexIterator::Close() [this=%p]", this)); |
56 | 0 |
|
57 | 0 | StaticMutexAutoLock lock(CacheIndex::sLock); |
58 | 0 |
|
59 | 0 | return CloseInternal(NS_ERROR_NOT_AVAILABLE); |
60 | 0 | } |
61 | | |
62 | | nsresult |
63 | | CacheIndexIterator::CloseInternal(nsresult aStatus) |
64 | 0 | { |
65 | 0 | LOG(("CacheIndexIterator::CloseInternal() [this=%p, status=0x%08" PRIx32 "]", this, |
66 | 0 | static_cast<uint32_t>(aStatus))); |
67 | 0 |
|
68 | 0 | // Make sure status will be a failure |
69 | 0 | MOZ_ASSERT(NS_FAILED(aStatus)); |
70 | 0 | if (NS_SUCCEEDED(aStatus)) { |
71 | 0 | aStatus = NS_ERROR_UNEXPECTED; |
72 | 0 | } |
73 | 0 |
|
74 | 0 | if (NS_FAILED(mStatus)) { |
75 | 0 | return NS_ERROR_NOT_AVAILABLE; |
76 | 0 | } |
77 | 0 | |
78 | 0 | DebugOnly<bool> removed = mIndex->mIterators.RemoveElement(this); |
79 | 0 | MOZ_ASSERT(removed); |
80 | 0 | mStatus = aStatus; |
81 | 0 |
|
82 | 0 | return NS_OK; |
83 | 0 | } |
84 | | |
85 | | void |
86 | | CacheIndexIterator::AddRecord(CacheIndexRecord *aRecord) |
87 | 0 | { |
88 | 0 | LOG(("CacheIndexIterator::AddRecord() [this=%p, record=%p]", this, aRecord)); |
89 | 0 |
|
90 | 0 | mRecords.AppendElement(aRecord); |
91 | 0 | } |
92 | | |
93 | | bool |
94 | | CacheIndexIterator::RemoveRecord(CacheIndexRecord *aRecord) |
95 | 0 | { |
96 | 0 | LOG(("CacheIndexIterator::RemoveRecord() [this=%p, record=%p]", this, |
97 | 0 | aRecord)); |
98 | 0 |
|
99 | 0 | return mRecords.RemoveElement(aRecord); |
100 | 0 | } |
101 | | |
102 | | bool |
103 | | CacheIndexIterator::ReplaceRecord(CacheIndexRecord *aOldRecord, |
104 | | CacheIndexRecord *aNewRecord) |
105 | 0 | { |
106 | 0 | LOG(("CacheIndexIterator::ReplaceRecord() [this=%p, oldRecord=%p, " |
107 | 0 | "newRecord=%p]", this, aOldRecord, aNewRecord)); |
108 | 0 |
|
109 | 0 | if (RemoveRecord(aOldRecord)) { |
110 | 0 | AddRecord(aNewRecord); |
111 | 0 | return true; |
112 | 0 | } |
113 | 0 | |
114 | 0 | return false; |
115 | 0 | } |
116 | | |
117 | | } // namespace net |
118 | | } // namespace mozilla |