| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/sync/driver/startup_controller.h" | 5 #include "components/sync/driver/startup_controller.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "components/sync/base/sync_prefs.h" | 13 #include "components/sync/base/sync_prefs.h" |
| 14 #include "components/sync/driver/sync_driver_switches.h" | 14 #include "components/sync/driver/sync_driver_switches.h" |
| 15 | 15 |
| 16 namespace syncer { | 16 namespace syncer { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 // The amount of time we'll wait to initialize sync if no data type triggers | 20 // The amount of time we'll wait to initialize sync if no data type triggers |
| 21 // initialization via a StartSyncFlare. | 21 // initialization via a StartSyncFlare. |
| 22 const int kDeferredInitFallbackSeconds = 10; | 22 const int kDeferredInitFallbackSeconds = 10; |
| 23 | 23 |
| 24 // Enum (for UMA, primarily) defining different events that cause us to | 24 // Enum (for UMA, primarily) defining different events that cause us to |
| 25 // exit the "deferred" state of initialization and invoke start_backend. | 25 // exit the "deferred" state of initialization and invoke start_engine. |
| 26 enum DeferredInitTrigger { | 26 enum DeferredInitTrigger { |
| 27 // We have received a signal from a SyncableService requesting that sync | 27 // We have received a signal from a SyncableService requesting that sync |
| 28 // starts as soon as possible. | 28 // starts as soon as possible. |
| 29 TRIGGER_DATA_TYPE_REQUEST, | 29 TRIGGER_DATA_TYPE_REQUEST, |
| 30 // No data type requested sync to start and our fallback timer expired. | 30 // No data type requested sync to start and our fallback timer expired. |
| 31 TRIGGER_FALLBACK_TIMER, | 31 TRIGGER_FALLBACK_TIMER, |
| 32 MAX_TRIGGER_VALUE | 32 MAX_TRIGGER_VALUE |
| 33 }; | 33 }; |
| 34 | 34 |
| 35 } // namespace | 35 } // namespace |
| 36 | 36 |
| 37 StartupController::StartupController(const SyncPrefs* sync_prefs, | 37 StartupController::StartupController(const SyncPrefs* sync_prefs, |
| 38 base::Callback<bool()> can_start, | 38 base::Callback<bool()> can_start, |
| 39 base::Closure start_backend) | 39 base::Closure start_engine) |
| 40 : bypass_setup_complete_(false), | 40 : bypass_setup_complete_(false), |
| 41 received_start_request_(false), | 41 received_start_request_(false), |
| 42 setup_in_progress_(false), | 42 setup_in_progress_(false), |
| 43 sync_prefs_(sync_prefs), | 43 sync_prefs_(sync_prefs), |
| 44 can_start_(can_start), | 44 can_start_(can_start), |
| 45 start_backend_(start_backend), | 45 start_engine_(start_engine), |
| 46 fallback_timeout_( | 46 fallback_timeout_( |
| 47 base::TimeDelta::FromSeconds(kDeferredInitFallbackSeconds)), | 47 base::TimeDelta::FromSeconds(kDeferredInitFallbackSeconds)), |
| 48 weak_factory_(this) { | 48 weak_factory_(this) { |
| 49 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 49 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 50 switches::kSyncDeferredStartupTimeoutSeconds)) { | 50 switches::kSyncDeferredStartupTimeoutSeconds)) { |
| 51 int timeout = kDeferredInitFallbackSeconds; | 51 int timeout = kDeferredInitFallbackSeconds; |
| 52 if (base::StringToInt( | 52 if (base::StringToInt( |
| 53 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 53 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 54 switches::kSyncDeferredStartupTimeoutSeconds), | 54 switches::kSyncDeferredStartupTimeoutSeconds), |
| 55 &timeout)) { | 55 &timeout)) { |
| 56 DCHECK_GE(timeout, 0); | 56 DCHECK_GE(timeout, 0); |
| 57 DVLOG(2) << "Sync StartupController overriding startup timeout to " | 57 DVLOG(2) << "Sync StartupController overriding startup timeout to " |
| 58 << timeout << " seconds."; | 58 << timeout << " seconds."; |
| 59 fallback_timeout_ = base::TimeDelta::FromSeconds(timeout); | 59 fallback_timeout_ = base::TimeDelta::FromSeconds(timeout); |
| 60 } | 60 } |
| 61 } | 61 } |
| 62 } | 62 } |
| 63 | 63 |
| 64 StartupController::~StartupController() {} | 64 StartupController::~StartupController() {} |
| 65 | 65 |
| 66 void StartupController::Reset(const ModelTypeSet registered_types) { | 66 void StartupController::Reset(const ModelTypeSet registered_types) { |
| 67 received_start_request_ = false; | 67 received_start_request_ = false; |
| 68 bypass_setup_complete_ = false; | 68 bypass_setup_complete_ = false; |
| 69 start_up_time_ = base::Time(); | 69 start_up_time_ = base::Time(); |
| 70 start_backend_time_ = base::Time(); | 70 start_engine_time_ = base::Time(); |
| 71 // Don't let previous timers affect us post-reset. | 71 // Don't let previous timers affect us post-reset. |
| 72 weak_factory_.InvalidateWeakPtrs(); | 72 weak_factory_.InvalidateWeakPtrs(); |
| 73 registered_types_ = registered_types; | 73 registered_types_ = registered_types; |
| 74 } | 74 } |
| 75 | 75 |
| 76 void StartupController::SetSetupInProgress(bool setup_in_progress) { | 76 void StartupController::SetSetupInProgress(bool setup_in_progress) { |
| 77 setup_in_progress_ = setup_in_progress; | 77 setup_in_progress_ = setup_in_progress; |
| 78 if (setup_in_progress_) { | 78 if (setup_in_progress_) { |
| 79 TryStart(); | 79 TryStart(); |
| 80 } | 80 } |
| 81 } | 81 } |
| 82 | 82 |
| 83 bool StartupController::StartUp(StartUpDeferredOption deferred_option) { | 83 bool StartupController::StartUp(StartUpDeferredOption deferred_option) { |
| 84 const bool first_start = start_up_time_.is_null(); | 84 const bool first_start = start_up_time_.is_null(); |
| 85 if (first_start) | 85 if (first_start) |
| 86 start_up_time_ = base::Time::Now(); | 86 start_up_time_ = base::Time::Now(); |
| 87 | 87 |
| 88 if (deferred_option == STARTUP_BACKEND_DEFERRED && | 88 if (deferred_option == STARTUP_DEFERRED && |
| 89 !base::CommandLine::ForCurrentProcess()->HasSwitch( | 89 !base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 90 switches::kSyncDisableDeferredStartup) && | 90 switches::kSyncDisableDeferredStartup) && |
| 91 sync_prefs_->GetPreferredDataTypes(registered_types_).Has(SESSIONS)) { | 91 sync_prefs_->GetPreferredDataTypes(registered_types_).Has(SESSIONS)) { |
| 92 if (first_start) { | 92 if (first_start) { |
| 93 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 93 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 94 FROM_HERE, | 94 FROM_HERE, |
| 95 base::Bind(&StartupController::OnFallbackStartupTimerExpired, | 95 base::Bind(&StartupController::OnFallbackStartupTimerExpired, |
| 96 weak_factory_.GetWeakPtr()), | 96 weak_factory_.GetWeakPtr()), |
| 97 fallback_timeout_); | 97 fallback_timeout_); |
| 98 } | 98 } |
| 99 return false; | 99 return false; |
| 100 } | 100 } |
| 101 | 101 |
| 102 if (start_backend_time_.is_null()) { | 102 if (start_engine_time_.is_null()) { |
| 103 start_backend_time_ = base::Time::Now(); | 103 start_engine_time_ = base::Time::Now(); |
| 104 start_backend_.Run(); | 104 start_engine_.Run(); |
| 105 } | 105 } |
| 106 | 106 |
| 107 return true; | 107 return true; |
| 108 } | 108 } |
| 109 | 109 |
| 110 void StartupController::OverrideFallbackTimeoutForTest( | 110 void StartupController::OverrideFallbackTimeoutForTest( |
| 111 const base::TimeDelta& timeout) { | 111 const base::TimeDelta& timeout) { |
| 112 fallback_timeout_ = timeout; | 112 fallback_timeout_ = timeout; |
| 113 } | 113 } |
| 114 | 114 |
| 115 bool StartupController::TryStart() { | 115 bool StartupController::TryStart() { |
| 116 if (!can_start_.Run()) | 116 if (!can_start_.Run()) |
| 117 return false; | 117 return false; |
| 118 | 118 |
| 119 // For performance reasons, defer the heavy lifting for sync init unless: | 119 // For performance reasons, defer the heavy lifting for sync init unless: |
| 120 // | 120 // |
| 121 // - a datatype has requested an immediate start of sync, or | 121 // - a datatype has requested an immediate start of sync, or |
| 122 // - sync needs to start up the backend immediately to provide control state | 122 // - sync needs to start up the engine immediately to provide control state |
| 123 // and encryption information to the UI. | 123 // and encryption information to the UI. |
| 124 // Do not start up the sync backend if setup has not completed and isn't | 124 // Do not start up the sync engine if setup has not completed and isn't |
| 125 // in progress, unless told to otherwise. | 125 // in progress, unless told to otherwise. |
| 126 if (setup_in_progress_) { | 126 if (setup_in_progress_) { |
| 127 return StartUp(STARTUP_IMMEDIATE); | 127 return StartUp(STARTUP_IMMEDIATE); |
| 128 } else if (sync_prefs_->IsFirstSetupComplete() || bypass_setup_complete_) { | 128 } else if (sync_prefs_->IsFirstSetupComplete() || bypass_setup_complete_) { |
| 129 return StartUp(received_start_request_ ? STARTUP_IMMEDIATE | 129 return StartUp(received_start_request_ ? STARTUP_IMMEDIATE |
| 130 : STARTUP_BACKEND_DEFERRED); | 130 : STARTUP_DEFERRED); |
| 131 } else { | 131 } else { |
| 132 return false; | 132 return false; |
| 133 } | 133 } |
| 134 } | 134 } |
| 135 | 135 |
| 136 bool StartupController::TryStartImmediately() { | 136 bool StartupController::TryStartImmediately() { |
| 137 received_start_request_ = true; | 137 received_start_request_ = true; |
| 138 bypass_setup_complete_ = true; | 138 bypass_setup_complete_ = true; |
| 139 return TryStart(); | 139 return TryStart(); |
| 140 } | 140 } |
| 141 | 141 |
| 142 void StartupController::RecordTimeDeferred() { | 142 void StartupController::RecordTimeDeferred() { |
| 143 DCHECK(!start_up_time_.is_null()); | 143 DCHECK(!start_up_time_.is_null()); |
| 144 base::TimeDelta time_deferred = base::Time::Now() - start_up_time_; | 144 base::TimeDelta time_deferred = base::Time::Now() - start_up_time_; |
| 145 UMA_HISTOGRAM_CUSTOM_TIMES("Sync.Startup.TimeDeferred2", time_deferred, | 145 UMA_HISTOGRAM_CUSTOM_TIMES("Sync.Startup.TimeDeferred2", time_deferred, |
| 146 base::TimeDelta::FromSeconds(0), | 146 base::TimeDelta::FromSeconds(0), |
| 147 base::TimeDelta::FromMinutes(2), 60); | 147 base::TimeDelta::FromMinutes(2), 60); |
| 148 } | 148 } |
| 149 | 149 |
| 150 void StartupController::OnFallbackStartupTimerExpired() { | 150 void StartupController::OnFallbackStartupTimerExpired() { |
| 151 DCHECK(!base::CommandLine::ForCurrentProcess()->HasSwitch( | 151 DCHECK(!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 152 switches::kSyncDisableDeferredStartup)); | 152 switches::kSyncDisableDeferredStartup)); |
| 153 | 153 |
| 154 if (!start_backend_time_.is_null()) | 154 if (!start_engine_time_.is_null()) |
| 155 return; | 155 return; |
| 156 | 156 |
| 157 DVLOG(2) << "Sync deferred init fallback timer expired, starting backend."; | 157 DVLOG(2) << "Sync deferred init fallback timer expired, starting engine."; |
| 158 RecordTimeDeferred(); | 158 RecordTimeDeferred(); |
| 159 UMA_HISTOGRAM_ENUMERATION("Sync.Startup.DeferredInitTrigger", | 159 UMA_HISTOGRAM_ENUMERATION("Sync.Startup.DeferredInitTrigger", |
| 160 TRIGGER_FALLBACK_TIMER, MAX_TRIGGER_VALUE); | 160 TRIGGER_FALLBACK_TIMER, MAX_TRIGGER_VALUE); |
| 161 received_start_request_ = true; | 161 received_start_request_ = true; |
| 162 TryStart(); | 162 TryStart(); |
| 163 } | 163 } |
| 164 | 164 |
| 165 std::string StartupController::GetBackendInitializationStateString() const { | 165 std::string StartupController::GetEngineInitializationStateString() const { |
| 166 if (!start_backend_time_.is_null()) | 166 if (!start_engine_time_.is_null()) |
| 167 return "Started"; | 167 return "Started"; |
| 168 else if (!start_up_time_.is_null()) | 168 else if (!start_up_time_.is_null()) |
| 169 return "Deferred"; | 169 return "Deferred"; |
| 170 else | 170 else |
| 171 return "Not started"; | 171 return "Not started"; |
| 172 } | 172 } |
| 173 | 173 |
| 174 void StartupController::OnDataTypeRequestsSyncStartup(ModelType type) { | 174 void StartupController::OnDataTypeRequestsSyncStartup(ModelType type) { |
| 175 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 175 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 176 switches::kSyncDisableDeferredStartup)) { | 176 switches::kSyncDisableDeferredStartup)) { |
| 177 DVLOG(2) << "Ignoring data type request for sync startup: " | 177 DVLOG(2) << "Ignoring data type request for sync startup: " |
| 178 << ModelTypeToString(type); | 178 << ModelTypeToString(type); |
| 179 return; | 179 return; |
| 180 } | 180 } |
| 181 | 181 |
| 182 if (!start_backend_time_.is_null()) | 182 if (!start_engine_time_.is_null()) |
| 183 return; | 183 return; |
| 184 | 184 |
| 185 DVLOG(2) << "Data type requesting sync startup: " << ModelTypeToString(type); | 185 DVLOG(2) << "Data type requesting sync startup: " << ModelTypeToString(type); |
| 186 // Measure the time spent waiting for init and the type that triggered it. | 186 // Measure the time spent waiting for init and the type that triggered it. |
| 187 // We could measure the time spent deferred on a per-datatype basis, but | 187 // We could measure the time spent deferred on a per-datatype basis, but |
| 188 // for now this is probably sufficient. | 188 // for now this is probably sufficient. |
| 189 UMA_HISTOGRAM_ENUMERATION("Sync.Startup.TypeTriggeringInit", | 189 UMA_HISTOGRAM_ENUMERATION("Sync.Startup.TypeTriggeringInit", |
| 190 ModelTypeToHistogramInt(type), MODEL_TYPE_COUNT); | 190 ModelTypeToHistogramInt(type), MODEL_TYPE_COUNT); |
| 191 if (!start_up_time_.is_null()) { | 191 if (!start_up_time_.is_null()) { |
| 192 RecordTimeDeferred(); | 192 RecordTimeDeferred(); |
| 193 UMA_HISTOGRAM_ENUMERATION("Sync.Startup.DeferredInitTrigger", | 193 UMA_HISTOGRAM_ENUMERATION("Sync.Startup.DeferredInitTrigger", |
| 194 TRIGGER_DATA_TYPE_REQUEST, MAX_TRIGGER_VALUE); | 194 TRIGGER_DATA_TYPE_REQUEST, MAX_TRIGGER_VALUE); |
| 195 } | 195 } |
| 196 received_start_request_ = true; | 196 received_start_request_ = true; |
| 197 TryStart(); | 197 TryStart(); |
| 198 } | 198 } |
| 199 | 199 |
| 200 } // namespace syncer | 200 } // namespace syncer |
| OLD | NEW |