|
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 { |
|
20 LOG(("CacheIndexIterator::CacheIndexIterator() [this=%p]", this)); |
|
21 } |
|
22 |
|
23 CacheIndexIterator::~CacheIndexIterator() |
|
24 { |
|
25 LOG(("CacheIndexIterator::~CacheIndexIterator() [this=%p]", this)); |
|
26 |
|
27 Close(); |
|
28 } |
|
29 |
|
30 nsresult |
|
31 CacheIndexIterator::GetNextHash(SHA1Sum::Hash *aHash) |
|
32 { |
|
33 LOG(("CacheIndexIterator::GetNextHash() [this=%p]", this)); |
|
34 |
|
35 CacheIndexAutoLock lock(mIndex); |
|
36 |
|
37 if (NS_FAILED(mStatus)) { |
|
38 return mStatus; |
|
39 } |
|
40 |
|
41 if (!mRecords.Length()) { |
|
42 CloseInternal(NS_ERROR_NOT_AVAILABLE); |
|
43 return mStatus; |
|
44 } |
|
45 |
|
46 memcpy(aHash, mRecords[mRecords.Length() - 1]->mHash, sizeof(SHA1Sum::Hash)); |
|
47 mRecords.RemoveElementAt(mRecords.Length() - 1); |
|
48 |
|
49 return NS_OK; |
|
50 } |
|
51 |
|
52 nsresult |
|
53 CacheIndexIterator::Close() |
|
54 { |
|
55 LOG(("CacheIndexIterator::Close() [this=%p]", this)); |
|
56 |
|
57 CacheIndexAutoLock lock(mIndex); |
|
58 |
|
59 return CloseInternal(NS_ERROR_NOT_AVAILABLE); |
|
60 } |
|
61 |
|
62 nsresult |
|
63 CacheIndexIterator::CloseInternal(nsresult aStatus) |
|
64 { |
|
65 LOG(("CacheIndexIterator::CloseInternal() [this=%p, status=0x%08x]", this, |
|
66 aStatus)); |
|
67 |
|
68 // Make sure status will be a failure |
|
69 MOZ_ASSERT(NS_FAILED(aStatus)); |
|
70 if (NS_SUCCEEDED(aStatus)) { |
|
71 aStatus = NS_ERROR_UNEXPECTED; |
|
72 } |
|
73 |
|
74 if (NS_FAILED(mStatus)) { |
|
75 return NS_ERROR_NOT_AVAILABLE; |
|
76 } |
|
77 |
|
78 DebugOnly<bool> removed = mIndex->mIterators.RemoveElement(this); |
|
79 MOZ_ASSERT(removed); |
|
80 mStatus = aStatus; |
|
81 |
|
82 return NS_OK; |
|
83 } |
|
84 |
|
85 void |
|
86 CacheIndexIterator::AddRecord(CacheIndexRecord *aRecord) |
|
87 { |
|
88 LOG(("CacheIndexIterator::AddRecord() [this=%p, record=%p]", this, aRecord)); |
|
89 |
|
90 mRecords.AppendElement(aRecord); |
|
91 } |
|
92 |
|
93 void |
|
94 CacheIndexIterator::AddRecords(const nsTArray<CacheIndexRecord *> &aRecords) |
|
95 { |
|
96 LOG(("CacheIndexIterator::AddRecords() [this=%p]", this)); |
|
97 |
|
98 mRecords.AppendElements(aRecords); |
|
99 } |
|
100 |
|
101 bool |
|
102 CacheIndexIterator::RemoveRecord(CacheIndexRecord *aRecord) |
|
103 { |
|
104 LOG(("CacheIndexIterator::RemoveRecord() [this=%p, record=%p]", this, |
|
105 aRecord)); |
|
106 |
|
107 return mRecords.RemoveElement(aRecord); |
|
108 } |
|
109 |
|
110 bool |
|
111 CacheIndexIterator::ReplaceRecord(CacheIndexRecord *aOldRecord, |
|
112 CacheIndexRecord *aNewRecord) |
|
113 { |
|
114 LOG(("CacheIndexIterator::ReplaceRecord() [this=%p, oldRecord=%p, " |
|
115 "newRecord=%p]", this, aOldRecord, aNewRecord)); |
|
116 |
|
117 if (RemoveRecord(aOldRecord)) { |
|
118 AddRecord(aNewRecord); |
|
119 return true; |
|
120 } |
|
121 |
|
122 return false; |
|
123 } |
|
124 |
|
125 } // net |
|
126 } // mozilla |