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