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::CountRegistration( |
| 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::CountUnregistration(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::CountRegistration( |
| 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 const BackgroundSyncRegistration* existing_registration = | 323 const 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)) { |
| 327 // Record the duplicated registration |
| 328 BackgroundSyncMetrics::CountRegistration( |
| 329 existing_registration->options()->periodicity, registration_could_fire, |
| 330 BackgroundSyncMetrics::REGISTRATION_IS_DUPLICATE, ERROR_TYPE_OK); |
| 331 |
305 base::ThreadTaskRunnerHandle::Get()->PostTask( | 332 base::ThreadTaskRunnerHandle::Get()->PostTask( |
306 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, *existing_registration)); | 333 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, *existing_registration)); |
307 return; | 334 return; |
308 } | 335 } |
309 | 336 |
310 BackgroundSyncRegistration new_registration; | 337 BackgroundSyncRegistration new_registration; |
311 *new_registration.options() = options; | 338 *new_registration.options() = options; |
312 | 339 |
313 BackgroundSyncRegistrations* registrations = | 340 BackgroundSyncRegistrations* registrations = |
314 &sw_to_registrations_map_[sw_registration_id]; | 341 &sw_to_registrations_map_[sw_registration_id]; |
315 new_registration.set_id(registrations->next_id++); | 342 new_registration.set_id(registrations->next_id++); |
316 | 343 |
317 ServiceWorkerRegistration* sw_registration = | 344 ServiceWorkerRegistration* sw_registration = |
318 service_worker_context_->GetLiveRegistration(sw_registration_id); | 345 service_worker_context_->GetLiveRegistration(sw_registration_id); |
319 if (!sw_registration || !sw_registration->active_version()) { | 346 if (!sw_registration || !sw_registration->active_version()) { |
| 347 BackgroundSyncMetrics::CountRegistration( |
| 348 options.periodicity, registration_could_fire, |
| 349 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, |
| 350 ERROR_TYPE_NO_SERVICE_WORKER); |
320 base::ThreadTaskRunnerHandle::Get()->PostTask( | 351 base::ThreadTaskRunnerHandle::Get()->PostTask( |
321 FROM_HERE, base::Bind(callback, ERROR_TYPE_NO_SERVICE_WORKER, | 352 FROM_HERE, base::Bind(callback, ERROR_TYPE_NO_SERVICE_WORKER, |
322 BackgroundSyncRegistration())); | 353 BackgroundSyncRegistration())); |
323 return; | 354 return; |
324 } | 355 } |
325 | 356 |
326 AddRegistrationToMap(sw_registration_id, | 357 AddRegistrationToMap(sw_registration_id, |
327 sw_registration->pattern().GetOrigin(), | 358 sw_registration->pattern().GetOrigin(), |
328 new_registration); | 359 new_registration); |
329 | 360 |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
445 kBackgroundSyncUserDataKey, serialized, callback); | 476 kBackgroundSyncUserDataKey, serialized, callback); |
446 } | 477 } |
447 | 478 |
448 void BackgroundSyncManager::RegisterDidStore( | 479 void BackgroundSyncManager::RegisterDidStore( |
449 int64 sw_registration_id, | 480 int64 sw_registration_id, |
450 const BackgroundSyncRegistration& new_registration, | 481 const BackgroundSyncRegistration& new_registration, |
451 const StatusAndRegistrationCallback& callback, | 482 const StatusAndRegistrationCallback& callback, |
452 ServiceWorkerStatusCode status) { | 483 ServiceWorkerStatusCode status) { |
453 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 484 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
454 | 485 |
| 486 // For UMA, determine here whether the sync could fire immediately |
| 487 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = |
| 488 AreOptionConditionsMet(*new_registration.options()) |
| 489 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE |
| 490 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; |
| 491 |
455 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { | 492 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
456 // The registration is gone. | 493 // The service worker registration is gone. |
| 494 BackgroundSyncMetrics::CountRegistration( |
| 495 new_registration.options()->periodicity, registration_could_fire, |
| 496 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, |
| 497 ERROR_TYPE_STORAGE); |
457 sw_to_registrations_map_.erase(sw_registration_id); | 498 sw_to_registrations_map_.erase(sw_registration_id); |
458 base::ThreadTaskRunnerHandle::Get()->PostTask( | 499 base::ThreadTaskRunnerHandle::Get()->PostTask( |
459 FROM_HERE, | 500 FROM_HERE, |
460 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | 501 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
461 return; | 502 return; |
462 } | 503 } |
463 | 504 |
464 if (status != SERVICE_WORKER_OK) { | 505 if (status != SERVICE_WORKER_OK) { |
465 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " | 506 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " |
466 "failure."; | 507 "failure."; |
| 508 BackgroundSyncMetrics::CountRegistration( |
| 509 new_registration.options()->periodicity, registration_could_fire, |
| 510 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, |
| 511 ERROR_TYPE_STORAGE); |
467 DisableAndClearManager( | 512 DisableAndClearManager( |
468 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | 513 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
469 return; | 514 return; |
470 } | 515 } |
471 | 516 |
| 517 BackgroundSyncMetrics::CountRegistration( |
| 518 new_registration.options()->periodicity, registration_could_fire, |
| 519 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, ERROR_TYPE_OK); |
472 FireReadyEvents(); | 520 FireReadyEvents(); |
473 base::ThreadTaskRunnerHandle::Get()->PostTask( | 521 base::ThreadTaskRunnerHandle::Get()->PostTask( |
474 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, new_registration)); | 522 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, new_registration)); |
475 } | 523 } |
476 | 524 |
477 void BackgroundSyncManager::RemoveRegistrationFromMap( | 525 void BackgroundSyncManager::RemoveRegistrationFromMap( |
478 int64 sw_registration_id, | 526 int64 sw_registration_id, |
479 const RegistrationKey& registration_key) { | 527 const RegistrationKey& registration_key) { |
480 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 528 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
481 DCHECK(LookupRegistration(sw_registration_id, registration_key)); | 529 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) { | 576 const ServiceWorkerVersion::StatusCallback& callback) { |
529 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 577 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
530 | 578 |
531 active_version->DispatchSyncEvent(callback); | 579 active_version->DispatchSyncEvent(callback); |
532 } | 580 } |
533 | 581 |
534 void BackgroundSyncManager::UnregisterImpl( | 582 void BackgroundSyncManager::UnregisterImpl( |
535 int64 sw_registration_id, | 583 int64 sw_registration_id, |
536 const RegistrationKey& registration_key, | 584 const RegistrationKey& registration_key, |
537 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 585 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| 586 SyncPeriodicity periodicity, |
538 const StatusCallback& callback) { | 587 const StatusCallback& callback) { |
539 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 588 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
540 | 589 |
541 if (disabled_) { | 590 if (disabled_) { |
| 591 BackgroundSyncMetrics::CountUnregistration(periodicity, ERROR_TYPE_STORAGE); |
542 base::ThreadTaskRunnerHandle::Get()->PostTask( | 592 base::ThreadTaskRunnerHandle::Get()->PostTask( |
543 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); | 593 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); |
544 return; | 594 return; |
545 } | 595 } |
546 | 596 |
547 const BackgroundSyncRegistration* existing_registration = | 597 const BackgroundSyncRegistration* existing_registration = |
548 LookupRegistration(sw_registration_id, registration_key); | 598 LookupRegistration(sw_registration_id, registration_key); |
549 if (!existing_registration || | 599 if (!existing_registration || |
550 existing_registration->id() != sync_registration_id) { | 600 existing_registration->id() != sync_registration_id) { |
| 601 BackgroundSyncMetrics::CountUnregistration(periodicity, |
| 602 ERROR_TYPE_NOT_FOUND); |
551 base::ThreadTaskRunnerHandle::Get()->PostTask( | 603 base::ThreadTaskRunnerHandle::Get()->PostTask( |
552 FROM_HERE, base::Bind(callback, ERROR_TYPE_NOT_FOUND)); | 604 FROM_HERE, base::Bind(callback, ERROR_TYPE_NOT_FOUND)); |
553 return; | 605 return; |
554 } | 606 } |
555 | 607 |
556 RemoveRegistrationFromMap(sw_registration_id, registration_key); | 608 RemoveRegistrationFromMap(sw_registration_id, registration_key); |
557 | 609 |
558 StoreRegistrations( | 610 StoreRegistrations(sw_registration_id, |
559 sw_registration_id, | 611 base::Bind(&BackgroundSyncManager::UnregisterDidStore, |
560 base::Bind(&BackgroundSyncManager::UnregisterDidStore, | 612 weak_ptr_factory_.GetWeakPtr(), |
561 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, callback)); | 613 sw_registration_id, periodicity, callback)); |
562 } | 614 } |
563 | 615 |
564 void BackgroundSyncManager::UnregisterDidStore( | 616 void BackgroundSyncManager::UnregisterDidStore(int64 sw_registration_id, |
565 int64 sw_registration_id, | 617 SyncPeriodicity periodicity, |
566 const StatusCallback& callback, | 618 const StatusCallback& callback, |
567 ServiceWorkerStatusCode status) { | 619 ServiceWorkerStatusCode status) { |
568 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 620 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
569 | 621 |
570 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { | 622 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
571 // ServiceWorker was unregistered. | 623 // ServiceWorker was unregistered. |
| 624 BackgroundSyncMetrics::CountUnregistration(periodicity, ERROR_TYPE_STORAGE); |
572 sw_to_registrations_map_.erase(sw_registration_id); | 625 sw_to_registrations_map_.erase(sw_registration_id); |
573 base::ThreadTaskRunnerHandle::Get()->PostTask( | 626 base::ThreadTaskRunnerHandle::Get()->PostTask( |
574 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); | 627 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); |
575 return; | 628 return; |
576 } | 629 } |
577 | 630 |
578 if (status != SERVICE_WORKER_OK) { | 631 if (status != SERVICE_WORKER_OK) { |
579 LOG(ERROR) << "BackgroundSync failed to unregister due to backend failure."; | 632 LOG(ERROR) << "BackgroundSync failed to unregister due to backend failure."; |
| 633 BackgroundSyncMetrics::CountUnregistration(periodicity, ERROR_TYPE_STORAGE); |
580 DisableAndClearManager(base::Bind(callback, ERROR_TYPE_STORAGE)); | 634 DisableAndClearManager(base::Bind(callback, ERROR_TYPE_STORAGE)); |
581 return; | 635 return; |
582 } | 636 } |
583 | 637 |
| 638 BackgroundSyncMetrics::CountUnregistration(periodicity, ERROR_TYPE_OK); |
584 base::ThreadTaskRunnerHandle::Get()->PostTask( | 639 base::ThreadTaskRunnerHandle::Get()->PostTask( |
585 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK)); | 640 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK)); |
586 } | 641 } |
587 | 642 |
588 void BackgroundSyncManager::GetRegistrationImpl( | 643 void BackgroundSyncManager::GetRegistrationImpl( |
589 int64 sw_registration_id, | 644 int64 sw_registration_id, |
590 const RegistrationKey& registration_key, | 645 const RegistrationKey& registration_key, |
591 const StatusAndRegistrationCallback& callback) { | 646 const StatusAndRegistrationCallback& callback) { |
592 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 647 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
593 | 648 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
635 tag_and_registration.second; | 690 tag_and_registration.second; |
636 if (registration.options()->periodicity == periodicity) | 691 if (registration.options()->periodicity == periodicity) |
637 out_registrations.push_back(registration); | 692 out_registrations.push_back(registration); |
638 } | 693 } |
639 } | 694 } |
640 | 695 |
641 base::ThreadTaskRunnerHandle::Get()->PostTask( | 696 base::ThreadTaskRunnerHandle::Get()->PostTask( |
642 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, out_registrations)); | 697 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, out_registrations)); |
643 } | 698 } |
644 | 699 |
| 700 bool BackgroundSyncManager::AreOptionConditionsMet( |
| 701 const BackgroundSyncRegistrationOptions& options) { |
| 702 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 703 return network_observer_->NetworkSufficient(options.network_state) && |
| 704 power_observer_->PowerSufficient(options.power_state); |
| 705 } |
| 706 |
645 bool BackgroundSyncManager::IsRegistrationReadyToFire( | 707 bool BackgroundSyncManager::IsRegistrationReadyToFire( |
646 const BackgroundSyncRegistration& registration) { | 708 const BackgroundSyncRegistration& registration) { |
647 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 709 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
648 | 710 |
649 // TODO(jkarlin): Add support for firing periodic registrations. | 711 // TODO(jkarlin): Add support for firing periodic registrations. |
650 if (registration.options()->periodicity == SYNC_PERIODIC) | 712 if (registration.options()->periodicity == SYNC_PERIODIC) |
651 return false; | 713 return false; |
652 | 714 |
653 if (registration.sync_state() != SYNC_STATE_PENDING) | 715 if (registration.sync_state() != SYNC_STATE_PENDING) |
654 return false; | 716 return false; |
655 | 717 |
656 DCHECK_EQ(SYNC_ONE_SHOT, registration.options()->periodicity); | 718 DCHECK_EQ(SYNC_ONE_SHOT, registration.options()->periodicity); |
657 | 719 |
658 return network_observer_->NetworkSufficient( | 720 return AreOptionConditionsMet(*registration.options()); |
659 registration.options()->network_state) && | |
660 power_observer_->PowerSufficient(registration.options()->power_state); | |
661 } | 721 } |
662 | 722 |
663 void BackgroundSyncManager::SchedulePendingRegistrations() { | 723 void BackgroundSyncManager::SchedulePendingRegistrations() { |
664 #if defined(OS_ANDROID) | 724 #if defined(OS_ANDROID) |
665 bool keep_browser_alive_for_one_shot = false; | 725 bool keep_browser_alive_for_one_shot = false; |
666 | 726 |
667 for (const auto& sw_id_and_registrations : sw_to_registrations_map_) { | 727 for (const auto& sw_id_and_registrations : sw_to_registrations_map_) { |
668 for (const auto& key_and_registration : | 728 for (const auto& key_and_registration : |
669 sw_id_and_registrations.second.registration_map) { | 729 sw_id_and_registrations.second.registration_map) { |
670 const BackgroundSyncRegistration& registration = | 730 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( | 783 sw_id_and_keys_to_fire.push_back( |
724 std::make_pair(service_worker_id, key_and_registration.first)); | 784 std::make_pair(service_worker_id, key_and_registration.first)); |
725 // The state change is not saved to persistent storage because | 785 // The state change is not saved to persistent storage because |
726 // if the sync event is killed mid-sync then it should return to | 786 // if the sync event is killed mid-sync then it should return to |
727 // SYNC_STATE_PENDING. | 787 // SYNC_STATE_PENDING. |
728 registration->set_sync_state(SYNC_STATE_FIRING); | 788 registration->set_sync_state(SYNC_STATE_FIRING); |
729 } | 789 } |
730 } | 790 } |
731 } | 791 } |
732 | 792 |
| 793 base::TimeTicks start_time = base::TimeTicks::Now(); |
| 794 |
733 // Fire the sync event of the ready registrations and run |callback| once | 795 // Fire the sync event of the ready registrations and run |callback| once |
734 // they're all done. | 796 // they're all done. |
735 base::Closure barrier_closure = | 797 base::Closure events_fired_barrier_closure = |
736 base::BarrierClosure(sw_id_and_keys_to_fire.size(), base::Bind(callback)); | 798 base::BarrierClosure(sw_id_and_keys_to_fire.size(), base::Bind(callback)); |
737 | 799 |
| 800 // Record the total time taken after all events have run to completion. |
| 801 base::Closure events_completed_barrier_closure = |
| 802 base::BarrierClosure(sw_id_and_keys_to_fire.size(), |
| 803 base::Bind(&OnAllSyncEventsCompleted, start_time)); |
| 804 |
738 for (const auto& sw_id_and_key : sw_id_and_keys_to_fire) { | 805 for (const auto& sw_id_and_key : sw_id_and_keys_to_fire) { |
739 int64 service_worker_id = sw_id_and_key.first; | 806 int64 service_worker_id = sw_id_and_key.first; |
740 const BackgroundSyncRegistration* registration = | 807 const BackgroundSyncRegistration* registration = |
741 LookupRegistration(service_worker_id, sw_id_and_key.second); | 808 LookupRegistration(service_worker_id, sw_id_and_key.second); |
742 | 809 |
743 service_worker_context_->FindRegistrationForId( | 810 service_worker_context_->FindRegistrationForId( |
744 service_worker_id, sw_to_registrations_map_[service_worker_id].origin, | 811 service_worker_id, sw_to_registrations_map_[service_worker_id].origin, |
745 base::Bind(&BackgroundSyncManager::FireReadyEventsDidFindRegistration, | 812 base::Bind(&BackgroundSyncManager::FireReadyEventsDidFindRegistration, |
746 weak_ptr_factory_.GetWeakPtr(), sw_id_and_key.second, | 813 weak_ptr_factory_.GetWeakPtr(), sw_id_and_key.second, |
747 registration->id(), barrier_closure)); | 814 registration->id(), events_fired_barrier_closure, |
| 815 events_completed_barrier_closure)); |
748 } | 816 } |
749 | 817 |
750 SchedulePendingRegistrations(); | 818 SchedulePendingRegistrations(); |
751 } | 819 } |
752 | 820 |
753 void BackgroundSyncManager::FireReadyEventsDidFindRegistration( | 821 void BackgroundSyncManager::FireReadyEventsDidFindRegistration( |
754 const RegistrationKey& registration_key, | 822 const RegistrationKey& registration_key, |
755 BackgroundSyncRegistration::RegistrationId registration_id, | 823 BackgroundSyncRegistration::RegistrationId registration_id, |
756 const base::Closure& callback, | 824 const base::Closure& event_fired_callback, |
| 825 const base::Closure& event_completed_callback, |
757 ServiceWorkerStatusCode service_worker_status, | 826 ServiceWorkerStatusCode service_worker_status, |
758 const scoped_refptr<ServiceWorkerRegistration>& | 827 const scoped_refptr<ServiceWorkerRegistration>& |
759 service_worker_registration) { | 828 service_worker_registration) { |
760 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 829 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
761 | |
762 if (service_worker_status != SERVICE_WORKER_OK) { | 830 if (service_worker_status != SERVICE_WORKER_OK) { |
763 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 831 base::ThreadTaskRunnerHandle::Get()->PostTask( |
764 base::Bind(callback)); | 832 FROM_HERE, base::Bind(event_fired_callback)); |
| 833 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 834 FROM_HERE, base::Bind(event_completed_callback)); |
765 return; | 835 return; |
766 } | 836 } |
767 | 837 |
768 FireOneShotSync( | 838 FireOneShotSync( |
769 service_worker_registration->active_version(), | 839 service_worker_registration->active_version(), |
770 base::Bind(&BackgroundSyncManager::EventComplete, | 840 base::Bind(&BackgroundSyncManager::EventComplete, |
771 weak_ptr_factory_.GetWeakPtr(), service_worker_registration, | 841 weak_ptr_factory_.GetWeakPtr(), service_worker_registration, |
772 service_worker_registration->id(), registration_key, | 842 service_worker_registration->id(), registration_key, |
773 registration_id)); | 843 registration_id, event_completed_callback)); |
774 | 844 |
775 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 845 base::ThreadTaskRunnerHandle::Get()->PostTask( |
776 base::Bind(callback)); | 846 FROM_HERE, base::Bind(event_fired_callback)); |
777 } | 847 } |
778 | 848 |
779 // |service_worker_registration| is just to keep the registration alive | 849 // |service_worker_registration| is just to keep the registration alive |
780 // while the event is firing. | 850 // while the event is firing. |
781 void BackgroundSyncManager::EventComplete( | 851 void BackgroundSyncManager::EventComplete( |
782 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, | 852 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, |
783 int64 service_worker_id, | 853 int64 service_worker_id, |
784 const RegistrationKey& key, | 854 const RegistrationKey& key, |
785 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 855 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| 856 const base::Closure& callback, |
786 ServiceWorkerStatusCode status_code) { | 857 ServiceWorkerStatusCode status_code) { |
787 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 858 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
788 | |
789 if (disabled_) | 859 if (disabled_) |
790 return; | 860 return; |
791 | 861 |
792 op_scheduler_.ScheduleOperation( | 862 op_scheduler_.ScheduleOperation(base::Bind( |
793 base::Bind(&BackgroundSyncManager::EventCompleteImpl, | 863 &BackgroundSyncManager::EventCompleteImpl, weak_ptr_factory_.GetWeakPtr(), |
794 weak_ptr_factory_.GetWeakPtr(), service_worker_id, key, | 864 service_worker_id, key, sync_registration_id, status_code, |
795 sync_registration_id, status_code, MakeEmptyCompletion())); | 865 MakeClosureCompletion(callback))); |
796 } | 866 } |
797 | 867 |
798 void BackgroundSyncManager::EventCompleteImpl( | 868 void BackgroundSyncManager::EventCompleteImpl( |
799 int64 service_worker_id, | 869 int64 service_worker_id, |
800 const RegistrationKey& key, | 870 const RegistrationKey& key, |
801 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 871 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
802 ServiceWorkerStatusCode status_code, | 872 ServiceWorkerStatusCode status_code, |
803 const base::Closure& callback) { | 873 const base::Closure& callback) { |
804 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 874 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
805 | 875 |
806 if (disabled_) { | 876 if (disabled_) { |
807 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 877 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
808 base::Bind(callback)); | 878 base::Bind(callback)); |
809 return; | 879 return; |
810 } | 880 } |
811 | 881 |
812 BackgroundSyncRegistration* registration = | 882 BackgroundSyncRegistration* registration = |
813 LookupRegistration(service_worker_id, key); | 883 LookupRegistration(service_worker_id, key); |
814 if (!registration || registration->id() != sync_registration_id) { | 884 if (!registration || registration->id() != sync_registration_id) { |
815 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 885 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
816 base::Bind(callback)); | 886 base::Bind(callback)); |
817 return; | 887 return; |
818 } | 888 } |
819 | 889 |
| 890 // The event ran to completion, we should count it, no matter what happens |
| 891 // from here. |
| 892 BackgroundSyncMetrics::RecordEventResult(registration->options()->periodicity, |
| 893 status_code == SERVICE_WORKER_OK); |
| 894 |
820 if (registration->options()->periodicity == SYNC_ONE_SHOT) { | 895 if (registration->options()->periodicity == SYNC_ONE_SHOT) { |
821 if (status_code != SERVICE_WORKER_OK) { | 896 if (status_code != SERVICE_WORKER_OK) { |
822 // TODO(jkarlin) Fire the sync event on the next page load controlled by | 897 // TODO(jkarlin) Fire the sync event on the next page load controlled by |
823 // this registration. (crbug.com/479665) | 898 // this registration. (crbug.com/479665) |
824 registration->set_sync_state(SYNC_STATE_FAILED); | 899 registration->set_sync_state(SYNC_STATE_FAILED); |
825 } else { | 900 } else { |
826 registration = nullptr; | 901 registration = nullptr; |
827 RemoveRegistrationFromMap(service_worker_id, key); | 902 RemoveRegistrationFromMap(service_worker_id, key); |
828 } | 903 } |
829 } else { | 904 } else { |
(...skipping 25 matching lines...) Expand all Loading... |
855 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " | 930 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " |
856 "failure."; | 931 "failure."; |
857 DisableAndClearManager(base::Bind(callback)); | 932 DisableAndClearManager(base::Bind(callback)); |
858 return; | 933 return; |
859 } | 934 } |
860 | 935 |
861 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 936 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
862 base::Bind(callback)); | 937 base::Bind(callback)); |
863 } | 938 } |
864 | 939 |
| 940 // static |
| 941 void BackgroundSyncManager::OnAllSyncEventsCompleted( |
| 942 const base::TimeTicks& start_time) { |
| 943 // Record the combined time taken by all sync events. |
| 944 BackgroundSyncMetrics::RecordSyncEventHandlingTime(base::TimeTicks::Now() - |
| 945 start_time); |
| 946 } |
| 947 |
865 void BackgroundSyncManager::OnRegistrationDeletedImpl( | 948 void BackgroundSyncManager::OnRegistrationDeletedImpl( |
866 int64 registration_id, | 949 int64 registration_id, |
867 const base::Closure& callback) { | 950 const base::Closure& callback) { |
868 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 951 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
869 | 952 |
870 // The backend (ServiceWorkerStorage) will delete the data, so just delete the | 953 // The backend (ServiceWorkerStorage) will delete the data, so just delete the |
871 // memory representation here. | 954 // memory representation here. |
872 sw_to_registrations_map_.erase(registration_id); | 955 sw_to_registrations_map_.erase(registration_id); |
873 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 956 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
874 base::Bind(callback)); | 957 base::Bind(callback)); |
(...skipping 24 matching lines...) Expand all Loading... |
899 Params... parameters) { | 982 Params... parameters) { |
900 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 983 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
901 | 984 |
902 callback.Run(parameters...); | 985 callback.Run(parameters...); |
903 op_scheduler_.CompleteOperationAndRunNext(); | 986 op_scheduler_.CompleteOperationAndRunNext(); |
904 } | 987 } |
905 | 988 |
906 base::Closure BackgroundSyncManager::MakeEmptyCompletion() { | 989 base::Closure BackgroundSyncManager::MakeEmptyCompletion() { |
907 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 990 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
908 | 991 |
| 992 return MakeClosureCompletion(base::Bind(base::DoNothing)); |
| 993 } |
| 994 |
| 995 base::Closure BackgroundSyncManager::MakeClosureCompletion( |
| 996 const base::Closure& callback) { |
| 997 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 998 |
909 return base::Bind( | 999 return base::Bind( |
910 &BackgroundSyncManager::CompleteOperationCallback<base::Closure>, | 1000 &BackgroundSyncManager::CompleteOperationCallback<base::Closure>, |
911 weak_ptr_factory_.GetWeakPtr(), base::Bind(base::DoNothing)); | 1001 weak_ptr_factory_.GetWeakPtr(), callback); |
912 } | 1002 } |
913 | 1003 |
914 BackgroundSyncManager::StatusAndRegistrationCallback | 1004 BackgroundSyncManager::StatusAndRegistrationCallback |
915 BackgroundSyncManager::MakeStatusAndRegistrationCompletion( | 1005 BackgroundSyncManager::MakeStatusAndRegistrationCompletion( |
916 const StatusAndRegistrationCallback& callback) { | 1006 const StatusAndRegistrationCallback& callback) { |
917 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1007 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
918 | 1008 |
919 return base::Bind(&BackgroundSyncManager::CompleteOperationCallback< | 1009 return base::Bind(&BackgroundSyncManager::CompleteOperationCallback< |
920 StatusAndRegistrationCallback, ErrorType, | 1010 StatusAndRegistrationCallback, ErrorType, |
921 const BackgroundSyncRegistration&>, | 1011 const BackgroundSyncRegistration&>, |
(...skipping 15 matching lines...) Expand all Loading... |
937 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { | 1027 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { |
938 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1028 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
939 | 1029 |
940 return base::Bind( | 1030 return base::Bind( |
941 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, | 1031 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, |
942 ErrorType>, | 1032 ErrorType>, |
943 weak_ptr_factory_.GetWeakPtr(), callback); | 1033 weak_ptr_factory_.GetWeakPtr(), callback); |
944 } | 1034 } |
945 | 1035 |
946 } // namespace content | 1036 } // namespace content |
OLD | NEW |