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> | 7 #include <limits> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 }; | 67 }; |
68 | 68 |
69 MockDiskEntry::MockDiskEntry(const std::string& key) | 69 MockDiskEntry::MockDiskEntry(const std::string& key) |
70 : key_(key), | 70 : key_(key), |
71 doomed_(false), | 71 doomed_(false), |
72 sparse_(false), | 72 sparse_(false), |
73 fail_requests_(false), | 73 fail_requests_(false), |
74 fail_sparse_requests_(false), | 74 fail_sparse_requests_(false), |
75 busy_(false), | 75 busy_(false), |
76 delayed_(false), | 76 delayed_(false), |
77 cancel_(false) { | 77 cancel_(false), |
| 78 defer_op_(DEFER_NONE), |
| 79 resume_return_code_(0) { |
78 test_mode_ = GetTestModeForEntry(key); | 80 test_mode_ = GetTestModeForEntry(key); |
79 } | 81 } |
80 | 82 |
81 void MockDiskEntry::Doom() { | 83 void MockDiskEntry::Doom() { |
82 doomed_ = true; | 84 doomed_ = true; |
83 } | 85 } |
84 | 86 |
85 void MockDiskEntry::Close() { | 87 void MockDiskEntry::Close() { |
86 Release(); | 88 Release(); |
87 } | 89 } |
(...skipping 30 matching lines...) Expand all Loading... |
118 return ERR_FAILED; | 120 return ERR_FAILED; |
119 if (static_cast<size_t>(offset) == data_[index].size()) | 121 if (static_cast<size_t>(offset) == data_[index].size()) |
120 return 0; | 122 return 0; |
121 | 123 |
122 int num = std::min(buf_len, static_cast<int>(data_[index].size()) - offset); | 124 int num = std::min(buf_len, static_cast<int>(data_[index].size()) - offset); |
123 memcpy(buf->data(), &data_[index][offset], num); | 125 memcpy(buf->data(), &data_[index][offset], num); |
124 | 126 |
125 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ) | 127 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ) |
126 return num; | 128 return num; |
127 | 129 |
| 130 // Pause and resume. |
| 131 if (defer_op_ == DEFER_READ) { |
| 132 defer_op_ = DEFER_NONE; |
| 133 resume_callback_ = callback; |
| 134 resume_return_code_ = num; |
| 135 return ERR_IO_PENDING; |
| 136 } |
| 137 |
128 CallbackLater(callback, num); | 138 CallbackLater(callback, num); |
129 return ERR_IO_PENDING; | 139 return ERR_IO_PENDING; |
130 } | 140 } |
131 | 141 |
| 142 void MockDiskEntry::ResumeDiskEntryOperation() { |
| 143 DCHECK(!resume_callback_.is_null()); |
| 144 CallbackLater(resume_callback_, resume_return_code_); |
| 145 resume_callback_.Reset(); |
| 146 resume_return_code_ = 0; |
| 147 } |
| 148 |
132 int MockDiskEntry::WriteData(int index, | 149 int MockDiskEntry::WriteData(int index, |
133 int offset, | 150 int offset, |
134 IOBuffer* buf, | 151 IOBuffer* buf, |
135 int buf_len, | 152 int buf_len, |
136 const CompletionCallback& callback, | 153 const CompletionCallback& callback, |
137 bool truncate) { | 154 bool truncate) { |
138 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices); | 155 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices); |
139 DCHECK(!callback.is_null()); | 156 DCHECK(!callback.is_null()); |
140 DCHECK(truncate); | 157 DCHECK(truncate); |
141 | 158 |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 | 386 |
370 //----------------------------------------------------------------------------- | 387 //----------------------------------------------------------------------------- |
371 | 388 |
372 MockDiskCache::MockDiskCache() | 389 MockDiskCache::MockDiskCache() |
373 : open_count_(0), | 390 : open_count_(0), |
374 create_count_(0), | 391 create_count_(0), |
375 doomed_count_(0), | 392 doomed_count_(0), |
376 fail_requests_(false), | 393 fail_requests_(false), |
377 soft_failures_(false), | 394 soft_failures_(false), |
378 double_create_check_(true), | 395 double_create_check_(true), |
379 fail_sparse_requests_(false) {} | 396 fail_sparse_requests_(false), |
| 397 defer_op_(MockDiskEntry::DEFER_NONE), |
| 398 resume_return_code_(0) {} |
380 | 399 |
381 MockDiskCache::~MockDiskCache() { | 400 MockDiskCache::~MockDiskCache() { |
382 ReleaseAll(); | 401 ReleaseAll(); |
383 } | 402 } |
384 | 403 |
385 CacheType MockDiskCache::GetCacheType() const { | 404 CacheType MockDiskCache::GetCacheType() const { |
386 return DISK_CACHE; | 405 return DISK_CACHE; |
387 } | 406 } |
388 | 407 |
389 int32_t MockDiskCache::GetEntryCount() const { | 408 int32_t MockDiskCache::GetEntryCount() const { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 | 472 |
454 if (soft_failures_) | 473 if (soft_failures_) |
455 new_entry->set_fail_requests(); | 474 new_entry->set_fail_requests(); |
456 | 475 |
457 if (fail_sparse_requests_) | 476 if (fail_sparse_requests_) |
458 new_entry->set_fail_sparse_requests(); | 477 new_entry->set_fail_sparse_requests(); |
459 | 478 |
460 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START) | 479 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START) |
461 return OK; | 480 return OK; |
462 | 481 |
| 482 // Pause and resume. |
| 483 if (defer_op_ == MockDiskEntry::DEFER_CREATE) { |
| 484 defer_op_ = MockDiskEntry::DEFER_NONE; |
| 485 resume_callback_ = callback; |
| 486 resume_return_code_ = OK; |
| 487 return ERR_IO_PENDING; |
| 488 } |
| 489 |
463 CallbackLater(callback, OK); | 490 CallbackLater(callback, OK); |
464 return ERR_IO_PENDING; | 491 return ERR_IO_PENDING; |
465 } | 492 } |
466 | 493 |
467 int MockDiskCache::DoomEntry(const std::string& key, | 494 int MockDiskCache::DoomEntry(const std::string& key, |
468 const CompletionCallback& callback) { | 495 const CompletionCallback& callback) { |
469 DCHECK(!callback.is_null()); | 496 DCHECK(!callback.is_null()); |
470 EntryMap::iterator it = entries_.find(key); | 497 EntryMap::iterator it = entries_.find(key); |
471 if (it != entries_.end()) { | 498 if (it != entries_.end()) { |
472 it->second->Release(); | 499 it->second->Release(); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 void MockDiskCache::OnExternalCacheHit(const std::string& key) { | 546 void MockDiskCache::OnExternalCacheHit(const std::string& key) { |
520 } | 547 } |
521 | 548 |
522 size_t MockDiskCache::DumpMemoryStats( | 549 size_t MockDiskCache::DumpMemoryStats( |
523 base::trace_event::ProcessMemoryDump* pmd, | 550 base::trace_event::ProcessMemoryDump* pmd, |
524 const std::string& parent_absolute_name) const { | 551 const std::string& parent_absolute_name) const { |
525 return 0u; | 552 return 0u; |
526 } | 553 } |
527 | 554 |
528 void MockDiskCache::ReleaseAll() { | 555 void MockDiskCache::ReleaseAll() { |
529 EntryMap::iterator it = entries_.begin(); | 556 for (auto entry : entries_) |
530 for (; it != entries_.end(); ++it) | 557 entry.second->Release(); |
531 it->second->Release(); | |
532 entries_.clear(); | 558 entries_.clear(); |
533 } | 559 } |
534 | 560 |
535 void MockDiskCache::CallbackLater(const CompletionCallback& callback, | 561 void MockDiskCache::CallbackLater(const CompletionCallback& callback, |
536 int result) { | 562 int result) { |
537 base::ThreadTaskRunnerHandle::Get()->PostTask( | 563 base::ThreadTaskRunnerHandle::Get()->PostTask( |
538 FROM_HERE, base::Bind(&CallbackForwader, callback, result)); | 564 FROM_HERE, base::Bind(&CallbackForwader, callback, result)); |
539 } | 565 } |
540 | 566 |
541 bool MockDiskCache::IsDiskEntryDoomed(const std::string& key) { | 567 bool MockDiskCache::IsDiskEntryDoomed(const std::string& key) { |
542 auto it = entries_.find(key); | 568 auto it = entries_.find(key); |
| 569 if (it != entries_.end()) |
| 570 return it->second->is_doomed(); |
| 571 |
| 572 return false; |
| 573 } |
| 574 |
| 575 void MockDiskCache::ResumeCacheOperation() { |
| 576 DCHECK(!resume_callback_.is_null()); |
| 577 CallbackLater(resume_callback_, resume_return_code_); |
| 578 resume_callback_.Reset(); |
| 579 resume_return_code_ = 0; |
| 580 } |
| 581 |
| 582 scoped_refptr<MockDiskEntry> MockDiskCache::GetDiskEntryRef( |
| 583 const std::string& key) { |
| 584 auto it = entries_.find(key); |
543 if (it == entries_.end()) | 585 if (it == entries_.end()) |
544 return false; | 586 return nullptr; |
545 return it->second->is_doomed(); | 587 return it->second; |
546 } | 588 } |
547 | 589 |
548 //----------------------------------------------------------------------------- | 590 //----------------------------------------------------------------------------- |
549 | 591 |
550 int MockBackendFactory::CreateBackend( | 592 int MockBackendFactory::CreateBackend( |
551 NetLog* net_log, | 593 NetLog* net_log, |
552 std::unique_ptr<disk_cache::Backend>* backend, | 594 std::unique_ptr<disk_cache::Backend>* backend, |
553 const CompletionCallback& callback) { | 595 const CompletionCallback& callback) { |
554 backend->reset(new MockDiskCache()); | 596 backend->reset(new MockDiskCache()); |
555 return OK; | 597 return OK; |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
746 if (!callback_.is_null()) { | 788 if (!callback_.is_null()) { |
747 if (!fail_) | 789 if (!fail_) |
748 backend_->reset(new MockDiskCache()); | 790 backend_->reset(new MockDiskCache()); |
749 CompletionCallback cb = callback_; | 791 CompletionCallback cb = callback_; |
750 callback_.Reset(); | 792 callback_.Reset(); |
751 cb.Run(Result()); // This object can be deleted here. | 793 cb.Run(Result()); // This object can be deleted here. |
752 } | 794 } |
753 } | 795 } |
754 | 796 |
755 } // namespace net | 797 } // namespace net |
OLD | NEW |