| 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 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 }; | 48 }; |
| 49 | 49 |
| 50 // TODO(jkarlin): Remove this and use the struct from IPC messages once it | 50 // TODO(jkarlin): Remove this and use the struct from IPC messages once it |
| 51 // lands. | 51 // lands. |
| 52 struct CONTENT_EXPORT BackgroundSyncRegistration { | 52 struct CONTENT_EXPORT BackgroundSyncRegistration { |
| 53 using RegistrationId = int64; | 53 using RegistrationId = int64; |
| 54 static const RegistrationId kInvalidRegistrationId; | 54 static const RegistrationId kInvalidRegistrationId; |
| 55 BackgroundSyncRegistration() {} | 55 BackgroundSyncRegistration() {} |
| 56 | 56 |
| 57 bool Equals(const BackgroundSyncRegistration& other) { | 57 bool Equals(const BackgroundSyncRegistration& other) { |
| 58 return this->name == other.name && this->fire_once == other.fire_once && | 58 return this->tag == other.tag && this->fire_once == other.fire_once && |
| 59 this->min_period == other.min_period && | 59 this->min_period == other.min_period && |
| 60 network_state == other.network_state && | 60 network_state == other.network_state && |
| 61 power_state == other.power_state; | 61 power_state == other.power_state; |
| 62 } | 62 } |
| 63 | 63 |
| 64 RegistrationId id = kInvalidRegistrationId; | 64 RegistrationId id = kInvalidRegistrationId; |
| 65 std::string name; | 65 std::string tag; |
| 66 bool fire_once = true; | 66 bool fire_once = true; |
| 67 int64 min_period = 0; | 67 int64 min_period = 0; |
| 68 SyncNetworkState network_state = NETWORK_STATE_ONLINE; | 68 SyncNetworkState network_state = NETWORK_STATE_ONLINE; |
| 69 SyncPowerState power_state = POWER_STATE_AVOID_DRAINING; | 69 SyncPowerState power_state = POWER_STATE_AVOID_DRAINING; |
| 70 }; | 70 }; |
| 71 | 71 |
| 72 struct CONTENT_EXPORT BackgroundSyncRegistrations { | 72 struct CONTENT_EXPORT BackgroundSyncRegistrations { |
| 73 using NameToRegistrationMap = | 73 using TagToRegistrationMap = |
| 74 std::map<std::string, BackgroundSyncRegistration>; | 74 std::map<std::string, BackgroundSyncRegistration>; |
| 75 static const BackgroundSyncRegistration::RegistrationId kInitialId; | 75 static const BackgroundSyncRegistration::RegistrationId kInitialId; |
| 76 | 76 |
| 77 BackgroundSyncRegistrations(); | 77 BackgroundSyncRegistrations(); |
| 78 explicit BackgroundSyncRegistrations( | 78 explicit BackgroundSyncRegistrations( |
| 79 BackgroundSyncRegistration::RegistrationId next_id); | 79 BackgroundSyncRegistration::RegistrationId next_id); |
| 80 ~BackgroundSyncRegistrations(); | 80 ~BackgroundSyncRegistrations(); |
| 81 | 81 |
| 82 NameToRegistrationMap name_to_registration_map; | 82 TagToRegistrationMap tag_to_registration_map; |
| 83 BackgroundSyncRegistration::RegistrationId next_id; | 83 BackgroundSyncRegistration::RegistrationId next_id; |
| 84 }; | 84 }; |
| 85 | 85 |
| 86 using StatusCallback = base::Callback<void(ErrorType)>; | 86 using StatusCallback = base::Callback<void(ErrorType)>; |
| 87 using StatusAndRegistrationCallback = | 87 using StatusAndRegistrationCallback = |
| 88 base::Callback<void(ErrorType, const BackgroundSyncRegistration&)>; | 88 base::Callback<void(ErrorType, const BackgroundSyncRegistration&)>; |
| 89 | 89 |
| 90 static scoped_ptr<BackgroundSyncManager> Create( | 90 static scoped_ptr<BackgroundSyncManager> Create( |
| 91 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context); | 91 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context); |
| 92 ~BackgroundSyncManager() override; | 92 ~BackgroundSyncManager() override; |
| 93 | 93 |
| 94 // Stores the given background sync registration and adds it to the scheduling | 94 // Stores the given background sync registration and adds it to the scheduling |
| 95 // queue. Overwrites any existing registration with the same name but | 95 // queue. Overwrites any existing registration with the same tag but |
| 96 // different parameters (other than the id). Calls |callback| with ErrorTypeOK | 96 // different parameters (other than the id). Calls |callback| with ErrorTypeOK |
| 97 // and the accepted registration on success. The accepted registration will | 97 // and the accepted registration on success. The accepted registration will |
| 98 // have a unique id. It may also have altered parameters if the user or UA | 98 // have a unique id. It may also have altered parameters if the user or UA |
| 99 // chose different parameters than those supplied. | 99 // chose different parameters than those supplied. |
| 100 void Register(const GURL& origin, | 100 void Register(const GURL& origin, |
| 101 int64 sw_registration_id, | 101 int64 sw_registration_id, |
| 102 const BackgroundSyncRegistration& sync_registration, | 102 const BackgroundSyncRegistration& sync_registration, |
| 103 const StatusAndRegistrationCallback& callback); | 103 const StatusAndRegistrationCallback& callback); |
| 104 | 104 |
| 105 // Removes the background sync registration with |sync_registration_name| if | 105 // Removes the background sync registration with |sync_registration_tag| if |
| 106 // the |sync_registration_id| matches. |sync_registration_id| will not match | 106 // the |sync_registration_id| matches. |sync_registration_id| will not match |
| 107 // if, for instance, a new registration with the same name has replaced it. | 107 // if, for instance, a new registration with the same tag has replaced it. |
| 108 // Calls |callback| with ErrorTypeNotFound if no match is found. Calls | 108 // Calls |callback| with ErrorTypeNotFound if no match is found. Calls |
| 109 // |callback| with ErrorTypeOK on success. | 109 // |callback| with ErrorTypeOK on success. |
| 110 void Unregister( | 110 void Unregister( |
| 111 const GURL& origin, | 111 const GURL& origin, |
| 112 int64 sw_registration_id, | 112 int64 sw_registration_id, |
| 113 const std::string& sync_registration_name, | 113 const std::string& sync_registration_tag, |
| 114 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 114 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| 115 const StatusCallback& callback); | 115 const StatusCallback& callback); |
| 116 | 116 |
| 117 // Finds the background sync registration associated with | 117 // Finds the background sync registration associated with |
| 118 // |sw_registration_id|. Calls |callback| with ErrorTypeNotFound if it doesn't | 118 // |sw_registration_id|. Calls |callback| with ErrorTypeNotFound if it doesn't |
| 119 // exist. Calls |callback| with ErrorTypeOK on success. | 119 // exist. Calls |callback| with ErrorTypeOK on success. |
| 120 void GetRegistration(const GURL& origin, | 120 void GetRegistration(const GURL& origin, |
| 121 int64 sw_registration_id, | 121 int64 sw_registration_id, |
| 122 const std::string sync_registration_name, | 122 const std::string sync_registration_tag, |
| 123 const StatusAndRegistrationCallback& callback); | 123 const StatusAndRegistrationCallback& callback); |
| 124 | 124 |
| 125 // ServiceWorkerContextObserver overrides. | 125 // ServiceWorkerContextObserver overrides. |
| 126 void OnRegistrationDeleted(int64 registration_id, | 126 void OnRegistrationDeleted(int64 registration_id, |
| 127 const GURL& pattern) override; | 127 const GURL& pattern) override; |
| 128 void OnStorageWiped() override; | 128 void OnStorageWiped() override; |
| 129 | 129 |
| 130 protected: | 130 protected: |
| 131 explicit BackgroundSyncManager( | 131 explicit BackgroundSyncManager( |
| 132 const scoped_refptr<ServiceWorkerContextWrapper>& context); | 132 const scoped_refptr<ServiceWorkerContextWrapper>& context); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 159 void DisableAndClearDidGetRegistrations( | 159 void DisableAndClearDidGetRegistrations( |
| 160 const base::Closure& callback, | 160 const base::Closure& callback, |
| 161 const std::vector<std::pair<int64, std::string>>& user_data, | 161 const std::vector<std::pair<int64, std::string>>& user_data, |
| 162 ServiceWorkerStatusCode status); | 162 ServiceWorkerStatusCode status); |
| 163 void DisableAndClearManagerClearedOne(const base::Closure& barrier_closure, | 163 void DisableAndClearManagerClearedOne(const base::Closure& barrier_closure, |
| 164 ServiceWorkerStatusCode status); | 164 ServiceWorkerStatusCode status); |
| 165 | 165 |
| 166 // Returns the existing registration in |existing_registration| if it is not | 166 // Returns the existing registration in |existing_registration| if it is not |
| 167 // null. | 167 // null. |
| 168 bool LookupRegistration(int64 sw_registration_id, | 168 bool LookupRegistration(int64 sw_registration_id, |
| 169 const std::string& sync_registration_name, | 169 const std::string& sync_registration_tag, |
| 170 BackgroundSyncRegistration* existing_registration); | 170 BackgroundSyncRegistration* existing_registration); |
| 171 | 171 |
| 172 // Store all registrations for a given |sw_registration_id|. | 172 // Store all registrations for a given |sw_registration_id|. |
| 173 void StoreRegistrations(const GURL& origin, | 173 void StoreRegistrations(const GURL& origin, |
| 174 int64 sw_registration_id, | 174 int64 sw_registration_id, |
| 175 const ServiceWorkerStorage::StatusCallback& callback); | 175 const ServiceWorkerStorage::StatusCallback& callback); |
| 176 | 176 |
| 177 // Removes the registration if it is in the map. | 177 // Removes the registration if it is in the map. |
| 178 void RemoveRegistrationFromMap(int64 sw_registration_id, | 178 void RemoveRegistrationFromMap(int64 sw_registration_id, |
| 179 const std::string& sync_registration_name); | 179 const std::string& sync_registration_tag); |
| 180 | 180 |
| 181 void AddRegistrationToMap( | 181 void AddRegistrationToMap( |
| 182 int64 sw_registration_id, | 182 int64 sw_registration_id, |
| 183 const BackgroundSyncRegistration& sync_registration); | 183 const BackgroundSyncRegistration& sync_registration); |
| 184 | 184 |
| 185 void InitImpl(const base::Closure& callback); | 185 void InitImpl(const base::Closure& callback); |
| 186 void InitDidGetDataFromBackend( | 186 void InitDidGetDataFromBackend( |
| 187 const base::Closure& callback, | 187 const base::Closure& callback, |
| 188 const std::vector<std::pair<int64, std::string>>& user_data, | 188 const std::vector<std::pair<int64, std::string>>& user_data, |
| 189 ServiceWorkerStatusCode status); | 189 ServiceWorkerStatusCode status); |
| 190 | 190 |
| 191 // Register callbacks | 191 // Register callbacks |
| 192 void RegisterImpl(const GURL& origin, | 192 void RegisterImpl(const GURL& origin, |
| 193 int64 sw_registration_id, | 193 int64 sw_registration_id, |
| 194 const BackgroundSyncRegistration& sync_registration, | 194 const BackgroundSyncRegistration& sync_registration, |
| 195 const StatusAndRegistrationCallback& callback); | 195 const StatusAndRegistrationCallback& callback); |
| 196 void RegisterDidStore(int64 sw_registration_id, | 196 void RegisterDidStore(int64 sw_registration_id, |
| 197 const BackgroundSyncRegistration& sync_registration, | 197 const BackgroundSyncRegistration& sync_registration, |
| 198 const StatusAndRegistrationCallback& callback, | 198 const StatusAndRegistrationCallback& callback, |
| 199 ServiceWorkerStatusCode status); | 199 ServiceWorkerStatusCode status); |
| 200 | 200 |
| 201 // Unregister callbacks | 201 // Unregister callbacks |
| 202 void UnregisterImpl( | 202 void UnregisterImpl( |
| 203 const GURL& origin, | 203 const GURL& origin, |
| 204 int64 sw_registration_id, | 204 int64 sw_registration_id, |
| 205 const std::string& sync_registration_name, | 205 const std::string& sync_registration_tag, |
| 206 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 206 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| 207 const StatusCallback& callback); | 207 const StatusCallback& callback); |
| 208 void UnregisterDidStore( | 208 void UnregisterDidStore( |
| 209 int64 sw_registration_id, | 209 int64 sw_registration_id, |
| 210 const StatusCallback& callback, | 210 const StatusCallback& callback, |
| 211 ServiceWorkerStatusCode status); | 211 ServiceWorkerStatusCode status); |
| 212 | 212 |
| 213 // GetRegistration callbacks | 213 // GetRegistration callbacks |
| 214 void GetRegistrationImpl(const GURL& origin, | 214 void GetRegistrationImpl(const GURL& origin, |
| 215 int64 sw_registration_id, | 215 int64 sw_registration_id, |
| 216 const std::string sync_registration_name, | 216 const std::string sync_registration_tag, |
| 217 const StatusAndRegistrationCallback& callback); | 217 const StatusAndRegistrationCallback& callback); |
| 218 | 218 |
| 219 // OnRegistrationDeleted callbacks | 219 // OnRegistrationDeleted callbacks |
| 220 void OnRegistrationDeletedImpl(int64 registration_id, | 220 void OnRegistrationDeletedImpl(int64 registration_id, |
| 221 const base::Closure& callback); | 221 const base::Closure& callback); |
| 222 | 222 |
| 223 // OnStorageWiped callbacks | 223 // OnStorageWiped callbacks |
| 224 void OnStorageWipedImpl(const base::Closure& callback); | 224 void OnStorageWipedImpl(const base::Closure& callback); |
| 225 | 225 |
| 226 // Operation Scheduling callbacks | 226 // Operation Scheduling callbacks |
| (...skipping 16 matching lines...) Expand all Loading... |
| 243 bool disabled_; | 243 bool disabled_; |
| 244 | 244 |
| 245 base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_; | 245 base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_; |
| 246 | 246 |
| 247 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager); | 247 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager); |
| 248 }; | 248 }; |
| 249 | 249 |
| 250 } // namespace content | 250 } // namespace content |
| 251 | 251 |
| 252 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ | 252 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ |
| OLD | NEW |