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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 //----------------------------------------------------------------------------- | 61 //----------------------------------------------------------------------------- |
62 | 62 |
63 struct MockDiskEntry::CallbackInfo { | 63 struct MockDiskEntry::CallbackInfo { |
64 scoped_refptr<MockDiskEntry> entry; | 64 scoped_refptr<MockDiskEntry> entry; |
65 CompletionCallback callback; | 65 CompletionCallback callback; |
66 int result; | 66 int result; |
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 oracle_byte_(0), |
71 doomed_(false), | 72 doomed_(false), |
72 sparse_(false), | 73 sparse_(false), |
73 fail_requests_(false), | 74 fail_requests_(false), |
74 fail_sparse_requests_(false), | 75 fail_sparse_requests_(false), |
75 busy_(false), | 76 busy_(false), |
76 delayed_(false), | 77 delayed_(false), |
77 cancel_(false) { | 78 cancel_(false) { |
78 test_mode_ = GetTestModeForEntry(key); | 79 test_mode_ = GetTestModeForEntry(key); |
79 } | 80 } |
80 | 81 |
(...skipping 10 matching lines...) Expand all Loading... |
91 } | 92 } |
92 | 93 |
93 base::Time MockDiskEntry::GetLastUsed() const { | 94 base::Time MockDiskEntry::GetLastUsed() const { |
94 return base::Time::Now(); | 95 return base::Time::Now(); |
95 } | 96 } |
96 | 97 |
97 base::Time MockDiskEntry::GetLastModified() const { | 98 base::Time MockDiskEntry::GetLastModified() const { |
98 return base::Time::Now(); | 99 return base::Time::Now(); |
99 } | 100 } |
100 | 101 |
| 102 void MockDiskEntry::SetOracleByte(uint8_t val) { |
| 103 oracle_byte_ = val; |
| 104 } |
| 105 |
101 int32_t MockDiskEntry::GetDataSize(int index) const { | 106 int32_t MockDiskEntry::GetDataSize(int index) const { |
102 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices); | 107 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices); |
103 return static_cast<int32_t>(data_[index].size()); | 108 return static_cast<int32_t>(data_[index].size()); |
104 } | 109 } |
105 | 110 |
106 int MockDiskEntry::ReadData(int index, | 111 int MockDiskEntry::ReadData(int index, |
107 int offset, | 112 int offset, |
108 IOBuffer* buf, | 113 IOBuffer* buf, |
109 int buf_len, | 114 int buf_len, |
110 const CompletionCallback& callback) { | 115 const CompletionCallback& callback) { |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 return DISK_CACHE; | 388 return DISK_CACHE; |
384 } | 389 } |
385 | 390 |
386 int32_t MockDiskCache::GetEntryCount() const { | 391 int32_t MockDiskCache::GetEntryCount() const { |
387 return static_cast<int32_t>(entries_.size()); | 392 return static_cast<int32_t>(entries_.size()); |
388 } | 393 } |
389 | 394 |
390 int MockDiskCache::OpenEntry(const std::string& key, | 395 int MockDiskCache::OpenEntry(const std::string& key, |
391 disk_cache::Entry** entry, | 396 disk_cache::Entry** entry, |
392 const CompletionCallback& callback) { | 397 const CompletionCallback& callback) { |
| 398 return OpenEntryWithOracleByte(key, entry, OracleCallback(), callback); |
| 399 } |
| 400 |
| 401 int MockDiskCache::OpenEntryWithOracleByte(const std::string& key, |
| 402 disk_cache::Entry** entry, |
| 403 const OracleCallback& oracle, |
| 404 const CompletionCallback& callback) { |
393 DCHECK(!callback.is_null()); | 405 DCHECK(!callback.is_null()); |
394 if (fail_requests_) | 406 if (fail_requests_) |
395 return ERR_CACHE_OPEN_FAILURE; | 407 return ERR_CACHE_OPEN_FAILURE; |
396 | 408 |
397 EntryMap::iterator it = entries_.find(key); | 409 EntryMap::iterator it = entries_.find(key); |
398 if (it == entries_.end()) | 410 if (it == entries_.end()) |
399 return ERR_CACHE_OPEN_FAILURE; | 411 return ERR_CACHE_OPEN_FAILURE; |
400 | 412 |
| 413 if (!oracle.is_null() && oracle.Run(it->second->GetOracleByte()) == |
| 414 Backend::OracleJudgement::MISS_AND_DOOM) { |
| 415 it->second->Doom(); |
| 416 // Will be handled immediately below. |
| 417 } |
| 418 |
401 if (it->second->is_doomed()) { | 419 if (it->second->is_doomed()) { |
402 it->second->Release(); | 420 it->second->Release(); |
403 entries_.erase(it); | 421 entries_.erase(it); |
404 return ERR_CACHE_OPEN_FAILURE; | 422 return ERR_CACHE_OPEN_FAILURE; |
405 } | 423 } |
406 | 424 |
407 open_count_++; | 425 open_count_++; |
408 | 426 |
409 it->second->AddRef(); | 427 it->second->AddRef(); |
410 *entry = it->second; | 428 *entry = it->second; |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
696 if (!callback_.is_null()) { | 714 if (!callback_.is_null()) { |
697 if (!fail_) | 715 if (!fail_) |
698 backend_->reset(new MockDiskCache()); | 716 backend_->reset(new MockDiskCache()); |
699 CompletionCallback cb = callback_; | 717 CompletionCallback cb = callback_; |
700 callback_.Reset(); | 718 callback_.Reset(); |
701 cb.Run(Result()); // This object can be deleted here. | 719 cb.Run(Result()); // This object can be deleted here. |
702 } | 720 } |
703 } | 721 } |
704 | 722 |
705 } // namespace net | 723 } // namespace net |
OLD | NEW |