Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(534)

Side by Side Diff: content/browser/background_sync/background_sync_manager.h

Issue 1282013004: BackgroundSyncManager tracks client registrations (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Clean up Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/callback_forward.h" 13 #include "base/callback_forward.h"
14 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h" 15 #include "base/memory/weak_ptr.h"
16 #include "content/browser/background_sync/background_sync.pb.h" 16 #include "content/browser/background_sync/background_sync.pb.h"
17 #include "content/browser/background_sync/background_sync_registration.h" 17 #include "content/browser/background_sync/background_sync_registration.h"
18 #include "content/browser/background_sync/background_sync_registration_handle.h"
18 #include "content/browser/background_sync/background_sync_status.h" 19 #include "content/browser/background_sync/background_sync_status.h"
19 #include "content/browser/cache_storage/cache_storage_scheduler.h" 20 #include "content/browser/cache_storage/cache_storage_scheduler.h"
20 #include "content/browser/service_worker/service_worker_context_observer.h" 21 #include "content/browser/service_worker/service_worker_context_observer.h"
21 #include "content/browser/service_worker/service_worker_storage.h" 22 #include "content/browser/service_worker/service_worker_storage.h"
22 #include "content/common/content_export.h" 23 #include "content/common/content_export.h"
23 #include "content/common/service_worker/service_worker_status_code.h" 24 #include "content/common/service_worker/service_worker_status_code.h"
24 #include "url/gurl.h" 25 #include "url/gurl.h"
25 26
26 namespace content { 27 namespace content {
27 28
(...skipping 15 matching lines...) Expand all
43 // TODO(jkarlin): Create a background sync scheduler to actually run the 44 // TODO(jkarlin): Create a background sync scheduler to actually run the
44 // registered events. 45 // registered events.
45 // TODO(jkarlin): Keep the browser alive if "Let Google Chrome Run in the 46 // TODO(jkarlin): Keep the browser alive if "Let Google Chrome Run in the
46 // Background" is true and a sync is registered. 47 // Background" is true and a sync is registered.
47 class CONTENT_EXPORT BackgroundSyncManager 48 class CONTENT_EXPORT BackgroundSyncManager
48 : NON_EXPORTED_BASE(public ServiceWorkerContextObserver) { 49 : NON_EXPORTED_BASE(public ServiceWorkerContextObserver) {
49 public: 50 public:
50 using StatusCallback = base::Callback<void(BackgroundSyncStatus)>; 51 using StatusCallback = base::Callback<void(BackgroundSyncStatus)>;
51 using StatusAndRegistrationCallback = 52 using StatusAndRegistrationCallback =
52 base::Callback<void(BackgroundSyncStatus, 53 base::Callback<void(BackgroundSyncStatus,
53 const BackgroundSyncRegistration&)>; 54 scoped_ptr<BackgroundSyncRegistrationHandle>)>;
54 using StatusAndRegistrationsCallback = 55 using StatusAndRegistrationsCallback = base::Callback<void(
55 base::Callback<void(BackgroundSyncStatus, 56 BackgroundSyncStatus,
56 const std::vector<BackgroundSyncRegistration>&)>; 57 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>>)>;
57 58
58 static scoped_ptr<BackgroundSyncManager> Create( 59 static scoped_ptr<BackgroundSyncManager> Create(
59 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context); 60 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context);
60 ~BackgroundSyncManager() override; 61 ~BackgroundSyncManager() override;
61 62
62 // Stores the given background sync registration and adds it to the scheduling 63 // Stores the given background sync registration and adds it to the scheduling
63 // queue. It will overwrite an existing registration with the same tag and 64 // queue. It will overwrite an existing registration with the same tag and
64 // periodicity unless they're identical (save for the id). Calls |callback| 65 // periodicity unless they're identical (save for the id). Calls |callback|
65 // with BACKGROUND_SYNC_STATUS_OK and the accepted registration on success. 66 // with BACKGROUND_SYNC_STATUS_OK and the accepted registration on success.
66 // The accepted registration will have a unique id. It may also have altered 67 // The accepted registration will have a unique id. It may also have altered
67 // parameters if the user or UA chose different parameters than those 68 // parameters if the user or UA chose different parameters than those
68 // supplied. 69 // supplied.
69 void Register(int64 sw_registration_id, 70 void Register(int64 sw_registration_id,
70 const BackgroundSyncRegistrationOptions& options, 71 const BackgroundSyncRegistrationOptions& options,
71 const StatusAndRegistrationCallback& callback); 72 const StatusAndRegistrationCallback& callback);
72 73
73 // Removes the background sync with tag |sync_registration_tag|, periodicity
74 // |periodicity|, and id |sync_registration_id|. Calls |callback| with
75 // BACKGROUND_SYNC_STATUS_NOT_FOUND if no match is found. Calls |callback|
76 // with BACKGROUND_SYNC_STATUS_OK on success.
77 void Unregister(
78 int64 sw_registration_id,
79 const std::string& sync_registration_tag,
80 SyncPeriodicity periodicity,
81 BackgroundSyncRegistration::RegistrationId sync_registration_id,
82 const StatusCallback& callback);
83
84 // Finds the background sync registration associated with 74 // Finds the background sync registration associated with
85 // |sw_registration_id| with periodicity |periodicity|. Calls 75 // |sw_registration_id| with periodicity |periodicity|. Calls
86 // |callback| with BACKGROUND_SYNC_STATUS_NOT_FOUND if it doesn't exist. Calls 76 // |callback| with BACKGROUND_SYNC_STATUS_NOT_FOUND if it doesn't exist. Calls
87 // |callback| with BACKGROUND_SYNC_STATUS_OK on success. 77 // |callback| with BACKGROUND_SYNC_STATUS_OK on success.
88 void GetRegistration(int64 sw_registration_id, 78 void GetRegistration(int64 sw_registration_id,
89 const std::string& sync_registration_tag, 79 const std::string& sync_registration_tag,
90 SyncPeriodicity periodicity, 80 SyncPeriodicity periodicity,
91 const StatusAndRegistrationCallback& callback); 81 const StatusAndRegistrationCallback& callback);
92 82
93 void GetRegistrations(int64 sw_registration_id, 83 void GetRegistrations(int64 sw_registration_id,
94 SyncPeriodicity periodicity, 84 SyncPeriodicity periodicity,
95 const StatusAndRegistrationsCallback& callback); 85 const StatusAndRegistrationsCallback& callback);
96 86
97 // ServiceWorkerContextObserver overrides. 87 // ServiceWorkerContextObserver overrides.
98 void OnRegistrationDeleted(int64 registration_id, 88 void OnRegistrationDeleted(int64 registration_id,
99 const GURL& pattern) override; 89 const GURL& pattern) override;
100 void OnStorageWiped() override; 90 void OnStorageWiped() override;
101 91
102 protected: 92 protected:
93 // A registration might be referenced by the client longer than
94 // the BackgroundSyncManager needs to keep track of it (e.g., the event has
95 // finished firing). The BackgroundSyncManager reference counts its
96 // registrations internally and every BackgroundSyncRegistrationHandle has a
97 // unique handle id which maps to a locally maintained (in
98 // client_registration_ids_) scoped_refptr.
99 class RefCountedRegistration
100 : public base::RefCounted<RefCountedRegistration> {
101 public:
102 BackgroundSyncRegistration* value() { return &registration_; }
103 const BackgroundSyncRegistration* value() const { return &registration_; }
104
105 private:
106 friend class base::RefCounted<RefCountedRegistration>;
107 virtual ~RefCountedRegistration() = default;
108
109 BackgroundSyncRegistration registration_;
110 };
111
103 explicit BackgroundSyncManager( 112 explicit BackgroundSyncManager(
104 const scoped_refptr<ServiceWorkerContextWrapper>& context); 113 const scoped_refptr<ServiceWorkerContextWrapper>& context);
105 114
106 // Init must be called before any public member function. Only call it once. 115 // Init must be called before any public member function. Only call it once.
107 void Init(); 116 void Init();
108 117
109 // The following methods are virtual for testing. 118 // The following methods are virtual for testing.
110 virtual void StoreDataInBackend( 119 virtual void StoreDataInBackend(
111 int64 sw_registration_id, 120 int64 sw_registration_id,
112 const GURL& origin, 121 const GURL& origin,
113 const std::string& backend_key, 122 const std::string& backend_key,
114 const std::string& data, 123 const std::string& data,
115 const ServiceWorkerStorage::StatusCallback& callback); 124 const ServiceWorkerStorage::StatusCallback& callback);
116 virtual void GetDataFromBackend( 125 virtual void GetDataFromBackend(
117 const std::string& backend_key, 126 const std::string& backend_key,
118 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& 127 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback&
119 callback); 128 callback);
120 virtual void FireOneShotSync( 129 virtual void FireOneShotSync(
121 const BackgroundSyncRegistration& registration, 130 const RefCountedRegistration& registration,
122 const scoped_refptr<ServiceWorkerVersion>& active_version, 131 const scoped_refptr<ServiceWorkerVersion>& active_version,
123 const ServiceWorkerVersion::StatusCallback& callback); 132 const ServiceWorkerVersion::StatusCallback& callback);
124 133
125 private: 134 private:
135 friend class BackgroundSyncRegistrationHandle;
136
126 class RegistrationKey { 137 class RegistrationKey {
127 public: 138 public:
128 explicit RegistrationKey(const BackgroundSyncRegistration& registration); 139 explicit RegistrationKey(const BackgroundSyncRegistration& registration);
129 explicit RegistrationKey(const BackgroundSyncRegistrationOptions& options); 140 explicit RegistrationKey(const BackgroundSyncRegistrationOptions& options);
130 RegistrationKey(const std::string& tag, SyncPeriodicity periodicity); 141 RegistrationKey(const std::string& tag, SyncPeriodicity periodicity);
131 RegistrationKey(const RegistrationKey& other) = default; 142 RegistrationKey(const RegistrationKey& other) = default;
132 RegistrationKey& operator=(const RegistrationKey& other) = default; 143 RegistrationKey& operator=(const RegistrationKey& other) = default;
133 144
134 bool operator<(const RegistrationKey& rhs) const { 145 bool operator<(const RegistrationKey& rhs) const {
135 return value_ < rhs.value_; 146 return value_ < rhs.value_;
136 } 147 }
137 148
138 private: 149 private:
139 std::string value_; 150 std::string value_;
140 }; 151 };
141 152
142 struct BackgroundSyncRegistrations { 153 struct BackgroundSyncRegistrations {
143 using RegistrationMap = 154 using RegistrationMap =
144 std::map<RegistrationKey, BackgroundSyncRegistration>; 155 std::map<RegistrationKey, scoped_refptr<RefCountedRegistration>>;
145 156
146 BackgroundSyncRegistrations(); 157 BackgroundSyncRegistrations();
147 ~BackgroundSyncRegistrations(); 158 ~BackgroundSyncRegistrations();
148 159
149 RegistrationMap registration_map; 160 RegistrationMap registration_map;
150 BackgroundSyncRegistration::RegistrationId next_id; 161 BackgroundSyncRegistration::RegistrationId next_id;
151 GURL origin; 162 GURL origin;
152 }; 163 };
153 164
154 using PermissionStatusCallback = base::Callback<void(bool)>; 165 using PermissionStatusCallback = base::Callback<void(bool)>;
155 using SWIdToRegistrationsMap = std::map<int64, BackgroundSyncRegistrations>; 166 using SWIdToRegistrationsMap = std::map<int64, BackgroundSyncRegistrations>;
michaeln 2015/08/21 02:39:24 hmmm... what if... // Used to dispatch the sync
jkarlin 2015/08/25 17:32:58 I solved the problem without the need for adoptabl
156 167
168 scoped_ptr<BackgroundSyncRegistrationHandle> CreateRegistrationHandle(
169 const RefCountedRegistration& registration);
170
171 // Returns the BackgroundSyncRegistration corresponding to |handle_id|.
172 // Returns nullptr if the registration is not found.
173 const BackgroundSyncRegistration* GetRegistrationForHandle(
174 BackgroundSyncRegistrationHandle::HandleId handle_id) const;
175
176 // The BackgroundSyncManager holds references to registrations that have
177 // active Handles. The handles must call this on destruction.
178 void ReleaseHandleId(BackgroundSyncRegistrationHandle::HandleId handle_id);
179
157 // Disable the manager. Already queued operations will abort once they start 180 // Disable the manager. Already queued operations will abort once they start
158 // to run (in their impl methods). Future operations will not queue. Any 181 // to run (in their impl methods). Future operations will not queue. Any
159 // registrations are cleared from memory and the backend (if it's still 182 // registrations are cleared from memory and the backend (if it's still
160 // functioning). The manager will reenable itself once it receives the 183 // functioning). The manager will reenable itself once it receives the
161 // OnStorageWiped message or on browser restart. 184 // OnStorageWiped message or on browser restart.
162 void DisableAndClearManager(const base::Closure& callback); 185 void DisableAndClearManager(const base::Closure& callback);
163 void DisableAndClearDidGetRegistrations( 186 void DisableAndClearDidGetRegistrations(
164 const base::Closure& callback, 187 const base::Closure& callback,
165 const std::vector<std::pair<int64, std::string>>& user_data, 188 const std::vector<std::pair<int64, std::string>>& user_data,
166 ServiceWorkerStatusCode status); 189 ServiceWorkerStatusCode status);
167 void DisableAndClearManagerClearedOne(const base::Closure& barrier_closure, 190 void DisableAndClearManagerClearedOne(const base::Closure& barrier_closure,
168 ServiceWorkerStatusCode status); 191 ServiceWorkerStatusCode status);
169 192
170 // Returns the existing registration in |existing_registration| if it is not 193 // Returns the existing registration in |existing_registration| if it is not
171 // null. 194 // null.
172 BackgroundSyncRegistration* LookupRegistration( 195 RefCountedRegistration* LookupRegistration(
173 int64 sw_registration_id, 196 int64 sw_registration_id,
174 const RegistrationKey& registration_key); 197 const RegistrationKey& registration_key);
175 198
176 // Store all registrations for a given |sw_registration_id|. 199 // Store all registrations for a given |sw_registration_id|.
177 void StoreRegistrations(int64 sw_registration_id, 200 void StoreRegistrations(int64 sw_registration_id,
178 const ServiceWorkerStorage::StatusCallback& callback); 201 const ServiceWorkerStorage::StatusCallback& callback);
179 202
180 // Removes the registration if it is in the map. 203 // Removes the registration if it is in the map.
181 void RemoveRegistrationFromMap(int64 sw_registration_id, 204 void RemoveRegistrationFromMap(int64 sw_registration_id,
182 const RegistrationKey& registration_key); 205 const RegistrationKey& registration_key);
183 206
184 void AddRegistrationToMap( 207 void AddRegistrationToMap(
185 int64 sw_registration_id, 208 int64 sw_registration_id,
186 const GURL& origin, 209 const GURL& origin,
187 const BackgroundSyncRegistration& sync_registration); 210 const scoped_refptr<RefCountedRegistration>& sync_registration);
188 211
189 void InitImpl(const base::Closure& callback); 212 void InitImpl(const base::Closure& callback);
190 void InitDidGetDataFromBackend( 213 void InitDidGetDataFromBackend(
191 const base::Closure& callback, 214 const base::Closure& callback,
192 const std::vector<std::pair<int64, std::string>>& user_data, 215 const std::vector<std::pair<int64, std::string>>& user_data,
193 ServiceWorkerStatusCode status); 216 ServiceWorkerStatusCode status);
194 217
195 // Register callbacks 218 // Register callbacks
196 void RegisterImpl(int64 sw_registration_id, 219 void RegisterImpl(int64 sw_registration_id,
197 const BackgroundSyncRegistrationOptions& options, 220 const BackgroundSyncRegistrationOptions& options,
198 const StatusAndRegistrationCallback& callback); 221 const StatusAndRegistrationCallback& callback);
199 void RegisterDidStore(int64 sw_registration_id, 222 void RegisterDidStore(
200 const BackgroundSyncRegistration& sync_registration, 223 int64 sw_registration_id,
201 const StatusAndRegistrationCallback& callback, 224 const scoped_refptr<RefCountedRegistration>& new_registration_ref,
202 ServiceWorkerStatusCode status); 225 const StatusAndRegistrationCallback& callback,
226 ServiceWorkerStatusCode status);
203 227
204 // Unregister callbacks 228 // Removes the background sync with periodicity |periodicity| and id
205 void UnregisterImpl( 229 // |sync_registration_id|. Calls |callback| with
206 int64 sw_registration_id, 230 // BACKGROUND_SYNC_STATUS_NOT_FOUND if no match is found. Calls |callback|
207 const RegistrationKey& registration_key, 231 // with BACKGROUND_SYNC_STATUS_OK on success.
208 BackgroundSyncRegistration::RegistrationId sync_registration_id, 232 void Unregister(int64 sw_registration_id,
209 SyncPeriodicity periodicity, 233 SyncPeriodicity periodicity,
210 const StatusCallback& callback); 234 BackgroundSyncRegistrationHandle::HandleId handle_id,
235 const StatusCallback& callback);
236 void UnregisterImpl(int64 sw_registration_id,
237 SyncPeriodicity periodicity,
238 BackgroundSyncRegistrationHandle::HandleId handle_id,
239 const StatusCallback& callback);
211 void UnregisterDidStore(int64 sw_registration_id, 240 void UnregisterDidStore(int64 sw_registration_id,
212 SyncPeriodicity periodicity, 241 SyncPeriodicity periodicity,
213 const StatusCallback& callback, 242 const StatusCallback& callback,
214 ServiceWorkerStatusCode status); 243 ServiceWorkerStatusCode status);
215 244
216 // GetRegistration callbacks 245 // GetRegistration callbacks
217 void GetRegistrationImpl(int64 sw_registration_id, 246 void GetRegistrationImpl(int64 sw_registration_id,
218 const RegistrationKey& registration_key, 247 const RegistrationKey& registration_key,
219 const StatusAndRegistrationCallback& callback); 248 const StatusAndRegistrationCallback& callback);
220 249
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 // OnStorageWiped callbacks 304 // OnStorageWiped callbacks
276 void OnStorageWipedImpl(const base::Closure& callback); 305 void OnStorageWipedImpl(const base::Closure& callback);
277 306
278 void OnNetworkChanged(); 307 void OnNetworkChanged();
279 void OnPowerChanged(); 308 void OnPowerChanged();
280 309
281 // Operation Scheduling callback and convenience functions. 310 // Operation Scheduling callback and convenience functions.
282 template <typename CallbackT, typename... Params> 311 template <typename CallbackT, typename... Params>
283 void CompleteOperationCallback(const CallbackT& callback, 312 void CompleteOperationCallback(const CallbackT& callback,
284 Params... parameters); 313 Params... parameters);
314 void CompleteStatusAndRegistrationCallback(
315 StatusAndRegistrationCallback callback,
316 BackgroundSyncStatus status,
317 scoped_ptr<BackgroundSyncRegistrationHandle> result);
318 void CompleteStatusAndRegistrationsCallback(
319 StatusAndRegistrationsCallback callback,
320 BackgroundSyncStatus status,
321 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>> results);
285 base::Closure MakeEmptyCompletion(); 322 base::Closure MakeEmptyCompletion();
286 base::Closure MakeClosureCompletion(const base::Closure& callback); 323 base::Closure MakeClosureCompletion(const base::Closure& callback);
287 StatusAndRegistrationCallback MakeStatusAndRegistrationCompletion( 324 StatusAndRegistrationCallback MakeStatusAndRegistrationCompletion(
288 const StatusAndRegistrationCallback& callback); 325 const StatusAndRegistrationCallback& callback);
289 StatusAndRegistrationsCallback MakeStatusAndRegistrationsCompletion( 326 StatusAndRegistrationsCallback MakeStatusAndRegistrationsCompletion(
290 const StatusAndRegistrationsCallback& callback); 327 const StatusAndRegistrationsCallback& callback);
291 BackgroundSyncManager::StatusCallback MakeStatusCompletion( 328 BackgroundSyncManager::StatusCallback MakeStatusCompletion(
292 const StatusCallback& callback); 329 const StatusCallback& callback);
293 330
294 SWIdToRegistrationsMap sw_to_registrations_map_; 331 SWIdToRegistrationsMap sw_to_registrations_map_;
295 CacheStorageScheduler op_scheduler_; 332 CacheStorageScheduler op_scheduler_;
296 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; 333 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_;
297 bool disabled_; 334 bool disabled_;
298 335
299 scoped_ptr<BackgroundSyncNetworkObserver> network_observer_; 336 scoped_ptr<BackgroundSyncNetworkObserver> network_observer_;
300 scoped_ptr<BackgroundSyncPowerObserver> power_observer_; 337 scoped_ptr<BackgroundSyncPowerObserver> power_observer_;
301 338
339 // The registrations that clients have handles to.
340 IDMap<scoped_refptr<const RefCountedRegistration>, IDMapOwnPointer>
341 registration_handle_ids_;
342
302 base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_; 343 base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_;
303 344
304 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager); 345 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager);
305 }; 346 };
306 347
307 } // namespace content 348 } // namespace content
308 349
309 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ 350 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698