Chromium Code Reviews| 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 resume_return_code_(0) { | |
| 78 test_mode_ = GetTestModeForEntry(key); | 79 test_mode_ = GetTestModeForEntry(key); |
| 79 } | 80 } |
| 80 | 81 |
| 81 void MockDiskEntry::Doom() { | 82 void MockDiskEntry::Doom() { |
| 82 doomed_ = true; | 83 doomed_ = true; |
| 83 } | 84 } |
| 84 | 85 |
| 85 void MockDiskEntry::Close() { | 86 void MockDiskEntry::Close() { |
| 86 Release(); | 87 Release(); |
| 87 } | 88 } |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 118 return ERR_FAILED; | 119 return ERR_FAILED; |
| 119 if (static_cast<size_t>(offset) == data_[index].size()) | 120 if (static_cast<size_t>(offset) == data_[index].size()) |
| 120 return 0; | 121 return 0; |
| 121 | 122 |
| 122 int num = std::min(buf_len, static_cast<int>(data_[index].size()) - offset); | 123 int num = std::min(buf_len, static_cast<int>(data_[index].size()) - offset); |
| 123 memcpy(buf->data(), &data_[index][offset], num); | 124 memcpy(buf->data(), &data_[index][offset], num); |
| 124 | 125 |
| 125 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ) | 126 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ) |
| 126 return num; | 127 return num; |
| 127 | 128 |
| 129 // Pause and resume. | |
| 130 if (!before_cache_callback_.is_null()) { | |
| 131 bool defer = false; | |
| 132 before_cache_callback_.Run(&defer); | |
| 133 if (defer) { | |
| 134 resume_callback_ = callback; | |
| 135 resume_return_code_ = num; | |
| 136 return ERR_IO_PENDING; | |
| 137 } | |
| 138 } | |
|
jkarlin
2017/07/07 19:12:39
This seems overly complex for our use case. Why no
jkarlin
2017/07/07 19:14:12
Note that this comment came before I added the sug
shivanisha
2017/07/10 18:51:27
Simplified by using an enum DeferOp.
| |
| 139 | |
| 128 CallbackLater(callback, num); | 140 CallbackLater(callback, num); |
| 129 return ERR_IO_PENDING; | 141 return ERR_IO_PENDING; |
| 130 } | 142 } |
| 131 | 143 |
| 144 int MockDiskEntry::ResumeCacheOperation() { | |
| 145 DCHECK(!resume_callback_.is_null()); | |
| 146 CallbackLater(resume_callback_, resume_return_code_); | |
| 147 return ERR_IO_PENDING; | |
| 148 } | |
| 149 | |
| 150 void MockDiskEntry::SetBeforeCacheOperationCallback( | |
| 151 const BeforeCacheOperationCallback& callback) { | |
| 152 before_cache_callback_ = callback; | |
| 153 } | |
| 154 | |
| 132 int MockDiskEntry::WriteData(int index, | 155 int MockDiskEntry::WriteData(int index, |
| 133 int offset, | 156 int offset, |
| 134 IOBuffer* buf, | 157 IOBuffer* buf, |
| 135 int buf_len, | 158 int buf_len, |
| 136 const CompletionCallback& callback, | 159 const CompletionCallback& callback, |
| 137 bool truncate) { | 160 bool truncate) { |
| 138 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices); | 161 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices); |
| 139 DCHECK(!callback.is_null()); | 162 DCHECK(!callback.is_null()); |
| 140 DCHECK(truncate); | 163 DCHECK(truncate); |
| 141 | 164 |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 392 const CompletionCallback& callback) { | 415 const CompletionCallback& callback) { |
| 393 DCHECK(!callback.is_null()); | 416 DCHECK(!callback.is_null()); |
| 394 if (fail_requests_) | 417 if (fail_requests_) |
| 395 return ERR_CACHE_OPEN_FAILURE; | 418 return ERR_CACHE_OPEN_FAILURE; |
| 396 | 419 |
| 397 EntryMap::iterator it = entries_.find(key); | 420 EntryMap::iterator it = entries_.find(key); |
| 398 if (it == entries_.end()) | 421 if (it == entries_.end()) |
| 399 return ERR_CACHE_OPEN_FAILURE; | 422 return ERR_CACHE_OPEN_FAILURE; |
| 400 | 423 |
| 401 if (it->second->is_doomed()) { | 424 if (it->second->is_doomed()) { |
| 402 it->second->Release(); | 425 doomed_entries_[key] = it->second; |
| 403 entries_.erase(it); | 426 entries_.erase(it); |
| 404 return ERR_CACHE_OPEN_FAILURE; | 427 return ERR_CACHE_OPEN_FAILURE; |
| 405 } | 428 } |
| 406 | 429 |
| 407 open_count_++; | 430 open_count_++; |
| 408 | 431 |
| 409 it->second->AddRef(); | 432 it->second->AddRef(); |
| 410 *entry = it->second; | 433 *entry = it->second; |
| 411 | 434 |
| 412 if (soft_failures_) | 435 if (soft_failures_) |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 427 return ERR_CACHE_CREATE_FAILURE; | 450 return ERR_CACHE_CREATE_FAILURE; |
| 428 | 451 |
| 429 EntryMap::iterator it = entries_.find(key); | 452 EntryMap::iterator it = entries_.find(key); |
| 430 if (it != entries_.end()) { | 453 if (it != entries_.end()) { |
| 431 if (!it->second->is_doomed()) { | 454 if (!it->second->is_doomed()) { |
| 432 if (double_create_check_) | 455 if (double_create_check_) |
| 433 NOTREACHED(); | 456 NOTREACHED(); |
| 434 else | 457 else |
| 435 return ERR_CACHE_CREATE_FAILURE; | 458 return ERR_CACHE_CREATE_FAILURE; |
| 436 } | 459 } |
| 437 it->second->Release(); | 460 doomed_entries_[key] = it->second; |
| 438 entries_.erase(it); | 461 entries_.erase(it); |
| 439 } | 462 } |
| 440 | 463 |
| 441 create_count_++; | 464 create_count_++; |
| 442 | 465 |
| 443 MockDiskEntry* new_entry = new MockDiskEntry(key); | 466 MockDiskEntry* new_entry = new MockDiskEntry(key); |
| 444 | 467 |
| 445 new_entry->AddRef(); | 468 new_entry->AddRef(); |
| 446 entries_[key] = new_entry; | 469 entries_[key] = new_entry; |
| 447 | 470 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 459 | 482 |
| 460 CallbackLater(callback, OK); | 483 CallbackLater(callback, OK); |
| 461 return ERR_IO_PENDING; | 484 return ERR_IO_PENDING; |
| 462 } | 485 } |
| 463 | 486 |
| 464 int MockDiskCache::DoomEntry(const std::string& key, | 487 int MockDiskCache::DoomEntry(const std::string& key, |
| 465 const CompletionCallback& callback) { | 488 const CompletionCallback& callback) { |
| 466 DCHECK(!callback.is_null()); | 489 DCHECK(!callback.is_null()); |
| 467 EntryMap::iterator it = entries_.find(key); | 490 EntryMap::iterator it = entries_.find(key); |
| 468 if (it != entries_.end()) { | 491 if (it != entries_.end()) { |
| 469 it->second->Release(); | 492 doomed_entries_[key] = it->second; |
| 470 entries_.erase(it); | 493 entries_.erase(it); |
| 471 } | 494 } |
| 472 | 495 |
| 473 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START) | 496 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START) |
| 474 return OK; | 497 return OK; |
| 475 | 498 |
| 476 CallbackLater(callback, OK); | 499 CallbackLater(callback, OK); |
| 477 return ERR_IO_PENDING; | 500 return ERR_IO_PENDING; |
| 478 } | 501 } |
| 479 | 502 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 515 void MockDiskCache::OnExternalCacheHit(const std::string& key) { | 538 void MockDiskCache::OnExternalCacheHit(const std::string& key) { |
| 516 } | 539 } |
| 517 | 540 |
| 518 size_t MockDiskCache::DumpMemoryStats( | 541 size_t MockDiskCache::DumpMemoryStats( |
| 519 base::trace_event::ProcessMemoryDump* pmd, | 542 base::trace_event::ProcessMemoryDump* pmd, |
| 520 const std::string& parent_absolute_name) const { | 543 const std::string& parent_absolute_name) const { |
| 521 return 0u; | 544 return 0u; |
| 522 } | 545 } |
| 523 | 546 |
| 524 void MockDiskCache::ReleaseAll() { | 547 void MockDiskCache::ReleaseAll() { |
| 525 EntryMap::iterator it = entries_.begin(); | 548 for (auto entry : entries_) |
| 526 for (; it != entries_.end(); ++it) | 549 entry.second->Release(); |
| 527 it->second->Release(); | |
| 528 entries_.clear(); | 550 entries_.clear(); |
| 551 | |
| 552 for (auto doomed : doomed_entries_) | |
| 553 doomed.second->Release(); | |
| 554 doomed_entries_.clear(); | |
| 529 } | 555 } |
| 530 | 556 |
| 531 void MockDiskCache::CallbackLater(const CompletionCallback& callback, | 557 void MockDiskCache::CallbackLater(const CompletionCallback& callback, |
| 532 int result) { | 558 int result) { |
| 533 base::ThreadTaskRunnerHandle::Get()->PostTask( | 559 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 534 FROM_HERE, base::Bind(&CallbackForwader, callback, result)); | 560 FROM_HERE, base::Bind(&CallbackForwader, callback, result)); |
| 535 } | 561 } |
| 536 | 562 |
| 537 bool MockDiskCache::IsDiskEntryDoomed(const std::string& key) { | 563 bool MockDiskCache::IsDiskEntryDoomed(const std::string& key) { |
| 564 auto doomed_it = doomed_entries_.find(key); | |
| 565 if (doomed_it != doomed_entries_.end()) | |
| 566 return true; | |
| 567 | |
| 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::SetBeforeCacheOperationCallback( | |
| 576 const std::string& key, | |
| 577 const MockDiskEntry::BeforeCacheOperationCallback& callback) { | |
| 538 auto it = entries_.find(key); | 578 auto it = entries_.find(key); |
| 539 if (it == entries_.end()) | 579 if (it == entries_.end()) |
| 540 return false; | 580 return; |
| 541 return it->second->is_doomed(); | 581 it->second->SetBeforeCacheOperationCallback(callback); |
| 582 } | |
| 583 | |
| 584 int MockDiskCache::ResumeCacheOperation(const std::string& key) { | |
| 585 auto it = entries_.find(key); | |
| 586 if (it != entries_.end()) | |
| 587 return it->second->ResumeCacheOperation(); | |
| 588 | |
| 589 return ERR_UNEXPECTED; | |
| 590 } | |
| 591 | |
| 592 int MockDiskCache::ResumeDoomedEntryCacheOperation(const std::string& key) { | |
| 593 auto doomed_it = doomed_entries_.find(key); | |
| 594 if (doomed_it != doomed_entries_.end()) | |
| 595 return doomed_it->second->ResumeCacheOperation(); | |
| 596 | |
| 597 auto it = entries_.find(key); | |
| 598 if (it != entries_.end()) { | |
| 599 DCHECK(it->second->is_doomed()); | |
| 600 return it->second->ResumeCacheOperation(); | |
| 601 } | |
| 602 | |
| 603 return ERR_UNEXPECTED; | |
| 542 } | 604 } |
| 543 | 605 |
| 544 //----------------------------------------------------------------------------- | 606 //----------------------------------------------------------------------------- |
| 545 | 607 |
| 546 int MockBackendFactory::CreateBackend( | 608 int MockBackendFactory::CreateBackend( |
| 547 NetLog* net_log, | 609 NetLog* net_log, |
| 548 std::unique_ptr<disk_cache::Backend>* backend, | 610 std::unique_ptr<disk_cache::Backend>* backend, |
| 549 const CompletionCallback& callback) { | 611 const CompletionCallback& callback) { |
| 550 backend->reset(new MockDiskCache()); | 612 backend->reset(new MockDiskCache()); |
| 551 return OK; | 613 return OK; |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 742 if (!callback_.is_null()) { | 804 if (!callback_.is_null()) { |
| 743 if (!fail_) | 805 if (!fail_) |
| 744 backend_->reset(new MockDiskCache()); | 806 backend_->reset(new MockDiskCache()); |
| 745 CompletionCallback cb = callback_; | 807 CompletionCallback cb = callback_; |
| 746 callback_.Reset(); | 808 callback_.Reset(); |
| 747 cb.Run(Result()); // This object can be deleted here. | 809 cb.Run(Result()); // This object can be deleted here. |
| 748 } | 810 } |
| 749 } | 811 } |
| 750 | 812 |
| 751 } // namespace net | 813 } // namespace net |
| OLD | NEW |