| 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_network_observer.h" | 13 #include "content/browser/background_sync/background_sync_network_observer.h" |
| 13 #include "content/browser/background_sync/background_sync_power_observer.h" | 14 #include "content/browser/background_sync/background_sync_power_observer.h" |
| 14 #include "content/browser/background_sync/background_sync_registration_options.h
" | 15 #include "content/browser/background_sync/background_sync_registration_options.h
" |
| 15 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 16 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 16 #include "content/browser/service_worker/service_worker_storage.h" | 17 #include "content/browser/service_worker/service_worker_storage.h" |
| 17 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 18 | 19 |
| 19 #if defined(OS_ANDROID) | 20 #if defined(OS_ANDROID) |
| 20 #include "content/browser/android/background_sync_launcher_android.h" | 21 #include "content/browser/android/background_sync_launcher_android.h" |
| 21 #endif | 22 #endif |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 SyncPeriodicity periodicity) | 69 SyncPeriodicity periodicity) |
| 69 : value_(periodicity == SYNC_ONE_SHOT ? "o_" + tag : "p_" + tag) { | 70 : value_(periodicity == SYNC_ONE_SHOT ? "o_" + tag : "p_" + tag) { |
| 70 } | 71 } |
| 71 | 72 |
| 72 void BackgroundSyncManager::Register( | 73 void BackgroundSyncManager::Register( |
| 73 int64 sw_registration_id, | 74 int64 sw_registration_id, |
| 74 const BackgroundSyncRegistrationOptions& options, | 75 const BackgroundSyncRegistrationOptions& options, |
| 75 const StatusAndRegistrationCallback& callback) { | 76 const StatusAndRegistrationCallback& callback) { |
| 76 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 77 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 77 | 78 |
| 79 // For UMA, determine here whether the sync could fire immediately |
| 80 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = |
| 81 AreOptionConditionsMet(options) |
| 82 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE |
| 83 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; |
| 84 |
| 78 if (disabled_) { | 85 if (disabled_) { |
| 86 BackgroundSyncMetrics::CountRegister( |
| 87 options.periodicity, registration_could_fire, |
| 88 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, |
| 89 ERROR_TYPE_STORAGE); |
| 79 base::ThreadTaskRunnerHandle::Get()->PostTask( | 90 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 80 FROM_HERE, | 91 FROM_HERE, |
| 81 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | 92 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
| 82 return; | 93 return; |
| 83 } | 94 } |
| 84 | 95 |
| 85 op_scheduler_.ScheduleOperation( | 96 op_scheduler_.ScheduleOperation( |
| 86 base::Bind(&BackgroundSyncManager::RegisterImpl, | 97 base::Bind(&BackgroundSyncManager::RegisterImpl, |
| 87 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options, | 98 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options, |
| 88 MakeStatusAndRegistrationCompletion(callback))); | 99 MakeStatusAndRegistrationCompletion(callback))); |
| 89 } | 100 } |
| 90 | 101 |
| 91 void BackgroundSyncManager::Unregister( | 102 void BackgroundSyncManager::Unregister( |
| 92 int64 sw_registration_id, | 103 int64 sw_registration_id, |
| 93 const std::string& sync_registration_tag, | 104 const std::string& sync_registration_tag, |
| 94 SyncPeriodicity periodicity, | 105 SyncPeriodicity periodicity, |
| 95 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 106 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| 96 const StatusCallback& callback) { | 107 const StatusCallback& callback) { |
| 97 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 108 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 98 | 109 |
| 99 if (disabled_) { | 110 if (disabled_) { |
| 111 BackgroundSyncMetrics::CountUnregister(periodicity, ERROR_TYPE_STORAGE); |
| 100 base::ThreadTaskRunnerHandle::Get()->PostTask( | 112 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 101 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); | 113 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); |
| 102 return; | 114 return; |
| 103 } | 115 } |
| 104 | 116 |
| 105 RegistrationKey registration_key(sync_registration_tag, periodicity); | 117 RegistrationKey registration_key(sync_registration_tag, periodicity); |
| 106 | 118 |
| 107 op_scheduler_.ScheduleOperation(base::Bind( | 119 op_scheduler_.ScheduleOperation(base::Bind( |
| 108 &BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(), | 120 &BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(), |
| 109 sw_registration_id, registration_key, sync_registration_id, | 121 sw_registration_id, registration_key, sync_registration_id, periodicity, |
| 110 MakeStatusCompletion(callback))); | 122 MakeStatusCompletion(callback))); |
| 111 } | 123 } |
| 112 | 124 |
| 113 void BackgroundSyncManager::GetRegistration( | 125 void BackgroundSyncManager::GetRegistration( |
| 114 int64 sw_registration_id, | 126 int64 sw_registration_id, |
| 115 const std::string& sync_registration_tag, | 127 const std::string& sync_registration_tag, |
| 116 SyncPeriodicity periodicity, | 128 SyncPeriodicity periodicity, |
| 117 const StatusAndRegistrationCallback& callback) { | 129 const StatusAndRegistrationCallback& callback) { |
| 118 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 130 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 119 | 131 |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 296 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 285 base::Bind(callback)); | 297 base::Bind(callback)); |
| 286 } | 298 } |
| 287 | 299 |
| 288 void BackgroundSyncManager::RegisterImpl( | 300 void BackgroundSyncManager::RegisterImpl( |
| 289 int64 sw_registration_id, | 301 int64 sw_registration_id, |
| 290 const BackgroundSyncRegistrationOptions& options, | 302 const BackgroundSyncRegistrationOptions& options, |
| 291 const StatusAndRegistrationCallback& callback) { | 303 const StatusAndRegistrationCallback& callback) { |
| 292 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 304 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 293 | 305 |
| 306 // For UMA, determine here whether the sync could fire immediately |
| 307 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = |
| 308 AreOptionConditionsMet(options) |
| 309 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE |
| 310 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; |
| 311 |
| 294 if (disabled_) { | 312 if (disabled_) { |
| 313 BackgroundSyncMetrics::CountRegister( |
| 314 options.periodicity, registration_could_fire, |
| 315 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, |
| 316 ERROR_TYPE_STORAGE); |
| 295 base::ThreadTaskRunnerHandle::Get()->PostTask( | 317 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 296 FROM_HERE, | 318 FROM_HERE, |
| 297 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | 319 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
| 298 return; | 320 return; |
| 299 } | 321 } |
| 300 | 322 |
| 301 BackgroundSyncRegistration* existing_registration = | 323 BackgroundSyncRegistration* existing_registration = |
| 302 LookupRegistration(sw_registration_id, RegistrationKey(options)); | 324 LookupRegistration(sw_registration_id, RegistrationKey(options)); |
| 303 if (existing_registration && | 325 if (existing_registration && |
| 304 existing_registration->options()->Equals(options)) { | 326 existing_registration->options()->Equals(options)) { |
| 305 if (existing_registration->sync_state() == SYNC_STATE_FAILED) { | 327 if (existing_registration->sync_state() == SYNC_STATE_FAILED) { |
| 306 existing_registration->set_sync_state(SYNC_STATE_PENDING); | 328 existing_registration->set_sync_state(SYNC_STATE_PENDING); |
| 307 StoreRegistrations( | 329 StoreRegistrations( |
| 308 sw_registration_id, | 330 sw_registration_id, |
| 309 base::Bind(&BackgroundSyncManager::RegisterDidStore, | 331 base::Bind(&BackgroundSyncManager::RegisterDidStore, |
| 310 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, | 332 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, |
| 311 *existing_registration, callback)); | 333 *existing_registration, callback)); |
| 312 return; | 334 return; |
| 313 } | 335 } |
| 314 | 336 |
| 337 // Record the duplicated registration |
| 338 BackgroundSyncMetrics::CountRegister( |
| 339 existing_registration->options()->periodicity, registration_could_fire, |
| 340 BackgroundSyncMetrics::REGISTRATION_IS_DUPLICATE, ERROR_TYPE_OK); |
| 341 |
| 315 base::ThreadTaskRunnerHandle::Get()->PostTask( | 342 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 316 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, *existing_registration)); | 343 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, *existing_registration)); |
| 317 return; | 344 return; |
| 318 } | 345 } |
| 319 | 346 |
| 320 BackgroundSyncRegistration new_registration; | 347 BackgroundSyncRegistration new_registration; |
| 321 *new_registration.options() = options; | 348 *new_registration.options() = options; |
| 322 | 349 |
| 323 BackgroundSyncRegistrations* registrations = | 350 BackgroundSyncRegistrations* registrations = |
| 324 &sw_to_registrations_map_[sw_registration_id]; | 351 &sw_to_registrations_map_[sw_registration_id]; |
| 325 new_registration.set_id(registrations->next_id++); | 352 new_registration.set_id(registrations->next_id++); |
| 326 | 353 |
| 327 ServiceWorkerRegistration* sw_registration = | 354 ServiceWorkerRegistration* sw_registration = |
| 328 service_worker_context_->GetLiveRegistration(sw_registration_id); | 355 service_worker_context_->GetLiveRegistration(sw_registration_id); |
| 329 if (!sw_registration || !sw_registration->active_version()) { | 356 if (!sw_registration || !sw_registration->active_version()) { |
| 357 BackgroundSyncMetrics::CountRegister( |
| 358 options.periodicity, registration_could_fire, |
| 359 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, |
| 360 ERROR_TYPE_NO_SERVICE_WORKER); |
| 330 base::ThreadTaskRunnerHandle::Get()->PostTask( | 361 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 331 FROM_HERE, base::Bind(callback, ERROR_TYPE_NO_SERVICE_WORKER, | 362 FROM_HERE, base::Bind(callback, ERROR_TYPE_NO_SERVICE_WORKER, |
| 332 BackgroundSyncRegistration())); | 363 BackgroundSyncRegistration())); |
| 333 return; | 364 return; |
| 334 } | 365 } |
| 335 | 366 |
| 336 AddRegistrationToMap(sw_registration_id, | 367 AddRegistrationToMap(sw_registration_id, |
| 337 sw_registration->pattern().GetOrigin(), | 368 sw_registration->pattern().GetOrigin(), |
| 338 new_registration); | 369 new_registration); |
| 339 | 370 |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 455 kBackgroundSyncUserDataKey, serialized, callback); | 486 kBackgroundSyncUserDataKey, serialized, callback); |
| 456 } | 487 } |
| 457 | 488 |
| 458 void BackgroundSyncManager::RegisterDidStore( | 489 void BackgroundSyncManager::RegisterDidStore( |
| 459 int64 sw_registration_id, | 490 int64 sw_registration_id, |
| 460 const BackgroundSyncRegistration& new_registration, | 491 const BackgroundSyncRegistration& new_registration, |
| 461 const StatusAndRegistrationCallback& callback, | 492 const StatusAndRegistrationCallback& callback, |
| 462 ServiceWorkerStatusCode status) { | 493 ServiceWorkerStatusCode status) { |
| 463 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 494 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 464 | 495 |
| 496 // For UMA, determine here whether the sync could fire immediately |
| 497 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = |
| 498 AreOptionConditionsMet(*new_registration.options()) |
| 499 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE |
| 500 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; |
| 501 |
| 465 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { | 502 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
| 466 // The registration is gone. | 503 // The service worker registration is gone. |
| 504 BackgroundSyncMetrics::CountRegister( |
| 505 new_registration.options()->periodicity, registration_could_fire, |
| 506 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, |
| 507 ERROR_TYPE_STORAGE); |
| 467 sw_to_registrations_map_.erase(sw_registration_id); | 508 sw_to_registrations_map_.erase(sw_registration_id); |
| 468 base::ThreadTaskRunnerHandle::Get()->PostTask( | 509 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 469 FROM_HERE, | 510 FROM_HERE, |
| 470 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | 511 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
| 471 return; | 512 return; |
| 472 } | 513 } |
| 473 | 514 |
| 474 if (status != SERVICE_WORKER_OK) { | 515 if (status != SERVICE_WORKER_OK) { |
| 475 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " | 516 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " |
| 476 "failure."; | 517 "failure."; |
| 518 BackgroundSyncMetrics::CountRegister( |
| 519 new_registration.options()->periodicity, registration_could_fire, |
| 520 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, |
| 521 ERROR_TYPE_STORAGE); |
| 477 DisableAndClearManager( | 522 DisableAndClearManager( |
| 478 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | 523 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
| 479 return; | 524 return; |
| 480 } | 525 } |
| 481 | 526 |
| 527 BackgroundSyncMetrics::CountRegister( |
| 528 new_registration.options()->periodicity, registration_could_fire, |
| 529 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, ERROR_TYPE_OK); |
| 482 FireReadyEvents(); | 530 FireReadyEvents(); |
| 483 base::ThreadTaskRunnerHandle::Get()->PostTask( | 531 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 484 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, new_registration)); | 532 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, new_registration)); |
| 485 } | 533 } |
| 486 | 534 |
| 487 void BackgroundSyncManager::RemoveRegistrationFromMap( | 535 void BackgroundSyncManager::RemoveRegistrationFromMap( |
| 488 int64 sw_registration_id, | 536 int64 sw_registration_id, |
| 489 const RegistrationKey& registration_key) { | 537 const RegistrationKey& registration_key) { |
| 490 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 538 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 491 DCHECK(LookupRegistration(sw_registration_id, registration_key)); | 539 DCHECK(LookupRegistration(sw_registration_id, registration_key)); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 538 const ServiceWorkerVersion::StatusCallback& callback) { | 586 const ServiceWorkerVersion::StatusCallback& callback) { |
| 539 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 587 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 540 | 588 |
| 541 active_version->DispatchSyncEvent(callback); | 589 active_version->DispatchSyncEvent(callback); |
| 542 } | 590 } |
| 543 | 591 |
| 544 void BackgroundSyncManager::UnregisterImpl( | 592 void BackgroundSyncManager::UnregisterImpl( |
| 545 int64 sw_registration_id, | 593 int64 sw_registration_id, |
| 546 const RegistrationKey& registration_key, | 594 const RegistrationKey& registration_key, |
| 547 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 595 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| 596 SyncPeriodicity periodicity, |
| 548 const StatusCallback& callback) { | 597 const StatusCallback& callback) { |
| 549 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 598 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 550 | 599 |
| 551 if (disabled_) { | 600 if (disabled_) { |
| 601 BackgroundSyncMetrics::CountUnregister(periodicity, ERROR_TYPE_STORAGE); |
| 552 base::ThreadTaskRunnerHandle::Get()->PostTask( | 602 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 553 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); | 603 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); |
| 554 return; | 604 return; |
| 555 } | 605 } |
| 556 | 606 |
| 557 const BackgroundSyncRegistration* existing_registration = | 607 const BackgroundSyncRegistration* existing_registration = |
| 558 LookupRegistration(sw_registration_id, registration_key); | 608 LookupRegistration(sw_registration_id, registration_key); |
| 559 if (!existing_registration || | 609 if (!existing_registration || |
| 560 existing_registration->id() != sync_registration_id) { | 610 existing_registration->id() != sync_registration_id) { |
| 611 BackgroundSyncMetrics::CountUnregister(periodicity, ERROR_TYPE_NOT_FOUND); |
| 561 base::ThreadTaskRunnerHandle::Get()->PostTask( | 612 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 562 FROM_HERE, base::Bind(callback, ERROR_TYPE_NOT_FOUND)); | 613 FROM_HERE, base::Bind(callback, ERROR_TYPE_NOT_FOUND)); |
| 563 return; | 614 return; |
| 564 } | 615 } |
| 565 | 616 |
| 566 RemoveRegistrationFromMap(sw_registration_id, registration_key); | 617 RemoveRegistrationFromMap(sw_registration_id, registration_key); |
| 567 | 618 |
| 568 StoreRegistrations( | 619 StoreRegistrations(sw_registration_id, |
| 569 sw_registration_id, | 620 base::Bind(&BackgroundSyncManager::UnregisterDidStore, |
| 570 base::Bind(&BackgroundSyncManager::UnregisterDidStore, | 621 weak_ptr_factory_.GetWeakPtr(), |
| 571 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, callback)); | 622 sw_registration_id, periodicity, callback)); |
| 572 } | 623 } |
| 573 | 624 |
| 574 void BackgroundSyncManager::UnregisterDidStore( | 625 void BackgroundSyncManager::UnregisterDidStore(int64 sw_registration_id, |
| 575 int64 sw_registration_id, | 626 SyncPeriodicity periodicity, |
| 576 const StatusCallback& callback, | 627 const StatusCallback& callback, |
| 577 ServiceWorkerStatusCode status) { | 628 ServiceWorkerStatusCode status) { |
| 578 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 629 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 579 | 630 |
| 580 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { | 631 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
| 581 // ServiceWorker was unregistered. | 632 // ServiceWorker was unregistered. |
| 633 BackgroundSyncMetrics::CountUnregister(periodicity, ERROR_TYPE_STORAGE); |
| 582 sw_to_registrations_map_.erase(sw_registration_id); | 634 sw_to_registrations_map_.erase(sw_registration_id); |
| 583 base::ThreadTaskRunnerHandle::Get()->PostTask( | 635 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 584 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); | 636 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); |
| 585 return; | 637 return; |
| 586 } | 638 } |
| 587 | 639 |
| 588 if (status != SERVICE_WORKER_OK) { | 640 if (status != SERVICE_WORKER_OK) { |
| 589 LOG(ERROR) << "BackgroundSync failed to unregister due to backend failure."; | 641 LOG(ERROR) << "BackgroundSync failed to unregister due to backend failure."; |
| 642 BackgroundSyncMetrics::CountUnregister(periodicity, ERROR_TYPE_STORAGE); |
| 590 DisableAndClearManager(base::Bind(callback, ERROR_TYPE_STORAGE)); | 643 DisableAndClearManager(base::Bind(callback, ERROR_TYPE_STORAGE)); |
| 591 return; | 644 return; |
| 592 } | 645 } |
| 593 | 646 |
| 647 BackgroundSyncMetrics::CountUnregister(periodicity, ERROR_TYPE_OK); |
| 594 base::ThreadTaskRunnerHandle::Get()->PostTask( | 648 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 595 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK)); | 649 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK)); |
| 596 } | 650 } |
| 597 | 651 |
| 598 void BackgroundSyncManager::GetRegistrationImpl( | 652 void BackgroundSyncManager::GetRegistrationImpl( |
| 599 int64 sw_registration_id, | 653 int64 sw_registration_id, |
| 600 const RegistrationKey& registration_key, | 654 const RegistrationKey& registration_key, |
| 601 const StatusAndRegistrationCallback& callback) { | 655 const StatusAndRegistrationCallback& callback) { |
| 602 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 656 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 603 | 657 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 645 tag_and_registration.second; | 699 tag_and_registration.second; |
| 646 if (registration.options()->periodicity == periodicity) | 700 if (registration.options()->periodicity == periodicity) |
| 647 out_registrations.push_back(registration); | 701 out_registrations.push_back(registration); |
| 648 } | 702 } |
| 649 } | 703 } |
| 650 | 704 |
| 651 base::ThreadTaskRunnerHandle::Get()->PostTask( | 705 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 652 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, out_registrations)); | 706 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, out_registrations)); |
| 653 } | 707 } |
| 654 | 708 |
| 709 bool BackgroundSyncManager::AreOptionConditionsMet( |
| 710 const BackgroundSyncRegistrationOptions& options) { |
| 711 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 712 return network_observer_->NetworkSufficient(options.network_state) && |
| 713 power_observer_->PowerSufficient(options.power_state); |
| 714 } |
| 715 |
| 655 bool BackgroundSyncManager::IsRegistrationReadyToFire( | 716 bool BackgroundSyncManager::IsRegistrationReadyToFire( |
| 656 const BackgroundSyncRegistration& registration) { | 717 const BackgroundSyncRegistration& registration) { |
| 657 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 718 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 658 | 719 |
| 659 // TODO(jkarlin): Add support for firing periodic registrations. | 720 // TODO(jkarlin): Add support for firing periodic registrations. |
| 660 if (registration.options()->periodicity == SYNC_PERIODIC) | 721 if (registration.options()->periodicity == SYNC_PERIODIC) |
| 661 return false; | 722 return false; |
| 662 | 723 |
| 663 if (registration.sync_state() != SYNC_STATE_PENDING) | 724 if (registration.sync_state() != SYNC_STATE_PENDING) |
| 664 return false; | 725 return false; |
| 665 | 726 |
| 666 DCHECK_EQ(SYNC_ONE_SHOT, registration.options()->periodicity); | 727 DCHECK_EQ(SYNC_ONE_SHOT, registration.options()->periodicity); |
| 667 | 728 |
| 668 return network_observer_->NetworkSufficient( | 729 return AreOptionConditionsMet(*registration.options()); |
| 669 registration.options()->network_state) && | |
| 670 power_observer_->PowerSufficient(registration.options()->power_state); | |
| 671 } | 730 } |
| 672 | 731 |
| 673 void BackgroundSyncManager::SchedulePendingRegistrations() { | 732 void BackgroundSyncManager::SchedulePendingRegistrations() { |
| 674 #if defined(OS_ANDROID) | 733 #if defined(OS_ANDROID) |
| 675 bool keep_browser_alive_for_one_shot = false; | 734 bool keep_browser_alive_for_one_shot = false; |
| 676 | 735 |
| 677 for (const auto& sw_id_and_registrations : sw_to_registrations_map_) { | 736 for (const auto& sw_id_and_registrations : sw_to_registrations_map_) { |
| 678 for (const auto& key_and_registration : | 737 for (const auto& key_and_registration : |
| 679 sw_id_and_registrations.second.registration_map) { | 738 sw_id_and_registrations.second.registration_map) { |
| 680 const BackgroundSyncRegistration& registration = | 739 const BackgroundSyncRegistration& registration = |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 733 sw_id_and_keys_to_fire.push_back( | 792 sw_id_and_keys_to_fire.push_back( |
| 734 std::make_pair(service_worker_id, key_and_registration.first)); | 793 std::make_pair(service_worker_id, key_and_registration.first)); |
| 735 // The state change is not saved to persistent storage because | 794 // The state change is not saved to persistent storage because |
| 736 // if the sync event is killed mid-sync then it should return to | 795 // if the sync event is killed mid-sync then it should return to |
| 737 // SYNC_STATE_PENDING. | 796 // SYNC_STATE_PENDING. |
| 738 registration->set_sync_state(SYNC_STATE_FIRING); | 797 registration->set_sync_state(SYNC_STATE_FIRING); |
| 739 } | 798 } |
| 740 } | 799 } |
| 741 } | 800 } |
| 742 | 801 |
| 802 base::TimeTicks start_time = base::TimeTicks::Now(); |
| 803 |
| 743 // Fire the sync event of the ready registrations and run |callback| once | 804 // Fire the sync event of the ready registrations and run |callback| once |
| 744 // they're all done. | 805 // they're all done. |
| 745 base::Closure barrier_closure = | 806 base::Closure events_fired_barrier_closure = |
| 746 base::BarrierClosure(sw_id_and_keys_to_fire.size(), base::Bind(callback)); | 807 base::BarrierClosure(sw_id_and_keys_to_fire.size(), base::Bind(callback)); |
| 747 | 808 |
| 809 // Record the total time taken after all events have run to completion. |
| 810 base::Closure events_completed_barrier_closure = |
| 811 base::BarrierClosure(sw_id_and_keys_to_fire.size(), |
| 812 base::Bind(&OnAllSyncEventsCompleted, start_time)); |
| 813 |
| 748 for (const auto& sw_id_and_key : sw_id_and_keys_to_fire) { | 814 for (const auto& sw_id_and_key : sw_id_and_keys_to_fire) { |
| 749 int64 service_worker_id = sw_id_and_key.first; | 815 int64 service_worker_id = sw_id_and_key.first; |
| 750 const BackgroundSyncRegistration* registration = | 816 const BackgroundSyncRegistration* registration = |
| 751 LookupRegistration(service_worker_id, sw_id_and_key.second); | 817 LookupRegistration(service_worker_id, sw_id_and_key.second); |
| 752 | 818 |
| 753 service_worker_context_->FindRegistrationForId( | 819 service_worker_context_->FindRegistrationForId( |
| 754 service_worker_id, sw_to_registrations_map_[service_worker_id].origin, | 820 service_worker_id, sw_to_registrations_map_[service_worker_id].origin, |
| 755 base::Bind(&BackgroundSyncManager::FireReadyEventsDidFindRegistration, | 821 base::Bind(&BackgroundSyncManager::FireReadyEventsDidFindRegistration, |
| 756 weak_ptr_factory_.GetWeakPtr(), sw_id_and_key.second, | 822 weak_ptr_factory_.GetWeakPtr(), sw_id_and_key.second, |
| 757 registration->id(), barrier_closure)); | 823 registration->id(), events_fired_barrier_closure, |
| 824 events_completed_barrier_closure)); |
| 758 } | 825 } |
| 759 | 826 |
| 760 SchedulePendingRegistrations(); | 827 SchedulePendingRegistrations(); |
| 761 } | 828 } |
| 762 | 829 |
| 763 void BackgroundSyncManager::FireReadyEventsDidFindRegistration( | 830 void BackgroundSyncManager::FireReadyEventsDidFindRegistration( |
| 764 const RegistrationKey& registration_key, | 831 const RegistrationKey& registration_key, |
| 765 BackgroundSyncRegistration::RegistrationId registration_id, | 832 BackgroundSyncRegistration::RegistrationId registration_id, |
| 766 const base::Closure& callback, | 833 const base::Closure& event_fired_callback, |
| 834 const base::Closure& event_completed_callback, |
| 767 ServiceWorkerStatusCode service_worker_status, | 835 ServiceWorkerStatusCode service_worker_status, |
| 768 const scoped_refptr<ServiceWorkerRegistration>& | 836 const scoped_refptr<ServiceWorkerRegistration>& |
| 769 service_worker_registration) { | 837 service_worker_registration) { |
| 770 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 838 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 771 | |
| 772 if (service_worker_status != SERVICE_WORKER_OK) { | 839 if (service_worker_status != SERVICE_WORKER_OK) { |
| 773 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 840 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 774 base::Bind(callback)); | 841 FROM_HERE, base::Bind(event_fired_callback)); |
| 842 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 843 FROM_HERE, base::Bind(event_completed_callback)); |
| 775 return; | 844 return; |
| 776 } | 845 } |
| 777 | 846 |
| 778 FireOneShotSync( | 847 FireOneShotSync( |
| 779 service_worker_registration->active_version(), | 848 service_worker_registration->active_version(), |
| 780 base::Bind(&BackgroundSyncManager::EventComplete, | 849 base::Bind(&BackgroundSyncManager::EventComplete, |
| 781 weak_ptr_factory_.GetWeakPtr(), service_worker_registration, | 850 weak_ptr_factory_.GetWeakPtr(), service_worker_registration, |
| 782 service_worker_registration->id(), registration_key, | 851 service_worker_registration->id(), registration_key, |
| 783 registration_id)); | 852 registration_id, event_completed_callback)); |
| 784 | 853 |
| 785 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 854 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 786 base::Bind(callback)); | 855 FROM_HERE, base::Bind(event_fired_callback)); |
| 787 } | 856 } |
| 788 | 857 |
| 789 // |service_worker_registration| is just to keep the registration alive | 858 // |service_worker_registration| is just to keep the registration alive |
| 790 // while the event is firing. | 859 // while the event is firing. |
| 791 void BackgroundSyncManager::EventComplete( | 860 void BackgroundSyncManager::EventComplete( |
| 792 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, | 861 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, |
| 793 int64 service_worker_id, | 862 int64 service_worker_id, |
| 794 const RegistrationKey& key, | 863 const RegistrationKey& key, |
| 795 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 864 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| 865 const base::Closure& callback, |
| 796 ServiceWorkerStatusCode status_code) { | 866 ServiceWorkerStatusCode status_code) { |
| 797 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 867 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 798 | |
| 799 if (disabled_) | 868 if (disabled_) |
| 800 return; | 869 return; |
| 801 | 870 |
| 802 op_scheduler_.ScheduleOperation( | 871 op_scheduler_.ScheduleOperation(base::Bind( |
| 803 base::Bind(&BackgroundSyncManager::EventCompleteImpl, | 872 &BackgroundSyncManager::EventCompleteImpl, weak_ptr_factory_.GetWeakPtr(), |
| 804 weak_ptr_factory_.GetWeakPtr(), service_worker_id, key, | 873 service_worker_id, key, sync_registration_id, status_code, |
| 805 sync_registration_id, status_code, MakeEmptyCompletion())); | 874 MakeClosureCompletion(callback))); |
| 806 } | 875 } |
| 807 | 876 |
| 808 void BackgroundSyncManager::EventCompleteImpl( | 877 void BackgroundSyncManager::EventCompleteImpl( |
| 809 int64 service_worker_id, | 878 int64 service_worker_id, |
| 810 const RegistrationKey& key, | 879 const RegistrationKey& key, |
| 811 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 880 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| 812 ServiceWorkerStatusCode status_code, | 881 ServiceWorkerStatusCode status_code, |
| 813 const base::Closure& callback) { | 882 const base::Closure& callback) { |
| 814 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 883 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 815 | 884 |
| 816 if (disabled_) { | 885 if (disabled_) { |
| 817 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 886 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 818 base::Bind(callback)); | 887 base::Bind(callback)); |
| 819 return; | 888 return; |
| 820 } | 889 } |
| 821 | 890 |
| 822 BackgroundSyncRegistration* registration = | 891 BackgroundSyncRegistration* registration = |
| 823 LookupRegistration(service_worker_id, key); | 892 LookupRegistration(service_worker_id, key); |
| 824 if (!registration || registration->id() != sync_registration_id) { | 893 if (!registration || registration->id() != sync_registration_id) { |
| 825 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 894 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 826 base::Bind(callback)); | 895 base::Bind(callback)); |
| 827 return; | 896 return; |
| 828 } | 897 } |
| 829 | 898 |
| 899 // The event ran to completion, we should count it, no matter what happens |
| 900 // from here. |
| 901 BackgroundSyncMetrics::RecordEventResult(registration->options()->periodicity, |
| 902 status_code == SERVICE_WORKER_OK); |
| 903 |
| 830 if (registration->options()->periodicity == SYNC_ONE_SHOT) { | 904 if (registration->options()->periodicity == SYNC_ONE_SHOT) { |
| 831 if (status_code != SERVICE_WORKER_OK) { | 905 if (status_code != SERVICE_WORKER_OK) { |
| 832 // TODO(jkarlin) Fire the sync event on the next page load controlled by | 906 // TODO(jkarlin) Fire the sync event on the next page load controlled by |
| 833 // this registration. (crbug.com/479665) | 907 // this registration. (crbug.com/479665) |
| 834 registration->set_sync_state(SYNC_STATE_FAILED); | 908 registration->set_sync_state(SYNC_STATE_FAILED); |
| 835 } else { | 909 } else { |
| 836 registration = nullptr; | 910 registration = nullptr; |
| 837 RemoveRegistrationFromMap(service_worker_id, key); | 911 RemoveRegistrationFromMap(service_worker_id, key); |
| 838 } | 912 } |
| 839 } else { | 913 } else { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 865 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " | 939 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " |
| 866 "failure."; | 940 "failure."; |
| 867 DisableAndClearManager(base::Bind(callback)); | 941 DisableAndClearManager(base::Bind(callback)); |
| 868 return; | 942 return; |
| 869 } | 943 } |
| 870 | 944 |
| 871 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 945 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 872 base::Bind(callback)); | 946 base::Bind(callback)); |
| 873 } | 947 } |
| 874 | 948 |
| 949 // static |
| 950 void BackgroundSyncManager::OnAllSyncEventsCompleted( |
| 951 const base::TimeTicks& start_time) { |
| 952 // Record the combined time taken by all sync events. |
| 953 BackgroundSyncMetrics::RecordBatchSyncEventHandlingTime( |
| 954 base::TimeTicks::Now() - start_time); |
| 955 } |
| 956 |
| 875 void BackgroundSyncManager::OnRegistrationDeletedImpl( | 957 void BackgroundSyncManager::OnRegistrationDeletedImpl( |
| 876 int64 registration_id, | 958 int64 registration_id, |
| 877 const base::Closure& callback) { | 959 const base::Closure& callback) { |
| 878 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 960 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 879 | 961 |
| 880 // The backend (ServiceWorkerStorage) will delete the data, so just delete the | 962 // The backend (ServiceWorkerStorage) will delete the data, so just delete the |
| 881 // memory representation here. | 963 // memory representation here. |
| 882 sw_to_registrations_map_.erase(registration_id); | 964 sw_to_registrations_map_.erase(registration_id); |
| 883 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 965 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 884 base::Bind(callback)); | 966 base::Bind(callback)); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 909 Params... parameters) { | 991 Params... parameters) { |
| 910 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 992 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 911 | 993 |
| 912 callback.Run(parameters...); | 994 callback.Run(parameters...); |
| 913 op_scheduler_.CompleteOperationAndRunNext(); | 995 op_scheduler_.CompleteOperationAndRunNext(); |
| 914 } | 996 } |
| 915 | 997 |
| 916 base::Closure BackgroundSyncManager::MakeEmptyCompletion() { | 998 base::Closure BackgroundSyncManager::MakeEmptyCompletion() { |
| 917 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 999 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 918 | 1000 |
| 1001 return MakeClosureCompletion(base::Bind(base::DoNothing)); |
| 1002 } |
| 1003 |
| 1004 base::Closure BackgroundSyncManager::MakeClosureCompletion( |
| 1005 const base::Closure& callback) { |
| 1006 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1007 |
| 919 return base::Bind( | 1008 return base::Bind( |
| 920 &BackgroundSyncManager::CompleteOperationCallback<base::Closure>, | 1009 &BackgroundSyncManager::CompleteOperationCallback<base::Closure>, |
| 921 weak_ptr_factory_.GetWeakPtr(), base::Bind(base::DoNothing)); | 1010 weak_ptr_factory_.GetWeakPtr(), callback); |
| 922 } | 1011 } |
| 923 | 1012 |
| 924 BackgroundSyncManager::StatusAndRegistrationCallback | 1013 BackgroundSyncManager::StatusAndRegistrationCallback |
| 925 BackgroundSyncManager::MakeStatusAndRegistrationCompletion( | 1014 BackgroundSyncManager::MakeStatusAndRegistrationCompletion( |
| 926 const StatusAndRegistrationCallback& callback) { | 1015 const StatusAndRegistrationCallback& callback) { |
| 927 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1016 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 928 | 1017 |
| 929 return base::Bind(&BackgroundSyncManager::CompleteOperationCallback< | 1018 return base::Bind(&BackgroundSyncManager::CompleteOperationCallback< |
| 930 StatusAndRegistrationCallback, ErrorType, | 1019 StatusAndRegistrationCallback, ErrorType, |
| 931 const BackgroundSyncRegistration&>, | 1020 const BackgroundSyncRegistration&>, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 947 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { | 1036 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { |
| 948 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1037 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 949 | 1038 |
| 950 return base::Bind( | 1039 return base::Bind( |
| 951 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, | 1040 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, |
| 952 ErrorType>, | 1041 ErrorType>, |
| 953 weak_ptr_factory_.GetWeakPtr(), callback); | 1042 weak_ptr_factory_.GetWeakPtr(), callback); |
| 954 } | 1043 } |
| 955 | 1044 |
| 956 } // namespace content | 1045 } // namespace content |
| OLD | NEW |