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

Side by Side Diff: chrome/browser/sync/startup_controller.cc

Issue 183563004: sync: reland deferred init (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add dvlog Created 6 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 | Annotate | Revision Log
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 "chrome/browser/sync/startup_controller.h" 5 #include "chrome/browser/sync/startup_controller.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "base/metrics/histogram.h" 9 #include "base/metrics/histogram.h"
10 #include "base/strings/string_number_conversions.h"
10 #include "chrome/browser/managed_mode/managed_user_signin_manager_wrapper.h" 11 #include "chrome/browser/managed_mode/managed_user_signin_manager_wrapper.h"
11 #include "chrome/browser/signin/profile_oauth2_token_service.h" 12 #include "chrome/browser/signin/profile_oauth2_token_service.h"
12 #include "chrome/browser/sync/sync_prefs.h" 13 #include "chrome/browser/sync/sync_prefs.h"
13 #include "chrome/common/chrome_switches.h" 14 #include "chrome/common/chrome_switches.h"
14 15
15 namespace browser_sync { 16 namespace browser_sync {
16 17
17 namespace { 18 namespace {
18 19
19 // 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
(...skipping 19 matching lines...) Expand all
39 const browser_sync::SyncPrefs* sync_prefs, 40 const browser_sync::SyncPrefs* sync_prefs,
40 const ManagedUserSigninManagerWrapper* signin, 41 const ManagedUserSigninManagerWrapper* signin,
41 base::Closure start_backend) 42 base::Closure start_backend)
42 : received_start_request_(false), 43 : received_start_request_(false),
43 setup_in_progress_(false), 44 setup_in_progress_(false),
44 auto_start_enabled_(start_behavior == AUTO_START), 45 auto_start_enabled_(start_behavior == AUTO_START),
45 sync_prefs_(sync_prefs), 46 sync_prefs_(sync_prefs),
46 token_service_(token_service), 47 token_service_(token_service),
47 signin_(signin), 48 signin_(signin),
48 start_backend_(start_backend), 49 start_backend_(start_backend),
49 fallback_timeout_( 50 fallback_timeout_(base::TimeDelta::FromSeconds(
50 base::TimeDelta::FromSeconds(kDeferredInitFallbackSeconds)), 51 kDeferredInitFallbackSeconds)),
51 weak_factory_(this) {} 52 weak_factory_(this) {
53
54 if (CommandLine::ForCurrentProcess()->HasSwitch(
55 switches::kSyncDeferredStartupTimeoutSeconds)) {
56 int timeout = kDeferredInitFallbackSeconds;
57 if (base::StringToInt(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
58 switches::kSyncDeferredStartupTimeoutSeconds), &timeout)) {
59 DCHECK_GE(0, timeout);
60 DVLOG(2) << "Sync StartupController overriding startup timeout to "
61 << timeout << " seconds.";
62 fallback_timeout_ = base::TimeDelta::FromSeconds(timeout);
63 }
64 }
65 }
52 66
53 StartupController::~StartupController() {} 67 StartupController::~StartupController() {}
54 68
55 void StartupController::Reset(const syncer::ModelTypeSet registered_types) { 69 void StartupController::Reset(const syncer::ModelTypeSet registered_types) {
56 received_start_request_ = false; 70 received_start_request_ = false;
57 setup_in_progress_ = false; 71 setup_in_progress_ = false;
58 start_up_time_ = base::Time(); 72 start_up_time_ = base::Time();
59 start_backend_time_ = base::Time(); 73 start_backend_time_ = base::Time();
60 // Don't let previous timers affect us post-reset. 74 // Don't let previous timers affect us post-reset.
61 weak_factory_.InvalidateWeakPtrs(); 75 weak_factory_.InvalidateWeakPtrs();
62 registered_types_ = registered_types; 76 registered_types_ = registered_types;
63 } 77 }
64 78
65 void StartupController::set_setup_in_progress(bool in_progress) { 79 void StartupController::set_setup_in_progress(bool in_progress) {
66 setup_in_progress_ = in_progress; 80 setup_in_progress_ = in_progress;
67 } 81 }
68 82
69 bool StartupController::StartUp(StartUpDeferredOption deferred_option) { 83 bool StartupController::StartUp(StartUpDeferredOption deferred_option) {
70 const bool first_start = start_up_time_.is_null(); 84 const bool first_start = start_up_time_.is_null();
71 if (first_start) 85 if (first_start)
72 start_up_time_ = base::Time::Now(); 86 start_up_time_ = base::Time::Now();
73 87
74 if (deferred_option == STARTUP_BACKEND_DEFERRED && 88 if (deferred_option == STARTUP_BACKEND_DEFERRED &&
75 CommandLine::ForCurrentProcess()->HasSwitch( 89 !CommandLine::ForCurrentProcess()->HasSwitch(
76 switches::kSyncEnableDeferredStartup) && 90 switches::kSyncDisableDeferredStartup) &&
77 sync_prefs_->GetPreferredDataTypes(registered_types_) 91 sync_prefs_->GetPreferredDataTypes(registered_types_)
78 .Has(syncer::SESSIONS)) { 92 .Has(syncer::SESSIONS)) {
79 if (first_start) { 93 if (first_start) {
80 base::MessageLoop::current()->PostDelayedTask( 94 base::MessageLoop::current()->PostDelayedTask(
81 FROM_HERE, 95 FROM_HERE,
82 base::Bind(&StartupController::OnFallbackStartupTimerExpired, 96 base::Bind(&StartupController::OnFallbackStartupTimerExpired,
83 weak_factory_.GetWeakPtr()), fallback_timeout_); 97 weak_factory_.GetWeakPtr()), fallback_timeout_);
84 } 98 }
85 return false; 99 return false;
86 } 100 }
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 } else if (setup_in_progress_ || auto_start_enabled_) { 159 } else if (setup_in_progress_ || auto_start_enabled_) {
146 // We haven't completed sync setup. Start immediately if the user explicitly 160 // We haven't completed sync setup. Start immediately if the user explicitly
147 // kicked this off or we're supposed to automatically start syncing. 161 // kicked this off or we're supposed to automatically start syncing.
148 return StartUp(STARTUP_IMMEDIATE); 162 return StartUp(STARTUP_IMMEDIATE);
149 } 163 }
150 164
151 return false; 165 return false;
152 } 166 }
153 167
154 void StartupController::OnFallbackStartupTimerExpired() { 168 void StartupController::OnFallbackStartupTimerExpired() {
155 DCHECK(CommandLine::ForCurrentProcess()->HasSwitch( 169 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch(
156 switches::kSyncEnableDeferredStartup)); 170 switches::kSyncDisableDeferredStartup));
157 171
158 if (!start_backend_time_.is_null()) 172 if (!start_backend_time_.is_null())
159 return; 173 return;
160 174
161 DVLOG(2) << "Sync deferred init fallback timer expired, starting backend."; 175 DVLOG(2) << "Sync deferred init fallback timer expired, starting backend.";
162 DCHECK(!start_up_time_.is_null()); 176 DCHECK(!start_up_time_.is_null());
163 base::TimeDelta time_deferred = base::Time::Now() - start_up_time_; 177 base::TimeDelta time_deferred = base::Time::Now() - start_up_time_;
164 UMA_HISTOGRAM_TIMES("Sync.Startup.TimeDeferred", time_deferred); 178 UMA_HISTOGRAM_TIMES("Sync.Startup.TimeDeferred", time_deferred);
165 UMA_HISTOGRAM_ENUMERATION("Sync.Startup.DeferredInitTrigger", 179 UMA_HISTOGRAM_ENUMERATION("Sync.Startup.DeferredInitTrigger",
166 TRIGGER_FALLBACK_TIMER, 180 TRIGGER_FALLBACK_TIMER,
167 MAX_TRIGGER_VALUE); 181 MAX_TRIGGER_VALUE);
168 received_start_request_ = true; 182 received_start_request_ = true;
169 TryStart(); 183 TryStart();
170 } 184 }
171 185
172 std::string StartupController::GetBackendInitializationStateString() const { 186 std::string StartupController::GetBackendInitializationStateString() const {
173 if (!start_backend_time_.is_null()) 187 if (!start_backend_time_.is_null())
174 return "Started"; 188 return "Started";
175 else if (!start_up_time_.is_null()) 189 else if (!start_up_time_.is_null())
176 return "Deferred"; 190 return "Deferred";
177 else 191 else
178 return "Not started"; 192 return "Not started";
179 } 193 }
180 194
181 void StartupController::OnDataTypeRequestsSyncStartup(syncer::ModelType type) { 195 void StartupController::OnDataTypeRequestsSyncStartup(syncer::ModelType type) {
182 if (!CommandLine::ForCurrentProcess()->HasSwitch( 196 if (CommandLine::ForCurrentProcess()->HasSwitch(
183 switches::kSyncEnableDeferredStartup)) { 197 switches::kSyncDisableDeferredStartup)) {
184 DVLOG(2) << "Ignoring data type request for sync startup: " 198 DVLOG(2) << "Ignoring data type request for sync startup: "
185 << syncer::ModelTypeToString(type); 199 << syncer::ModelTypeToString(type);
186 return; 200 return;
187 } 201 }
188 202
189 if (!start_backend_time_.is_null()) 203 if (!start_backend_time_.is_null())
190 return; 204 return;
191 205
192 DVLOG(2) << "Data type requesting sync startup: " 206 DVLOG(2) << "Data type requesting sync startup: "
193 << syncer::ModelTypeToString(type); 207 << syncer::ModelTypeToString(type);
194 // Measure the time spent waiting for init and the type that triggered it. 208 // Measure the time spent waiting for init and the type that triggered it.
195 // We could measure the time spent deferred on a per-datatype basis, but 209 // We could measure the time spent deferred on a per-datatype basis, but
196 // for now this is probably sufficient. 210 // for now this is probably sufficient.
197 if (!start_up_time_.is_null()) { 211 if (!start_up_time_.is_null()) {
198 base::TimeDelta time_deferred = base::Time::Now() - start_up_time_; 212 base::TimeDelta time_deferred = base::Time::Now() - start_up_time_;
199 UMA_HISTOGRAM_TIMES("Sync.Startup.TimeDeferred", time_deferred); 213 UMA_HISTOGRAM_TIMES("Sync.Startup.TimeDeferred", time_deferred);
200 UMA_HISTOGRAM_ENUMERATION("Sync.Startup.TypeTriggeringInit", 214 UMA_HISTOGRAM_ENUMERATION("Sync.Startup.TypeTriggeringInit",
201 ModelTypeToHistogramInt(type), 215 ModelTypeToHistogramInt(type),
202 syncer::MODEL_TYPE_COUNT); 216 syncer::MODEL_TYPE_COUNT);
203 UMA_HISTOGRAM_ENUMERATION("Sync.Startup.DeferredInitTrigger", 217 UMA_HISTOGRAM_ENUMERATION("Sync.Startup.DeferredInitTrigger",
204 TRIGGER_DATA_TYPE_REQUEST, 218 TRIGGER_DATA_TYPE_REQUEST,
205 MAX_TRIGGER_VALUE); 219 MAX_TRIGGER_VALUE);
206 } 220 }
207 received_start_request_ = true; 221 received_start_request_ = true;
208 TryStart(); 222 TryStart();
209 } 223 }
210 224
211 } // namespace browser_sync 225 } // namespace browser_sync
OLDNEW
« no previous file with comments | « chrome/android/host_driven_tests/SyncTest.py ('k') | chrome/browser/sync/startup_controller_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698