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" |
| 10 #include "base/single_thread_task_runner.h" |
| 11 #include "base/thread_task_runner_handle.h" |
9 #include "content/browser/background_sync/background_sync_network_observer.h" | 12 #include "content/browser/background_sync/background_sync_network_observer.h" |
10 #include "content/browser/background_sync/background_sync_power_observer.h" | 13 #include "content/browser/background_sync/background_sync_power_observer.h" |
11 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 14 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
12 #include "content/browser/service_worker/service_worker_storage.h" | 15 #include "content/browser/service_worker/service_worker_storage.h" |
13 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
14 | 17 |
15 #if defined(OS_ANDROID) | 18 #if defined(OS_ANDROID) |
16 #include "content/browser/android/background_sync_launcher_android.h" | 19 #include "content/browser/android/background_sync_launcher_android.h" |
17 #endif | 20 #endif |
18 | 21 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 | 71 |
69 void BackgroundSyncManager::Register( | 72 void BackgroundSyncManager::Register( |
70 int64 sw_registration_id, | 73 int64 sw_registration_id, |
71 const BackgroundSyncRegistration& sync_registration, | 74 const BackgroundSyncRegistration& sync_registration, |
72 const StatusAndRegistrationCallback& callback) { | 75 const StatusAndRegistrationCallback& callback) { |
73 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 76 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
74 DCHECK_EQ(BackgroundSyncRegistration::kInvalidRegistrationId, | 77 DCHECK_EQ(BackgroundSyncRegistration::kInvalidRegistrationId, |
75 sync_registration.id); | 78 sync_registration.id); |
76 | 79 |
77 if (disabled_) { | 80 if (disabled_) { |
78 base::MessageLoop::current()->PostTask( | 81 base::ThreadTaskRunnerHandle::Get()->PostTask( |
79 FROM_HERE, | 82 FROM_HERE, |
80 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | 83 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
81 return; | 84 return; |
82 } | 85 } |
83 | 86 |
84 op_scheduler_.ScheduleOperation(base::Bind( | 87 op_scheduler_.ScheduleOperation(base::Bind( |
85 &BackgroundSyncManager::RegisterImpl, weak_ptr_factory_.GetWeakPtr(), | 88 &BackgroundSyncManager::RegisterImpl, weak_ptr_factory_.GetWeakPtr(), |
86 sw_registration_id, sync_registration, | 89 sw_registration_id, sync_registration, |
87 MakeStatusAndRegistrationCompletion(callback))); | 90 MakeStatusAndRegistrationCompletion(callback))); |
88 } | 91 } |
89 | 92 |
90 void BackgroundSyncManager::Unregister( | 93 void BackgroundSyncManager::Unregister( |
91 int64 sw_registration_id, | 94 int64 sw_registration_id, |
92 const std::string& sync_registration_tag, | 95 const std::string& sync_registration_tag, |
93 SyncPeriodicity periodicity, | 96 SyncPeriodicity periodicity, |
94 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 97 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
95 const StatusCallback& callback) { | 98 const StatusCallback& callback) { |
96 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 99 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
97 | 100 |
98 if (disabled_) { | 101 if (disabled_) { |
99 base::MessageLoop::current()->PostTask( | 102 base::ThreadTaskRunnerHandle::Get()->PostTask( |
100 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); | 103 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); |
101 return; | 104 return; |
102 } | 105 } |
103 | 106 |
104 RegistrationKey registration_key(sync_registration_tag, periodicity); | 107 RegistrationKey registration_key(sync_registration_tag, periodicity); |
105 | 108 |
106 op_scheduler_.ScheduleOperation(base::Bind( | 109 op_scheduler_.ScheduleOperation(base::Bind( |
107 &BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(), | 110 &BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(), |
108 sw_registration_id, registration_key, sync_registration_id, | 111 sw_registration_id, registration_key, sync_registration_id, |
109 MakeStatusCompletion(callback))); | 112 MakeStatusCompletion(callback))); |
110 } | 113 } |
111 | 114 |
112 void BackgroundSyncManager::GetRegistration( | 115 void BackgroundSyncManager::GetRegistration( |
113 int64 sw_registration_id, | 116 int64 sw_registration_id, |
114 const std::string& sync_registration_tag, | 117 const std::string& sync_registration_tag, |
115 SyncPeriodicity periodicity, | 118 SyncPeriodicity periodicity, |
116 const StatusAndRegistrationCallback& callback) { | 119 const StatusAndRegistrationCallback& callback) { |
117 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 120 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
118 | 121 |
119 if (disabled_) { | 122 if (disabled_) { |
120 base::MessageLoop::current()->PostTask( | 123 base::ThreadTaskRunnerHandle::Get()->PostTask( |
121 FROM_HERE, | 124 FROM_HERE, |
122 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | 125 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
123 return; | 126 return; |
124 } | 127 } |
125 | 128 |
126 RegistrationKey registration_key(sync_registration_tag, periodicity); | 129 RegistrationKey registration_key(sync_registration_tag, periodicity); |
127 | 130 |
128 op_scheduler_.ScheduleOperation(base::Bind( | 131 op_scheduler_.ScheduleOperation(base::Bind( |
129 &BackgroundSyncManager::GetRegistrationImpl, | 132 &BackgroundSyncManager::GetRegistrationImpl, |
130 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, registration_key, | 133 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, registration_key, |
131 MakeStatusAndRegistrationCompletion(callback))); | 134 MakeStatusAndRegistrationCompletion(callback))); |
132 } | 135 } |
133 | 136 |
134 void BackgroundSyncManager::GetRegistrations( | 137 void BackgroundSyncManager::GetRegistrations( |
135 int64 sw_registration_id, | 138 int64 sw_registration_id, |
136 SyncPeriodicity periodicity, | 139 SyncPeriodicity periodicity, |
137 const StatusAndRegistrationsCallback& callback) { | 140 const StatusAndRegistrationsCallback& callback) { |
138 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 141 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
139 | 142 |
140 if (disabled_) { | 143 if (disabled_) { |
141 base::MessageLoop::current()->PostTask( | 144 base::ThreadTaskRunnerHandle::Get()->PostTask( |
142 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE, | 145 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE, |
143 std::vector<BackgroundSyncRegistration>())); | 146 std::vector<BackgroundSyncRegistration>())); |
144 return; | 147 return; |
145 } | 148 } |
146 | 149 |
147 op_scheduler_.ScheduleOperation( | 150 op_scheduler_.ScheduleOperation( |
148 base::Bind(&BackgroundSyncManager::GetRegistrationsImpl, | 151 base::Bind(&BackgroundSyncManager::GetRegistrationsImpl, |
149 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, | 152 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, |
150 periodicity, MakeStatusAndRegistrationsCompletion(callback))); | 153 periodicity, MakeStatusAndRegistrationsCompletion(callback))); |
151 } | 154 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 | 199 |
197 op_scheduler_.ScheduleOperation(base::Bind(&BackgroundSyncManager::InitImpl, | 200 op_scheduler_.ScheduleOperation(base::Bind(&BackgroundSyncManager::InitImpl, |
198 weak_ptr_factory_.GetWeakPtr(), | 201 weak_ptr_factory_.GetWeakPtr(), |
199 MakeEmptyCompletion())); | 202 MakeEmptyCompletion())); |
200 } | 203 } |
201 | 204 |
202 void BackgroundSyncManager::InitImpl(const base::Closure& callback) { | 205 void BackgroundSyncManager::InitImpl(const base::Closure& callback) { |
203 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 206 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
204 | 207 |
205 if (disabled_) { | 208 if (disabled_) { |
206 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback)); | 209 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 210 base::Bind(callback)); |
207 return; | 211 return; |
208 } | 212 } |
209 | 213 |
210 GetDataFromBackend( | 214 GetDataFromBackend( |
211 kBackgroundSyncUserDataKey, | 215 kBackgroundSyncUserDataKey, |
212 base::Bind(&BackgroundSyncManager::InitDidGetDataFromBackend, | 216 base::Bind(&BackgroundSyncManager::InitDidGetDataFromBackend, |
213 weak_ptr_factory_.GetWeakPtr(), callback)); | 217 weak_ptr_factory_.GetWeakPtr(), callback)); |
214 } | 218 } |
215 | 219 |
216 void BackgroundSyncManager::InitDidGetDataFromBackend( | 220 void BackgroundSyncManager::InitDidGetDataFromBackend( |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 } | 273 } |
270 | 274 |
271 if (corruption_detected) { | 275 if (corruption_detected) { |
272 LOG(ERROR) << "Corruption detected in background sync backend"; | 276 LOG(ERROR) << "Corruption detected in background sync backend"; |
273 DisableAndClearManager(base::Bind(callback)); | 277 DisableAndClearManager(base::Bind(callback)); |
274 return; | 278 return; |
275 } | 279 } |
276 | 280 |
277 FireReadyEvents(); | 281 FireReadyEvents(); |
278 | 282 |
279 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback)); | 283 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 284 base::Bind(callback)); |
280 } | 285 } |
281 | 286 |
282 void BackgroundSyncManager::RegisterImpl( | 287 void BackgroundSyncManager::RegisterImpl( |
283 int64 sw_registration_id, | 288 int64 sw_registration_id, |
284 const BackgroundSyncRegistration& sync_registration, | 289 const BackgroundSyncRegistration& sync_registration, |
285 const StatusAndRegistrationCallback& callback) { | 290 const StatusAndRegistrationCallback& callback) { |
286 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 291 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
287 | 292 |
288 if (disabled_) { | 293 if (disabled_) { |
289 base::MessageLoop::current()->PostTask( | 294 base::ThreadTaskRunnerHandle::Get()->PostTask( |
290 FROM_HERE, | 295 FROM_HERE, |
291 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | 296 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
292 return; | 297 return; |
293 } | 298 } |
294 | 299 |
295 const BackgroundSyncRegistration* existing_registration = LookupRegistration( | 300 const BackgroundSyncRegistration* existing_registration = LookupRegistration( |
296 sw_registration_id, RegistrationKey(sync_registration)); | 301 sw_registration_id, RegistrationKey(sync_registration)); |
297 if (existing_registration && | 302 if (existing_registration && |
298 existing_registration->Equals(sync_registration)) { | 303 existing_registration->Equals(sync_registration)) { |
299 base::MessageLoop::current()->PostTask( | 304 base::ThreadTaskRunnerHandle::Get()->PostTask( |
300 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, *existing_registration)); | 305 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, *existing_registration)); |
301 return; | 306 return; |
302 } | 307 } |
303 | 308 |
304 BackgroundSyncRegistration new_registration = sync_registration; | 309 BackgroundSyncRegistration new_registration = sync_registration; |
305 BackgroundSyncRegistrations* registrations = | 310 BackgroundSyncRegistrations* registrations = |
306 &sw_to_registrations_map_[sw_registration_id]; | 311 &sw_to_registrations_map_[sw_registration_id]; |
307 new_registration.id = registrations->next_id++; | 312 new_registration.id = registrations->next_id++; |
308 | 313 |
309 ServiceWorkerRegistration* sw_registration = | 314 ServiceWorkerRegistration* sw_registration = |
310 service_worker_context_->GetLiveRegistration(sw_registration_id); | 315 service_worker_context_->GetLiveRegistration(sw_registration_id); |
311 if (!sw_registration || !sw_registration->active_version()) { | 316 if (!sw_registration || !sw_registration->active_version()) { |
312 base::MessageLoop::current()->PostTask( | 317 base::ThreadTaskRunnerHandle::Get()->PostTask( |
313 FROM_HERE, base::Bind(callback, ERROR_TYPE_NO_SERVICE_WORKER, | 318 FROM_HERE, base::Bind(callback, ERROR_TYPE_NO_SERVICE_WORKER, |
314 BackgroundSyncRegistration())); | 319 BackgroundSyncRegistration())); |
315 return; | 320 return; |
316 } | 321 } |
317 | 322 |
318 AddRegistrationToMap(sw_registration_id, | 323 AddRegistrationToMap(sw_registration_id, |
319 sw_registration->pattern().GetOrigin(), | 324 sw_registration->pattern().GetOrigin(), |
320 new_registration); | 325 new_registration); |
321 | 326 |
322 StoreRegistrations( | 327 StoreRegistrations( |
323 sw_registration_id, | 328 sw_registration_id, |
324 base::Bind(&BackgroundSyncManager::RegisterDidStore, | 329 base::Bind(&BackgroundSyncManager::RegisterDidStore, |
325 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, | 330 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, |
326 new_registration, callback)); | 331 new_registration, callback)); |
327 } | 332 } |
328 | 333 |
329 void BackgroundSyncManager::DisableAndClearManager( | 334 void BackgroundSyncManager::DisableAndClearManager( |
330 const base::Closure& callback) { | 335 const base::Closure& callback) { |
331 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 336 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
332 | 337 |
333 if (disabled_) { | 338 if (disabled_) { |
334 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback)); | 339 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 340 base::Bind(callback)); |
335 return; | 341 return; |
336 } | 342 } |
337 | 343 |
338 disabled_ = true; | 344 disabled_ = true; |
339 sw_to_registrations_map_.clear(); | 345 sw_to_registrations_map_.clear(); |
340 | 346 |
341 // Delete all backend entries. The memory representation of registered syncs | 347 // Delete all backend entries. The memory representation of registered syncs |
342 // may be out of sync with storage (e.g., due to corruption detection on | 348 // may be out of sync with storage (e.g., due to corruption detection on |
343 // loading from storage), so reload the registrations from storage again. | 349 // loading from storage), so reload the registrations from storage again. |
344 GetDataFromBackend( | 350 GetDataFromBackend( |
345 kBackgroundSyncUserDataKey, | 351 kBackgroundSyncUserDataKey, |
346 base::Bind(&BackgroundSyncManager::DisableAndClearDidGetRegistrations, | 352 base::Bind(&BackgroundSyncManager::DisableAndClearDidGetRegistrations, |
347 weak_ptr_factory_.GetWeakPtr(), callback)); | 353 weak_ptr_factory_.GetWeakPtr(), callback)); |
348 } | 354 } |
349 | 355 |
350 void BackgroundSyncManager::DisableAndClearDidGetRegistrations( | 356 void BackgroundSyncManager::DisableAndClearDidGetRegistrations( |
351 const base::Closure& callback, | 357 const base::Closure& callback, |
352 const std::vector<std::pair<int64, std::string>>& user_data, | 358 const std::vector<std::pair<int64, std::string>>& user_data, |
353 ServiceWorkerStatusCode status) { | 359 ServiceWorkerStatusCode status) { |
354 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 360 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
355 | 361 |
356 if (status != SERVICE_WORKER_OK || user_data.empty()) { | 362 if (status != SERVICE_WORKER_OK || user_data.empty()) { |
357 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback)); | 363 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 364 base::Bind(callback)); |
358 return; | 365 return; |
359 } | 366 } |
360 | 367 |
361 base::Closure barrier_closure = | 368 base::Closure barrier_closure = |
362 base::BarrierClosure(user_data.size(), base::Bind(callback)); | 369 base::BarrierClosure(user_data.size(), base::Bind(callback)); |
363 | 370 |
364 for (const auto& sw_id_and_regs : user_data) { | 371 for (const auto& sw_id_and_regs : user_data) { |
365 service_worker_context_->ClearRegistrationUserData( | 372 service_worker_context_->ClearRegistrationUserData( |
366 sw_id_and_regs.first, kBackgroundSyncUserDataKey, | 373 sw_id_and_regs.first, kBackgroundSyncUserDataKey, |
367 base::Bind(&BackgroundSyncManager::DisableAndClearManagerClearedOne, | 374 base::Bind(&BackgroundSyncManager::DisableAndClearManagerClearedOne, |
368 weak_ptr_factory_.GetWeakPtr(), barrier_closure)); | 375 weak_ptr_factory_.GetWeakPtr(), barrier_closure)); |
369 } | 376 } |
370 } | 377 } |
371 | 378 |
372 void BackgroundSyncManager::DisableAndClearManagerClearedOne( | 379 void BackgroundSyncManager::DisableAndClearManagerClearedOne( |
373 const base::Closure& barrier_closure, | 380 const base::Closure& barrier_closure, |
374 ServiceWorkerStatusCode status) { | 381 ServiceWorkerStatusCode status) { |
375 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 382 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
376 | 383 |
377 // The status doesn't matter at this point, there is nothing else to be done. | 384 // The status doesn't matter at this point, there is nothing else to be done. |
378 base::MessageLoop::current()->PostTask(FROM_HERE, | 385 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
379 base::Bind(barrier_closure)); | 386 base::Bind(barrier_closure)); |
380 } | 387 } |
381 | 388 |
382 BackgroundSyncManager::BackgroundSyncRegistration* | 389 BackgroundSyncManager::BackgroundSyncRegistration* |
383 BackgroundSyncManager::LookupRegistration( | 390 BackgroundSyncManager::LookupRegistration( |
384 int64 sw_registration_id, | 391 int64 sw_registration_id, |
385 const RegistrationKey& registration_key) { | 392 const RegistrationKey& registration_key) { |
386 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 393 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
387 | 394 |
388 SWIdToRegistrationsMap::iterator it = | 395 SWIdToRegistrationsMap::iterator it = |
389 sw_to_registrations_map_.find(sw_registration_id); | 396 sw_to_registrations_map_.find(sw_registration_id); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 void BackgroundSyncManager::RegisterDidStore( | 445 void BackgroundSyncManager::RegisterDidStore( |
439 int64 sw_registration_id, | 446 int64 sw_registration_id, |
440 const BackgroundSyncRegistration& new_registration, | 447 const BackgroundSyncRegistration& new_registration, |
441 const StatusAndRegistrationCallback& callback, | 448 const StatusAndRegistrationCallback& callback, |
442 ServiceWorkerStatusCode status) { | 449 ServiceWorkerStatusCode status) { |
443 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 450 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
444 | 451 |
445 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { | 452 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
446 // The registration is gone. | 453 // The registration is gone. |
447 sw_to_registrations_map_.erase(sw_registration_id); | 454 sw_to_registrations_map_.erase(sw_registration_id); |
448 base::MessageLoop::current()->PostTask( | 455 base::ThreadTaskRunnerHandle::Get()->PostTask( |
449 FROM_HERE, | 456 FROM_HERE, |
450 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | 457 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
451 return; | 458 return; |
452 } | 459 } |
453 | 460 |
454 if (status != SERVICE_WORKER_OK) { | 461 if (status != SERVICE_WORKER_OK) { |
455 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " | 462 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " |
456 "failure."; | 463 "failure."; |
457 DisableAndClearManager( | 464 DisableAndClearManager( |
458 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | 465 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
459 return; | 466 return; |
460 } | 467 } |
461 | 468 |
462 FireReadyEvents(); | 469 FireReadyEvents(); |
463 base::MessageLoop::current()->PostTask( | 470 base::ThreadTaskRunnerHandle::Get()->PostTask( |
464 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, new_registration)); | 471 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, new_registration)); |
465 } | 472 } |
466 | 473 |
467 void BackgroundSyncManager::RemoveRegistrationFromMap( | 474 void BackgroundSyncManager::RemoveRegistrationFromMap( |
468 int64 sw_registration_id, | 475 int64 sw_registration_id, |
469 const RegistrationKey& registration_key) { | 476 const RegistrationKey& registration_key) { |
470 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 477 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
471 DCHECK(LookupRegistration(sw_registration_id, registration_key)); | 478 DCHECK(LookupRegistration(sw_registration_id, registration_key)); |
472 | 479 |
473 BackgroundSyncRegistrations* registrations = | 480 BackgroundSyncRegistrations* registrations = |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
523 } | 530 } |
524 | 531 |
525 void BackgroundSyncManager::UnregisterImpl( | 532 void BackgroundSyncManager::UnregisterImpl( |
526 int64 sw_registration_id, | 533 int64 sw_registration_id, |
527 const RegistrationKey& registration_key, | 534 const RegistrationKey& registration_key, |
528 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 535 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
529 const StatusCallback& callback) { | 536 const StatusCallback& callback) { |
530 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 537 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
531 | 538 |
532 if (disabled_) { | 539 if (disabled_) { |
533 base::MessageLoop::current()->PostTask( | 540 base::ThreadTaskRunnerHandle::Get()->PostTask( |
534 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); | 541 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); |
535 return; | 542 return; |
536 } | 543 } |
537 | 544 |
538 const BackgroundSyncRegistration* existing_registration = | 545 const BackgroundSyncRegistration* existing_registration = |
539 LookupRegistration(sw_registration_id, registration_key); | 546 LookupRegistration(sw_registration_id, registration_key); |
540 if (!existing_registration || | 547 if (!existing_registration || |
541 existing_registration->id != sync_registration_id) { | 548 existing_registration->id != sync_registration_id) { |
542 base::MessageLoop::current()->PostTask( | 549 base::ThreadTaskRunnerHandle::Get()->PostTask( |
543 FROM_HERE, base::Bind(callback, ERROR_TYPE_NOT_FOUND)); | 550 FROM_HERE, base::Bind(callback, ERROR_TYPE_NOT_FOUND)); |
544 return; | 551 return; |
545 } | 552 } |
546 | 553 |
547 RemoveRegistrationFromMap(sw_registration_id, registration_key); | 554 RemoveRegistrationFromMap(sw_registration_id, registration_key); |
548 | 555 |
549 StoreRegistrations( | 556 StoreRegistrations( |
550 sw_registration_id, | 557 sw_registration_id, |
551 base::Bind(&BackgroundSyncManager::UnregisterDidStore, | 558 base::Bind(&BackgroundSyncManager::UnregisterDidStore, |
552 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, callback)); | 559 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, callback)); |
553 } | 560 } |
554 | 561 |
555 void BackgroundSyncManager::UnregisterDidStore( | 562 void BackgroundSyncManager::UnregisterDidStore( |
556 int64 sw_registration_id, | 563 int64 sw_registration_id, |
557 const StatusCallback& callback, | 564 const StatusCallback& callback, |
558 ServiceWorkerStatusCode status) { | 565 ServiceWorkerStatusCode status) { |
559 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 566 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
560 | 567 |
561 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { | 568 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
562 // ServiceWorker was unregistered. | 569 // ServiceWorker was unregistered. |
563 sw_to_registrations_map_.erase(sw_registration_id); | 570 sw_to_registrations_map_.erase(sw_registration_id); |
564 base::MessageLoop::current()->PostTask( | 571 base::ThreadTaskRunnerHandle::Get()->PostTask( |
565 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); | 572 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); |
566 return; | 573 return; |
567 } | 574 } |
568 | 575 |
569 if (status != SERVICE_WORKER_OK) { | 576 if (status != SERVICE_WORKER_OK) { |
570 LOG(ERROR) << "BackgroundSync failed to unregister due to backend failure."; | 577 LOG(ERROR) << "BackgroundSync failed to unregister due to backend failure."; |
571 DisableAndClearManager(base::Bind(callback, ERROR_TYPE_STORAGE)); | 578 DisableAndClearManager(base::Bind(callback, ERROR_TYPE_STORAGE)); |
572 return; | 579 return; |
573 } | 580 } |
574 | 581 |
575 base::MessageLoop::current()->PostTask(FROM_HERE, | 582 base::ThreadTaskRunnerHandle::Get()->PostTask( |
576 base::Bind(callback, ERROR_TYPE_OK)); | 583 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK)); |
577 } | 584 } |
578 | 585 |
579 void BackgroundSyncManager::GetRegistrationImpl( | 586 void BackgroundSyncManager::GetRegistrationImpl( |
580 int64 sw_registration_id, | 587 int64 sw_registration_id, |
581 const RegistrationKey& registration_key, | 588 const RegistrationKey& registration_key, |
582 const StatusAndRegistrationCallback& callback) { | 589 const StatusAndRegistrationCallback& callback) { |
583 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 590 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
584 | 591 |
585 if (disabled_) { | 592 if (disabled_) { |
586 base::MessageLoop::current()->PostTask( | 593 base::ThreadTaskRunnerHandle::Get()->PostTask( |
587 FROM_HERE, | 594 FROM_HERE, |
588 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | 595 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
589 return; | 596 return; |
590 } | 597 } |
591 | 598 |
592 const BackgroundSyncRegistration* out_registration = | 599 const BackgroundSyncRegistration* out_registration = |
593 LookupRegistration(sw_registration_id, registration_key); | 600 LookupRegistration(sw_registration_id, registration_key); |
594 if (!out_registration) { | 601 if (!out_registration) { |
595 base::MessageLoop::current()->PostTask( | 602 base::ThreadTaskRunnerHandle::Get()->PostTask( |
596 FROM_HERE, base::Bind(callback, ERROR_TYPE_NOT_FOUND, | 603 FROM_HERE, base::Bind(callback, ERROR_TYPE_NOT_FOUND, |
597 BackgroundSyncRegistration())); | 604 BackgroundSyncRegistration())); |
598 return; | 605 return; |
599 } | 606 } |
600 | 607 |
601 base::MessageLoop::current()->PostTask( | 608 base::ThreadTaskRunnerHandle::Get()->PostTask( |
602 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, *out_registration)); | 609 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, *out_registration)); |
603 } | 610 } |
604 | 611 |
605 void BackgroundSyncManager::GetRegistrationsImpl( | 612 void BackgroundSyncManager::GetRegistrationsImpl( |
606 int64 sw_registration_id, | 613 int64 sw_registration_id, |
607 SyncPeriodicity periodicity, | 614 SyncPeriodicity periodicity, |
608 const StatusAndRegistrationsCallback& callback) { | 615 const StatusAndRegistrationsCallback& callback) { |
609 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 616 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
610 | 617 |
611 std::vector<BackgroundSyncRegistration> out_registrations; | 618 std::vector<BackgroundSyncRegistration> out_registrations; |
612 | 619 |
613 if (disabled_) { | 620 if (disabled_) { |
614 base::MessageLoop::current()->PostTask( | 621 base::ThreadTaskRunnerHandle::Get()->PostTask( |
615 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE, out_registrations)); | 622 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE, out_registrations)); |
616 return; | 623 return; |
617 } | 624 } |
618 | 625 |
619 SWIdToRegistrationsMap::iterator it = | 626 SWIdToRegistrationsMap::iterator it = |
620 sw_to_registrations_map_.find(sw_registration_id); | 627 sw_to_registrations_map_.find(sw_registration_id); |
621 | 628 |
622 if (it != sw_to_registrations_map_.end()) { | 629 if (it != sw_to_registrations_map_.end()) { |
623 const BackgroundSyncRegistrations& registrations = it->second; | 630 const BackgroundSyncRegistrations& registrations = it->second; |
624 for (const auto& tag_and_registration : registrations.registration_map) { | 631 for (const auto& tag_and_registration : registrations.registration_map) { |
625 const BackgroundSyncRegistration& registration = | 632 const BackgroundSyncRegistration& registration = |
626 tag_and_registration.second; | 633 tag_and_registration.second; |
627 if (registration.periodicity == periodicity) | 634 if (registration.periodicity == periodicity) |
628 out_registrations.push_back(registration); | 635 out_registrations.push_back(registration); |
629 } | 636 } |
630 } | 637 } |
631 | 638 |
632 base::MessageLoop::current()->PostTask( | 639 base::ThreadTaskRunnerHandle::Get()->PostTask( |
633 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, out_registrations)); | 640 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, out_registrations)); |
634 } | 641 } |
635 | 642 |
636 bool BackgroundSyncManager::IsRegistrationReadyToFire( | 643 bool BackgroundSyncManager::IsRegistrationReadyToFire( |
637 const BackgroundSyncRegistration& registration) { | 644 const BackgroundSyncRegistration& registration) { |
638 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 645 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
639 | 646 |
640 // TODO(jkarlin): Add support for firing periodic registrations. | 647 // TODO(jkarlin): Add support for firing periodic registrations. |
641 if (registration.periodicity == SYNC_PERIODIC) | 648 if (registration.periodicity == SYNC_PERIODIC) |
642 return false; | 649 return false; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
689 | 696 |
690 op_scheduler_.ScheduleOperation( | 697 op_scheduler_.ScheduleOperation( |
691 base::Bind(&BackgroundSyncManager::FireReadyEventsImpl, | 698 base::Bind(&BackgroundSyncManager::FireReadyEventsImpl, |
692 weak_ptr_factory_.GetWeakPtr(), MakeEmptyCompletion())); | 699 weak_ptr_factory_.GetWeakPtr(), MakeEmptyCompletion())); |
693 } | 700 } |
694 | 701 |
695 void BackgroundSyncManager::FireReadyEventsImpl(const base::Closure& callback) { | 702 void BackgroundSyncManager::FireReadyEventsImpl(const base::Closure& callback) { |
696 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 703 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
697 | 704 |
698 if (disabled_) { | 705 if (disabled_) { |
699 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback)); | 706 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 707 base::Bind(callback)); |
700 return; | 708 return; |
701 } | 709 } |
702 | 710 |
703 // Find the registrations that are ready to run. | 711 // Find the registrations that are ready to run. |
704 std::vector<std::pair<int64, RegistrationKey>> sw_id_and_keys_to_fire; | 712 std::vector<std::pair<int64, RegistrationKey>> sw_id_and_keys_to_fire; |
705 | 713 |
706 for (auto& sw_id_and_registrations : sw_to_registrations_map_) { | 714 for (auto& sw_id_and_registrations : sw_to_registrations_map_) { |
707 const int64 service_worker_id = sw_id_and_registrations.first; | 715 const int64 service_worker_id = sw_id_and_registrations.first; |
708 for (auto& key_and_registration : | 716 for (auto& key_and_registration : |
709 sw_id_and_registrations.second.registration_map) { | 717 sw_id_and_registrations.second.registration_map) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
742 void BackgroundSyncManager::FireReadyEventsDidFindRegistration( | 750 void BackgroundSyncManager::FireReadyEventsDidFindRegistration( |
743 const RegistrationKey& registration_key, | 751 const RegistrationKey& registration_key, |
744 BackgroundSyncRegistration::RegistrationId registration_id, | 752 BackgroundSyncRegistration::RegistrationId registration_id, |
745 const base::Closure& callback, | 753 const base::Closure& callback, |
746 ServiceWorkerStatusCode service_worker_status, | 754 ServiceWorkerStatusCode service_worker_status, |
747 const scoped_refptr<ServiceWorkerRegistration>& | 755 const scoped_refptr<ServiceWorkerRegistration>& |
748 service_worker_registration) { | 756 service_worker_registration) { |
749 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 757 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
750 | 758 |
751 if (service_worker_status != SERVICE_WORKER_OK) { | 759 if (service_worker_status != SERVICE_WORKER_OK) { |
752 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback)); | 760 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 761 base::Bind(callback)); |
753 return; | 762 return; |
754 } | 763 } |
755 | 764 |
756 FireOneShotSync( | 765 FireOneShotSync( |
757 service_worker_registration->active_version(), | 766 service_worker_registration->active_version(), |
758 base::Bind(&BackgroundSyncManager::EventComplete, | 767 base::Bind(&BackgroundSyncManager::EventComplete, |
759 weak_ptr_factory_.GetWeakPtr(), service_worker_registration, | 768 weak_ptr_factory_.GetWeakPtr(), service_worker_registration, |
760 service_worker_registration->id(), registration_key, | 769 service_worker_registration->id(), registration_key, |
761 registration_id)); | 770 registration_id)); |
762 | 771 |
763 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback)); | 772 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 773 base::Bind(callback)); |
764 } | 774 } |
765 | 775 |
766 // |service_worker_registration| is just to keep the registration alive | 776 // |service_worker_registration| is just to keep the registration alive |
767 // while the event is firing. | 777 // while the event is firing. |
768 void BackgroundSyncManager::EventComplete( | 778 void BackgroundSyncManager::EventComplete( |
769 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, | 779 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, |
770 int64 service_worker_id, | 780 int64 service_worker_id, |
771 const RegistrationKey& key, | 781 const RegistrationKey& key, |
772 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 782 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
773 ServiceWorkerStatusCode status_code) { | 783 ServiceWorkerStatusCode status_code) { |
(...skipping 10 matching lines...) Expand all Loading... |
784 | 794 |
785 void BackgroundSyncManager::EventCompleteImpl( | 795 void BackgroundSyncManager::EventCompleteImpl( |
786 int64 service_worker_id, | 796 int64 service_worker_id, |
787 const RegistrationKey& key, | 797 const RegistrationKey& key, |
788 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 798 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
789 ServiceWorkerStatusCode status_code, | 799 ServiceWorkerStatusCode status_code, |
790 const base::Closure& callback) { | 800 const base::Closure& callback) { |
791 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 801 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
792 | 802 |
793 if (disabled_) { | 803 if (disabled_) { |
794 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback)); | 804 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 805 base::Bind(callback)); |
795 return; | 806 return; |
796 } | 807 } |
797 | 808 |
798 BackgroundSyncRegistration* registration = | 809 BackgroundSyncRegistration* registration = |
799 LookupRegistration(service_worker_id, key); | 810 LookupRegistration(service_worker_id, key); |
800 if (!registration || registration->id != sync_registration_id) { | 811 if (!registration || registration->id != sync_registration_id) { |
801 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback)); | 812 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 813 base::Bind(callback)); |
802 return; | 814 return; |
803 } | 815 } |
804 | 816 |
805 if (registration->periodicity == SYNC_ONE_SHOT) { | 817 if (registration->periodicity == SYNC_ONE_SHOT) { |
806 if (status_code != SERVICE_WORKER_OK) { | 818 if (status_code != SERVICE_WORKER_OK) { |
807 // TODO(jkarlin) Fire the sync event on the next page load controlled by | 819 // TODO(jkarlin) Fire the sync event on the next page load controlled by |
808 // this registration. (crbug.com/479665) | 820 // this registration. (crbug.com/479665) |
809 registration->sync_state = SYNC_STATE_FAILED; | 821 registration->sync_state = SYNC_STATE_FAILED; |
810 } else { | 822 } else { |
811 registration = nullptr; | 823 registration = nullptr; |
(...skipping 12 matching lines...) Expand all Loading... |
824 | 836 |
825 void BackgroundSyncManager::EventCompleteDidStore( | 837 void BackgroundSyncManager::EventCompleteDidStore( |
826 int64 service_worker_id, | 838 int64 service_worker_id, |
827 const base::Closure& callback, | 839 const base::Closure& callback, |
828 ServiceWorkerStatusCode status_code) { | 840 ServiceWorkerStatusCode status_code) { |
829 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 841 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
830 | 842 |
831 if (status_code == SERVICE_WORKER_ERROR_NOT_FOUND) { | 843 if (status_code == SERVICE_WORKER_ERROR_NOT_FOUND) { |
832 // The registration is gone. | 844 // The registration is gone. |
833 sw_to_registrations_map_.erase(service_worker_id); | 845 sw_to_registrations_map_.erase(service_worker_id); |
834 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback)); | 846 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 847 base::Bind(callback)); |
835 return; | 848 return; |
836 } | 849 } |
837 | 850 |
838 if (status_code != SERVICE_WORKER_OK) { | 851 if (status_code != SERVICE_WORKER_OK) { |
839 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " | 852 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " |
840 "failure."; | 853 "failure."; |
841 DisableAndClearManager(base::Bind(callback)); | 854 DisableAndClearManager(base::Bind(callback)); |
842 return; | 855 return; |
843 } | 856 } |
844 | 857 |
845 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback)); | 858 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 859 base::Bind(callback)); |
846 } | 860 } |
847 | 861 |
848 void BackgroundSyncManager::OnRegistrationDeletedImpl( | 862 void BackgroundSyncManager::OnRegistrationDeletedImpl( |
849 int64 registration_id, | 863 int64 registration_id, |
850 const base::Closure& callback) { | 864 const base::Closure& callback) { |
851 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 865 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
852 | 866 |
853 // The backend (ServiceWorkerStorage) will delete the data, so just delete the | 867 // The backend (ServiceWorkerStorage) will delete the data, so just delete the |
854 // memory representation here. | 868 // memory representation here. |
855 sw_to_registrations_map_.erase(registration_id); | 869 sw_to_registrations_map_.erase(registration_id); |
856 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback)); | 870 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 871 base::Bind(callback)); |
857 } | 872 } |
858 | 873 |
859 void BackgroundSyncManager::OnStorageWipedImpl(const base::Closure& callback) { | 874 void BackgroundSyncManager::OnStorageWipedImpl(const base::Closure& callback) { |
860 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 875 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
861 | 876 |
862 sw_to_registrations_map_.clear(); | 877 sw_to_registrations_map_.clear(); |
863 disabled_ = false; | 878 disabled_ = false; |
864 InitImpl(callback); | 879 InitImpl(callback); |
865 } | 880 } |
866 | 881 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
919 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { | 934 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { |
920 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 935 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
921 | 936 |
922 return base::Bind( | 937 return base::Bind( |
923 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, | 938 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, |
924 ErrorType>, | 939 ErrorType>, |
925 weak_ptr_factory_.GetWeakPtr(), callback); | 940 weak_ptr_factory_.GetWeakPtr(), callback); |
926 } | 941 } |
927 | 942 |
928 } // namespace content | 943 } // namespace content |
OLD | NEW |