Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(400)

Side by Side Diff: components/sync_driver/startup_controller.cc

Issue 1575153004: [Sync] Simplify sync startup behavior. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@setup
Patch Set: Rebase. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
(...skipping 21 matching lines...) Expand all
32 // starts as soon as possible. 32 // starts as soon as possible.
33 TRIGGER_DATA_TYPE_REQUEST, 33 TRIGGER_DATA_TYPE_REQUEST,
34 // No data type requested sync to start and our fallback timer expired. 34 // No data type requested sync to start and our fallback timer expired.
35 TRIGGER_FALLBACK_TIMER, 35 TRIGGER_FALLBACK_TIMER,
36 MAX_TRIGGER_VALUE 36 MAX_TRIGGER_VALUE
37 }; 37 };
38 38
39 } // namespace 39 } // namespace
40 40
41 StartupController::StartupController( 41 StartupController::StartupController(
42 ProfileSyncServiceStartBehavior start_behavior,
43 const ProfileOAuth2TokenService* token_service, 42 const ProfileOAuth2TokenService* token_service,
44 const sync_driver::SyncPrefs* sync_prefs, 43 const sync_driver::SyncPrefs* sync_prefs,
45 const SigninManagerWrapper* signin, 44 const SigninManagerWrapper* signin,
46 base::Closure start_backend) 45 base::Closure start_backend)
47 : received_start_request_(false), 46 : received_start_request_(false),
48 setup_in_progress_(false), 47 setup_in_progress_(false),
49 auto_start_enabled_(start_behavior == AUTO_START),
50 sync_prefs_(sync_prefs), 48 sync_prefs_(sync_prefs),
51 token_service_(token_service), 49 token_service_(token_service),
52 signin_(signin), 50 signin_(signin),
53 start_backend_(start_backend), 51 start_backend_(start_backend),
54 fallback_timeout_( 52 fallback_timeout_(
55 base::TimeDelta::FromSeconds(kDeferredInitFallbackSeconds)), 53 base::TimeDelta::FromSeconds(kDeferredInitFallbackSeconds)),
56 first_start_(true),
57 weak_factory_(this) { 54 weak_factory_(this) {
58 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 55 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
59 switches::kSyncDeferredStartupTimeoutSeconds)) { 56 switches::kSyncDeferredStartupTimeoutSeconds)) {
60 int timeout = kDeferredInitFallbackSeconds; 57 int timeout = kDeferredInitFallbackSeconds;
61 if (base::StringToInt( 58 if (base::StringToInt(
62 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 59 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
63 switches::kSyncDeferredStartupTimeoutSeconds), 60 switches::kSyncDeferredStartupTimeoutSeconds),
64 &timeout)) { 61 &timeout)) {
65 DCHECK_GE(timeout, 0); 62 DCHECK_GE(timeout, 0);
66 DVLOG(2) << "Sync StartupController overriding startup timeout to " 63 DVLOG(2) << "Sync StartupController overriding startup timeout to "
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 base::Bind(&StartupController::OnFallbackStartupTimerExpired, 98 base::Bind(&StartupController::OnFallbackStartupTimerExpired,
102 weak_factory_.GetWeakPtr()), 99 weak_factory_.GetWeakPtr()),
103 fallback_timeout_); 100 fallback_timeout_);
104 } 101 }
105 return false; 102 return false;
106 } 103 }
107 104
108 if (start_backend_time_.is_null()) { 105 if (start_backend_time_.is_null()) {
109 start_backend_time_ = base::Time::Now(); 106 start_backend_time_ = base::Time::Now();
110 start_backend_.Run(); 107 start_backend_.Run();
111 first_start_ = false;
112 } 108 }
113 109
114 return true; 110 return true;
115 } 111 }
116 112
117 void StartupController::OverrideFallbackTimeoutForTest( 113 void StartupController::OverrideFallbackTimeoutForTest(
118 const base::TimeDelta& timeout) { 114 const base::TimeDelta& timeout) {
119 fallback_timeout_ = timeout; 115 fallback_timeout_ = timeout;
120 } 116 }
121 117
(...skipping 14 matching lines...) Expand all
136 return false; 132 return false;
137 } 133 }
138 134
139 // TODO(tim): Seems wrong to always record this histogram here... 135 // TODO(tim): Seems wrong to always record this histogram here...
140 // If we got here then tokens are loaded and user logged in and sync is 136 // If we got here then tokens are loaded and user logged in and sync is
141 // enabled. If OAuth refresh token is not available then something is wrong. 137 // enabled. If OAuth refresh token is not available then something is wrong.
142 // When PSS requests access token, OAuth2TokenService will return error and 138 // When PSS requests access token, OAuth2TokenService will return error and
143 // PSS will show error to user asking to reauthenticate. 139 // PSS will show error to user asking to reauthenticate.
144 UMA_HISTOGRAM_BOOLEAN("Sync.RefreshTokenAvailable", true); 140 UMA_HISTOGRAM_BOOLEAN("Sync.RefreshTokenAvailable", true);
145 141
146 // If sync setup has completed we always start the backend. If the user is in 142 // For performance reasons, defer the heavy lifting for sync init unless:
147 // the process of setting up now, we should start the backend to download
148 // account control state / encryption information). If autostart is enabled,
149 // but we haven't completed sync setup, we try to start sync anyway, since
150 // it's possible we crashed/shutdown after logging in but before the backend
151 // finished initializing the last time.
152 // 143 //
153 // However, the only time we actually need to start sync _immediately_ is if 144 // - a datatype has requested an immediate start of sync, or
154 // we haven't completed sync setup and the user is in the process of setting 145 // - sync needs to start up the backend immediately to provide control state
155 // up - either they just signed in (for the first time) on an auto-start 146 // and encryption information to the UI, or
156 // platform or they explicitly kicked off sync setup, and e.g we need to 147 // - this is the first time sync is ever starting up.
157 // fetch account details like encryption state to populate UI. Otherwise, 148 if (received_start_request_ || setup_in_progress_ ||
158 // for performance reasons and maximizing parallelism at chrome startup, we 149 !sync_prefs_->IsFirstSetupComplete()) {
159 // defer the heavy lifting for sync init until things have calmed down.
160 if (sync_prefs_->IsFirstSetupComplete()) {
161 // For first time, defer start if data type hasn't requested sync to avoid
162 // stressing browser start.
163 if (!received_start_request_ && first_start_)
164 return StartUp(STARTUP_BACKEND_DEFERRED);
165 else
166 return StartUp(STARTUP_IMMEDIATE);
167 } else if (setup_in_progress_ || auto_start_enabled_) {
168 // We haven't completed sync setup. Start immediately if the user explicitly
169 // kicked this off or we're supposed to automatically start syncing.
170 return StartUp(STARTUP_IMMEDIATE); 150 return StartUp(STARTUP_IMMEDIATE);
151 } else {
152 return StartUp(STARTUP_BACKEND_DEFERRED);
171 } 153 }
172
173 return false;
174 } 154 }
175 155
176 void StartupController::RecordTimeDeferred() { 156 void StartupController::RecordTimeDeferred() {
177 DCHECK(!start_up_time_.is_null()); 157 DCHECK(!start_up_time_.is_null());
178 base::TimeDelta time_deferred = base::Time::Now() - start_up_time_; 158 base::TimeDelta time_deferred = base::Time::Now() - start_up_time_;
179 UMA_HISTOGRAM_CUSTOM_TIMES("Sync.Startup.TimeDeferred2", 159 UMA_HISTOGRAM_CUSTOM_TIMES("Sync.Startup.TimeDeferred2",
180 time_deferred, 160 time_deferred,
181 base::TimeDelta::FromSeconds(0), 161 base::TimeDelta::FromSeconds(0),
182 base::TimeDelta::FromMinutes(2), 162 base::TimeDelta::FromMinutes(2),
183 60); 163 60);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 syncer::MODEL_TYPE_COUNT); 211 syncer::MODEL_TYPE_COUNT);
232 UMA_HISTOGRAM_ENUMERATION("Sync.Startup.DeferredInitTrigger", 212 UMA_HISTOGRAM_ENUMERATION("Sync.Startup.DeferredInitTrigger",
233 TRIGGER_DATA_TYPE_REQUEST, 213 TRIGGER_DATA_TYPE_REQUEST,
234 MAX_TRIGGER_VALUE); 214 MAX_TRIGGER_VALUE);
235 } 215 }
236 received_start_request_ = true; 216 received_start_request_ = true;
237 TryStart(); 217 TryStart();
238 } 218 }
239 219
240 } // namespace browser_sync 220 } // namespace browser_sync
OLDNEW
« no previous file with comments | « components/sync_driver/startup_controller.h ('k') | components/sync_driver/startup_controller_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698