| 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 <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 | 46 |
| 47 // BackgroundSyncManager manages and stores the set of background sync | 47 // BackgroundSyncManager manages and stores the set of background sync |
| 48 // registrations across all registered service workers for a profile. | 48 // registrations across all registered service workers for a profile. |
| 49 // Registrations are stored along with their associated Service Worker | 49 // Registrations are stored along with their associated Service Worker |
| 50 // registration in ServiceWorkerStorage. If the ServiceWorker is unregistered, | 50 // registration in ServiceWorkerStorage. If the ServiceWorker is unregistered, |
| 51 // the sync registrations are removed. This class must be run on the IO | 51 // the sync registrations are removed. This class must be run on the IO |
| 52 // thread. The asynchronous methods are executed sequentially. | 52 // thread. The asynchronous methods are executed sequentially. |
| 53 class CONTENT_EXPORT BackgroundSyncManager | 53 class CONTENT_EXPORT BackgroundSyncManager |
| 54 : NON_EXPORTED_BASE(public ServiceWorkerContextCoreObserver) { | 54 : NON_EXPORTED_BASE(public ServiceWorkerContextCoreObserver) { |
| 55 public: | 55 public: |
| 56 using BoolCallback = base::Callback<void(bool)>; | 56 using BoolCallback = base::OnceCallback<void(bool)>; |
| 57 using StatusCallback = base::Callback<void(BackgroundSyncStatus)>; | |
| 58 using StatusAndRegistrationCallback = | 57 using StatusAndRegistrationCallback = |
| 59 base::Callback<void(BackgroundSyncStatus, | 58 base::OnceCallback<void(BackgroundSyncStatus, |
| 60 std::unique_ptr<BackgroundSyncRegistration>)>; | 59 std::unique_ptr<BackgroundSyncRegistration>)>; |
| 61 using StatusAndRegistrationsCallback = base::Callback<void( | 60 using StatusAndRegistrationsCallback = base::OnceCallback<void( |
| 62 BackgroundSyncStatus, | 61 BackgroundSyncStatus, |
| 63 std::vector<std::unique_ptr<BackgroundSyncRegistration>>)>; | 62 std::vector<std::unique_ptr<BackgroundSyncRegistration>>)>; |
| 64 | 63 |
| 65 static std::unique_ptr<BackgroundSyncManager> Create( | 64 static std::unique_ptr<BackgroundSyncManager> Create( |
| 66 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context); | 65 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context); |
| 67 ~BackgroundSyncManager() override; | 66 ~BackgroundSyncManager() override; |
| 68 | 67 |
| 69 // Stores the given background sync registration and adds it to the scheduling | 68 // Stores the given background sync registration and adds it to the scheduling |
| 70 // queue. It will overwrite an existing registration with the same tag unless | 69 // queue. It will overwrite an existing registration with the same tag unless |
| 71 // they're identical (save for the id). Calls |callback| with | 70 // they're identical (save for the id). Calls |callback| with |
| 72 // BACKGROUND_SYNC_STATUS_OK and the accepted registration on success. | 71 // BACKGROUND_SYNC_STATUS_OK and the accepted registration on success. |
| 73 // The accepted registration will have a unique id. It may also have altered | 72 // The accepted registration will have a unique id. It may also have altered |
| 74 // parameters if the user or UA chose different parameters than those | 73 // parameters if the user or UA chose different parameters than those |
| 75 // supplied. | 74 // supplied. |
| 76 void Register(int64_t sw_registration_id, | 75 void Register(int64_t sw_registration_id, |
| 77 const BackgroundSyncRegistrationOptions& options, | 76 const BackgroundSyncRegistrationOptions& options, |
| 78 const StatusAndRegistrationCallback& callback); | 77 StatusAndRegistrationCallback callback); |
| 79 | 78 |
| 80 // Finds the background sync registrations associated with | 79 // Finds the background sync registrations associated with |
| 81 // |sw_registration_id|. Calls |callback| with BACKGROUND_SYNC_STATUS_OK on | 80 // |sw_registration_id|. Calls |callback| with BACKGROUND_SYNC_STATUS_OK on |
| 82 // success. | 81 // success. |
| 83 void GetRegistrations(int64_t sw_registration_id, | 82 void GetRegistrations(int64_t sw_registration_id, |
| 84 const StatusAndRegistrationsCallback& callback); | 83 StatusAndRegistrationsCallback callback); |
| 85 | 84 |
| 86 // ServiceWorkerContextCoreObserver overrides. | 85 // ServiceWorkerContextCoreObserver overrides. |
| 87 void OnRegistrationDeleted(int64_t sw_registration_id, | 86 void OnRegistrationDeleted(int64_t sw_registration_id, |
| 88 const GURL& pattern) override; | 87 const GURL& pattern) override; |
| 89 void OnStorageWiped() override; | 88 void OnStorageWiped() override; |
| 90 | 89 |
| 91 // Sets the max number of sync attempts after any pending operations have | 90 // Sets the max number of sync attempts after any pending operations have |
| 92 // completed. | 91 // completed. |
| 93 void SetMaxSyncAttemptsForTesting(int max_attempts); | 92 void SetMaxSyncAttemptsForTesting(int max_attempts); |
| 94 | 93 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 124 const ServiceWorkerStorage::StatusCallback& callback); | 123 const ServiceWorkerStorage::StatusCallback& callback); |
| 125 virtual void GetDataFromBackend( | 124 virtual void GetDataFromBackend( |
| 126 const std::string& backend_key, | 125 const std::string& backend_key, |
| 127 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& | 126 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& |
| 128 callback); | 127 callback); |
| 129 virtual void DispatchSyncEvent( | 128 virtual void DispatchSyncEvent( |
| 130 const std::string& tag, | 129 const std::string& tag, |
| 131 scoped_refptr<ServiceWorkerVersion> active_version, | 130 scoped_refptr<ServiceWorkerVersion> active_version, |
| 132 blink::mojom::BackgroundSyncEventLastChance last_chance, | 131 blink::mojom::BackgroundSyncEventLastChance last_chance, |
| 133 const ServiceWorkerVersion::StatusCallback& callback); | 132 const ServiceWorkerVersion::StatusCallback& callback); |
| 134 virtual void ScheduleDelayedTask(const base::Closure& callback, | 133 virtual void ScheduleDelayedTask(base::OnceClosure callback, |
| 135 base::TimeDelta delay); | 134 base::TimeDelta delay); |
| 136 virtual void HasMainFrameProviderHost(const GURL& origin, | 135 virtual void HasMainFrameProviderHost(const GURL& origin, |
| 137 const BoolCallback& callback); | 136 BoolCallback callback); |
| 138 | 137 |
| 139 private: | 138 private: |
| 140 friend class TestBackgroundSyncManager; | 139 friend class TestBackgroundSyncManager; |
| 141 friend class BackgroundSyncManagerTest; | 140 friend class BackgroundSyncManagerTest; |
| 142 | 141 |
| 143 struct BackgroundSyncRegistrations { | 142 struct BackgroundSyncRegistrations { |
| 144 using RegistrationMap = std::map<std::string, BackgroundSyncRegistration>; | 143 using RegistrationMap = std::map<std::string, BackgroundSyncRegistration>; |
| 145 | 144 |
| 146 BackgroundSyncRegistrations(); | 145 BackgroundSyncRegistrations(); |
| 147 BackgroundSyncRegistrations(const BackgroundSyncRegistrations& other); | 146 BackgroundSyncRegistrations(const BackgroundSyncRegistrations& other); |
| 148 ~BackgroundSyncRegistrations(); | 147 ~BackgroundSyncRegistrations(); |
| 149 | 148 |
| 150 RegistrationMap registration_map; | 149 RegistrationMap registration_map; |
| 151 BackgroundSyncRegistration::RegistrationId next_id; | 150 BackgroundSyncRegistration::RegistrationId next_id; |
| 152 GURL origin; | 151 GURL origin; |
| 153 }; | 152 }; |
| 154 | 153 |
| 155 using PermissionStatusCallback = base::Callback<void(bool)>; | |
| 156 using SWIdToRegistrationsMap = std::map<int64_t, BackgroundSyncRegistrations>; | 154 using SWIdToRegistrationsMap = std::map<int64_t, BackgroundSyncRegistrations>; |
| 157 | 155 |
| 158 static const size_t kMaxTagLength = 10240; | 156 static const size_t kMaxTagLength = 10240; |
| 159 | 157 |
| 160 // Disable the manager. Already queued operations will abort once they start | 158 // Disable the manager. Already queued operations will abort once they start |
| 161 // to run (in their impl methods). Future operations will not queue. | 159 // to run (in their impl methods). Future operations will not queue. |
| 162 // The list of active registrations is cleared and the backend is also cleared | 160 // The list of active registrations is cleared and the backend is also cleared |
| 163 // (if it's still functioning). The manager will reenable itself once it | 161 // (if it's still functioning). The manager will reenable itself once it |
| 164 // receives the OnStorageWiped message or on browser restart. | 162 // receives the OnStorageWiped message or on browser restart. |
| 165 void DisableAndClearManager(const base::Closure& callback); | 163 void DisableAndClearManager(base::OnceClosure callback); |
| 166 void DisableAndClearDidGetRegistrations( | 164 void DisableAndClearDidGetRegistrations( |
| 167 const base::Closure& callback, | 165 base::OnceClosure callback, |
| 168 const std::vector<std::pair<int64_t, std::string>>& user_data, | 166 const std::vector<std::pair<int64_t, std::string>>& user_data, |
| 169 ServiceWorkerStatusCode status); | 167 ServiceWorkerStatusCode status); |
| 170 void DisableAndClearManagerClearedOne(const base::Closure& barrier_closure, | 168 void DisableAndClearManagerClearedOne(base::OnceClosure barrier_closure, |
| 171 ServiceWorkerStatusCode status); | 169 ServiceWorkerStatusCode status); |
| 172 | 170 |
| 173 // Returns the existing registration or nullptr if it cannot be found. | 171 // Returns the existing registration or nullptr if it cannot be found. |
| 174 BackgroundSyncRegistration* LookupActiveRegistration( | 172 BackgroundSyncRegistration* LookupActiveRegistration( |
| 175 int64_t sw_registration_id, | 173 int64_t sw_registration_id, |
| 176 const std::string& tag); | 174 const std::string& tag); |
| 177 | 175 |
| 178 // Write all registrations for a given |sw_registration_id| to persistent | 176 // Write all registrations for a given |sw_registration_id| to persistent |
| 179 // storage. | 177 // storage. |
| 180 void StoreRegistrations(int64_t sw_registration_id, | 178 void StoreRegistrations(int64_t sw_registration_id, |
| 181 const ServiceWorkerStorage::StatusCallback& callback); | 179 const ServiceWorkerStorage::StatusCallback& callback); |
| 182 | 180 |
| 183 // Removes the active registration if it is in the map. | 181 // Removes the active registration if it is in the map. |
| 184 void RemoveActiveRegistration(int64_t sw_registration_id, | 182 void RemoveActiveRegistration(int64_t sw_registration_id, |
| 185 const std::string& tag); | 183 const std::string& tag); |
| 186 | 184 |
| 187 void AddActiveRegistration( | 185 void AddActiveRegistration( |
| 188 int64_t sw_registration_id, | 186 int64_t sw_registration_id, |
| 189 const GURL& origin, | 187 const GURL& origin, |
| 190 const BackgroundSyncRegistration& sync_registration); | 188 const BackgroundSyncRegistration& sync_registration); |
| 191 | 189 |
| 192 void InitImpl(const base::Closure& callback); | 190 void InitImpl(base::OnceClosure callback); |
| 193 void InitDidGetControllerParameters( | 191 void InitDidGetControllerParameters( |
| 194 const base::Closure& callback, | 192 base::OnceClosure callback, |
| 195 std::unique_ptr<BackgroundSyncParameters> parameters); | 193 std::unique_ptr<BackgroundSyncParameters> parameters); |
| 196 void InitDidGetDataFromBackend( | 194 void InitDidGetDataFromBackend( |
| 197 const base::Closure& callback, | 195 base::OnceClosure callback, |
| 198 const std::vector<std::pair<int64_t, std::string>>& user_data, | 196 const std::vector<std::pair<int64_t, std::string>>& user_data, |
| 199 ServiceWorkerStatusCode status); | 197 ServiceWorkerStatusCode status); |
| 200 | 198 |
| 201 // Register callbacks | 199 // Register callbacks |
| 202 void RegisterCheckIfHasMainFrame( | 200 void RegisterCheckIfHasMainFrame( |
| 203 int64_t sw_registration_id, | 201 int64_t sw_registration_id, |
| 204 const BackgroundSyncRegistrationOptions& options, | 202 const BackgroundSyncRegistrationOptions& options, |
| 205 const StatusAndRegistrationCallback& callback); | 203 StatusAndRegistrationCallback callback); |
| 206 void RegisterDidCheckIfMainFrame( | 204 void RegisterDidCheckIfMainFrame( |
| 207 int64_t sw_registration_id, | 205 int64_t sw_registration_id, |
| 208 const BackgroundSyncRegistrationOptions& options, | 206 const BackgroundSyncRegistrationOptions& options, |
| 209 const StatusAndRegistrationCallback& callback, | 207 StatusAndRegistrationCallback callback, |
| 210 bool has_main_frame_client); | 208 bool has_main_frame_client); |
| 211 void RegisterImpl(int64_t sw_registration_id, | 209 void RegisterImpl(int64_t sw_registration_id, |
| 212 const BackgroundSyncRegistrationOptions& options, | 210 const BackgroundSyncRegistrationOptions& options, |
| 213 const StatusAndRegistrationCallback& callback); | 211 StatusAndRegistrationCallback callback); |
| 214 void RegisterDidAskForPermission( | 212 void RegisterDidAskForPermission( |
| 215 int64_t sw_registration_id, | 213 int64_t sw_registration_id, |
| 216 const BackgroundSyncRegistrationOptions& options, | 214 const BackgroundSyncRegistrationOptions& options, |
| 217 const StatusAndRegistrationCallback& callback, | 215 StatusAndRegistrationCallback callback, |
| 218 blink::mojom::PermissionStatus permission_status); | 216 blink::mojom::PermissionStatus permission_status); |
| 219 void RegisterDidStore(int64_t sw_registration_id, | 217 void RegisterDidStore(int64_t sw_registration_id, |
| 220 const BackgroundSyncRegistration& new_registration, | 218 const BackgroundSyncRegistration& new_registration, |
| 221 const StatusAndRegistrationCallback& callback, | 219 StatusAndRegistrationCallback callback, |
| 222 ServiceWorkerStatusCode status); | 220 ServiceWorkerStatusCode status); |
| 223 | 221 |
| 224 // GetRegistrations callbacks | 222 // GetRegistrations callbacks |
| 225 void GetRegistrationsImpl(int64_t sw_registration_id, | 223 void GetRegistrationsImpl(int64_t sw_registration_id, |
| 226 const StatusAndRegistrationsCallback& callback); | 224 StatusAndRegistrationsCallback callback); |
| 227 | 225 |
| 228 bool AreOptionConditionsMet(const BackgroundSyncRegistrationOptions& options); | 226 bool AreOptionConditionsMet(const BackgroundSyncRegistrationOptions& options); |
| 229 bool IsRegistrationReadyToFire( | 227 bool IsRegistrationReadyToFire( |
| 230 const BackgroundSyncRegistration& registration); | 228 const BackgroundSyncRegistration& registration); |
| 231 | 229 |
| 232 // Determines if the browser needs to be able to run in the background (e.g., | 230 // Determines if the browser needs to be able to run in the background (e.g., |
| 233 // to run a pending registration or verify that a firing registration | 231 // to run a pending registration or verify that a firing registration |
| 234 // completed). If background processing is required it calls out to the | 232 // completed). If background processing is required it calls out to the |
| 235 // BackgroundSyncController to enable it. | 233 // BackgroundSyncController to enable it. |
| 236 // Assumes that all registrations in the pending state are not currently ready | 234 // Assumes that all registrations in the pending state are not currently ready |
| 237 // to fire. Therefore this should not be called directly and should only be | 235 // to fire. Therefore this should not be called directly and should only be |
| 238 // called by FireReadyEvents. | 236 // called by FireReadyEvents. |
| 239 void RunInBackgroundIfNecessary(); | 237 void RunInBackgroundIfNecessary(); |
| 240 | 238 |
| 241 // FireReadyEvents scans the list of available events and fires those that are | 239 // FireReadyEvents scans the list of available events and fires those that are |
| 242 // ready to fire. For those that can't yet be fired, wakeup alarms are set. | 240 // ready to fire. For those that can't yet be fired, wakeup alarms are set. |
| 243 void FireReadyEvents(); | 241 void FireReadyEvents(); |
| 244 void FireReadyEventsImpl(const base::Closure& callback); | 242 void FireReadyEventsImpl(base::OnceClosure callback); |
| 245 void FireReadyEventsDidFindRegistration( | 243 void FireReadyEventsDidFindRegistration( |
| 246 const std::string& tag, | 244 const std::string& tag, |
| 247 BackgroundSyncRegistration::RegistrationId registration_id, | 245 BackgroundSyncRegistration::RegistrationId registration_id, |
| 248 const base::Closure& event_fired_callback, | 246 base::OnceClosure event_fired_callback, |
| 249 const base::Closure& event_completed_callback, | 247 base::OnceClosure event_completed_callback, |
| 250 ServiceWorkerStatusCode service_worker_status, | 248 ServiceWorkerStatusCode service_worker_status, |
| 251 scoped_refptr<ServiceWorkerRegistration> service_worker_registration); | 249 scoped_refptr<ServiceWorkerRegistration> service_worker_registration); |
| 252 void FireReadyEventsAllEventsFiring(const base::Closure& callback); | 250 void FireReadyEventsAllEventsFiring(base::OnceClosure callback); |
| 253 | 251 |
| 254 // Called when a sync event has completed. | 252 // Called when a sync event has completed. |
| 255 void EventComplete( | 253 void EventComplete( |
| 256 scoped_refptr<ServiceWorkerRegistration> service_worker_registration, | 254 scoped_refptr<ServiceWorkerRegistration> service_worker_registration, |
| 257 int64_t service_worker_id, | 255 int64_t service_worker_id, |
| 258 const std::string& tag, | 256 const std::string& tag, |
| 259 const base::Closure& callback, | 257 base::OnceClosure callback, |
| 260 ServiceWorkerStatusCode status_code); | 258 ServiceWorkerStatusCode status_code); |
| 261 void EventCompleteImpl(int64_t service_worker_id, | 259 void EventCompleteImpl(int64_t service_worker_id, |
| 262 const std::string& tag, | 260 const std::string& tag, |
| 263 ServiceWorkerStatusCode status_code, | 261 ServiceWorkerStatusCode status_code, |
| 264 const base::Closure& callback); | 262 base::OnceClosure callback); |
| 265 void EventCompleteDidStore(int64_t service_worker_id, | 263 void EventCompleteDidStore(int64_t service_worker_id, |
| 266 const base::Closure& callback, | 264 base::OnceClosure callback, |
| 267 ServiceWorkerStatusCode status_code); | 265 ServiceWorkerStatusCode status_code); |
| 268 | 266 |
| 269 // Called when all sync events have completed. | 267 // Called when all sync events have completed. |
| 270 static void OnAllSyncEventsCompleted(const base::TimeTicks& start_time, | 268 static void OnAllSyncEventsCompleted(const base::TimeTicks& start_time, |
| 271 int number_of_batched_sync_events); | 269 int number_of_batched_sync_events); |
| 272 | 270 |
| 273 // OnRegistrationDeleted callbacks | 271 // OnRegistrationDeleted callbacks |
| 274 void OnRegistrationDeletedImpl(int64_t sw_registration_id, | 272 void OnRegistrationDeletedImpl(int64_t sw_registration_id, |
| 275 const base::Closure& callback); | 273 base::OnceClosure callback); |
| 276 | 274 |
| 277 // OnStorageWiped callbacks | 275 // OnStorageWiped callbacks |
| 278 void OnStorageWipedImpl(const base::Closure& callback); | 276 void OnStorageWipedImpl(base::OnceClosure callback); |
| 279 | 277 |
| 280 void OnNetworkChanged(); | 278 void OnNetworkChanged(); |
| 281 | 279 |
| 282 // SetMaxSyncAttempts callback | 280 // SetMaxSyncAttempts callback |
| 283 void SetMaxSyncAttemptsImpl(int max_sync_attempts, | 281 void SetMaxSyncAttemptsImpl(int max_sync_attempts, |
| 284 const base::Closure& callback); | 282 base::OnceClosure callback); |
| 285 | 283 |
| 286 base::Closure MakeEmptyCompletion(); | 284 base::OnceClosure MakeEmptyCompletion(); |
| 287 | 285 |
| 288 SWIdToRegistrationsMap active_registrations_; | 286 SWIdToRegistrationsMap active_registrations_; |
| 289 CacheStorageScheduler op_scheduler_; | 287 CacheStorageScheduler op_scheduler_; |
| 290 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; | 288 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; |
| 291 | 289 |
| 292 std::unique_ptr<BackgroundSyncParameters> parameters_; | 290 std::unique_ptr<BackgroundSyncParameters> parameters_; |
| 293 | 291 |
| 294 // True if the manager is disabled and registrations should fail. | 292 // True if the manager is disabled and registrations should fail. |
| 295 bool disabled_; | 293 bool disabled_; |
| 296 | 294 |
| 297 // The number of registrations currently in the firing state. | 295 // The number of registrations currently in the firing state. |
| 298 int num_firing_registrations_; | 296 int num_firing_registrations_; |
| 299 | 297 |
| 300 base::CancelableCallback<void()> delayed_sync_task_; | 298 base::CancelableCallback<void()> delayed_sync_task_; |
| 301 | 299 |
| 302 std::unique_ptr<BackgroundSyncNetworkObserver> network_observer_; | 300 std::unique_ptr<BackgroundSyncNetworkObserver> network_observer_; |
| 303 | 301 |
| 304 std::unique_ptr<base::Clock> clock_; | 302 std::unique_ptr<base::Clock> clock_; |
| 305 | 303 |
| 306 base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_; | 304 base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_; |
| 307 | 305 |
| 308 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager); | 306 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager); |
| 309 }; | 307 }; |
| 310 | 308 |
| 311 } // namespace content | 309 } // namespace content |
| 312 | 310 |
| 313 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ | 311 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ |
| OLD | NEW |