| 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 <list> | 8 #include <list> |
| 9 #include <map> | 9 #include <map> |
| 10 | 10 |
| 11 #include "base/callback_forward.h" | 11 #include "base/callback_forward.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "content/browser/cache_storage/cache_storage_scheduler.h" | 14 #include "content/browser/cache_storage/cache_storage_scheduler.h" |
| 15 #include "content/browser/service_worker/service_worker_context_observer.h" |
| 15 #include "content/browser/service_worker/service_worker_storage.h" | 16 #include "content/browser/service_worker/service_worker_storage.h" |
| 16 #include "content/common/content_export.h" | 17 #include "content/common/content_export.h" |
| 17 #include "content/common/service_worker/service_worker_status_code.h" | 18 #include "content/common/service_worker/service_worker_status_code.h" |
| 18 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 19 | 20 |
| 20 namespace content { | 21 namespace content { |
| 21 | 22 |
| 22 class ServiceWorkerContextWrapper; | 23 class ServiceWorkerContextWrapper; |
| 23 | 24 |
| 24 // BackgroundSyncManager manages and stores the set of background sync | 25 // BackgroundSyncManager manages and stores the set of background sync |
| 25 // registrations across all registered service workers for a profile. | 26 // registrations across all registered service workers for a profile. |
| 26 // Registrations are stored along with their associated Service Worker | 27 // Registrations are stored along with their associated Service Worker |
| 27 // registration in ServiceWorkerStorage. If the ServiceWorker is unregistered, | 28 // registration in ServiceWorkerStorage. If the ServiceWorker is unregistered, |
| 28 // the sync registrations are removed. This class expects to be run on the IO | 29 // the sync registrations are removed. This class expects to be run on the IO |
| 29 // thread. The asynchronous methods are executed sequentially. | 30 // thread. The asynchronous methods are executed sequentially. |
| 30 | 31 |
| 31 // TODO(jkarlin): Check permissions when registering, scheduling, and firing | 32 // TODO(jkarlin): Check permissions when registering, scheduling, and firing |
| 32 // background sync. In the meantime, --enable-service-worker-sync is required to | 33 // background sync. In the meantime, --enable-service-worker-sync is required to |
| 33 // fire a sync event. | 34 // fire a sync event. |
| 34 // TODO(jkarlin): Unregister syncs when permission is revoked. | 35 // TODO(jkarlin): Unregister syncs when permission is revoked. |
| 35 // TODO(jkarlin): Create a background sync scheduler to actually run the | 36 // TODO(jkarlin): Create a background sync scheduler to actually run the |
| 36 // registered events. | 37 // registered events. |
| 37 // TODO(jkarlin): Keep the browser alive if "Let Google Chrome Run in the | 38 // TODO(jkarlin): Keep the browser alive if "Let Google Chrome Run in the |
| 38 // Background" is true and a sync is registered. | 39 // Background" is true and a sync is registered. |
| 39 // TODO(jkarlin): Unregister syncs when storage for an origin is cleared. | 40 class CONTENT_EXPORT BackgroundSyncManager |
| 40 // TODO(jkarlin): Detect and handle a corrupt or broken backend. | 41 : NON_EXPORTED_BASE(public ServiceWorkerContextObserver) { |
| 41 class CONTENT_EXPORT BackgroundSyncManager { | |
| 42 public: | 42 public: |
| 43 enum ErrorType { | 43 enum ErrorType { |
| 44 ERROR_TYPE_OK = 0, | 44 ERROR_TYPE_OK = 0, |
| 45 ERROR_TYPE_STORAGE, | 45 ERROR_TYPE_STORAGE, |
| 46 ERROR_TYPE_NOT_FOUND | 46 ERROR_TYPE_NOT_FOUND |
| 47 }; | 47 }; |
| 48 | 48 |
| 49 // TODO(jkarlin): Remove this and use the struct from IPC messages once it | 49 // TODO(jkarlin): Remove this and use the struct from IPC messages once it |
| 50 // lands. | 50 // lands. |
| 51 struct CONTENT_EXPORT BackgroundSyncRegistration { | 51 struct CONTENT_EXPORT BackgroundSyncRegistration { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 81 NameToRegistrationMap name_to_registration_map; | 81 NameToRegistrationMap name_to_registration_map; |
| 82 BackgroundSyncRegistration::RegistrationId next_id; | 82 BackgroundSyncRegistration::RegistrationId next_id; |
| 83 }; | 83 }; |
| 84 | 84 |
| 85 using StatusCallback = base::Callback<void(ErrorType)>; | 85 using StatusCallback = base::Callback<void(ErrorType)>; |
| 86 using StatusAndRegistrationCallback = | 86 using StatusAndRegistrationCallback = |
| 87 base::Callback<void(ErrorType, const BackgroundSyncRegistration&)>; | 87 base::Callback<void(ErrorType, const BackgroundSyncRegistration&)>; |
| 88 | 88 |
| 89 static scoped_ptr<BackgroundSyncManager> Create( | 89 static scoped_ptr<BackgroundSyncManager> Create( |
| 90 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context); | 90 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context); |
| 91 virtual ~BackgroundSyncManager(); | 91 ~BackgroundSyncManager() override; |
| 92 | 92 |
| 93 // Stores the given background sync registration and adds it to the scheduling | 93 // Stores the given background sync registration and adds it to the scheduling |
| 94 // queue. Overwrites any existing registration with the same name but | 94 // queue. Overwrites any existing registration with the same name but |
| 95 // different parameters (other than the id). Calls |callback| with ErrorTypeOK | 95 // different parameters (other than the id). Calls |callback| with ErrorTypeOK |
| 96 // and the accepted registration on success. The accepted registration will | 96 // and the accepted registration on success. The accepted registration will |
| 97 // have a unique id. It may also have altered parameters if the user or UA | 97 // have a unique id. It may also have altered parameters if the user or UA |
| 98 // chose different parameters than those supplied. | 98 // chose different parameters than those supplied. |
| 99 void Register(const GURL& origin, | 99 void Register(const GURL& origin, |
| 100 int64 sw_registration_id, | 100 int64 sw_registration_id, |
| 101 const BackgroundSyncRegistration& sync_registration, | 101 const BackgroundSyncRegistration& sync_registration, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 114 const StatusCallback& callback); | 114 const StatusCallback& callback); |
| 115 | 115 |
| 116 // Finds the background sync registration associated with | 116 // Finds the background sync registration associated with |
| 117 // |sw_registration_id|. Calls |callback| with ErrorTypeNotFound if it doesn't | 117 // |sw_registration_id|. Calls |callback| with ErrorTypeNotFound if it doesn't |
| 118 // exist. Calls |callback| with ErrorTypeOK on success. | 118 // exist. Calls |callback| with ErrorTypeOK on success. |
| 119 void GetRegistration(const GURL& origin, | 119 void GetRegistration(const GURL& origin, |
| 120 int64 sw_registration_id, | 120 int64 sw_registration_id, |
| 121 const std::string sync_registration_name, | 121 const std::string sync_registration_name, |
| 122 const StatusAndRegistrationCallback& callback); | 122 const StatusAndRegistrationCallback& callback); |
| 123 | 123 |
| 124 // ServiceWorkerContextObserver overrides. |
| 125 void OnRegistrationDeleted(int64 registration_id, |
| 126 const GURL& pattern) override; |
| 127 void OnStorageWiped() override; |
| 128 |
| 124 protected: | 129 protected: |
| 125 explicit BackgroundSyncManager( | 130 explicit BackgroundSyncManager( |
| 126 const scoped_refptr<ServiceWorkerContextWrapper>& context); | 131 const scoped_refptr<ServiceWorkerContextWrapper>& context); |
| 127 | 132 |
| 128 // Init must be called before any public member function. Only call it once. | 133 // Init must be called before any public member function. Only call it once. |
| 129 void Init(); | 134 void Init(); |
| 130 | 135 |
| 131 // The following methods are virtual for testing. | 136 // The following methods are virtual for testing. |
| 132 virtual void StoreDataInBackend( | 137 virtual void StoreDataInBackend( |
| 133 int64 sw_registration_id, | 138 int64 sw_registration_id, |
| 134 const GURL& origin, | 139 const GURL& origin, |
| 135 const std::string& key, | 140 const std::string& key, |
| 136 const std::string& data, | 141 const std::string& data, |
| 137 const ServiceWorkerStorage::StatusCallback& callback); | 142 const ServiceWorkerStorage::StatusCallback& callback); |
| 138 virtual void GetDataFromBackend( | 143 virtual void GetDataFromBackend( |
| 139 const std::string& key, | 144 const std::string& key, |
| 140 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& | 145 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& |
| 141 callback); | 146 callback); |
| 142 | 147 |
| 143 private: | 148 private: |
| 144 using PermissionStatusCallback = base::Callback<void(bool)>; | 149 using PermissionStatusCallback = base::Callback<void(bool)>; |
| 145 using SWIdToRegistrationsMap = std::map<int64, BackgroundSyncRegistrations>; | 150 using SWIdToRegistrationsMap = std::map<int64, BackgroundSyncRegistrations>; |
| 146 | 151 |
| 152 // Disable the manager. Already queued operations will abort once they start |
| 153 // to run (in their impl methods). Future operations will not queue. Any |
| 154 // registrations are cleared from memory and the backend (if it's still |
| 155 // functioning). The manager will reenable itself once it receives the |
| 156 // OnStorageWiped message or on browser restart. |
| 157 void DisableAndClearManager(); |
| 158 void DisableAndClearManagerImpl(const base::Closure& callback); |
| 159 void DisableAndClearDidGetRegistrations( |
| 160 const base::Closure& callback, |
| 161 const std::vector<std::pair<int64, std::string>>& user_data, |
| 162 ServiceWorkerStatusCode status); |
| 163 void DisableAndClearManagerClearedOne(const base::Closure& barrier_closure, |
| 164 ServiceWorkerStatusCode status); |
| 165 |
| 147 // Returns the existing registration in |existing_registration| if it is not | 166 // Returns the existing registration in |existing_registration| if it is not |
| 148 // null. | 167 // null. |
| 149 bool LookupRegistration(int64 sw_registration_id, | 168 bool LookupRegistration(int64 sw_registration_id, |
| 150 const std::string& sync_registration_name, | 169 const std::string& sync_registration_name, |
| 151 BackgroundSyncRegistration* existing_registration); | 170 BackgroundSyncRegistration* existing_registration); |
| 152 | 171 |
| 153 // Store all registrations for a given |sw_registration_id|. | 172 // Store all registrations for a given |sw_registration_id|. |
| 154 void StoreRegistrations(const GURL& origin, | 173 void StoreRegistrations(const GURL& origin, |
| 155 int64 sw_registration_id, | 174 int64 sw_registration_id, |
| 156 const ServiceWorkerStorage::StatusCallback& callback); | 175 const ServiceWorkerStorage::StatusCallback& callback); |
| 157 | 176 |
| 158 // If the registration is in the map, removes it and returns the removed | 177 // If the registration is in the map, removes it and returns the removed |
| 159 // registration in |old_registration|. |old_registration| may be null. | 178 // registration in |old_registration|. |old_registration| may be null. |
| 160 void RemoveRegistrationFromMap(int64 sw_registration_id, | 179 void RemoveRegistrationFromMap(int64 sw_registration_id, |
| 161 const std::string& sync_registration_name, | 180 const std::string& sync_registration_name, |
| 162 BackgroundSyncRegistration* old_registration); | 181 BackgroundSyncRegistration* old_registration); |
| 163 | 182 |
| 164 void AddRegistrationToMap( | 183 void AddRegistrationToMap( |
| 165 int64 sw_registration_id, | 184 int64 sw_registration_id, |
| 166 const BackgroundSyncRegistration& sync_registration); | 185 const BackgroundSyncRegistration& sync_registration); |
| 167 | 186 |
| 168 void InitImpl(); | 187 void InitImpl(const base::Closure& callback); |
| 169 void InitDidGetDataFromBackend( | 188 void InitDidGetDataFromBackend( |
| 189 const base::Closure& callback, |
| 170 const std::vector<std::pair<int64, std::string>>& user_data, | 190 const std::vector<std::pair<int64, std::string>>& user_data, |
| 171 ServiceWorkerStatusCode status); | 191 ServiceWorkerStatusCode status); |
| 172 | 192 |
| 173 // Register callbacks | 193 // Register callbacks |
| 174 void RegisterImpl(const GURL& origin, | 194 void RegisterImpl(const GURL& origin, |
| 175 int64 sw_registration_id, | 195 int64 sw_registration_id, |
| 176 const BackgroundSyncRegistration& sync_registration, | 196 const BackgroundSyncRegistration& sync_registration, |
| 177 const StatusAndRegistrationCallback& callback); | 197 const StatusAndRegistrationCallback& callback); |
| 178 void RegisterDidStore(int64 sw_registration_id, | 198 void RegisterDidStore(int64 sw_registration_id, |
| 179 const BackgroundSyncRegistration& sync_registration, | 199 const BackgroundSyncRegistration& sync_registration, |
| 180 const BackgroundSyncRegistration& previous_registration, | |
| 181 const StatusAndRegistrationCallback& callback, | 200 const StatusAndRegistrationCallback& callback, |
| 182 ServiceWorkerStatusCode status); | 201 ServiceWorkerStatusCode status); |
| 183 | 202 |
| 184 // Unregister callbacks | 203 // Unregister callbacks |
| 185 void UnregisterImpl( | 204 void UnregisterImpl( |
| 186 const GURL& origin, | 205 const GURL& origin, |
| 187 int64 sw_registration_id, | 206 int64 sw_registration_id, |
| 188 const std::string& sync_registration_name, | 207 const std::string& sync_registration_name, |
| 189 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 208 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| 190 const StatusCallback& callback); | 209 const StatusCallback& callback); |
| 191 void UnregisterDidStore( | 210 void UnregisterDidStore( |
| 192 int64 sw_registration_id, | 211 int64 sw_registration_id, |
| 193 const BackgroundSyncRegistration& old_sync_registration, | 212 const BackgroundSyncRegistration& old_sync_registration, |
| 194 const StatusCallback& callback, | 213 const StatusCallback& callback, |
| 195 ServiceWorkerStatusCode status); | 214 ServiceWorkerStatusCode status); |
| 196 | 215 |
| 197 // GetRegistration callbacks | 216 // GetRegistration callbacks |
| 198 void GetRegistrationImpl(const GURL& origin, | 217 void GetRegistrationImpl(const GURL& origin, |
| 199 int64 sw_registration_id, | 218 int64 sw_registration_id, |
| 200 const std::string sync_registration_name, | 219 const std::string sync_registration_name, |
| 201 const StatusAndRegistrationCallback& callback); | 220 const StatusAndRegistrationCallback& callback); |
| 202 | 221 |
| 222 // OnRegistrationDeleted callbacks |
| 223 void OnRegistrationDeletedImpl(int64 registration_id, |
| 224 const base::Closure& callback); |
| 225 |
| 226 // OnStorageWiped callbacks |
| 227 void OnStorageWipedImpl(const base::Closure& callback); |
| 228 |
| 203 // Operation Scheduling callbacks | 229 // Operation Scheduling callbacks |
| 204 void PendingStatusAndRegistrationCallback( | 230 void PendingStatusAndRegistrationCallback( |
| 205 const StatusAndRegistrationCallback& callback, | 231 const StatusAndRegistrationCallback& callback, |
| 206 ErrorType error, | 232 ErrorType error, |
| 207 const BackgroundSyncRegistration& sync_registration); | 233 const BackgroundSyncRegistration& sync_registration); |
| 208 void PendingStatusCallback(const StatusCallback& callback, ErrorType error); | 234 void PendingStatusCallback(const StatusCallback& callback, ErrorType error); |
| 235 void PendingClosure(const base::Closure& closure); |
| 209 | 236 |
| 210 SWIdToRegistrationsMap sw_to_registrations_map_; | 237 SWIdToRegistrationsMap sw_to_registrations_map_; |
| 211 CacheStorageScheduler op_scheduler_; | 238 CacheStorageScheduler op_scheduler_; |
| 212 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; | 239 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; |
| 240 bool disabled_; |
| 213 | 241 |
| 214 base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_; | 242 base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_; |
| 215 | 243 |
| 216 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager); | 244 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager); |
| 217 }; | 245 }; |
| 218 | 246 |
| 219 } // namespace content | 247 } // namespace content |
| 220 | 248 |
| 221 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ | 249 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ |
| OLD | NEW |