OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ | 5 #ifndef CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ |
6 #define CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ | 6 #define CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 30 matching lines...) Expand all Loading... |
41 // registered events. | 41 // registered events. |
42 // TODO(jkarlin): Keep the browser alive if "Let Google Chrome Run in the | 42 // TODO(jkarlin): Keep the browser alive if "Let Google Chrome Run in the |
43 // Background" is true and a sync is registered. | 43 // Background" is true and a sync is registered. |
44 class CONTENT_EXPORT BackgroundSyncManager | 44 class CONTENT_EXPORT BackgroundSyncManager |
45 : NON_EXPORTED_BASE(public ServiceWorkerContextObserver) { | 45 : NON_EXPORTED_BASE(public ServiceWorkerContextObserver) { |
46 public: | 46 public: |
47 enum ErrorType { | 47 enum ErrorType { |
48 ERROR_TYPE_OK = 0, | 48 ERROR_TYPE_OK = 0, |
49 ERROR_TYPE_STORAGE, | 49 ERROR_TYPE_STORAGE, |
50 ERROR_TYPE_NOT_FOUND, | 50 ERROR_TYPE_NOT_FOUND, |
51 ERROR_TYPE_NO_SERVICE_WORKER | 51 ERROR_TYPE_NO_SERVICE_WORKER, |
| 52 ERROR_TYPE_MAX = ERROR_TYPE_NO_SERVICE_WORKER |
52 }; | 53 }; |
53 | 54 |
54 using StatusCallback = base::Callback<void(ErrorType)>; | 55 using StatusCallback = base::Callback<void(ErrorType)>; |
55 using StatusAndRegistrationCallback = | 56 using StatusAndRegistrationCallback = |
56 base::Callback<void(ErrorType, const BackgroundSyncRegistration&)>; | 57 base::Callback<void(ErrorType, const BackgroundSyncRegistration&)>; |
57 using StatusAndRegistrationsCallback = | 58 using StatusAndRegistrationsCallback = |
58 base::Callback<void(ErrorType, | 59 base::Callback<void(ErrorType, |
59 const std::vector<BackgroundSyncRegistration>&)>; | 60 const std::vector<BackgroundSyncRegistration>&)>; |
60 | 61 |
61 static scoped_ptr<BackgroundSyncManager> Create( | 62 static scoped_ptr<BackgroundSyncManager> Create( |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 void RegisterDidStore(int64 sw_registration_id, | 201 void RegisterDidStore(int64 sw_registration_id, |
201 const BackgroundSyncRegistration& sync_registration, | 202 const BackgroundSyncRegistration& sync_registration, |
202 const StatusAndRegistrationCallback& callback, | 203 const StatusAndRegistrationCallback& callback, |
203 ServiceWorkerStatusCode status); | 204 ServiceWorkerStatusCode status); |
204 | 205 |
205 // Unregister callbacks | 206 // Unregister callbacks |
206 void UnregisterImpl( | 207 void UnregisterImpl( |
207 int64 sw_registration_id, | 208 int64 sw_registration_id, |
208 const RegistrationKey& registration_key, | 209 const RegistrationKey& registration_key, |
209 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 210 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| 211 SyncPeriodicity periodicity, |
210 const StatusCallback& callback); | 212 const StatusCallback& callback); |
211 void UnregisterDidStore( | 213 void UnregisterDidStore(int64 sw_registration_id, |
212 int64 sw_registration_id, | 214 SyncPeriodicity periodicity, |
213 const StatusCallback& callback, | 215 const StatusCallback& callback, |
214 ServiceWorkerStatusCode status); | 216 ServiceWorkerStatusCode status); |
215 | 217 |
216 // GetRegistration callbacks | 218 // GetRegistration callbacks |
217 void GetRegistrationImpl(int64 sw_registration_id, | 219 void GetRegistrationImpl(int64 sw_registration_id, |
218 const RegistrationKey& registration_key, | 220 const RegistrationKey& registration_key, |
219 const StatusAndRegistrationCallback& callback); | 221 const StatusAndRegistrationCallback& callback); |
220 | 222 |
221 // GetRegistrations callbacks | 223 // GetRegistrations callbacks |
222 void GetRegistrationsImpl(int64 sw_registration_id, | 224 void GetRegistrationsImpl(int64 sw_registration_id, |
223 SyncPeriodicity periodicity, | 225 SyncPeriodicity periodicity, |
224 const StatusAndRegistrationsCallback& callback); | 226 const StatusAndRegistrationsCallback& callback); |
225 | 227 |
| 228 bool AreOptionConditionsMet(const BackgroundSyncRegistrationOptions& options); |
226 bool IsRegistrationReadyToFire( | 229 bool IsRegistrationReadyToFire( |
227 const BackgroundSyncRegistration& registration); | 230 const BackgroundSyncRegistration& registration); |
228 | 231 |
229 // Schedules pending registrations to run in the future. For one-shots this | 232 // Schedules pending registrations to run in the future. For one-shots this |
230 // means keeping the browser alive so that network connectivity events can be | 233 // means keeping the browser alive so that network connectivity events can be |
231 // seen (on Android the browser is instead woken up the next time it goes | 234 // seen (on Android the browser is instead woken up the next time it goes |
232 // online). For periodic syncs this means creating an alarm. | 235 // online). For periodic syncs this means creating an alarm. |
233 void SchedulePendingRegistrations(); | 236 void SchedulePendingRegistrations(); |
234 | 237 |
235 // FireReadyEvents and callbacks | 238 // FireReadyEvents and callbacks |
236 void FireReadyEvents(); | 239 void FireReadyEvents(); |
237 void FireReadyEventsImpl(const base::Closure& callback); | 240 void FireReadyEventsImpl(const base::Closure& callback); |
238 void FireReadyEventsDidFindRegistration( | 241 void FireReadyEventsDidFindRegistration( |
239 const RegistrationKey& registration_key, | 242 const RegistrationKey& registration_key, |
240 BackgroundSyncRegistration::RegistrationId registration_id, | 243 BackgroundSyncRegistration::RegistrationId registration_id, |
241 const base::Closure& callback, | 244 const base::Closure& event_fired_callback, |
| 245 const base::Closure& event_completed_callback, |
242 ServiceWorkerStatusCode service_worker_status, | 246 ServiceWorkerStatusCode service_worker_status, |
243 const scoped_refptr<ServiceWorkerRegistration>& | 247 const scoped_refptr<ServiceWorkerRegistration>& |
244 service_worker_registration); | 248 service_worker_registration); |
245 | 249 |
246 // Called when a sync event has completed. | 250 // Called when a sync event has completed. |
247 void EventComplete( | 251 void EventComplete( |
248 const scoped_refptr<ServiceWorkerRegistration>& | 252 const scoped_refptr<ServiceWorkerRegistration>& |
249 service_worker_registration, | 253 service_worker_registration, |
250 int64 service_worker_id, | 254 int64 service_worker_id, |
251 const RegistrationKey& key, | 255 const RegistrationKey& key, |
252 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 256 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| 257 const base::Closure& callback, |
253 ServiceWorkerStatusCode status_code); | 258 ServiceWorkerStatusCode status_code); |
254 void EventCompleteImpl( | 259 void EventCompleteImpl( |
255 int64 service_worker_id, | 260 int64 service_worker_id, |
256 const RegistrationKey& key, | 261 const RegistrationKey& key, |
257 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 262 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
258 ServiceWorkerStatusCode status_code, | 263 ServiceWorkerStatusCode status_code, |
259 const base::Closure& callback); | 264 const base::Closure& callback); |
260 void EventCompleteDidStore(int64 service_worker_id, | 265 void EventCompleteDidStore(int64 service_worker_id, |
261 const base::Closure& callback, | 266 const base::Closure& callback, |
262 ServiceWorkerStatusCode status_code); | 267 ServiceWorkerStatusCode status_code); |
263 | 268 |
| 269 // Called when all sync events have completed. |
| 270 static void OnAllSyncEventsCompleted(const base::TimeTicks& start_time); |
| 271 |
264 // OnRegistrationDeleted callbacks | 272 // OnRegistrationDeleted callbacks |
265 void OnRegistrationDeletedImpl(int64 registration_id, | 273 void OnRegistrationDeletedImpl(int64 registration_id, |
266 const base::Closure& callback); | 274 const base::Closure& callback); |
267 | 275 |
268 // OnStorageWiped callbacks | 276 // OnStorageWiped callbacks |
269 void OnStorageWipedImpl(const base::Closure& callback); | 277 void OnStorageWipedImpl(const base::Closure& callback); |
270 | 278 |
271 void OnNetworkChanged(); | 279 void OnNetworkChanged(); |
272 void OnPowerChanged(); | 280 void OnPowerChanged(); |
273 | 281 |
274 // Operation Scheduling callback and convenience functions. | 282 // Operation Scheduling callback and convenience functions. |
275 template <typename CallbackT, typename... Params> | 283 template <typename CallbackT, typename... Params> |
276 void CompleteOperationCallback(const CallbackT& callback, | 284 void CompleteOperationCallback(const CallbackT& callback, |
277 Params... parameters); | 285 Params... parameters); |
278 base::Closure MakeEmptyCompletion(); | 286 base::Closure MakeEmptyCompletion(); |
| 287 base::Closure MakeClosureCompletion(const base::Closure& callback); |
279 StatusAndRegistrationCallback MakeStatusAndRegistrationCompletion( | 288 StatusAndRegistrationCallback MakeStatusAndRegistrationCompletion( |
280 const StatusAndRegistrationCallback& callback); | 289 const StatusAndRegistrationCallback& callback); |
281 StatusAndRegistrationsCallback MakeStatusAndRegistrationsCompletion( | 290 StatusAndRegistrationsCallback MakeStatusAndRegistrationsCompletion( |
282 const StatusAndRegistrationsCallback& callback); | 291 const StatusAndRegistrationsCallback& callback); |
283 BackgroundSyncManager::StatusCallback MakeStatusCompletion( | 292 BackgroundSyncManager::StatusCallback MakeStatusCompletion( |
284 const StatusCallback& callback); | 293 const StatusCallback& callback); |
285 | 294 |
286 SWIdToRegistrationsMap sw_to_registrations_map_; | 295 SWIdToRegistrationsMap sw_to_registrations_map_; |
287 CacheStorageScheduler op_scheduler_; | 296 CacheStorageScheduler op_scheduler_; |
288 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; | 297 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; |
289 bool disabled_; | 298 bool disabled_; |
290 | 299 |
291 scoped_ptr<BackgroundSyncNetworkObserver> network_observer_; | 300 scoped_ptr<BackgroundSyncNetworkObserver> network_observer_; |
292 scoped_ptr<BackgroundSyncPowerObserver> power_observer_; | 301 scoped_ptr<BackgroundSyncPowerObserver> power_observer_; |
293 | 302 |
294 base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_; | 303 base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_; |
295 | 304 |
296 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager); | 305 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager); |
297 }; | 306 }; |
298 | 307 |
299 } // namespace content | 308 } // namespace content |
300 | 309 |
301 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ | 310 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ |
OLD | NEW |