Chromium Code Reviews| Index: content/browser/background_sync/background_sync_manager.h |
| diff --git a/content/browser/background_sync/background_sync_manager.h b/content/browser/background_sync/background_sync_manager.h |
| index 84353f906328115ca2abc077f8c52b52aaf39be4..e3153ced5a8f50efa2df79dfb4e858493860fad3 100644 |
| --- a/content/browser/background_sync/background_sync_manager.h |
| +++ b/content/browser/background_sync/background_sync_manager.h |
| @@ -27,6 +27,7 @@ namespace content { |
| class BackgroundSyncNetworkObserver; |
| class BackgroundSyncPowerObserver; |
| +class ClientBackgroundSyncRegistration; |
| class ServiceWorkerContextWrapper; |
| // BackgroundSyncManager manages and stores the set of background sync |
| @@ -50,10 +51,10 @@ class CONTENT_EXPORT BackgroundSyncManager |
| using StatusCallback = base::Callback<void(BackgroundSyncStatus)>; |
| using StatusAndRegistrationCallback = |
| base::Callback<void(BackgroundSyncStatus, |
| - const BackgroundSyncRegistration&)>; |
| - using StatusAndRegistrationsCallback = |
| - base::Callback<void(BackgroundSyncStatus, |
| - const std::vector<BackgroundSyncRegistration>&)>; |
| + scoped_ptr<ClientBackgroundSyncRegistration>)>; |
| + using StatusAndRegistrationsCallback = base::Callback<void( |
| + BackgroundSyncStatus, |
| + scoped_ptr<ScopedVector<ClientBackgroundSyncRegistration>>)>; |
| static scoped_ptr<BackgroundSyncManager> Create( |
| const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context); |
| @@ -70,17 +71,6 @@ class CONTENT_EXPORT BackgroundSyncManager |
| const BackgroundSyncRegistrationOptions& options, |
| const StatusAndRegistrationCallback& callback); |
| - // Removes the background sync with tag |sync_registration_tag|, periodicity |
| - // |periodicity|, and id |sync_registration_id|. Calls |callback| with |
| - // BACKGROUND_SYNC_STATUS_NOT_FOUND if no match is found. Calls |callback| |
| - // with BACKGROUND_SYNC_STATUS_OK on success. |
| - void Unregister( |
| - int64 sw_registration_id, |
| - const std::string& sync_registration_tag, |
| - SyncPeriodicity periodicity, |
| - BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| - const StatusCallback& callback); |
| - |
| // Finds the background sync registration associated with |
| // |sw_registration_id| with periodicity |periodicity|. Calls |
| // |callback| with BACKGROUND_SYNC_STATUS_NOT_FOUND if it doesn't exist. Calls |
| @@ -100,6 +90,25 @@ class CONTENT_EXPORT BackgroundSyncManager |
| void OnStorageWiped() override; |
| protected: |
| + // A registration might be referenced by the client longer than |
| + // the BackgroundSyncManager needs to keep track of it (e.g., the event has |
| + // finished firing). The BackgroundSyncManager reference counts its |
| + // registrations internally and every ClientBackgroundSyncRegistration has a |
| + // unique registration id which maps to a locally maintained (in |
| + // client_registration_ids_) scoped_refptr. |
| + class RefCountedRegistration |
| + : public base::RefCounted<RefCountedRegistration> { |
| + public: |
| + BackgroundSyncRegistration* value() { return ®istration_; } |
| + const BackgroundSyncRegistration* value() const { return ®istration_; } |
| + |
| + private: |
| + friend class base::RefCounted<RefCountedRegistration>; |
| + virtual ~RefCountedRegistration() {} |
| + |
| + BackgroundSyncRegistration registration_; |
| + }; |
| + |
| explicit BackgroundSyncManager( |
| const scoped_refptr<ServiceWorkerContextWrapper>& context); |
| @@ -118,11 +127,13 @@ class CONTENT_EXPORT BackgroundSyncManager |
| const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& |
| callback); |
| virtual void FireOneShotSync( |
| - const BackgroundSyncRegistration& registration, |
| + const RefCountedRegistration& registration, |
| const scoped_refptr<ServiceWorkerVersion>& active_version, |
| const ServiceWorkerVersion::StatusCallback& callback); |
| private: |
| + friend class ClientBackgroundSyncRegistration; |
| + |
| class RegistrationKey { |
| public: |
| explicit RegistrationKey(const BackgroundSyncRegistration& registration); |
| @@ -141,7 +152,7 @@ class CONTENT_EXPORT BackgroundSyncManager |
| struct BackgroundSyncRegistrations { |
| using RegistrationMap = |
| - std::map<RegistrationKey, BackgroundSyncRegistration>; |
| + std::map<RegistrationKey, scoped_refptr<RefCountedRegistration>>; |
| BackgroundSyncRegistrations(); |
| ~BackgroundSyncRegistrations(); |
| @@ -154,6 +165,19 @@ class CONTENT_EXPORT BackgroundSyncManager |
| using PermissionStatusCallback = base::Callback<void(bool)>; |
| using SWIdToRegistrationsMap = std::map<int64, BackgroundSyncRegistrations>; |
| + // Returns true if the BackgrounjdSyncManager is tracking a client |
| + // registration |
| + // with id |sync_id|. |
|
iclelland
2015/08/17 19:22:02
Random line break
jkarlin
2015/08/18 15:12:38
Done.
|
| + bool HasClientRegistration( |
| + BackgroundSyncRegistration::RegistrationId sync_id) const; |
| + |
| + // The BackgroundSyncManager holds references to registrations that have |
| + // active ClientBackgroundSyncRegistrations. ClientBackgroundSyncRegistrations |
| + // call this function on deletion so that the manager can free up old |
| + // registrations. |
| + void ReleaseClientRegistration( |
| + BackgroundSyncRegistration::RegistrationId sync_id); |
| + |
| // Disable the manager. Already queued operations will abort once they start |
| // to run (in their impl methods). Future operations will not queue. Any |
| // registrations are cleared from memory and the backend (if it's still |
| @@ -169,7 +193,7 @@ class CONTENT_EXPORT BackgroundSyncManager |
| // Returns the existing registration in |existing_registration| if it is not |
| // null. |
| - BackgroundSyncRegistration* LookupRegistration( |
| + RefCountedRegistration* LookupRegistration( |
| int64 sw_registration_id, |
| const RegistrationKey& registration_key); |
| @@ -184,7 +208,7 @@ class CONTENT_EXPORT BackgroundSyncManager |
| void AddRegistrationToMap( |
| int64 sw_registration_id, |
| const GURL& origin, |
| - const BackgroundSyncRegistration& sync_registration); |
| + const scoped_refptr<RefCountedRegistration>& sync_registration); |
| void InitImpl(const base::Closure& callback); |
| void InitDidGetDataFromBackend( |
| @@ -196,17 +220,25 @@ class CONTENT_EXPORT BackgroundSyncManager |
| void RegisterImpl(int64 sw_registration_id, |
| const BackgroundSyncRegistrationOptions& options, |
| const StatusAndRegistrationCallback& callback); |
| - void RegisterDidStore(int64 sw_registration_id, |
| - const BackgroundSyncRegistration& sync_registration, |
| - const StatusAndRegistrationCallback& callback, |
| - ServiceWorkerStatusCode status); |
| + void RegisterDidStore( |
| + int64 sw_registration_id, |
| + const scoped_refptr<RefCountedRegistration>& new_registration_ref, |
| + const StatusAndRegistrationCallback& callback, |
| + ServiceWorkerStatusCode status); |
| - // Unregister callbacks |
| - void UnregisterImpl( |
| + // Removes the background sync with periodicity |periodicity| and id |
| + // |sync_registration_id|. Calls |callback| with |
| + // BACKGROUND_SYNC_STATUS_NOT_FOUND if no match is found. Calls |callback| |
| + // with BACKGROUND_SYNC_STATUS_OK on success. |
| + void Unregister( |
| int64 sw_registration_id, |
| - const RegistrationKey& registration_key, |
| + SyncPeriodicity periodicity, |
| BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| + const StatusCallback& callback); |
| + void UnregisterImpl( |
| + int64 sw_registration_id, |
| SyncPeriodicity periodicity, |
| + BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| const StatusCallback& callback); |
| void UnregisterDidStore(int64 sw_registration_id, |
| SyncPeriodicity periodicity, |
| @@ -275,6 +307,9 @@ class CONTENT_EXPORT BackgroundSyncManager |
| // OnStorageWiped callbacks |
| void OnStorageWipedImpl(const base::Closure& callback); |
| + scoped_ptr<ClientBackgroundSyncRegistration> CreateClientRegistration( |
| + const RefCountedRegistration& registration); |
| + |
| void OnNetworkChanged(); |
| void OnPowerChanged(); |
| @@ -282,6 +317,14 @@ class CONTENT_EXPORT BackgroundSyncManager |
| template <typename CallbackT, typename... Params> |
| void CompleteOperationCallback(const CallbackT& callback, |
| Params... parameters); |
| + void CompleteStatusAndRegistrationCallback( |
| + StatusAndRegistrationCallback callback, |
| + BackgroundSyncStatus status, |
| + scoped_ptr<ClientBackgroundSyncRegistration> result); |
| + void CompleteStatusAndRegistrationsCallback( |
| + StatusAndRegistrationsCallback callback, |
| + BackgroundSyncStatus status, |
| + scoped_ptr<ScopedVector<ClientBackgroundSyncRegistration>> results); |
| base::Closure MakeEmptyCompletion(); |
| base::Closure MakeClosureCompletion(const base::Closure& callback); |
| StatusAndRegistrationCallback MakeStatusAndRegistrationCompletion( |
| @@ -299,6 +342,10 @@ class CONTENT_EXPORT BackgroundSyncManager |
| scoped_ptr<BackgroundSyncNetworkObserver> network_observer_; |
| scoped_ptr<BackgroundSyncPowerObserver> power_observer_; |
| + // The registrations that clients might still reference. |
| + IDMap<scoped_refptr<const RefCountedRegistration>, IDMapOwnPointer> |
| + client_registration_ids_; |
| + |
| base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_; |
| DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager); |