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