| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "net/base/completion_callback.h" | 9 #include "net/base/completion_callback.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 } // namespace | 43 } // namespace |
| 44 | 44 |
| 45 //----------------------------------------------------------------------------- | 45 //----------------------------------------------------------------------------- |
| 46 | 46 |
| 47 struct MockDiskEntry::CallbackInfo { | 47 struct MockDiskEntry::CallbackInfo { |
| 48 scoped_refptr<MockDiskEntry> entry; | 48 scoped_refptr<MockDiskEntry> entry; |
| 49 net::CompletionCallback callback; | 49 net::CompletionCallback callback; |
| 50 int result; | 50 int result; |
| 51 }; | 51 }; |
| 52 | 52 |
| 53 MockDiskEntry::MockDiskEntry() | |
| 54 : test_mode_(0), doomed_(false), sparse_(false), | |
| 55 fail_requests_(false), busy_(false), delayed_(false) { | |
| 56 } | |
| 57 | |
| 58 MockDiskEntry::MockDiskEntry(const std::string& key) | 53 MockDiskEntry::MockDiskEntry(const std::string& key) |
| 59 : key_(key), doomed_(false), sparse_(false), | 54 : key_(key), doomed_(false), sparse_(false), |
| 60 fail_requests_(false), busy_(false), delayed_(false) { | 55 fail_requests_(false), fail_sparse_requests_(false), busy_(false), |
| 56 delayed_(false) { |
| 61 test_mode_ = GetTestModeForEntry(key); | 57 test_mode_ = GetTestModeForEntry(key); |
| 62 } | 58 } |
| 63 | 59 |
| 64 void MockDiskEntry::Doom() { | 60 void MockDiskEntry::Doom() { |
| 65 doomed_ = true; | 61 doomed_ = true; |
| 66 } | 62 } |
| 67 | 63 |
| 68 void MockDiskEntry::Close() { | 64 void MockDiskEntry::Close() { |
| 69 Release(); | 65 Release(); |
| 70 } | 66 } |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE) | 128 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE) |
| 133 return buf_len; | 129 return buf_len; |
| 134 | 130 |
| 135 CallbackLater(callback, buf_len); | 131 CallbackLater(callback, buf_len); |
| 136 return net::ERR_IO_PENDING; | 132 return net::ERR_IO_PENDING; |
| 137 } | 133 } |
| 138 | 134 |
| 139 int MockDiskEntry::ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len, | 135 int MockDiskEntry::ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len, |
| 140 const net::CompletionCallback& callback) { | 136 const net::CompletionCallback& callback) { |
| 141 DCHECK(!callback.is_null()); | 137 DCHECK(!callback.is_null()); |
| 138 if (fail_sparse_requests_) |
| 139 return net::ERR_NOT_IMPLEMENTED; |
| 142 if (!sparse_ || busy_) | 140 if (!sparse_ || busy_) |
| 143 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 141 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
| 144 if (offset < 0) | 142 if (offset < 0) |
| 145 return net::ERR_FAILED; | 143 return net::ERR_FAILED; |
| 146 | 144 |
| 147 if (fail_requests_) | 145 if (fail_requests_) |
| 148 return net::ERR_CACHE_READ_FAILURE; | 146 return net::ERR_CACHE_READ_FAILURE; |
| 149 | 147 |
| 150 DCHECK(offset < kint32max); | 148 DCHECK(offset < kint32max); |
| 151 int real_offset = static_cast<int>(offset); | 149 int real_offset = static_cast<int>(offset); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 162 CallbackLater(callback, num); | 160 CallbackLater(callback, num); |
| 163 busy_ = true; | 161 busy_ = true; |
| 164 delayed_ = false; | 162 delayed_ = false; |
| 165 return net::ERR_IO_PENDING; | 163 return net::ERR_IO_PENDING; |
| 166 } | 164 } |
| 167 | 165 |
| 168 int MockDiskEntry::WriteSparseData(int64 offset, net::IOBuffer* buf, | 166 int MockDiskEntry::WriteSparseData(int64 offset, net::IOBuffer* buf, |
| 169 int buf_len, | 167 int buf_len, |
| 170 const net::CompletionCallback& callback) { | 168 const net::CompletionCallback& callback) { |
| 171 DCHECK(!callback.is_null()); | 169 DCHECK(!callback.is_null()); |
| 170 if (fail_sparse_requests_) |
| 171 return net::ERR_NOT_IMPLEMENTED; |
| 172 if (busy_) | 172 if (busy_) |
| 173 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 173 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
| 174 if (!sparse_) { | 174 if (!sparse_) { |
| 175 if (data_[1].size()) | 175 if (data_[1].size()) |
| 176 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 176 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
| 177 sparse_ = true; | 177 sparse_ = true; |
| 178 } | 178 } |
| 179 if (offset < 0) | 179 if (offset < 0) |
| 180 return net::ERR_FAILED; | 180 return net::ERR_FAILED; |
| 181 if (!buf_len) | 181 if (!buf_len) |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 } | 230 } |
| 231 } | 231 } |
| 232 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE) | 232 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE) |
| 233 return count; | 233 return count; |
| 234 | 234 |
| 235 CallbackLater(callback, count); | 235 CallbackLater(callback, count); |
| 236 return net::ERR_IO_PENDING; | 236 return net::ERR_IO_PENDING; |
| 237 } | 237 } |
| 238 | 238 |
| 239 bool MockDiskEntry::CouldBeSparse() const { | 239 bool MockDiskEntry::CouldBeSparse() const { |
| 240 if (fail_sparse_requests_) |
| 241 return false; |
| 240 return sparse_; | 242 return sparse_; |
| 241 } | 243 } |
| 242 | 244 |
| 243 void MockDiskEntry::CancelSparseIO() { | 245 void MockDiskEntry::CancelSparseIO() { |
| 244 cancel_ = true; | 246 cancel_ = true; |
| 245 } | 247 } |
| 246 | 248 |
| 247 int MockDiskEntry::ReadyForSparseIO(const net::CompletionCallback& callback) { | 249 int MockDiskEntry::ReadyForSparseIO(const net::CompletionCallback& callback) { |
| 250 if (fail_sparse_requests_) |
| 251 return net::ERR_NOT_IMPLEMENTED; |
| 248 if (!cancel_) | 252 if (!cancel_) |
| 249 return net::OK; | 253 return net::OK; |
| 250 | 254 |
| 251 cancel_ = false; | 255 cancel_ = false; |
| 252 DCHECK(!callback.is_null()); | 256 DCHECK(!callback.is_null()); |
| 253 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ) | 257 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ) |
| 254 return net::OK; | 258 return net::OK; |
| 255 | 259 |
| 256 // The pending operation is already in the message loop (and hopefully | 260 // The pending operation is already in the message loop (and hopefully |
| 257 // already in the second pass). Just notify the caller that it finished. | 261 // already in the second pass). Just notify the caller that it finished. |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 } | 330 } |
| 327 | 331 |
| 328 // Statics. | 332 // Statics. |
| 329 bool MockDiskEntry::cancel_ = false; | 333 bool MockDiskEntry::cancel_ = false; |
| 330 bool MockDiskEntry::ignore_callbacks_ = false; | 334 bool MockDiskEntry::ignore_callbacks_ = false; |
| 331 | 335 |
| 332 //----------------------------------------------------------------------------- | 336 //----------------------------------------------------------------------------- |
| 333 | 337 |
| 334 MockDiskCache::MockDiskCache() | 338 MockDiskCache::MockDiskCache() |
| 335 : open_count_(0), create_count_(0), fail_requests_(false), | 339 : open_count_(0), create_count_(0), fail_requests_(false), |
| 336 soft_failures_(false), double_create_check_(true) { | 340 soft_failures_(false), double_create_check_(true), |
| 341 fail_sparse_requests_(false) { |
| 337 } | 342 } |
| 338 | 343 |
| 339 MockDiskCache::~MockDiskCache() { | 344 MockDiskCache::~MockDiskCache() { |
| 340 ReleaseAll(); | 345 ReleaseAll(); |
| 341 } | 346 } |
| 342 | 347 |
| 343 net::CacheType MockDiskCache::GetCacheType() const { | 348 net::CacheType MockDiskCache::GetCacheType() const { |
| 344 return net::DISK_CACHE; | 349 return net::DISK_CACHE; |
| 345 } | 350 } |
| 346 | 351 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 | 409 |
| 405 new_entry->AddRef(); | 410 new_entry->AddRef(); |
| 406 entries_[key] = new_entry; | 411 entries_[key] = new_entry; |
| 407 | 412 |
| 408 new_entry->AddRef(); | 413 new_entry->AddRef(); |
| 409 *entry = new_entry; | 414 *entry = new_entry; |
| 410 | 415 |
| 411 if (soft_failures_) | 416 if (soft_failures_) |
| 412 new_entry->set_fail_requests(); | 417 new_entry->set_fail_requests(); |
| 413 | 418 |
| 419 if (fail_sparse_requests_) |
| 420 new_entry->set_fail_sparse_requests(); |
| 421 |
| 414 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START) | 422 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START) |
| 415 return net::OK; | 423 return net::OK; |
| 416 | 424 |
| 417 CallbackLater(callback, net::OK); | 425 CallbackLater(callback, net::OK); |
| 418 return net::ERR_IO_PENDING; | 426 return net::ERR_IO_PENDING; |
| 419 } | 427 } |
| 420 | 428 |
| 421 int MockDiskCache::DoomEntry(const std::string& key, | 429 int MockDiskCache::DoomEntry(const std::string& key, |
| 422 const net::CompletionCallback& callback) { | 430 const net::CompletionCallback& callback) { |
| 423 DCHECK(!callback.is_null()); | 431 DCHECK(!callback.is_null()); |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 610 void MockBlockingBackendFactory::FinishCreation() { | 618 void MockBlockingBackendFactory::FinishCreation() { |
| 611 block_ = false; | 619 block_ = false; |
| 612 if (!callback_.is_null()) { | 620 if (!callback_.is_null()) { |
| 613 if (!fail_) | 621 if (!fail_) |
| 614 backend_->reset(new MockDiskCache()); | 622 backend_->reset(new MockDiskCache()); |
| 615 net::CompletionCallback cb = callback_; | 623 net::CompletionCallback cb = callback_; |
| 616 callback_.Reset(); | 624 callback_.Reset(); |
| 617 cb.Run(Result()); // This object can be deleted here. | 625 cb.Run(Result()); // This object can be deleted here. |
| 618 } | 626 } |
| 619 } | 627 } |
| OLD | NEW |