| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/background_sync/background_sync_manager.h" | 5 #include "content/browser/background_sync/background_sync_manager.h" |
| 6 | 6 |
| 7 #include "base/barrier_closure.h" | 7 #include "base/barrier_closure.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/metrics/field_trial.h" | 10 #include "base/metrics/field_trial.h" |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 : value_(periodicity == SYNC_ONE_SHOT ? "o_" + tag : "p_" + tag) { | 167 : value_(periodicity == SYNC_ONE_SHOT ? "o_" + tag : "p_" + tag) { |
| 168 } | 168 } |
| 169 | 169 |
| 170 void BackgroundSyncManager::Register( | 170 void BackgroundSyncManager::Register( |
| 171 int64 sw_registration_id, | 171 int64 sw_registration_id, |
| 172 const BackgroundSyncRegistrationOptions& options, | 172 const BackgroundSyncRegistrationOptions& options, |
| 173 bool requested_from_service_worker, | 173 bool requested_from_service_worker, |
| 174 const StatusAndRegistrationCallback& callback) { | 174 const StatusAndRegistrationCallback& callback) { |
| 175 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 175 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 176 | 176 |
| 177 // For UMA, determine here whether the sync could fire immediately | |
| 178 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = | |
| 179 AreOptionConditionsMet(options) | |
| 180 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE | |
| 181 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; | |
| 182 | |
| 183 if (disabled_) { | 177 if (disabled_) { |
| 184 BackgroundSyncMetrics::CountRegister( | 178 BackgroundSyncMetrics::CountRegisterFailure( |
| 185 options.periodicity, registration_could_fire, | 179 options.periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
| 186 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, | |
| 187 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | |
| 188 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); | 180 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); |
| 189 return; | 181 return; |
| 190 } | 182 } |
| 191 | 183 |
| 192 op_scheduler_.ScheduleOperation(base::Bind( | 184 op_scheduler_.ScheduleOperation(base::Bind( |
| 193 &BackgroundSyncManager::RegisterImpl, weak_ptr_factory_.GetWeakPtr(), | 185 &BackgroundSyncManager::RegisterImpl, weak_ptr_factory_.GetWeakPtr(), |
| 194 sw_registration_id, options, requested_from_service_worker, | 186 sw_registration_id, options, requested_from_service_worker, |
| 195 MakeStatusAndRegistrationCompletion(callback))); | 187 MakeStatusAndRegistrationCompletion(callback))); |
| 196 } | 188 } |
| 197 | 189 |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 base::Bind(callback)); | 392 base::Bind(callback)); |
| 401 } | 393 } |
| 402 | 394 |
| 403 void BackgroundSyncManager::RegisterImpl( | 395 void BackgroundSyncManager::RegisterImpl( |
| 404 int64 sw_registration_id, | 396 int64 sw_registration_id, |
| 405 const BackgroundSyncRegistrationOptions& options, | 397 const BackgroundSyncRegistrationOptions& options, |
| 406 bool requested_from_service_worker, | 398 bool requested_from_service_worker, |
| 407 const StatusAndRegistrationCallback& callback) { | 399 const StatusAndRegistrationCallback& callback) { |
| 408 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 400 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 409 | 401 |
| 410 // For UMA, determine here whether the sync could fire immediately | |
| 411 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = | |
| 412 AreOptionConditionsMet(options) | |
| 413 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE | |
| 414 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; | |
| 415 | |
| 416 if (disabled_) { | 402 if (disabled_) { |
| 417 BackgroundSyncMetrics::CountRegister( | 403 BackgroundSyncMetrics::CountRegisterFailure( |
| 418 options.periodicity, registration_could_fire, | 404 options.periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
| 419 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, | |
| 420 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | |
| 421 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); | 405 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); |
| 422 return; | 406 return; |
| 423 } | 407 } |
| 424 | 408 |
| 425 if (ShouldDisableForFieldTrial()) { | 409 if (ShouldDisableForFieldTrial()) { |
| 426 DisableAndClearManager(base::Bind( | 410 DisableAndClearManager(base::Bind( |
| 427 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | 411 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
| 428 base::Passed(scoped_ptr<BackgroundSyncRegistrationHandle>().Pass()))); | 412 base::Passed(scoped_ptr<BackgroundSyncRegistrationHandle>().Pass()))); |
| 429 return; | 413 return; |
| 430 } | 414 } |
| 431 | 415 |
| 432 if (options.tag.length() > kMaxTagLength) { | 416 if (options.tag.length() > kMaxTagLength) { |
| 433 BackgroundSyncMetrics::CountRegister( | 417 BackgroundSyncMetrics::CountRegisterFailure( |
| 434 options.periodicity, registration_could_fire, | 418 options.periodicity, BACKGROUND_SYNC_STATUS_NOT_ALLOWED); |
| 435 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, | |
| 436 BACKGROUND_SYNC_STATUS_NOT_ALLOWED); | |
| 437 PostErrorResponse(BACKGROUND_SYNC_STATUS_NOT_ALLOWED, callback); | 419 PostErrorResponse(BACKGROUND_SYNC_STATUS_NOT_ALLOWED, callback); |
| 438 return; | 420 return; |
| 439 } | 421 } |
| 440 | 422 |
| 441 ServiceWorkerRegistration* sw_registration = | 423 ServiceWorkerRegistration* sw_registration = |
| 442 service_worker_context_->GetLiveRegistration(sw_registration_id); | 424 service_worker_context_->GetLiveRegistration(sw_registration_id); |
| 443 if (!sw_registration || !sw_registration->active_version()) { | 425 if (!sw_registration || !sw_registration->active_version()) { |
| 444 BackgroundSyncMetrics::CountRegister( | 426 BackgroundSyncMetrics::CountRegisterFailure( |
| 445 options.periodicity, registration_could_fire, | 427 options.periodicity, BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER); |
| 446 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, | |
| 447 BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER); | |
| 448 PostErrorResponse(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, callback); | 428 PostErrorResponse(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, callback); |
| 449 return; | 429 return; |
| 450 } | 430 } |
| 451 | 431 |
| 452 if (requested_from_service_worker && | 432 if (requested_from_service_worker && |
| 453 !service_worker_context_->HasWindowProviderHost( | 433 !service_worker_context_->HasWindowProviderHost( |
| 454 sw_registration->pattern().GetOrigin())) { | 434 sw_registration->pattern().GetOrigin())) { |
| 455 PostErrorResponse(BACKGROUND_SYNC_STATUS_NOT_ALLOWED, callback); | 435 PostErrorResponse(BACKGROUND_SYNC_STATUS_NOT_ALLOWED, callback); |
| 456 return; | 436 return; |
| 457 } | 437 } |
| 458 | 438 |
| 459 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 439 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 460 base::Bind(&NotifyBackgroundSyncRegisteredOnUIThread, | 440 base::Bind(&NotifyBackgroundSyncRegisteredOnUIThread, |
| 461 service_worker_context_, | 441 service_worker_context_, |
| 462 sw_registration->pattern().GetOrigin())); | 442 sw_registration->pattern().GetOrigin())); |
| 463 | 443 |
| 464 RefCountedRegistration* existing_registration_ref = | 444 RefCountedRegistration* existing_registration_ref = |
| 465 LookupActiveRegistration(sw_registration_id, RegistrationKey(options)); | 445 LookupActiveRegistration(sw_registration_id, RegistrationKey(options)); |
| 466 if (existing_registration_ref) { | 446 if (existing_registration_ref) { |
| 467 if (existing_registration_ref->value()->options()->Equals(options)) { | 447 if (existing_registration_ref->value()->options()->Equals(options)) { |
| 468 BackgroundSyncRegistration* existing_registration = | 448 BackgroundSyncRegistration* existing_registration = |
| 469 existing_registration_ref->value(); | 449 existing_registration_ref->value(); |
| 470 | 450 |
| 471 // Record the duplicated registration | 451 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = |
| 472 BackgroundSyncMetrics::CountRegister( | 452 AreOptionConditionsMet(options) |
| 473 existing_registration->options()->periodicity, registration_could_fire, | 453 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE |
| 474 BackgroundSyncMetrics::REGISTRATION_IS_DUPLICATE, | 454 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; |
| 475 BACKGROUND_SYNC_STATUS_OK); | 455 BackgroundSyncMetrics::CountRegisterSuccess( |
| 456 existing_registration->options()->periodicity, |
| 457 registration_could_fire, |
| 458 BackgroundSyncMetrics::REGISTRATION_IS_DUPLICATE); |
| 476 | 459 |
| 477 if (existing_registration->IsFiring()) { | 460 if (existing_registration->IsFiring()) { |
| 478 existing_registration->set_sync_state( | 461 existing_registration->set_sync_state( |
| 479 BACKGROUND_SYNC_STATE_REREGISTERED_WHILE_FIRING); | 462 BACKGROUND_SYNC_STATE_REREGISTERED_WHILE_FIRING); |
| 480 } | 463 } |
| 481 | 464 |
| 482 base::ThreadTaskRunnerHandle::Get()->PostTask( | 465 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 483 FROM_HERE, | 466 FROM_HERE, |
| 484 base::Bind( | 467 base::Bind( |
| 485 callback, BACKGROUND_SYNC_STATUS_OK, | 468 callback, BACKGROUND_SYNC_STATUS_OK, |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 639 void BackgroundSyncManager::RegisterDidStore( | 622 void BackgroundSyncManager::RegisterDidStore( |
| 640 int64 sw_registration_id, | 623 int64 sw_registration_id, |
| 641 const scoped_refptr<RefCountedRegistration>& new_registration_ref, | 624 const scoped_refptr<RefCountedRegistration>& new_registration_ref, |
| 642 const StatusAndRegistrationCallback& callback, | 625 const StatusAndRegistrationCallback& callback, |
| 643 ServiceWorkerStatusCode status) { | 626 ServiceWorkerStatusCode status) { |
| 644 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 627 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 645 | 628 |
| 646 const BackgroundSyncRegistration* new_registration = | 629 const BackgroundSyncRegistration* new_registration = |
| 647 new_registration_ref->value(); | 630 new_registration_ref->value(); |
| 648 | 631 |
| 649 // For UMA, determine here whether the sync could fire immediately | |
| 650 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = | |
| 651 AreOptionConditionsMet(*new_registration->options()) | |
| 652 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE | |
| 653 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; | |
| 654 | |
| 655 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { | 632 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
| 656 // The service worker registration is gone. | 633 // The service worker registration is gone. |
| 657 BackgroundSyncMetrics::CountRegister( | 634 BackgroundSyncMetrics::CountRegisterFailure( |
| 658 new_registration->options()->periodicity, registration_could_fire, | 635 new_registration->options()->periodicity, |
| 659 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, | |
| 660 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 636 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
| 661 active_registrations_.erase(sw_registration_id); | 637 active_registrations_.erase(sw_registration_id); |
| 662 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); | 638 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); |
| 663 return; | 639 return; |
| 664 } | 640 } |
| 665 | 641 |
| 666 if (status != SERVICE_WORKER_OK) { | 642 if (status != SERVICE_WORKER_OK) { |
| 667 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " | 643 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " |
| 668 "failure."; | 644 "failure."; |
| 669 BackgroundSyncMetrics::CountRegister( | 645 BackgroundSyncMetrics::CountRegisterFailure( |
| 670 new_registration->options()->periodicity, registration_could_fire, | 646 new_registration->options()->periodicity, |
| 671 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, | |
| 672 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 647 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
| 673 DisableAndClearManager(base::Bind( | 648 DisableAndClearManager(base::Bind( |
| 674 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | 649 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
| 675 base::Passed(scoped_ptr<BackgroundSyncRegistrationHandle>().Pass()))); | 650 base::Passed(scoped_ptr<BackgroundSyncRegistrationHandle>().Pass()))); |
| 676 return; | 651 return; |
| 677 } | 652 } |
| 678 | 653 |
| 679 BackgroundSyncMetrics::CountRegister( | 654 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = |
| 655 AreOptionConditionsMet(*new_registration->options()) |
| 656 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE |
| 657 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; |
| 658 BackgroundSyncMetrics::CountRegisterSuccess( |
| 680 new_registration->options()->periodicity, registration_could_fire, | 659 new_registration->options()->periodicity, registration_could_fire, |
| 681 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, | 660 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE); |
| 682 BACKGROUND_SYNC_STATUS_OK); | 661 |
| 683 FireReadyEvents(); | 662 FireReadyEvents(); |
| 684 base::ThreadTaskRunnerHandle::Get()->PostTask( | 663 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 685 FROM_HERE, | 664 FROM_HERE, |
| 686 base::Bind( | 665 base::Bind( |
| 687 callback, BACKGROUND_SYNC_STATUS_OK, | 666 callback, BACKGROUND_SYNC_STATUS_OK, |
| 688 base::Passed( | 667 base::Passed( |
| 689 CreateRegistrationHandle(new_registration_ref.get()).Pass()))); | 668 CreateRegistrationHandle(new_registration_ref.get()).Pass()))); |
| 690 } | 669 } |
| 691 | 670 |
| 692 void BackgroundSyncManager::RemoveActiveRegistration( | 671 void BackgroundSyncManager::RemoveActiveRegistration( |
| (...skipping 759 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1452 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { | 1431 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { |
| 1453 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1432 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1454 | 1433 |
| 1455 return base::Bind( | 1434 return base::Bind( |
| 1456 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, | 1435 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, |
| 1457 BackgroundSyncStatus>, | 1436 BackgroundSyncStatus>, |
| 1458 weak_ptr_factory_.GetWeakPtr(), callback); | 1437 weak_ptr_factory_.GetWeakPtr(), callback); |
| 1459 } | 1438 } |
| 1460 | 1439 |
| 1461 } // namespace content | 1440 } // namespace content |
| OLD | NEW |