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 #include "content/browser/background_sync/background_sync_manager.h" | 5 #include "content/browser/background_sync/background_sync_manager.h" |
6 | 6 |
7 #include "base/barrier_closure.h" | 7 #include "base/barrier_closure.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
11 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
12 #include "content/browser/background_sync/background_sync_metrics.h" | 12 #include "content/browser/background_sync/background_sync_metrics.h" |
13 #include "content/browser/background_sync/background_sync_network_observer.h" | 13 #include "content/browser/background_sync/background_sync_network_observer.h" |
14 #include "content/browser/background_sync/background_sync_power_observer.h" | 14 #include "content/browser/background_sync/background_sync_power_observer.h" |
15 #include "content/browser/background_sync/background_sync_registration_handle.h" | |
15 #include "content/browser/background_sync/background_sync_registration_options.h " | 16 #include "content/browser/background_sync/background_sync_registration_options.h " |
16 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 17 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
17 #include "content/browser/service_worker/service_worker_storage.h" | 18 #include "content/browser/service_worker/service_worker_storage.h" |
18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
19 | 20 |
20 #if defined(OS_ANDROID) | 21 #if defined(OS_ANDROID) |
21 #include "content/browser/android/background_sync_launcher_android.h" | 22 #include "content/browser/android/background_sync_launcher_android.h" |
22 #endif | 23 #endif |
23 | 24 |
25 namespace content { | |
26 | |
27 class BackgroundSyncManager::RefCountedRegistration | |
28 : public base::RefCounted<RefCountedRegistration> { | |
29 public: | |
30 BackgroundSyncRegistration* value() { return ®istration_; } | |
31 const BackgroundSyncRegistration* value() const { return ®istration_; } | |
32 | |
33 private: | |
34 friend class base::RefCounted<RefCountedRegistration>; | |
35 virtual ~RefCountedRegistration() = default; | |
michaeln
2015/08/28 02:53:11
does this need to be virtual? if not, nix the vtab
jkarlin
2015/09/02 23:51:40
Done.
| |
36 | |
37 BackgroundSyncRegistration registration_; | |
38 }; | |
39 | |
24 namespace { | 40 namespace { |
41 | |
25 const char kBackgroundSyncUserDataKey[] = "BackgroundSyncUserData"; | 42 const char kBackgroundSyncUserDataKey[] = "BackgroundSyncUserData"; |
43 | |
44 void PostErrorResponse( | |
45 BackgroundSyncStatus status, | |
46 const BackgroundSyncManager::StatusAndRegistrationCallback& callback) { | |
47 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
48 FROM_HERE, | |
49 base::Bind( | |
50 callback, status, | |
51 base::Passed(scoped_ptr<BackgroundSyncRegistrationHandle>().Pass()))); | |
26 } | 52 } |
27 | 53 |
28 namespace content { | 54 } // namespace |
29 | 55 |
30 BackgroundSyncManager::BackgroundSyncRegistrations:: | 56 BackgroundSyncManager::BackgroundSyncRegistrations:: |
31 BackgroundSyncRegistrations() | 57 BackgroundSyncRegistrations() |
32 : next_id(BackgroundSyncRegistration::kInitialId) { | 58 : next_id(BackgroundSyncRegistration::kInitialId) { |
33 } | 59 } |
34 | 60 |
35 BackgroundSyncManager::BackgroundSyncRegistrations:: | 61 BackgroundSyncManager::BackgroundSyncRegistrations:: |
36 ~BackgroundSyncRegistrations() { | 62 ~BackgroundSyncRegistrations() { |
37 } | 63 } |
38 | 64 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = | 106 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = |
81 AreOptionConditionsMet(options) | 107 AreOptionConditionsMet(options) |
82 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE | 108 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE |
83 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; | 109 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; |
84 | 110 |
85 if (disabled_) { | 111 if (disabled_) { |
86 BackgroundSyncMetrics::CountRegister( | 112 BackgroundSyncMetrics::CountRegister( |
87 options.periodicity, registration_could_fire, | 113 options.periodicity, registration_could_fire, |
88 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, | 114 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, |
89 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 115 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
90 base::ThreadTaskRunnerHandle::Get()->PostTask( | 116 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); |
91 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | |
92 BackgroundSyncRegistration())); | |
93 return; | 117 return; |
94 } | 118 } |
95 | 119 |
96 op_scheduler_.ScheduleOperation( | 120 op_scheduler_.ScheduleOperation( |
97 base::Bind(&BackgroundSyncManager::RegisterImpl, | 121 base::Bind(&BackgroundSyncManager::RegisterImpl, |
98 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options, | 122 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options, |
99 MakeStatusAndRegistrationCompletion(callback))); | 123 MakeStatusAndRegistrationCompletion(callback))); |
100 } | 124 } |
101 | 125 |
102 void BackgroundSyncManager::Unregister( | |
103 int64 sw_registration_id, | |
104 const std::string& sync_registration_tag, | |
105 SyncPeriodicity periodicity, | |
106 BackgroundSyncRegistration::RegistrationId sync_registration_id, | |
107 const StatusCallback& callback) { | |
108 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
109 | |
110 if (disabled_) { | |
111 BackgroundSyncMetrics::CountUnregister( | |
112 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | |
113 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
114 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); | |
115 return; | |
116 } | |
117 | |
118 RegistrationKey registration_key(sync_registration_tag, periodicity); | |
119 | |
120 op_scheduler_.ScheduleOperation(base::Bind( | |
121 &BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(), | |
122 sw_registration_id, registration_key, sync_registration_id, periodicity, | |
123 MakeStatusCompletion(callback))); | |
124 } | |
125 | |
126 void BackgroundSyncManager::GetRegistration( | 126 void BackgroundSyncManager::GetRegistration( |
127 int64 sw_registration_id, | 127 int64 sw_registration_id, |
128 const std::string& sync_registration_tag, | 128 const std::string& sync_registration_tag, |
129 SyncPeriodicity periodicity, | 129 SyncPeriodicity periodicity, |
130 const StatusAndRegistrationCallback& callback) { | 130 const StatusAndRegistrationCallback& callback) { |
131 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 131 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
132 | 132 |
133 if (disabled_) { | 133 if (disabled_) { |
134 base::ThreadTaskRunnerHandle::Get()->PostTask( | 134 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); |
135 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | |
136 BackgroundSyncRegistration())); | |
137 return; | 135 return; |
138 } | 136 } |
139 | 137 |
140 RegistrationKey registration_key(sync_registration_tag, periodicity); | 138 RegistrationKey registration_key(sync_registration_tag, periodicity); |
141 | 139 |
142 op_scheduler_.ScheduleOperation(base::Bind( | 140 op_scheduler_.ScheduleOperation(base::Bind( |
143 &BackgroundSyncManager::GetRegistrationImpl, | 141 &BackgroundSyncManager::GetRegistrationImpl, |
144 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, registration_key, | 142 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, registration_key, |
145 MakeStatusAndRegistrationCompletion(callback))); | 143 MakeStatusAndRegistrationCompletion(callback))); |
146 } | 144 } |
147 | 145 |
148 void BackgroundSyncManager::GetRegistrations( | 146 void BackgroundSyncManager::GetRegistrations( |
149 int64 sw_registration_id, | 147 int64 sw_registration_id, |
150 SyncPeriodicity periodicity, | 148 SyncPeriodicity periodicity, |
151 const StatusAndRegistrationsCallback& callback) { | 149 const StatusAndRegistrationsCallback& callback) { |
152 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 150 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
153 | 151 |
154 if (disabled_) { | 152 if (disabled_) { |
155 base::ThreadTaskRunnerHandle::Get()->PostTask( | 153 base::ThreadTaskRunnerHandle::Get()->PostTask( |
156 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | 154 FROM_HERE, |
157 std::vector<BackgroundSyncRegistration>())); | 155 base::Bind( |
156 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | |
157 base::Passed( | |
158 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>>() | |
159 .Pass()))); | |
158 return; | 160 return; |
159 } | 161 } |
160 | 162 |
161 op_scheduler_.ScheduleOperation( | 163 op_scheduler_.ScheduleOperation( |
162 base::Bind(&BackgroundSyncManager::GetRegistrationsImpl, | 164 base::Bind(&BackgroundSyncManager::GetRegistrationsImpl, |
163 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, | 165 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, |
164 periodicity, MakeStatusAndRegistrationsCompletion(callback))); | 166 periodicity, MakeStatusAndRegistrationsCompletion(callback))); |
165 } | 167 } |
166 | 168 |
169 // Given a HandleId |handle_id|, return a new handle for the same | |
170 // registration. | |
171 scoped_ptr<BackgroundSyncRegistrationHandle> | |
172 BackgroundSyncManager::DuplicateRegistrationHandle( | |
173 int64 sw_registration_id, | |
michaeln
2015/08/28 02:53:11
|sw_registration_id| is not used, nix it?
jkarlin
2015/09/02 23:51:40
Done.
| |
174 BackgroundSyncRegistrationHandle::HandleId handle_id) { | |
175 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
176 | |
177 scoped_refptr<RefCountedRegistration>* ref_registration = | |
178 registration_handle_ids_.Lookup(handle_id); | |
179 if (!ref_registration) | |
180 return scoped_ptr<BackgroundSyncRegistrationHandle>(); | |
181 return CreateRegistrationHandle(ref_registration->get()); | |
182 } | |
183 | |
167 void BackgroundSyncManager::OnRegistrationDeleted(int64 registration_id, | 184 void BackgroundSyncManager::OnRegistrationDeleted(int64 registration_id, |
168 const GURL& pattern) { | 185 const GURL& pattern) { |
169 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 186 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
170 | 187 |
171 // Operations already in the queue will either fail when they write to storage | 188 // Operations already in the queue will either fail when they write to storage |
172 // or return stale results based on registrations loaded in memory. This is | 189 // or return stale results based on registrations loaded in memory. This is |
173 // inconsequential since the service worker is gone. | 190 // inconsequential since the service worker is gone. |
174 op_scheduler_.ScheduleOperation(base::Bind( | 191 op_scheduler_.ScheduleOperation(base::Bind( |
175 &BackgroundSyncManager::OnRegistrationDeletedImpl, | 192 &BackgroundSyncManager::OnRegistrationDeletedImpl, |
176 weak_ptr_factory_.GetWeakPtr(), registration_id, MakeEmptyCompletion())); | 193 weak_ptr_factory_.GetWeakPtr(), registration_id, MakeEmptyCompletion())); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
238 LOG(ERROR) << "BackgroundSync failed to init due to backend failure."; | 255 LOG(ERROR) << "BackgroundSync failed to init due to backend failure."; |
239 DisableAndClearManager(base::Bind(callback)); | 256 DisableAndClearManager(base::Bind(callback)); |
240 return; | 257 return; |
241 } | 258 } |
242 | 259 |
243 bool corruption_detected = false; | 260 bool corruption_detected = false; |
244 for (const std::pair<int64, std::string>& data : user_data) { | 261 for (const std::pair<int64, std::string>& data : user_data) { |
245 BackgroundSyncRegistrationsProto registrations_proto; | 262 BackgroundSyncRegistrationsProto registrations_proto; |
246 if (registrations_proto.ParseFromString(data.second)) { | 263 if (registrations_proto.ParseFromString(data.second)) { |
247 BackgroundSyncRegistrations* registrations = | 264 BackgroundSyncRegistrations* registrations = |
248 &sw_to_registrations_map_[data.first]; | 265 &active_registrations_[data.first]; |
249 registrations->next_id = registrations_proto.next_registration_id(); | 266 registrations->next_id = registrations_proto.next_registration_id(); |
250 registrations->origin = GURL(registrations_proto.origin()); | 267 registrations->origin = GURL(registrations_proto.origin()); |
251 | 268 |
252 for (int i = 0, max = registrations_proto.registration_size(); i < max; | 269 for (int i = 0, max = registrations_proto.registration_size(); i < max; |
253 ++i) { | 270 ++i) { |
254 const BackgroundSyncRegistrationProto& registration_proto = | 271 const BackgroundSyncRegistrationProto& registration_proto = |
255 registrations_proto.registration(i); | 272 registrations_proto.registration(i); |
256 | 273 |
257 if (registration_proto.id() >= registrations->next_id) { | 274 if (registration_proto.id() >= registrations->next_id) { |
258 corruption_detected = true; | 275 corruption_detected = true; |
259 break; | 276 break; |
260 } | 277 } |
261 | 278 |
262 RegistrationKey registration_key(registration_proto.tag(), | 279 RegistrationKey registration_key(registration_proto.tag(), |
263 registration_proto.periodicity()); | 280 registration_proto.periodicity()); |
264 BackgroundSyncRegistration* registration = | 281 |
265 ®istrations->registration_map[registration_key]; | 282 scoped_refptr<RefCountedRegistration> ref_registration( |
283 new RefCountedRegistration()); | |
284 registrations->registration_map[registration_key] = ref_registration; | |
285 BackgroundSyncRegistration* registration = ref_registration->value(); | |
266 | 286 |
267 BackgroundSyncRegistrationOptions* options = registration->options(); | 287 BackgroundSyncRegistrationOptions* options = registration->options(); |
268 options->tag = registration_proto.tag(); | 288 options->tag = registration_proto.tag(); |
269 options->periodicity = registration_proto.periodicity(); | 289 options->periodicity = registration_proto.periodicity(); |
270 options->min_period = registration_proto.min_period(); | 290 options->min_period = registration_proto.min_period(); |
271 options->network_state = registration_proto.network_state(); | 291 options->network_state = registration_proto.network_state(); |
272 options->power_state = registration_proto.power_state(); | 292 options->power_state = registration_proto.power_state(); |
273 | 293 |
274 registration->set_id(registration_proto.id()); | 294 registration->set_id(registration_proto.id()); |
275 registration->set_sync_state(registration_proto.sync_state()); | 295 registration->set_sync_state(registration_proto.sync_state()); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
308 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = | 328 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = |
309 AreOptionConditionsMet(options) | 329 AreOptionConditionsMet(options) |
310 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE | 330 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE |
311 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; | 331 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; |
312 | 332 |
313 if (disabled_) { | 333 if (disabled_) { |
314 BackgroundSyncMetrics::CountRegister( | 334 BackgroundSyncMetrics::CountRegister( |
315 options.periodicity, registration_could_fire, | 335 options.periodicity, registration_could_fire, |
316 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, | 336 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, |
317 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 337 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
318 base::ThreadTaskRunnerHandle::Get()->PostTask( | 338 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); |
319 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | |
320 BackgroundSyncRegistration())); | |
321 return; | 339 return; |
322 } | 340 } |
323 | 341 |
324 ServiceWorkerRegistration* sw_registration = | 342 ServiceWorkerRegistration* sw_registration = |
325 service_worker_context_->GetLiveRegistration(sw_registration_id); | 343 service_worker_context_->GetLiveRegistration(sw_registration_id); |
326 if (!sw_registration || !sw_registration->active_version()) { | 344 if (!sw_registration || !sw_registration->active_version()) { |
327 BackgroundSyncMetrics::CountRegister( | 345 BackgroundSyncMetrics::CountRegister( |
328 options.periodicity, registration_could_fire, | 346 options.periodicity, registration_could_fire, |
329 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, | 347 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, |
330 BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER); | 348 BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER); |
331 base::ThreadTaskRunnerHandle::Get()->PostTask( | 349 PostErrorResponse(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, callback); |
332 FROM_HERE, | |
333 base::Bind(callback, BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, | |
334 BackgroundSyncRegistration())); | |
335 return; | 350 return; |
336 } | 351 } |
337 | 352 |
338 if (!sw_registration->active_version()->HasWindowClients()) { | 353 if (!sw_registration->active_version()->HasWindowClients()) { |
339 base::ThreadTaskRunnerHandle::Get()->PostTask( | 354 PostErrorResponse(BACKGROUND_SYNC_STATUS_NOT_ALLOWED, callback); |
340 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_NOT_ALLOWED, | |
341 BackgroundSyncRegistration())); | |
342 return; | 355 return; |
343 } | 356 } |
344 | 357 |
345 BackgroundSyncRegistration* existing_registration = | 358 RefCountedRegistration* existing_registration_ref = |
346 LookupRegistration(sw_registration_id, RegistrationKey(options)); | 359 LookupActiveRegistration(sw_registration_id, RegistrationKey(options)); |
347 if (existing_registration && | 360 if (existing_registration_ref && |
348 existing_registration->options()->Equals(options)) { | 361 existing_registration_ref->value()->options()->Equals(options)) { |
362 BackgroundSyncRegistration* existing_registration = | |
363 existing_registration_ref->value(); | |
349 if (existing_registration->sync_state() == SYNC_STATE_FAILED) { | 364 if (existing_registration->sync_state() == SYNC_STATE_FAILED) { |
350 existing_registration->set_sync_state(SYNC_STATE_PENDING); | 365 existing_registration->set_sync_state(SYNC_STATE_PENDING); |
351 StoreRegistrations( | 366 StoreRegistrations( |
352 sw_registration_id, | 367 sw_registration_id, |
353 base::Bind(&BackgroundSyncManager::RegisterDidStore, | 368 base::Bind(&BackgroundSyncManager::RegisterDidStore, |
354 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, | 369 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, |
355 *existing_registration, callback)); | 370 make_scoped_refptr(existing_registration_ref), callback)); |
356 return; | 371 return; |
357 } | 372 } |
358 | 373 |
359 // Record the duplicated registration | 374 // Record the duplicated registration |
360 BackgroundSyncMetrics::CountRegister( | 375 BackgroundSyncMetrics::CountRegister( |
361 existing_registration->options()->periodicity, registration_could_fire, | 376 existing_registration->options()->periodicity, registration_could_fire, |
362 BackgroundSyncMetrics::REGISTRATION_IS_DUPLICATE, | 377 BackgroundSyncMetrics::REGISTRATION_IS_DUPLICATE, |
363 BACKGROUND_SYNC_STATUS_OK); | 378 BACKGROUND_SYNC_STATUS_OK); |
364 | 379 |
365 base::ThreadTaskRunnerHandle::Get()->PostTask( | 380 base::ThreadTaskRunnerHandle::Get()->PostTask( |
366 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, | 381 FROM_HERE, |
367 *existing_registration)); | 382 base::Bind( |
383 callback, BACKGROUND_SYNC_STATUS_OK, | |
384 base::Passed( | |
385 CreateRegistrationHandle(existing_registration_ref).Pass()))); | |
368 return; | 386 return; |
369 } | 387 } |
370 | 388 |
371 BackgroundSyncRegistration new_registration; | 389 scoped_refptr<RefCountedRegistration> new_ref_registration( |
372 *new_registration.options() = options; | 390 new RefCountedRegistration()); |
391 BackgroundSyncRegistration* new_registration = new_ref_registration->value(); | |
392 | |
393 *new_registration->options() = options; | |
373 | 394 |
374 BackgroundSyncRegistrations* registrations = | 395 BackgroundSyncRegistrations* registrations = |
375 &sw_to_registrations_map_[sw_registration_id]; | 396 &active_registrations_[sw_registration_id]; |
376 new_registration.set_id(registrations->next_id++); | 397 new_registration->set_id(registrations->next_id++); |
377 | 398 |
378 AddRegistrationToMap(sw_registration_id, | 399 AddActiveRegistration(sw_registration_id, |
379 sw_registration->pattern().GetOrigin(), | 400 sw_registration->pattern().GetOrigin(), |
380 new_registration); | 401 new_ref_registration); |
381 | 402 |
382 StoreRegistrations( | 403 StoreRegistrations( |
383 sw_registration_id, | 404 sw_registration_id, |
384 base::Bind(&BackgroundSyncManager::RegisterDidStore, | 405 base::Bind(&BackgroundSyncManager::RegisterDidStore, |
385 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, | 406 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, |
386 new_registration, callback)); | 407 new_ref_registration, callback)); |
387 } | 408 } |
388 | 409 |
389 void BackgroundSyncManager::DisableAndClearManager( | 410 void BackgroundSyncManager::DisableAndClearManager( |
390 const base::Closure& callback) { | 411 const base::Closure& callback) { |
391 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 412 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
392 | 413 |
393 if (disabled_) { | 414 if (disabled_) { |
394 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 415 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
395 base::Bind(callback)); | 416 base::Bind(callback)); |
396 return; | 417 return; |
397 } | 418 } |
398 | 419 |
399 disabled_ = true; | 420 disabled_ = true; |
400 sw_to_registrations_map_.clear(); | 421 active_registrations_.clear(); |
401 | 422 |
402 // Delete all backend entries. The memory representation of registered syncs | 423 // Delete all backend entries. The memory representation of registered syncs |
403 // may be out of sync with storage (e.g., due to corruption detection on | 424 // may be out of sync with storage (e.g., due to corruption detection on |
404 // loading from storage), so reload the registrations from storage again. | 425 // loading from storage), so reload the registrations from storage again. |
405 GetDataFromBackend( | 426 GetDataFromBackend( |
406 kBackgroundSyncUserDataKey, | 427 kBackgroundSyncUserDataKey, |
407 base::Bind(&BackgroundSyncManager::DisableAndClearDidGetRegistrations, | 428 base::Bind(&BackgroundSyncManager::DisableAndClearDidGetRegistrations, |
408 weak_ptr_factory_.GetWeakPtr(), callback)); | 429 weak_ptr_factory_.GetWeakPtr(), callback)); |
409 } | 430 } |
410 | 431 |
(...skipping 23 matching lines...) Expand all Loading... | |
434 void BackgroundSyncManager::DisableAndClearManagerClearedOne( | 455 void BackgroundSyncManager::DisableAndClearManagerClearedOne( |
435 const base::Closure& barrier_closure, | 456 const base::Closure& barrier_closure, |
436 ServiceWorkerStatusCode status) { | 457 ServiceWorkerStatusCode status) { |
437 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 458 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
438 | 459 |
439 // The status doesn't matter at this point, there is nothing else to be done. | 460 // The status doesn't matter at this point, there is nothing else to be done. |
440 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 461 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
441 base::Bind(barrier_closure)); | 462 base::Bind(barrier_closure)); |
442 } | 463 } |
443 | 464 |
444 BackgroundSyncRegistration* BackgroundSyncManager::LookupRegistration( | 465 BackgroundSyncManager::RefCountedRegistration* |
466 BackgroundSyncManager::LookupActiveRegistration( | |
445 int64 sw_registration_id, | 467 int64 sw_registration_id, |
446 const RegistrationKey& registration_key) { | 468 const RegistrationKey& registration_key) { |
447 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 469 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
448 | 470 |
449 SWIdToRegistrationsMap::iterator it = | 471 SWIdToRegistrationsMap::iterator it = |
450 sw_to_registrations_map_.find(sw_registration_id); | 472 active_registrations_.find(sw_registration_id); |
451 if (it == sw_to_registrations_map_.end()) | 473 if (it == active_registrations_.end()) |
452 return nullptr; | 474 return nullptr; |
453 | 475 |
454 BackgroundSyncRegistrations& registrations = it->second; | 476 BackgroundSyncRegistrations& registrations = it->second; |
455 DCHECK_LE(BackgroundSyncRegistration::kInitialId, registrations.next_id); | 477 DCHECK_LE(BackgroundSyncRegistration::kInitialId, registrations.next_id); |
456 DCHECK(!registrations.origin.is_empty()); | 478 DCHECK(!registrations.origin.is_empty()); |
457 | 479 |
458 auto key_and_registration_iter = | 480 auto key_and_registration_iter = |
459 registrations.registration_map.find(registration_key); | 481 registrations.registration_map.find(registration_key); |
460 if (key_and_registration_iter == registrations.registration_map.end()) | 482 if (key_and_registration_iter == registrations.registration_map.end()) |
461 return nullptr; | 483 return nullptr; |
462 | 484 |
463 return &key_and_registration_iter->second; | 485 return key_and_registration_iter->second.get(); |
464 } | 486 } |
465 | 487 |
466 void BackgroundSyncManager::StoreRegistrations( | 488 void BackgroundSyncManager::StoreRegistrations( |
467 int64 sw_registration_id, | 489 int64 sw_registration_id, |
468 const ServiceWorkerStorage::StatusCallback& callback) { | 490 const ServiceWorkerStorage::StatusCallback& callback) { |
469 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 491 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
470 | 492 |
471 // Serialize the data. | 493 // Serialize the data. |
472 const BackgroundSyncRegistrations& registrations = | 494 const BackgroundSyncRegistrations& registrations = |
473 sw_to_registrations_map_[sw_registration_id]; | 495 active_registrations_[sw_registration_id]; |
474 BackgroundSyncRegistrationsProto registrations_proto; | 496 BackgroundSyncRegistrationsProto registrations_proto; |
475 registrations_proto.set_next_registration_id(registrations.next_id); | 497 registrations_proto.set_next_registration_id(registrations.next_id); |
476 registrations_proto.set_origin(registrations.origin.spec()); | 498 registrations_proto.set_origin(registrations.origin.spec()); |
477 | 499 |
478 for (const auto& key_and_registration : registrations.registration_map) { | 500 for (const auto& key_and_registration : registrations.registration_map) { |
479 const BackgroundSyncRegistration& registration = | 501 const BackgroundSyncRegistration& registration = |
480 key_and_registration.second; | 502 *key_and_registration.second->value(); |
481 BackgroundSyncRegistrationProto* registration_proto = | 503 BackgroundSyncRegistrationProto* registration_proto = |
482 registrations_proto.add_registration(); | 504 registrations_proto.add_registration(); |
483 registration_proto->set_id(registration.id()); | 505 registration_proto->set_id(registration.id()); |
484 registration_proto->set_sync_state(registration.sync_state()); | 506 registration_proto->set_sync_state(registration.sync_state()); |
485 registration_proto->set_tag(registration.options()->tag); | 507 registration_proto->set_tag(registration.options()->tag); |
486 registration_proto->set_periodicity(registration.options()->periodicity); | 508 registration_proto->set_periodicity(registration.options()->periodicity); |
487 registration_proto->set_min_period(registration.options()->min_period); | 509 registration_proto->set_min_period(registration.options()->min_period); |
488 registration_proto->set_network_state( | 510 registration_proto->set_network_state( |
489 registration.options()->network_state); | 511 registration.options()->network_state); |
490 registration_proto->set_power_state(registration.options()->power_state); | 512 registration_proto->set_power_state(registration.options()->power_state); |
491 } | 513 } |
492 std::string serialized; | 514 std::string serialized; |
493 bool success = registrations_proto.SerializeToString(&serialized); | 515 bool success = registrations_proto.SerializeToString(&serialized); |
494 DCHECK(success); | 516 DCHECK(success); |
495 | 517 |
496 StoreDataInBackend(sw_registration_id, registrations.origin, | 518 StoreDataInBackend(sw_registration_id, registrations.origin, |
497 kBackgroundSyncUserDataKey, serialized, callback); | 519 kBackgroundSyncUserDataKey, serialized, callback); |
498 } | 520 } |
499 | 521 |
500 void BackgroundSyncManager::RegisterDidStore( | 522 void BackgroundSyncManager::RegisterDidStore( |
501 int64 sw_registration_id, | 523 int64 sw_registration_id, |
502 const BackgroundSyncRegistration& new_registration, | 524 const scoped_refptr<RefCountedRegistration>& new_registration_ref, |
503 const StatusAndRegistrationCallback& callback, | 525 const StatusAndRegistrationCallback& callback, |
504 ServiceWorkerStatusCode status) { | 526 ServiceWorkerStatusCode status) { |
505 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 527 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
506 | 528 |
529 const BackgroundSyncRegistration* new_registration = | |
530 new_registration_ref->value(); | |
531 | |
507 // For UMA, determine here whether the sync could fire immediately | 532 // For UMA, determine here whether the sync could fire immediately |
508 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = | 533 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = |
509 AreOptionConditionsMet(*new_registration.options()) | 534 AreOptionConditionsMet(*new_registration->options()) |
510 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE | 535 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE |
511 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; | 536 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; |
512 | 537 |
513 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { | 538 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
514 // The service worker registration is gone. | 539 // The service worker registration is gone. |
515 BackgroundSyncMetrics::CountRegister( | 540 BackgroundSyncMetrics::CountRegister( |
516 new_registration.options()->periodicity, registration_could_fire, | 541 new_registration->options()->periodicity, registration_could_fire, |
517 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, | 542 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, |
518 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 543 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
519 sw_to_registrations_map_.erase(sw_registration_id); | 544 active_registrations_.erase(sw_registration_id); |
520 base::ThreadTaskRunnerHandle::Get()->PostTask( | 545 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); |
521 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | |
522 BackgroundSyncRegistration())); | |
523 return; | 546 return; |
524 } | 547 } |
525 | 548 |
526 if (status != SERVICE_WORKER_OK) { | 549 if (status != SERVICE_WORKER_OK) { |
527 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " | 550 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " |
528 "failure."; | 551 "failure."; |
529 BackgroundSyncMetrics::CountRegister( | 552 BackgroundSyncMetrics::CountRegister( |
530 new_registration.options()->periodicity, registration_could_fire, | 553 new_registration->options()->periodicity, registration_could_fire, |
531 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, | 554 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, |
532 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 555 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
533 DisableAndClearManager(base::Bind(callback, | 556 DisableAndClearManager(base::Bind( |
534 BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | 557 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
535 BackgroundSyncRegistration())); | 558 base::Passed(scoped_ptr<BackgroundSyncRegistrationHandle>().Pass()))); |
536 return; | 559 return; |
537 } | 560 } |
538 | 561 |
539 BackgroundSyncMetrics::CountRegister( | 562 BackgroundSyncMetrics::CountRegister( |
540 new_registration.options()->periodicity, registration_could_fire, | 563 new_registration->options()->periodicity, registration_could_fire, |
541 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, | 564 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, |
542 BACKGROUND_SYNC_STATUS_OK); | 565 BACKGROUND_SYNC_STATUS_OK); |
543 FireReadyEvents(); | 566 FireReadyEvents(); |
544 base::ThreadTaskRunnerHandle::Get()->PostTask( | 567 base::ThreadTaskRunnerHandle::Get()->PostTask( |
545 FROM_HERE, | 568 FROM_HERE, |
546 base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, new_registration)); | 569 base::Bind( |
570 callback, BACKGROUND_SYNC_STATUS_OK, | |
571 base::Passed( | |
572 CreateRegistrationHandle(new_registration_ref.get()).Pass()))); | |
547 } | 573 } |
548 | 574 |
549 void BackgroundSyncManager::RemoveRegistrationFromMap( | 575 void BackgroundSyncManager::RemoveActiveRegistration( |
550 int64 sw_registration_id, | 576 int64 sw_registration_id, |
551 const RegistrationKey& registration_key) { | 577 const RegistrationKey& registration_key) { |
552 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 578 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
553 DCHECK(LookupRegistration(sw_registration_id, registration_key)); | 579 DCHECK(LookupActiveRegistration(sw_registration_id, registration_key)); |
554 | 580 |
555 BackgroundSyncRegistrations* registrations = | 581 BackgroundSyncRegistrations* registrations = |
556 &sw_to_registrations_map_[sw_registration_id]; | 582 &active_registrations_[sw_registration_id]; |
557 | 583 |
558 registrations->registration_map.erase(registration_key); | 584 registrations->registration_map.erase(registration_key); |
559 } | 585 } |
560 | 586 |
561 void BackgroundSyncManager::AddRegistrationToMap( | 587 void BackgroundSyncManager::AddActiveRegistration( |
562 int64 sw_registration_id, | 588 int64 sw_registration_id, |
563 const GURL& origin, | 589 const GURL& origin, |
564 const BackgroundSyncRegistration& sync_registration) { | 590 const scoped_refptr<RefCountedRegistration>& sync_registration) { |
565 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 591 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
566 DCHECK(sync_registration.IsValid()); | 592 DCHECK(sync_registration->value()->IsValid()); |
567 | 593 |
568 BackgroundSyncRegistrations* registrations = | 594 BackgroundSyncRegistrations* registrations = |
569 &sw_to_registrations_map_[sw_registration_id]; | 595 &active_registrations_[sw_registration_id]; |
570 registrations->origin = origin; | 596 registrations->origin = origin; |
571 | 597 |
572 RegistrationKey registration_key(sync_registration); | 598 RegistrationKey registration_key(*sync_registration->value()); |
573 registrations->registration_map[registration_key] = sync_registration; | 599 registrations->registration_map[registration_key] = sync_registration; |
574 } | 600 } |
575 | 601 |
576 void BackgroundSyncManager::StoreDataInBackend( | 602 void BackgroundSyncManager::StoreDataInBackend( |
577 int64 sw_registration_id, | 603 int64 sw_registration_id, |
578 const GURL& origin, | 604 const GURL& origin, |
579 const std::string& backend_key, | 605 const std::string& backend_key, |
580 const std::string& data, | 606 const std::string& data, |
581 const ServiceWorkerStorage::StatusCallback& callback) { | 607 const ServiceWorkerStorage::StatusCallback& callback) { |
582 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 608 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
583 | 609 |
584 service_worker_context_->StoreRegistrationUserData( | 610 service_worker_context_->StoreRegistrationUserData( |
585 sw_registration_id, origin, backend_key, data, callback); | 611 sw_registration_id, origin, backend_key, data, callback); |
586 } | 612 } |
587 | 613 |
588 void BackgroundSyncManager::GetDataFromBackend( | 614 void BackgroundSyncManager::GetDataFromBackend( |
589 const std::string& backend_key, | 615 const std::string& backend_key, |
590 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& | 616 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& |
591 callback) { | 617 callback) { |
592 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 618 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
593 | 619 |
594 service_worker_context_->GetUserDataForAllRegistrations(backend_key, | 620 service_worker_context_->GetUserDataForAllRegistrations(backend_key, |
595 callback); | 621 callback); |
596 } | 622 } |
597 | 623 |
598 void BackgroundSyncManager::FireOneShotSync( | 624 void BackgroundSyncManager::FireOneShotSync( |
599 const BackgroundSyncRegistration& registration, | 625 BackgroundSyncRegistrationHandle::HandleId handle_id, |
600 const scoped_refptr<ServiceWorkerVersion>& active_version, | 626 const scoped_refptr<ServiceWorkerVersion>& active_version, |
601 const ServiceWorkerVersion::StatusCallback& callback) { | 627 const ServiceWorkerVersion::StatusCallback& callback) { |
602 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 628 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
629 DCHECK(active_version); | |
603 | 630 |
604 active_version->DispatchSyncEvent( | 631 // The ServiceWorkerVersion doesn't know when the client (javascript) is done |
605 mojo::ConvertTo<SyncRegistrationPtr>(registration), callback); | 632 // with the registration so don't give it a BackgroundSyncRegistrationHandle. |
633 // Once the render process gets the handle_id it can create its own handle | |
634 // (with a new unique handle id). | |
635 active_version->DispatchSyncEvent(handle_id, callback); | |
636 } | |
637 | |
638 scoped_ptr<BackgroundSyncRegistrationHandle> | |
639 BackgroundSyncManager::CreateRegistrationHandle( | |
640 RefCountedRegistration* registration) { | |
michaeln
2015/08/28 02:53:11
i think const scoped_refptr<T>& registration for t
jkarlin
2015/09/02 23:51:40
Done.
| |
641 scoped_refptr<RefCountedRegistration>* ptr = | |
642 new scoped_refptr<RefCountedRegistration>(registration); | |
643 | |
644 // Registration handles have unique handle ids. The handle id maps to an | |
645 // internal RefCountedRegistration (which has the persistent registration id) | |
646 // via | |
647 // registration_reference_ids_. | |
648 BackgroundSyncRegistrationHandle::HandleId handle_id = | |
649 registration_handle_ids_.Add(ptr); | |
650 | |
651 return make_scoped_ptr(new BackgroundSyncRegistrationHandle( | |
652 weak_ptr_factory_.GetWeakPtr(), handle_id)); | |
653 } | |
654 | |
655 BackgroundSyncRegistration* BackgroundSyncManager::GetRegistrationForHandle( | |
656 BackgroundSyncRegistrationHandle::HandleId handle_id) const { | |
657 scoped_refptr<RefCountedRegistration>* ref_registration = | |
658 registration_handle_ids_.Lookup(handle_id); | |
659 if (!ref_registration) | |
660 return nullptr; | |
661 return (*ref_registration)->value(); | |
662 } | |
663 | |
664 void BackgroundSyncManager::ReleaseRegistrationHandle( | |
665 BackgroundSyncRegistrationHandle::HandleId handle_id) { | |
666 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
667 DCHECK(registration_handle_ids_.Lookup(handle_id)); | |
668 registration_handle_ids_.Remove(handle_id); | |
669 } | |
670 | |
671 void BackgroundSyncManager::Unregister( | |
672 int64 sw_registration_id, | |
michaeln
2015/08/28 02:53:11
I was wondering if sw_registration_id is needed? I
jkarlin
2015/09/02 23:51:40
Acknowledged.
| |
673 SyncPeriodicity periodicity, | |
674 BackgroundSyncRegistrationHandle::HandleId handle_id, | |
675 const StatusCallback& callback) { | |
676 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
677 | |
678 if (disabled_) { | |
679 BackgroundSyncMetrics::CountUnregister( | |
680 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | |
681 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
682 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); | |
683 return; | |
684 } | |
685 | |
686 BackgroundSyncRegistration* registration = | |
687 GetRegistrationForHandle(handle_id); | |
688 DCHECK(registration); | |
689 | |
690 op_scheduler_.ScheduleOperation(base::Bind( | |
691 &BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(), | |
692 sw_registration_id, RegistrationKey(*registration), registration->id(), | |
693 periodicity, MakeStatusCompletion(callback))); | |
606 } | 694 } |
607 | 695 |
608 void BackgroundSyncManager::UnregisterImpl( | 696 void BackgroundSyncManager::UnregisterImpl( |
609 int64 sw_registration_id, | 697 int64 sw_registration_id, |
610 const RegistrationKey& registration_key, | 698 const RegistrationKey& registration_key, |
611 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 699 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
612 SyncPeriodicity periodicity, | 700 SyncPeriodicity periodicity, |
613 const StatusCallback& callback) { | 701 const StatusCallback& callback) { |
614 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 702 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
615 | 703 |
616 if (disabled_) { | 704 if (disabled_) { |
617 BackgroundSyncMetrics::CountUnregister( | 705 BackgroundSyncMetrics::CountUnregister( |
618 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 706 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
619 base::ThreadTaskRunnerHandle::Get()->PostTask( | 707 base::ThreadTaskRunnerHandle::Get()->PostTask( |
620 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); | 708 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); |
621 return; | 709 return; |
622 } | 710 } |
623 | 711 |
624 const BackgroundSyncRegistration* existing_registration = | 712 const RefCountedRegistration* existing_registration = |
625 LookupRegistration(sw_registration_id, registration_key); | 713 LookupActiveRegistration(sw_registration_id, registration_key); |
714 | |
626 if (!existing_registration || | 715 if (!existing_registration || |
627 existing_registration->id() != sync_registration_id) { | 716 existing_registration->value()->id() != sync_registration_id) { |
628 BackgroundSyncMetrics::CountUnregister(periodicity, | 717 BackgroundSyncMetrics::CountUnregister(periodicity, |
629 BACKGROUND_SYNC_STATUS_NOT_FOUND); | 718 BACKGROUND_SYNC_STATUS_NOT_FOUND); |
630 base::ThreadTaskRunnerHandle::Get()->PostTask( | 719 base::ThreadTaskRunnerHandle::Get()->PostTask( |
631 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_NOT_FOUND)); | 720 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_NOT_FOUND)); |
632 return; | 721 return; |
633 } | 722 } |
634 | 723 |
635 RemoveRegistrationFromMap(sw_registration_id, registration_key); | 724 RemoveActiveRegistration(sw_registration_id, registration_key); |
636 | 725 |
637 StoreRegistrations(sw_registration_id, | 726 StoreRegistrations(sw_registration_id, |
638 base::Bind(&BackgroundSyncManager::UnregisterDidStore, | 727 base::Bind(&BackgroundSyncManager::UnregisterDidStore, |
639 weak_ptr_factory_.GetWeakPtr(), | 728 weak_ptr_factory_.GetWeakPtr(), |
640 sw_registration_id, periodicity, callback)); | 729 sw_registration_id, periodicity, callback)); |
641 } | 730 } |
642 | 731 |
643 void BackgroundSyncManager::UnregisterDidStore(int64 sw_registration_id, | 732 void BackgroundSyncManager::UnregisterDidStore(int64 sw_registration_id, |
644 SyncPeriodicity periodicity, | 733 SyncPeriodicity periodicity, |
645 const StatusCallback& callback, | 734 const StatusCallback& callback, |
646 ServiceWorkerStatusCode status) { | 735 ServiceWorkerStatusCode status) { |
647 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 736 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
648 | 737 |
649 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { | 738 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
650 // ServiceWorker was unregistered. | 739 // ServiceWorker was unregistered. |
651 BackgroundSyncMetrics::CountUnregister( | 740 BackgroundSyncMetrics::CountUnregister( |
652 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 741 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
653 sw_to_registrations_map_.erase(sw_registration_id); | 742 active_registrations_.erase(sw_registration_id); |
654 base::ThreadTaskRunnerHandle::Get()->PostTask( | 743 base::ThreadTaskRunnerHandle::Get()->PostTask( |
655 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); | 744 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); |
656 return; | 745 return; |
657 } | 746 } |
658 | 747 |
659 if (status != SERVICE_WORKER_OK) { | 748 if (status != SERVICE_WORKER_OK) { |
660 LOG(ERROR) << "BackgroundSync failed to unregister due to backend failure."; | 749 LOG(ERROR) << "BackgroundSync failed to unregister due to backend failure."; |
661 BackgroundSyncMetrics::CountUnregister( | 750 BackgroundSyncMetrics::CountUnregister( |
662 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 751 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
663 DisableAndClearManager( | 752 DisableAndClearManager( |
664 base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); | 753 base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); |
665 return; | 754 return; |
666 } | 755 } |
667 | 756 |
668 BackgroundSyncMetrics::CountUnregister(periodicity, | 757 BackgroundSyncMetrics::CountUnregister(periodicity, |
669 BACKGROUND_SYNC_STATUS_OK); | 758 BACKGROUND_SYNC_STATUS_OK); |
670 base::ThreadTaskRunnerHandle::Get()->PostTask( | 759 base::ThreadTaskRunnerHandle::Get()->PostTask( |
671 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK)); | 760 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK)); |
672 } | 761 } |
673 | 762 |
674 void BackgroundSyncManager::GetRegistrationImpl( | 763 void BackgroundSyncManager::GetRegistrationImpl( |
675 int64 sw_registration_id, | 764 int64 sw_registration_id, |
676 const RegistrationKey& registration_key, | 765 const RegistrationKey& registration_key, |
677 const StatusAndRegistrationCallback& callback) { | 766 const StatusAndRegistrationCallback& callback) { |
678 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 767 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
679 | 768 |
680 if (disabled_) { | 769 if (disabled_) { |
681 base::ThreadTaskRunnerHandle::Get()->PostTask( | 770 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); |
682 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | |
683 BackgroundSyncRegistration())); | |
684 return; | 771 return; |
685 } | 772 } |
686 | 773 |
687 const BackgroundSyncRegistration* out_registration = | 774 RefCountedRegistration* registration = |
688 LookupRegistration(sw_registration_id, registration_key); | 775 LookupActiveRegistration(sw_registration_id, registration_key); |
689 if (!out_registration) { | 776 if (!registration) { |
690 base::ThreadTaskRunnerHandle::Get()->PostTask( | 777 PostErrorResponse(BACKGROUND_SYNC_STATUS_NOT_FOUND, callback); |
691 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_NOT_FOUND, | |
692 BackgroundSyncRegistration())); | |
693 return; | 778 return; |
694 } | 779 } |
695 | 780 |
696 base::ThreadTaskRunnerHandle::Get()->PostTask( | 781 base::ThreadTaskRunnerHandle::Get()->PostTask( |
697 FROM_HERE, | 782 FROM_HERE, |
698 base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, *out_registration)); | 783 base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, |
784 base::Passed(CreateRegistrationHandle(registration).Pass()))); | |
699 } | 785 } |
700 | 786 |
701 void BackgroundSyncManager::GetRegistrationsImpl( | 787 void BackgroundSyncManager::GetRegistrationsImpl( |
702 int64 sw_registration_id, | 788 int64 sw_registration_id, |
703 SyncPeriodicity periodicity, | 789 SyncPeriodicity periodicity, |
704 const StatusAndRegistrationsCallback& callback) { | 790 const StatusAndRegistrationsCallback& callback) { |
705 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 791 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
706 | 792 |
707 std::vector<BackgroundSyncRegistration> out_registrations; | 793 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>> out_registrations( |
794 new ScopedVector<BackgroundSyncRegistrationHandle>()); | |
708 | 795 |
709 if (disabled_) { | 796 if (disabled_) { |
710 base::ThreadTaskRunnerHandle::Get()->PostTask( | 797 base::ThreadTaskRunnerHandle::Get()->PostTask( |
711 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | 798 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
712 out_registrations)); | 799 base::Passed(out_registrations.Pass()))); |
713 return; | 800 return; |
714 } | 801 } |
715 | 802 |
716 SWIdToRegistrationsMap::iterator it = | 803 SWIdToRegistrationsMap::iterator it = |
717 sw_to_registrations_map_.find(sw_registration_id); | 804 active_registrations_.find(sw_registration_id); |
718 | 805 |
719 if (it != sw_to_registrations_map_.end()) { | 806 if (it != active_registrations_.end()) { |
720 const BackgroundSyncRegistrations& registrations = it->second; | 807 const BackgroundSyncRegistrations& registrations = it->second; |
721 for (const auto& tag_and_registration : registrations.registration_map) { | 808 for (const auto& tag_and_registration : registrations.registration_map) { |
722 const BackgroundSyncRegistration& registration = | 809 RefCountedRegistration* registration = tag_and_registration.second.get(); |
723 tag_and_registration.second; | 810 if (registration->value()->options()->periodicity == periodicity) { |
724 if (registration.options()->periodicity == periodicity) | 811 out_registrations->push_back( |
725 out_registrations.push_back(registration); | 812 CreateRegistrationHandle(registration).release()); |
813 } | |
726 } | 814 } |
727 } | 815 } |
728 | 816 |
729 base::ThreadTaskRunnerHandle::Get()->PostTask( | 817 base::ThreadTaskRunnerHandle::Get()->PostTask( |
730 FROM_HERE, | 818 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, |
731 base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, out_registrations)); | 819 base::Passed(out_registrations.Pass()))); |
732 } | 820 } |
733 | 821 |
734 bool BackgroundSyncManager::AreOptionConditionsMet( | 822 bool BackgroundSyncManager::AreOptionConditionsMet( |
735 const BackgroundSyncRegistrationOptions& options) { | 823 const BackgroundSyncRegistrationOptions& options) { |
736 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 824 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
737 return network_observer_->NetworkSufficient(options.network_state) && | 825 return network_observer_->NetworkSufficient(options.network_state) && |
738 power_observer_->PowerSufficient(options.power_state); | 826 power_observer_->PowerSufficient(options.power_state); |
739 } | 827 } |
740 | 828 |
741 bool BackgroundSyncManager::IsRegistrationReadyToFire( | 829 bool BackgroundSyncManager::IsRegistrationReadyToFire( |
742 const BackgroundSyncRegistration& registration) { | 830 const BackgroundSyncRegistration& registration) { |
743 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 831 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
744 | 832 |
745 // TODO(jkarlin): Add support for firing periodic registrations. | 833 // TODO(jkarlin): Add support for firing periodic registrations. |
746 if (registration.options()->periodicity == SYNC_PERIODIC) | 834 if (registration.options()->periodicity == SYNC_PERIODIC) |
747 return false; | 835 return false; |
748 | 836 |
749 if (registration.sync_state() != SYNC_STATE_PENDING) | 837 if (registration.sync_state() != SYNC_STATE_PENDING) |
750 return false; | 838 return false; |
751 | 839 |
752 DCHECK_EQ(SYNC_ONE_SHOT, registration.options()->periodicity); | 840 DCHECK_EQ(SYNC_ONE_SHOT, registration.options()->periodicity); |
753 | 841 |
754 return AreOptionConditionsMet(*registration.options()); | 842 return AreOptionConditionsMet(*registration.options()); |
755 } | 843 } |
756 | 844 |
757 void BackgroundSyncManager::SchedulePendingRegistrations() { | 845 void BackgroundSyncManager::SchedulePendingRegistrations() { |
758 #if defined(OS_ANDROID) | 846 #if defined(OS_ANDROID) |
759 bool keep_browser_alive_for_one_shot = false; | 847 bool keep_browser_alive_for_one_shot = false; |
760 | 848 |
761 for (const auto& sw_id_and_registrations : sw_to_registrations_map_) { | 849 for (const auto& sw_id_and_registrations : active_registrations_) { |
762 for (const auto& key_and_registration : | 850 for (const auto& key_and_registration : |
763 sw_id_and_registrations.second.registration_map) { | 851 sw_id_and_registrations.second.registration_map) { |
764 const BackgroundSyncRegistration& registration = | 852 const BackgroundSyncRegistration& registration = |
765 key_and_registration.second; | 853 *key_and_registration.second->value(); |
766 if (registration.sync_state() == SYNC_STATE_PENDING) { | 854 if (registration.sync_state() == SYNC_STATE_PENDING) { |
767 if (registration.options()->periodicity == SYNC_ONE_SHOT) { | 855 if (registration.options()->periodicity == SYNC_ONE_SHOT) { |
768 keep_browser_alive_for_one_shot = true; | 856 keep_browser_alive_for_one_shot = true; |
769 } else { | 857 } else { |
770 // TODO(jkarlin): Support keeping the browser alive for periodic | 858 // TODO(jkarlin): Support keeping the browser alive for periodic |
771 // syncs. | 859 // syncs. |
772 } | 860 } |
773 } | 861 } |
774 } | 862 } |
775 } | 863 } |
(...skipping 25 matching lines...) Expand all Loading... | |
801 | 889 |
802 if (disabled_) { | 890 if (disabled_) { |
803 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 891 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
804 base::Bind(callback)); | 892 base::Bind(callback)); |
805 return; | 893 return; |
806 } | 894 } |
807 | 895 |
808 // Find the registrations that are ready to run. | 896 // Find the registrations that are ready to run. |
809 std::vector<std::pair<int64, RegistrationKey>> sw_id_and_keys_to_fire; | 897 std::vector<std::pair<int64, RegistrationKey>> sw_id_and_keys_to_fire; |
810 | 898 |
811 for (auto& sw_id_and_registrations : sw_to_registrations_map_) { | 899 for (auto& sw_id_and_registrations : active_registrations_) { |
812 const int64 service_worker_id = sw_id_and_registrations.first; | 900 const int64 service_worker_id = sw_id_and_registrations.first; |
813 for (auto& key_and_registration : | 901 for (auto& key_and_registration : |
814 sw_id_and_registrations.second.registration_map) { | 902 sw_id_and_registrations.second.registration_map) { |
815 BackgroundSyncRegistration* registration = &key_and_registration.second; | 903 BackgroundSyncRegistration* registration = |
904 key_and_registration.second->value(); | |
816 if (IsRegistrationReadyToFire(*registration)) { | 905 if (IsRegistrationReadyToFire(*registration)) { |
817 sw_id_and_keys_to_fire.push_back( | 906 sw_id_and_keys_to_fire.push_back( |
818 std::make_pair(service_worker_id, key_and_registration.first)); | 907 std::make_pair(service_worker_id, key_and_registration.first)); |
819 // The state change is not saved to persistent storage because | 908 // The state change is not saved to persistent storage because |
820 // if the sync event is killed mid-sync then it should return to | 909 // if the sync event is killed mid-sync then it should return to |
821 // SYNC_STATE_PENDING. | 910 // SYNC_STATE_PENDING. |
822 registration->set_sync_state(SYNC_STATE_FIRING); | 911 registration->set_sync_state(SYNC_STATE_FIRING); |
823 } | 912 } |
824 } | 913 } |
825 } | 914 } |
(...skipping 12 matching lines...) Expand all Loading... | |
838 sw_id_and_keys_to_fire.size(), base::Bind(callback)); | 927 sw_id_and_keys_to_fire.size(), base::Bind(callback)); |
839 | 928 |
840 // Record the total time taken after all events have run to completion. | 929 // Record the total time taken after all events have run to completion. |
841 base::Closure events_completed_barrier_closure = | 930 base::Closure events_completed_barrier_closure = |
842 base::BarrierClosure(sw_id_and_keys_to_fire.size(), | 931 base::BarrierClosure(sw_id_and_keys_to_fire.size(), |
843 base::Bind(&OnAllSyncEventsCompleted, start_time, | 932 base::Bind(&OnAllSyncEventsCompleted, start_time, |
844 sw_id_and_keys_to_fire.size())); | 933 sw_id_and_keys_to_fire.size())); |
845 | 934 |
846 for (const auto& sw_id_and_key : sw_id_and_keys_to_fire) { | 935 for (const auto& sw_id_and_key : sw_id_and_keys_to_fire) { |
847 int64 service_worker_id = sw_id_and_key.first; | 936 int64 service_worker_id = sw_id_and_key.first; |
848 const BackgroundSyncRegistration* registration = | 937 const RefCountedRegistration* registration = |
849 LookupRegistration(service_worker_id, sw_id_and_key.second); | 938 LookupActiveRegistration(service_worker_id, sw_id_and_key.second); |
939 DCHECK(registration); | |
850 | 940 |
851 service_worker_context_->FindRegistrationForId( | 941 service_worker_context_->FindRegistrationForId( |
852 service_worker_id, sw_to_registrations_map_[service_worker_id].origin, | 942 service_worker_id, active_registrations_[service_worker_id].origin, |
853 base::Bind(&BackgroundSyncManager::FireReadyEventsDidFindRegistration, | 943 base::Bind(&BackgroundSyncManager::FireReadyEventsDidFindRegistration, |
854 weak_ptr_factory_.GetWeakPtr(), sw_id_and_key.second, | 944 weak_ptr_factory_.GetWeakPtr(), sw_id_and_key.second, |
855 registration->id(), events_fired_barrier_closure, | 945 registration->value()->id(), events_fired_barrier_closure, |
856 events_completed_barrier_closure)); | 946 events_completed_barrier_closure)); |
857 } | 947 } |
858 } | 948 } |
859 | 949 |
860 SchedulePendingRegistrations(); | 950 SchedulePendingRegistrations(); |
861 } | 951 } |
862 | 952 |
863 void BackgroundSyncManager::FireReadyEventsDidFindRegistration( | 953 void BackgroundSyncManager::FireReadyEventsDidFindRegistration( |
864 const RegistrationKey& registration_key, | 954 const RegistrationKey& registration_key, |
865 BackgroundSyncRegistration::RegistrationId registration_id, | 955 BackgroundSyncRegistration::RegistrationId registration_id, |
866 const base::Closure& event_fired_callback, | 956 const base::Closure& event_fired_callback, |
867 const base::Closure& event_completed_callback, | 957 const base::Closure& event_completed_callback, |
868 ServiceWorkerStatusCode service_worker_status, | 958 ServiceWorkerStatusCode service_worker_status, |
869 const scoped_refptr<ServiceWorkerRegistration>& | 959 const scoped_refptr<ServiceWorkerRegistration>& |
870 service_worker_registration) { | 960 service_worker_registration) { |
871 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 961 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
872 if (service_worker_status != SERVICE_WORKER_OK) { | 962 if (service_worker_status != SERVICE_WORKER_OK) { |
873 base::ThreadTaskRunnerHandle::Get()->PostTask( | 963 base::ThreadTaskRunnerHandle::Get()->PostTask( |
874 FROM_HERE, base::Bind(event_fired_callback)); | 964 FROM_HERE, base::Bind(event_fired_callback)); |
875 base::ThreadTaskRunnerHandle::Get()->PostTask( | 965 base::ThreadTaskRunnerHandle::Get()->PostTask( |
876 FROM_HERE, base::Bind(event_completed_callback)); | 966 FROM_HERE, base::Bind(event_completed_callback)); |
877 return; | 967 return; |
878 } | 968 } |
879 | 969 |
880 BackgroundSyncRegistration* registration = | 970 RefCountedRegistration* registration = LookupActiveRegistration( |
881 LookupRegistration(service_worker_registration->id(), registration_key); | 971 service_worker_registration->id(), registration_key); |
972 DCHECK(registration); | |
882 | 973 |
974 // Create a handle and keep it until the sync event completes. The client can | |
975 // acquire its own handle for longer-term use. | |
976 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle = | |
977 CreateRegistrationHandle(registration); | |
978 | |
979 BackgroundSyncRegistrationHandle::HandleId handle_id = | |
980 registration_handle->handle_id(); | |
883 FireOneShotSync( | 981 FireOneShotSync( |
884 *registration, service_worker_registration->active_version(), | 982 handle_id, service_worker_registration->active_version(), |
885 base::Bind(&BackgroundSyncManager::EventComplete, | 983 base::Bind( |
886 weak_ptr_factory_.GetWeakPtr(), service_worker_registration, | 984 &BackgroundSyncManager::EventComplete, weak_ptr_factory_.GetWeakPtr(), |
887 service_worker_registration->id(), registration_key, | 985 service_worker_registration, service_worker_registration->id(), |
888 registration_id, event_completed_callback)); | 986 base::Passed(registration_handle.Pass()), event_completed_callback)); |
889 | 987 |
890 base::ThreadTaskRunnerHandle::Get()->PostTask( | 988 base::ThreadTaskRunnerHandle::Get()->PostTask( |
891 FROM_HERE, base::Bind(event_fired_callback)); | 989 FROM_HERE, base::Bind(event_fired_callback)); |
892 } | 990 } |
893 | 991 |
894 // |service_worker_registration| is just to keep the registration alive | 992 // |service_worker_registration| is just to keep the registration alive |
895 // while the event is firing. | 993 // while the event is firing. |
896 void BackgroundSyncManager::EventComplete( | 994 void BackgroundSyncManager::EventComplete( |
897 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, | 995 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, |
898 int64 service_worker_id, | 996 int64 service_worker_id, |
899 const RegistrationKey& key, | 997 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle, |
900 BackgroundSyncRegistration::RegistrationId sync_registration_id, | |
901 const base::Closure& callback, | 998 const base::Closure& callback, |
902 ServiceWorkerStatusCode status_code) { | 999 ServiceWorkerStatusCode status_code) { |
903 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1000 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
904 if (disabled_) | 1001 if (disabled_) |
905 return; | 1002 return; |
906 | 1003 |
907 op_scheduler_.ScheduleOperation(base::Bind( | 1004 op_scheduler_.ScheduleOperation(base::Bind( |
908 &BackgroundSyncManager::EventCompleteImpl, weak_ptr_factory_.GetWeakPtr(), | 1005 &BackgroundSyncManager::EventCompleteImpl, weak_ptr_factory_.GetWeakPtr(), |
909 service_worker_id, key, sync_registration_id, status_code, | 1006 service_worker_id, base::Passed(registration_handle.Pass()), status_code, |
910 MakeClosureCompletion(callback))); | 1007 MakeClosureCompletion(callback))); |
911 } | 1008 } |
912 | 1009 |
913 void BackgroundSyncManager::EventCompleteImpl( | 1010 void BackgroundSyncManager::EventCompleteImpl( |
914 int64 service_worker_id, | 1011 int64 service_worker_id, |
915 const RegistrationKey& key, | 1012 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle, |
916 BackgroundSyncRegistration::RegistrationId sync_registration_id, | |
917 ServiceWorkerStatusCode status_code, | 1013 ServiceWorkerStatusCode status_code, |
918 const base::Closure& callback) { | 1014 const base::Closure& callback) { |
919 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1015 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
920 | 1016 |
921 if (disabled_) { | 1017 if (disabled_) { |
922 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 1018 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
923 base::Bind(callback)); | 1019 base::Bind(callback)); |
924 return; | 1020 return; |
925 } | 1021 } |
926 | 1022 |
927 BackgroundSyncRegistration* registration = | 1023 BackgroundSyncRegistration* registration = |
928 LookupRegistration(service_worker_id, key); | 1024 registration_handle->registration(); |
929 if (!registration || registration->id() != sync_registration_id) { | 1025 DCHECK(registration); |
930 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
931 base::Bind(callback)); | |
932 return; | |
933 } | |
934 | 1026 |
935 // The event ran to completion, we should count it, no matter what happens | 1027 // The event ran to completion, we should count it, no matter what happens |
936 // from here. | 1028 // from here. |
937 BackgroundSyncMetrics::RecordEventResult(registration->options()->periodicity, | 1029 BackgroundSyncMetrics::RecordEventResult(registration->options()->periodicity, |
938 status_code == SERVICE_WORKER_OK); | 1030 status_code == SERVICE_WORKER_OK); |
939 | 1031 |
940 if (registration->options()->periodicity == SYNC_ONE_SHOT) { | 1032 if (registration->options()->periodicity == SYNC_ONE_SHOT) { |
941 if (status_code != SERVICE_WORKER_OK) { | 1033 if (status_code != SERVICE_WORKER_OK) { |
942 // TODO(jkarlin) Fire the sync event on the next page load controlled by | 1034 // TODO(jkarlin) Fire the sync event on the next page load controlled by |
943 // this registration. (crbug.com/479665) | 1035 // this registration. (crbug.com/479665) |
944 registration->set_sync_state(SYNC_STATE_FAILED); | 1036 registration->set_sync_state(SYNC_STATE_FAILED); |
945 } else { | 1037 } else { |
946 registration = nullptr; | 1038 RegistrationKey key(*registration); |
947 RemoveRegistrationFromMap(service_worker_id, key); | 1039 // Remove the registration if it's still active. |
1040 RefCountedRegistration* active_registration = | |
1041 LookupActiveRegistration(service_worker_id, key); | |
1042 if (active_registration && | |
1043 active_registration->value()->id() == registration->id()) | |
1044 RemoveActiveRegistration(service_worker_id, key); | |
948 } | 1045 } |
949 } else { | 1046 } else { |
950 // TODO(jkarlin): Add support for running periodic syncs. (crbug.com/479674) | 1047 // TODO(jkarlin): Add support for running periodic syncs. (crbug.com/479674) |
951 NOTREACHED(); | 1048 NOTREACHED(); |
952 } | 1049 } |
953 | 1050 |
954 StoreRegistrations( | 1051 StoreRegistrations( |
955 service_worker_id, | 1052 service_worker_id, |
956 base::Bind(&BackgroundSyncManager::EventCompleteDidStore, | 1053 base::Bind(&BackgroundSyncManager::EventCompleteDidStore, |
957 weak_ptr_factory_.GetWeakPtr(), service_worker_id, callback)); | 1054 weak_ptr_factory_.GetWeakPtr(), service_worker_id, callback)); |
958 } | 1055 } |
959 | 1056 |
960 void BackgroundSyncManager::EventCompleteDidStore( | 1057 void BackgroundSyncManager::EventCompleteDidStore( |
961 int64 service_worker_id, | 1058 int64 service_worker_id, |
962 const base::Closure& callback, | 1059 const base::Closure& callback, |
963 ServiceWorkerStatusCode status_code) { | 1060 ServiceWorkerStatusCode status_code) { |
964 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1061 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
965 | 1062 |
966 if (status_code == SERVICE_WORKER_ERROR_NOT_FOUND) { | 1063 if (status_code == SERVICE_WORKER_ERROR_NOT_FOUND) { |
967 // The registration is gone. | 1064 // The registration is gone. |
968 sw_to_registrations_map_.erase(service_worker_id); | 1065 active_registrations_.erase(service_worker_id); |
969 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 1066 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
970 base::Bind(callback)); | 1067 base::Bind(callback)); |
971 return; | 1068 return; |
972 } | 1069 } |
973 | 1070 |
974 if (status_code != SERVICE_WORKER_OK) { | 1071 if (status_code != SERVICE_WORKER_OK) { |
975 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " | 1072 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " |
976 "failure."; | 1073 "failure."; |
977 DisableAndClearManager(base::Bind(callback)); | 1074 DisableAndClearManager(base::Bind(callback)); |
978 return; | 1075 return; |
(...skipping 12 matching lines...) Expand all Loading... | |
991 base::TimeTicks::Now() - start_time, number_of_batched_sync_events); | 1088 base::TimeTicks::Now() - start_time, number_of_batched_sync_events); |
992 } | 1089 } |
993 | 1090 |
994 void BackgroundSyncManager::OnRegistrationDeletedImpl( | 1091 void BackgroundSyncManager::OnRegistrationDeletedImpl( |
995 int64 registration_id, | 1092 int64 registration_id, |
996 const base::Closure& callback) { | 1093 const base::Closure& callback) { |
997 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1094 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
998 | 1095 |
999 // The backend (ServiceWorkerStorage) will delete the data, so just delete the | 1096 // The backend (ServiceWorkerStorage) will delete the data, so just delete the |
1000 // memory representation here. | 1097 // memory representation here. |
1001 sw_to_registrations_map_.erase(registration_id); | 1098 active_registrations_.erase(registration_id); |
1002 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 1099 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
1003 base::Bind(callback)); | 1100 base::Bind(callback)); |
1004 } | 1101 } |
1005 | 1102 |
1006 void BackgroundSyncManager::OnStorageWipedImpl(const base::Closure& callback) { | 1103 void BackgroundSyncManager::OnStorageWipedImpl(const base::Closure& callback) { |
1007 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1104 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1008 | 1105 |
1009 sw_to_registrations_map_.clear(); | 1106 active_registrations_.clear(); |
1010 disabled_ = false; | 1107 disabled_ = false; |
1011 InitImpl(callback); | 1108 InitImpl(callback); |
1012 } | 1109 } |
1013 | 1110 |
1014 void BackgroundSyncManager::OnNetworkChanged() { | 1111 void BackgroundSyncManager::OnNetworkChanged() { |
1015 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1112 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1016 | 1113 |
1017 FireReadyEvents(); | 1114 FireReadyEvents(); |
1018 } | 1115 } |
1019 | 1116 |
1020 void BackgroundSyncManager::OnPowerChanged() { | 1117 void BackgroundSyncManager::OnPowerChanged() { |
1021 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1118 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1022 | 1119 |
1023 FireReadyEvents(); | 1120 FireReadyEvents(); |
1024 } | 1121 } |
1025 | 1122 |
1123 // TODO(jkarlin): Figure out how to pass scoped_ptrs with this. | |
1026 template <typename CallbackT, typename... Params> | 1124 template <typename CallbackT, typename... Params> |
1027 void BackgroundSyncManager::CompleteOperationCallback(const CallbackT& callback, | 1125 void BackgroundSyncManager::CompleteOperationCallback(const CallbackT& callback, |
1028 Params... parameters) { | 1126 Params... parameters) { |
1029 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1127 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1030 | 1128 |
1031 callback.Run(parameters...); | 1129 callback.Run(parameters...); |
1032 op_scheduler_.CompleteOperationAndRunNext(); | 1130 op_scheduler_.CompleteOperationAndRunNext(); |
1033 } | 1131 } |
1034 | 1132 |
1133 void BackgroundSyncManager::CompleteStatusAndRegistrationCallback( | |
1134 StatusAndRegistrationCallback callback, | |
1135 BackgroundSyncStatus status, | |
1136 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle) { | |
1137 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
1138 | |
1139 callback.Run(status, registration_handle.Pass()); | |
1140 op_scheduler_.CompleteOperationAndRunNext(); | |
1141 } | |
1142 | |
1143 void BackgroundSyncManager::CompleteStatusAndRegistrationsCallback( | |
1144 StatusAndRegistrationsCallback callback, | |
1145 BackgroundSyncStatus status, | |
1146 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>> | |
1147 registration_handles) { | |
1148 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
1149 | |
1150 callback.Run(status, registration_handles.Pass()); | |
1151 op_scheduler_.CompleteOperationAndRunNext(); | |
1152 } | |
1153 | |
1035 base::Closure BackgroundSyncManager::MakeEmptyCompletion() { | 1154 base::Closure BackgroundSyncManager::MakeEmptyCompletion() { |
1036 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1155 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1037 | 1156 |
1038 return MakeClosureCompletion(base::Bind(base::DoNothing)); | 1157 return MakeClosureCompletion(base::Bind(base::DoNothing)); |
1039 } | 1158 } |
1040 | 1159 |
1041 base::Closure BackgroundSyncManager::MakeClosureCompletion( | 1160 base::Closure BackgroundSyncManager::MakeClosureCompletion( |
1042 const base::Closure& callback) { | 1161 const base::Closure& callback) { |
1043 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1162 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1044 | 1163 |
1045 return base::Bind( | 1164 return base::Bind( |
1046 &BackgroundSyncManager::CompleteOperationCallback<base::Closure>, | 1165 &BackgroundSyncManager::CompleteOperationCallback<base::Closure>, |
1047 weak_ptr_factory_.GetWeakPtr(), callback); | 1166 weak_ptr_factory_.GetWeakPtr(), callback); |
1048 } | 1167 } |
1049 | 1168 |
1050 BackgroundSyncManager::StatusAndRegistrationCallback | 1169 BackgroundSyncManager::StatusAndRegistrationCallback |
1051 BackgroundSyncManager::MakeStatusAndRegistrationCompletion( | 1170 BackgroundSyncManager::MakeStatusAndRegistrationCompletion( |
1052 const StatusAndRegistrationCallback& callback) { | 1171 const StatusAndRegistrationCallback& callback) { |
1053 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1172 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1054 | 1173 |
1055 return base::Bind(&BackgroundSyncManager::CompleteOperationCallback< | 1174 return base::Bind( |
1056 StatusAndRegistrationCallback, BackgroundSyncStatus, | 1175 &BackgroundSyncManager::CompleteStatusAndRegistrationCallback, |
1057 const BackgroundSyncRegistration&>, | 1176 weak_ptr_factory_.GetWeakPtr(), callback); |
1058 weak_ptr_factory_.GetWeakPtr(), callback); | |
1059 } | 1177 } |
1060 | 1178 |
1061 BackgroundSyncManager::StatusAndRegistrationsCallback | 1179 BackgroundSyncManager::StatusAndRegistrationsCallback |
1062 BackgroundSyncManager::MakeStatusAndRegistrationsCompletion( | 1180 BackgroundSyncManager::MakeStatusAndRegistrationsCompletion( |
1063 const StatusAndRegistrationsCallback& callback) { | 1181 const StatusAndRegistrationsCallback& callback) { |
1064 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1182 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1065 | 1183 |
1066 return base::Bind(&BackgroundSyncManager::CompleteOperationCallback< | 1184 return base::Bind( |
1067 StatusAndRegistrationsCallback, BackgroundSyncStatus, | 1185 &BackgroundSyncManager::CompleteStatusAndRegistrationsCallback, |
1068 const std::vector<BackgroundSyncRegistration>&>, | 1186 weak_ptr_factory_.GetWeakPtr(), callback); |
1069 weak_ptr_factory_.GetWeakPtr(), callback); | |
1070 } | 1187 } |
1071 | 1188 |
1072 BackgroundSyncManager::StatusCallback | 1189 BackgroundSyncManager::StatusCallback |
1073 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { | 1190 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { |
1074 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1191 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1075 | 1192 |
1076 return base::Bind( | 1193 return base::Bind( |
1077 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, | 1194 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, |
1078 BackgroundSyncStatus>, | 1195 BackgroundSyncStatus>, |
1079 weak_ptr_factory_.GetWeakPtr(), callback); | 1196 weak_ptr_factory_.GetWeakPtr(), callback); |
1080 } | 1197 } |
1081 | 1198 |
1082 } // namespace content | 1199 } // namespace content |
OLD | NEW |