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
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_BACKGROUND_SYNC_BACKGROUND_SYNC_CONTROLLER_IMPL_H_
#define COMPONENTS_BACKGROUND_SYNC_BACKGROUND_SYNC_CONTROLLER_IMPL_H_
#include "base/memory/raw_ptr.h"
#include "content/public/browser/background_sync_controller.h"
#include <stdint.h>
#include <set>
#include "base/time/time.h"
#include "build/build_config.h"
#include "components/background_sync/background_sync_delegate.h"
#include "components/background_sync/background_sync_metrics.h"
#include "components/content_settings/core/browser/content_settings_observer.h"
#include "components/keep_alive_registry/keep_alive_types.h"
#include "components/keep_alive_registry/scoped_keep_alive.h"
#include "components/keyed_service/core/keyed_service.h"
#include "content/public/browser/background_sync_registration.h"
#include "content/public/browser/browser_thread.h"
#include "third_party/blink/public/mojom/background_sync/background_sync.mojom-forward.h"
namespace content {
struct BackgroundSyncParameters;
class BrowserContext;
} // namespace content
namespace url {
class Origin;
} // namespace url
class BackgroundSyncControllerImpl : public content::BackgroundSyncController,
public KeyedService,
public content_settings::Observer {
public:
static const char kFieldTrialName[];
static const char kDisabledParameterName[];
static const char kKeepBrowserAwakeParameterName[];
static const char kSkipPermissionsCheckParameterName[];
static const char kMaxAttemptsParameterName[];
static const char kRelyOnAndroidNetworkDetection[];
static const char kMaxAttemptsWithNotificationPermissionParameterName[];
static const char kInitialRetryParameterName[];
static const char kRetryDelayFactorParameterName[];
static const char kMinSyncRecoveryTimeName[];
static const char kMaxSyncEventDurationName[];
static const char kMinPeriodicSyncEventsInterval[];
BackgroundSyncControllerImpl(
content::BrowserContext* browser_context,
std::unique_ptr<background_sync::BackgroundSyncDelegate> delegate);
BackgroundSyncControllerImpl(const BackgroundSyncControllerImpl&) = delete;
BackgroundSyncControllerImpl& operator=(const BackgroundSyncControllerImpl&) =
delete;
~BackgroundSyncControllerImpl() override;
// content::BackgroundSyncController overrides.
void GetParameterOverrides(
content::BackgroundSyncParameters* parameters) override;
void NotifyOneShotBackgroundSyncRegistered(const url::Origin& origin,
bool can_fire,
bool is_reregistered) override;
void NotifyPeriodicBackgroundSyncRegistered(const url::Origin& origin,
int min_interval,
bool is_reregistered) override;
void NotifyOneShotBackgroundSyncCompleted(
const url::Origin& origin,
blink::ServiceWorkerStatusCode status_code,
int num_attempts,
int max_attempts) override;
void NotifyPeriodicBackgroundSyncCompleted(
const url::Origin& origin,
blink::ServiceWorkerStatusCode status_code,
int num_attempts,
int max_attempts) override;
void ScheduleBrowserWakeUpWithDelay(
blink::mojom::BackgroundSyncType sync_type,
base::TimeDelta delay) override;
void CancelBrowserWakeup(blink::mojom::BackgroundSyncType sync_type) override;
base::TimeDelta GetNextEventDelay(
const content::BackgroundSyncRegistration& registration,
content::BackgroundSyncParameters* parameters,
base::TimeDelta time_till_soonest_scheduled_event_for_origin) override;
std::unique_ptr<BackgroundSyncEventKeepAlive>
CreateBackgroundSyncEventKeepAlive() override;
void NoteSuspendedPeriodicSyncOrigins(
std::set<url::Origin> suspended_origins) override;
void NoteRegisteredPeriodicSyncOrigins(
std::set<url::Origin> registered_origins) override;
void AddToTrackedOrigins(const url::Origin& origin) override;
void RemoveFromTrackedOrigins(const url::Origin& origin) override;
// content_settings::Observer overrides.
void OnContentSettingChanged(
const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern,
ContentSettingsTypeSet content_type_set) override;
bool IsOriginTracked(const url::Origin& origin) {
return periodic_sync_origins_.find(origin) != periodic_sync_origins_.end();
}
private:
// Once we've identified the minimum number of hours between each periodicsync
// event for an origin, every delay calculated for the origin should be a
// multiple of the same.
base::TimeDelta SnapToMaxOriginFrequency(int64_t min_interval,
int64_t min_gap_for_origin);
// Returns an updated delay for a Periodic Background Sync registration -- one
// that ensures the |min_gap_for_origin|.
base::TimeDelta ApplyMinGapForOrigin(
base::TimeDelta delay,
base::TimeDelta time_till_next_scheduled_event_for_origin,
base::TimeDelta min_gap_for_origin);
bool IsContentSettingBlocked(const url::Origin& origin);
// KeyedService implementation.
void Shutdown() override;
raw_ptr<content::BrowserContext> browser_context_;
std::unique_ptr<background_sync::BackgroundSyncDelegate> delegate_;
std::unique_ptr<BackgroundSyncMetrics> background_sync_metrics_;
std::set<url::Origin> periodic_sync_origins_;
};
#endif // COMPONENTS_BACKGROUND_SYNC_BACKGROUND_SYNC_CONTROLLER_IMPL_H_
|