blob: 3ae150b10fd642211a95a3af448d57262f950bcf [file] [log] [blame]
[email protected]93fe75162012-02-09 21:51:311// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]f40156002011-11-22 21:19:082// 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
Josh Karlin79689072018-02-26 19:00:367#include <algorithm>
avid0181f32015-12-10 19:41:478#include <limits>
Bence Béky8f9d7d3952017-10-09 19:58:049#include <memory>
dchengc7eeda422015-12-26 03:56:4810#include <utility>
avid0181f32015-12-10 19:41:4711
[email protected]f40156002011-11-22 21:19:0812#include "base/bind.h"
Bence Békybc1de292018-02-01 15:48:0313#include "base/callback_helpers.h"
skyostil4891b25b2015-06-11 11:43:4514#include "base/location.h"
15#include "base/single_thread_task_runner.h"
gabf767595f2016-05-11 18:50:3516#include "base/threading/thread_task_runner_handle.h"
[email protected]f40156002011-11-22 21:19:0817#include "net/base/net_errors.h"
Shivani Sharmac18f9762017-10-23 16:43:2318#include "net/http/http_cache_writers.h"
[email protected]f40156002011-11-22 21:19:0819#include "testing/gtest/include/gtest/gtest.h"
20
ttuttle859dc7a2015-04-23 19:42:2921namespace net {
22
[email protected]f40156002011-11-22 21:19:0823namespace {
24
asankab5fb4b42016-06-29 11:06:0825// During testing, we are going to limit the size of a cache entry to this many
26// bytes using DCHECKs in order to prevent a test from causing unbounded memory
27// growth. In practice cache entry shouldn't come anywhere near this limit for
28// tests that use the mock cache. If they do, that's likely a problem with the
29// test. If a test requires using massive cache entries, they should use a real
30// cache backend instead.
31const int kMaxMockCacheEntrySize = 100 * 1000 * 1000;
32
[email protected]42c459632011-12-17 02:20:2333// We can override the test mode for a given operation by setting this global
34// variable.
35int g_test_mode = 0;
36
[email protected]f40156002011-11-22 21:19:0837int GetTestModeForEntry(const std::string& key) {
Josh Karlinf3caee1642018-12-10 22:54:2738 std::string url = key;
39
[email protected]f40156002011-11-22 21:19:0840 // 'key' is prefixed with an identifier if it corresponds to a cached POST.
41 // Skip past that to locate the actual URL.
42 //
43 // TODO(darin): It breaks the abstraction a bit that we assume 'key' is an
44 // URL corresponding to a registered MockTransaction. It would be good to
45 // have another way to access the test_mode.
[email protected]f40156002011-11-22 21:19:0846 if (isdigit(key[0])) {
47 size_t slash = key.find('/');
48 DCHECK(slash != std::string::npos);
Josh Karlinf3caee1642018-12-10 22:54:2749 url = url.substr(slash + 1);
[email protected]f40156002011-11-22 21:19:0850 }
Josh Karlinf3caee1642018-12-10 22:54:2751
52 // If we split the cache by top frame origin, then the origin is prepended to
53 // the key. Skip to the second url in the key.
54 if (base::StartsWith(url, "_dk_", base::CompareCase::SENSITIVE)) {
55 auto const pos = url.find("\nhttp");
56 url = url.substr(pos + 1);
57 }
58
59 const MockTransaction* t = FindMockTransaction(GURL(url));
[email protected]f40156002011-11-22 21:19:0860 DCHECK(t);
61 return t->test_mode;
62}
63
[email protected]f40156002011-11-22 21:19:0864} // namespace
65
66//-----------------------------------------------------------------------------
67
68struct MockDiskEntry::CallbackInfo {
69 scoped_refptr<MockDiskEntry> entry;
Maks Orlovich4acf0b582018-06-07 11:39:4470 net::CompletionOnceCallback callback;
[email protected]f40156002011-11-22 21:19:0871 int result;
72};
73
[email protected]f40156002011-11-22 21:19:0874MockDiskEntry::MockDiskEntry(const std::string& key)
rvargas9ba24452015-07-18 03:13:2575 : key_(key),
morloviche63bba92017-08-30 16:28:4476 in_memory_data_(0),
Maks Orlovich780531d2018-11-12 19:51:0577 max_file_size_(std::numeric_limits<int>::max()),
rvargas9ba24452015-07-18 03:13:2578 doomed_(false),
79 sparse_(false),
80 fail_requests_(false),
81 fail_sparse_requests_(false),
82 busy_(false),
83 delayed_(false),
shivanisha2b6e7ee2017-07-14 20:32:0284 cancel_(false),
85 defer_op_(DEFER_NONE),
86 resume_return_code_(0) {
[email protected]f40156002011-11-22 21:19:0887 test_mode_ = GetTestModeForEntry(key);
88}
89
90void MockDiskEntry::Doom() {
91 doomed_ = true;
92}
93
94void MockDiskEntry::Close() {
95 Release();
96}
97
98std::string MockDiskEntry::GetKey() const {
[email protected]f40156002011-11-22 21:19:0899 return key_;
100}
101
102base::Time MockDiskEntry::GetLastUsed() const {
jkarlin91ae3602016-05-25 14:05:38103 return base::Time::Now();
[email protected]f40156002011-11-22 21:19:08104}
105
106base::Time MockDiskEntry::GetLastModified() const {
jkarlin91ae3602016-05-25 14:05:38107 return base::Time::Now();
[email protected]f40156002011-11-22 21:19:08108}
109
avid0181f32015-12-10 19:41:47110int32_t MockDiskEntry::GetDataSize(int index) const {
[email protected]f40156002011-11-22 21:19:08111 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices);
avid0181f32015-12-10 19:41:47112 return static_cast<int32_t>(data_[index].size());
[email protected]f40156002011-11-22 21:19:08113}
114
ttuttle859dc7a2015-04-23 19:42:29115int MockDiskEntry::ReadData(int index,
116 int offset,
117 IOBuffer* buf,
118 int buf_len,
Maks Orlovich4acf0b582018-06-07 11:39:44119 CompletionOnceCallback callback) {
[email protected]f40156002011-11-22 21:19:08120 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices);
[email protected]2a65aceb82011-12-19 20:59:27121 DCHECK(!callback.is_null());
[email protected]f40156002011-11-22 21:19:08122
123 if (fail_requests_)
ttuttle859dc7a2015-04-23 19:42:29124 return ERR_CACHE_READ_FAILURE;
[email protected]f40156002011-11-22 21:19:08125
126 if (offset < 0 || offset > static_cast<int>(data_[index].size()))
ttuttle859dc7a2015-04-23 19:42:29127 return ERR_FAILED;
[email protected]f40156002011-11-22 21:19:08128 if (static_cast<size_t>(offset) == data_[index].size())
129 return 0;
130
131 int num = std::min(buf_len, static_cast<int>(data_[index].size()) - offset);
132 memcpy(buf->data(), &data_[index][offset], num);
133
134 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ)
135 return num;
136
shivanisha2b6e7ee2017-07-14 20:32:02137 // Pause and resume.
138 if (defer_op_ == DEFER_READ) {
139 defer_op_ = DEFER_NONE;
Maks Orlovich4acf0b582018-06-07 11:39:44140 resume_callback_ = std::move(callback);
shivanisha2b6e7ee2017-07-14 20:32:02141 resume_return_code_ = num;
142 return ERR_IO_PENDING;
143 }
144
Maks Orlovich4acf0b582018-06-07 11:39:44145 CallbackLater(std::move(callback), num);
ttuttle859dc7a2015-04-23 19:42:29146 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08147}
148
shivanisha2b6e7ee2017-07-14 20:32:02149void MockDiskEntry::ResumeDiskEntryOperation() {
150 DCHECK(!resume_callback_.is_null());
Maks Orlovich4acf0b582018-06-07 11:39:44151 CallbackLater(std::move(resume_callback_), resume_return_code_);
shivanisha2b6e7ee2017-07-14 20:32:02152 resume_return_code_ = 0;
153}
154
ttuttle859dc7a2015-04-23 19:42:29155int MockDiskEntry::WriteData(int index,
156 int offset,
157 IOBuffer* buf,
158 int buf_len,
Maks Orlovich4acf0b582018-06-07 11:39:44159 CompletionOnceCallback callback,
ttuttle859dc7a2015-04-23 19:42:29160 bool truncate) {
[email protected]f40156002011-11-22 21:19:08161 DCHECK(index >= 0 && index < kNumCacheEntryDataIndices);
[email protected]2a65aceb82011-12-19 20:59:27162 DCHECK(!callback.is_null());
[email protected]f40156002011-11-22 21:19:08163 DCHECK(truncate);
164
165 if (fail_requests_) {
Maks Orlovich4acf0b582018-06-07 11:39:44166 CallbackLater(std::move(callback), ERR_CACHE_READ_FAILURE);
ttuttle859dc7a2015-04-23 19:42:29167 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08168 }
169
170 if (offset < 0 || offset > static_cast<int>(data_[index].size()))
ttuttle859dc7a2015-04-23 19:42:29171 return ERR_FAILED;
[email protected]f40156002011-11-22 21:19:08172
asankab5fb4b42016-06-29 11:06:08173 DCHECK_LT(offset + buf_len, kMaxMockCacheEntrySize);
Maks Orlovich780531d2018-11-12 19:51:05174 if (offset + buf_len > max_file_size_ && index == 1)
175 return net::ERR_FAILED;
176
[email protected]f40156002011-11-22 21:19:08177 data_[index].resize(offset + buf_len);
178 if (buf_len)
179 memcpy(&data_[index][offset], buf->data(), buf_len);
180
181 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE)
182 return buf_len;
183
Josh Karlin79689072018-02-26 19:00:36184 if (defer_op_ == DEFER_WRITE) {
185 defer_op_ = DEFER_NONE;
Maks Orlovich4acf0b582018-06-07 11:39:44186 resume_callback_ = std::move(callback);
Josh Karlin79689072018-02-26 19:00:36187 resume_return_code_ = buf_len;
188 return ERR_IO_PENDING;
189 }
190
Maks Orlovich4acf0b582018-06-07 11:39:44191 CallbackLater(std::move(callback), buf_len);
ttuttle859dc7a2015-04-23 19:42:29192 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08193}
194
avid0181f32015-12-10 19:41:47195int MockDiskEntry::ReadSparseData(int64_t offset,
ttuttle859dc7a2015-04-23 19:42:29196 IOBuffer* buf,
197 int buf_len,
Maks Orlovich4acf0b582018-06-07 11:39:44198 CompletionOnceCallback callback) {
[email protected]2a65aceb82011-12-19 20:59:27199 DCHECK(!callback.is_null());
[email protected]954bbe42013-08-30 12:38:39200 if (fail_sparse_requests_)
ttuttle859dc7a2015-04-23 19:42:29201 return ERR_NOT_IMPLEMENTED;
rvargas9ba24452015-07-18 03:13:25202 if (!sparse_ || busy_ || cancel_)
ttuttle859dc7a2015-04-23 19:42:29203 return ERR_CACHE_OPERATION_NOT_SUPPORTED;
[email protected]f40156002011-11-22 21:19:08204 if (offset < 0)
ttuttle859dc7a2015-04-23 19:42:29205 return ERR_FAILED;
[email protected]f40156002011-11-22 21:19:08206
207 if (fail_requests_)
ttuttle859dc7a2015-04-23 19:42:29208 return ERR_CACHE_READ_FAILURE;
[email protected]f40156002011-11-22 21:19:08209
avid0181f32015-12-10 19:41:47210 DCHECK(offset < std::numeric_limits<int32_t>::max());
[email protected]f40156002011-11-22 21:19:08211 int real_offset = static_cast<int>(offset);
212 if (!buf_len)
213 return 0;
214
215 int num = std::min(static_cast<int>(data_[1].size()) - real_offset,
216 buf_len);
217 memcpy(buf->data(), &data_[1][real_offset], num);
218
219 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ)
220 return num;
221
Maks Orlovich4acf0b582018-06-07 11:39:44222 CallbackLater(std::move(callback), num);
[email protected]f40156002011-11-22 21:19:08223 busy_ = true;
224 delayed_ = false;
ttuttle859dc7a2015-04-23 19:42:29225 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08226}
227
avid0181f32015-12-10 19:41:47228int MockDiskEntry::WriteSparseData(int64_t offset,
ttuttle859dc7a2015-04-23 19:42:29229 IOBuffer* buf,
[email protected]f40156002011-11-22 21:19:08230 int buf_len,
Maks Orlovich4acf0b582018-06-07 11:39:44231 CompletionOnceCallback callback) {
[email protected]2a65aceb82011-12-19 20:59:27232 DCHECK(!callback.is_null());
[email protected]954bbe42013-08-30 12:38:39233 if (fail_sparse_requests_)
ttuttle859dc7a2015-04-23 19:42:29234 return ERR_NOT_IMPLEMENTED;
rvargas9ba24452015-07-18 03:13:25235 if (busy_ || cancel_)
ttuttle859dc7a2015-04-23 19:42:29236 return ERR_CACHE_OPERATION_NOT_SUPPORTED;
[email protected]f40156002011-11-22 21:19:08237 if (!sparse_) {
238 if (data_[1].size())
ttuttle859dc7a2015-04-23 19:42:29239 return ERR_CACHE_OPERATION_NOT_SUPPORTED;
[email protected]f40156002011-11-22 21:19:08240 sparse_ = true;
241 }
242 if (offset < 0)
ttuttle859dc7a2015-04-23 19:42:29243 return ERR_FAILED;
[email protected]f40156002011-11-22 21:19:08244 if (!buf_len)
245 return 0;
246
247 if (fail_requests_)
ttuttle859dc7a2015-04-23 19:42:29248 return ERR_CACHE_READ_FAILURE;
[email protected]f40156002011-11-22 21:19:08249
avid0181f32015-12-10 19:41:47250 DCHECK(offset < std::numeric_limits<int32_t>::max());
[email protected]f40156002011-11-22 21:19:08251 int real_offset = static_cast<int>(offset);
252
asankab5fb4b42016-06-29 11:06:08253 if (static_cast<int>(data_[1].size()) < real_offset + buf_len) {
254 DCHECK_LT(real_offset + buf_len, kMaxMockCacheEntrySize);
[email protected]f40156002011-11-22 21:19:08255 data_[1].resize(real_offset + buf_len);
asankab5fb4b42016-06-29 11:06:08256 }
[email protected]f40156002011-11-22 21:19:08257
258 memcpy(&data_[1][real_offset], buf->data(), buf_len);
259 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE)
260 return buf_len;
261
Maks Orlovich4acf0b582018-06-07 11:39:44262 CallbackLater(std::move(callback), buf_len);
ttuttle859dc7a2015-04-23 19:42:29263 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08264}
265
avid0181f32015-12-10 19:41:47266int MockDiskEntry::GetAvailableRange(int64_t offset,
ttuttle859dc7a2015-04-23 19:42:29267 int len,
avid0181f32015-12-10 19:41:47268 int64_t* start,
Maks Orlovich4acf0b582018-06-07 11:39:44269 CompletionOnceCallback callback) {
[email protected]2a65aceb82011-12-19 20:59:27270 DCHECK(!callback.is_null());
rvargas9ba24452015-07-18 03:13:25271 if (!sparse_ || busy_ || cancel_)
ttuttle859dc7a2015-04-23 19:42:29272 return ERR_CACHE_OPERATION_NOT_SUPPORTED;
[email protected]f40156002011-11-22 21:19:08273 if (offset < 0)
ttuttle859dc7a2015-04-23 19:42:29274 return ERR_FAILED;
[email protected]f40156002011-11-22 21:19:08275
276 if (fail_requests_)
ttuttle859dc7a2015-04-23 19:42:29277 return ERR_CACHE_READ_FAILURE;
[email protected]f40156002011-11-22 21:19:08278
279 *start = offset;
avid0181f32015-12-10 19:41:47280 DCHECK(offset < std::numeric_limits<int32_t>::max());
[email protected]f40156002011-11-22 21:19:08281 int real_offset = static_cast<int>(offset);
282 if (static_cast<int>(data_[1].size()) < real_offset)
283 return 0;
284
285 int num = std::min(static_cast<int>(data_[1].size()) - real_offset, len);
286 int count = 0;
287 for (; num > 0; num--, real_offset++) {
288 if (!count) {
289 if (data_[1][real_offset]) {
290 count++;
291 *start = real_offset;
292 }
293 } else {
294 if (!data_[1][real_offset])
295 break;
296 count++;
297 }
298 }
299 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_WRITE)
300 return count;
301
Maks Orlovich4acf0b582018-06-07 11:39:44302 CallbackLater(std::move(callback), count);
ttuttle859dc7a2015-04-23 19:42:29303 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08304}
305
306bool MockDiskEntry::CouldBeSparse() const {
[email protected]954bbe42013-08-30 12:38:39307 if (fail_sparse_requests_)
308 return false;
[email protected]f40156002011-11-22 21:19:08309 return sparse_;
310}
311
312void MockDiskEntry::CancelSparseIO() {
313 cancel_ = true;
314}
315
Victor Costan45c36ac2018-10-08 07:31:52316net::Error MockDiskEntry::ReadyForSparseIO(CompletionOnceCallback callback) {
[email protected]954bbe42013-08-30 12:38:39317 if (fail_sparse_requests_)
ttuttle859dc7a2015-04-23 19:42:29318 return ERR_NOT_IMPLEMENTED;
[email protected]f40156002011-11-22 21:19:08319 if (!cancel_)
ttuttle859dc7a2015-04-23 19:42:29320 return OK;
[email protected]f40156002011-11-22 21:19:08321
322 cancel_ = false;
[email protected]2a65aceb82011-12-19 20:59:27323 DCHECK(!callback.is_null());
[email protected]f40156002011-11-22 21:19:08324 if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ)
ttuttle859dc7a2015-04-23 19:42:29325 return OK;
[email protected]f40156002011-11-22 21:19:08326
[email protected]2a65aceb82011-12-19 20:59:27327 // The pending operation is already in the message loop (and hopefully
[email protected]f40156002011-11-22 21:19:08328 // already in the second pass). Just notify the caller that it finished.
Maks Orlovich4acf0b582018-06-07 11:39:44329 CallbackLater(std::move(callback), 0);
ttuttle859dc7a2015-04-23 19:42:29330 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08331}
332
Jay Civelli78612bf2018-03-01 20:59:12333void MockDiskEntry::SetLastUsedTimeForTest(base::Time time) {
334 NOTREACHED();
335}
336
[email protected]f40156002011-11-22 21:19:08337// If |value| is true, don't deliver any completion callbacks until called
338// again with |value| set to false. Caution: remember to enable callbacks
339// again or all subsequent tests will fail.
340// Static.
341void MockDiskEntry::IgnoreCallbacks(bool value) {
342 if (ignore_callbacks_ == value)
343 return;
344 ignore_callbacks_ = value;
345 if (!value)
Bence Békya55b3522018-06-11 15:58:52346 StoreAndDeliverCallbacks(false, NULL, CompletionOnceCallback(), 0);
[email protected]f40156002011-11-22 21:19:08347}
348
Chris Watkins7a41d3552017-12-01 02:13:27349MockDiskEntry::~MockDiskEntry() = default;
[email protected]f40156002011-11-22 21:19:08350
351// Unlike the callbacks for MockHttpTransaction, we want this one to run even
352// if the consumer called Close on the MockDiskEntry. We achieve that by
353// leveraging the fact that this class is reference counted.
Maks Orlovich4acf0b582018-06-07 11:39:44354void MockDiskEntry::CallbackLater(CompletionOnceCallback callback, int result) {
[email protected]2c5c9d52011-12-07 14:15:36355 if (ignore_callbacks_)
Maks Orlovich4acf0b582018-06-07 11:39:44356 return StoreAndDeliverCallbacks(true, this, std::move(callback), result);
skyostil4891b25b2015-06-11 11:43:45357 base::ThreadTaskRunnerHandle::Get()->PostTask(
Maks Orlovich4acf0b582018-06-07 11:39:44358 FROM_HERE, base::BindOnce(&MockDiskEntry::RunCallback, this,
359 std::move(callback), result));
[email protected]f40156002011-11-22 21:19:08360}
361
Maks Orlovich4acf0b582018-06-07 11:39:44362void MockDiskEntry::RunCallback(CompletionOnceCallback callback, int result) {
[email protected]f40156002011-11-22 21:19:08363 if (busy_) {
364 // This is kind of hacky, but controlling the behavior of just this entry
365 // from a test is sort of complicated. What we really want to do is
366 // delay the delivery of a sparse IO operation a little more so that the
367 // request start operation (async) will finish without seeing the end of
368 // this operation (already posted to the message loop)... and without
369 // just delaying for n mS (which may cause trouble with slow bots). So
370 // we re-post this operation (all async sparse IO operations will take two
[email protected]2a65aceb82011-12-19 20:59:27371 // trips through the message loop instead of one).
[email protected]f40156002011-11-22 21:19:08372 if (!delayed_) {
373 delayed_ = true;
Maks Orlovich4acf0b582018-06-07 11:39:44374 return CallbackLater(std::move(callback), result);
[email protected]f40156002011-11-22 21:19:08375 }
376 }
377 busy_ = false;
Maks Orlovich4acf0b582018-06-07 11:39:44378 std::move(callback).Run(result);
[email protected]f40156002011-11-22 21:19:08379}
380
381// When |store| is true, stores the callback to be delivered later; otherwise
382// delivers any callback previously stored.
383// Static.
ttuttle859dc7a2015-04-23 19:42:29384void MockDiskEntry::StoreAndDeliverCallbacks(bool store,
385 MockDiskEntry* entry,
Maks Orlovich4acf0b582018-06-07 11:39:44386 CompletionOnceCallback callback,
ttuttle859dc7a2015-04-23 19:42:29387 int result) {
[email protected]f40156002011-11-22 21:19:08388 static std::vector<CallbackInfo> callback_list;
389 if (store) {
Maks Orlovich4acf0b582018-06-07 11:39:44390 CallbackInfo c = {entry, std::move(callback), result};
391 callback_list.push_back(std::move(c));
[email protected]f40156002011-11-22 21:19:08392 } else {
[email protected]2c5c9d52011-12-07 14:15:36393 for (size_t i = 0; i < callback_list.size(); i++) {
[email protected]f40156002011-11-22 21:19:08394 CallbackInfo& c = callback_list[i];
Maks Orlovich4acf0b582018-06-07 11:39:44395 c.entry->CallbackLater(std::move(c.callback), c.result);
[email protected]f40156002011-11-22 21:19:08396 }
397 callback_list.clear();
398 }
399}
400
401// Statics.
[email protected]f40156002011-11-22 21:19:08402bool MockDiskEntry::ignore_callbacks_ = false;
403
404//-----------------------------------------------------------------------------
405
[email protected]f40156002011-11-22 21:19:08406MockDiskCache::MockDiskCache()
shivanisha36a816e2017-07-05 22:27:45407 : open_count_(0),
408 create_count_(0),
409 doomed_count_(0),
Maks Orlovich780531d2018-11-12 19:51:05410 max_file_size_(std::numeric_limits<int>::max()),
shivanisha36a816e2017-07-05 22:27:45411 fail_requests_(false),
412 soft_failures_(false),
Maks Orlovich6a1ba1812018-11-02 00:41:31413 soft_failures_one_instance_(false),
shivanisha36a816e2017-07-05 22:27:45414 double_create_check_(true),
shivanisha2b6e7ee2017-07-14 20:32:02415 fail_sparse_requests_(false),
morloviche63bba92017-08-30 16:28:44416 support_in_memory_entry_data_(true),
shivanisha2b6e7ee2017-07-14 20:32:02417 defer_op_(MockDiskEntry::DEFER_NONE),
418 resume_return_code_(0) {}
[email protected]f40156002011-11-22 21:19:08419
420MockDiskCache::~MockDiskCache() {
421 ReleaseAll();
422}
423
ttuttle859dc7a2015-04-23 19:42:29424CacheType MockDiskCache::GetCacheType() const {
425 return DISK_CACHE;
[email protected]6e143962012-08-09 21:34:33426}
427
avid0181f32015-12-10 19:41:47428int32_t MockDiskCache::GetEntryCount() const {
429 return static_cast<int32_t>(entries_.size());
[email protected]f40156002011-11-22 21:19:08430}
431
Victor Costan45c36ac2018-10-08 07:31:52432net::Error MockDiskCache::OpenEntry(const std::string& key,
433 net::RequestPriority request_priority,
434 disk_cache::Entry** entry,
435 CompletionOnceCallback callback) {
[email protected]2a65aceb82011-12-19 20:59:27436 DCHECK(!callback.is_null());
[email protected]f40156002011-11-22 21:19:08437 if (fail_requests_)
ttuttle859dc7a2015-04-23 19:42:29438 return ERR_CACHE_OPEN_FAILURE;
[email protected]f40156002011-11-22 21:19:08439
jdoerrie22a91d8b92018-10-05 08:43:26440 auto it = entries_.find(key);
[email protected]f40156002011-11-22 21:19:08441 if (it == entries_.end())
ttuttle859dc7a2015-04-23 19:42:29442 return ERR_CACHE_OPEN_FAILURE;
[email protected]f40156002011-11-22 21:19:08443
444 if (it->second->is_doomed()) {
445 it->second->Release();
446 entries_.erase(it);
ttuttle859dc7a2015-04-23 19:42:29447 return ERR_CACHE_OPEN_FAILURE;
[email protected]f40156002011-11-22 21:19:08448 }
449
450 open_count_++;
451
452 it->second->AddRef();
453 *entry = it->second;
454
Maks Orlovich6a1ba1812018-11-02 00:41:31455 if (soft_failures_ || soft_failures_one_instance_) {
[email protected]f40156002011-11-22 21:19:08456 it->second->set_fail_requests();
Maks Orlovich6a1ba1812018-11-02 00:41:31457 soft_failures_one_instance_ = false;
458 }
[email protected]f40156002011-11-22 21:19:08459
Maks Orlovich780531d2018-11-12 19:51:05460 it->second->set_max_file_size(max_file_size_);
461
[email protected]f40156002011-11-22 21:19:08462 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START)
ttuttle859dc7a2015-04-23 19:42:29463 return OK;
[email protected]f40156002011-11-22 21:19:08464
Maks Orlovich4acf0b582018-06-07 11:39:44465 CallbackLater(std::move(callback), OK);
ttuttle859dc7a2015-04-23 19:42:29466 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08467}
468
Victor Costan45c36ac2018-10-08 07:31:52469net::Error MockDiskCache::CreateEntry(const std::string& key,
470 net::RequestPriority request_priority,
471 disk_cache::Entry** entry,
472 CompletionOnceCallback callback) {
[email protected]2a65aceb82011-12-19 20:59:27473 DCHECK(!callback.is_null());
[email protected]f40156002011-11-22 21:19:08474 if (fail_requests_)
ttuttle859dc7a2015-04-23 19:42:29475 return ERR_CACHE_CREATE_FAILURE;
[email protected]f40156002011-11-22 21:19:08476
jdoerrie22a91d8b92018-10-05 08:43:26477 auto it = entries_.find(key);
[email protected]f40156002011-11-22 21:19:08478 if (it != entries_.end()) {
[email protected]6a019de2011-11-30 19:57:29479 if (!it->second->is_doomed()) {
480 if (double_create_check_)
481 NOTREACHED();
482 else
ttuttle859dc7a2015-04-23 19:42:29483 return ERR_CACHE_CREATE_FAILURE;
[email protected]6a019de2011-11-30 19:57:29484 }
[email protected]f40156002011-11-22 21:19:08485 it->second->Release();
486 entries_.erase(it);
487 }
488
489 create_count_++;
490
491 MockDiskEntry* new_entry = new MockDiskEntry(key);
492
493 new_entry->AddRef();
494 entries_[key] = new_entry;
495
496 new_entry->AddRef();
497 *entry = new_entry;
498
Maks Orlovich6a1ba1812018-11-02 00:41:31499 if (soft_failures_ || soft_failures_one_instance_) {
[email protected]f40156002011-11-22 21:19:08500 new_entry->set_fail_requests();
Maks Orlovich6a1ba1812018-11-02 00:41:31501 soft_failures_one_instance_ = false;
502 }
[email protected]f40156002011-11-22 21:19:08503
[email protected]954bbe42013-08-30 12:38:39504 if (fail_sparse_requests_)
505 new_entry->set_fail_sparse_requests();
506
Maks Orlovich780531d2018-11-12 19:51:05507 new_entry->set_max_file_size(max_file_size_);
508
[email protected]f40156002011-11-22 21:19:08509 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START)
ttuttle859dc7a2015-04-23 19:42:29510 return OK;
[email protected]f40156002011-11-22 21:19:08511
shivanisha2b6e7ee2017-07-14 20:32:02512 // Pause and resume.
513 if (defer_op_ == MockDiskEntry::DEFER_CREATE) {
514 defer_op_ = MockDiskEntry::DEFER_NONE;
Maks Orlovich4acf0b582018-06-07 11:39:44515 resume_callback_ = std::move(callback);
shivanisha2b6e7ee2017-07-14 20:32:02516 resume_return_code_ = OK;
517 return ERR_IO_PENDING;
518 }
519
Maks Orlovich4acf0b582018-06-07 11:39:44520 CallbackLater(std::move(callback), OK);
ttuttle859dc7a2015-04-23 19:42:29521 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08522}
523
Victor Costan45c36ac2018-10-08 07:31:52524net::Error MockDiskCache::DoomEntry(const std::string& key,
525 net::RequestPriority request_priority,
526 CompletionOnceCallback callback) {
[email protected]42c459632011-12-17 02:20:23527 DCHECK(!callback.is_null());
jdoerrie22a91d8b92018-10-05 08:43:26528 auto it = entries_.find(key);
[email protected]f40156002011-11-22 21:19:08529 if (it != entries_.end()) {
530 it->second->Release();
531 entries_.erase(it);
shivanisha36a816e2017-07-05 22:27:45532 doomed_count_++;
[email protected]f40156002011-11-22 21:19:08533 }
534
535 if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START)
ttuttle859dc7a2015-04-23 19:42:29536 return OK;
[email protected]f40156002011-11-22 21:19:08537
Maks Orlovich4acf0b582018-06-07 11:39:44538 CallbackLater(std::move(callback), OK);
ttuttle859dc7a2015-04-23 19:42:29539 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08540}
541
Victor Costan45c36ac2018-10-08 07:31:52542net::Error MockDiskCache::DoomAllEntries(CompletionOnceCallback callback) {
ttuttle859dc7a2015-04-23 19:42:29543 return ERR_NOT_IMPLEMENTED;
[email protected]dbe1ffe2011-12-12 19:18:56544}
545
Victor Costan45c36ac2018-10-08 07:31:52546net::Error MockDiskCache::DoomEntriesBetween(const base::Time initial_time,
547 const base::Time end_time,
548 CompletionOnceCallback callback) {
ttuttle859dc7a2015-04-23 19:42:29549 return ERR_NOT_IMPLEMENTED;
[email protected]dbe1ffe2011-12-12 19:18:56550}
551
Victor Costan45c36ac2018-10-08 07:31:52552net::Error MockDiskCache::DoomEntriesSince(const base::Time initial_time,
553 CompletionOnceCallback callback) {
ttuttle859dc7a2015-04-23 19:42:29554 return ERR_NOT_IMPLEMENTED;
[email protected]f40156002011-11-22 21:19:08555}
556
Ben Kelly5a16ed02018-09-07 21:28:09557int64_t MockDiskCache::CalculateSizeOfAllEntries(
558 Int64CompletionOnceCallback callback) {
msramekaee01ceb2015-10-07 14:23:33559 return ERR_NOT_IMPLEMENTED;
560}
561
gavinpb1aaa052014-09-24 14:54:35562class MockDiskCache::NotImplementedIterator : public Iterator {
563 public:
Victor Costan45c36ac2018-10-08 07:31:52564 net::Error OpenNextEntry(disk_cache::Entry** next_entry,
565 CompletionOnceCallback callback) override {
ttuttle859dc7a2015-04-23 19:42:29566 return ERR_NOT_IMPLEMENTED;
gavinpb1aaa052014-09-24 14:54:35567 }
568};
[email protected]275dbf42011-12-12 20:41:01569
danakj1fd259a02016-04-16 03:17:09570std::unique_ptr<disk_cache::Backend::Iterator> MockDiskCache::CreateIterator() {
571 return std::unique_ptr<Iterator>(new NotImplementedIterator());
[email protected]f40156002011-11-22 21:19:08572}
573
payal.pandey90b81372015-04-30 06:53:04574void MockDiskCache::GetStats(base::StringPairs* stats) {
[email protected]f40156002011-11-22 21:19:08575}
576
577void MockDiskCache::OnExternalCacheHit(const std::string& key) {
Steven Bingler674fb0822018-12-12 18:04:52578 external_cache_hits_.push_back(key);
[email protected]f40156002011-11-22 21:19:08579}
580
jkarlin1f2ef1772017-03-28 19:04:38581size_t MockDiskCache::DumpMemoryStats(
582 base::trace_event::ProcessMemoryDump* pmd,
583 const std::string& parent_absolute_name) const {
584 return 0u;
xunjielia0166f42017-02-23 17:44:57585}
586
morloviche63bba92017-08-30 16:28:44587uint8_t MockDiskCache::GetEntryInMemoryData(const std::string& key) {
588 if (!support_in_memory_entry_data_)
589 return 0;
590
jdoerrie22a91d8b92018-10-05 08:43:26591 auto it = entries_.find(key);
morloviche63bba92017-08-30 16:28:44592 if (it != entries_.end())
593 return it->second->in_memory_data();
594 return 0;
595}
596
597void MockDiskCache::SetEntryInMemoryData(const std::string& key, uint8_t data) {
jdoerrie22a91d8b92018-10-05 08:43:26598 auto it = entries_.find(key);
morloviche63bba92017-08-30 16:28:44599 if (it != entries_.end())
600 it->second->set_in_memory_data(data);
601}
602
Maks Orlovich780531d2018-11-12 19:51:05603int64_t MockDiskCache::MaxFileSize() const {
604 return max_file_size_;
605}
606
[email protected]f40156002011-11-22 21:19:08607void MockDiskCache::ReleaseAll() {
shivanisha2b6e7ee2017-07-14 20:32:02608 for (auto entry : entries_)
609 entry.second->Release();
[email protected]f40156002011-11-22 21:19:08610 entries_.clear();
611}
612
Maks Orlovich4acf0b582018-06-07 11:39:44613void MockDiskCache::CallbackLater(CompletionOnceCallback callback, int result) {
skyostil4891b25b2015-06-11 11:43:45614 base::ThreadTaskRunnerHandle::Get()->PostTask(
Maks Orlovich4acf0b582018-06-07 11:39:44615 FROM_HERE, base::BindOnce(std::move(callback), result));
[email protected]f40156002011-11-22 21:19:08616}
617
shivanisha8061c4202017-06-13 23:35:52618bool MockDiskCache::IsDiskEntryDoomed(const std::string& key) {
619 auto it = entries_.find(key);
shivanisha2b6e7ee2017-07-14 20:32:02620 if (it != entries_.end())
621 return it->second->is_doomed();
622
623 return false;
624}
625
626void MockDiskCache::ResumeCacheOperation() {
627 DCHECK(!resume_callback_.is_null());
Maks Orlovich4acf0b582018-06-07 11:39:44628 CallbackLater(std::move(resume_callback_), resume_return_code_);
shivanisha2b6e7ee2017-07-14 20:32:02629 resume_return_code_ = 0;
630}
631
632scoped_refptr<MockDiskEntry> MockDiskCache::GetDiskEntryRef(
633 const std::string& key) {
634 auto it = entries_.find(key);
shivanisha8061c4202017-06-13 23:35:52635 if (it == entries_.end())
shivanisha2b6e7ee2017-07-14 20:32:02636 return nullptr;
637 return it->second;
shivanisha8061c4202017-06-13 23:35:52638}
639
Steven Bingler674fb0822018-12-12 18:04:52640const std::vector<std::string>& MockDiskCache::GetExternalCacheHits() const {
641 return external_cache_hits_;
642}
643
[email protected]f40156002011-11-22 21:19:08644//-----------------------------------------------------------------------------
645
danakj1fd259a02016-04-16 03:17:09646int MockBackendFactory::CreateBackend(
647 NetLog* net_log,
648 std::unique_ptr<disk_cache::Backend>* backend,
Bence Békya55b3522018-06-11 15:58:52649 CompletionOnceCallback callback) {
[email protected]8c3f5a32013-08-01 11:57:53650 backend->reset(new MockDiskCache());
ttuttle859dc7a2015-04-23 19:42:29651 return OK;
[email protected]f40156002011-11-22 21:19:08652}
653
654//-----------------------------------------------------------------------------
655
zhongyifcd139742017-02-02 02:26:42656MockHttpCache::MockHttpCache() : MockHttpCache(false) {}
[email protected]f40156002011-11-22 21:19:08657
mmenkebc31a2c2015-10-29 13:44:45658MockHttpCache::MockHttpCache(
danakj1fd259a02016-04-16 03:17:09659 std::unique_ptr<HttpCache::BackendFactory> disk_cache_factory)
zhongyifcd139742017-02-02 02:26:42660 : MockHttpCache(std::move(disk_cache_factory), false) {}
661
zhongyi4928bd52017-02-08 02:16:27662MockHttpCache::MockHttpCache(bool is_main_cache)
Jeremy Roman0579ed62017-08-29 15:56:19663 : MockHttpCache(std::make_unique<MockBackendFactory>(), is_main_cache) {}
zhongyifcd139742017-02-02 02:26:42664
665MockHttpCache::MockHttpCache(
666 std::unique_ptr<HttpCache::BackendFactory> disk_cache_factory,
zhongyi4928bd52017-02-08 02:16:27667 bool is_main_cache)
Jeremy Roman0579ed62017-08-29 15:56:19668 : http_cache_(std::make_unique<MockNetworkLayer>(),
dchengc7eeda422015-12-26 03:56:48669 std::move(disk_cache_factory),
zhongyi4928bd52017-02-08 02:16:27670 is_main_cache) {}
[email protected]f40156002011-11-22 21:19:08671
pcc64237962015-02-25 03:32:53672disk_cache::Backend* MockHttpCache::backend() {
ttuttle859dc7a2015-04-23 19:42:29673 TestCompletionCallback cb;
[email protected]f40156002011-11-22 21:19:08674 disk_cache::Backend* backend;
[email protected]2a65aceb82011-12-19 20:59:27675 int rv = http_cache_.GetBackend(&backend, cb.callback());
[email protected]f40156002011-11-22 21:19:08676 rv = cb.GetResult(rv);
ttuttle859dc7a2015-04-23 19:42:29677 return (rv == OK) ? backend : NULL;
pcc64237962015-02-25 03:32:53678}
679
680MockDiskCache* MockHttpCache::disk_cache() {
681 return static_cast<MockDiskCache*>(backend());
[email protected]f40156002011-11-22 21:19:08682}
683
danakj1fd259a02016-04-16 03:17:09684int MockHttpCache::CreateTransaction(std::unique_ptr<HttpTransaction>* trans) {
ttuttle859dc7a2015-04-23 19:42:29685 return http_cache_.CreateTransaction(DEFAULT_PRIORITY, trans);
[email protected]027bd85a2013-12-27 22:39:10686}
687
shivanisha3701cde02017-02-09 17:43:07688void MockHttpCache::SimulateCacheLockTimeout() {
shivanishabc3b71b2017-06-24 07:21:14689 http_cache_.SimulateCacheLockTimeoutForTesting();
690}
691
692void MockHttpCache::SimulateCacheLockTimeoutAfterHeaders() {
693 http_cache_.SimulateCacheLockTimeoutAfterHeadersForTesting();
[email protected]8aacaf382014-06-24 05:33:41694}
695
rvargas43dc8fd2015-01-07 23:03:25696void MockHttpCache::FailConditionalizations() {
697 http_cache_.FailConditionalizationForTest();
698}
699
[email protected]f40156002011-11-22 21:19:08700bool MockHttpCache::ReadResponseInfo(disk_cache::Entry* disk_entry,
ttuttle859dc7a2015-04-23 19:42:29701 HttpResponseInfo* response_info,
[email protected]f40156002011-11-22 21:19:08702 bool* response_truncated) {
703 int size = disk_entry->GetDataSize(0);
704
ttuttle859dc7a2015-04-23 19:42:29705 TestCompletionCallback cb;
Victor Costan9c7302b2018-08-27 16:39:44706 scoped_refptr<IOBuffer> buffer = base::MakeRefCounted<IOBuffer>(size);
[email protected]90499482013-06-01 00:39:50707 int rv = disk_entry->ReadData(0, 0, buffer.get(), size, cb.callback());
[email protected]f40156002011-11-22 21:19:08708 rv = cb.GetResult(rv);
709 EXPECT_EQ(size, rv);
710
ttuttle859dc7a2015-04-23 19:42:29711 return HttpCache::ParseResponseInfo(buffer->data(), size, response_info,
712 response_truncated);
[email protected]f40156002011-11-22 21:19:08713}
714
ttuttle859dc7a2015-04-23 19:42:29715bool MockHttpCache::WriteResponseInfo(disk_cache::Entry* disk_entry,
716 const HttpResponseInfo* response_info,
717 bool skip_transient_headers,
718 bool response_truncated) {
brettwbd4d7112015-06-03 04:29:25719 base::Pickle pickle;
[email protected]f40156002011-11-22 21:19:08720 response_info->Persist(
721 &pickle, skip_transient_headers, response_truncated);
722
ttuttle859dc7a2015-04-23 19:42:29723 TestCompletionCallback cb;
Victor Costan9c7302b2018-08-27 16:39:44724 scoped_refptr<WrappedIOBuffer> data = base::MakeRefCounted<WrappedIOBuffer>(
725 reinterpret_cast<const char*>(pickle.data()));
[email protected]f40156002011-11-22 21:19:08726 int len = static_cast<int>(pickle.size());
727
[email protected]90499482013-06-01 00:39:50728 int rv = disk_entry->WriteData(0, 0, data.get(), len, cb.callback(), true);
[email protected]f40156002011-11-22 21:19:08729 rv = cb.GetResult(rv);
730 return (rv == len);
731}
732
733bool MockHttpCache::OpenBackendEntry(const std::string& key,
734 disk_cache::Entry** entry) {
ttuttle859dc7a2015-04-23 19:42:29735 TestCompletionCallback cb;
Josh Karlindd9a5d142018-06-06 00:35:48736 int rv = backend()->OpenEntry(key, net::HIGHEST, entry, cb.callback());
ttuttle859dc7a2015-04-23 19:42:29737 return (cb.GetResult(rv) == OK);
[email protected]f40156002011-11-22 21:19:08738}
739
740bool MockHttpCache::CreateBackendEntry(const std::string& key,
741 disk_cache::Entry** entry,
ttuttle859dc7a2015-04-23 19:42:29742 NetLog* net_log) {
743 TestCompletionCallback cb;
Josh Karlindd9a5d142018-06-06 00:35:48744 int rv = backend()->CreateEntry(key, net::HIGHEST, entry, cb.callback());
ttuttle859dc7a2015-04-23 19:42:29745 return (cb.GetResult(rv) == OK);
[email protected]f40156002011-11-22 21:19:08746}
747
748// Static.
749int MockHttpCache::GetTestMode(int test_mode) {
750 if (!g_test_mode)
751 return test_mode;
752
753 return g_test_mode;
754}
755
756// Static.
757void MockHttpCache::SetTestMode(int test_mode) {
758 g_test_mode = test_mode;
759}
760
shivanisha8061c4202017-06-13 23:35:52761bool MockHttpCache::IsWriterPresent(const std::string& key) {
762 HttpCache::ActiveEntry* entry = http_cache_.FindActiveEntry(key);
Shivani Sharmac18f9762017-10-23 16:43:23763 return entry && entry->writers && !entry->writers->IsEmpty();
shivanisha8061c4202017-06-13 23:35:52764}
765
766bool MockHttpCache::IsHeadersTransactionPresent(const std::string& key) {
767 HttpCache::ActiveEntry* entry = http_cache_.FindActiveEntry(key);
Shivani Sharmac18f9762017-10-23 16:43:23768 return entry && entry->headers_transaction;
shivanisha8061c4202017-06-13 23:35:52769}
770
771int MockHttpCache::GetCountReaders(const std::string& key) {
772 HttpCache::ActiveEntry* entry = http_cache_.FindActiveEntry(key);
Shivani Sharmaedd8dd282017-10-30 14:04:45773 return entry ? entry->readers.size() : 0;
shivanisha8061c4202017-06-13 23:35:52774}
775
776int MockHttpCache::GetCountAddToEntryQueue(const std::string& key) {
777 HttpCache::ActiveEntry* entry = http_cache_.FindActiveEntry(key);
Shivani Sharmaedd8dd282017-10-30 14:04:45778 return entry ? entry->add_to_entry_queue.size() : 0;
shivanisha8061c4202017-06-13 23:35:52779}
780
781int MockHttpCache::GetCountDoneHeadersQueue(const std::string& key) {
782 HttpCache::ActiveEntry* entry = http_cache_.FindActiveEntry(key);
Shivani Sharmaedd8dd282017-10-30 14:04:45783 return entry ? entry->done_headers_queue.size() : 0;
784}
785
786int MockHttpCache::GetCountWriterTransactions(const std::string& key) {
787 HttpCache::ActiveEntry* entry = http_cache_.FindActiveEntry(key);
788 return entry && entry->writers ? entry->writers->GetTransactionsCount() : 0;
shivanisha8061c4202017-06-13 23:35:52789}
790
[email protected]f40156002011-11-22 21:19:08791//-----------------------------------------------------------------------------
792
Victor Costan45c36ac2018-10-08 07:31:52793net::Error MockDiskCacheNoCB::CreateEntry(const std::string& key,
794 net::RequestPriority request_priority,
795 disk_cache::Entry** entry,
796 CompletionOnceCallback callback) {
ttuttle859dc7a2015-04-23 19:42:29797 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08798}
799
800//-----------------------------------------------------------------------------
801
[email protected]2a65aceb82011-12-19 20:59:27802int MockBackendNoCbFactory::CreateBackend(
ttuttle859dc7a2015-04-23 19:42:29803 NetLog* net_log,
danakj1fd259a02016-04-16 03:17:09804 std::unique_ptr<disk_cache::Backend>* backend,
Bence Békya55b3522018-06-11 15:58:52805 CompletionOnceCallback callback) {
[email protected]8c3f5a32013-08-01 11:57:53806 backend->reset(new MockDiskCacheNoCB());
ttuttle859dc7a2015-04-23 19:42:29807 return OK;
[email protected]f40156002011-11-22 21:19:08808}
809
810//-----------------------------------------------------------------------------
811
812MockBlockingBackendFactory::MockBlockingBackendFactory()
[email protected]2a65aceb82011-12-19 20:59:27813 : backend_(NULL),
814 block_(true),
815 fail_(false) {
816}
817
Chris Watkins7a41d3552017-12-01 02:13:27818MockBlockingBackendFactory::~MockBlockingBackendFactory() = default;
[email protected]f40156002011-11-22 21:19:08819
820int MockBlockingBackendFactory::CreateBackend(
ttuttle859dc7a2015-04-23 19:42:29821 NetLog* net_log,
danakj1fd259a02016-04-16 03:17:09822 std::unique_ptr<disk_cache::Backend>* backend,
Bence Békya55b3522018-06-11 15:58:52823 CompletionOnceCallback callback) {
[email protected]f40156002011-11-22 21:19:08824 if (!block_) {
825 if (!fail_)
[email protected]8c3f5a32013-08-01 11:57:53826 backend->reset(new MockDiskCache());
[email protected]f40156002011-11-22 21:19:08827 return Result();
828 }
829
830 backend_ = backend;
Bence Békya55b3522018-06-11 15:58:52831 callback_ = std::move(callback);
ttuttle859dc7a2015-04-23 19:42:29832 return ERR_IO_PENDING;
[email protected]f40156002011-11-22 21:19:08833}
834
835void MockBlockingBackendFactory::FinishCreation() {
836 block_ = false;
[email protected]2a65aceb82011-12-19 20:59:27837 if (!callback_.is_null()) {
[email protected]f40156002011-11-22 21:19:08838 if (!fail_)
[email protected]8c3f5a32013-08-01 11:57:53839 backend_->reset(new MockDiskCache());
Bence Békybc1de292018-02-01 15:48:03840 // Running the callback might delete |this|.
841 base::ResetAndReturn(&callback_).Run(Result());
[email protected]f40156002011-11-22 21:19:08842 }
843}
ttuttle859dc7a2015-04-23 19:42:29844
845} // namespace net