Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(231)

Side by Side Diff: net/http/mock_http_cache.cc

Issue 22926031: Sparse IO: Allow failover to network in debug builds (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« net/http/http_cache_unittest.cc ('K') | « net/http/mock_http_cache.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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() 53 MockDiskEntry::MockDiskEntry(MockDiskCache* backend, const std::string& key)
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)
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), busy_(false), delayed_(false), backend_(backend) {
61 test_mode_ = GetTestModeForEntry(key); 56 test_mode_ = GetTestModeForEntry(key);
57 DCHECK(backend);
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 }
71 67
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 backend_->NotifySparseIO();
rvargas (doing something else) 2013/08/28 21:35:47 This is slightly problematic because the contract
pasko 2013/08/28 21:56:28 I did not quite understand this sentence, can you
rvargas (doing something else) 2013/08/28 22:33:40 The thing is that it is possible (valid, and used)
139 if (fail_sparse_requests_)
140 return net::ERR_NOT_IMPLEMENTED;
142 if (!sparse_ || busy_) 141 if (!sparse_ || busy_)
143 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; 142 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
144 if (offset < 0) 143 if (offset < 0)
145 return net::ERR_FAILED; 144 return net::ERR_FAILED;
146 145
147 if (fail_requests_) 146 if (fail_requests_)
148 return net::ERR_CACHE_READ_FAILURE; 147 return net::ERR_CACHE_READ_FAILURE;
149 148
150 DCHECK(offset < kint32max); 149 DCHECK(offset < kint32max);
151 int real_offset = static_cast<int>(offset); 150 int real_offset = static_cast<int>(offset);
(...skipping 10 matching lines...) Expand all
162 CallbackLater(callback, num); 161 CallbackLater(callback, num);
163 busy_ = true; 162 busy_ = true;
164 delayed_ = false; 163 delayed_ = false;
165 return net::ERR_IO_PENDING; 164 return net::ERR_IO_PENDING;
166 } 165 }
167 166
168 int MockDiskEntry::WriteSparseData(int64 offset, net::IOBuffer* buf, 167 int MockDiskEntry::WriteSparseData(int64 offset, net::IOBuffer* buf,
169 int buf_len, 168 int buf_len,
170 const net::CompletionCallback& callback) { 169 const net::CompletionCallback& callback) {
171 DCHECK(!callback.is_null()); 170 DCHECK(!callback.is_null());
171 backend_->NotifySparseIO();
172 if (fail_sparse_requests_)
173 return net::ERR_NOT_IMPLEMENTED;
172 if (busy_) 174 if (busy_)
173 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; 175 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
174 if (!sparse_) { 176 if (!sparse_) {
175 if (data_[1].size()) 177 if (data_[1].size())
176 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; 178 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
177 sparse_ = true; 179 sparse_ = true;
178 } 180 }
179 if (offset < 0) 181 if (offset < 0)
180 return net::ERR_FAILED; 182 return net::ERR_FAILED;
181 if (!buf_len) 183 if (!buf_len)
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 } 232 }
231 } 233 }
232 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE) 234 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE)
233 return count; 235 return count;
234 236
235 CallbackLater(callback, count); 237 CallbackLater(callback, count);
236 return net::ERR_IO_PENDING; 238 return net::ERR_IO_PENDING;
237 } 239 }
238 240
239 bool MockDiskEntry::CouldBeSparse() const { 241 bool MockDiskEntry::CouldBeSparse() const {
242 backend_->NotifySparseIO();
243 if (fail_sparse_requests_)
244 return false;
240 return sparse_; 245 return sparse_;
241 } 246 }
242 247
243 void MockDiskEntry::CancelSparseIO() { 248 void MockDiskEntry::CancelSparseIO() {
249 backend_->NotifySparseIO();
244 cancel_ = true; 250 cancel_ = true;
245 } 251 }
246 252
247 int MockDiskEntry::ReadyForSparseIO(const net::CompletionCallback& callback) { 253 int MockDiskEntry::ReadyForSparseIO(const net::CompletionCallback& callback) {
254 backend_->NotifySparseIO();
255 if (fail_sparse_requests_)
256 return net::ERR_NOT_IMPLEMENTED;
248 if (!cancel_) 257 if (!cancel_)
249 return net::OK; 258 return net::OK;
250 259
251 cancel_ = false; 260 cancel_ = false;
252 DCHECK(!callback.is_null()); 261 DCHECK(!callback.is_null());
253 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ) 262 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ)
254 return net::OK; 263 return net::OK;
255 264
256 // The pending operation is already in the message loop (and hopefully 265 // The pending operation is already in the message loop (and hopefully
257 // already in the second pass). Just notify the caller that it finished. 266 // already in the second pass). Just notify the caller that it finished.
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 } 334 }
326 } 335 }
327 336
328 // Statics. 337 // Statics.
329 bool MockDiskEntry::cancel_ = false; 338 bool MockDiskEntry::cancel_ = false;
330 bool MockDiskEntry::ignore_callbacks_ = false; 339 bool MockDiskEntry::ignore_callbacks_ = false;
331 340
332 //----------------------------------------------------------------------------- 341 //-----------------------------------------------------------------------------
333 342
334 MockDiskCache::MockDiskCache() 343 MockDiskCache::MockDiskCache()
335 : open_count_(0), create_count_(0), fail_requests_(false), 344 : open_count_(0), create_count_(0), sparse_count_(0), fail_requests_(false),
336 soft_failures_(false), double_create_check_(true) { 345 soft_failures_(false), double_create_check_(true),
346 fail_sparse_requests_(false) {
337 } 347 }
338 348
339 MockDiskCache::~MockDiskCache() { 349 MockDiskCache::~MockDiskCache() {
340 ReleaseAll(); 350 ReleaseAll();
341 } 351 }
342 352
343 net::CacheType MockDiskCache::GetCacheType() const { 353 net::CacheType MockDiskCache::GetCacheType() const {
344 return net::DISK_CACHE; 354 return net::DISK_CACHE;
345 } 355 }
346 356
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 NOTREACHED(); 403 NOTREACHED();
394 else 404 else
395 return net::ERR_CACHE_CREATE_FAILURE; 405 return net::ERR_CACHE_CREATE_FAILURE;
396 } 406 }
397 it->second->Release(); 407 it->second->Release();
398 entries_.erase(it); 408 entries_.erase(it);
399 } 409 }
400 410
401 create_count_++; 411 create_count_++;
402 412
403 MockDiskEntry* new_entry = new MockDiskEntry(key); 413 MockDiskEntry* new_entry = new MockDiskEntry(this, key);
404 414
405 new_entry->AddRef(); 415 new_entry->AddRef();
406 entries_[key] = new_entry; 416 entries_[key] = new_entry;
407 417
408 new_entry->AddRef(); 418 new_entry->AddRef();
409 *entry = new_entry; 419 *entry = new_entry;
410 420
411 if (soft_failures_) 421 if (soft_failures_)
412 new_entry->set_fail_requests(); 422 new_entry->set_fail_requests();
413 423
424 if (fail_sparse_requests_)
425 new_entry->set_fail_sparse_requests();
426
414 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START) 427 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START)
415 return net::OK; 428 return net::OK;
416 429
417 CallbackLater(callback, net::OK); 430 CallbackLater(callback, net::OK);
418 return net::ERR_IO_PENDING; 431 return net::ERR_IO_PENDING;
419 } 432 }
420 433
421 int MockDiskCache::DoomEntry(const std::string& key, 434 int MockDiskCache::DoomEntry(const std::string& key,
422 const net::CompletionCallback& callback) { 435 const net::CompletionCallback& callback) {
423 DCHECK(!callback.is_null()); 436 DCHECK(!callback.is_null());
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
610 void MockBlockingBackendFactory::FinishCreation() { 623 void MockBlockingBackendFactory::FinishCreation() {
611 block_ = false; 624 block_ = false;
612 if (!callback_.is_null()) { 625 if (!callback_.is_null()) {
613 if (!fail_) 626 if (!fail_)
614 backend_->reset(new MockDiskCache()); 627 backend_->reset(new MockDiskCache());
615 net::CompletionCallback cb = callback_; 628 net::CompletionCallback cb = callback_;
616 callback_.Reset(); 629 callback_.Reset();
617 cb.Run(Result()); // This object can be deleted here. 630 cb.Run(Result()); // This object can be deleted here.
618 } 631 }
619 } 632 }
OLDNEW
« net/http/http_cache_unittest.cc ('K') | « net/http/mock_http_cache.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698