background_sync_controller_impl.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_BACKGROUND_SYNC_BACKGROUND_SYNC_CONTROLLER_IMPL_H_
  5. #define COMPONENTS_BACKGROUND_SYNC_BACKGROUND_SYNC_CONTROLLER_IMPL_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "content/public/browser/background_sync_controller.h"
  8. #include <stdint.h>
  9. #include <set>
  10. #include "base/time/time.h"
  11. #include "build/build_config.h"
  12. #include "components/background_sync/background_sync_delegate.h"
  13. #include "components/background_sync/background_sync_metrics.h"
  14. #include "components/content_settings/core/browser/content_settings_observer.h"
  15. #include "components/keep_alive_registry/keep_alive_types.h"
  16. #include "components/keep_alive_registry/scoped_keep_alive.h"
  17. #include "components/keyed_service/core/keyed_service.h"
  18. #include "content/public/browser/background_sync_registration.h"
  19. #include "content/public/browser/browser_thread.h"
  20. #include "third_party/blink/public/mojom/background_sync/background_sync.mojom-forward.h"
  21. namespace content {
  22. struct BackgroundSyncParameters;
  23. class BrowserContext;
  24. } // namespace content
  25. namespace url {
  26. class Origin;
  27. } // namespace url
  28. class BackgroundSyncControllerImpl : public content::BackgroundSyncController,
  29. public KeyedService,
  30. public content_settings::Observer {
  31. public:
  32. static const char kFieldTrialName[];
  33. static const char kDisabledParameterName[];
  34. static const char kKeepBrowserAwakeParameterName[];
  35. static const char kSkipPermissionsCheckParameterName[];
  36. static const char kMaxAttemptsParameterName[];
  37. static const char kRelyOnAndroidNetworkDetection[];
  38. static const char kMaxAttemptsWithNotificationPermissionParameterName[];
  39. static const char kInitialRetryParameterName[];
  40. static const char kRetryDelayFactorParameterName[];
  41. static const char kMinSyncRecoveryTimeName[];
  42. static const char kMaxSyncEventDurationName[];
  43. static const char kMinPeriodicSyncEventsInterval[];
  44. BackgroundSyncControllerImpl(
  45. content::BrowserContext* browser_context,
  46. std::unique_ptr<background_sync::BackgroundSyncDelegate> delegate);
  47. BackgroundSyncControllerImpl(const BackgroundSyncControllerImpl&) = delete;
  48. BackgroundSyncControllerImpl& operator=(const BackgroundSyncControllerImpl&) =
  49. delete;
  50. ~BackgroundSyncControllerImpl() override;
  51. // content::BackgroundSyncController overrides.
  52. void GetParameterOverrides(
  53. content::BackgroundSyncParameters* parameters) override;
  54. void NotifyOneShotBackgroundSyncRegistered(const url::Origin& origin,
  55. bool can_fire,
  56. bool is_reregistered) override;
  57. void NotifyPeriodicBackgroundSyncRegistered(const url::Origin& origin,
  58. int min_interval,
  59. bool is_reregistered) override;
  60. void NotifyOneShotBackgroundSyncCompleted(
  61. const url::Origin& origin,
  62. blink::ServiceWorkerStatusCode status_code,
  63. int num_attempts,
  64. int max_attempts) override;
  65. void NotifyPeriodicBackgroundSyncCompleted(
  66. const url::Origin& origin,
  67. blink::ServiceWorkerStatusCode status_code,
  68. int num_attempts,
  69. int max_attempts) override;
  70. void ScheduleBrowserWakeUpWithDelay(
  71. blink::mojom::BackgroundSyncType sync_type,
  72. base::TimeDelta delay) override;
  73. void CancelBrowserWakeup(blink::mojom::BackgroundSyncType sync_type) override;
  74. base::TimeDelta GetNextEventDelay(
  75. const content::BackgroundSyncRegistration& registration,
  76. content::BackgroundSyncParameters* parameters,
  77. base::TimeDelta time_till_soonest_scheduled_event_for_origin) override;
  78. std::unique_ptr<BackgroundSyncEventKeepAlive>
  79. CreateBackgroundSyncEventKeepAlive() override;
  80. void NoteSuspendedPeriodicSyncOrigins(
  81. std::set<url::Origin> suspended_origins) override;
  82. void NoteRegisteredPeriodicSyncOrigins(
  83. std::set<url::Origin> registered_origins) override;
  84. void AddToTrackedOrigins(const url::Origin& origin) override;
  85. void RemoveFromTrackedOrigins(const url::Origin& origin) override;
  86. // content_settings::Observer overrides.
  87. void OnContentSettingChanged(
  88. const ContentSettingsPattern& primary_pattern,
  89. const ContentSettingsPattern& secondary_pattern,
  90. ContentSettingsTypeSet content_type_set) override;
  91. bool IsOriginTracked(const url::Origin& origin) {
  92. return periodic_sync_origins_.find(origin) != periodic_sync_origins_.end();
  93. }
  94. private:
  95. // Once we've identified the minimum number of hours between each periodicsync
  96. // event for an origin, every delay calculated for the origin should be a
  97. // multiple of the same.
  98. base::TimeDelta SnapToMaxOriginFrequency(int64_t min_interval,
  99. int64_t min_gap_for_origin);
  100. // Returns an updated delay for a Periodic Background Sync registration -- one
  101. // that ensures the |min_gap_for_origin|.
  102. base::TimeDelta ApplyMinGapForOrigin(
  103. base::TimeDelta delay,
  104. base::TimeDelta time_till_next_scheduled_event_for_origin,
  105. base::TimeDelta min_gap_for_origin);
  106. bool IsContentSettingBlocked(const url::Origin& origin);
  107. // KeyedService implementation.
  108. void Shutdown() override;
  109. raw_ptr<content::BrowserContext> browser_context_;
  110. std::unique_ptr<background_sync::BackgroundSyncDelegate> delegate_;
  111. std::unique_ptr<BackgroundSyncMetrics> background_sync_metrics_;
  112. std::set<url::Origin> periodic_sync_origins_;
  113. };
  114. #endif // COMPONENTS_BACKGROUND_SYNC_BACKGROUND_SYNC_CONTROLLER_IMPL_H_