[email protected] | 93fe7516 | 2012-02-09 21:51:31 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/http/mock_http_cache.h" |
| 6 | |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 7 | #include <limits> |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 8 | #include <utility> |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 9 | |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 10 | #include "base/bind.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 11 | #include "base/location.h" |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12 | #include "base/memory/ptr_util.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 13 | #include "base/single_thread_task_runner.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 14 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | 42c45963 | 2011-12-17 02:20:23 | [diff] [blame] | 15 | #include "net/base/completion_callback.h" |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 16 | #include "net/base/net_errors.h" |
| 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 19 | namespace net { |
| 20 | |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 21 | namespace { |
| 22 | |
asanka | b5fb4b4 | 2016-06-29 11:06:08 | [diff] [blame] | 23 | // During testing, we are going to limit the size of a cache entry to this many |
| 24 | // bytes using DCHECKs in order to prevent a test from causing unbounded memory |
| 25 | // growth. In practice cache entry shouldn't come anywhere near this limit for |
| 26 | // tests that use the mock cache. If they do, that's likely a problem with the |
| 27 | // test. If a test requires using massive cache entries, they should use a real |
| 28 | // cache backend instead. |
| 29 | const int kMaxMockCacheEntrySize = 100 * 1000 * 1000; |
| 30 | |
[email protected] | 42c45963 | 2011-12-17 02:20:23 | [diff] [blame] | 31 | // We can override the test mode for a given operation by setting this global |
| 32 | // variable. |
| 33 | int g_test_mode = 0; |
| 34 | |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 35 | int GetTestModeForEntry(const std::string& key) { |
| 36 | // 'key' is prefixed with an identifier if it corresponds to a cached POST. |
| 37 | // Skip past that to locate the actual URL. |
| 38 | // |
| 39 | // TODO(darin): It breaks the abstraction a bit that we assume 'key' is an |
| 40 | // URL corresponding to a registered MockTransaction. It would be good to |
| 41 | // have another way to access the test_mode. |
| 42 | GURL url; |
| 43 | if (isdigit(key[0])) { |
| 44 | size_t slash = key.find('/'); |
| 45 | DCHECK(slash != std::string::npos); |
| 46 | url = GURL(key.substr(slash + 1)); |
| 47 | } else { |
| 48 | url = GURL(key); |
| 49 | } |
| 50 | const MockTransaction* t = FindMockTransaction(url); |
| 51 | DCHECK(t); |
| 52 | return t->test_mode; |
| 53 | } |
| 54 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 55 | void CallbackForwader(const CompletionCallback& callback, int result) { |
[email protected] | 42c45963 | 2011-12-17 02:20:23 | [diff] [blame] | 56 | callback.Run(result); |
| 57 | } |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 58 | |
| 59 | } // namespace |
| 60 | |
| 61 | //----------------------------------------------------------------------------- |
| 62 | |
| 63 | struct MockDiskEntry::CallbackInfo { |
| 64 | scoped_refptr<MockDiskEntry> entry; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 65 | CompletionCallback callback; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 66 | int result; |
| 67 | }; |
| 68 | |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 69 | MockDiskEntry::MockDiskEntry(const std::string& key) |
rvargas | 9ba2445 | 2015-07-18 03:13:25 | [diff] [blame] | 70 | : key_(key), |
| 71 | doomed_(false), |
| 72 | sparse_(false), |
| 73 | fail_requests_(false), |
| 74 | fail_sparse_requests_(false), |
| 75 | busy_(false), |
| 76 | delayed_(false), |
| 77 | cancel_(false) { |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 78 | test_mode_ = GetTestModeForEntry(key); |
| 79 | } |
| 80 | |
| 81 | void MockDiskEntry::Doom() { |
| 82 | doomed_ = true; |
| 83 | } |
| 84 | |
| 85 | void MockDiskEntry::Close() { |
| 86 | Release(); |
| 87 | } |
| 88 | |
| 89 | std::string MockDiskEntry::GetKey() const { |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 90 | return key_; |
| 91 | } |
| 92 | |
| 93 | base::Time MockDiskEntry::GetLastUsed() const { |
jkarlin | 91ae360 | 2016-05-25 14:05:38 | [diff] [blame] | 94 | return base::Time::Now(); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | base::Time MockDiskEntry::GetLastModified() const { |
jkarlin | 91ae360 | 2016-05-25 14:05:38 | [diff] [blame] | 98 | return base::Time::Now(); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 99 | } |
| 100 | |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 101 | int32_t MockDiskEntry::GetDataSize(int index) const { |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 102 | DCHECK(index >= 0 && index < kNumCacheEntryDataIndices); |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 103 | return static_cast<int32_t>(data_[index].size()); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 104 | } |
| 105 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 106 | int MockDiskEntry::ReadData(int index, |
| 107 | int offset, |
| 108 | IOBuffer* buf, |
| 109 | int buf_len, |
| 110 | const CompletionCallback& callback) { |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 111 | DCHECK(index >= 0 && index < kNumCacheEntryDataIndices); |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 112 | DCHECK(!callback.is_null()); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 113 | |
| 114 | if (fail_requests_) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 115 | return ERR_CACHE_READ_FAILURE; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 116 | |
| 117 | if (offset < 0 || offset > static_cast<int>(data_[index].size())) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 118 | return ERR_FAILED; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 119 | if (static_cast<size_t>(offset) == data_[index].size()) |
| 120 | return 0; |
| 121 | |
| 122 | int num = std::min(buf_len, static_cast<int>(data_[index].size()) - offset); |
| 123 | memcpy(buf->data(), &data_[index][offset], num); |
| 124 | |
| 125 | if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ) |
| 126 | return num; |
| 127 | |
| 128 | CallbackLater(callback, num); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 129 | return ERR_IO_PENDING; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 130 | } |
| 131 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 132 | int MockDiskEntry::WriteData(int index, |
| 133 | int offset, |
| 134 | IOBuffer* buf, |
| 135 | int buf_len, |
| 136 | const CompletionCallback& callback, |
| 137 | bool truncate) { |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 138 | DCHECK(index >= 0 && index < kNumCacheEntryDataIndices); |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 139 | DCHECK(!callback.is_null()); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 140 | DCHECK(truncate); |
| 141 | |
| 142 | if (fail_requests_) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 143 | CallbackLater(callback, ERR_CACHE_READ_FAILURE); |
| 144 | return ERR_IO_PENDING; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | if (offset < 0 || offset > static_cast<int>(data_[index].size())) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 148 | return ERR_FAILED; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 149 | |
asanka | b5fb4b4 | 2016-06-29 11:06:08 | [diff] [blame] | 150 | DCHECK_LT(offset + buf_len, kMaxMockCacheEntrySize); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 151 | data_[index].resize(offset + buf_len); |
| 152 | if (buf_len) |
| 153 | memcpy(&data_[index][offset], buf->data(), buf_len); |
| 154 | |
| 155 | if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE) |
| 156 | return buf_len; |
| 157 | |
| 158 | CallbackLater(callback, buf_len); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 159 | return ERR_IO_PENDING; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 160 | } |
| 161 | |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 162 | int MockDiskEntry::ReadSparseData(int64_t offset, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 163 | IOBuffer* buf, |
| 164 | int buf_len, |
| 165 | const CompletionCallback& callback) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 166 | DCHECK(!callback.is_null()); |
[email protected] | 954bbe4 | 2013-08-30 12:38:39 | [diff] [blame] | 167 | if (fail_sparse_requests_) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 168 | return ERR_NOT_IMPLEMENTED; |
rvargas | 9ba2445 | 2015-07-18 03:13:25 | [diff] [blame] | 169 | if (!sparse_ || busy_ || cancel_) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 170 | return ERR_CACHE_OPERATION_NOT_SUPPORTED; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 171 | if (offset < 0) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 172 | return ERR_FAILED; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 173 | |
| 174 | if (fail_requests_) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 175 | return ERR_CACHE_READ_FAILURE; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 176 | |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 177 | DCHECK(offset < std::numeric_limits<int32_t>::max()); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 178 | int real_offset = static_cast<int>(offset); |
| 179 | if (!buf_len) |
| 180 | return 0; |
| 181 | |
| 182 | int num = std::min(static_cast<int>(data_[1].size()) - real_offset, |
| 183 | buf_len); |
| 184 | memcpy(buf->data(), &data_[1][real_offset], num); |
| 185 | |
| 186 | if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ) |
| 187 | return num; |
| 188 | |
| 189 | CallbackLater(callback, num); |
| 190 | busy_ = true; |
| 191 | delayed_ = false; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 192 | return ERR_IO_PENDING; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 193 | } |
| 194 | |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 195 | int MockDiskEntry::WriteSparseData(int64_t offset, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 196 | IOBuffer* buf, |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 197 | int buf_len, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 198 | const CompletionCallback& callback) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 199 | DCHECK(!callback.is_null()); |
[email protected] | 954bbe4 | 2013-08-30 12:38:39 | [diff] [blame] | 200 | if (fail_sparse_requests_) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 201 | return ERR_NOT_IMPLEMENTED; |
rvargas | 9ba2445 | 2015-07-18 03:13:25 | [diff] [blame] | 202 | if (busy_ || cancel_) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 203 | return ERR_CACHE_OPERATION_NOT_SUPPORTED; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 204 | if (!sparse_) { |
| 205 | if (data_[1].size()) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 206 | return ERR_CACHE_OPERATION_NOT_SUPPORTED; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 207 | sparse_ = true; |
| 208 | } |
| 209 | if (offset < 0) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 210 | return ERR_FAILED; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 211 | if (!buf_len) |
| 212 | return 0; |
| 213 | |
| 214 | if (fail_requests_) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 215 | return ERR_CACHE_READ_FAILURE; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 216 | |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 217 | DCHECK(offset < std::numeric_limits<int32_t>::max()); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 218 | int real_offset = static_cast<int>(offset); |
| 219 | |
asanka | b5fb4b4 | 2016-06-29 11:06:08 | [diff] [blame] | 220 | if (static_cast<int>(data_[1].size()) < real_offset + buf_len) { |
| 221 | DCHECK_LT(real_offset + buf_len, kMaxMockCacheEntrySize); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 222 | data_[1].resize(real_offset + buf_len); |
asanka | b5fb4b4 | 2016-06-29 11:06:08 | [diff] [blame] | 223 | } |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 224 | |
| 225 | memcpy(&data_[1][real_offset], buf->data(), buf_len); |
| 226 | if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE) |
| 227 | return buf_len; |
| 228 | |
| 229 | CallbackLater(callback, buf_len); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 230 | return ERR_IO_PENDING; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 231 | } |
| 232 | |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 233 | int MockDiskEntry::GetAvailableRange(int64_t offset, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 234 | int len, |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 235 | int64_t* start, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 236 | const CompletionCallback& callback) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 237 | DCHECK(!callback.is_null()); |
rvargas | 9ba2445 | 2015-07-18 03:13:25 | [diff] [blame] | 238 | if (!sparse_ || busy_ || cancel_) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 239 | return ERR_CACHE_OPERATION_NOT_SUPPORTED; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 240 | if (offset < 0) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 241 | return ERR_FAILED; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 242 | |
| 243 | if (fail_requests_) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 244 | return ERR_CACHE_READ_FAILURE; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 245 | |
| 246 | *start = offset; |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 247 | DCHECK(offset < std::numeric_limits<int32_t>::max()); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 248 | int real_offset = static_cast<int>(offset); |
| 249 | if (static_cast<int>(data_[1].size()) < real_offset) |
| 250 | return 0; |
| 251 | |
| 252 | int num = std::min(static_cast<int>(data_[1].size()) - real_offset, len); |
| 253 | int count = 0; |
| 254 | for (; num > 0; num--, real_offset++) { |
| 255 | if (!count) { |
| 256 | if (data_[1][real_offset]) { |
| 257 | count++; |
| 258 | *start = real_offset; |
| 259 | } |
| 260 | } else { |
| 261 | if (!data_[1][real_offset]) |
| 262 | break; |
| 263 | count++; |
| 264 | } |
| 265 | } |
| 266 | if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE) |
| 267 | return count; |
| 268 | |
| 269 | CallbackLater(callback, count); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 270 | return ERR_IO_PENDING; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | bool MockDiskEntry::CouldBeSparse() const { |
[email protected] | 954bbe4 | 2013-08-30 12:38:39 | [diff] [blame] | 274 | if (fail_sparse_requests_) |
| 275 | return false; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 276 | return sparse_; |
| 277 | } |
| 278 | |
| 279 | void MockDiskEntry::CancelSparseIO() { |
| 280 | cancel_ = true; |
| 281 | } |
| 282 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 283 | int MockDiskEntry::ReadyForSparseIO(const CompletionCallback& callback) { |
[email protected] | 954bbe4 | 2013-08-30 12:38:39 | [diff] [blame] | 284 | if (fail_sparse_requests_) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 285 | return ERR_NOT_IMPLEMENTED; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 286 | if (!cancel_) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 287 | return OK; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 288 | |
| 289 | cancel_ = false; |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 290 | DCHECK(!callback.is_null()); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 291 | if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 292 | return OK; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 293 | |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 294 | // The pending operation is already in the message loop (and hopefully |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 295 | // already in the second pass). Just notify the caller that it finished. |
| 296 | CallbackLater(callback, 0); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 297 | return ERR_IO_PENDING; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | // If |value| is true, don't deliver any completion callbacks until called |
| 301 | // again with |value| set to false. Caution: remember to enable callbacks |
| 302 | // again or all subsequent tests will fail. |
| 303 | // Static. |
| 304 | void MockDiskEntry::IgnoreCallbacks(bool value) { |
| 305 | if (ignore_callbacks_ == value) |
| 306 | return; |
| 307 | ignore_callbacks_ = value; |
| 308 | if (!value) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 309 | StoreAndDeliverCallbacks(false, NULL, CompletionCallback(), 0); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | MockDiskEntry::~MockDiskEntry() { |
| 313 | } |
| 314 | |
| 315 | // Unlike the callbacks for MockHttpTransaction, we want this one to run even |
| 316 | // if the consumer called Close on the MockDiskEntry. We achieve that by |
| 317 | // leveraging the fact that this class is reference counted. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 318 | void MockDiskEntry::CallbackLater(const CompletionCallback& callback, |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 319 | int result) { |
[email protected] | 2c5c9d5 | 2011-12-07 14:15:36 | [diff] [blame] | 320 | if (ignore_callbacks_) |
| 321 | return StoreAndDeliverCallbacks(true, this, callback, result); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 322 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 323 | FROM_HERE, |
| 324 | base::Bind(&MockDiskEntry::RunCallback, this, callback, result)); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 325 | } |
| 326 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 327 | void MockDiskEntry::RunCallback(const CompletionCallback& callback, |
| 328 | int result) { |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 329 | if (busy_) { |
| 330 | // This is kind of hacky, but controlling the behavior of just this entry |
| 331 | // from a test is sort of complicated. What we really want to do is |
| 332 | // delay the delivery of a sparse IO operation a little more so that the |
| 333 | // request start operation (async) will finish without seeing the end of |
| 334 | // this operation (already posted to the message loop)... and without |
| 335 | // just delaying for n mS (which may cause trouble with slow bots). So |
| 336 | // we re-post this operation (all async sparse IO operations will take two |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 337 | // trips through the message loop instead of one). |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 338 | if (!delayed_) { |
| 339 | delayed_ = true; |
| 340 | return CallbackLater(callback, result); |
| 341 | } |
| 342 | } |
| 343 | busy_ = false; |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 344 | callback.Run(result); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | // When |store| is true, stores the callback to be delivered later; otherwise |
| 348 | // delivers any callback previously stored. |
| 349 | // Static. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 350 | void MockDiskEntry::StoreAndDeliverCallbacks(bool store, |
| 351 | MockDiskEntry* entry, |
| 352 | const CompletionCallback& callback, |
| 353 | int result) { |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 354 | static std::vector<CallbackInfo> callback_list; |
| 355 | if (store) { |
[email protected] | 2c5c9d5 | 2011-12-07 14:15:36 | [diff] [blame] | 356 | CallbackInfo c = {entry, callback, result}; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 357 | callback_list.push_back(c); |
| 358 | } else { |
[email protected] | 2c5c9d5 | 2011-12-07 14:15:36 | [diff] [blame] | 359 | for (size_t i = 0; i < callback_list.size(); i++) { |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 360 | CallbackInfo& c = callback_list[i]; |
[email protected] | 2c5c9d5 | 2011-12-07 14:15:36 | [diff] [blame] | 361 | c.entry->CallbackLater(c.callback, c.result); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 362 | } |
| 363 | callback_list.clear(); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | // Statics. |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 368 | bool MockDiskEntry::ignore_callbacks_ = false; |
| 369 | |
| 370 | //----------------------------------------------------------------------------- |
| 371 | |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 372 | MockDiskCache::MockDiskCache() |
| 373 | : open_count_(0), create_count_(0), fail_requests_(false), |
[email protected] | 954bbe4 | 2013-08-30 12:38:39 | [diff] [blame] | 374 | soft_failures_(false), double_create_check_(true), |
| 375 | fail_sparse_requests_(false) { |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | MockDiskCache::~MockDiskCache() { |
| 379 | ReleaseAll(); |
| 380 | } |
| 381 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 382 | CacheType MockDiskCache::GetCacheType() const { |
| 383 | return DISK_CACHE; |
[email protected] | 6e14396 | 2012-08-09 21:34:33 | [diff] [blame] | 384 | } |
| 385 | |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 386 | int32_t MockDiskCache::GetEntryCount() const { |
| 387 | return static_cast<int32_t>(entries_.size()); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 388 | } |
| 389 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 390 | int MockDiskCache::OpenEntry(const std::string& key, |
| 391 | disk_cache::Entry** entry, |
| 392 | const CompletionCallback& callback) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 393 | DCHECK(!callback.is_null()); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 394 | if (fail_requests_) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 395 | return ERR_CACHE_OPEN_FAILURE; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 396 | |
| 397 | EntryMap::iterator it = entries_.find(key); |
| 398 | if (it == entries_.end()) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 399 | return ERR_CACHE_OPEN_FAILURE; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 400 | |
| 401 | if (it->second->is_doomed()) { |
| 402 | it->second->Release(); |
| 403 | entries_.erase(it); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 404 | return ERR_CACHE_OPEN_FAILURE; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | open_count_++; |
| 408 | |
| 409 | it->second->AddRef(); |
| 410 | *entry = it->second; |
| 411 | |
| 412 | if (soft_failures_) |
| 413 | it->second->set_fail_requests(); |
| 414 | |
| 415 | if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 416 | return OK; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 417 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 418 | CallbackLater(callback, OK); |
| 419 | return ERR_IO_PENDING; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | int MockDiskCache::CreateEntry(const std::string& key, |
| 423 | disk_cache::Entry** entry, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 424 | const CompletionCallback& callback) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 425 | DCHECK(!callback.is_null()); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 426 | if (fail_requests_) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 427 | return ERR_CACHE_CREATE_FAILURE; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 428 | |
| 429 | EntryMap::iterator it = entries_.find(key); |
| 430 | if (it != entries_.end()) { |
[email protected] | 6a019de | 2011-11-30 19:57:29 | [diff] [blame] | 431 | if (!it->second->is_doomed()) { |
| 432 | if (double_create_check_) |
| 433 | NOTREACHED(); |
| 434 | else |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 435 | return ERR_CACHE_CREATE_FAILURE; |
[email protected] | 6a019de | 2011-11-30 19:57:29 | [diff] [blame] | 436 | } |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 437 | it->second->Release(); |
| 438 | entries_.erase(it); |
| 439 | } |
| 440 | |
| 441 | create_count_++; |
| 442 | |
| 443 | MockDiskEntry* new_entry = new MockDiskEntry(key); |
| 444 | |
| 445 | new_entry->AddRef(); |
| 446 | entries_[key] = new_entry; |
| 447 | |
| 448 | new_entry->AddRef(); |
| 449 | *entry = new_entry; |
| 450 | |
| 451 | if (soft_failures_) |
| 452 | new_entry->set_fail_requests(); |
| 453 | |
[email protected] | 954bbe4 | 2013-08-30 12:38:39 | [diff] [blame] | 454 | if (fail_sparse_requests_) |
| 455 | new_entry->set_fail_sparse_requests(); |
| 456 | |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 457 | if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 458 | return OK; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 459 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 460 | CallbackLater(callback, OK); |
| 461 | return ERR_IO_PENDING; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | int MockDiskCache::DoomEntry(const std::string& key, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 465 | const CompletionCallback& callback) { |
[email protected] | 42c45963 | 2011-12-17 02:20:23 | [diff] [blame] | 466 | DCHECK(!callback.is_null()); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 467 | EntryMap::iterator it = entries_.find(key); |
| 468 | if (it != entries_.end()) { |
| 469 | it->second->Release(); |
| 470 | entries_.erase(it); |
| 471 | } |
| 472 | |
| 473 | if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 474 | return OK; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 475 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 476 | CallbackLater(callback, OK); |
| 477 | return ERR_IO_PENDING; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 478 | } |
| 479 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 480 | int MockDiskCache::DoomAllEntries(const CompletionCallback& callback) { |
| 481 | return ERR_NOT_IMPLEMENTED; |
[email protected] | dbe1ffe | 2011-12-12 19:18:56 | [diff] [blame] | 482 | } |
| 483 | |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 484 | int MockDiskCache::DoomEntriesBetween(const base::Time initial_time, |
| 485 | const base::Time end_time, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 486 | const CompletionCallback& callback) { |
| 487 | return ERR_NOT_IMPLEMENTED; |
[email protected] | dbe1ffe | 2011-12-12 19:18:56 | [diff] [blame] | 488 | } |
| 489 | |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 490 | int MockDiskCache::DoomEntriesSince(const base::Time initial_time, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 491 | const CompletionCallback& callback) { |
| 492 | return ERR_NOT_IMPLEMENTED; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 493 | } |
| 494 | |
msramek | aee01ceb | 2015-10-07 14:23:33 | [diff] [blame] | 495 | int MockDiskCache::CalculateSizeOfAllEntries( |
| 496 | const CompletionCallback& callback) { |
| 497 | return ERR_NOT_IMPLEMENTED; |
| 498 | } |
| 499 | |
gavinp | b1aaa05 | 2014-09-24 14:54:35 | [diff] [blame] | 500 | class MockDiskCache::NotImplementedIterator : public Iterator { |
| 501 | public: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 502 | int OpenNextEntry(disk_cache::Entry** next_entry, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 503 | const CompletionCallback& callback) override { |
| 504 | return ERR_NOT_IMPLEMENTED; |
gavinp | b1aaa05 | 2014-09-24 14:54:35 | [diff] [blame] | 505 | } |
| 506 | }; |
[email protected] | 275dbf4 | 2011-12-12 20:41:01 | [diff] [blame] | 507 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 508 | std::unique_ptr<disk_cache::Backend::Iterator> MockDiskCache::CreateIterator() { |
| 509 | return std::unique_ptr<Iterator>(new NotImplementedIterator()); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 510 | } |
| 511 | |
payal.pandey | 90b8137 | 2015-04-30 06:53:04 | [diff] [blame] | 512 | void MockDiskCache::GetStats(base::StringPairs* stats) { |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | void MockDiskCache::OnExternalCacheHit(const std::string& key) { |
| 516 | } |
| 517 | |
jkarlin | 1f2ef177 | 2017-03-28 19:04:38 | [diff] [blame^] | 518 | size_t MockDiskCache::DumpMemoryStats( |
| 519 | base::trace_event::ProcessMemoryDump* pmd, |
| 520 | const std::string& parent_absolute_name) const { |
| 521 | return 0u; |
xunjieli | a0166f4 | 2017-02-23 17:44:57 | [diff] [blame] | 522 | } |
| 523 | |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 524 | void MockDiskCache::ReleaseAll() { |
| 525 | EntryMap::iterator it = entries_.begin(); |
| 526 | for (; it != entries_.end(); ++it) |
| 527 | it->second->Release(); |
| 528 | entries_.clear(); |
| 529 | } |
| 530 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 531 | void MockDiskCache::CallbackLater(const CompletionCallback& callback, |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 532 | int result) { |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 533 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
[email protected] | 42c45963 | 2011-12-17 02:20:23 | [diff] [blame] | 534 | FROM_HERE, base::Bind(&CallbackForwader, callback, result)); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | //----------------------------------------------------------------------------- |
| 538 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 539 | int MockBackendFactory::CreateBackend( |
| 540 | NetLog* net_log, |
| 541 | std::unique_ptr<disk_cache::Backend>* backend, |
| 542 | const CompletionCallback& callback) { |
[email protected] | 8c3f5a3 | 2013-08-01 11:57:53 | [diff] [blame] | 543 | backend->reset(new MockDiskCache()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 544 | return OK; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | //----------------------------------------------------------------------------- |
| 548 | |
zhongyi | fcd13974 | 2017-02-02 02:26:42 | [diff] [blame] | 549 | MockHttpCache::MockHttpCache() : MockHttpCache(false) {} |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 550 | |
mmenke | bc31a2c | 2015-10-29 13:44:45 | [diff] [blame] | 551 | MockHttpCache::MockHttpCache( |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 552 | std::unique_ptr<HttpCache::BackendFactory> disk_cache_factory) |
zhongyi | fcd13974 | 2017-02-02 02:26:42 | [diff] [blame] | 553 | : MockHttpCache(std::move(disk_cache_factory), false) {} |
| 554 | |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 555 | MockHttpCache::MockHttpCache(bool is_main_cache) |
| 556 | : MockHttpCache(base::MakeUnique<MockBackendFactory>(), is_main_cache) {} |
zhongyi | fcd13974 | 2017-02-02 02:26:42 | [diff] [blame] | 557 | |
| 558 | MockHttpCache::MockHttpCache( |
| 559 | std::unique_ptr<HttpCache::BackendFactory> disk_cache_factory, |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 560 | bool is_main_cache) |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 561 | : http_cache_(base::MakeUnique<MockNetworkLayer>(), |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 562 | std::move(disk_cache_factory), |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 563 | is_main_cache) {} |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 564 | |
pcc | 6423796 | 2015-02-25 03:32:53 | [diff] [blame] | 565 | disk_cache::Backend* MockHttpCache::backend() { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 566 | TestCompletionCallback cb; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 567 | disk_cache::Backend* backend; |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 568 | int rv = http_cache_.GetBackend(&backend, cb.callback()); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 569 | rv = cb.GetResult(rv); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 570 | return (rv == OK) ? backend : NULL; |
pcc | 6423796 | 2015-02-25 03:32:53 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | MockDiskCache* MockHttpCache::disk_cache() { |
| 574 | return static_cast<MockDiskCache*>(backend()); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 575 | } |
| 576 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 577 | int MockHttpCache::CreateTransaction(std::unique_ptr<HttpTransaction>* trans) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 578 | return http_cache_.CreateTransaction(DEFAULT_PRIORITY, trans); |
[email protected] | 027bd85a | 2013-12-27 22:39:10 | [diff] [blame] | 579 | } |
| 580 | |
shivanisha | 3701cde0 | 2017-02-09 17:43:07 | [diff] [blame] | 581 | void MockHttpCache::SimulateCacheLockTimeout() { |
| 582 | http_cache_.SimulateCacheLockTimeout(); |
[email protected] | 8aacaf38 | 2014-06-24 05:33:41 | [diff] [blame] | 583 | } |
| 584 | |
rvargas | 43dc8fd | 2015-01-07 23:03:25 | [diff] [blame] | 585 | void MockHttpCache::FailConditionalizations() { |
| 586 | http_cache_.FailConditionalizationForTest(); |
| 587 | } |
| 588 | |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 589 | bool MockHttpCache::ReadResponseInfo(disk_cache::Entry* disk_entry, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 590 | HttpResponseInfo* response_info, |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 591 | bool* response_truncated) { |
| 592 | int size = disk_entry->GetDataSize(0); |
| 593 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 594 | TestCompletionCallback cb; |
| 595 | scoped_refptr<IOBuffer> buffer(new IOBuffer(size)); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 596 | int rv = disk_entry->ReadData(0, 0, buffer.get(), size, cb.callback()); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 597 | rv = cb.GetResult(rv); |
| 598 | EXPECT_EQ(size, rv); |
| 599 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 600 | return HttpCache::ParseResponseInfo(buffer->data(), size, response_info, |
| 601 | response_truncated); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 602 | } |
| 603 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 604 | bool MockHttpCache::WriteResponseInfo(disk_cache::Entry* disk_entry, |
| 605 | const HttpResponseInfo* response_info, |
| 606 | bool skip_transient_headers, |
| 607 | bool response_truncated) { |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 608 | base::Pickle pickle; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 609 | response_info->Persist( |
| 610 | &pickle, skip_transient_headers, response_truncated); |
| 611 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 612 | TestCompletionCallback cb; |
| 613 | scoped_refptr<WrappedIOBuffer> data( |
| 614 | new WrappedIOBuffer(reinterpret_cast<const char*>(pickle.data()))); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 615 | int len = static_cast<int>(pickle.size()); |
| 616 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 617 | int rv = disk_entry->WriteData(0, 0, data.get(), len, cb.callback(), true); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 618 | rv = cb.GetResult(rv); |
| 619 | return (rv == len); |
| 620 | } |
| 621 | |
| 622 | bool MockHttpCache::OpenBackendEntry(const std::string& key, |
| 623 | disk_cache::Entry** entry) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 624 | TestCompletionCallback cb; |
pcc | 6423796 | 2015-02-25 03:32:53 | [diff] [blame] | 625 | int rv = backend()->OpenEntry(key, entry, cb.callback()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 626 | return (cb.GetResult(rv) == OK); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | bool MockHttpCache::CreateBackendEntry(const std::string& key, |
| 630 | disk_cache::Entry** entry, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 631 | NetLog* net_log) { |
| 632 | TestCompletionCallback cb; |
pcc | 6423796 | 2015-02-25 03:32:53 | [diff] [blame] | 633 | int rv = backend()->CreateEntry(key, entry, cb.callback()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 634 | return (cb.GetResult(rv) == OK); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | // Static. |
| 638 | int MockHttpCache::GetTestMode(int test_mode) { |
| 639 | if (!g_test_mode) |
| 640 | return test_mode; |
| 641 | |
| 642 | return g_test_mode; |
| 643 | } |
| 644 | |
| 645 | // Static. |
| 646 | void MockHttpCache::SetTestMode(int test_mode) { |
| 647 | g_test_mode = test_mode; |
| 648 | } |
| 649 | |
| 650 | //----------------------------------------------------------------------------- |
| 651 | |
| 652 | int MockDiskCacheNoCB::CreateEntry(const std::string& key, |
| 653 | disk_cache::Entry** entry, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 654 | const CompletionCallback& callback) { |
| 655 | return ERR_IO_PENDING; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | //----------------------------------------------------------------------------- |
| 659 | |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 660 | int MockBackendNoCbFactory::CreateBackend( |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 661 | NetLog* net_log, |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 662 | std::unique_ptr<disk_cache::Backend>* backend, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 663 | const CompletionCallback& callback) { |
[email protected] | 8c3f5a3 | 2013-08-01 11:57:53 | [diff] [blame] | 664 | backend->reset(new MockDiskCacheNoCB()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 665 | return OK; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | //----------------------------------------------------------------------------- |
| 669 | |
| 670 | MockBlockingBackendFactory::MockBlockingBackendFactory() |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 671 | : backend_(NULL), |
| 672 | block_(true), |
| 673 | fail_(false) { |
| 674 | } |
| 675 | |
| 676 | MockBlockingBackendFactory::~MockBlockingBackendFactory() { |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | int MockBlockingBackendFactory::CreateBackend( |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 680 | NetLog* net_log, |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 681 | std::unique_ptr<disk_cache::Backend>* backend, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 682 | const CompletionCallback& callback) { |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 683 | if (!block_) { |
| 684 | if (!fail_) |
[email protected] | 8c3f5a3 | 2013-08-01 11:57:53 | [diff] [blame] | 685 | backend->reset(new MockDiskCache()); |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 686 | return Result(); |
| 687 | } |
| 688 | |
| 689 | backend_ = backend; |
| 690 | callback_ = callback; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 691 | return ERR_IO_PENDING; |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | void MockBlockingBackendFactory::FinishCreation() { |
| 695 | block_ = false; |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 696 | if (!callback_.is_null()) { |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 697 | if (!fail_) |
[email protected] | 8c3f5a3 | 2013-08-01 11:57:53 | [diff] [blame] | 698 | backend_->reset(new MockDiskCache()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 699 | CompletionCallback cb = callback_; |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 700 | callback_.Reset(); |
| 701 | cb.Run(Result()); // This object can be deleted here. |
[email protected] | f4015600 | 2011-11-22 21:19:08 | [diff] [blame] | 702 | } |
| 703 | } |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 704 | |
| 705 | } // namespace net |