| 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 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 } | 156 } |
| 157 | 157 |
| 158 BackgroundSyncManager::~BackgroundSyncManager() { | 158 BackgroundSyncManager::~BackgroundSyncManager() { |
| 159 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 159 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 160 | 160 |
| 161 service_worker_context_->RemoveObserver(this); | 161 service_worker_context_->RemoveObserver(this); |
| 162 } | 162 } |
| 163 | 163 |
| 164 BackgroundSyncManager::RegistrationKey::RegistrationKey( | 164 BackgroundSyncManager::RegistrationKey::RegistrationKey( |
| 165 const BackgroundSyncRegistration& registration) | 165 const BackgroundSyncRegistration& registration) |
| 166 : RegistrationKey(registration.options()->tag, | 166 : RegistrationKey(registration.options()->tag) {} |
| 167 registration.options()->periodicity) { | |
| 168 } | |
| 169 | 167 |
| 170 BackgroundSyncManager::RegistrationKey::RegistrationKey( | 168 BackgroundSyncManager::RegistrationKey::RegistrationKey( |
| 171 const BackgroundSyncRegistrationOptions& options) | 169 const BackgroundSyncRegistrationOptions& options) |
| 172 : RegistrationKey(options.tag, options.periodicity) { | 170 : RegistrationKey(options.tag) {} |
| 173 } | |
| 174 | 171 |
| 175 BackgroundSyncManager::RegistrationKey::RegistrationKey( | 172 BackgroundSyncManager::RegistrationKey::RegistrationKey(const std::string& tag) |
| 176 const std::string& tag, | 173 : value_("o_" + tag) { |
| 177 SyncPeriodicity periodicity) | 174 // Note that the "o_" prefix on the key is because at one time one-shots were |
| 178 : value_(periodicity == SYNC_ONE_SHOT ? "o_" + tag : "p_" + tag) { | 175 // prefixed with an "o_" and periodic with a "p_". Removing the "o_" requires |
| 176 // migration. |
| 179 } | 177 } |
| 180 | 178 |
| 181 void BackgroundSyncManager::Register( | 179 void BackgroundSyncManager::Register( |
| 182 int64_t sw_registration_id, | 180 int64_t sw_registration_id, |
| 183 const BackgroundSyncRegistrationOptions& options, | 181 const BackgroundSyncRegistrationOptions& options, |
| 184 bool requested_from_service_worker, | 182 bool requested_from_service_worker, |
| 185 const StatusAndRegistrationCallback& callback) { | 183 const StatusAndRegistrationCallback& callback) { |
| 186 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 184 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 187 | 185 |
| 188 if (disabled_) { | 186 if (disabled_) { |
| 189 BackgroundSyncMetrics::CountRegisterFailure( | 187 BackgroundSyncMetrics::CountRegisterFailure( |
| 190 options.periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 188 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
| 191 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); | 189 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); |
| 192 return; | 190 return; |
| 193 } | 191 } |
| 194 | 192 |
| 195 if (requested_from_service_worker) { | 193 if (requested_from_service_worker) { |
| 196 op_scheduler_.ScheduleOperation( | 194 op_scheduler_.ScheduleOperation( |
| 197 base::Bind(&BackgroundSyncManager::RegisterCheckIfHasMainFrame, | 195 base::Bind(&BackgroundSyncManager::RegisterCheckIfHasMainFrame, |
| 198 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options, | 196 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options, |
| 199 MakeStatusAndRegistrationCompletion(callback))); | 197 MakeStatusAndRegistrationCompletion(callback))); |
| 200 return; | 198 return; |
| 201 } | 199 } |
| 202 | 200 |
| 203 op_scheduler_.ScheduleOperation( | 201 op_scheduler_.ScheduleOperation( |
| 204 base::Bind(&BackgroundSyncManager::RegisterImpl, | 202 base::Bind(&BackgroundSyncManager::RegisterImpl, |
| 205 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options, | 203 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options, |
| 206 MakeStatusAndRegistrationCompletion(callback))); | 204 MakeStatusAndRegistrationCompletion(callback))); |
| 207 } | 205 } |
| 208 | 206 |
| 209 void BackgroundSyncManager::GetRegistration( | 207 void BackgroundSyncManager::GetRegistration( |
| 210 int64_t sw_registration_id, | 208 int64_t sw_registration_id, |
| 211 const std::string& sync_registration_tag, | 209 const std::string& sync_registration_tag, |
| 212 SyncPeriodicity periodicity, | |
| 213 const StatusAndRegistrationCallback& callback) { | 210 const StatusAndRegistrationCallback& callback) { |
| 214 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 211 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 215 | 212 |
| 216 if (disabled_) { | 213 if (disabled_) { |
| 217 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); | 214 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); |
| 218 return; | 215 return; |
| 219 } | 216 } |
| 220 | 217 |
| 221 RegistrationKey registration_key(sync_registration_tag, periodicity); | 218 RegistrationKey registration_key(sync_registration_tag); |
| 222 | 219 |
| 223 op_scheduler_.ScheduleOperation(base::Bind( | 220 op_scheduler_.ScheduleOperation(base::Bind( |
| 224 &BackgroundSyncManager::GetRegistrationImpl, | 221 &BackgroundSyncManager::GetRegistrationImpl, |
| 225 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, registration_key, | 222 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, registration_key, |
| 226 MakeStatusAndRegistrationCompletion(callback))); | 223 MakeStatusAndRegistrationCompletion(callback))); |
| 227 } | 224 } |
| 228 | 225 |
| 229 void BackgroundSyncManager::GetRegistrations( | 226 void BackgroundSyncManager::GetRegistrations( |
| 230 int64_t sw_registration_id, | 227 int64_t sw_registration_id, |
| 231 SyncPeriodicity periodicity, | |
| 232 const StatusAndRegistrationsCallback& callback) { | 228 const StatusAndRegistrationsCallback& callback) { |
| 233 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 229 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 234 | 230 |
| 235 if (disabled_) { | 231 if (disabled_) { |
| 236 base::ThreadTaskRunnerHandle::Get()->PostTask( | 232 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 237 FROM_HERE, | 233 FROM_HERE, |
| 238 base::Bind( | 234 base::Bind( |
| 239 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | 235 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
| 240 base::Passed( | 236 base::Passed( |
| 241 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>>( | 237 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>>( |
| 242 new ScopedVector<BackgroundSyncRegistrationHandle>())))); | 238 new ScopedVector<BackgroundSyncRegistrationHandle>())))); |
| 243 return; | 239 return; |
| 244 } | 240 } |
| 245 | 241 |
| 246 op_scheduler_.ScheduleOperation( | 242 op_scheduler_.ScheduleOperation( |
| 247 base::Bind(&BackgroundSyncManager::GetRegistrationsImpl, | 243 base::Bind(&BackgroundSyncManager::GetRegistrationsImpl, |
| 248 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, | 244 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, |
| 249 periodicity, MakeStatusAndRegistrationsCompletion(callback))); | 245 MakeStatusAndRegistrationsCompletion(callback))); |
| 250 } | 246 } |
| 251 | 247 |
| 252 // Given a HandleId |handle_id|, return a new handle for the same | 248 // Given a HandleId |handle_id|, return a new handle for the same |
| 253 // registration. | 249 // registration. |
| 254 scoped_ptr<BackgroundSyncRegistrationHandle> | 250 scoped_ptr<BackgroundSyncRegistrationHandle> |
| 255 BackgroundSyncManager::DuplicateRegistrationHandle( | 251 BackgroundSyncManager::DuplicateRegistrationHandle( |
| 256 BackgroundSyncRegistrationHandle::HandleId handle_id) { | 252 BackgroundSyncRegistrationHandle::HandleId handle_id) { |
| 257 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 253 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 258 | 254 |
| 259 scoped_refptr<RefCountedRegistration>* ref_registration = | 255 scoped_refptr<RefCountedRegistration>* ref_registration = |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 for (int i = 0, max = registrations_proto.registration_size(); i < max; | 386 for (int i = 0, max = registrations_proto.registration_size(); i < max; |
| 391 ++i) { | 387 ++i) { |
| 392 const BackgroundSyncRegistrationProto& registration_proto = | 388 const BackgroundSyncRegistrationProto& registration_proto = |
| 393 registrations_proto.registration(i); | 389 registrations_proto.registration(i); |
| 394 | 390 |
| 395 if (registration_proto.id() >= registrations->next_id) { | 391 if (registration_proto.id() >= registrations->next_id) { |
| 396 corruption_detected = true; | 392 corruption_detected = true; |
| 397 break; | 393 break; |
| 398 } | 394 } |
| 399 | 395 |
| 400 RegistrationKey registration_key(registration_proto.tag(), | 396 RegistrationKey registration_key(registration_proto.tag()); |
| 401 registration_proto.periodicity()); | |
| 402 | 397 |
| 403 scoped_refptr<RefCountedRegistration> ref_registration( | 398 scoped_refptr<RefCountedRegistration> ref_registration( |
| 404 new RefCountedRegistration()); | 399 new RefCountedRegistration()); |
| 405 registrations->registration_map[registration_key] = ref_registration; | 400 registrations->registration_map[registration_key] = ref_registration; |
| 406 BackgroundSyncRegistration* registration = ref_registration->value(); | 401 BackgroundSyncRegistration* registration = ref_registration->value(); |
| 407 | 402 |
| 408 BackgroundSyncRegistrationOptions* options = registration->options(); | 403 BackgroundSyncRegistrationOptions* options = registration->options(); |
| 409 options->tag = registration_proto.tag(); | 404 options->tag = registration_proto.tag(); |
| 410 options->periodicity = registration_proto.periodicity(); | |
| 411 options->min_period = registration_proto.min_period(); | |
| 412 options->network_state = registration_proto.network_state(); | 405 options->network_state = registration_proto.network_state(); |
| 413 | 406 |
| 414 registration->set_id(registration_proto.id()); | 407 registration->set_id(registration_proto.id()); |
| 415 registration->set_num_attempts(registration_proto.num_attempts()); | 408 registration->set_num_attempts(registration_proto.num_attempts()); |
| 416 registration->set_delay_until( | 409 registration->set_delay_until( |
| 417 base::Time::FromInternalValue(registration_proto.delay_until())); | 410 base::Time::FromInternalValue(registration_proto.delay_until())); |
| 418 } | 411 } |
| 419 } | 412 } |
| 420 | 413 |
| 421 if (corruption_detected) | 414 if (corruption_detected) |
| (...skipping 15 matching lines...) Expand all Loading... |
| 437 void BackgroundSyncManager::RegisterCheckIfHasMainFrame( | 430 void BackgroundSyncManager::RegisterCheckIfHasMainFrame( |
| 438 int64_t sw_registration_id, | 431 int64_t sw_registration_id, |
| 439 const BackgroundSyncRegistrationOptions& options, | 432 const BackgroundSyncRegistrationOptions& options, |
| 440 const StatusAndRegistrationCallback& callback) { | 433 const StatusAndRegistrationCallback& callback) { |
| 441 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 434 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 442 | 435 |
| 443 ServiceWorkerRegistration* sw_registration = | 436 ServiceWorkerRegistration* sw_registration = |
| 444 service_worker_context_->GetLiveRegistration(sw_registration_id); | 437 service_worker_context_->GetLiveRegistration(sw_registration_id); |
| 445 if (!sw_registration || !sw_registration->active_version()) { | 438 if (!sw_registration || !sw_registration->active_version()) { |
| 446 BackgroundSyncMetrics::CountRegisterFailure( | 439 BackgroundSyncMetrics::CountRegisterFailure( |
| 447 options.periodicity, BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER); | 440 BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER); |
| 448 PostErrorResponse(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, callback); | 441 PostErrorResponse(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, callback); |
| 449 return; | 442 return; |
| 450 } | 443 } |
| 451 | 444 |
| 452 HasMainFrameProviderHost( | 445 HasMainFrameProviderHost( |
| 453 sw_registration->pattern().GetOrigin(), | 446 sw_registration->pattern().GetOrigin(), |
| 454 base::Bind(&BackgroundSyncManager::RegisterDidCheckIfMainFrame, | 447 base::Bind(&BackgroundSyncManager::RegisterDidCheckIfMainFrame, |
| 455 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options, | 448 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options, |
| 456 callback)); | 449 callback)); |
| 457 } | 450 } |
| 458 | 451 |
| 459 void BackgroundSyncManager::RegisterDidCheckIfMainFrame( | 452 void BackgroundSyncManager::RegisterDidCheckIfMainFrame( |
| 460 int64_t sw_registration_id, | 453 int64_t sw_registration_id, |
| 461 const BackgroundSyncRegistrationOptions& options, | 454 const BackgroundSyncRegistrationOptions& options, |
| 462 const StatusAndRegistrationCallback& callback, | 455 const StatusAndRegistrationCallback& callback, |
| 463 bool has_main_frame_client) { | 456 bool has_main_frame_client) { |
| 464 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 457 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 465 | 458 |
| 466 if (!has_main_frame_client) { | 459 if (!has_main_frame_client) { |
| 467 BackgroundSyncMetrics::CountRegisterFailure( | 460 BackgroundSyncMetrics::CountRegisterFailure( |
| 468 options.periodicity, BACKGROUND_SYNC_STATUS_NOT_ALLOWED); | 461 BACKGROUND_SYNC_STATUS_NOT_ALLOWED); |
| 469 PostErrorResponse(BACKGROUND_SYNC_STATUS_NOT_ALLOWED, callback); | 462 PostErrorResponse(BACKGROUND_SYNC_STATUS_NOT_ALLOWED, callback); |
| 470 return; | 463 return; |
| 471 } | 464 } |
| 472 RegisterImpl(sw_registration_id, options, callback); | 465 RegisterImpl(sw_registration_id, options, callback); |
| 473 } | 466 } |
| 474 | 467 |
| 475 void BackgroundSyncManager::RegisterImpl( | 468 void BackgroundSyncManager::RegisterImpl( |
| 476 int64_t sw_registration_id, | 469 int64_t sw_registration_id, |
| 477 const BackgroundSyncRegistrationOptions& options, | 470 const BackgroundSyncRegistrationOptions& options, |
| 478 const StatusAndRegistrationCallback& callback) { | 471 const StatusAndRegistrationCallback& callback) { |
| 479 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 472 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 480 | 473 |
| 481 if (disabled_) { | 474 if (disabled_) { |
| 482 BackgroundSyncMetrics::CountRegisterFailure( | 475 BackgroundSyncMetrics::CountRegisterFailure( |
| 483 options.periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 476 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
| 484 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); | 477 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); |
| 485 return; | 478 return; |
| 486 } | 479 } |
| 487 | 480 |
| 488 if (options.tag.length() > kMaxTagLength) { | 481 if (options.tag.length() > kMaxTagLength) { |
| 489 BackgroundSyncMetrics::CountRegisterFailure( | 482 BackgroundSyncMetrics::CountRegisterFailure( |
| 490 options.periodicity, BACKGROUND_SYNC_STATUS_NOT_ALLOWED); | 483 BACKGROUND_SYNC_STATUS_NOT_ALLOWED); |
| 491 PostErrorResponse(BACKGROUND_SYNC_STATUS_NOT_ALLOWED, callback); | 484 PostErrorResponse(BACKGROUND_SYNC_STATUS_NOT_ALLOWED, callback); |
| 492 return; | 485 return; |
| 493 } | 486 } |
| 494 | 487 |
| 495 ServiceWorkerRegistration* sw_registration = | 488 ServiceWorkerRegistration* sw_registration = |
| 496 service_worker_context_->GetLiveRegistration(sw_registration_id); | 489 service_worker_context_->GetLiveRegistration(sw_registration_id); |
| 497 if (!sw_registration || !sw_registration->active_version()) { | 490 if (!sw_registration || !sw_registration->active_version()) { |
| 498 BackgroundSyncMetrics::CountRegisterFailure( | 491 BackgroundSyncMetrics::CountRegisterFailure( |
| 499 options.periodicity, BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER); | 492 BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER); |
| 500 PostErrorResponse(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, callback); | 493 PostErrorResponse(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, callback); |
| 501 return; | 494 return; |
| 502 } | 495 } |
| 503 | 496 |
| 504 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 497 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 505 base::Bind(&NotifyBackgroundSyncRegisteredOnUIThread, | 498 base::Bind(&NotifyBackgroundSyncRegisteredOnUIThread, |
| 506 service_worker_context_, | 499 service_worker_context_, |
| 507 sw_registration->pattern().GetOrigin())); | 500 sw_registration->pattern().GetOrigin())); |
| 508 | 501 |
| 509 RefCountedRegistration* existing_registration_ref = | 502 RefCountedRegistration* existing_registration_ref = |
| 510 LookupActiveRegistration(sw_registration_id, RegistrationKey(options)); | 503 LookupActiveRegistration(sw_registration_id, RegistrationKey(options)); |
| 511 if (existing_registration_ref) { | 504 if (existing_registration_ref) { |
| 512 if (existing_registration_ref->value()->options()->Equals(options)) { | 505 if (existing_registration_ref->value()->options()->Equals(options)) { |
| 513 BackgroundSyncRegistration* existing_registration = | 506 BackgroundSyncRegistration* existing_registration = |
| 514 existing_registration_ref->value(); | 507 existing_registration_ref->value(); |
| 515 | 508 |
| 516 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = | 509 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = |
| 517 AreOptionConditionsMet(options) | 510 AreOptionConditionsMet(options) |
| 518 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE | 511 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE |
| 519 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; | 512 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; |
| 520 BackgroundSyncMetrics::CountRegisterSuccess( | 513 BackgroundSyncMetrics::CountRegisterSuccess( |
| 521 existing_registration->options()->periodicity, | |
| 522 registration_could_fire, | 514 registration_could_fire, |
| 523 BackgroundSyncMetrics::REGISTRATION_IS_DUPLICATE); | 515 BackgroundSyncMetrics::REGISTRATION_IS_DUPLICATE); |
| 524 | 516 |
| 525 if (existing_registration->IsFiring()) { | 517 if (existing_registration->IsFiring()) { |
| 526 existing_registration->set_sync_state( | 518 existing_registration->set_sync_state( |
| 527 BackgroundSyncState::REREGISTERED_WHILE_FIRING); | 519 BackgroundSyncState::REREGISTERED_WHILE_FIRING); |
| 528 } | 520 } |
| 529 | 521 |
| 530 base::ThreadTaskRunnerHandle::Get()->PostTask( | 522 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 531 FROM_HERE, | 523 FROM_HERE, |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 659 registrations_proto.set_next_registration_id(registrations.next_id); | 651 registrations_proto.set_next_registration_id(registrations.next_id); |
| 660 registrations_proto.set_origin(registrations.origin.spec()); | 652 registrations_proto.set_origin(registrations.origin.spec()); |
| 661 | 653 |
| 662 for (const auto& key_and_registration : registrations.registration_map) { | 654 for (const auto& key_and_registration : registrations.registration_map) { |
| 663 const BackgroundSyncRegistration& registration = | 655 const BackgroundSyncRegistration& registration = |
| 664 *key_and_registration.second->value(); | 656 *key_and_registration.second->value(); |
| 665 BackgroundSyncRegistrationProto* registration_proto = | 657 BackgroundSyncRegistrationProto* registration_proto = |
| 666 registrations_proto.add_registration(); | 658 registrations_proto.add_registration(); |
| 667 registration_proto->set_id(registration.id()); | 659 registration_proto->set_id(registration.id()); |
| 668 registration_proto->set_tag(registration.options()->tag); | 660 registration_proto->set_tag(registration.options()->tag); |
| 669 registration_proto->set_periodicity(registration.options()->periodicity); | |
| 670 registration_proto->set_min_period(registration.options()->min_period); | |
| 671 registration_proto->set_network_state( | 661 registration_proto->set_network_state( |
| 672 registration.options()->network_state); | 662 registration.options()->network_state); |
| 673 registration_proto->set_num_attempts(registration.num_attempts()); | 663 registration_proto->set_num_attempts(registration.num_attempts()); |
| 674 registration_proto->set_delay_until( | 664 registration_proto->set_delay_until( |
| 675 registration.delay_until().ToInternalValue()); | 665 registration.delay_until().ToInternalValue()); |
| 676 } | 666 } |
| 677 std::string serialized; | 667 std::string serialized; |
| 678 bool success = registrations_proto.SerializeToString(&serialized); | 668 bool success = registrations_proto.SerializeToString(&serialized); |
| 679 DCHECK(success); | 669 DCHECK(success); |
| 680 | 670 |
| 681 StoreDataInBackend(sw_registration_id, registrations.origin, | 671 StoreDataInBackend(sw_registration_id, registrations.origin, |
| 682 kBackgroundSyncUserDataKey, serialized, callback); | 672 kBackgroundSyncUserDataKey, serialized, callback); |
| 683 } | 673 } |
| 684 | 674 |
| 685 void BackgroundSyncManager::RegisterDidStore( | 675 void BackgroundSyncManager::RegisterDidStore( |
| 686 int64_t sw_registration_id, | 676 int64_t sw_registration_id, |
| 687 const scoped_refptr<RefCountedRegistration>& new_registration_ref, | 677 const scoped_refptr<RefCountedRegistration>& new_registration_ref, |
| 688 const StatusAndRegistrationCallback& callback, | 678 const StatusAndRegistrationCallback& callback, |
| 689 ServiceWorkerStatusCode status) { | 679 ServiceWorkerStatusCode status) { |
| 690 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 680 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 691 | 681 |
| 692 const BackgroundSyncRegistration* new_registration = | 682 const BackgroundSyncRegistration* new_registration = |
| 693 new_registration_ref->value(); | 683 new_registration_ref->value(); |
| 694 | 684 |
| 695 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { | 685 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
| 696 // The service worker registration is gone. | 686 // The service worker registration is gone. |
| 697 BackgroundSyncMetrics::CountRegisterFailure( | 687 BackgroundSyncMetrics::CountRegisterFailure( |
| 698 new_registration->options()->periodicity, | |
| 699 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 688 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
| 700 active_registrations_.erase(sw_registration_id); | 689 active_registrations_.erase(sw_registration_id); |
| 701 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); | 690 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); |
| 702 return; | 691 return; |
| 703 } | 692 } |
| 704 | 693 |
| 705 if (status != SERVICE_WORKER_OK) { | 694 if (status != SERVICE_WORKER_OK) { |
| 706 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " | 695 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " |
| 707 "failure."; | 696 "failure."; |
| 708 BackgroundSyncMetrics::CountRegisterFailure( | 697 BackgroundSyncMetrics::CountRegisterFailure( |
| 709 new_registration->options()->periodicity, | |
| 710 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 698 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
| 711 DisableAndClearManager(base::Bind( | 699 DisableAndClearManager(base::Bind( |
| 712 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | 700 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
| 713 base::Passed(scoped_ptr<BackgroundSyncRegistrationHandle>()))); | 701 base::Passed(scoped_ptr<BackgroundSyncRegistrationHandle>()))); |
| 714 return; | 702 return; |
| 715 } | 703 } |
| 716 | 704 |
| 717 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = | 705 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = |
| 718 AreOptionConditionsMet(*new_registration->options()) | 706 AreOptionConditionsMet(*new_registration->options()) |
| 719 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE | 707 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE |
| 720 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; | 708 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; |
| 721 BackgroundSyncMetrics::CountRegisterSuccess( | 709 BackgroundSyncMetrics::CountRegisterSuccess( |
| 722 new_registration->options()->periodicity, registration_could_fire, | 710 registration_could_fire, |
| 723 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE); | 711 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE); |
| 724 | 712 |
| 725 FireReadyEvents(); | 713 FireReadyEvents(); |
| 726 base::ThreadTaskRunnerHandle::Get()->PostTask( | 714 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 727 FROM_HERE, | 715 FROM_HERE, |
| 728 base::Bind( | 716 base::Bind( |
| 729 callback, BACKGROUND_SYNC_STATUS_OK, | 717 callback, BACKGROUND_SYNC_STATUS_OK, |
| 730 base::Passed(CreateRegistrationHandle(new_registration_ref.get())))); | 718 base::Passed(CreateRegistrationHandle(new_registration_ref.get())))); |
| 731 } | 719 } |
| 732 | 720 |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 863 BackgroundSyncRegistrationHandle::HandleId handle_id, | 851 BackgroundSyncRegistrationHandle::HandleId handle_id, |
| 864 const StatusCallback& callback) { | 852 const StatusCallback& callback) { |
| 865 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 853 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 866 | 854 |
| 867 BackgroundSyncRegistration* registration = | 855 BackgroundSyncRegistration* registration = |
| 868 GetRegistrationForHandle(handle_id); | 856 GetRegistrationForHandle(handle_id); |
| 869 DCHECK(registration); | 857 DCHECK(registration); |
| 870 | 858 |
| 871 if (disabled_) { | 859 if (disabled_) { |
| 872 BackgroundSyncMetrics::CountUnregister( | 860 BackgroundSyncMetrics::CountUnregister( |
| 873 registration->options()->periodicity, | |
| 874 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 861 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
| 875 base::ThreadTaskRunnerHandle::Get()->PostTask( | 862 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 876 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); | 863 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); |
| 877 return; | 864 return; |
| 878 } | 865 } |
| 879 | 866 |
| 880 op_scheduler_.ScheduleOperation(base::Bind( | 867 op_scheduler_.ScheduleOperation(base::Bind( |
| 881 &BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(), | 868 &BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(), |
| 882 sw_registration_id, RegistrationKey(*registration), registration->id(), | 869 sw_registration_id, RegistrationKey(*registration), registration->id(), |
| 883 registration->options()->periodicity, MakeStatusCompletion(callback))); | 870 MakeStatusCompletion(callback))); |
| 884 } | 871 } |
| 885 | 872 |
| 886 void BackgroundSyncManager::UnregisterImpl( | 873 void BackgroundSyncManager::UnregisterImpl( |
| 887 int64_t sw_registration_id, | 874 int64_t sw_registration_id, |
| 888 const RegistrationKey& registration_key, | 875 const RegistrationKey& registration_key, |
| 889 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 876 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| 890 SyncPeriodicity periodicity, | |
| 891 const StatusCallback& callback) { | 877 const StatusCallback& callback) { |
| 892 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 878 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 893 | 879 |
| 894 if (disabled_) { | 880 if (disabled_) { |
| 895 BackgroundSyncMetrics::CountUnregister( | 881 BackgroundSyncMetrics::CountUnregister( |
| 896 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 882 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
| 897 base::ThreadTaskRunnerHandle::Get()->PostTask( | 883 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 898 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); | 884 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); |
| 899 return; | 885 return; |
| 900 } | 886 } |
| 901 | 887 |
| 902 RefCountedRegistration* existing_registration = | 888 RefCountedRegistration* existing_registration = |
| 903 LookupActiveRegistration(sw_registration_id, registration_key); | 889 LookupActiveRegistration(sw_registration_id, registration_key); |
| 904 | 890 |
| 905 if (!existing_registration || | 891 if (!existing_registration || |
| 906 existing_registration->value()->id() != sync_registration_id) { | 892 existing_registration->value()->id() != sync_registration_id) { |
| 907 BackgroundSyncMetrics::CountUnregister(periodicity, | 893 BackgroundSyncMetrics::CountUnregister(BACKGROUND_SYNC_STATUS_NOT_FOUND); |
| 908 BACKGROUND_SYNC_STATUS_NOT_FOUND); | |
| 909 base::ThreadTaskRunnerHandle::Get()->PostTask( | 894 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 910 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_NOT_FOUND)); | 895 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_NOT_FOUND)); |
| 911 return; | 896 return; |
| 912 } | 897 } |
| 913 | 898 |
| 914 existing_registration->value()->SetUnregisteredState(); | 899 existing_registration->value()->SetUnregisteredState(); |
| 915 | 900 |
| 916 RemoveActiveRegistration(sw_registration_id, registration_key); | 901 RemoveActiveRegistration(sw_registration_id, registration_key); |
| 917 | 902 |
| 918 StoreRegistrations(sw_registration_id, | 903 StoreRegistrations( |
| 919 base::Bind(&BackgroundSyncManager::UnregisterDidStore, | 904 sw_registration_id, |
| 920 weak_ptr_factory_.GetWeakPtr(), | 905 base::Bind(&BackgroundSyncManager::UnregisterDidStore, |
| 921 sw_registration_id, periodicity, callback)); | 906 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, callback)); |
| 922 } | 907 } |
| 923 | 908 |
| 924 void BackgroundSyncManager::UnregisterDidStore(int64_t sw_registration_id, | 909 void BackgroundSyncManager::UnregisterDidStore(int64_t sw_registration_id, |
| 925 SyncPeriodicity periodicity, | |
| 926 const StatusCallback& callback, | 910 const StatusCallback& callback, |
| 927 ServiceWorkerStatusCode status) { | 911 ServiceWorkerStatusCode status) { |
| 928 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 912 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 929 | 913 |
| 930 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { | 914 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
| 931 // ServiceWorker was unregistered. | 915 // ServiceWorker was unregistered. |
| 932 BackgroundSyncMetrics::CountUnregister( | 916 BackgroundSyncMetrics::CountUnregister( |
| 933 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 917 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
| 934 active_registrations_.erase(sw_registration_id); | 918 active_registrations_.erase(sw_registration_id); |
| 935 base::ThreadTaskRunnerHandle::Get()->PostTask( | 919 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 936 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); | 920 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); |
| 937 return; | 921 return; |
| 938 } | 922 } |
| 939 | 923 |
| 940 if (status != SERVICE_WORKER_OK) { | 924 if (status != SERVICE_WORKER_OK) { |
| 941 LOG(ERROR) << "BackgroundSync failed to unregister due to backend failure."; | 925 LOG(ERROR) << "BackgroundSync failed to unregister due to backend failure."; |
| 942 BackgroundSyncMetrics::CountUnregister( | 926 BackgroundSyncMetrics::CountUnregister( |
| 943 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 927 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
| 944 DisableAndClearManager( | 928 DisableAndClearManager( |
| 945 base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); | 929 base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); |
| 946 return; | 930 return; |
| 947 } | 931 } |
| 948 | 932 |
| 949 BackgroundSyncMetrics::CountUnregister(periodicity, | 933 BackgroundSyncMetrics::CountUnregister(BACKGROUND_SYNC_STATUS_OK); |
| 950 BACKGROUND_SYNC_STATUS_OK); | |
| 951 base::ThreadTaskRunnerHandle::Get()->PostTask( | 934 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 952 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK)); | 935 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK)); |
| 953 } | 936 } |
| 954 | 937 |
| 955 void BackgroundSyncManager::NotifyWhenFinished( | 938 void BackgroundSyncManager::NotifyWhenFinished( |
| 956 BackgroundSyncRegistrationHandle::HandleId handle_id, | 939 BackgroundSyncRegistrationHandle::HandleId handle_id, |
| 957 const StatusAndStateCallback& callback) { | 940 const StatusAndStateCallback& callback) { |
| 958 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 941 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 959 | 942 |
| 960 if (disabled_) { | 943 if (disabled_) { |
| 961 base::ThreadTaskRunnerHandle::Get()->PostTask( | 944 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 962 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | 945 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
| 963 BackgroundSyncState::FAILED)); | 946 BackgroundSyncState::FAILED)); |
| 964 return; | 947 return; |
| 965 } | 948 } |
| 966 | 949 |
| 967 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle = | 950 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle = |
| 968 DuplicateRegistrationHandle(handle_id); | 951 DuplicateRegistrationHandle(handle_id); |
| 969 | 952 |
| 970 op_scheduler_.ScheduleOperation( | 953 op_scheduler_.ScheduleOperation( |
| 971 base::Bind(&BackgroundSyncManager::NotifyWhenFinishedImpl, | 954 base::Bind(&BackgroundSyncManager::NotifyWhenFinishedImpl, |
| 972 weak_ptr_factory_.GetWeakPtr(), | 955 weak_ptr_factory_.GetWeakPtr(), |
| 973 base::Passed(std::move(registration_handle)), callback)); | 956 base::Passed(std::move(registration_handle)), callback)); |
| 974 } | 957 } |
| 975 | 958 |
| 976 void BackgroundSyncManager::NotifyWhenFinishedImpl( | 959 void BackgroundSyncManager::NotifyWhenFinishedImpl( |
| 977 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle, | 960 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle, |
| 978 const StatusAndStateCallback& callback) { | 961 const StatusAndStateCallback& callback) { |
| 979 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 962 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 980 DCHECK_EQ(SYNC_ONE_SHOT, registration_handle->options()->periodicity); | |
| 981 | 963 |
| 982 if (disabled_) { | 964 if (disabled_) { |
| 983 base::ThreadTaskRunnerHandle::Get()->PostTask( | 965 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 984 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | 966 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
| 985 BackgroundSyncState::FAILED)); | 967 BackgroundSyncState::FAILED)); |
| 986 return; | 968 return; |
| 987 } | 969 } |
| 988 | 970 |
| 989 if (!registration_handle->registration()->HasCompleted()) { | 971 if (!registration_handle->registration()->HasCompleted()) { |
| 990 registration_handle->registration()->AddFinishedCallback( | 972 registration_handle->registration()->AddFinishedCallback( |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1027 } | 1009 } |
| 1028 | 1010 |
| 1029 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1011 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 1030 FROM_HERE, | 1012 FROM_HERE, |
| 1031 base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, | 1013 base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, |
| 1032 base::Passed(CreateRegistrationHandle(registration)))); | 1014 base::Passed(CreateRegistrationHandle(registration)))); |
| 1033 } | 1015 } |
| 1034 | 1016 |
| 1035 void BackgroundSyncManager::GetRegistrationsImpl( | 1017 void BackgroundSyncManager::GetRegistrationsImpl( |
| 1036 int64_t sw_registration_id, | 1018 int64_t sw_registration_id, |
| 1037 SyncPeriodicity periodicity, | |
| 1038 const StatusAndRegistrationsCallback& callback) { | 1019 const StatusAndRegistrationsCallback& callback) { |
| 1039 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1020 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1040 | 1021 |
| 1041 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>> out_registrations( | 1022 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>> out_registrations( |
| 1042 new ScopedVector<BackgroundSyncRegistrationHandle>()); | 1023 new ScopedVector<BackgroundSyncRegistrationHandle>()); |
| 1043 | 1024 |
| 1044 if (disabled_) { | 1025 if (disabled_) { |
| 1045 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1026 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 1046 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | 1027 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
| 1047 base::Passed(std::move(out_registrations)))); | 1028 base::Passed(std::move(out_registrations)))); |
| 1048 return; | 1029 return; |
| 1049 } | 1030 } |
| 1050 | 1031 |
| 1051 SWIdToRegistrationsMap::iterator it = | 1032 SWIdToRegistrationsMap::iterator it = |
| 1052 active_registrations_.find(sw_registration_id); | 1033 active_registrations_.find(sw_registration_id); |
| 1053 | 1034 |
| 1054 if (it != active_registrations_.end()) { | 1035 if (it != active_registrations_.end()) { |
| 1055 const BackgroundSyncRegistrations& registrations = it->second; | 1036 const BackgroundSyncRegistrations& registrations = it->second; |
| 1056 for (const auto& tag_and_registration : registrations.registration_map) { | 1037 for (const auto& tag_and_registration : registrations.registration_map) { |
| 1057 RefCountedRegistration* registration = tag_and_registration.second.get(); | 1038 RefCountedRegistration* registration = tag_and_registration.second.get(); |
| 1058 if (registration->value()->options()->periodicity == periodicity) { | 1039 out_registrations->push_back( |
| 1059 out_registrations->push_back( | 1040 CreateRegistrationHandle(registration).release()); |
| 1060 CreateRegistrationHandle(registration).release()); | |
| 1061 } | |
| 1062 } | 1041 } |
| 1063 } | 1042 } |
| 1064 | 1043 |
| 1065 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1044 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 1066 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, | 1045 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, |
| 1067 base::Passed(std::move(out_registrations)))); | 1046 base::Passed(std::move(out_registrations)))); |
| 1068 } | 1047 } |
| 1069 | 1048 |
| 1070 bool BackgroundSyncManager::AreOptionConditionsMet( | 1049 bool BackgroundSyncManager::AreOptionConditionsMet( |
| 1071 const BackgroundSyncRegistrationOptions& options) { | 1050 const BackgroundSyncRegistrationOptions& options) { |
| 1072 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1051 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1073 return network_observer_->NetworkSufficient(options.network_state); | 1052 return network_observer_->NetworkSufficient(options.network_state); |
| 1074 } | 1053 } |
| 1075 | 1054 |
| 1076 bool BackgroundSyncManager::IsRegistrationReadyToFire( | 1055 bool BackgroundSyncManager::IsRegistrationReadyToFire( |
| 1077 const BackgroundSyncRegistration& registration) { | 1056 const BackgroundSyncRegistration& registration) { |
| 1078 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1057 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1079 | 1058 |
| 1080 // TODO(jkarlin): Add support for firing periodic registrations. | |
| 1081 if (registration.options()->periodicity == SYNC_PERIODIC) | |
| 1082 return false; | |
| 1083 | |
| 1084 if (registration.sync_state() != BackgroundSyncState::PENDING) | 1059 if (registration.sync_state() != BackgroundSyncState::PENDING) |
| 1085 return false; | 1060 return false; |
| 1086 | 1061 |
| 1087 if (clock_->Now() < registration.delay_until()) | 1062 if (clock_->Now() < registration.delay_until()) |
| 1088 return false; | 1063 return false; |
| 1089 | 1064 |
| 1090 DCHECK_EQ(SYNC_ONE_SHOT, registration.options()->periodicity); | |
| 1091 | |
| 1092 return AreOptionConditionsMet(*registration.options()); | 1065 return AreOptionConditionsMet(*registration.options()); |
| 1093 } | 1066 } |
| 1094 | 1067 |
| 1095 void BackgroundSyncManager::RunInBackgroundIfNecessary() { | 1068 void BackgroundSyncManager::RunInBackgroundIfNecessary() { |
| 1096 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1069 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1097 base::TimeDelta soonest_wakeup_delta = base::TimeDelta::Max(); | 1070 base::TimeDelta soonest_wakeup_delta = base::TimeDelta::Max(); |
| 1098 | 1071 |
| 1099 for (const auto& sw_id_and_registrations : active_registrations_) { | 1072 for (const auto& sw_id_and_registrations : active_registrations_) { |
| 1100 for (const auto& key_and_registration : | 1073 for (const auto& key_and_registration : |
| 1101 sw_id_and_registrations.second.registration_map) { | 1074 sw_id_and_registrations.second.registration_map) { |
| 1102 const BackgroundSyncRegistration& registration = | 1075 const BackgroundSyncRegistration& registration = |
| 1103 *key_and_registration.second->value(); | 1076 *key_and_registration.second->value(); |
| 1104 if (registration.sync_state() == BackgroundSyncState::PENDING) { | 1077 if (registration.sync_state() == BackgroundSyncState::PENDING) { |
| 1105 if (registration.options()->periodicity == SYNC_ONE_SHOT) { | 1078 if (clock_->Now() >= registration.delay_until()) { |
| 1106 if (clock_->Now() >= registration.delay_until()) { | 1079 soonest_wakeup_delta = base::TimeDelta(); |
| 1107 soonest_wakeup_delta = base::TimeDelta(); | |
| 1108 } else { | |
| 1109 base::TimeDelta delay_delta = | |
| 1110 registration.delay_until() - clock_->Now(); | |
| 1111 if (delay_delta < soonest_wakeup_delta) | |
| 1112 soonest_wakeup_delta = delay_delta; | |
| 1113 } | |
| 1114 } else { | 1080 } else { |
| 1115 // TODO(jkarlin): Support keeping the browser alive for periodic | 1081 base::TimeDelta delay_delta = |
| 1116 // syncs. | 1082 registration.delay_until() - clock_->Now(); |
| 1083 if (delay_delta < soonest_wakeup_delta) |
| 1084 soonest_wakeup_delta = delay_delta; |
| 1117 } | 1085 } |
| 1118 } | 1086 } |
| 1119 } | 1087 } |
| 1120 } | 1088 } |
| 1121 | 1089 |
| 1122 // If the browser is closed while firing events, the browser needs a task to | 1090 // If the browser is closed while firing events, the browser needs a task to |
| 1123 // wake it back up and try again. | 1091 // wake it back up and try again. |
| 1124 if (num_firing_registrations_ > 0 && | 1092 if (num_firing_registrations_ > 0 && |
| 1125 soonest_wakeup_delta > parameters_->min_sync_recovery_time) { | 1093 soonest_wakeup_delta > parameters_->min_sync_recovery_time) { |
| 1126 soonest_wakeup_delta = parameters_->min_sync_recovery_time; | 1094 soonest_wakeup_delta = parameters_->min_sync_recovery_time; |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1253 registration_handle->handle_id(); | 1221 registration_handle->handle_id(); |
| 1254 | 1222 |
| 1255 BackgroundSyncEventLastChance last_chance = | 1223 BackgroundSyncEventLastChance last_chance = |
| 1256 registration->value()->num_attempts() == | 1224 registration->value()->num_attempts() == |
| 1257 parameters_->max_sync_attempts - 1 | 1225 parameters_->max_sync_attempts - 1 |
| 1258 ? BackgroundSyncEventLastChance::IS_LAST_CHANCE | 1226 ? BackgroundSyncEventLastChance::IS_LAST_CHANCE |
| 1259 : BackgroundSyncEventLastChance::IS_NOT_LAST_CHANCE; | 1227 : BackgroundSyncEventLastChance::IS_NOT_LAST_CHANCE; |
| 1260 | 1228 |
| 1261 HasMainFrameProviderHost( | 1229 HasMainFrameProviderHost( |
| 1262 service_worker_registration->pattern().GetOrigin(), | 1230 service_worker_registration->pattern().GetOrigin(), |
| 1263 base::Bind(&BackgroundSyncMetrics::RecordEventStarted, | 1231 base::Bind(&BackgroundSyncMetrics::RecordEventStarted)); |
| 1264 registration->value()->options()->periodicity)); | |
| 1265 | 1232 |
| 1266 FireOneShotSync( | 1233 FireOneShotSync( |
| 1267 handle_id, service_worker_registration->active_version(), last_chance, | 1234 handle_id, service_worker_registration->active_version(), last_chance, |
| 1268 base::Bind(&BackgroundSyncManager::EventComplete, | 1235 base::Bind(&BackgroundSyncManager::EventComplete, |
| 1269 weak_ptr_factory_.GetWeakPtr(), service_worker_registration, | 1236 weak_ptr_factory_.GetWeakPtr(), service_worker_registration, |
| 1270 service_worker_registration->id(), | 1237 service_worker_registration->id(), |
| 1271 base::Passed(std::move(registration_handle)), | 1238 base::Passed(std::move(registration_handle)), |
| 1272 event_completed_callback)); | 1239 event_completed_callback)); |
| 1273 | 1240 |
| 1274 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1241 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1319 num_firing_registrations_ -= 1; | 1286 num_firing_registrations_ -= 1; |
| 1320 | 1287 |
| 1321 // The event ran to completion, we should count it, no matter what happens | 1288 // The event ran to completion, we should count it, no matter what happens |
| 1322 // from here. | 1289 // from here. |
| 1323 ServiceWorkerRegistration* sw_registration = | 1290 ServiceWorkerRegistration* sw_registration = |
| 1324 service_worker_context_->GetLiveRegistration(service_worker_id); | 1291 service_worker_context_->GetLiveRegistration(service_worker_id); |
| 1325 if (sw_registration) { | 1292 if (sw_registration) { |
| 1326 HasMainFrameProviderHost( | 1293 HasMainFrameProviderHost( |
| 1327 sw_registration->pattern().GetOrigin(), | 1294 sw_registration->pattern().GetOrigin(), |
| 1328 base::Bind(&BackgroundSyncMetrics::RecordEventResult, | 1295 base::Bind(&BackgroundSyncMetrics::RecordEventResult, |
| 1329 registration->options()->periodicity, | |
| 1330 status_code == SERVICE_WORKER_OK)); | 1296 status_code == SERVICE_WORKER_OK)); |
| 1331 } | 1297 } |
| 1332 | 1298 |
| 1333 if (registration->options()->periodicity == SYNC_ONE_SHOT) { | 1299 if (registration->sync_state() == |
| 1300 BackgroundSyncState::REREGISTERED_WHILE_FIRING) { |
| 1301 registration->set_sync_state(BackgroundSyncState::PENDING); |
| 1302 registration->set_num_attempts(0); |
| 1303 } else if (status_code != SERVICE_WORKER_OK) { // Sync failed |
| 1304 bool can_retry = |
| 1305 registration->num_attempts() < parameters_->max_sync_attempts; |
| 1334 if (registration->sync_state() == | 1306 if (registration->sync_state() == |
| 1335 BackgroundSyncState::REREGISTERED_WHILE_FIRING) { | 1307 BackgroundSyncState::UNREGISTERED_WHILE_FIRING) { |
| 1308 registration->set_sync_state(can_retry ? BackgroundSyncState::UNREGISTERED |
| 1309 : BackgroundSyncState::FAILED); |
| 1310 registration->RunFinishedCallbacks(); |
| 1311 } else if (can_retry) { |
| 1336 registration->set_sync_state(BackgroundSyncState::PENDING); | 1312 registration->set_sync_state(BackgroundSyncState::PENDING); |
| 1337 registration->set_num_attempts(0); | 1313 registration->set_delay_until(clock_->Now() + |
| 1338 } else if (status_code != SERVICE_WORKER_OK) { // Sync failed | 1314 parameters_->initial_retry_delay * |
| 1339 bool can_retry = | 1315 pow(parameters_->retry_delay_factor, |
| 1340 registration->num_attempts() < parameters_->max_sync_attempts; | 1316 registration->num_attempts() - 1)); |
| 1341 if (registration->sync_state() == | 1317 } else { |
| 1342 BackgroundSyncState::UNREGISTERED_WHILE_FIRING) { | 1318 registration->set_sync_state(BackgroundSyncState::FAILED); |
| 1343 registration->set_sync_state(can_retry | |
| 1344 ? BackgroundSyncState::UNREGISTERED | |
| 1345 : BackgroundSyncState::FAILED); | |
| 1346 registration->RunFinishedCallbacks(); | |
| 1347 } else if (can_retry) { | |
| 1348 registration->set_sync_state(BackgroundSyncState::PENDING); | |
| 1349 registration->set_delay_until( | |
| 1350 clock_->Now() + | |
| 1351 parameters_->initial_retry_delay * | |
| 1352 pow(parameters_->retry_delay_factor, | |
| 1353 registration->num_attempts() - 1)); | |
| 1354 } else { | |
| 1355 registration->set_sync_state(BackgroundSyncState::FAILED); | |
| 1356 registration->RunFinishedCallbacks(); | |
| 1357 } | |
| 1358 } else { // Sync succeeded | |
| 1359 registration->set_sync_state(BackgroundSyncState::SUCCESS); | |
| 1360 registration->RunFinishedCallbacks(); | 1319 registration->RunFinishedCallbacks(); |
| 1361 } | 1320 } |
| 1321 } else { // Sync succeeded |
| 1322 registration->set_sync_state(BackgroundSyncState::SUCCESS); |
| 1323 registration->RunFinishedCallbacks(); |
| 1324 } |
| 1362 | 1325 |
| 1363 if (registration->HasCompleted()) { | 1326 if (registration->HasCompleted()) { |
| 1364 RegistrationKey key(*registration); | 1327 RegistrationKey key(*registration); |
| 1365 RefCountedRegistration* active_registration = | 1328 RefCountedRegistration* active_registration = |
| 1366 LookupActiveRegistration(service_worker_id, key); | 1329 LookupActiveRegistration(service_worker_id, key); |
| 1367 if (active_registration && | 1330 if (active_registration && |
| 1368 active_registration->value()->id() == registration->id()) { | 1331 active_registration->value()->id() == registration->id()) { |
| 1369 RemoveActiveRegistration(service_worker_id, key); | 1332 RemoveActiveRegistration(service_worker_id, key); |
| 1370 } | |
| 1371 } | 1333 } |
| 1372 } else { // !SYNC_ONE_SHOT | |
| 1373 // TODO(jkarlin): Add support for running periodic syncs. (crbug.com/479674) | |
| 1374 NOTREACHED(); | |
| 1375 } | 1334 } |
| 1376 | 1335 |
| 1377 if (disabled_) { | 1336 if (disabled_) { |
| 1378 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 1337 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 1379 base::Bind(callback)); | 1338 base::Bind(callback)); |
| 1380 return; | 1339 return; |
| 1381 } | 1340 } |
| 1382 | 1341 |
| 1383 StoreRegistrations( | 1342 StoreRegistrations( |
| 1384 service_worker_id, | 1343 service_worker_id, |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1528 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { | 1487 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { |
| 1529 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1488 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1530 | 1489 |
| 1531 return base::Bind( | 1490 return base::Bind( |
| 1532 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, | 1491 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, |
| 1533 BackgroundSyncStatus>, | 1492 BackgroundSyncStatus>, |
| 1534 weak_ptr_factory_.GetWeakPtr(), callback); | 1493 weak_ptr_factory_.GetWeakPtr(), callback); |
| 1535 } | 1494 } |
| 1536 | 1495 |
| 1537 } // namespace content | 1496 } // namespace content |
| OLD | NEW |