Chromium Code Reviews| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/barrier_closure.h" | 9 #include "base/barrier_closure.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 sync_manager->Init(); | 142 sync_manager->Init(); |
| 143 return make_scoped_ptr(sync_manager); | 143 return make_scoped_ptr(sync_manager); |
| 144 } | 144 } |
| 145 | 145 |
| 146 BackgroundSyncManager::~BackgroundSyncManager() { | 146 BackgroundSyncManager::~BackgroundSyncManager() { |
| 147 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 147 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 148 | 148 |
| 149 service_worker_context_->RemoveObserver(this); | 149 service_worker_context_->RemoveObserver(this); |
| 150 } | 150 } |
| 151 | 151 |
| 152 BackgroundSyncManager::RegistrationKey::RegistrationKey( | |
| 153 const BackgroundSyncRegistration& registration) | |
| 154 : RegistrationKey(registration.options()->tag) {} | |
| 155 | |
| 156 BackgroundSyncManager::RegistrationKey::RegistrationKey( | |
| 157 const BackgroundSyncRegistrationOptions& options) | |
| 158 : RegistrationKey(options.tag) {} | |
| 159 | |
| 160 BackgroundSyncManager::RegistrationKey::RegistrationKey(const std::string& tag) | |
| 161 : value_("o_" + tag) { | |
|
iclelland
2016/03/08 16:53:15
What affect will removing this have on previously
jkarlin
2016/03/08 17:25:58
I'm not sure what I was thinking when I wrote that
| |
| 162 // Note that the "o_" prefix on the key is because at one time one-shots were | |
| 163 // prefixed with an "o_" and periodic with a "p_". Removing the "o_" requires | |
| 164 // migration. | |
| 165 } | |
| 166 | |
| 167 void BackgroundSyncManager::Register( | 152 void BackgroundSyncManager::Register( |
| 168 int64_t sw_registration_id, | 153 int64_t sw_registration_id, |
| 169 const BackgroundSyncRegistrationOptions& options, | 154 const BackgroundSyncRegistrationOptions& options, |
| 170 bool requested_from_service_worker, | 155 bool requested_from_service_worker, |
| 171 const StatusAndRegistrationCallback& callback) { | 156 const StatusAndRegistrationCallback& callback) { |
| 172 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 157 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 173 | 158 |
| 174 if (disabled_) { | 159 if (disabled_) { |
| 175 BackgroundSyncMetrics::CountRegisterFailure( | 160 BackgroundSyncMetrics::CountRegisterFailure( |
| 176 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 161 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 340 for (int i = 0, max = registrations_proto.registration_size(); i < max; | 325 for (int i = 0, max = registrations_proto.registration_size(); i < max; |
| 341 ++i) { | 326 ++i) { |
| 342 const BackgroundSyncRegistrationProto& registration_proto = | 327 const BackgroundSyncRegistrationProto& registration_proto = |
| 343 registrations_proto.registration(i); | 328 registrations_proto.registration(i); |
| 344 | 329 |
| 345 if (registration_proto.id() >= registrations->next_id) { | 330 if (registration_proto.id() >= registrations->next_id) { |
| 346 corruption_detected = true; | 331 corruption_detected = true; |
| 347 break; | 332 break; |
| 348 } | 333 } |
| 349 | 334 |
| 350 RegistrationKey registration_key(registration_proto.tag()); | |
| 351 | |
| 352 BackgroundSyncRegistration* registration = | 335 BackgroundSyncRegistration* registration = |
| 353 ®istrations->registration_map[registration_key]; | 336 ®istrations->registration_map[registration_proto.tag()]; |
| 354 | 337 |
| 355 BackgroundSyncRegistrationOptions* options = registration->options(); | 338 BackgroundSyncRegistrationOptions* options = registration->options(); |
| 356 options->tag = registration_proto.tag(); | 339 options->tag = registration_proto.tag(); |
| 357 options->network_state = registration_proto.network_state(); | 340 options->network_state = registration_proto.network_state(); |
| 358 | 341 |
| 359 registration->set_id(registration_proto.id()); | 342 registration->set_id(registration_proto.id()); |
| 360 registration->set_num_attempts(registration_proto.num_attempts()); | 343 registration->set_num_attempts(registration_proto.num_attempts()); |
| 361 registration->set_delay_until( | 344 registration->set_delay_until( |
| 362 base::Time::FromInternalValue(registration_proto.delay_until())); | 345 base::Time::FromInternalValue(registration_proto.delay_until())); |
| 363 } | 346 } |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 445 PostErrorResponse(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, callback); | 428 PostErrorResponse(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, callback); |
| 446 return; | 429 return; |
| 447 } | 430 } |
| 448 | 431 |
| 449 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 432 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 450 base::Bind(&NotifyBackgroundSyncRegisteredOnUIThread, | 433 base::Bind(&NotifyBackgroundSyncRegisteredOnUIThread, |
| 451 service_worker_context_, | 434 service_worker_context_, |
| 452 sw_registration->pattern().GetOrigin())); | 435 sw_registration->pattern().GetOrigin())); |
| 453 | 436 |
| 454 BackgroundSyncRegistration* existing_registration = | 437 BackgroundSyncRegistration* existing_registration = |
| 455 LookupActiveRegistration(sw_registration_id, RegistrationKey(options)); | 438 LookupActiveRegistration(sw_registration_id, options.tag); |
| 456 if (existing_registration) { | 439 if (existing_registration) { |
| 457 DCHECK(existing_registration->options()->Equals(options)); | 440 DCHECK(existing_registration->options()->Equals(options)); |
| 458 | 441 |
| 459 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = | 442 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = |
| 460 AreOptionConditionsMet(options) | 443 AreOptionConditionsMet(options) |
| 461 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE | 444 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE |
| 462 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; | 445 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; |
| 463 BackgroundSyncMetrics::CountRegisterSuccess( | 446 BackgroundSyncMetrics::CountRegisterSuccess( |
| 464 registration_could_fire, | 447 registration_could_fire, |
| 465 BackgroundSyncMetrics::REGISTRATION_IS_DUPLICATE); | 448 BackgroundSyncMetrics::REGISTRATION_IS_DUPLICATE); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 548 ServiceWorkerStatusCode status) { | 531 ServiceWorkerStatusCode status) { |
| 549 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 532 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 550 | 533 |
| 551 // The status doesn't matter at this point, there is nothing else to be done. | 534 // The status doesn't matter at this point, there is nothing else to be done. |
| 552 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 535 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 553 base::Bind(barrier_closure)); | 536 base::Bind(barrier_closure)); |
| 554 } | 537 } |
| 555 | 538 |
| 556 BackgroundSyncRegistration* BackgroundSyncManager::LookupActiveRegistration( | 539 BackgroundSyncRegistration* BackgroundSyncManager::LookupActiveRegistration( |
| 557 int64_t sw_registration_id, | 540 int64_t sw_registration_id, |
| 558 const RegistrationKey& registration_key) { | 541 const std::string& tag) { |
| 559 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 542 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 560 | 543 |
| 561 SWIdToRegistrationsMap::iterator it = | 544 SWIdToRegistrationsMap::iterator it = |
| 562 active_registrations_.find(sw_registration_id); | 545 active_registrations_.find(sw_registration_id); |
| 563 if (it == active_registrations_.end()) | 546 if (it == active_registrations_.end()) |
| 564 return nullptr; | 547 return nullptr; |
| 565 | 548 |
| 566 BackgroundSyncRegistrations& registrations = it->second; | 549 BackgroundSyncRegistrations& registrations = it->second; |
| 567 DCHECK_LE(BackgroundSyncRegistration::kInitialId, registrations.next_id); | 550 DCHECK_LE(BackgroundSyncRegistration::kInitialId, registrations.next_id); |
| 568 DCHECK(!registrations.origin.is_empty()); | 551 DCHECK(!registrations.origin.is_empty()); |
| 569 | 552 |
| 570 auto key_and_registration_iter = | 553 auto key_and_registration_iter = registrations.registration_map.find(tag); |
| 571 registrations.registration_map.find(registration_key); | |
| 572 if (key_and_registration_iter == registrations.registration_map.end()) | 554 if (key_and_registration_iter == registrations.registration_map.end()) |
| 573 return nullptr; | 555 return nullptr; |
| 574 | 556 |
| 575 return &key_and_registration_iter->second; | 557 return &key_and_registration_iter->second; |
| 576 } | 558 } |
| 577 | 559 |
| 578 void BackgroundSyncManager::StoreRegistrations( | 560 void BackgroundSyncManager::StoreRegistrations( |
| 579 int64_t sw_registration_id, | 561 int64_t sw_registration_id, |
| 580 const ServiceWorkerStorage::StatusCallback& callback) { | 562 const ServiceWorkerStorage::StatusCallback& callback) { |
| 581 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 563 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 646 FireReadyEvents(); | 628 FireReadyEvents(); |
| 647 | 629 |
| 648 scoped_ptr<BackgroundSyncRegistration> out_registration( | 630 scoped_ptr<BackgroundSyncRegistration> out_registration( |
| 649 new BackgroundSyncRegistration(new_registration)); | 631 new BackgroundSyncRegistration(new_registration)); |
| 650 | 632 |
| 651 base::ThreadTaskRunnerHandle::Get()->PostTask( | 633 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 652 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, | 634 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, |
| 653 base::Passed(std::move(out_registration)))); | 635 base::Passed(std::move(out_registration)))); |
| 654 } | 636 } |
| 655 | 637 |
| 656 void BackgroundSyncManager::RemoveActiveRegistration( | 638 void BackgroundSyncManager::RemoveActiveRegistration(int64_t sw_registration_id, |
| 657 int64_t sw_registration_id, | 639 const std::string& tag) { |
| 658 const RegistrationKey& registration_key) { | |
| 659 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 640 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 660 DCHECK(LookupActiveRegistration(sw_registration_id, registration_key)); | 641 DCHECK(LookupActiveRegistration(sw_registration_id, tag)); |
| 661 | 642 |
| 662 BackgroundSyncRegistrations* registrations = | 643 BackgroundSyncRegistrations* registrations = |
| 663 &active_registrations_[sw_registration_id]; | 644 &active_registrations_[sw_registration_id]; |
| 664 | 645 |
| 665 registrations->registration_map.erase(registration_key); | 646 registrations->registration_map.erase(tag); |
| 666 } | 647 } |
| 667 | 648 |
| 668 void BackgroundSyncManager::AddActiveRegistration( | 649 void BackgroundSyncManager::AddActiveRegistration( |
| 669 int64_t sw_registration_id, | 650 int64_t sw_registration_id, |
| 670 const GURL& origin, | 651 const GURL& origin, |
| 671 const BackgroundSyncRegistration& sync_registration) { | 652 const BackgroundSyncRegistration& sync_registration) { |
| 672 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 653 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 673 DCHECK(sync_registration.IsValid()); | 654 DCHECK(sync_registration.IsValid()); |
| 674 | 655 |
| 675 BackgroundSyncRegistrations* registrations = | 656 BackgroundSyncRegistrations* registrations = |
| 676 &active_registrations_[sw_registration_id]; | 657 &active_registrations_[sw_registration_id]; |
| 677 registrations->origin = origin; | 658 registrations->origin = origin; |
| 678 | 659 |
| 679 RegistrationKey registration_key(sync_registration); | 660 registrations->registration_map[sync_registration.options()->tag] = |
| 680 registrations->registration_map[registration_key] = sync_registration; | 661 sync_registration; |
| 681 } | 662 } |
| 682 | 663 |
| 683 void BackgroundSyncManager::StoreDataInBackend( | 664 void BackgroundSyncManager::StoreDataInBackend( |
| 684 int64_t sw_registration_id, | 665 int64_t sw_registration_id, |
| 685 const GURL& origin, | 666 const GURL& origin, |
| 686 const std::string& backend_key, | 667 const std::string& backend_key, |
| 687 const std::string& data, | 668 const std::string& data, |
| 688 const ServiceWorkerStorage::StatusCallback& callback) { | 669 const ServiceWorkerStorage::StatusCallback& callback) { |
| 689 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 670 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 690 | 671 |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 858 void BackgroundSyncManager::FireReadyEventsImpl(const base::Closure& callback) { | 839 void BackgroundSyncManager::FireReadyEventsImpl(const base::Closure& callback) { |
| 859 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 840 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 860 | 841 |
| 861 if (disabled_) { | 842 if (disabled_) { |
| 862 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 843 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 863 base::Bind(callback)); | 844 base::Bind(callback)); |
| 864 return; | 845 return; |
| 865 } | 846 } |
| 866 | 847 |
| 867 // Find the registrations that are ready to run. | 848 // Find the registrations that are ready to run. |
| 868 std::vector<std::pair<int64_t, RegistrationKey>> sw_id_and_keys_to_fire; | 849 std::vector<std::pair<int64_t, std::string>> sw_id_and_tags_to_fire; |
| 869 | 850 |
| 870 for (auto& sw_id_and_registrations : active_registrations_) { | 851 for (auto& sw_id_and_registrations : active_registrations_) { |
| 871 const int64_t service_worker_id = sw_id_and_registrations.first; | 852 const int64_t service_worker_id = sw_id_and_registrations.first; |
| 872 for (auto& key_and_registration : | 853 for (auto& key_and_registration : |
| 873 sw_id_and_registrations.second.registration_map) { | 854 sw_id_and_registrations.second.registration_map) { |
| 874 BackgroundSyncRegistration* registration = &key_and_registration.second; | 855 BackgroundSyncRegistration* registration = &key_and_registration.second; |
| 875 if (IsRegistrationReadyToFire(*registration)) { | 856 if (IsRegistrationReadyToFire(*registration)) { |
| 876 sw_id_and_keys_to_fire.push_back( | 857 sw_id_and_tags_to_fire.push_back( |
| 877 std::make_pair(service_worker_id, key_and_registration.first)); | 858 std::make_pair(service_worker_id, key_and_registration.first)); |
| 878 // The state change is not saved to persistent storage because | 859 // The state change is not saved to persistent storage because |
| 879 // if the sync event is killed mid-sync then it should return to | 860 // if the sync event is killed mid-sync then it should return to |
| 880 // SYNC_STATE_PENDING. | 861 // SYNC_STATE_PENDING. |
| 881 registration->set_sync_state(BackgroundSyncState::FIRING); | 862 registration->set_sync_state(BackgroundSyncState::FIRING); |
| 882 } | 863 } |
| 883 } | 864 } |
| 884 } | 865 } |
| 885 | 866 |
| 886 if (sw_id_and_keys_to_fire.empty()) { | 867 if (sw_id_and_tags_to_fire.empty()) { |
| 887 RunInBackgroundIfNecessary(); | 868 RunInBackgroundIfNecessary(); |
| 888 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 869 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 889 base::Bind(callback)); | 870 base::Bind(callback)); |
| 890 return; | 871 return; |
| 891 } | 872 } |
| 892 | 873 |
| 893 base::TimeTicks start_time = base::TimeTicks::Now(); | 874 base::TimeTicks start_time = base::TimeTicks::Now(); |
| 894 | 875 |
| 895 // Fire the sync event of the ready registrations and run |callback| once | 876 // Fire the sync event of the ready registrations and run |callback| once |
| 896 // they're all done. | 877 // they're all done. |
| 897 base::Closure events_fired_barrier_closure = base::BarrierClosure( | 878 base::Closure events_fired_barrier_closure = base::BarrierClosure( |
| 898 sw_id_and_keys_to_fire.size(), | 879 sw_id_and_tags_to_fire.size(), |
| 899 base::Bind(&BackgroundSyncManager::FireReadyEventsAllEventsFiring, | 880 base::Bind(&BackgroundSyncManager::FireReadyEventsAllEventsFiring, |
| 900 weak_ptr_factory_.GetWeakPtr(), callback)); | 881 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 901 | 882 |
| 902 // Record the total time taken after all events have run to completion. | 883 // Record the total time taken after all events have run to completion. |
| 903 base::Closure events_completed_barrier_closure = | 884 base::Closure events_completed_barrier_closure = |
| 904 base::BarrierClosure(sw_id_and_keys_to_fire.size(), | 885 base::BarrierClosure(sw_id_and_tags_to_fire.size(), |
| 905 base::Bind(&OnAllSyncEventsCompleted, start_time, | 886 base::Bind(&OnAllSyncEventsCompleted, start_time, |
| 906 sw_id_and_keys_to_fire.size())); | 887 sw_id_and_tags_to_fire.size())); |
| 907 | 888 |
| 908 for (const auto& sw_id_and_key : sw_id_and_keys_to_fire) { | 889 for (const auto& sw_id_and_tag : sw_id_and_tags_to_fire) { |
| 909 int64_t service_worker_id = sw_id_and_key.first; | 890 int64_t service_worker_id = sw_id_and_tag.first; |
| 910 const BackgroundSyncRegistration* registration = | 891 const BackgroundSyncRegistration* registration = |
| 911 LookupActiveRegistration(service_worker_id, sw_id_and_key.second); | 892 LookupActiveRegistration(service_worker_id, sw_id_and_tag.second); |
| 912 DCHECK(registration); | 893 DCHECK(registration); |
| 913 | 894 |
| 914 service_worker_context_->FindReadyRegistrationForId( | 895 service_worker_context_->FindReadyRegistrationForId( |
| 915 service_worker_id, active_registrations_[service_worker_id].origin, | 896 service_worker_id, active_registrations_[service_worker_id].origin, |
| 916 base::Bind(&BackgroundSyncManager::FireReadyEventsDidFindRegistration, | 897 base::Bind(&BackgroundSyncManager::FireReadyEventsDidFindRegistration, |
| 917 weak_ptr_factory_.GetWeakPtr(), sw_id_and_key.second, | 898 weak_ptr_factory_.GetWeakPtr(), sw_id_and_tag.second, |
| 918 registration->id(), events_fired_barrier_closure, | 899 registration->id(), events_fired_barrier_closure, |
| 919 events_completed_barrier_closure)); | 900 events_completed_barrier_closure)); |
| 920 } | 901 } |
| 921 } | 902 } |
| 922 | 903 |
| 923 void BackgroundSyncManager::FireReadyEventsDidFindRegistration( | 904 void BackgroundSyncManager::FireReadyEventsDidFindRegistration( |
| 924 const RegistrationKey& registration_key, | 905 const std::string& tag, |
| 925 BackgroundSyncRegistration::RegistrationId registration_id, | 906 BackgroundSyncRegistration::RegistrationId registration_id, |
| 926 const base::Closure& event_fired_callback, | 907 const base::Closure& event_fired_callback, |
| 927 const base::Closure& event_completed_callback, | 908 const base::Closure& event_completed_callback, |
| 928 ServiceWorkerStatusCode service_worker_status, | 909 ServiceWorkerStatusCode service_worker_status, |
| 929 const scoped_refptr<ServiceWorkerRegistration>& | 910 const scoped_refptr<ServiceWorkerRegistration>& |
| 930 service_worker_registration) { | 911 service_worker_registration) { |
| 931 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 912 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 932 if (service_worker_status != SERVICE_WORKER_OK) { | 913 if (service_worker_status != SERVICE_WORKER_OK) { |
| 933 base::ThreadTaskRunnerHandle::Get()->PostTask( | 914 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 934 FROM_HERE, base::Bind(event_fired_callback)); | 915 FROM_HERE, base::Bind(event_fired_callback)); |
| 935 base::ThreadTaskRunnerHandle::Get()->PostTask( | 916 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 936 FROM_HERE, base::Bind(event_completed_callback)); | 917 FROM_HERE, base::Bind(event_completed_callback)); |
| 937 return; | 918 return; |
| 938 } | 919 } |
| 939 | 920 |
| 940 BackgroundSyncRegistration* registration = LookupActiveRegistration( | 921 BackgroundSyncRegistration* registration = |
| 941 service_worker_registration->id(), registration_key); | 922 LookupActiveRegistration(service_worker_registration->id(), tag); |
| 942 DCHECK(registration); | 923 DCHECK(registration); |
| 943 | 924 |
| 944 num_firing_registrations_ += 1; | 925 num_firing_registrations_ += 1; |
| 945 | 926 |
| 946 BackgroundSyncEventLastChance last_chance = | 927 BackgroundSyncEventLastChance last_chance = |
| 947 registration->num_attempts() == parameters_->max_sync_attempts - 1 | 928 registration->num_attempts() == parameters_->max_sync_attempts - 1 |
| 948 ? BackgroundSyncEventLastChance::IS_LAST_CHANCE | 929 ? BackgroundSyncEventLastChance::IS_LAST_CHANCE |
| 949 : BackgroundSyncEventLastChance::IS_NOT_LAST_CHANCE; | 930 : BackgroundSyncEventLastChance::IS_NOT_LAST_CHANCE; |
| 950 | 931 |
| 951 HasMainFrameProviderHost( | 932 HasMainFrameProviderHost( |
| 952 service_worker_registration->pattern().GetOrigin(), | 933 service_worker_registration->pattern().GetOrigin(), |
| 953 base::Bind(&BackgroundSyncMetrics::RecordEventStarted)); | 934 base::Bind(&BackgroundSyncMetrics::RecordEventStarted)); |
| 954 | 935 |
| 955 DispatchSyncEvent( | 936 DispatchSyncEvent( |
| 956 registration->options()->tag, | 937 registration->options()->tag, |
| 957 service_worker_registration->active_version(), last_chance, | 938 service_worker_registration->active_version(), last_chance, |
| 958 base::Bind(&BackgroundSyncManager::EventComplete, | 939 base::Bind(&BackgroundSyncManager::EventComplete, |
| 959 weak_ptr_factory_.GetWeakPtr(), service_worker_registration, | 940 weak_ptr_factory_.GetWeakPtr(), service_worker_registration, |
| 960 service_worker_registration->id(), registration_key, | 941 service_worker_registration->id(), tag, |
| 961 event_completed_callback)); | 942 event_completed_callback)); |
| 962 | 943 |
| 963 base::ThreadTaskRunnerHandle::Get()->PostTask( | 944 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 964 FROM_HERE, base::Bind(event_fired_callback)); | 945 FROM_HERE, base::Bind(event_fired_callback)); |
| 965 } | 946 } |
| 966 | 947 |
| 967 void BackgroundSyncManager::FireReadyEventsAllEventsFiring( | 948 void BackgroundSyncManager::FireReadyEventsAllEventsFiring( |
| 968 const base::Closure& callback) { | 949 const base::Closure& callback) { |
| 969 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 950 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 970 | 951 |
| 971 RunInBackgroundIfNecessary(); | 952 RunInBackgroundIfNecessary(); |
| 972 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 953 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 973 base::Bind(callback)); | 954 base::Bind(callback)); |
| 974 } | 955 } |
| 975 | 956 |
| 976 // |service_worker_registration| is just to keep the registration alive | 957 // |service_worker_registration| is just to keep the registration alive |
| 977 // while the event is firing. | 958 // while the event is firing. |
| 978 void BackgroundSyncManager::EventComplete( | 959 void BackgroundSyncManager::EventComplete( |
| 979 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, | 960 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, |
| 980 int64_t service_worker_id, | 961 int64_t service_worker_id, |
| 981 const RegistrationKey& registration_key, | 962 const std::string& tag, |
| 982 const base::Closure& callback, | 963 const base::Closure& callback, |
| 983 ServiceWorkerStatusCode status_code) { | 964 ServiceWorkerStatusCode status_code) { |
| 984 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 965 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 985 | 966 |
| 986 if (disabled_) { | 967 if (disabled_) { |
| 987 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 968 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 988 base::Bind(callback)); | 969 base::Bind(callback)); |
| 989 return; | 970 return; |
| 990 } | 971 } |
| 991 | 972 |
| 992 op_scheduler_.ScheduleOperation(base::Bind( | 973 op_scheduler_.ScheduleOperation(base::Bind( |
| 993 &BackgroundSyncManager::EventCompleteImpl, weak_ptr_factory_.GetWeakPtr(), | 974 &BackgroundSyncManager::EventCompleteImpl, weak_ptr_factory_.GetWeakPtr(), |
| 994 service_worker_id, registration_key, status_code, | 975 service_worker_id, tag, status_code, MakeClosureCompletion(callback))); |
| 995 MakeClosureCompletion(callback))); | |
| 996 } | 976 } |
| 997 | 977 |
| 998 void BackgroundSyncManager::EventCompleteImpl( | 978 void BackgroundSyncManager::EventCompleteImpl( |
| 999 int64_t service_worker_id, | 979 int64_t service_worker_id, |
| 1000 const RegistrationKey& registration_key, | 980 const std::string& tag, |
| 1001 ServiceWorkerStatusCode status_code, | 981 ServiceWorkerStatusCode status_code, |
| 1002 const base::Closure& callback) { | 982 const base::Closure& callback) { |
| 1003 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 983 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1004 | 984 |
| 1005 if (disabled_) { | 985 if (disabled_) { |
| 1006 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 986 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 1007 base::Bind(callback)); | 987 base::Bind(callback)); |
| 1008 return; | 988 return; |
| 1009 } | 989 } |
| 1010 | 990 |
| 1011 BackgroundSyncRegistration* registration = | 991 BackgroundSyncRegistration* registration = |
| 1012 LookupActiveRegistration(service_worker_id, registration_key); | 992 LookupActiveRegistration(service_worker_id, tag); |
| 1013 if (!registration) { | 993 if (!registration) { |
| 1014 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 994 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 1015 base::Bind(callback)); | 995 base::Bind(callback)); |
| 1016 return; | 996 return; |
| 1017 } | 997 } |
| 1018 | 998 |
| 1019 DCHECK(registration); | 999 DCHECK(registration); |
| 1020 DCHECK_NE(BackgroundSyncState::PENDING, registration->sync_state()); | 1000 DCHECK_NE(BackgroundSyncState::PENDING, registration->sync_state()); |
| 1021 | 1001 |
| 1022 registration->set_num_attempts(registration->num_attempts() + 1); | 1002 registration->set_num_attempts(registration->num_attempts() + 1); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 1047 can_retry) { // Sync failed but can retry | 1027 can_retry) { // Sync failed but can retry |
| 1048 registration->set_sync_state(BackgroundSyncState::PENDING); | 1028 registration->set_sync_state(BackgroundSyncState::PENDING); |
| 1049 registration->set_delay_until(clock_->Now() + | 1029 registration->set_delay_until(clock_->Now() + |
| 1050 parameters_->initial_retry_delay * | 1030 parameters_->initial_retry_delay * |
| 1051 pow(parameters_->retry_delay_factor, | 1031 pow(parameters_->retry_delay_factor, |
| 1052 registration->num_attempts() - 1)); | 1032 registration->num_attempts() - 1)); |
| 1053 registration_completed = false; | 1033 registration_completed = false; |
| 1054 } | 1034 } |
| 1055 | 1035 |
| 1056 if (registration_completed) { | 1036 if (registration_completed) { |
| 1057 RegistrationKey key(*registration); | 1037 const std::string& tag = registration->options()->tag; |
| 1058 BackgroundSyncRegistration* active_registration = | 1038 BackgroundSyncRegistration* active_registration = |
| 1059 LookupActiveRegistration(service_worker_id, key); | 1039 LookupActiveRegistration(service_worker_id, tag); |
| 1060 if (active_registration && | 1040 if (active_registration && |
| 1061 active_registration->id() == registration->id()) { | 1041 active_registration->id() == registration->id()) { |
| 1062 RemoveActiveRegistration(service_worker_id, key); | 1042 RemoveActiveRegistration(service_worker_id, tag); |
| 1063 } | 1043 } |
| 1064 } | 1044 } |
| 1065 | 1045 |
| 1066 StoreRegistrations( | 1046 StoreRegistrations( |
| 1067 service_worker_id, | 1047 service_worker_id, |
| 1068 base::Bind(&BackgroundSyncManager::EventCompleteDidStore, | 1048 base::Bind(&BackgroundSyncManager::EventCompleteDidStore, |
| 1069 weak_ptr_factory_.GetWeakPtr(), service_worker_id, callback)); | 1049 weak_ptr_factory_.GetWeakPtr(), service_worker_id, callback)); |
| 1070 } | 1050 } |
| 1071 | 1051 |
| 1072 void BackgroundSyncManager::EventCompleteDidStore( | 1052 void BackgroundSyncManager::EventCompleteDidStore( |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1210 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { | 1190 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { |
| 1211 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1191 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1212 | 1192 |
| 1213 return base::Bind( | 1193 return base::Bind( |
| 1214 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, | 1194 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, |
| 1215 BackgroundSyncStatus>, | 1195 BackgroundSyncStatus>, |
| 1216 weak_ptr_factory_.GetWeakPtr(), callback); | 1196 weak_ptr_factory_.GetWeakPtr(), callback); |
| 1217 } | 1197 } |
| 1218 | 1198 |
| 1219 } // namespace content | 1199 } // namespace content |
| OLD | NEW |