| 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 <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
| 15 #include "components/signin/core/browser/profile_oauth2_token_service.h" | |
| 16 #include "components/sync_driver/signin_manager_wrapper.h" | |
| 17 #include "components/sync_driver/sync_driver_switches.h" | 15 #include "components/sync_driver/sync_driver_switches.h" |
| 18 #include "components/sync_driver/sync_prefs.h" | 16 #include "components/sync_driver/sync_prefs.h" |
| 19 | 17 |
| 20 namespace browser_sync { | 18 namespace browser_sync { |
| 21 | 19 |
| 22 namespace { | 20 namespace { |
| 23 | 21 |
| 24 // The amount of time we'll wait to initialize sync if no data type triggers | 22 // The amount of time we'll wait to initialize sync if no data type triggers |
| 25 // initialization via a StartSyncFlare. | 23 // initialization via a StartSyncFlare. |
| 26 const int kDeferredInitFallbackSeconds = 10; | 24 const int kDeferredInitFallbackSeconds = 10; |
| 27 | 25 |
| 28 // Enum (for UMA, primarily) defining different events that cause us to | 26 // Enum (for UMA, primarily) defining different events that cause us to |
| 29 // exit the "deferred" state of initialization and invoke start_backend. | 27 // exit the "deferred" state of initialization and invoke start_backend. |
| 30 enum DeferredInitTrigger { | 28 enum DeferredInitTrigger { |
| 31 // We have received a signal from a SyncableService requesting that sync | 29 // We have received a signal from a SyncableService requesting that sync |
| 32 // starts as soon as possible. | 30 // starts as soon as possible. |
| 33 TRIGGER_DATA_TYPE_REQUEST, | 31 TRIGGER_DATA_TYPE_REQUEST, |
| 34 // No data type requested sync to start and our fallback timer expired. | 32 // No data type requested sync to start and our fallback timer expired. |
| 35 TRIGGER_FALLBACK_TIMER, | 33 TRIGGER_FALLBACK_TIMER, |
| 36 MAX_TRIGGER_VALUE | 34 MAX_TRIGGER_VALUE |
| 37 }; | 35 }; |
| 38 | 36 |
| 39 } // namespace | 37 } // namespace |
| 40 | 38 |
| 41 StartupController::StartupController( | 39 StartupController::StartupController(const sync_driver::SyncPrefs* sync_prefs, |
| 42 const ProfileOAuth2TokenService* token_service, | 40 base::Callback<bool()> can_start, |
| 43 const sync_driver::SyncPrefs* sync_prefs, | 41 base::Closure start_backend) |
| 44 const SigninManagerWrapper* signin, | |
| 45 base::Closure start_backend) | |
| 46 : received_start_request_(false), | 42 : received_start_request_(false), |
| 47 setup_in_progress_(false), | 43 setup_in_progress_(false), |
| 48 sync_prefs_(sync_prefs), | 44 sync_prefs_(sync_prefs), |
| 49 token_service_(token_service), | 45 can_start_(can_start), |
| 50 signin_(signin), | |
| 51 start_backend_(start_backend), | 46 start_backend_(start_backend), |
| 52 fallback_timeout_( | 47 fallback_timeout_( |
| 53 base::TimeDelta::FromSeconds(kDeferredInitFallbackSeconds)), | 48 base::TimeDelta::FromSeconds(kDeferredInitFallbackSeconds)), |
| 54 weak_factory_(this) { | 49 weak_factory_(this) { |
| 55 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 50 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 56 switches::kSyncDeferredStartupTimeoutSeconds)) { | 51 switches::kSyncDeferredStartupTimeoutSeconds)) { |
| 57 int timeout = kDeferredInitFallbackSeconds; | 52 int timeout = kDeferredInitFallbackSeconds; |
| 58 if (base::StringToInt( | 53 if (base::StringToInt( |
| 59 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 54 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 60 switches::kSyncDeferredStartupTimeoutSeconds), | 55 switches::kSyncDeferredStartupTimeoutSeconds), |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 | 107 |
| 113 return true; | 108 return true; |
| 114 } | 109 } |
| 115 | 110 |
| 116 void StartupController::OverrideFallbackTimeoutForTest( | 111 void StartupController::OverrideFallbackTimeoutForTest( |
| 117 const base::TimeDelta& timeout) { | 112 const base::TimeDelta& timeout) { |
| 118 fallback_timeout_ = timeout; | 113 fallback_timeout_ = timeout; |
| 119 } | 114 } |
| 120 | 115 |
| 121 bool StartupController::TryStart() { | 116 bool StartupController::TryStart() { |
| 122 if (sync_prefs_->IsManaged()) | 117 if (!can_start_.Run()) |
| 123 return false; | 118 return false; |
| 124 | 119 |
| 125 if (!sync_prefs_->IsSyncRequested()) | |
| 126 return false; | |
| 127 | |
| 128 if (signin_->GetAccountIdToUse().empty()) | |
| 129 return false; | |
| 130 | |
| 131 if (!token_service_) | |
| 132 return false; | |
| 133 | |
| 134 if (!token_service_->RefreshTokenIsAvailable(signin_->GetAccountIdToUse())) { | |
| 135 return false; | |
| 136 } | |
| 137 | |
| 138 // TODO(tim): Seems wrong to always record this histogram here... | |
| 139 // If we got here then tokens are loaded and user logged in and sync is | |
| 140 // enabled. If OAuth refresh token is not available then something is wrong. | |
| 141 // When PSS requests access token, OAuth2TokenService will return error and | |
| 142 // PSS will show error to user asking to reauthenticate. | |
| 143 UMA_HISTOGRAM_BOOLEAN("Sync.RefreshTokenAvailable", true); | |
| 144 | |
| 145 // For performance reasons, defer the heavy lifting for sync init unless: | 120 // For performance reasons, defer the heavy lifting for sync init unless: |
| 146 // | 121 // |
| 147 // - a datatype has requested an immediate start of sync, or | 122 // - a datatype has requested an immediate start of sync, or |
| 148 // - sync needs to start up the backend immediately to provide control state | 123 // - sync needs to start up the backend immediately to provide control state |
| 149 // and encryption information to the UI, or | 124 // and encryption information to the UI, or |
| 150 // - this is the first time sync is ever starting up. | 125 // - this is the first time sync is ever starting up. |
| 151 if (received_start_request_ || setup_in_progress_ || | 126 if (received_start_request_ || setup_in_progress_ || |
| 152 !sync_prefs_->IsFirstSetupComplete()) { | 127 !sync_prefs_->IsFirstSetupComplete()) { |
| 153 return StartUp(STARTUP_IMMEDIATE); | 128 return StartUp(STARTUP_IMMEDIATE); |
| 154 } else { | 129 } else { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 syncer::MODEL_TYPE_COUNT); | 189 syncer::MODEL_TYPE_COUNT); |
| 215 UMA_HISTOGRAM_ENUMERATION("Sync.Startup.DeferredInitTrigger", | 190 UMA_HISTOGRAM_ENUMERATION("Sync.Startup.DeferredInitTrigger", |
| 216 TRIGGER_DATA_TYPE_REQUEST, | 191 TRIGGER_DATA_TYPE_REQUEST, |
| 217 MAX_TRIGGER_VALUE); | 192 MAX_TRIGGER_VALUE); |
| 218 } | 193 } |
| 219 received_start_request_ = true; | 194 received_start_request_ = true; |
| 220 TryStart(); | 195 TryStart(); |
| 221 } | 196 } |
| 222 | 197 |
| 223 } // namespace browser_sync | 198 } // namespace browser_sync |
| OLD | NEW |