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
|
// 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_context.h"
#include <utility>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/observer_list.h"
#include "content/browser/background_fetch/background_fetch_data_manager.h"
#include "content/browser/background_fetch/background_fetch_job_controller.h"
#include "content/browser/background_fetch/background_fetch_metrics.h"
#include "content/browser/background_fetch/background_fetch_registration_id.h"
#include "content/browser/background_fetch/background_fetch_registration_notifier.h"
#include "content/browser/background_fetch/background_fetch_registration_service_impl.h"
#include "content/browser/background_fetch/background_fetch_request_match_params.h"
#include "content/browser/background_fetch/background_fetch_scheduler.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/browser/storage_partition_impl.h"
#include "content/common/background_fetch/background_fetch_types.h"
#include "content/public/browser/background_fetch_delegate.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"
#include "storage/browser/blob/blob_data_handle.h"
#include "storage/browser/quota/quota_manager_proxy.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
namespace content {
using FailureReason = blink::mojom::BackgroundFetchFailureReason;
BackgroundFetchContext::BackgroundFetchContext(
base::WeakPtr<StoragePartitionImpl> storage_partition,
const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context,
scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy,
DevToolsBackgroundServicesContextImpl& devtools_context)
: service_worker_context_(service_worker_context),
devtools_context_(&devtools_context),
registration_notifier_(
std::make_unique<BackgroundFetchRegistrationNotifier>()),
delegate_proxy_(storage_partition) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(service_worker_context_);
data_manager_ = std::make_unique<BackgroundFetchDataManager>(
storage_partition, service_worker_context,
std::move(quota_manager_proxy));
scheduler_ = std::make_unique<BackgroundFetchScheduler>(
this, data_manager_.get(), registration_notifier_.get(), &delegate_proxy_,
*devtools_context_, service_worker_context_);
}
BackgroundFetchContext::~BackgroundFetchContext() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
service_worker_context_->RemoveObserver(scheduler_.get());
data_manager_->RemoveObserver(scheduler_.get());
}
void BackgroundFetchContext::Initialize() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
service_worker_context_->AddObserver(scheduler_.get());
data_manager_->AddObserver(scheduler_.get());
data_manager_->Initialize();
data_manager_->GetInitializationData(
base::BindOnce(&BackgroundFetchContext::DidGetInitializationData,
weak_factory_.GetWeakPtr()));
}
void BackgroundFetchContext::DidGetInitializationData(
blink::mojom::BackgroundFetchError error,
std::vector<background_fetch::BackgroundFetchInitializationData>
initialization_data) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (error != blink::mojom::BackgroundFetchError::NONE)
return;
for (auto& data : initialization_data) {
for (auto& observer : data_manager_->observers()) {
observer.OnRegistrationLoadedAtStartup(
data.registration_id, *data.registration_data, data.options.Clone(),
data.icon, data.num_completed_requests, data.num_requests,
data.active_fetch_requests, data.isolation_info);
}
}
}
void BackgroundFetchContext::GetRegistration(
int64_t service_worker_registration_id,
const blink::StorageKey& storage_key,
const std::string& developer_id,
blink::mojom::BackgroundFetchService::GetRegistrationCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
data_manager_->GetRegistration(
service_worker_registration_id, storage_key, developer_id,
base::BindOnce(&BackgroundFetchContext::DidGetRegistration,
weak_factory_.GetWeakPtr(), std::move(callback)));
}
void BackgroundFetchContext::GetDeveloperIdsForServiceWorker(
int64_t service_worker_registration_id,
const blink::StorageKey& storage_key,
blink::mojom::BackgroundFetchService::GetDeveloperIdsCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
data_manager_->GetDeveloperIdsForServiceWorker(
service_worker_registration_id, storage_key, std::move(callback));
}
void BackgroundFetchContext::DidGetRegistration(
blink::mojom::BackgroundFetchService::GetRegistrationCallback callback,
blink::mojom::BackgroundFetchError error,
BackgroundFetchRegistrationId registration_id,
blink::mojom::BackgroundFetchRegistrationDataPtr registration_data) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (error != blink::mojom::BackgroundFetchError::NONE) {
std::move(callback).Run(
error, nullptr /* blink::mojom::BackgroundFetchRegistration */);
return;
}
for (auto& observer : data_manager_->observers())
observer.OnRegistrationQueried(registration_id, registration_data.get());
auto registration = blink::mojom::BackgroundFetchRegistration::New(
std::move(registration_data),
BackgroundFetchRegistrationServiceImpl::CreateInterfaceInfo(
std::move(registration_id), weak_factory_.GetWeakPtr()));
std::move(callback).Run(error, std::move(registration));
}
void BackgroundFetchContext::StartFetch(
const BackgroundFetchRegistrationId& registration_id,
std::vector<blink::mojom::FetchAPIRequestPtr> requests,
blink::mojom::BackgroundFetchOptionsPtr options,
const SkBitmap& icon,
blink::mojom::BackgroundFetchUkmDataPtr ukm_data,
RenderProcessHost* rph,
RenderFrameHostImpl* rfh,
const net::IsolationInfo& isolation_info,
blink::mojom::BackgroundFetchService::FetchCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// |registration_id| should be unique even if developer id has been
// duplicated, because the caller of this function generates a new unique_id
// every time, which is what BackgroundFetchRegistrationId's comparison
// operator uses.
DCHECK_EQ(0u, fetch_callbacks_.count(registration_id));
fetch_callbacks_[registration_id] = std::move(callback);
auto rfh_id = rfh ? rfh->GetGlobalId() : GlobalRenderFrameHostId();
delegate_proxy_.GetPermissionForOrigin(
registration_id.storage_key().origin(), rph, rfh,
base::BindOnce(&BackgroundFetchContext::DidGetPermission,
weak_factory_.GetWeakPtr(), registration_id,
std::move(requests), std::move(options), icon,
std::move(ukm_data), rfh_id, isolation_info));
}
void BackgroundFetchContext::DidGetPermission(
const BackgroundFetchRegistrationId& registration_id,
std::vector<blink::mojom::FetchAPIRequestPtr> requests,
blink::mojom::BackgroundFetchOptionsPtr options,
const SkBitmap& icon,
blink::mojom::BackgroundFetchUkmDataPtr ukm_data,
const GlobalRenderFrameHostId& rfh_id,
const net::IsolationInfo& isolation_info,
BackgroundFetchPermission permission) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
background_fetch::RecordBackgroundFetchUkmEvent(
registration_id.storage_key(), requests.size(), options.Clone(), icon,
std::move(ukm_data), RenderFrameHostImpl::FromID(rfh_id), permission);
if (permission != BackgroundFetchPermission::BLOCKED) {
data_manager_->CreateRegistration(
registration_id, std::move(requests), std::move(options), icon,
/* start_paused= */ permission == BackgroundFetchPermission::ASK,
isolation_info,
base::BindOnce(&BackgroundFetchContext::DidCreateRegistration,
weak_factory_.GetWeakPtr(), registration_id));
return;
}
// No permission, the fetch should be rejected.
std::move(fetch_callbacks_[registration_id])
.Run(blink::mojom::BackgroundFetchError::PERMISSION_DENIED, nullptr);
fetch_callbacks_.erase(registration_id);
}
void BackgroundFetchContext::GetIconDisplaySize(
blink::mojom::BackgroundFetchService::GetIconDisplaySizeCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
delegate_proxy_.GetIconDisplaySize(std::move(callback));
}
void BackgroundFetchContext::DidCreateRegistration(
const BackgroundFetchRegistrationId& registration_id,
blink::mojom::BackgroundFetchError error,
blink::mojom::BackgroundFetchRegistrationDataPtr registration_data) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto iter = fetch_callbacks_.find(registration_id);
// The fetch might have been abandoned already if the Service Worker was
// unregistered or corrupted while registration was in progress.
if (iter == fetch_callbacks_.end())
return;
if (error == blink::mojom::BackgroundFetchError::NONE) {
auto registration = blink::mojom::BackgroundFetchRegistration::New(
std::move(registration_data),
BackgroundFetchRegistrationServiceImpl::CreateInterfaceInfo(
registration_id, weak_factory_.GetWeakPtr()));
std::move(iter->second).Run(error, std::move(registration));
} else {
std::move(iter->second).Run(error, /* registration= */ nullptr);
}
fetch_callbacks_.erase(registration_id);
}
void BackgroundFetchContext::AddRegistrationObserver(
const std::string& unique_id,
mojo::PendingRemote<blink::mojom::BackgroundFetchRegistrationObserver>
observer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
registration_notifier_->AddObserver(unique_id, std::move(observer));
}
void BackgroundFetchContext::UpdateUI(
const BackgroundFetchRegistrationId& registration_id,
const std::optional<std::string>& title,
const std::optional<SkBitmap>& icon,
blink::mojom::BackgroundFetchRegistrationService::UpdateUICallback
callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
delegate_proxy_.UpdateUI(registration_id.unique_id(), title, icon,
std::move(callback));
}
base::WeakPtr<BackgroundFetchContext> BackgroundFetchContext::GetWeakPtr() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
return weak_factory_.GetWeakPtr();
}
void BackgroundFetchContext::Abort(
const BackgroundFetchRegistrationId& registration_id,
blink::mojom::BackgroundFetchRegistrationService::AbortCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
scheduler_->Abort(registration_id, FailureReason::CANCELLED_BY_DEVELOPER,
std::move(callback));
}
void BackgroundFetchContext::MatchRequests(
const BackgroundFetchRegistrationId& registration_id,
std::unique_ptr<BackgroundFetchRequestMatchParams> match_params,
blink::mojom::BackgroundFetchRegistrationService::MatchRequestsCallback
callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
data_manager_->MatchRequests(
registration_id, std::move(match_params),
base::BindOnce(&BackgroundFetchContext::DidGetMatchingRequests,
weak_factory_.GetWeakPtr(), registration_id.unique_id(),
std::move(callback)));
}
void BackgroundFetchContext::DidGetMatchingRequests(
const std::string& unique_id,
blink::mojom::BackgroundFetchRegistrationService::MatchRequestsCallback
callback,
blink::mojom::BackgroundFetchError error,
std::vector<blink::mojom::BackgroundFetchSettledFetchPtr> settled_fetches) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (error != blink::mojom::BackgroundFetchError::NONE)
DCHECK(settled_fetches.empty());
// TODO(crbug.com/40579759): We don't need to call this for requests that're
// complete.
// AddObservedUrl() is a no-op in those cases, but we can skip calling it.
for (const auto& fetch : settled_fetches)
registration_notifier_->AddObservedUrl(unique_id, fetch->request->url);
std::move(callback).Run(std::move(settled_fetches));
}
void BackgroundFetchContext::Shutdown() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
data_manager_->Shutdown();
scheduler_->Shutdown();
devtools_context_ = nullptr;
}
void BackgroundFetchContext::SetDataManagerForTesting(
std::unique_ptr<BackgroundFetchDataManager> data_manager) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(data_manager);
CHECK(devtools_context_);
data_manager_ = std::move(data_manager);
scheduler_ = std::make_unique<BackgroundFetchScheduler>(
this, data_manager_.get(), registration_notifier_.get(), &delegate_proxy_,
*devtools_context_, service_worker_context_);
}
} // namespace content
|