forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathappcache_disk_cache.cc
396 lines (333 loc) · 13 KB
/
appcache_disk_cache.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/appcache/appcache_disk_cache.h"
#include <limits>
#include <utility>
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/check.h"
#include "base/files/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "net/base/cache_type.h"
#include "net/base/completion_repeating_callback.h"
#include "net/base/net_errors.h"
namespace content {
// A callback shim that provides storage for the 'backend_ptr' value
// and will delete a resulting ptr if completion occurs after its
// been canceled.
class AppCacheDiskCache::CreateBackendCallbackShim
: public base::RefCounted<CreateBackendCallbackShim> {
public:
explicit CreateBackendCallbackShim(AppCacheDiskCache* object)
: appcache_diskcache_(object) {
}
void Cancel() { appcache_diskcache_ = nullptr; }
void Callback(int return_value) {
if (appcache_diskcache_)
appcache_diskcache_->OnCreateBackendComplete(return_value);
}
std::unique_ptr<disk_cache::Backend> backend_ptr_; // Accessed directly.
private:
friend class base::RefCounted<CreateBackendCallbackShim>;
~CreateBackendCallbackShim() = default;
AppCacheDiskCache* appcache_diskcache_; // Unowned pointer.
};
AppCacheDiskCacheEntry::AppCacheDiskCacheEntry(
disk_cache::Entry* disk_cache_entry,
AppCacheDiskCache* cache)
: disk_cache_entry_(disk_cache_entry), cache_(cache) {
DCHECK(disk_cache_entry);
DCHECK(cache);
cache_->AddOpenEntry(this);
}
AppCacheDiskCacheEntry::~AppCacheDiskCacheEntry() {
if (cache_)
cache_->RemoveOpenEntry(this);
}
int AppCacheDiskCacheEntry::Read(int index,
int64_t offset,
net::IOBuffer* buf,
int buf_len,
net::CompletionOnceCallback callback) {
if (offset < 0 || offset > std::numeric_limits<int32_t>::max())
return net::ERR_INVALID_ARGUMENT;
if (!disk_cache_entry_)
return net::ERR_ABORTED;
return disk_cache_entry_->ReadData(index, static_cast<int>(offset), buf,
buf_len, std::move(callback));
}
int AppCacheDiskCacheEntry::Write(int index,
int64_t offset,
net::IOBuffer* buf,
int buf_len,
net::CompletionOnceCallback callback) {
if (offset < 0 || offset > std::numeric_limits<int32_t>::max())
return net::ERR_INVALID_ARGUMENT;
if (!disk_cache_entry_)
return net::ERR_ABORTED;
const bool kTruncate = true;
return disk_cache_entry_->WriteData(index, static_cast<int>(offset), buf,
buf_len, std::move(callback), kTruncate);
}
int64_t AppCacheDiskCacheEntry::GetSize(int index) {
return disk_cache_entry_ ? disk_cache_entry_->GetDataSize(index) : 0L;
}
void AppCacheDiskCacheEntry::Close() {
if (disk_cache_entry_)
disk_cache_entry_->Close();
delete this;
}
void AppCacheDiskCacheEntry::Abandon() {
cache_ = nullptr;
disk_cache_entry_->Close();
disk_cache_entry_ = nullptr;
}
namespace {
// Separate object to hold state for each Create, Delete, or Doom call
// while the call is in-flight and to produce an EntryImpl upon completion.
class ActiveCall : public base::RefCounted<ActiveCall> {
public:
ActiveCall(const base::WeakPtr<AppCacheDiskCache>& owner,
AppCacheDiskCacheEntry** entry,
net::CompletionOnceCallback callback)
: owner_(owner), entry_(entry), callback_(std::move(callback)) {
DCHECK(owner_);
}
static net::Error CreateEntry(const base::WeakPtr<AppCacheDiskCache>& owner,
int64_t key,
AppCacheDiskCacheEntry** entry,
net::CompletionOnceCallback callback) {
scoped_refptr<ActiveCall> active_call =
base::MakeRefCounted<ActiveCall>(owner, entry, std::move(callback));
disk_cache::EntryResult result = owner->disk_cache()->CreateEntry(
base::NumberToString(key), net::HIGHEST,
base::BindOnce(&ActiveCall::OnAsyncCompletion, active_call));
return active_call->HandleImmediateReturnValue(std::move(result));
}
static net::Error OpenEntry(const base::WeakPtr<AppCacheDiskCache>& owner,
int64_t key,
AppCacheDiskCacheEntry** entry,
net::CompletionOnceCallback callback) {
scoped_refptr<ActiveCall> active_call =
base::MakeRefCounted<ActiveCall>(owner, entry, std::move(callback));
disk_cache::EntryResult result = owner->disk_cache()->OpenEntry(
base::NumberToString(key), net::HIGHEST,
base::BindOnce(&ActiveCall::OnAsyncCompletion, active_call));
return active_call->HandleImmediateReturnValue(std::move(result));
}
static net::Error DoomEntry(const base::WeakPtr<AppCacheDiskCache>& owner,
int64_t key,
net::CompletionOnceCallback callback) {
return owner->disk_cache()->DoomEntry(base::NumberToString(key),
net::HIGHEST, std::move(callback));
}
private:
friend class base::RefCounted<ActiveCall>;
~ActiveCall() = default;
net::Error HandleImmediateReturnValue(disk_cache::EntryResult result) {
net::Error rv = result.net_error();
if (rv == net::ERR_IO_PENDING) {
// OnAsyncCompletion will be called later.
return rv;
}
if (rv == net::OK) {
*entry_ = new AppCacheDiskCacheEntry(result.ReleaseEntry(), owner_.get());
}
return rv;
}
void OnAsyncCompletion(disk_cache::EntryResult result) {
int rv = result.net_error();
if (rv == net::OK) {
if (owner_) {
*entry_ =
new AppCacheDiskCacheEntry(result.ReleaseEntry(), owner_.get());
} else {
result.ReleaseEntry()->Close();
rv = net::ERR_ABORTED;
}
}
std::move(callback_).Run(rv);
}
base::WeakPtr<AppCacheDiskCache> owner_;
AppCacheDiskCacheEntry** entry_;
net::CompletionOnceCallback callback_;
};
} // namespace
AppCacheDiskCache::AppCacheDiskCache()
#if defined(APPCACHE_USE_SIMPLE_CACHE)
: AppCacheDiskCache(true)
#else
: AppCacheDiskCache(false)
#endif
{
}
AppCacheDiskCache::~AppCacheDiskCache() {
Disable();
}
net::Error AppCacheDiskCache::InitWithDiskBackend(
const base::FilePath& disk_cache_directory,
bool force,
base::OnceClosure post_cleanup_callback,
net::CompletionOnceCallback callback) {
return Init(net::APP_CACHE, disk_cache_directory,
std::numeric_limits<int64_t>::max(), force,
std::move(post_cleanup_callback), std::move(callback));
}
net::Error AppCacheDiskCache::InitWithMemBackend(
int64_t mem_cache_size,
net::CompletionOnceCallback callback) {
return Init(net::MEMORY_CACHE, base::FilePath(), mem_cache_size, false,
base::OnceClosure(), std::move(callback));
}
void AppCacheDiskCache::Disable() {
if (is_disabled_)
return;
is_disabled_ = true;
if (create_backend_callback_.get()) {
create_backend_callback_->Cancel();
create_backend_callback_ = nullptr;
OnCreateBackendComplete(net::ERR_ABORTED);
}
// We need to close open file handles in order to reinitalize the
// appcache system on the fly. File handles held in both entries and in
// the main disk_cache::Backend class need to be released.
for (AppCacheDiskCacheEntry* entry : open_entries_) {
entry->Abandon();
}
open_entries_.clear();
disk_cache_.reset();
}
net::Error AppCacheDiskCache::CreateEntry(
int64_t key,
AppCacheDiskCacheEntry** entry,
net::CompletionOnceCallback callback) {
DCHECK(entry);
DCHECK(!callback.is_null());
if (is_disabled_)
return net::ERR_ABORTED;
if (is_initializing_or_waiting_to_initialize()) {
pending_calls_.emplace_back(CREATE, key, entry, std::move(callback));
return net::ERR_IO_PENDING;
}
if (!disk_cache_)
return net::ERR_FAILED;
return ActiveCall::CreateEntry(weak_factory_.GetWeakPtr(), key, entry,
std::move(callback));
}
net::Error AppCacheDiskCache::OpenEntry(int64_t key,
AppCacheDiskCacheEntry** entry,
net::CompletionOnceCallback callback) {
DCHECK(entry);
DCHECK(!callback.is_null());
if (is_disabled_)
return net::ERR_ABORTED;
if (is_initializing_or_waiting_to_initialize()) {
pending_calls_.emplace_back(OPEN, key, entry, std::move(callback));
return net::ERR_IO_PENDING;
}
if (!disk_cache_)
return net::ERR_FAILED;
return ActiveCall::OpenEntry(weak_factory_.GetWeakPtr(), key, entry,
std::move(callback));
}
net::Error AppCacheDiskCache::DoomEntry(int64_t key,
net::CompletionOnceCallback callback) {
DCHECK(!callback.is_null());
if (is_disabled_)
return net::ERR_ABORTED;
if (is_initializing_or_waiting_to_initialize()) {
pending_calls_.emplace_back(DOOM, key, nullptr, std::move(callback));
return net::ERR_IO_PENDING;
}
if (!disk_cache_)
return net::ERR_FAILED;
return ActiveCall::DoomEntry(weak_factory_.GetWeakPtr(), key,
std::move(callback));
}
base::WeakPtr<AppCacheDiskCache> AppCacheDiskCache::GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
AppCacheDiskCache::AppCacheDiskCache(bool use_simple_cache)
: use_simple_cache_(use_simple_cache),
is_disabled_(false),
is_waiting_to_initialize_(false) {}
AppCacheDiskCache::PendingCall::PendingCall(
PendingCallType call_type,
int64_t key,
AppCacheDiskCacheEntry** entry,
net::CompletionOnceCallback callback)
: call_type(call_type),
key(key),
entry(entry),
callback(std::move(callback)) {}
AppCacheDiskCache::PendingCall::PendingCall(PendingCall&&) = default;
AppCacheDiskCache::PendingCall::~PendingCall() = default;
net::Error AppCacheDiskCache::Init(net::CacheType cache_type,
const base::FilePath& cache_directory,
int64_t cache_size,
bool force,
base::OnceClosure post_cleanup_callback,
net::CompletionOnceCallback callback) {
DCHECK(!is_initializing_or_waiting_to_initialize() && !disk_cache_.get());
is_disabled_ = false;
create_backend_callback_ =
base::MakeRefCounted<CreateBackendCallbackShim>(this);
disk_cache::ResetHandling reset_handling =
force ? disk_cache::ResetHandling::kResetOnError
: disk_cache::ResetHandling::kNeverReset;
net::Error return_value = disk_cache::CreateCacheBackend(
cache_type,
use_simple_cache_ ? net::CACHE_BACKEND_SIMPLE
: net::CACHE_BACKEND_DEFAULT,
cache_directory, cache_size, reset_handling, nullptr,
&(create_backend_callback_->backend_ptr_),
std::move(post_cleanup_callback),
base::BindOnce(&CreateBackendCallbackShim::Callback,
create_backend_callback_));
if (return_value == net::ERR_IO_PENDING)
init_callback_ = std::move(callback);
else
OnCreateBackendComplete(return_value);
return return_value;
}
void AppCacheDiskCache::OnCreateBackendComplete(int return_value) {
if (return_value == net::OK) {
disk_cache_ = std::move(create_backend_callback_->backend_ptr_);
}
create_backend_callback_ = nullptr;
// Invoke our clients callback function.
if (!init_callback_.is_null()) {
std::move(init_callback_).Run(return_value);
}
// Service pending calls that were queued up while we were initializing.
for (auto& call : pending_calls_) {
// This is safe, because the callback will only be called once.
net::CompletionRepeatingCallback copyable_callback =
base::AdaptCallbackForRepeating(std::move(call.callback));
return_value = net::ERR_FAILED;
switch (call.call_type) {
case CREATE:
return_value = CreateEntry(call.key, call.entry, copyable_callback);
break;
case OPEN:
return_value = OpenEntry(call.key, call.entry, copyable_callback);
break;
case DOOM:
return_value = DoomEntry(call.key, copyable_callback);
break;
}
// disk_cache::{Create,Open,Doom}Entry() call their callbacks iff they
// return net::ERR_IO_PENDING. In this case, the callback was not called.
// However, the corresponding ServiceWorkerDiskCache wrapper returned
// net::ERR_IO_PENDING as it queued up the pending call. To follow the
// disk_cache API contract, we need to call the callback ourselves here.
if (return_value != net::ERR_IO_PENDING)
copyable_callback.Run(return_value);
}
pending_calls_.clear();
}
} // namespace content