Chromium Code Reviews| Index: content/browser/background_sync/background_sync_manager.cc |
| diff --git a/content/browser/background_sync/background_sync_manager.cc b/content/browser/background_sync/background_sync_manager.cc |
| index e056d23979b9c02ebcaacccacd1ca21856b3b14c..ecd986b5464f60b62ada82f374b4055354ec4996 100644 |
| --- a/content/browser/background_sync/background_sync_manager.cc |
| +++ b/content/browser/background_sync/background_sync_manager.cc |
| @@ -9,6 +9,7 @@ |
| #include "base/location.h" |
| #include "base/single_thread_task_runner.h" |
| #include "base/thread_task_runner_handle.h" |
| +#include "content/browser/background_sync/background_sync_metrics.h" |
| #include "content/browser/background_sync/background_sync_network_observer.h" |
| #include "content/browser/background_sync/background_sync_power_observer.h" |
| #include "content/browser/background_sync/background_sync_registration_options.h" |
| @@ -76,6 +77,9 @@ void BackgroundSyncManager::Register( |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| if (disabled_) { |
| + BackgroundSyncMetrics::CountRegistration( |
| + options.periodicity, false, |
| + BackgroundSyncMetrics::REGISTRATION_RESULT_FAILED); |
| base::ThreadTaskRunnerHandle::Get()->PostTask( |
| FROM_HERE, |
| base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
| @@ -97,6 +101,7 @@ void BackgroundSyncManager::Unregister( |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| if (disabled_) { |
| + BackgroundSyncMetrics::CountUnregistration(periodicity, false); |
| base::ThreadTaskRunnerHandle::Get()->PostTask( |
| FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); |
| return; |
| @@ -106,7 +111,7 @@ void BackgroundSyncManager::Unregister( |
| op_scheduler_.ScheduleOperation(base::Bind( |
| &BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(), |
| - sw_registration_id, registration_key, sync_registration_id, |
| + sw_registration_id, registration_key, sync_registration_id, periodicity, |
| MakeStatusCompletion(callback))); |
| } |
| @@ -292,16 +297,27 @@ void BackgroundSyncManager::RegisterImpl( |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| if (disabled_) { |
| + BackgroundSyncMetrics::CountRegistration( |
| + options.periodicity, false, |
| + BackgroundSyncMetrics::REGISTRATION_RESULT_FAILED); |
| base::ThreadTaskRunnerHandle::Get()->PostTask( |
| FROM_HERE, |
| base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
| return; |
| } |
| + // For UMA, determine here whether the sync could fire immediately |
| + bool registrationCouldFire = WouldFireRegistrationWithOptions(options); |
| + |
| const BackgroundSyncRegistration* existing_registration = |
| LookupRegistration(sw_registration_id, RegistrationKey(options)); |
| if (existing_registration && |
| existing_registration->options()->Equals(options)) { |
| + // Record the duplicated registration |
| + BackgroundSyncMetrics::CountRegistration( |
| + existing_registration->options()->periodicity, registrationCouldFire, |
| + BackgroundSyncMetrics::REGISTRATION_RESULT_DUPLICATE); |
| + |
| base::ThreadTaskRunnerHandle::Get()->PostTask( |
| FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, *existing_registration)); |
| return; |
| @@ -317,6 +333,9 @@ void BackgroundSyncManager::RegisterImpl( |
| ServiceWorkerRegistration* sw_registration = |
| service_worker_context_->GetLiveRegistration(sw_registration_id); |
| if (!sw_registration || !sw_registration->active_version()) { |
| + BackgroundSyncMetrics::CountRegistration( |
| + options.periodicity, registrationCouldFire, |
| + BackgroundSyncMetrics::REGISTRATION_RESULT_FAILED); |
| base::ThreadTaskRunnerHandle::Get()->PostTask( |
| FROM_HERE, base::Bind(callback, ERROR_TYPE_NO_SERVICE_WORKER, |
| BackgroundSyncRegistration())); |
| @@ -452,8 +471,15 @@ void BackgroundSyncManager::RegisterDidStore( |
| ServiceWorkerStatusCode status) { |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + // For UMA, determine here whether the sync could fire immediately |
| + bool registrationCouldFire = |
| + WouldFireRegistrationWithOptions(*new_registration.options()); |
| + |
| if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
| - // The registration is gone. |
| + // The service worker registration is gone. |
| + BackgroundSyncMetrics::CountRegistration( |
| + new_registration.options()->periodicity, registrationCouldFire, |
| + BackgroundSyncMetrics::REGISTRATION_RESULT_FAILED); |
| sw_to_registrations_map_.erase(sw_registration_id); |
| base::ThreadTaskRunnerHandle::Get()->PostTask( |
| FROM_HERE, |
| @@ -464,11 +490,17 @@ void BackgroundSyncManager::RegisterDidStore( |
| if (status != SERVICE_WORKER_OK) { |
| LOG(ERROR) << "BackgroundSync failed to store registration due to backend " |
| "failure."; |
| + BackgroundSyncMetrics::CountRegistration( |
| + new_registration.options()->periodicity, registrationCouldFire, |
| + BackgroundSyncMetrics::REGISTRATION_RESULT_FAILED); |
| DisableAndClearManager( |
| base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
| return; |
| } |
| + BackgroundSyncMetrics::CountRegistration( |
| + new_registration.options()->periodicity, registrationCouldFire, |
| + BackgroundSyncMetrics::REGISTRATION_RESULT_SUCCEEDED); |
| FireReadyEvents(); |
| base::ThreadTaskRunnerHandle::Get()->PostTask( |
| FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, new_registration)); |
| @@ -535,10 +567,12 @@ void BackgroundSyncManager::UnregisterImpl( |
| int64 sw_registration_id, |
| const RegistrationKey& registration_key, |
| BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| + SyncPeriodicity periodicity, |
| const StatusCallback& callback) { |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| if (disabled_) { |
| + BackgroundSyncMetrics::CountUnregistration(periodicity, false); |
| base::ThreadTaskRunnerHandle::Get()->PostTask( |
| FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); |
| return; |
| @@ -548,6 +582,7 @@ void BackgroundSyncManager::UnregisterImpl( |
| LookupRegistration(sw_registration_id, registration_key); |
| if (!existing_registration || |
| existing_registration->id() != sync_registration_id) { |
| + BackgroundSyncMetrics::CountUnregistration(periodicity, false); |
| base::ThreadTaskRunnerHandle::Get()->PostTask( |
| FROM_HERE, base::Bind(callback, ERROR_TYPE_NOT_FOUND)); |
| return; |
| @@ -555,20 +590,21 @@ void BackgroundSyncManager::UnregisterImpl( |
| RemoveRegistrationFromMap(sw_registration_id, registration_key); |
| - StoreRegistrations( |
| - sw_registration_id, |
| - base::Bind(&BackgroundSyncManager::UnregisterDidStore, |
| - weak_ptr_factory_.GetWeakPtr(), sw_registration_id, callback)); |
| + StoreRegistrations(sw_registration_id, |
| + base::Bind(&BackgroundSyncManager::UnregisterDidStore, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + sw_registration_id, periodicity, callback)); |
| } |
| -void BackgroundSyncManager::UnregisterDidStore( |
| - int64 sw_registration_id, |
| - const StatusCallback& callback, |
| - ServiceWorkerStatusCode status) { |
| +void BackgroundSyncManager::UnregisterDidStore(int64 sw_registration_id, |
| + SyncPeriodicity periodicity, |
| + const StatusCallback& callback, |
| + ServiceWorkerStatusCode status) { |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
| // ServiceWorker was unregistered. |
| + BackgroundSyncMetrics::CountUnregistration(periodicity, false); |
| sw_to_registrations_map_.erase(sw_registration_id); |
| base::ThreadTaskRunnerHandle::Get()->PostTask( |
| FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); |
| @@ -577,10 +613,12 @@ void BackgroundSyncManager::UnregisterDidStore( |
| if (status != SERVICE_WORKER_OK) { |
| LOG(ERROR) << "BackgroundSync failed to unregister due to backend failure."; |
| + BackgroundSyncMetrics::CountUnregistration(periodicity, false); |
| DisableAndClearManager(base::Bind(callback, ERROR_TYPE_STORAGE)); |
| return; |
| } |
| + BackgroundSyncMetrics::CountUnregistration(periodicity, true); |
| base::ThreadTaskRunnerHandle::Get()->PostTask( |
| FROM_HERE, base::Bind(callback, ERROR_TYPE_OK)); |
| } |
| @@ -642,6 +680,13 @@ void BackgroundSyncManager::GetRegistrationsImpl( |
| FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, out_registrations)); |
| } |
| +bool BackgroundSyncManager::WouldFireRegistrationWithOptions( |
| + const BackgroundSyncRegistrationOptions& options) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + return network_observer_->NetworkSufficient(options.network_state) && |
| + power_observer_->PowerSufficient(options.power_state); |
| +} |
| + |
| bool BackgroundSyncManager::IsRegistrationReadyToFire( |
| const BackgroundSyncRegistration& registration) { |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| @@ -655,9 +700,7 @@ bool BackgroundSyncManager::IsRegistrationReadyToFire( |
| DCHECK_EQ(SYNC_ONE_SHOT, registration.options()->periodicity); |
| - return network_observer_->NetworkSufficient( |
| - registration.options()->network_state) && |
| - power_observer_->PowerSufficient(registration.options()->power_state); |
| + return WouldFireRegistrationWithOptions(*registration.options()); |
| } |
| void BackgroundSyncManager::SchedulePendingRegistrations() { |
| @@ -730,10 +773,13 @@ void BackgroundSyncManager::FireReadyEventsImpl(const base::Closure& callback) { |
| } |
| } |
| + base::TimeTicks startTime = base::TimeTicks::Now(); |
| + |
| // Fire the sync event of the ready registrations and run |callback| once |
| // they're all done. |
| - base::Closure barrier_closure = |
| - base::BarrierClosure(sw_id_and_keys_to_fire.size(), base::Bind(callback)); |
| + base::Closure barrier_closure = base::BarrierClosure( |
| + sw_id_and_keys_to_fire.size(), |
| + base::Bind(&OnAllSyncEventsCompleted, startTime, callback)); |
| for (const auto& sw_id_and_key : sw_id_and_keys_to_fire) { |
| int64 service_worker_id = sw_id_and_key.first; |
| @@ -817,6 +863,11 @@ void BackgroundSyncManager::EventCompleteImpl( |
| return; |
| } |
| + // The event ran to completion, we should count it, no matter what happens |
| + // from here. |
| + BackgroundSyncMetrics::RecordEventResult(registration->options()->periodicity, |
| + status_code == SERVICE_WORKER_OK); |
| + |
| if (registration->options()->periodicity == SYNC_ONE_SHOT) { |
| if (status_code != SERVICE_WORKER_OK) { |
| // TODO(jkarlin) Fire the sync event on the next page load controlled by |
| @@ -862,6 +913,16 @@ void BackgroundSyncManager::EventCompleteDidStore( |
| base::Bind(callback)); |
| } |
| +// static |
| +void BackgroundSyncManager::OnAllSyncEventsCompleted( |
| + const base::TimeTicks& startTime, |
| + const base::Closure& callback) { |
| + // Record the combined time taken by all sync events and run |callback|. |
| + BackgroundSyncMetrics::RecordSyncEventHandlingTime(base::TimeTicks::Now() - |
| + startTime); |
|
Alexei Svitkine (slow)
2015/07/09 21:02:57
Ditto
iclelland
2015/07/10 14:57:26
Done.
|
| + callback.Run(); |
| +} |
| + |
| void BackgroundSyncManager::OnRegistrationDeletedImpl( |
| int64 registration_id, |
| const base::Closure& callback) { |