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
content / browser / background_fetch / background_fetch_data_manager.cc [blame]
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/background_fetch/background_fetch_data_manager.h"
#include <utility>
#include "base/command_line.h"
#include "base/containers/queue.h"
#include "base/observer_list.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "components/services/storage/public/cpp/buckets/bucket_locator.h"
#include "components/services/storage/public/mojom/cache_storage_control.mojom.h"
#include "content/browser/background_fetch/background_fetch_constants.h"
#include "content/browser/background_fetch/background_fetch_data_manager_observer.h"
#include "content/browser/background_fetch/background_fetch_request_info.h"
#include "content/browser/background_fetch/storage/cleanup_task.h"
#include "content/browser/background_fetch/storage/create_metadata_task.h"
#include "content/browser/background_fetch/storage/database_task.h"
#include "content/browser/background_fetch/storage/delete_registration_task.h"
#include "content/browser/background_fetch/storage/get_developer_ids_task.h"
#include "content/browser/background_fetch/storage/get_metadata_task.h"
#include "content/browser/background_fetch/storage/get_registration_task.h"
#include "content/browser/background_fetch/storage/get_request_blob_task.h"
#include "content/browser/background_fetch/storage/mark_registration_for_deletion_task.h"
#include "content/browser/background_fetch/storage/mark_request_complete_task.h"
#include "content/browser/background_fetch/storage/match_requests_task.h"
#include "content/browser/background_fetch/storage/start_next_pending_request_task.h"
#include "content/browser/blob_storage/chrome_blob_storage_context.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/browser/storage_partition_impl.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "storage/browser/quota/quota_manager_proxy.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/blob/blob.mojom.h"
namespace content {
BackgroundFetchDataManager::BackgroundFetchDataManager(
base::WeakPtr<StoragePartitionImpl> storage_partition,
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context,
scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy)
: service_worker_context_(std::move(service_worker_context)),
storage_partition_(std::move(storage_partition)),
quota_manager_proxy_(std::move(quota_manager_proxy)) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(storage_partition_);
// Store the blob storage context for the given |browser_context|.
blob_storage_context_ = base::WrapRefCounted(
ChromeBlobStorageContext::GetFor(storage_partition_->browser_context()));
DCHECK(blob_storage_context_);
}
void BackgroundFetchDataManager::Initialize() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Delete inactive registrations still in the DB.
Cleanup();
}
void BackgroundFetchDataManager::AddObserver(
BackgroundFetchDataManagerObserver* observer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
observers_.AddObserver(observer);
}
void BackgroundFetchDataManager::RemoveObserver(
BackgroundFetchDataManagerObserver* observer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
observers_.RemoveObserver(observer);
}
void BackgroundFetchDataManager::Cleanup() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
AddDatabaseTask(std::make_unique<background_fetch::CleanupTask>(this));
}
mojo::Remote<blink::mojom::CacheStorage>&
BackgroundFetchDataManager::GetOrOpenCacheStorage(
const blink::StorageKey& storage_key,
const std::string& unique_id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto it = cache_storage_remote_map_.find(unique_id);
if (it != cache_storage_remote_map_.end()) {
// TODO(enne): should we store `storage_key` so we can DCHECK it matches
// here?
return it->second;
}
if (!storage_partition_)
return null_remote_;
// This storage key and unique_id has never been opened before.
mojo::Remote<blink::mojom::CacheStorage> remote;
network::CrossOriginEmbedderPolicy cross_origin_embedder_policy;
network::DocumentIsolationPolicy document_isolation_policy;
storage_partition_->GetCacheStorageControl()->AddReceiver(
cross_origin_embedder_policy, mojo::NullRemote(),
document_isolation_policy,
storage::BucketLocator::ForDefaultBucket(storage_key),
storage::mojom::CacheStorageOwner::kBackgroundFetch,
remote.BindNewPipeAndPassReceiver());
auto result = cache_storage_remote_map_.emplace(unique_id, std::move(remote));
DCHECK(result.second);
return result.first->second;
}
void BackgroundFetchDataManager::OpenCache(
const blink::StorageKey& storage_key,
const std::string& unique_id,
int64_t trace_id,
blink::mojom::CacheStorage::OpenCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto& cache_storage = GetOrOpenCacheStorage(storage_key, unique_id);
if (!cache_storage)
return;
cache_storage->Open(base::UTF8ToUTF16(unique_id), trace_id,
std::move(callback));
}
void BackgroundFetchDataManager::DeleteCache(
const blink::StorageKey& storage_key,
const std::string& unique_id,
int64_t trace_id,
blink::mojom::CacheStorage::DeleteCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto& cache_storage = GetOrOpenCacheStorage(storage_key, unique_id);
if (!cache_storage)
return;
cache_storage->Delete(
base::UTF8ToUTF16(unique_id), trace_id,
base::BindOnce(&BackgroundFetchDataManager::DidDeleteCache,
weak_ptr_factory_.GetWeakPtr(), unique_id,
std::move(callback)));
}
void BackgroundFetchDataManager::DidDeleteCache(
const std::string& unique_id,
blink::mojom::CacheStorage::DeleteCallback callback,
blink::mojom::CacheStorageError result) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Preserve the lifetime of the cache storage remote until here so that this
// DidDeleteCache callback will not be dropped.
cache_storage_remote_map_.erase(unique_id);
std::move(callback).Run(result);
}
void BackgroundFetchDataManager::HasCache(
const blink::StorageKey& storage_key,
const std::string& unique_id,
int64_t trace_id,
blink::mojom::CacheStorage::HasCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto& cache_storage = GetOrOpenCacheStorage(storage_key, unique_id);
if (!cache_storage)
return;
cache_storage->Has(base::UTF8ToUTF16(unique_id), trace_id,
std::move(callback));
}
BackgroundFetchDataManager::~BackgroundFetchDataManager() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
void BackgroundFetchDataManager::GetInitializationData(
GetInitializationDataCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
AddDatabaseTask(std::make_unique<background_fetch::GetInitializationDataTask>(
this, std::move(callback)));
}
void BackgroundFetchDataManager::CreateRegistration(
const BackgroundFetchRegistrationId& registration_id,
std::vector<blink::mojom::FetchAPIRequestPtr> requests,
blink::mojom::BackgroundFetchOptionsPtr options,
const SkBitmap& icon,
bool start_paused,
const net::IsolationInfo& isolation_info,
CreateRegistrationCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
AddDatabaseTask(std::make_unique<background_fetch::CreateMetadataTask>(
this, registration_id, std::move(requests), std::move(options), icon,
start_paused, isolation_info, std::move(callback)));
}
void BackgroundFetchDataManager::GetRegistration(
int64_t service_worker_registration_id,
const blink::StorageKey& storage_key,
const std::string& developer_id,
GetRegistrationCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
AddDatabaseTask(std::make_unique<background_fetch::GetRegistrationTask>(
this, service_worker_registration_id, storage_key, developer_id,
std::move(callback)));
}
void BackgroundFetchDataManager::PopNextRequest(
const BackgroundFetchRegistrationId& registration_id,
NextRequestCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
AddDatabaseTask(
std::make_unique<background_fetch::StartNextPendingRequestTask>(
this, registration_id, std::move(callback)));
}
void BackgroundFetchDataManager::GetRequestBlob(
const BackgroundFetchRegistrationId& registration_id,
const scoped_refptr<BackgroundFetchRequestInfo>& request_info,
GetRequestBlobCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
AddDatabaseTask(std::make_unique<background_fetch::GetRequestBlobTask>(
this, registration_id, request_info, std::move(callback)));
}
void BackgroundFetchDataManager::MarkRequestAsComplete(
const BackgroundFetchRegistrationId& registration_id,
scoped_refptr<BackgroundFetchRequestInfo> request_info,
MarkRequestCompleteCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
AddDatabaseTask(std::make_unique<background_fetch::MarkRequestCompleteTask>(
this, registration_id, std::move(request_info), std::move(callback)));
}
void BackgroundFetchDataManager::MatchRequests(
const BackgroundFetchRegistrationId& registration_id,
std::unique_ptr<BackgroundFetchRequestMatchParams> match_params,
SettledFetchesCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
AddDatabaseTask(std::make_unique<background_fetch::MatchRequestsTask>(
this, registration_id, std::move(match_params), std::move(callback)));
}
void BackgroundFetchDataManager::MarkRegistrationForDeletion(
const BackgroundFetchRegistrationId& registration_id,
bool check_for_failure,
MarkRegistrationForDeletionCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
AddDatabaseTask(
std::make_unique<background_fetch::MarkRegistrationForDeletionTask>(
this, registration_id, check_for_failure, std::move(callback)));
}
void BackgroundFetchDataManager::DeleteRegistration(
const BackgroundFetchRegistrationId& registration_id,
HandleBackgroundFetchErrorCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
AddDatabaseTask(std::make_unique<background_fetch::DeleteRegistrationTask>(
this, registration_id.service_worker_registration_id(),
registration_id.storage_key(), registration_id.unique_id(),
std::move(callback)));
}
void BackgroundFetchDataManager::GetDeveloperIdsForServiceWorker(
int64_t service_worker_registration_id,
const blink::StorageKey& storage_key,
blink::mojom::BackgroundFetchService::GetDeveloperIdsCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
AddDatabaseTask(std::make_unique<background_fetch::GetDeveloperIdsTask>(
this, service_worker_registration_id, storage_key, std::move(callback)));
}
void BackgroundFetchDataManager::Shutdown() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
shutting_down_ = true;
}
void BackgroundFetchDataManager::AddDatabaseTask(
std::unique_ptr<background_fetch::DatabaseTask> task) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// If Shutdown was called don't add any new tasks.
if (shutting_down_)
return;
database_tasks_.push(std::move(task));
if (database_tasks_.size() == 1u)
database_tasks_.front()->Start();
}
void BackgroundFetchDataManager::OnTaskFinished(
background_fetch::DatabaseTask* task) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!database_tasks_.empty());
DCHECK_EQ(database_tasks_.front().get(), task);
database_tasks_.pop();
if (!database_tasks_.empty())
database_tasks_.front()->Start();
}
BackgroundFetchDataManager* BackgroundFetchDataManager::data_manager() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return this;
}
base::WeakPtr<background_fetch::DatabaseTaskHost>
BackgroundFetchDataManager::GetWeakPtr() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return weak_ptr_factory_.GetWeakPtr();
}
} // namespace content