OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/http/mock_http_cache.h" | 5 #include "net/http/mock_http_cache.h" |
6 | 6 |
| 7 #include <limits> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/location.h" | 10 #include "base/location.h" |
9 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
10 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
11 #include "net/base/completion_callback.h" | 13 #include "net/base/completion_callback.h" |
12 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
14 | 16 |
15 namespace net { | 17 namespace net { |
16 | 18 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 } | 81 } |
80 | 82 |
81 base::Time MockDiskEntry::GetLastUsed() const { | 83 base::Time MockDiskEntry::GetLastUsed() const { |
82 return base::Time::FromInternalValue(0); | 84 return base::Time::FromInternalValue(0); |
83 } | 85 } |
84 | 86 |
85 base::Time MockDiskEntry::GetLastModified() const { | 87 base::Time MockDiskEntry::GetLastModified() const { |
86 return base::Time::FromInternalValue(0); | 88 return base::Time::FromInternalValue(0); |
87 } | 89 } |
88 | 90 |
89 int32 MockDiskEntry::GetDataSize(int index) const { | 91 int32_t MockDiskEntry::GetDataSize(int index) const { |
90 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices); | 92 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices); |
91 return static_cast<int32>(data_[index].size()); | 93 return static_cast<int32_t>(data_[index].size()); |
92 } | 94 } |
93 | 95 |
94 int MockDiskEntry::ReadData(int index, | 96 int MockDiskEntry::ReadData(int index, |
95 int offset, | 97 int offset, |
96 IOBuffer* buf, | 98 IOBuffer* buf, |
97 int buf_len, | 99 int buf_len, |
98 const CompletionCallback& callback) { | 100 const CompletionCallback& callback) { |
99 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices); | 101 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices); |
100 DCHECK(!callback.is_null()); | 102 DCHECK(!callback.is_null()); |
101 | 103 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 if (buf_len) | 141 if (buf_len) |
140 memcpy(&data_[index][offset], buf->data(), buf_len); | 142 memcpy(&data_[index][offset], buf->data(), buf_len); |
141 | 143 |
142 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE) | 144 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE) |
143 return buf_len; | 145 return buf_len; |
144 | 146 |
145 CallbackLater(callback, buf_len); | 147 CallbackLater(callback, buf_len); |
146 return ERR_IO_PENDING; | 148 return ERR_IO_PENDING; |
147 } | 149 } |
148 | 150 |
149 int MockDiskEntry::ReadSparseData(int64 offset, | 151 int MockDiskEntry::ReadSparseData(int64_t offset, |
150 IOBuffer* buf, | 152 IOBuffer* buf, |
151 int buf_len, | 153 int buf_len, |
152 const CompletionCallback& callback) { | 154 const CompletionCallback& callback) { |
153 DCHECK(!callback.is_null()); | 155 DCHECK(!callback.is_null()); |
154 if (fail_sparse_requests_) | 156 if (fail_sparse_requests_) |
155 return ERR_NOT_IMPLEMENTED; | 157 return ERR_NOT_IMPLEMENTED; |
156 if (!sparse_ || busy_ || cancel_) | 158 if (!sparse_ || busy_ || cancel_) |
157 return ERR_CACHE_OPERATION_NOT_SUPPORTED; | 159 return ERR_CACHE_OPERATION_NOT_SUPPORTED; |
158 if (offset < 0) | 160 if (offset < 0) |
159 return ERR_FAILED; | 161 return ERR_FAILED; |
160 | 162 |
161 if (fail_requests_) | 163 if (fail_requests_) |
162 return ERR_CACHE_READ_FAILURE; | 164 return ERR_CACHE_READ_FAILURE; |
163 | 165 |
164 DCHECK(offset < kint32max); | 166 DCHECK(offset < std::numeric_limits<int32_t>::max()); |
165 int real_offset = static_cast<int>(offset); | 167 int real_offset = static_cast<int>(offset); |
166 if (!buf_len) | 168 if (!buf_len) |
167 return 0; | 169 return 0; |
168 | 170 |
169 int num = std::min(static_cast<int>(data_[1].size()) - real_offset, | 171 int num = std::min(static_cast<int>(data_[1].size()) - real_offset, |
170 buf_len); | 172 buf_len); |
171 memcpy(buf->data(), &data_[1][real_offset], num); | 173 memcpy(buf->data(), &data_[1][real_offset], num); |
172 | 174 |
173 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ) | 175 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ) |
174 return num; | 176 return num; |
175 | 177 |
176 CallbackLater(callback, num); | 178 CallbackLater(callback, num); |
177 busy_ = true; | 179 busy_ = true; |
178 delayed_ = false; | 180 delayed_ = false; |
179 return ERR_IO_PENDING; | 181 return ERR_IO_PENDING; |
180 } | 182 } |
181 | 183 |
182 int MockDiskEntry::WriteSparseData(int64 offset, | 184 int MockDiskEntry::WriteSparseData(int64_t offset, |
183 IOBuffer* buf, | 185 IOBuffer* buf, |
184 int buf_len, | 186 int buf_len, |
185 const CompletionCallback& callback) { | 187 const CompletionCallback& callback) { |
186 DCHECK(!callback.is_null()); | 188 DCHECK(!callback.is_null()); |
187 if (fail_sparse_requests_) | 189 if (fail_sparse_requests_) |
188 return ERR_NOT_IMPLEMENTED; | 190 return ERR_NOT_IMPLEMENTED; |
189 if (busy_ || cancel_) | 191 if (busy_ || cancel_) |
190 return ERR_CACHE_OPERATION_NOT_SUPPORTED; | 192 return ERR_CACHE_OPERATION_NOT_SUPPORTED; |
191 if (!sparse_) { | 193 if (!sparse_) { |
192 if (data_[1].size()) | 194 if (data_[1].size()) |
193 return ERR_CACHE_OPERATION_NOT_SUPPORTED; | 195 return ERR_CACHE_OPERATION_NOT_SUPPORTED; |
194 sparse_ = true; | 196 sparse_ = true; |
195 } | 197 } |
196 if (offset < 0) | 198 if (offset < 0) |
197 return ERR_FAILED; | 199 return ERR_FAILED; |
198 if (!buf_len) | 200 if (!buf_len) |
199 return 0; | 201 return 0; |
200 | 202 |
201 if (fail_requests_) | 203 if (fail_requests_) |
202 return ERR_CACHE_READ_FAILURE; | 204 return ERR_CACHE_READ_FAILURE; |
203 | 205 |
204 DCHECK(offset < kint32max); | 206 DCHECK(offset < std::numeric_limits<int32_t>::max()); |
205 int real_offset = static_cast<int>(offset); | 207 int real_offset = static_cast<int>(offset); |
206 | 208 |
207 if (static_cast<int>(data_[1].size()) < real_offset + buf_len) | 209 if (static_cast<int>(data_[1].size()) < real_offset + buf_len) |
208 data_[1].resize(real_offset + buf_len); | 210 data_[1].resize(real_offset + buf_len); |
209 | 211 |
210 memcpy(&data_[1][real_offset], buf->data(), buf_len); | 212 memcpy(&data_[1][real_offset], buf->data(), buf_len); |
211 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE) | 213 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE) |
212 return buf_len; | 214 return buf_len; |
213 | 215 |
214 CallbackLater(callback, buf_len); | 216 CallbackLater(callback, buf_len); |
215 return ERR_IO_PENDING; | 217 return ERR_IO_PENDING; |
216 } | 218 } |
217 | 219 |
218 int MockDiskEntry::GetAvailableRange(int64 offset, | 220 int MockDiskEntry::GetAvailableRange(int64_t offset, |
219 int len, | 221 int len, |
220 int64* start, | 222 int64_t* start, |
221 const CompletionCallback& callback) { | 223 const CompletionCallback& callback) { |
222 DCHECK(!callback.is_null()); | 224 DCHECK(!callback.is_null()); |
223 if (!sparse_ || busy_ || cancel_) | 225 if (!sparse_ || busy_ || cancel_) |
224 return ERR_CACHE_OPERATION_NOT_SUPPORTED; | 226 return ERR_CACHE_OPERATION_NOT_SUPPORTED; |
225 if (offset < 0) | 227 if (offset < 0) |
226 return ERR_FAILED; | 228 return ERR_FAILED; |
227 | 229 |
228 if (fail_requests_) | 230 if (fail_requests_) |
229 return ERR_CACHE_READ_FAILURE; | 231 return ERR_CACHE_READ_FAILURE; |
230 | 232 |
231 *start = offset; | 233 *start = offset; |
232 DCHECK(offset < kint32max); | 234 DCHECK(offset < std::numeric_limits<int32_t>::max()); |
233 int real_offset = static_cast<int>(offset); | 235 int real_offset = static_cast<int>(offset); |
234 if (static_cast<int>(data_[1].size()) < real_offset) | 236 if (static_cast<int>(data_[1].size()) < real_offset) |
235 return 0; | 237 return 0; |
236 | 238 |
237 int num = std::min(static_cast<int>(data_[1].size()) - real_offset, len); | 239 int num = std::min(static_cast<int>(data_[1].size()) - real_offset, len); |
238 int count = 0; | 240 int count = 0; |
239 for (; num > 0; num--, real_offset++) { | 241 for (; num > 0; num--, real_offset++) { |
240 if (!count) { | 242 if (!count) { |
241 if (data_[1][real_offset]) { | 243 if (data_[1][real_offset]) { |
242 count++; | 244 count++; |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 } | 363 } |
362 | 364 |
363 MockDiskCache::~MockDiskCache() { | 365 MockDiskCache::~MockDiskCache() { |
364 ReleaseAll(); | 366 ReleaseAll(); |
365 } | 367 } |
366 | 368 |
367 CacheType MockDiskCache::GetCacheType() const { | 369 CacheType MockDiskCache::GetCacheType() const { |
368 return DISK_CACHE; | 370 return DISK_CACHE; |
369 } | 371 } |
370 | 372 |
371 int32 MockDiskCache::GetEntryCount() const { | 373 int32_t MockDiskCache::GetEntryCount() const { |
372 return static_cast<int32>(entries_.size()); | 374 return static_cast<int32_t>(entries_.size()); |
373 } | 375 } |
374 | 376 |
375 int MockDiskCache::OpenEntry(const std::string& key, | 377 int MockDiskCache::OpenEntry(const std::string& key, |
376 disk_cache::Entry** entry, | 378 disk_cache::Entry** entry, |
377 const CompletionCallback& callback) { | 379 const CompletionCallback& callback) { |
378 DCHECK(!callback.is_null()); | 380 DCHECK(!callback.is_null()); |
379 if (fail_requests_) | 381 if (fail_requests_) |
380 return ERR_CACHE_OPEN_FAILURE; | 382 return ERR_CACHE_OPEN_FAILURE; |
381 | 383 |
382 EntryMap::iterator it = entries_.find(key); | 384 EntryMap::iterator it = entries_.find(key); |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
667 if (!callback_.is_null()) { | 669 if (!callback_.is_null()) { |
668 if (!fail_) | 670 if (!fail_) |
669 backend_->reset(new MockDiskCache()); | 671 backend_->reset(new MockDiskCache()); |
670 CompletionCallback cb = callback_; | 672 CompletionCallback cb = callback_; |
671 callback_.Reset(); | 673 callback_.Reset(); |
672 cb.Run(Result()); // This object can be deleted here. | 674 cb.Run(Result()); // This object can be deleted here. |
673 } | 675 } |
674 } | 676 } |
675 | 677 |
676 } // namespace net | 678 } // namespace net |
OLD | NEW |