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.
5 #ifndef CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_
6 #define CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_
13 #include "base/callback_forward.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "content/browser/background_sync/background_sync.pb.h"
17 #include "content/browser/background_sync/background_sync_registration.h"
18 #include "content/browser/background_sync/background_sync_status.h"
19 #include "content/browser/cache_storage/cache_storage_scheduler.h"
20 #include "content/browser/service_worker/service_worker_context_observer.h"
21 #include "content/browser/service_worker/service_worker_storage.h"
22 #include "content/common/content_export.h"
23 #include "content/common/service_worker/service_worker_status_code.h"
28 class BackgroundSyncNetworkObserver
;
29 class BackgroundSyncPowerObserver
;
30 class ServiceWorkerContextWrapper
;
32 // BackgroundSyncManager manages and stores the set of background sync
33 // registrations across all registered service workers for a profile.
34 // Registrations are stored along with their associated Service Worker
35 // registration in ServiceWorkerStorage. If the ServiceWorker is unregistered,
36 // the sync registrations are removed. This class expects to be run on the IO
37 // thread. The asynchronous methods are executed sequentially.
39 // TODO(jkarlin): Check permissions when registering, scheduling, and firing
40 // background sync. In the meantime, --enable-service-worker-sync is required to
42 // TODO(jkarlin): Unregister syncs when permission is revoked.
43 // TODO(jkarlin): Create a background sync scheduler to actually run the
45 // TODO(jkarlin): Keep the browser alive if "Let Google Chrome Run in the
46 // Background" is true and a sync is registered.
47 class CONTENT_EXPORT BackgroundSyncManager
48 : NON_EXPORTED_BASE(public ServiceWorkerContextObserver
) {
50 using StatusCallback
= base::Callback
<void(BackgroundSyncStatus
)>;
51 using StatusAndRegistrationCallback
=
52 base::Callback
<void(BackgroundSyncStatus
,
53 const BackgroundSyncRegistration
&)>;
54 using StatusAndRegistrationsCallback
=
55 base::Callback
<void(BackgroundSyncStatus
,
56 const std::vector
<BackgroundSyncRegistration
>&)>;
58 static scoped_ptr
<BackgroundSyncManager
> Create(
59 const scoped_refptr
<ServiceWorkerContextWrapper
>& service_worker_context
);
60 ~BackgroundSyncManager() override
;
62 // Stores the given background sync registration and adds it to the scheduling
63 // queue. It will overwrite an existing registration with the same tag and
64 // periodicity unless they're identical (save for the id). Calls |callback|
65 // with BACKGROUND_SYNC_STATUS_OK and the accepted registration on success.
66 // The accepted registration will have a unique id. It may also have altered
67 // parameters if the user or UA chose different parameters than those
69 void Register(int64 sw_registration_id
,
70 const BackgroundSyncRegistrationOptions
& options
,
71 bool requested_from_service_worker
,
72 const StatusAndRegistrationCallback
& callback
);
74 // Removes the background sync with tag |sync_registration_tag|, periodicity
75 // |periodicity|, and id |sync_registration_id|. Calls |callback| with
76 // BACKGROUND_SYNC_STATUS_NOT_FOUND if no match is found. Calls |callback|
77 // with BACKGROUND_SYNC_STATUS_OK on success.
79 int64 sw_registration_id
,
80 const std::string
& sync_registration_tag
,
81 SyncPeriodicity periodicity
,
82 BackgroundSyncRegistration::RegistrationId sync_registration_id
,
83 const StatusCallback
& callback
);
85 // Finds the background sync registration associated with
86 // |sw_registration_id| with periodicity |periodicity|. Calls
87 // |callback| with BACKGROUND_SYNC_STATUS_NOT_FOUND if it doesn't exist. Calls
88 // |callback| with BACKGROUND_SYNC_STATUS_OK on success.
89 void GetRegistration(int64 sw_registration_id
,
90 const std::string
& sync_registration_tag
,
91 SyncPeriodicity periodicity
,
92 const StatusAndRegistrationCallback
& callback
);
94 void GetRegistrations(int64 sw_registration_id
,
95 SyncPeriodicity periodicity
,
96 const StatusAndRegistrationsCallback
& callback
);
98 // ServiceWorkerContextObserver overrides.
99 void OnRegistrationDeleted(int64 registration_id
,
100 const GURL
& pattern
) override
;
101 void OnStorageWiped() override
;
104 explicit BackgroundSyncManager(
105 const scoped_refptr
<ServiceWorkerContextWrapper
>& context
);
107 // Init must be called before any public member function. Only call it once.
110 // The following methods are virtual for testing.
111 virtual void StoreDataInBackend(
112 int64 sw_registration_id
,
114 const std::string
& backend_key
,
115 const std::string
& data
,
116 const ServiceWorkerStorage::StatusCallback
& callback
);
117 virtual void GetDataFromBackend(
118 const std::string
& backend_key
,
119 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback
&
121 virtual void FireOneShotSync(
122 const BackgroundSyncRegistration
& registration
,
123 const scoped_refptr
<ServiceWorkerVersion
>& active_version
,
124 const ServiceWorkerVersion::StatusCallback
& callback
);
127 class RegistrationKey
{
129 explicit RegistrationKey(const BackgroundSyncRegistration
& registration
);
130 explicit RegistrationKey(const BackgroundSyncRegistrationOptions
& options
);
131 RegistrationKey(const std::string
& tag
, SyncPeriodicity periodicity
);
132 RegistrationKey(const RegistrationKey
& other
) = default;
133 RegistrationKey
& operator=(const RegistrationKey
& other
) = default;
135 bool operator<(const RegistrationKey
& rhs
) const {
136 return value_
< rhs
.value_
;
143 struct BackgroundSyncRegistrations
{
144 using RegistrationMap
=
145 std::map
<RegistrationKey
, BackgroundSyncRegistration
>;
147 BackgroundSyncRegistrations();
148 ~BackgroundSyncRegistrations();
150 RegistrationMap registration_map
;
151 BackgroundSyncRegistration::RegistrationId next_id
;
155 using PermissionStatusCallback
= base::Callback
<void(bool)>;
156 using SWIdToRegistrationsMap
= std::map
<int64
, BackgroundSyncRegistrations
>;
158 // Disable the manager. Already queued operations will abort once they start
159 // to run (in their impl methods). Future operations will not queue. Any
160 // registrations are cleared from memory and the backend (if it's still
161 // functioning). The manager will reenable itself once it receives the
162 // OnStorageWiped message or on browser restart.
163 void DisableAndClearManager(const base::Closure
& callback
);
164 void DisableAndClearDidGetRegistrations(
165 const base::Closure
& callback
,
166 const std::vector
<std::pair
<int64
, std::string
>>& user_data
,
167 ServiceWorkerStatusCode status
);
168 void DisableAndClearManagerClearedOne(const base::Closure
& barrier_closure
,
169 ServiceWorkerStatusCode status
);
171 // Returns the existing registration in |existing_registration| if it is not
173 BackgroundSyncRegistration
* LookupRegistration(
174 int64 sw_registration_id
,
175 const RegistrationKey
& registration_key
);
177 // Store all registrations for a given |sw_registration_id|.
178 void StoreRegistrations(int64 sw_registration_id
,
179 const ServiceWorkerStorage::StatusCallback
& callback
);
181 // Removes the registration if it is in the map.
182 void RemoveRegistrationFromMap(int64 sw_registration_id
,
183 const RegistrationKey
& registration_key
);
185 void AddRegistrationToMap(
186 int64 sw_registration_id
,
188 const BackgroundSyncRegistration
& sync_registration
);
190 void InitImpl(const base::Closure
& callback
);
191 void InitDidGetDataFromBackend(
192 const base::Closure
& callback
,
193 const std::vector
<std::pair
<int64
, std::string
>>& user_data
,
194 ServiceWorkerStatusCode status
);
196 // Register callbacks
197 void RegisterImpl(int64 sw_registration_id
,
198 const BackgroundSyncRegistrationOptions
& options
,
199 bool requested_from_service_worker
,
200 const StatusAndRegistrationCallback
& callback
);
201 void RegisterDidStore(int64 sw_registration_id
,
202 const BackgroundSyncRegistration
& sync_registration
,
203 const StatusAndRegistrationCallback
& callback
,
204 ServiceWorkerStatusCode status
);
206 // Unregister callbacks
208 int64 sw_registration_id
,
209 const RegistrationKey
& registration_key
,
210 BackgroundSyncRegistration::RegistrationId sync_registration_id
,
211 SyncPeriodicity periodicity
,
212 const StatusCallback
& callback
);
213 void UnregisterDidStore(int64 sw_registration_id
,
214 SyncPeriodicity periodicity
,
215 const StatusCallback
& callback
,
216 ServiceWorkerStatusCode status
);
218 // GetRegistration callbacks
219 void GetRegistrationImpl(int64 sw_registration_id
,
220 const RegistrationKey
& registration_key
,
221 const StatusAndRegistrationCallback
& callback
);
223 // GetRegistrations callbacks
224 void GetRegistrationsImpl(int64 sw_registration_id
,
225 SyncPeriodicity periodicity
,
226 const StatusAndRegistrationsCallback
& callback
);
228 bool AreOptionConditionsMet(const BackgroundSyncRegistrationOptions
& options
);
229 bool IsRegistrationReadyToFire(
230 const BackgroundSyncRegistration
& registration
);
232 // Schedules pending registrations to run in the future. For one-shots this
233 // means keeping the browser alive so that network connectivity events can be
234 // seen (on Android the browser is instead woken up the next time it goes
235 // online). For periodic syncs this means creating an alarm.
236 void SchedulePendingRegistrations();
238 // FireReadyEvents and callbacks
239 void FireReadyEvents();
240 void FireReadyEventsImpl(const base::Closure
& callback
);
241 void FireReadyEventsDidFindRegistration(
242 const RegistrationKey
& registration_key
,
243 BackgroundSyncRegistration::RegistrationId registration_id
,
244 const base::Closure
& event_fired_callback
,
245 const base::Closure
& event_completed_callback
,
246 ServiceWorkerStatusCode service_worker_status
,
247 const scoped_refptr
<ServiceWorkerRegistration
>&
248 service_worker_registration
);
250 // Called when a sync event has completed.
252 const scoped_refptr
<ServiceWorkerRegistration
>&
253 service_worker_registration
,
254 int64 service_worker_id
,
255 const RegistrationKey
& key
,
256 BackgroundSyncRegistration::RegistrationId sync_registration_id
,
257 const base::Closure
& callback
,
258 ServiceWorkerStatusCode status_code
);
259 void EventCompleteImpl(
260 int64 service_worker_id
,
261 const RegistrationKey
& key
,
262 BackgroundSyncRegistration::RegistrationId sync_registration_id
,
263 ServiceWorkerStatusCode status_code
,
264 const base::Closure
& callback
);
265 void EventCompleteDidStore(int64 service_worker_id
,
266 const base::Closure
& callback
,
267 ServiceWorkerStatusCode status_code
);
269 // Called when all sync events have completed.
270 static void OnAllSyncEventsCompleted(const base::TimeTicks
& start_time
,
271 int number_of_batched_sync_events
);
273 // OnRegistrationDeleted callbacks
274 void OnRegistrationDeletedImpl(int64 registration_id
,
275 const base::Closure
& callback
);
277 // OnStorageWiped callbacks
278 void OnStorageWipedImpl(const base::Closure
& callback
);
280 void OnNetworkChanged();
281 void OnPowerChanged();
283 // Operation Scheduling callback and convenience functions.
284 template <typename CallbackT
, typename
... Params
>
285 void CompleteOperationCallback(const CallbackT
& callback
,
286 Params
... parameters
);
287 base::Closure
MakeEmptyCompletion();
288 base::Closure
MakeClosureCompletion(const base::Closure
& callback
);
289 StatusAndRegistrationCallback
MakeStatusAndRegistrationCompletion(
290 const StatusAndRegistrationCallback
& callback
);
291 StatusAndRegistrationsCallback
MakeStatusAndRegistrationsCompletion(
292 const StatusAndRegistrationsCallback
& callback
);
293 BackgroundSyncManager::StatusCallback
MakeStatusCompletion(
294 const StatusCallback
& callback
);
296 SWIdToRegistrationsMap sw_to_registrations_map_
;
297 CacheStorageScheduler op_scheduler_
;
298 scoped_refptr
<ServiceWorkerContextWrapper
> service_worker_context_
;
301 scoped_ptr
<BackgroundSyncNetworkObserver
> network_observer_
;
302 scoped_ptr
<BackgroundSyncPowerObserver
> power_observer_
;
304 base::WeakPtrFactory
<BackgroundSyncManager
> weak_ptr_factory_
;
306 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager
);
309 } // namespace content
311 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_