| 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 28 matching lines...) Expand all Loading... |
| 39 | 39 |
| 40 // The key used to index the background sync data in ServiceWorkerStorage. | 40 // The key used to index the background sync data in ServiceWorkerStorage. |
| 41 const char kBackgroundSyncUserDataKey[] = "BackgroundSyncUserData"; | 41 const char kBackgroundSyncUserDataKey[] = "BackgroundSyncUserData"; |
| 42 | 42 |
| 43 void RecordFailureAndPostError( | 43 void RecordFailureAndPostError( |
| 44 BackgroundSyncStatus status, | 44 BackgroundSyncStatus status, |
| 45 const BackgroundSyncManager::StatusAndRegistrationCallback& callback) { | 45 const BackgroundSyncManager::StatusAndRegistrationCallback& callback) { |
| 46 BackgroundSyncMetrics::CountRegisterFailure(status); | 46 BackgroundSyncMetrics::CountRegisterFailure(status); |
| 47 | 47 |
| 48 base::ThreadTaskRunnerHandle::Get()->PostTask( | 48 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 49 FROM_HERE, | 49 FROM_HERE, base::Bind(callback, status, nullptr)); |
| 50 base::Bind(callback, status, | |
| 51 base::Passed(std::unique_ptr<BackgroundSyncRegistration>()))); | |
| 52 } | 50 } |
| 53 | 51 |
| 54 // Returns nullptr if the browser context cannot be accessed for any reason. | 52 // Returns nullptr if the browser context cannot be accessed for any reason. |
| 55 BrowserContext* GetBrowserContextOnUIThread( | 53 BrowserContext* GetBrowserContextOnUIThread( |
| 56 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) { | 54 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) { |
| 57 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 55 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 58 | 56 |
| 59 if (!service_worker_context) | 57 if (!service_worker_context) |
| 60 return nullptr; | 58 return nullptr; |
| 61 StoragePartitionImpl* storage_partition_impl = | 59 StoragePartitionImpl* storage_partition_impl = |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 | 305 |
| 308 op_scheduler_.ScheduleOperation(base::Bind(&BackgroundSyncManager::InitImpl, | 306 op_scheduler_.ScheduleOperation(base::Bind(&BackgroundSyncManager::InitImpl, |
| 309 weak_ptr_factory_.GetWeakPtr(), | 307 weak_ptr_factory_.GetWeakPtr(), |
| 310 MakeEmptyCompletion())); | 308 MakeEmptyCompletion())); |
| 311 } | 309 } |
| 312 | 310 |
| 313 void BackgroundSyncManager::InitImpl(const base::Closure& callback) { | 311 void BackgroundSyncManager::InitImpl(const base::Closure& callback) { |
| 314 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 312 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 315 | 313 |
| 316 if (disabled_) { | 314 if (disabled_) { |
| 317 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 315 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
| 318 base::Bind(callback)); | |
| 319 return; | 316 return; |
| 320 } | 317 } |
| 321 | 318 |
| 322 std::unique_ptr<BackgroundSyncParameters> parameters_copy( | 319 std::unique_ptr<BackgroundSyncParameters> parameters_copy( |
| 323 new BackgroundSyncParameters(*parameters_)); | 320 new BackgroundSyncParameters(*parameters_)); |
| 324 | 321 |
| 325 BrowserThread::PostTaskAndReplyWithResult( | 322 BrowserThread::PostTaskAndReplyWithResult( |
| 326 BrowserThread::UI, FROM_HERE, | 323 BrowserThread::UI, FROM_HERE, |
| 327 base::Bind(&GetControllerParameters, service_worker_context_, | 324 base::Bind(&GetControllerParameters, service_worker_context_, |
| 328 base::Passed(std::move(parameters_copy))), | 325 base::Passed(std::move(parameters_copy))), |
| 329 base::Bind(&BackgroundSyncManager::InitDidGetControllerParameters, | 326 base::Bind(&BackgroundSyncManager::InitDidGetControllerParameters, |
| 330 weak_ptr_factory_.GetWeakPtr(), callback)); | 327 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 331 } | 328 } |
| 332 | 329 |
| 333 void BackgroundSyncManager::InitDidGetControllerParameters( | 330 void BackgroundSyncManager::InitDidGetControllerParameters( |
| 334 const base::Closure& callback, | 331 const base::Closure& callback, |
| 335 std::unique_ptr<BackgroundSyncParameters> updated_parameters) { | 332 std::unique_ptr<BackgroundSyncParameters> updated_parameters) { |
| 336 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 333 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 337 | 334 |
| 338 parameters_ = std::move(updated_parameters); | 335 parameters_ = std::move(updated_parameters); |
| 339 if (parameters_->disable) { | 336 if (parameters_->disable) { |
| 340 disabled_ = true; | 337 disabled_ = true; |
| 341 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 338 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
| 342 base::Bind(callback)); | |
| 343 return; | 339 return; |
| 344 } | 340 } |
| 345 | 341 |
| 346 GetDataFromBackend( | 342 GetDataFromBackend( |
| 347 kBackgroundSyncUserDataKey, | 343 kBackgroundSyncUserDataKey, |
| 348 base::Bind(&BackgroundSyncManager::InitDidGetDataFromBackend, | 344 base::Bind(&BackgroundSyncManager::InitDidGetDataFromBackend, |
| 349 weak_ptr_factory_.GetWeakPtr(), callback)); | 345 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 350 } | 346 } |
| 351 | 347 |
| 352 void BackgroundSyncManager::InitDidGetDataFromBackend( | 348 void BackgroundSyncManager::InitDidGetDataFromBackend( |
| 353 const base::Closure& callback, | 349 const base::Closure& callback, |
| 354 const std::vector<std::pair<int64_t, std::string>>& user_data, | 350 const std::vector<std::pair<int64_t, std::string>>& user_data, |
| 355 ServiceWorkerStatusCode status) { | 351 ServiceWorkerStatusCode status) { |
| 356 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 352 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 357 | 353 |
| 358 if (status != SERVICE_WORKER_OK && status != SERVICE_WORKER_ERROR_NOT_FOUND) { | 354 if (status != SERVICE_WORKER_OK && status != SERVICE_WORKER_ERROR_NOT_FOUND) { |
| 359 LOG(ERROR) << "BackgroundSync failed to init due to backend failure."; | 355 LOG(ERROR) << "BackgroundSync failed to init due to backend failure."; |
| 360 DisableAndClearManager(base::Bind(callback)); | 356 DisableAndClearManager(callback); |
| 361 return; | 357 return; |
| 362 } | 358 } |
| 363 | 359 |
| 364 bool corruption_detected = false; | 360 bool corruption_detected = false; |
| 365 for (const std::pair<int64_t, std::string>& data : user_data) { | 361 for (const std::pair<int64_t, std::string>& data : user_data) { |
| 366 BackgroundSyncRegistrationsProto registrations_proto; | 362 BackgroundSyncRegistrationsProto registrations_proto; |
| 367 if (registrations_proto.ParseFromString(data.second)) { | 363 if (registrations_proto.ParseFromString(data.second)) { |
| 368 BackgroundSyncRegistrations* registrations = | 364 BackgroundSyncRegistrations* registrations = |
| 369 &active_registrations_[data.first]; | 365 &active_registrations_[data.first]; |
| 370 registrations->next_id = registrations_proto.next_registration_id(); | 366 registrations->next_id = registrations_proto.next_registration_id(); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 393 base::Time::FromInternalValue(registration_proto.delay_until())); | 389 base::Time::FromInternalValue(registration_proto.delay_until())); |
| 394 } | 390 } |
| 395 } | 391 } |
| 396 | 392 |
| 397 if (corruption_detected) | 393 if (corruption_detected) |
| 398 break; | 394 break; |
| 399 } | 395 } |
| 400 | 396 |
| 401 if (corruption_detected) { | 397 if (corruption_detected) { |
| 402 LOG(ERROR) << "Corruption detected in background sync backend"; | 398 LOG(ERROR) << "Corruption detected in background sync backend"; |
| 403 DisableAndClearManager(base::Bind(callback)); | 399 DisableAndClearManager(callback); |
| 404 return; | 400 return; |
| 405 } | 401 } |
| 406 | 402 |
| 407 FireReadyEvents(); | 403 FireReadyEvents(); |
| 408 | 404 |
| 409 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 405 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
| 410 base::Bind(callback)); | |
| 411 } | 406 } |
| 412 | 407 |
| 413 void BackgroundSyncManager::RegisterCheckIfHasMainFrame( | 408 void BackgroundSyncManager::RegisterCheckIfHasMainFrame( |
| 414 int64_t sw_registration_id, | 409 int64_t sw_registration_id, |
| 415 const BackgroundSyncRegistrationOptions& options, | 410 const BackgroundSyncRegistrationOptions& options, |
| 416 const StatusAndRegistrationCallback& callback) { | 411 const StatusAndRegistrationCallback& callback) { |
| 417 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 412 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 418 | 413 |
| 419 ServiceWorkerRegistration* sw_registration = | 414 ServiceWorkerRegistration* sw_registration = |
| 420 service_worker_context_->GetLiveRegistration(sw_registration_id); | 415 service_worker_context_->GetLiveRegistration(sw_registration_id); |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 550 base::Bind(&BackgroundSyncManager::RegisterDidStore, | 545 base::Bind(&BackgroundSyncManager::RegisterDidStore, |
| 551 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, | 546 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, |
| 552 new_registration, callback)); | 547 new_registration, callback)); |
| 553 } | 548 } |
| 554 | 549 |
| 555 void BackgroundSyncManager::DisableAndClearManager( | 550 void BackgroundSyncManager::DisableAndClearManager( |
| 556 const base::Closure& callback) { | 551 const base::Closure& callback) { |
| 557 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 552 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 558 | 553 |
| 559 if (disabled_) { | 554 if (disabled_) { |
| 560 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 555 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
| 561 base::Bind(callback)); | |
| 562 return; | 556 return; |
| 563 } | 557 } |
| 564 | 558 |
| 565 disabled_ = true; | 559 disabled_ = true; |
| 566 | 560 |
| 567 active_registrations_.clear(); | 561 active_registrations_.clear(); |
| 568 | 562 |
| 569 // Delete all backend entries. The memory representation of registered syncs | 563 // Delete all backend entries. The memory representation of registered syncs |
| 570 // may be out of sync with storage (e.g., due to corruption detection on | 564 // may be out of sync with storage (e.g., due to corruption detection on |
| 571 // loading from storage), so reload the registrations from storage again. | 565 // loading from storage), so reload the registrations from storage again. |
| 572 GetDataFromBackend( | 566 GetDataFromBackend( |
| 573 kBackgroundSyncUserDataKey, | 567 kBackgroundSyncUserDataKey, |
| 574 base::Bind(&BackgroundSyncManager::DisableAndClearDidGetRegistrations, | 568 base::Bind(&BackgroundSyncManager::DisableAndClearDidGetRegistrations, |
| 575 weak_ptr_factory_.GetWeakPtr(), callback)); | 569 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 576 } | 570 } |
| 577 | 571 |
| 578 void BackgroundSyncManager::DisableAndClearDidGetRegistrations( | 572 void BackgroundSyncManager::DisableAndClearDidGetRegistrations( |
| 579 const base::Closure& callback, | 573 const base::Closure& callback, |
| 580 const std::vector<std::pair<int64_t, std::string>>& user_data, | 574 const std::vector<std::pair<int64_t, std::string>>& user_data, |
| 581 ServiceWorkerStatusCode status) { | 575 ServiceWorkerStatusCode status) { |
| 582 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 576 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 583 | 577 |
| 584 if (status != SERVICE_WORKER_OK || user_data.empty()) { | 578 if (status != SERVICE_WORKER_OK || user_data.empty()) { |
| 585 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 579 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
| 586 base::Bind(callback)); | |
| 587 return; | 580 return; |
| 588 } | 581 } |
| 589 | 582 |
| 590 base::Closure barrier_closure = | 583 base::Closure barrier_closure = |
| 591 base::BarrierClosure(user_data.size(), base::Bind(callback)); | 584 base::BarrierClosure(user_data.size(), callback); |
| 592 | 585 |
| 593 for (const auto& sw_id_and_regs : user_data) { | 586 for (const auto& sw_id_and_regs : user_data) { |
| 594 service_worker_context_->ClearRegistrationUserData( | 587 service_worker_context_->ClearRegistrationUserData( |
| 595 sw_id_and_regs.first, {kBackgroundSyncUserDataKey}, | 588 sw_id_and_regs.first, {kBackgroundSyncUserDataKey}, |
| 596 base::Bind(&BackgroundSyncManager::DisableAndClearManagerClearedOne, | 589 base::Bind(&BackgroundSyncManager::DisableAndClearManagerClearedOne, |
| 597 weak_ptr_factory_.GetWeakPtr(), barrier_closure)); | 590 weak_ptr_factory_.GetWeakPtr(), barrier_closure)); |
| 598 } | 591 } |
| 599 } | 592 } |
| 600 | 593 |
| 601 void BackgroundSyncManager::DisableAndClearManagerClearedOne( | 594 void BackgroundSyncManager::DisableAndClearManagerClearedOne( |
| 602 const base::Closure& barrier_closure, | 595 const base::Closure& barrier_closure, |
| 603 ServiceWorkerStatusCode status) { | 596 ServiceWorkerStatusCode status) { |
| 604 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 597 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 605 | 598 |
| 606 // The status doesn't matter at this point, there is nothing else to be done. | 599 // The status doesn't matter at this point, there is nothing else to be done. |
| 607 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 600 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, barrier_closure); |
| 608 base::Bind(barrier_closure)); | |
| 609 } | 601 } |
| 610 | 602 |
| 611 BackgroundSyncRegistration* BackgroundSyncManager::LookupActiveRegistration( | 603 BackgroundSyncRegistration* BackgroundSyncManager::LookupActiveRegistration( |
| 612 int64_t sw_registration_id, | 604 int64_t sw_registration_id, |
| 613 const std::string& tag) { | 605 const std::string& tag) { |
| 614 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 606 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 615 | 607 |
| 616 SWIdToRegistrationsMap::iterator it = | 608 SWIdToRegistrationsMap::iterator it = |
| 617 active_registrations_.find(sw_registration_id); | 609 active_registrations_.find(sw_registration_id); |
| 618 if (it == active_registrations_.end()) | 610 if (it == active_registrations_.end()) |
| (...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 904 | 896 |
| 905 op_scheduler_.ScheduleOperation( | 897 op_scheduler_.ScheduleOperation( |
| 906 base::Bind(&BackgroundSyncManager::FireReadyEventsImpl, | 898 base::Bind(&BackgroundSyncManager::FireReadyEventsImpl, |
| 907 weak_ptr_factory_.GetWeakPtr(), MakeEmptyCompletion())); | 899 weak_ptr_factory_.GetWeakPtr(), MakeEmptyCompletion())); |
| 908 } | 900 } |
| 909 | 901 |
| 910 void BackgroundSyncManager::FireReadyEventsImpl(const base::Closure& callback) { | 902 void BackgroundSyncManager::FireReadyEventsImpl(const base::Closure& callback) { |
| 911 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 903 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 912 | 904 |
| 913 if (disabled_) { | 905 if (disabled_) { |
| 914 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 906 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
| 915 base::Bind(callback)); | |
| 916 return; | 907 return; |
| 917 } | 908 } |
| 918 | 909 |
| 919 // Find the registrations that are ready to run. | 910 // Find the registrations that are ready to run. |
| 920 std::vector<std::pair<int64_t, std::string>> sw_id_and_tags_to_fire; | 911 std::vector<std::pair<int64_t, std::string>> sw_id_and_tags_to_fire; |
| 921 | 912 |
| 922 for (auto& sw_id_and_registrations : active_registrations_) { | 913 for (auto& sw_id_and_registrations : active_registrations_) { |
| 923 const int64_t service_worker_id = sw_id_and_registrations.first; | 914 const int64_t service_worker_id = sw_id_and_registrations.first; |
| 924 for (auto& key_and_registration : | 915 for (auto& key_and_registration : |
| 925 sw_id_and_registrations.second.registration_map) { | 916 sw_id_and_registrations.second.registration_map) { |
| 926 BackgroundSyncRegistration* registration = &key_and_registration.second; | 917 BackgroundSyncRegistration* registration = &key_and_registration.second; |
| 927 if (IsRegistrationReadyToFire(*registration)) { | 918 if (IsRegistrationReadyToFire(*registration)) { |
| 928 sw_id_and_tags_to_fire.push_back( | 919 sw_id_and_tags_to_fire.push_back( |
| 929 std::make_pair(service_worker_id, key_and_registration.first)); | 920 std::make_pair(service_worker_id, key_and_registration.first)); |
| 930 // The state change is not saved to persistent storage because | 921 // The state change is not saved to persistent storage because |
| 931 // if the sync event is killed mid-sync then it should return to | 922 // if the sync event is killed mid-sync then it should return to |
| 932 // SYNC_STATE_PENDING. | 923 // SYNC_STATE_PENDING. |
| 933 registration->set_sync_state(blink::mojom::BackgroundSyncState::FIRING); | 924 registration->set_sync_state(blink::mojom::BackgroundSyncState::FIRING); |
| 934 } | 925 } |
| 935 } | 926 } |
| 936 } | 927 } |
| 937 | 928 |
| 938 if (sw_id_and_tags_to_fire.empty()) { | 929 if (sw_id_and_tags_to_fire.empty()) { |
| 939 RunInBackgroundIfNecessary(); | 930 RunInBackgroundIfNecessary(); |
| 940 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 931 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
| 941 base::Bind(callback)); | |
| 942 return; | 932 return; |
| 943 } | 933 } |
| 944 | 934 |
| 945 base::TimeTicks start_time = base::TimeTicks::Now(); | 935 base::TimeTicks start_time = base::TimeTicks::Now(); |
| 946 | 936 |
| 947 // Fire the sync event of the ready registrations and run |callback| once | 937 // Fire the sync event of the ready registrations and run |callback| once |
| 948 // they're all done. | 938 // they're all done. |
| 949 base::Closure events_fired_barrier_closure = base::BarrierClosure( | 939 base::Closure events_fired_barrier_closure = base::BarrierClosure( |
| 950 sw_id_and_tags_to_fire.size(), | 940 sw_id_and_tags_to_fire.size(), |
| 951 base::Bind(&BackgroundSyncManager::FireReadyEventsAllEventsFiring, | 941 base::Bind(&BackgroundSyncManager::FireReadyEventsAllEventsFiring, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 974 | 964 |
| 975 void BackgroundSyncManager::FireReadyEventsDidFindRegistration( | 965 void BackgroundSyncManager::FireReadyEventsDidFindRegistration( |
| 976 const std::string& tag, | 966 const std::string& tag, |
| 977 BackgroundSyncRegistration::RegistrationId registration_id, | 967 BackgroundSyncRegistration::RegistrationId registration_id, |
| 978 const base::Closure& event_fired_callback, | 968 const base::Closure& event_fired_callback, |
| 979 const base::Closure& event_completed_callback, | 969 const base::Closure& event_completed_callback, |
| 980 ServiceWorkerStatusCode service_worker_status, | 970 ServiceWorkerStatusCode service_worker_status, |
| 981 scoped_refptr<ServiceWorkerRegistration> service_worker_registration) { | 971 scoped_refptr<ServiceWorkerRegistration> service_worker_registration) { |
| 982 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 972 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 983 if (service_worker_status != SERVICE_WORKER_OK) { | 973 if (service_worker_status != SERVICE_WORKER_OK) { |
| 984 base::ThreadTaskRunnerHandle::Get()->PostTask( | 974 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 985 FROM_HERE, base::Bind(event_fired_callback)); | 975 event_fired_callback); |
| 986 base::ThreadTaskRunnerHandle::Get()->PostTask( | 976 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 987 FROM_HERE, base::Bind(event_completed_callback)); | 977 event_completed_callback); |
| 988 return; | 978 return; |
| 989 } | 979 } |
| 990 | 980 |
| 991 BackgroundSyncRegistration* registration = | 981 BackgroundSyncRegistration* registration = |
| 992 LookupActiveRegistration(service_worker_registration->id(), tag); | 982 LookupActiveRegistration(service_worker_registration->id(), tag); |
| 993 DCHECK(registration); | 983 DCHECK(registration); |
| 994 | 984 |
| 995 num_firing_registrations_ += 1; | 985 num_firing_registrations_ += 1; |
| 996 | 986 |
| 997 blink::mojom::BackgroundSyncEventLastChance last_chance = | 987 blink::mojom::BackgroundSyncEventLastChance last_chance = |
| 998 registration->num_attempts() == parameters_->max_sync_attempts - 1 | 988 registration->num_attempts() == parameters_->max_sync_attempts - 1 |
| 999 ? blink::mojom::BackgroundSyncEventLastChance::IS_LAST_CHANCE | 989 ? blink::mojom::BackgroundSyncEventLastChance::IS_LAST_CHANCE |
| 1000 : blink::mojom::BackgroundSyncEventLastChance::IS_NOT_LAST_CHANCE; | 990 : blink::mojom::BackgroundSyncEventLastChance::IS_NOT_LAST_CHANCE; |
| 1001 | 991 |
| 1002 HasMainFrameProviderHost( | 992 HasMainFrameProviderHost( |
| 1003 service_worker_registration->pattern().GetOrigin(), | 993 service_worker_registration->pattern().GetOrigin(), |
| 1004 base::Bind(&BackgroundSyncMetrics::RecordEventStarted)); | 994 base::Bind(&BackgroundSyncMetrics::RecordEventStarted)); |
| 1005 | 995 |
| 1006 DispatchSyncEvent( | 996 DispatchSyncEvent( |
| 1007 registration->options()->tag, | 997 registration->options()->tag, |
| 1008 service_worker_registration->active_version(), last_chance, | 998 service_worker_registration->active_version(), last_chance, |
| 1009 base::Bind(&BackgroundSyncManager::EventComplete, | 999 base::Bind(&BackgroundSyncManager::EventComplete, |
| 1010 weak_ptr_factory_.GetWeakPtr(), service_worker_registration, | 1000 weak_ptr_factory_.GetWeakPtr(), service_worker_registration, |
| 1011 service_worker_registration->id(), tag, | 1001 service_worker_registration->id(), tag, |
| 1012 event_completed_callback)); | 1002 event_completed_callback)); |
| 1013 | 1003 |
| 1014 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1004 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 1015 FROM_HERE, base::Bind(event_fired_callback)); | 1005 event_fired_callback); |
| 1016 } | 1006 } |
| 1017 | 1007 |
| 1018 void BackgroundSyncManager::FireReadyEventsAllEventsFiring( | 1008 void BackgroundSyncManager::FireReadyEventsAllEventsFiring( |
| 1019 const base::Closure& callback) { | 1009 const base::Closure& callback) { |
| 1020 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1010 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1021 | 1011 |
| 1022 RunInBackgroundIfNecessary(); | 1012 RunInBackgroundIfNecessary(); |
| 1023 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 1013 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
| 1024 base::Bind(callback)); | |
| 1025 } | 1014 } |
| 1026 | 1015 |
| 1027 // |service_worker_registration| is just to keep the registration alive | 1016 // |service_worker_registration| is just to keep the registration alive |
| 1028 // while the event is firing. | 1017 // while the event is firing. |
| 1029 void BackgroundSyncManager::EventComplete( | 1018 void BackgroundSyncManager::EventComplete( |
| 1030 scoped_refptr<ServiceWorkerRegistration> service_worker_registration, | 1019 scoped_refptr<ServiceWorkerRegistration> service_worker_registration, |
| 1031 int64_t service_worker_id, | 1020 int64_t service_worker_id, |
| 1032 const std::string& tag, | 1021 const std::string& tag, |
| 1033 const base::Closure& callback, | 1022 const base::Closure& callback, |
| 1034 ServiceWorkerStatusCode status_code) { | 1023 ServiceWorkerStatusCode status_code) { |
| 1035 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1024 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1036 | 1025 |
| 1037 if (disabled_) { | 1026 if (disabled_) { |
| 1038 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 1027 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
| 1039 base::Bind(callback)); | |
| 1040 return; | 1028 return; |
| 1041 } | 1029 } |
| 1042 | 1030 |
| 1043 op_scheduler_.ScheduleOperation( | 1031 op_scheduler_.ScheduleOperation( |
| 1044 base::Bind(&BackgroundSyncManager::EventCompleteImpl, | 1032 base::Bind(&BackgroundSyncManager::EventCompleteImpl, |
| 1045 weak_ptr_factory_.GetWeakPtr(), service_worker_id, tag, | 1033 weak_ptr_factory_.GetWeakPtr(), service_worker_id, tag, |
| 1046 status_code, op_scheduler_.WrapCallbackToRunNext(callback))); | 1034 status_code, op_scheduler_.WrapCallbackToRunNext(callback))); |
| 1047 } | 1035 } |
| 1048 | 1036 |
| 1049 void BackgroundSyncManager::EventCompleteImpl( | 1037 void BackgroundSyncManager::EventCompleteImpl( |
| 1050 int64_t service_worker_id, | 1038 int64_t service_worker_id, |
| 1051 const std::string& tag, | 1039 const std::string& tag, |
| 1052 ServiceWorkerStatusCode status_code, | 1040 ServiceWorkerStatusCode status_code, |
| 1053 const base::Closure& callback) { | 1041 const base::Closure& callback) { |
| 1054 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1042 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1055 | 1043 |
| 1056 if (disabled_) { | 1044 if (disabled_) { |
| 1057 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 1045 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
| 1058 base::Bind(callback)); | |
| 1059 return; | 1046 return; |
| 1060 } | 1047 } |
| 1061 | 1048 |
| 1062 num_firing_registrations_ -= 1; | 1049 num_firing_registrations_ -= 1; |
| 1063 | 1050 |
| 1064 BackgroundSyncRegistration* registration = | 1051 BackgroundSyncRegistration* registration = |
| 1065 LookupActiveRegistration(service_worker_id, tag); | 1052 LookupActiveRegistration(service_worker_id, tag); |
| 1066 if (!registration) { | 1053 if (!registration) { |
| 1067 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 1054 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
| 1068 base::Bind(callback)); | |
| 1069 return; | 1055 return; |
| 1070 } | 1056 } |
| 1071 | 1057 |
| 1072 DCHECK_NE(blink::mojom::BackgroundSyncState::PENDING, | 1058 DCHECK_NE(blink::mojom::BackgroundSyncState::PENDING, |
| 1073 registration->sync_state()); | 1059 registration->sync_state()); |
| 1074 | 1060 |
| 1075 registration->set_num_attempts(registration->num_attempts() + 1); | 1061 registration->set_num_attempts(registration->num_attempts() + 1); |
| 1076 | 1062 |
| 1077 // The event ran to completion, we should count it, no matter what happens | 1063 // The event ran to completion, we should count it, no matter what happens |
| 1078 // from here. | 1064 // from here. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1122 | 1108 |
| 1123 void BackgroundSyncManager::EventCompleteDidStore( | 1109 void BackgroundSyncManager::EventCompleteDidStore( |
| 1124 int64_t service_worker_id, | 1110 int64_t service_worker_id, |
| 1125 const base::Closure& callback, | 1111 const base::Closure& callback, |
| 1126 ServiceWorkerStatusCode status_code) { | 1112 ServiceWorkerStatusCode status_code) { |
| 1127 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1113 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1128 | 1114 |
| 1129 if (status_code == SERVICE_WORKER_ERROR_NOT_FOUND) { | 1115 if (status_code == SERVICE_WORKER_ERROR_NOT_FOUND) { |
| 1130 // The registration is gone. | 1116 // The registration is gone. |
| 1131 active_registrations_.erase(service_worker_id); | 1117 active_registrations_.erase(service_worker_id); |
| 1132 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 1118 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
| 1133 base::Bind(callback)); | |
| 1134 return; | 1119 return; |
| 1135 } | 1120 } |
| 1136 | 1121 |
| 1137 if (status_code != SERVICE_WORKER_OK) { | 1122 if (status_code != SERVICE_WORKER_OK) { |
| 1138 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " | 1123 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " |
| 1139 "failure."; | 1124 "failure."; |
| 1140 DisableAndClearManager(base::Bind(callback)); | 1125 DisableAndClearManager(callback); |
| 1141 return; | 1126 return; |
| 1142 } | 1127 } |
| 1143 | 1128 |
| 1144 // Fire any ready events and call RunInBackground if anything is waiting. | 1129 // Fire any ready events and call RunInBackground if anything is waiting. |
| 1145 FireReadyEvents(); | 1130 FireReadyEvents(); |
| 1146 | 1131 |
| 1147 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 1132 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
| 1148 base::Bind(callback)); | |
| 1149 } | 1133 } |
| 1150 | 1134 |
| 1151 // static | 1135 // static |
| 1152 void BackgroundSyncManager::OnAllSyncEventsCompleted( | 1136 void BackgroundSyncManager::OnAllSyncEventsCompleted( |
| 1153 const base::TimeTicks& start_time, | 1137 const base::TimeTicks& start_time, |
| 1154 int number_of_batched_sync_events) { | 1138 int number_of_batched_sync_events) { |
| 1155 // Record the combined time taken by all sync events. | 1139 // Record the combined time taken by all sync events. |
| 1156 BackgroundSyncMetrics::RecordBatchSyncEventComplete( | 1140 BackgroundSyncMetrics::RecordBatchSyncEventComplete( |
| 1157 base::TimeTicks::Now() - start_time, number_of_batched_sync_events); | 1141 base::TimeTicks::Now() - start_time, number_of_batched_sync_events); |
| 1158 } | 1142 } |
| 1159 | 1143 |
| 1160 void BackgroundSyncManager::OnRegistrationDeletedImpl( | 1144 void BackgroundSyncManager::OnRegistrationDeletedImpl( |
| 1161 int64_t sw_registration_id, | 1145 int64_t sw_registration_id, |
| 1162 const base::Closure& callback) { | 1146 const base::Closure& callback) { |
| 1163 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1147 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1164 | 1148 |
| 1165 // The backend (ServiceWorkerStorage) will delete the data, so just delete the | 1149 // The backend (ServiceWorkerStorage) will delete the data, so just delete the |
| 1166 // memory representation here. | 1150 // memory representation here. |
| 1167 active_registrations_.erase(sw_registration_id); | 1151 active_registrations_.erase(sw_registration_id); |
| 1168 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 1152 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
| 1169 base::Bind(callback)); | |
| 1170 } | 1153 } |
| 1171 | 1154 |
| 1172 void BackgroundSyncManager::OnStorageWipedImpl(const base::Closure& callback) { | 1155 void BackgroundSyncManager::OnStorageWipedImpl(const base::Closure& callback) { |
| 1173 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1156 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1174 | 1157 |
| 1175 active_registrations_.clear(); | 1158 active_registrations_.clear(); |
| 1176 disabled_ = false; | 1159 disabled_ = false; |
| 1177 InitImpl(callback); | 1160 InitImpl(callback); |
| 1178 } | 1161 } |
| 1179 | 1162 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 1191 parameters_->max_sync_attempts = max_attempts; | 1174 parameters_->max_sync_attempts = max_attempts; |
| 1192 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); | 1175 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
| 1193 } | 1176 } |
| 1194 | 1177 |
| 1195 base::Closure BackgroundSyncManager::MakeEmptyCompletion() { | 1178 base::Closure BackgroundSyncManager::MakeEmptyCompletion() { |
| 1196 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1179 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1197 return op_scheduler_.WrapCallbackToRunNext(base::Bind(&base::DoNothing)); | 1180 return op_scheduler_.WrapCallbackToRunNext(base::Bind(&base::DoNothing)); |
| 1198 } | 1181 } |
| 1199 | 1182 |
| 1200 } // namespace content | 1183 } // namespace content |
| OLD | NEW |