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