| 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 |
| 11 #include <map> | 11 #include <map> |
| 12 #include <memory> |
| 12 #include <string> | 13 #include <string> |
| 13 #include <utility> | 14 #include <utility> |
| 14 #include <vector> | 15 #include <vector> |
| 15 | 16 |
| 16 #include "base/callback_forward.h" | 17 #include "base/callback_forward.h" |
| 17 #include "base/cancelable_callback.h" | 18 #include "base/cancelable_callback.h" |
| 18 #include "base/macros.h" | 19 #include "base/macros.h" |
| 19 #include "base/memory/scoped_ptr.h" | |
| 20 #include "base/memory/scoped_vector.h" | 20 #include "base/memory/scoped_vector.h" |
| 21 #include "base/memory/weak_ptr.h" | 21 #include "base/memory/weak_ptr.h" |
| 22 #include "base/time/clock.h" | 22 #include "base/time/clock.h" |
| 23 #include "content/browser/background_sync/background_sync.pb.h" | 23 #include "content/browser/background_sync/background_sync.pb.h" |
| 24 #include "content/browser/background_sync/background_sync_registration.h" | 24 #include "content/browser/background_sync/background_sync_registration.h" |
| 25 #include "content/browser/background_sync/background_sync_status.h" | 25 #include "content/browser/background_sync/background_sync_status.h" |
| 26 #include "content/browser/cache_storage/cache_storage_scheduler.h" | 26 #include "content/browser/cache_storage/cache_storage_scheduler.h" |
| 27 #include "content/browser/service_worker/service_worker_context_observer.h" | 27 #include "content/browser/service_worker/service_worker_context_observer.h" |
| 28 #include "content/browser/service_worker/service_worker_storage.h" | 28 #include "content/browser/service_worker/service_worker_storage.h" |
| 29 #include "content/common/background_sync_service.mojom.h" | 29 #include "content/common/background_sync_service.mojom.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 51 // registration in ServiceWorkerStorage. If the ServiceWorker is unregistered, | 51 // registration in ServiceWorkerStorage. If the ServiceWorker is unregistered, |
| 52 // the sync registrations are removed. This class must be run on the IO | 52 // the sync registrations are removed. This class must be run on the IO |
| 53 // thread. The asynchronous methods are executed sequentially. | 53 // thread. The asynchronous methods are executed sequentially. |
| 54 class CONTENT_EXPORT BackgroundSyncManager | 54 class CONTENT_EXPORT BackgroundSyncManager |
| 55 : NON_EXPORTED_BASE(public ServiceWorkerContextObserver) { | 55 : NON_EXPORTED_BASE(public ServiceWorkerContextObserver) { |
| 56 public: | 56 public: |
| 57 using BoolCallback = base::Callback<void(bool)>; | 57 using BoolCallback = base::Callback<void(bool)>; |
| 58 using StatusCallback = base::Callback<void(BackgroundSyncStatus)>; | 58 using StatusCallback = base::Callback<void(BackgroundSyncStatus)>; |
| 59 using StatusAndRegistrationCallback = | 59 using StatusAndRegistrationCallback = |
| 60 base::Callback<void(BackgroundSyncStatus, | 60 base::Callback<void(BackgroundSyncStatus, |
| 61 scoped_ptr<BackgroundSyncRegistration>)>; | 61 std::unique_ptr<BackgroundSyncRegistration>)>; |
| 62 using StatusAndRegistrationsCallback = base::Callback<void( | 62 using StatusAndRegistrationsCallback = base::Callback<void( |
| 63 BackgroundSyncStatus, | 63 BackgroundSyncStatus, |
| 64 scoped_ptr<ScopedVector<BackgroundSyncRegistration>>)>; | 64 std::unique_ptr<ScopedVector<BackgroundSyncRegistration>>)>; |
| 65 | 65 |
| 66 static scoped_ptr<BackgroundSyncManager> Create( | 66 static std::unique_ptr<BackgroundSyncManager> Create( |
| 67 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context); | 67 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context); |
| 68 ~BackgroundSyncManager() override; | 68 ~BackgroundSyncManager() override; |
| 69 | 69 |
| 70 // Stores the given background sync registration and adds it to the scheduling | 70 // Stores the given background sync registration and adds it to the scheduling |
| 71 // queue. It will overwrite an existing registration with the same tag unless | 71 // queue. It will overwrite an existing registration with the same tag unless |
| 72 // they're identical (save for the id). Calls |callback| with | 72 // they're identical (save for the id). Calls |callback| with |
| 73 // BACKGROUND_SYNC_STATUS_OK and the accepted registration on success. | 73 // BACKGROUND_SYNC_STATUS_OK and the accepted registration on success. |
| 74 // The accepted registration will have a unique id. It may also have altered | 74 // The accepted registration will have a unique id. It may also have altered |
| 75 // parameters if the user or UA chose different parameters than those | 75 // parameters if the user or UA chose different parameters than those |
| 76 // supplied. | 76 // supplied. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 90 void OnStorageWiped() override; | 90 void OnStorageWiped() override; |
| 91 | 91 |
| 92 // Sets the max number of sync attempts after any pending operations have | 92 // Sets the max number of sync attempts after any pending operations have |
| 93 // completed. | 93 // completed. |
| 94 void SetMaxSyncAttemptsForTesting(int max_attempts); | 94 void SetMaxSyncAttemptsForTesting(int max_attempts); |
| 95 | 95 |
| 96 BackgroundSyncNetworkObserver* GetNetworkObserverForTesting() { | 96 BackgroundSyncNetworkObserver* GetNetworkObserverForTesting() { |
| 97 return network_observer_.get(); | 97 return network_observer_.get(); |
| 98 } | 98 } |
| 99 | 99 |
| 100 void set_clock(scoped_ptr<base::Clock> clock) { | 100 void set_clock(std::unique_ptr<base::Clock> clock) { |
| 101 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 101 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 102 clock_ = std::move(clock); | 102 clock_ = std::move(clock); |
| 103 } | 103 } |
| 104 | 104 |
| 105 protected: | 105 protected: |
| 106 explicit BackgroundSyncManager( | 106 explicit BackgroundSyncManager( |
| 107 const scoped_refptr<ServiceWorkerContextWrapper>& context); | 107 const scoped_refptr<ServiceWorkerContextWrapper>& context); |
| 108 | 108 |
| 109 // Init must be called before any public member function. Only call it once. | 109 // Init must be called before any public member function. Only call it once. |
| 110 void Init(); | 110 void Init(); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 const std::string& tag); | 179 const std::string& tag); |
| 180 | 180 |
| 181 void AddActiveRegistration( | 181 void AddActiveRegistration( |
| 182 int64_t sw_registration_id, | 182 int64_t sw_registration_id, |
| 183 const GURL& origin, | 183 const GURL& origin, |
| 184 const BackgroundSyncRegistration& sync_registration); | 184 const BackgroundSyncRegistration& sync_registration); |
| 185 | 185 |
| 186 void InitImpl(const base::Closure& callback); | 186 void InitImpl(const base::Closure& callback); |
| 187 void InitDidGetControllerParameters( | 187 void InitDidGetControllerParameters( |
| 188 const base::Closure& callback, | 188 const base::Closure& callback, |
| 189 scoped_ptr<BackgroundSyncParameters> parameters); | 189 std::unique_ptr<BackgroundSyncParameters> parameters); |
| 190 void InitDidGetDataFromBackend( | 190 void InitDidGetDataFromBackend( |
| 191 const base::Closure& callback, | 191 const base::Closure& callback, |
| 192 const std::vector<std::pair<int64_t, std::string>>& user_data, | 192 const std::vector<std::pair<int64_t, std::string>>& user_data, |
| 193 ServiceWorkerStatusCode status); | 193 ServiceWorkerStatusCode status); |
| 194 | 194 |
| 195 // Register callbacks | 195 // Register callbacks |
| 196 void RegisterCheckIfHasMainFrame( | 196 void RegisterCheckIfHasMainFrame( |
| 197 int64_t sw_registration_id, | 197 int64_t sw_registration_id, |
| 198 const BackgroundSyncRegistrationOptions& options, | 198 const BackgroundSyncRegistrationOptions& options, |
| 199 const StatusAndRegistrationCallback& callback); | 199 const StatusAndRegistrationCallback& callback); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 void SetMaxSyncAttemptsImpl(int max_sync_attempts, | 278 void SetMaxSyncAttemptsImpl(int max_sync_attempts, |
| 279 const base::Closure& callback); | 279 const base::Closure& callback); |
| 280 | 280 |
| 281 // Operation Scheduling callback and convenience functions. | 281 // Operation Scheduling callback and convenience functions. |
| 282 template <typename CallbackT, typename... Params> | 282 template <typename CallbackT, typename... Params> |
| 283 void CompleteOperationCallback(const CallbackT& callback, | 283 void CompleteOperationCallback(const CallbackT& callback, |
| 284 Params... parameters); | 284 Params... parameters); |
| 285 void CompleteStatusAndRegistrationCallback( | 285 void CompleteStatusAndRegistrationCallback( |
| 286 StatusAndRegistrationCallback callback, | 286 StatusAndRegistrationCallback callback, |
| 287 BackgroundSyncStatus status, | 287 BackgroundSyncStatus status, |
| 288 scoped_ptr<BackgroundSyncRegistration> registration); | 288 std::unique_ptr<BackgroundSyncRegistration> registration); |
| 289 void CompleteStatusAndRegistrationsCallback( | 289 void CompleteStatusAndRegistrationsCallback( |
| 290 StatusAndRegistrationsCallback callback, | 290 StatusAndRegistrationsCallback callback, |
| 291 BackgroundSyncStatus status, | 291 BackgroundSyncStatus status, |
| 292 scoped_ptr<ScopedVector<BackgroundSyncRegistration>> registrations); | 292 std::unique_ptr<ScopedVector<BackgroundSyncRegistration>> registrations); |
| 293 base::Closure MakeEmptyCompletion(); | 293 base::Closure MakeEmptyCompletion(); |
| 294 base::Closure MakeClosureCompletion(const base::Closure& callback); | 294 base::Closure MakeClosureCompletion(const base::Closure& callback); |
| 295 StatusAndRegistrationCallback MakeStatusAndRegistrationCompletion( | 295 StatusAndRegistrationCallback MakeStatusAndRegistrationCompletion( |
| 296 const StatusAndRegistrationCallback& callback); | 296 const StatusAndRegistrationCallback& callback); |
| 297 StatusAndRegistrationsCallback MakeStatusAndRegistrationsCompletion( | 297 StatusAndRegistrationsCallback MakeStatusAndRegistrationsCompletion( |
| 298 const StatusAndRegistrationsCallback& callback); | 298 const StatusAndRegistrationsCallback& callback); |
| 299 BackgroundSyncManager::StatusCallback MakeStatusCompletion( | 299 BackgroundSyncManager::StatusCallback MakeStatusCompletion( |
| 300 const StatusCallback& callback); | 300 const StatusCallback& callback); |
| 301 | 301 |
| 302 SWIdToRegistrationsMap active_registrations_; | 302 SWIdToRegistrationsMap active_registrations_; |
| 303 CacheStorageScheduler op_scheduler_; | 303 CacheStorageScheduler op_scheduler_; |
| 304 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; | 304 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; |
| 305 | 305 |
| 306 scoped_ptr<BackgroundSyncParameters> parameters_; | 306 std::unique_ptr<BackgroundSyncParameters> parameters_; |
| 307 | 307 |
| 308 // True if the manager is disabled and registrations should fail. | 308 // True if the manager is disabled and registrations should fail. |
| 309 bool disabled_; | 309 bool disabled_; |
| 310 | 310 |
| 311 // The number of registrations currently in the firing state. | 311 // The number of registrations currently in the firing state. |
| 312 int num_firing_registrations_; | 312 int num_firing_registrations_; |
| 313 | 313 |
| 314 base::CancelableCallback<void()> delayed_sync_task_; | 314 base::CancelableCallback<void()> delayed_sync_task_; |
| 315 | 315 |
| 316 scoped_ptr<BackgroundSyncNetworkObserver> network_observer_; | 316 std::unique_ptr<BackgroundSyncNetworkObserver> network_observer_; |
| 317 | 317 |
| 318 scoped_ptr<base::Clock> clock_; | 318 std::unique_ptr<base::Clock> clock_; |
| 319 | 319 |
| 320 base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_; | 320 base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_; |
| 321 | 321 |
| 322 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager); | 322 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager); |
| 323 }; | 323 }; |
| 324 | 324 |
| 325 } // namespace content | 325 } // namespace content |
| 326 | 326 |
| 327 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ | 327 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ |
| OLD | NEW |