forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground_sync_controller_impl.cc
414 lines (345 loc) · 14.8 KB
/
background_sync_controller_impl.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Copyright 2015 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 "components/background_sync/background_sync_controller_impl.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/keep_alive_registry/keep_alive_registry.h"
#include "components/variations/variations_associated_data.h"
#include "content/public/browser/background_sync_context.h"
#include "content/public/browser/background_sync_controller.h"
#include "content/public/browser/background_sync_parameters.h"
#include "content/public/browser/background_sync_registration.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/storage_partition.h"
#include "url/gurl.h"
#include "url/origin.h"
// static
const char BackgroundSyncControllerImpl::kFieldTrialName[] = "BackgroundSync";
const char BackgroundSyncControllerImpl::kDisabledParameterName[] = "disabled";
#if defined(OS_ANDROID)
const char BackgroundSyncControllerImpl::kRelyOnAndroidNetworkDetection[] =
"rely_on_android_network_detection";
#endif
const char BackgroundSyncControllerImpl::kKeepBrowserAwakeParameterName[] =
"keep_browser_awake_till_events_complete";
const char BackgroundSyncControllerImpl::kSkipPermissionsCheckParameterName[] =
"skip_permissions_check_for_testing";
const char BackgroundSyncControllerImpl::kMaxAttemptsParameterName[] =
"max_sync_attempts";
const char BackgroundSyncControllerImpl::
kMaxAttemptsWithNotificationPermissionParameterName[] =
"max_sync_attempts_with_notification_permission";
const char BackgroundSyncControllerImpl::kInitialRetryParameterName[] =
"initial_retry_delay_sec";
const char BackgroundSyncControllerImpl::kRetryDelayFactorParameterName[] =
"retry_delay_factor";
const char BackgroundSyncControllerImpl::kMinSyncRecoveryTimeName[] =
"min_recovery_time_sec";
const char BackgroundSyncControllerImpl::kMaxSyncEventDurationName[] =
"max_sync_event_duration_sec";
const char BackgroundSyncControllerImpl::kMinPeriodicSyncEventsInterval[] =
"min_periodic_sync_events_interval_sec";
BackgroundSyncControllerImpl::BackgroundSyncControllerImpl(
content::BrowserContext* browser_context,
std::unique_ptr<background_sync::BackgroundSyncDelegate> delegate)
: browser_context_(browser_context), delegate_(std::move(delegate)) {
DCHECK(browser_context_);
DCHECK(delegate_);
background_sync_metrics_ =
std::make_unique<BackgroundSyncMetrics>(delegate_.get());
delegate_->GetHostContentSettingsMap()->AddObserver(this);
}
BackgroundSyncControllerImpl::~BackgroundSyncControllerImpl() = default;
void BackgroundSyncControllerImpl::OnContentSettingChanged(
const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern,
ContentSettingsType content_type) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (content_type != ContentSettingsType::BACKGROUND_SYNC &&
content_type != ContentSettingsType::PERIODIC_BACKGROUND_SYNC) {
return;
}
std::vector<url::Origin> affected_origins;
for (const auto& origin : periodic_sync_origins_) {
if (!IsContentSettingBlocked(origin))
continue;
auto* storage_partition =
content::BrowserContext::GetStoragePartitionForSite(
browser_context_, origin.GetURL(), /* can_create= */ false);
if (!storage_partition)
continue;
auto* background_sync_context =
storage_partition->GetBackgroundSyncContext();
if (!background_sync_context)
continue;
background_sync_context->UnregisterPeriodicSyncForOrigin(origin);
affected_origins.push_back(origin);
}
// Stop tracking affected origins.
for (const auto& origin : affected_origins) {
periodic_sync_origins_.erase(origin);
}
}
void BackgroundSyncControllerImpl::GetParameterOverrides(
content::BackgroundSyncParameters* parameters) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
#if defined(OS_ANDROID)
if (delegate_->ShouldDisableBackgroundSync())
parameters->disable = true;
#endif
std::map<std::string, std::string> field_params;
if (!variations::GetVariationParams(kFieldTrialName, &field_params))
return;
if (base::LowerCaseEqualsASCII(field_params[kDisabledParameterName],
"true")) {
parameters->disable = true;
}
if (base::LowerCaseEqualsASCII(field_params[kKeepBrowserAwakeParameterName],
"true")) {
parameters->keep_browser_awake_till_events_complete = true;
}
if (base::LowerCaseEqualsASCII(
field_params[kSkipPermissionsCheckParameterName], "true")) {
parameters->skip_permissions_check_for_testing = true;
}
if (base::Contains(field_params,
kMaxAttemptsWithNotificationPermissionParameterName)) {
int max_attempts;
if (base::StringToInt(
field_params[kMaxAttemptsWithNotificationPermissionParameterName],
&max_attempts)) {
parameters->max_sync_attempts_with_notification_permission = max_attempts;
}
}
if (base::Contains(field_params, kMaxAttemptsParameterName)) {
int max_attempts;
if (base::StringToInt(field_params[kMaxAttemptsParameterName],
&max_attempts)) {
parameters->max_sync_attempts = max_attempts;
}
}
if (base::Contains(field_params, kInitialRetryParameterName)) {
int initial_retry_delay_sec;
if (base::StringToInt(field_params[kInitialRetryParameterName],
&initial_retry_delay_sec)) {
parameters->initial_retry_delay =
base::TimeDelta::FromSeconds(initial_retry_delay_sec);
}
}
if (base::Contains(field_params, kRetryDelayFactorParameterName)) {
int retry_delay_factor;
if (base::StringToInt(field_params[kRetryDelayFactorParameterName],
&retry_delay_factor)) {
parameters->retry_delay_factor = retry_delay_factor;
}
}
if (base::Contains(field_params, kMinSyncRecoveryTimeName)) {
int min_sync_recovery_time_sec;
if (base::StringToInt(field_params[kMinSyncRecoveryTimeName],
&min_sync_recovery_time_sec)) {
parameters->min_sync_recovery_time =
base::TimeDelta::FromSeconds(min_sync_recovery_time_sec);
}
}
if (base::Contains(field_params, kMaxSyncEventDurationName)) {
int max_sync_event_duration_sec;
if (base::StringToInt(field_params[kMaxSyncEventDurationName],
&max_sync_event_duration_sec)) {
parameters->max_sync_event_duration =
base::TimeDelta::FromSeconds(max_sync_event_duration_sec);
}
}
if (base::Contains(field_params, kMinPeriodicSyncEventsInterval)) {
int min_periodic_sync_events_interval_sec;
if (base::StringToInt(field_params[kMinPeriodicSyncEventsInterval],
&min_periodic_sync_events_interval_sec)) {
parameters->min_periodic_sync_events_interval =
base::TimeDelta::FromSeconds(min_periodic_sync_events_interval_sec);
}
}
#if defined(OS_ANDROID)
// Check if the delegate explicitly disabled this feature.
if (delegate_->ShouldDisableAndroidNetworkDetection()) {
parameters->rely_on_android_network_detection = false;
} else if (base::Contains(field_params, kRelyOnAndroidNetworkDetection)) {
if (base::LowerCaseEqualsASCII(field_params[kRelyOnAndroidNetworkDetection],
"true")) {
parameters->rely_on_android_network_detection = true;
}
}
#endif
return;
}
void BackgroundSyncControllerImpl::NotifyOneShotBackgroundSyncRegistered(
const url::Origin& origin,
bool can_fire,
bool is_reregistered) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
background_sync_metrics_->MaybeRecordOneShotSyncRegistrationEvent(
origin, can_fire, is_reregistered);
}
void BackgroundSyncControllerImpl::NotifyPeriodicBackgroundSyncRegistered(
const url::Origin& origin,
int min_interval,
bool is_reregistered) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
background_sync_metrics_->MaybeRecordPeriodicSyncRegistrationEvent(
origin, min_interval, is_reregistered);
}
void BackgroundSyncControllerImpl::NotifyOneShotBackgroundSyncCompleted(
const url::Origin& origin,
blink::ServiceWorkerStatusCode status_code,
int num_attempts,
int max_attempts) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
background_sync_metrics_->MaybeRecordOneShotSyncCompletionEvent(
origin, status_code, num_attempts, max_attempts);
}
void BackgroundSyncControllerImpl::NotifyPeriodicBackgroundSyncCompleted(
const url::Origin& origin,
blink::ServiceWorkerStatusCode status_code,
int num_attempts,
int max_attempts) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
background_sync_metrics_->MaybeRecordPeriodicSyncEventCompletion(
origin, status_code, num_attempts, max_attempts);
}
void BackgroundSyncControllerImpl::ScheduleBrowserWakeUpWithDelay(
blink::mojom::BackgroundSyncType sync_type,
base::TimeDelta delay) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (delegate_->IsProfileOffTheRecord())
return;
#if defined(OS_ANDROID)
delegate_->ScheduleBrowserWakeUpWithDelay(sync_type, delay);
#endif
}
void BackgroundSyncControllerImpl::CancelBrowserWakeup(
blink::mojom::BackgroundSyncType sync_type) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (delegate_->IsProfileOffTheRecord())
return;
#if defined(OS_ANDROID)
delegate_->CancelBrowserWakeup(sync_type);
#endif
}
base::TimeDelta BackgroundSyncControllerImpl::SnapToMaxOriginFrequency(
int64_t min_interval,
int64_t min_gap_for_origin) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK_GE(min_gap_for_origin, 0);
DCHECK_GE(min_interval, 0);
if (min_interval < min_gap_for_origin)
return base::TimeDelta::FromMilliseconds(min_gap_for_origin);
if (min_interval % min_gap_for_origin == 0)
return base::TimeDelta::FromMilliseconds(min_interval);
return base::TimeDelta::FromMilliseconds(
(min_interval / min_gap_for_origin + 1) * min_gap_for_origin);
}
base::TimeDelta BackgroundSyncControllerImpl::ApplyMinGapForOrigin(
base::TimeDelta delay,
base::TimeDelta time_till_next_scheduled_event_for_origin,
base::TimeDelta min_gap_for_origin) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (time_till_next_scheduled_event_for_origin.is_max())
return delay;
if (delay <= time_till_next_scheduled_event_for_origin - min_gap_for_origin)
return delay;
if (delay <= time_till_next_scheduled_event_for_origin)
return time_till_next_scheduled_event_for_origin;
if (delay <= time_till_next_scheduled_event_for_origin + min_gap_for_origin)
return time_till_next_scheduled_event_for_origin + min_gap_for_origin;
return delay;
}
bool BackgroundSyncControllerImpl::IsContentSettingBlocked(
const url::Origin& origin) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
auto* host_content_settings_map = delegate_->GetHostContentSettingsMap();
DCHECK(host_content_settings_map);
auto url = origin.GetURL();
return CONTENT_SETTING_ALLOW != host_content_settings_map->GetContentSetting(
/* primary_url= */ url,
/* secondary_url= */ url,
ContentSettingsType::BACKGROUND_SYNC);
}
void BackgroundSyncControllerImpl::Shutdown() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
delegate_->GetHostContentSettingsMap()->RemoveObserver(this);
delegate_->Shutdown();
}
base::TimeDelta BackgroundSyncControllerImpl::GetNextEventDelay(
const content::BackgroundSyncRegistration& registration,
content::BackgroundSyncParameters* parameters,
base::TimeDelta time_till_soonest_scheduled_event_for_origin) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(parameters);
int num_attempts = registration.num_attempts();
if (!num_attempts) {
// First attempt.
switch (registration.sync_type()) {
case blink::mojom::BackgroundSyncType::ONE_SHOT:
return base::TimeDelta();
case blink::mojom::BackgroundSyncType::PERIODIC:
int site_engagement_factor =
delegate_->GetSiteEngagementPenalty(registration.origin().GetURL());
if (!site_engagement_factor)
return base::TimeDelta::Max();
int64_t effective_gap_ms =
site_engagement_factor *
parameters->min_periodic_sync_events_interval.InMilliseconds();
return ApplyMinGapForOrigin(
SnapToMaxOriginFrequency(registration.options()->min_interval,
effective_gap_ms),
time_till_soonest_scheduled_event_for_origin,
parameters->min_periodic_sync_events_interval);
}
}
// After a sync event has been fired.
DCHECK_LT(num_attempts, parameters->max_sync_attempts);
return parameters->initial_retry_delay *
pow(parameters->retry_delay_factor, num_attempts - 1);
}
std::unique_ptr<content::BackgroundSyncController::BackgroundSyncEventKeepAlive>
BackgroundSyncControllerImpl::CreateBackgroundSyncEventKeepAlive() {
#if !defined(OS_ANDROID)
if (!KeepAliveRegistry::GetInstance()->IsShuttingDown())
return std::make_unique<BackgroundSyncEventKeepAliveImpl>();
#endif
return nullptr;
}
#if !defined(OS_ANDROID)
BackgroundSyncControllerImpl::BackgroundSyncEventKeepAliveImpl::
BackgroundSyncEventKeepAliveImpl() {
keepalive_ = std::unique_ptr<ScopedKeepAlive,
content::BrowserThread::DeleteOnUIThread>(
new ScopedKeepAlive(KeepAliveOrigin::BACKGROUND_SYNC,
KeepAliveRestartOption::DISABLED));
}
BackgroundSyncControllerImpl::BackgroundSyncEventKeepAliveImpl::
~BackgroundSyncEventKeepAliveImpl() = default;
#endif
void BackgroundSyncControllerImpl::NoteSuspendedPeriodicSyncOrigins(
std::set<url::Origin> suspended_origins) {
delegate_->NoteSuspendedPeriodicSyncOrigins(std::move(suspended_origins));
}
void BackgroundSyncControllerImpl::NoteRegisteredPeriodicSyncOrigins(
std::set<url::Origin> registered_origins) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
for (auto& origin : registered_origins)
periodic_sync_origins_.insert(std::move(origin));
}
void BackgroundSyncControllerImpl::AddToTrackedOrigins(
const url::Origin& origin) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
periodic_sync_origins_.insert(origin);
}
void BackgroundSyncControllerImpl::RemoveFromTrackedOrigins(
const url::Origin& origin) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
periodic_sync_origins_.erase(origin);
}