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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 |
78 if (disabled_) { | 79 if (disabled_) { |
80 BackgroundSyncMetrics::CountRegistration( | |
81 options.periodicity, false, | |
82 BackgroundSyncMetrics::REGISTRATION_RESULT_FAILED); | |
79 base::ThreadTaskRunnerHandle::Get()->PostTask( | 83 base::ThreadTaskRunnerHandle::Get()->PostTask( |
80 FROM_HERE, | 84 FROM_HERE, |
81 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | 85 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
82 return; | 86 return; |
83 } | 87 } |
84 | 88 |
85 op_scheduler_.ScheduleOperation( | 89 op_scheduler_.ScheduleOperation( |
86 base::Bind(&BackgroundSyncManager::RegisterImpl, | 90 base::Bind(&BackgroundSyncManager::RegisterImpl, |
87 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options, | 91 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options, |
88 MakeStatusAndRegistrationCompletion(callback))); | 92 MakeStatusAndRegistrationCompletion(callback))); |
89 } | 93 } |
90 | 94 |
91 void BackgroundSyncManager::Unregister( | 95 void BackgroundSyncManager::Unregister( |
92 int64 sw_registration_id, | 96 int64 sw_registration_id, |
93 const std::string& sync_registration_tag, | 97 const std::string& sync_registration_tag, |
94 SyncPeriodicity periodicity, | 98 SyncPeriodicity periodicity, |
95 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 99 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
96 const StatusCallback& callback) { | 100 const StatusCallback& callback) { |
97 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 101 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
98 | 102 |
99 if (disabled_) { | 103 if (disabled_) { |
104 BackgroundSyncMetrics::CountUnregistration(periodicity, false); | |
100 base::ThreadTaskRunnerHandle::Get()->PostTask( | 105 base::ThreadTaskRunnerHandle::Get()->PostTask( |
101 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); | 106 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); |
102 return; | 107 return; |
103 } | 108 } |
104 | 109 |
105 RegistrationKey registration_key(sync_registration_tag, periodicity); | 110 RegistrationKey registration_key(sync_registration_tag, periodicity); |
106 | 111 |
107 op_scheduler_.ScheduleOperation(base::Bind( | 112 op_scheduler_.ScheduleOperation(base::Bind( |
108 &BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(), | 113 &BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(), |
109 sw_registration_id, registration_key, sync_registration_id, | 114 sw_registration_id, registration_key, sync_registration_id, periodicity, |
110 MakeStatusCompletion(callback))); | 115 MakeStatusCompletion(callback))); |
111 } | 116 } |
112 | 117 |
113 void BackgroundSyncManager::GetRegistration( | 118 void BackgroundSyncManager::GetRegistration( |
114 int64 sw_registration_id, | 119 int64 sw_registration_id, |
115 const std::string& sync_registration_tag, | 120 const std::string& sync_registration_tag, |
116 SyncPeriodicity periodicity, | 121 SyncPeriodicity periodicity, |
117 const StatusAndRegistrationCallback& callback) { | 122 const StatusAndRegistrationCallback& callback) { |
118 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 123 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
119 | 124 |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
285 base::Bind(callback)); | 290 base::Bind(callback)); |
286 } | 291 } |
287 | 292 |
288 void BackgroundSyncManager::RegisterImpl( | 293 void BackgroundSyncManager::RegisterImpl( |
289 int64 sw_registration_id, | 294 int64 sw_registration_id, |
290 const BackgroundSyncRegistrationOptions& options, | 295 const BackgroundSyncRegistrationOptions& options, |
291 const StatusAndRegistrationCallback& callback) { | 296 const StatusAndRegistrationCallback& callback) { |
292 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 297 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
293 | 298 |
294 if (disabled_) { | 299 if (disabled_) { |
300 BackgroundSyncMetrics::CountRegistration( | |
301 options.periodicity, false, | |
302 BackgroundSyncMetrics::REGISTRATION_RESULT_FAILED); | |
295 base::ThreadTaskRunnerHandle::Get()->PostTask( | 303 base::ThreadTaskRunnerHandle::Get()->PostTask( |
296 FROM_HERE, | 304 FROM_HERE, |
297 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | 305 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
298 return; | 306 return; |
299 } | 307 } |
300 | 308 |
309 // For UMA, determine here whether the sync could fire immediately | |
310 bool registrationCouldFire = WouldFireRegistrationWithOptions(options); | |
311 | |
301 const BackgroundSyncRegistration* existing_registration = | 312 const BackgroundSyncRegistration* existing_registration = |
302 LookupRegistration(sw_registration_id, RegistrationKey(options)); | 313 LookupRegistration(sw_registration_id, RegistrationKey(options)); |
303 if (existing_registration && | 314 if (existing_registration && |
304 existing_registration->options()->Equals(options)) { | 315 existing_registration->options()->Equals(options)) { |
316 // Record the duplicated registration | |
317 BackgroundSyncMetrics::CountRegistration( | |
318 existing_registration->options()->periodicity, registrationCouldFire, | |
319 BackgroundSyncMetrics::REGISTRATION_RESULT_DUPLICATE); | |
320 | |
305 base::ThreadTaskRunnerHandle::Get()->PostTask( | 321 base::ThreadTaskRunnerHandle::Get()->PostTask( |
306 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, *existing_registration)); | 322 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, *existing_registration)); |
307 return; | 323 return; |
308 } | 324 } |
309 | 325 |
310 BackgroundSyncRegistration new_registration; | 326 BackgroundSyncRegistration new_registration; |
311 *new_registration.options() = options; | 327 *new_registration.options() = options; |
312 | 328 |
313 BackgroundSyncRegistrations* registrations = | 329 BackgroundSyncRegistrations* registrations = |
314 &sw_to_registrations_map_[sw_registration_id]; | 330 &sw_to_registrations_map_[sw_registration_id]; |
315 new_registration.set_id(registrations->next_id++); | 331 new_registration.set_id(registrations->next_id++); |
316 | 332 |
317 ServiceWorkerRegistration* sw_registration = | 333 ServiceWorkerRegistration* sw_registration = |
318 service_worker_context_->GetLiveRegistration(sw_registration_id); | 334 service_worker_context_->GetLiveRegistration(sw_registration_id); |
319 if (!sw_registration || !sw_registration->active_version()) { | 335 if (!sw_registration || !sw_registration->active_version()) { |
336 BackgroundSyncMetrics::CountRegistration( | |
337 options.periodicity, registrationCouldFire, | |
338 BackgroundSyncMetrics::REGISTRATION_RESULT_FAILED); | |
320 base::ThreadTaskRunnerHandle::Get()->PostTask( | 339 base::ThreadTaskRunnerHandle::Get()->PostTask( |
321 FROM_HERE, base::Bind(callback, ERROR_TYPE_NO_SERVICE_WORKER, | 340 FROM_HERE, base::Bind(callback, ERROR_TYPE_NO_SERVICE_WORKER, |
322 BackgroundSyncRegistration())); | 341 BackgroundSyncRegistration())); |
323 return; | 342 return; |
324 } | 343 } |
325 | 344 |
326 AddRegistrationToMap(sw_registration_id, | 345 AddRegistrationToMap(sw_registration_id, |
327 sw_registration->pattern().GetOrigin(), | 346 sw_registration->pattern().GetOrigin(), |
328 new_registration); | 347 new_registration); |
329 | 348 |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
445 kBackgroundSyncUserDataKey, serialized, callback); | 464 kBackgroundSyncUserDataKey, serialized, callback); |
446 } | 465 } |
447 | 466 |
448 void BackgroundSyncManager::RegisterDidStore( | 467 void BackgroundSyncManager::RegisterDidStore( |
449 int64 sw_registration_id, | 468 int64 sw_registration_id, |
450 const BackgroundSyncRegistration& new_registration, | 469 const BackgroundSyncRegistration& new_registration, |
451 const StatusAndRegistrationCallback& callback, | 470 const StatusAndRegistrationCallback& callback, |
452 ServiceWorkerStatusCode status) { | 471 ServiceWorkerStatusCode status) { |
453 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 472 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
454 | 473 |
474 // For UMA, determine here whether the sync could fire immediately | |
475 bool registrationCouldFire = | |
476 WouldFireRegistrationWithOptions(*new_registration.options()); | |
477 | |
455 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { | 478 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
456 // The registration is gone. | 479 // The service worker registration is gone. |
480 BackgroundSyncMetrics::CountRegistration( | |
481 new_registration.options()->periodicity, registrationCouldFire, | |
482 BackgroundSyncMetrics::REGISTRATION_RESULT_FAILED); | |
457 sw_to_registrations_map_.erase(sw_registration_id); | 483 sw_to_registrations_map_.erase(sw_registration_id); |
458 base::ThreadTaskRunnerHandle::Get()->PostTask( | 484 base::ThreadTaskRunnerHandle::Get()->PostTask( |
459 FROM_HERE, | 485 FROM_HERE, |
460 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | 486 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
461 return; | 487 return; |
462 } | 488 } |
463 | 489 |
464 if (status != SERVICE_WORKER_OK) { | 490 if (status != SERVICE_WORKER_OK) { |
465 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " | 491 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " |
466 "failure."; | 492 "failure."; |
493 BackgroundSyncMetrics::CountRegistration( | |
494 new_registration.options()->periodicity, registrationCouldFire, | |
495 BackgroundSyncMetrics::REGISTRATION_RESULT_FAILED); | |
467 DisableAndClearManager( | 496 DisableAndClearManager( |
468 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | 497 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
469 return; | 498 return; |
470 } | 499 } |
471 | 500 |
501 BackgroundSyncMetrics::CountRegistration( | |
502 new_registration.options()->periodicity, registrationCouldFire, | |
503 BackgroundSyncMetrics::REGISTRATION_RESULT_SUCCEEDED); | |
472 FireReadyEvents(); | 504 FireReadyEvents(); |
473 base::ThreadTaskRunnerHandle::Get()->PostTask( | 505 base::ThreadTaskRunnerHandle::Get()->PostTask( |
474 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, new_registration)); | 506 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, new_registration)); |
475 } | 507 } |
476 | 508 |
477 void BackgroundSyncManager::RemoveRegistrationFromMap( | 509 void BackgroundSyncManager::RemoveRegistrationFromMap( |
478 int64 sw_registration_id, | 510 int64 sw_registration_id, |
479 const RegistrationKey& registration_key) { | 511 const RegistrationKey& registration_key) { |
480 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 512 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
481 DCHECK(LookupRegistration(sw_registration_id, registration_key)); | 513 DCHECK(LookupRegistration(sw_registration_id, registration_key)); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
528 const ServiceWorkerVersion::StatusCallback& callback) { | 560 const ServiceWorkerVersion::StatusCallback& callback) { |
529 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 561 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
530 | 562 |
531 active_version->DispatchSyncEvent(callback); | 563 active_version->DispatchSyncEvent(callback); |
532 } | 564 } |
533 | 565 |
534 void BackgroundSyncManager::UnregisterImpl( | 566 void BackgroundSyncManager::UnregisterImpl( |
535 int64 sw_registration_id, | 567 int64 sw_registration_id, |
536 const RegistrationKey& registration_key, | 568 const RegistrationKey& registration_key, |
537 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 569 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
570 SyncPeriodicity periodicity, | |
538 const StatusCallback& callback) { | 571 const StatusCallback& callback) { |
539 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 572 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
540 | 573 |
541 if (disabled_) { | 574 if (disabled_) { |
575 BackgroundSyncMetrics::CountUnregistration(periodicity, false); | |
542 base::ThreadTaskRunnerHandle::Get()->PostTask( | 576 base::ThreadTaskRunnerHandle::Get()->PostTask( |
543 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); | 577 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); |
544 return; | 578 return; |
545 } | 579 } |
546 | 580 |
547 const BackgroundSyncRegistration* existing_registration = | 581 const BackgroundSyncRegistration* existing_registration = |
548 LookupRegistration(sw_registration_id, registration_key); | 582 LookupRegistration(sw_registration_id, registration_key); |
549 if (!existing_registration || | 583 if (!existing_registration || |
550 existing_registration->id() != sync_registration_id) { | 584 existing_registration->id() != sync_registration_id) { |
585 BackgroundSyncMetrics::CountUnregistration(periodicity, false); | |
551 base::ThreadTaskRunnerHandle::Get()->PostTask( | 586 base::ThreadTaskRunnerHandle::Get()->PostTask( |
552 FROM_HERE, base::Bind(callback, ERROR_TYPE_NOT_FOUND)); | 587 FROM_HERE, base::Bind(callback, ERROR_TYPE_NOT_FOUND)); |
553 return; | 588 return; |
554 } | 589 } |
555 | 590 |
556 RemoveRegistrationFromMap(sw_registration_id, registration_key); | 591 RemoveRegistrationFromMap(sw_registration_id, registration_key); |
557 | 592 |
558 StoreRegistrations( | 593 StoreRegistrations(sw_registration_id, |
559 sw_registration_id, | 594 base::Bind(&BackgroundSyncManager::UnregisterDidStore, |
560 base::Bind(&BackgroundSyncManager::UnregisterDidStore, | 595 weak_ptr_factory_.GetWeakPtr(), |
561 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, callback)); | 596 sw_registration_id, periodicity, callback)); |
562 } | 597 } |
563 | 598 |
564 void BackgroundSyncManager::UnregisterDidStore( | 599 void BackgroundSyncManager::UnregisterDidStore(int64 sw_registration_id, |
565 int64 sw_registration_id, | 600 SyncPeriodicity periodicity, |
566 const StatusCallback& callback, | 601 const StatusCallback& callback, |
567 ServiceWorkerStatusCode status) { | 602 ServiceWorkerStatusCode status) { |
568 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 603 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
569 | 604 |
570 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { | 605 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
571 // ServiceWorker was unregistered. | 606 // ServiceWorker was unregistered. |
607 BackgroundSyncMetrics::CountUnregistration(periodicity, false); | |
572 sw_to_registrations_map_.erase(sw_registration_id); | 608 sw_to_registrations_map_.erase(sw_registration_id); |
573 base::ThreadTaskRunnerHandle::Get()->PostTask( | 609 base::ThreadTaskRunnerHandle::Get()->PostTask( |
574 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); | 610 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); |
575 return; | 611 return; |
576 } | 612 } |
577 | 613 |
578 if (status != SERVICE_WORKER_OK) { | 614 if (status != SERVICE_WORKER_OK) { |
579 LOG(ERROR) << "BackgroundSync failed to unregister due to backend failure."; | 615 LOG(ERROR) << "BackgroundSync failed to unregister due to backend failure."; |
616 BackgroundSyncMetrics::CountUnregistration(periodicity, false); | |
580 DisableAndClearManager(base::Bind(callback, ERROR_TYPE_STORAGE)); | 617 DisableAndClearManager(base::Bind(callback, ERROR_TYPE_STORAGE)); |
581 return; | 618 return; |
582 } | 619 } |
583 | 620 |
621 BackgroundSyncMetrics::CountUnregistration(periodicity, true); | |
584 base::ThreadTaskRunnerHandle::Get()->PostTask( | 622 base::ThreadTaskRunnerHandle::Get()->PostTask( |
585 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK)); | 623 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK)); |
586 } | 624 } |
587 | 625 |
588 void BackgroundSyncManager::GetRegistrationImpl( | 626 void BackgroundSyncManager::GetRegistrationImpl( |
589 int64 sw_registration_id, | 627 int64 sw_registration_id, |
590 const RegistrationKey& registration_key, | 628 const RegistrationKey& registration_key, |
591 const StatusAndRegistrationCallback& callback) { | 629 const StatusAndRegistrationCallback& callback) { |
592 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 630 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
593 | 631 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
635 tag_and_registration.second; | 673 tag_and_registration.second; |
636 if (registration.options()->periodicity == periodicity) | 674 if (registration.options()->periodicity == periodicity) |
637 out_registrations.push_back(registration); | 675 out_registrations.push_back(registration); |
638 } | 676 } |
639 } | 677 } |
640 | 678 |
641 base::ThreadTaskRunnerHandle::Get()->PostTask( | 679 base::ThreadTaskRunnerHandle::Get()->PostTask( |
642 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, out_registrations)); | 680 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, out_registrations)); |
643 } | 681 } |
644 | 682 |
683 bool BackgroundSyncManager::WouldFireRegistrationWithOptions( | |
684 const BackgroundSyncRegistrationOptions& options) { | |
685 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
686 return network_observer_->NetworkSufficient(options.network_state) && | |
687 power_observer_->PowerSufficient(options.power_state); | |
688 } | |
689 | |
645 bool BackgroundSyncManager::IsRegistrationReadyToFire( | 690 bool BackgroundSyncManager::IsRegistrationReadyToFire( |
646 const BackgroundSyncRegistration& registration) { | 691 const BackgroundSyncRegistration& registration) { |
647 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 692 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
648 | 693 |
649 // TODO(jkarlin): Add support for firing periodic registrations. | 694 // TODO(jkarlin): Add support for firing periodic registrations. |
650 if (registration.options()->periodicity == SYNC_PERIODIC) | 695 if (registration.options()->periodicity == SYNC_PERIODIC) |
651 return false; | 696 return false; |
652 | 697 |
653 if (registration.sync_state() != SYNC_STATE_PENDING) | 698 if (registration.sync_state() != SYNC_STATE_PENDING) |
654 return false; | 699 return false; |
655 | 700 |
656 DCHECK_EQ(SYNC_ONE_SHOT, registration.options()->periodicity); | 701 DCHECK_EQ(SYNC_ONE_SHOT, registration.options()->periodicity); |
657 | 702 |
658 return network_observer_->NetworkSufficient( | 703 return WouldFireRegistrationWithOptions(*registration.options()); |
659 registration.options()->network_state) && | |
660 power_observer_->PowerSufficient(registration.options()->power_state); | |
661 } | 704 } |
662 | 705 |
663 void BackgroundSyncManager::SchedulePendingRegistrations() { | 706 void BackgroundSyncManager::SchedulePendingRegistrations() { |
664 #if defined(OS_ANDROID) | 707 #if defined(OS_ANDROID) |
665 bool keep_browser_alive_for_one_shot = false; | 708 bool keep_browser_alive_for_one_shot = false; |
666 | 709 |
667 for (const auto& sw_id_and_registrations : sw_to_registrations_map_) { | 710 for (const auto& sw_id_and_registrations : sw_to_registrations_map_) { |
668 for (const auto& key_and_registration : | 711 for (const auto& key_and_registration : |
669 sw_id_and_registrations.second.registration_map) { | 712 sw_id_and_registrations.second.registration_map) { |
670 const BackgroundSyncRegistration& registration = | 713 const BackgroundSyncRegistration& registration = |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
723 sw_id_and_keys_to_fire.push_back( | 766 sw_id_and_keys_to_fire.push_back( |
724 std::make_pair(service_worker_id, key_and_registration.first)); | 767 std::make_pair(service_worker_id, key_and_registration.first)); |
725 // The state change is not saved to persistent storage because | 768 // The state change is not saved to persistent storage because |
726 // if the sync event is killed mid-sync then it should return to | 769 // if the sync event is killed mid-sync then it should return to |
727 // SYNC_STATE_PENDING. | 770 // SYNC_STATE_PENDING. |
728 registration->set_sync_state(SYNC_STATE_FIRING); | 771 registration->set_sync_state(SYNC_STATE_FIRING); |
729 } | 772 } |
730 } | 773 } |
731 } | 774 } |
732 | 775 |
776 base::TimeTicks startTime = base::TimeTicks::Now(); | |
777 | |
733 // Fire the sync event of the ready registrations and run |callback| once | 778 // Fire the sync event of the ready registrations and run |callback| once |
734 // they're all done. | 779 // they're all done. |
735 base::Closure barrier_closure = | 780 base::Closure barrier_closure = base::BarrierClosure( |
736 base::BarrierClosure(sw_id_and_keys_to_fire.size(), base::Bind(callback)); | 781 sw_id_and_keys_to_fire.size(), |
782 base::Bind(&OnAllSyncEventsCompleted, startTime, callback)); | |
737 | 783 |
738 for (const auto& sw_id_and_key : sw_id_and_keys_to_fire) { | 784 for (const auto& sw_id_and_key : sw_id_and_keys_to_fire) { |
739 int64 service_worker_id = sw_id_and_key.first; | 785 int64 service_worker_id = sw_id_and_key.first; |
740 const BackgroundSyncRegistration* registration = | 786 const BackgroundSyncRegistration* registration = |
741 LookupRegistration(service_worker_id, sw_id_and_key.second); | 787 LookupRegistration(service_worker_id, sw_id_and_key.second); |
742 | 788 |
743 service_worker_context_->FindRegistrationForId( | 789 service_worker_context_->FindRegistrationForId( |
744 service_worker_id, sw_to_registrations_map_[service_worker_id].origin, | 790 service_worker_id, sw_to_registrations_map_[service_worker_id].origin, |
745 base::Bind(&BackgroundSyncManager::FireReadyEventsDidFindRegistration, | 791 base::Bind(&BackgroundSyncManager::FireReadyEventsDidFindRegistration, |
746 weak_ptr_factory_.GetWeakPtr(), sw_id_and_key.second, | 792 weak_ptr_factory_.GetWeakPtr(), sw_id_and_key.second, |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
810 } | 856 } |
811 | 857 |
812 BackgroundSyncRegistration* registration = | 858 BackgroundSyncRegistration* registration = |
813 LookupRegistration(service_worker_id, key); | 859 LookupRegistration(service_worker_id, key); |
814 if (!registration || registration->id() != sync_registration_id) { | 860 if (!registration || registration->id() != sync_registration_id) { |
815 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 861 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
816 base::Bind(callback)); | 862 base::Bind(callback)); |
817 return; | 863 return; |
818 } | 864 } |
819 | 865 |
866 // The event ran to completion, we should count it, no matter what happens | |
867 // from here. | |
868 BackgroundSyncMetrics::RecordEventResult(registration->options()->periodicity, | |
869 status_code == SERVICE_WORKER_OK); | |
870 | |
820 if (registration->options()->periodicity == SYNC_ONE_SHOT) { | 871 if (registration->options()->periodicity == SYNC_ONE_SHOT) { |
821 if (status_code != SERVICE_WORKER_OK) { | 872 if (status_code != SERVICE_WORKER_OK) { |
822 // TODO(jkarlin) Fire the sync event on the next page load controlled by | 873 // TODO(jkarlin) Fire the sync event on the next page load controlled by |
823 // this registration. (crbug.com/479665) | 874 // this registration. (crbug.com/479665) |
824 registration->set_sync_state(SYNC_STATE_FAILED); | 875 registration->set_sync_state(SYNC_STATE_FAILED); |
825 } else { | 876 } else { |
826 registration = nullptr; | 877 registration = nullptr; |
827 RemoveRegistrationFromMap(service_worker_id, key); | 878 RemoveRegistrationFromMap(service_worker_id, key); |
828 } | 879 } |
829 } else { | 880 } else { |
(...skipping 25 matching lines...) Expand all Loading... | |
855 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " | 906 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " |
856 "failure."; | 907 "failure."; |
857 DisableAndClearManager(base::Bind(callback)); | 908 DisableAndClearManager(base::Bind(callback)); |
858 return; | 909 return; |
859 } | 910 } |
860 | 911 |
861 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 912 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
862 base::Bind(callback)); | 913 base::Bind(callback)); |
863 } | 914 } |
864 | 915 |
916 // static | |
917 void BackgroundSyncManager::OnAllSyncEventsCompleted( | |
918 const base::TimeTicks& startTime, | |
919 const base::Closure& callback) { | |
920 // Record the combined time taken by all sync events and run |callback|. | |
921 BackgroundSyncMetrics::RecordSyncEventHandlingTime(base::TimeTicks::Now() - | |
922 startTime); | |
Alexei Svitkine (slow)
2015/07/09 21:02:57
Ditto
iclelland
2015/07/10 14:57:26
Done.
| |
923 callback.Run(); | |
924 } | |
925 | |
865 void BackgroundSyncManager::OnRegistrationDeletedImpl( | 926 void BackgroundSyncManager::OnRegistrationDeletedImpl( |
866 int64 registration_id, | 927 int64 registration_id, |
867 const base::Closure& callback) { | 928 const base::Closure& callback) { |
868 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 929 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
869 | 930 |
870 // The backend (ServiceWorkerStorage) will delete the data, so just delete the | 931 // The backend (ServiceWorkerStorage) will delete the data, so just delete the |
871 // memory representation here. | 932 // memory representation here. |
872 sw_to_registrations_map_.erase(registration_id); | 933 sw_to_registrations_map_.erase(registration_id); |
873 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 934 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
874 base::Bind(callback)); | 935 base::Bind(callback)); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
937 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { | 998 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { |
938 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 999 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
939 | 1000 |
940 return base::Bind( | 1001 return base::Bind( |
941 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, | 1002 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, |
942 ErrorType>, | 1003 ErrorType>, |
943 weak_ptr_factory_.GetWeakPtr(), callback); | 1004 weak_ptr_factory_.GetWeakPtr(), callback); |
944 } | 1005 } |
945 | 1006 |
946 } // namespace content | 1007 } // namespace content |
OLD | NEW |