OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/sync_setup_flow.h" | 5 #include "chrome/browser/sync/sync_setup_flow.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 namespace { | 27 namespace { |
28 | 28 |
29 // Helper function to disable password sync. | 29 // Helper function to disable password sync. |
30 void DisablePasswordSync(ProfileSyncService* service) { | 30 void DisablePasswordSync(ProfileSyncService* service) { |
31 syncable::ModelTypeSet types; | 31 syncable::ModelTypeSet types; |
32 service->GetPreferredDataTypes(&types); | 32 service->GetPreferredDataTypes(&types); |
33 types.erase(syncable::PASSWORDS); | 33 types.erase(syncable::PASSWORDS); |
34 service->OnUserChoseDatatypes(false, types); | 34 service->OnUserChoseDatatypes(false, types); |
35 } | 35 } |
36 | 36 |
37 // Fills |args| for the enter passphrase screen. | |
38 void GetArgsForEnterPassphrase(bool tried_creating_explicit_passphrase, | |
39 bool tried_setting_explicit_passphrase, | |
40 DictionaryValue* args) { | |
41 args->SetBoolean("show_passphrase", true); | |
42 args->SetBoolean("passphrase_creation_rejected", | |
43 tried_creating_explicit_passphrase); | |
44 args->SetBoolean("passphrase_setting_rejected", | |
45 tried_setting_explicit_passphrase); | |
46 } | |
47 | |
48 // Returns the next step for the non-fatal error case. | 37 // Returns the next step for the non-fatal error case. |
49 SyncSetupWizard::State GetStepForNonFatalError(ProfileSyncService* service) { | 38 SyncSetupWizard::State GetStepForNonFatalError(ProfileSyncService* service) { |
50 // TODO(sync): Update this error handling to allow different platforms to | 39 // TODO(sync): Update this error handling to allow different platforms to |
51 // display the error appropriately (http://crbug.com/92722) instead of | 40 // display the error appropriately (http://crbug.com/92722) instead of |
52 // navigating to a LOGIN state that is not supported on every platform. | 41 // navigating to a LOGIN state that is not supported on every platform. |
53 if (service->IsPassphraseRequired()) { | 42 if (service->IsPassphraseRequired()) { |
54 if (service->IsUsingSecondaryPassphrase()) | 43 if (service->IsUsingSecondaryPassphrase()) |
55 return SyncSetupWizard::ENTER_PASSPHRASE; | 44 return SyncSetupWizard::ENTER_PASSPHRASE; |
56 return SyncSetupWizard::GetLoginState(); | 45 return SyncSetupWizard::GetLoginState(); |
57 } | 46 } |
(...skipping 24 matching lines...) Expand all Loading... |
82 flow_handler_->SetFlow(NULL); | 71 flow_handler_->SetFlow(NULL); |
83 } | 72 } |
84 | 73 |
85 // static | 74 // static |
86 SyncSetupFlow* SyncSetupFlow::Run(ProfileSyncService* service, | 75 SyncSetupFlow* SyncSetupFlow::Run(ProfileSyncService* service, |
87 SyncSetupFlowContainer* container, | 76 SyncSetupFlowContainer* container, |
88 SyncSetupWizard::State start, | 77 SyncSetupWizard::State start, |
89 SyncSetupWizard::State end) { | 78 SyncSetupWizard::State end) { |
90 if (start == SyncSetupWizard::NONFATAL_ERROR) | 79 if (start == SyncSetupWizard::NONFATAL_ERROR) |
91 start = GetStepForNonFatalError(service); | 80 start = GetStepForNonFatalError(service); |
92 | 81 return new SyncSetupFlow(start, end, container, service); |
93 DictionaryValue args; | |
94 if (start == SyncSetupWizard::GAIA_LOGIN) | |
95 SyncSetupFlow::GetArgsForGaiaLogin(service, &args); | |
96 else if (start == SyncSetupWizard::CONFIGURE) | |
97 SyncSetupFlow::GetArgsForConfigure(service, &args); | |
98 else if (start == SyncSetupWizard::ENTER_PASSPHRASE) | |
99 GetArgsForEnterPassphrase(false, false, &args); | |
100 | |
101 std::string json_args; | |
102 base::JSONWriter::Write(&args, false, &json_args); | |
103 | |
104 return new SyncSetupFlow(start, end, json_args, container, service); | |
105 } | 82 } |
106 | 83 |
107 // static | 84 // static |
108 void SyncSetupFlow::GetArgsForGaiaLogin(const ProfileSyncService* service, | 85 void SyncSetupFlow::GetArgsForGaiaLogin(const ProfileSyncService* service, |
109 DictionaryValue* args) { | 86 DictionaryValue* args) { |
110 const GoogleServiceAuthError& error = service->GetAuthError(); | 87 const GoogleServiceAuthError& error = service->GetAuthError(); |
111 if (!service->last_attempted_user_email().empty()) { | 88 if (!service->last_attempted_user_email().empty()) { |
112 args->SetString("user", service->last_attempted_user_email()); | 89 args->SetString("user", service->last_attempted_user_email()); |
113 args->SetInteger("error", error.state()); | 90 args->SetInteger("error", error.state()); |
114 args->SetBoolean("editable_user", true); | 91 args->SetBoolean("editable_user", true); |
115 } else { | 92 } else { |
116 string16 user; | 93 string16 user; |
117 if (!service->cros_user().empty()) | 94 if (!service->cros_user().empty()) |
118 user = UTF8ToUTF16(service->cros_user()); | 95 user = UTF8ToUTF16(service->cros_user()); |
119 else | 96 else |
120 user = service->GetAuthenticatedUsername(); | 97 user = service->GetAuthenticatedUsername(); |
121 args->SetString("user", user); | 98 args->SetString("user", user); |
122 args->SetInteger("error", 0); | 99 args->SetInteger("error", 0); |
123 args->SetBoolean("editable_user", user.empty()); | 100 args->SetBoolean("editable_user", user.empty()); |
124 } | 101 } |
125 | 102 |
126 args->SetString("captchaUrl", error.captcha().image_url.spec()); | 103 args->SetString("captchaUrl", error.captcha().image_url.spec()); |
127 } | 104 } |
128 | 105 |
129 // static | |
130 void SyncSetupFlow::GetArgsForConfigure(ProfileSyncService* service, | 106 void SyncSetupFlow::GetArgsForConfigure(ProfileSyncService* service, |
131 DictionaryValue* args) { | 107 DictionaryValue* args) { |
132 // The SYNC_EVERYTHING case will set this to true. | 108 // The SYNC_EVERYTHING case will set this to true. |
133 args->SetBoolean("showSyncEverythingPage", false); | 109 args->SetBoolean("showSyncEverythingPage", false); |
134 | 110 |
135 args->SetBoolean("keepEverythingSynced", | 111 args->SetBoolean("keepEverythingSynced", |
136 service->profile()->GetPrefs()->GetBoolean(prefs::kKeepEverythingSynced)); | 112 service->profile()->GetPrefs()->GetBoolean(prefs::kKeepEverythingSynced)); |
137 | 113 |
138 // Bookmarks, Preferences, and Themes are launched for good, there's no | 114 // Bookmarks, Preferences, and Themes are launched for good, there's no |
139 // going back now. Check if the other data types are registered though. | 115 // going back now. Check if the other data types are registered though. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 syncable::ModelTypeSet encrypted_types; | 156 syncable::ModelTypeSet encrypted_types; |
181 service->GetEncryptedDataTypes(&encrypted_types); | 157 service->GetEncryptedDataTypes(&encrypted_types); |
182 bool encrypt_all = | 158 bool encrypt_all = |
183 encrypted_types.upper_bound(syncable::PASSWORDS) != encrypted_types.end(); | 159 encrypted_types.upper_bound(syncable::PASSWORDS) != encrypted_types.end(); |
184 if (service->HasPendingEncryptedTypes()) | 160 if (service->HasPendingEncryptedTypes()) |
185 encrypt_all = true; | 161 encrypt_all = true; |
186 args->SetBoolean("encryptAllData", encrypt_all); | 162 args->SetBoolean("encryptAllData", encrypt_all); |
187 | 163 |
188 // Load the parameters for the encryption tab. | 164 // Load the parameters for the encryption tab. |
189 args->SetBoolean("usePassphrase", service->IsUsingSecondaryPassphrase()); | 165 args->SetBoolean("usePassphrase", service->IsUsingSecondaryPassphrase()); |
| 166 |
| 167 // Determine if we need a passphrase or not, and if so, prompt the user. |
| 168 if (service->IsPassphraseRequiredForDecryption() && |
| 169 (service->IsUsingSecondaryPassphrase() || cached_passphrase_.empty())) { |
| 170 // We need a passphrase, and either it's an explicit passphrase, or we |
| 171 // don't have a cached gaia passphrase, so we have to prompt the user. |
| 172 args->SetBoolean("show_passphrase", true); |
| 173 } |
| 174 args->SetBoolean("passphrase_creation_rejected", |
| 175 tried_creating_explicit_passphrase_); |
| 176 args->SetBoolean("passphrase_setting_rejected", |
| 177 tried_setting_explicit_passphrase_); |
190 } | 178 } |
191 | 179 |
192 bool SyncSetupFlow::AttachSyncSetupHandler(SyncSetupFlowHandler* handler) { | 180 bool SyncSetupFlow::AttachSyncSetupHandler(SyncSetupFlowHandler* handler) { |
193 if (flow_handler_) | 181 if (flow_handler_) |
194 return false; | 182 return false; |
195 | 183 |
196 flow_handler_ = handler; | 184 flow_handler_ = handler; |
197 handler->SetFlow(this); | 185 handler->SetFlow(this); |
198 ActivateState(current_state_); | 186 ActivateState(current_state_); |
199 return true; | 187 return true; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 } | 249 } |
262 | 250 |
263 service_->OnUserCancelledDialog(); | 251 service_->OnUserCancelledDialog(); |
264 delete this; | 252 delete this; |
265 } | 253 } |
266 | 254 |
267 void SyncSetupFlow::OnUserSubmittedAuth(const std::string& username, | 255 void SyncSetupFlow::OnUserSubmittedAuth(const std::string& username, |
268 const std::string& password, | 256 const std::string& password, |
269 const std::string& captcha, | 257 const std::string& captcha, |
270 const std::string& access_code) { | 258 const std::string& access_code) { |
| 259 cached_passphrase_ = password; |
271 service_->OnUserSubmittedAuth(username, password, captcha, access_code); | 260 service_->OnUserSubmittedAuth(username, password, captcha, access_code); |
272 } | 261 } |
273 | 262 |
274 void SyncSetupFlow::OnUserConfigured(const SyncConfiguration& configuration) { | 263 void SyncSetupFlow::OnUserConfigured(const SyncConfiguration& configuration) { |
275 // Go to the "loading..." screen. | 264 // Go to the "loading..." screen. |
276 Advance(SyncSetupWizard::SETTING_UP); | 265 Advance(SyncSetupWizard::SETTING_UP); |
277 | 266 |
278 // Note: encryption will not occur until OnUserChoseDatatypes is called. | 267 // Note: encryption will not occur until OnUserChoseDatatypes is called. |
279 syncable::ModelTypeSet encrypted_types; | 268 syncable::ModelTypeSet encrypted_types; |
280 if (configuration.encrypt_all) { | 269 if (configuration.encrypt_all) { |
281 // Encrypt all registered types. | 270 // Encrypt all registered types. |
282 service_->GetRegisteredDataTypes(&encrypted_types); | 271 service_->GetRegisteredDataTypes(&encrypted_types); |
283 } // Else we clear the pending types for encryption. | 272 } // Else we clear the pending types for encryption. |
284 service_->set_pending_types_for_encryption(encrypted_types); | 273 service_->set_pending_types_for_encryption(encrypted_types); |
285 | 274 |
286 if (!configuration.gaia_passphrase.empty()) { | 275 if (!configuration.gaia_passphrase.empty()) { |
287 // Caller passed a gaia passphrase. This is illegal if we are currently | 276 // Caller passed a gaia passphrase. This is illegal if we are currently |
288 // using a secondary passphrase. | 277 // using a secondary passphrase. |
289 DCHECK(!service_->IsUsingSecondaryPassphrase()); | 278 DCHECK(!service_->IsUsingSecondaryPassphrase()); |
290 service_->SetPassphrase(configuration.gaia_passphrase, false, false); | 279 service_->SetPassphrase(configuration.gaia_passphrase, false); |
| 280 } else if (!service_->IsUsingSecondaryPassphrase() && |
| 281 !cached_passphrase_.empty()) { |
| 282 // Service needs a GAIA passphrase and we have one cached, so try it. |
| 283 service_->SetPassphrase(cached_passphrase_, false); |
| 284 cached_passphrase_.clear(); |
| 285 } else { |
| 286 // No gaia passphrase cached or set, so make sure the ProfileSyncService |
| 287 // wasn't expecting one. |
| 288 DCHECK(!service_->IsPassphraseRequiredForDecryption() || |
| 289 service_->IsUsingSecondaryPassphrase()); |
291 } | 290 } |
292 | 291 |
293 // It's possible the user has to provide a secondary passphrase even when | 292 // It's possible the user has to provide a secondary passphrase even when |
294 // they have not set one previously. This occurs when the user has changed | 293 // they have not set one previously. This occurs when the user has changed |
295 // their gaia password and then sign in to a new machine for the first time. | 294 // their gaia password and then sign in to a new machine for the first time. |
296 // The new machine will download data encrypted with their old gaia password, | 295 // The new machine will download data encrypted with their old gaia password, |
297 // which their current gaia password will not be able to decrypt, triggering | 296 // which their current gaia password will not be able to decrypt, triggering |
298 // a prompt for a passphrase. At this point, the user must enter their old | 297 // a prompt for a passphrase. At this point, the user must enter their old |
299 // password, which we store as a new secondary passphrase. | 298 // password, which we store as a new secondary passphrase. |
300 // TODO(zea): eventually use the above gaia_passphrase instead of the | 299 // TODO(zea): eventually use the above gaia_passphrase instead of the |
301 // secondary passphrase in this case. | 300 // secondary passphrase in this case. |
302 if (configuration.use_secondary_passphrase) { | 301 if (configuration.use_secondary_passphrase) { |
303 if (!service_->IsUsingSecondaryPassphrase()) { | 302 service_->SetPassphrase(configuration.secondary_passphrase, true); |
304 service_->SetPassphrase(configuration.secondary_passphrase, true, true); | 303 if (service_->IsUsingSecondaryPassphrase()) |
| 304 tried_setting_explicit_passphrase_ = true; |
| 305 else |
305 tried_creating_explicit_passphrase_ = true; | 306 tried_creating_explicit_passphrase_ = true; |
306 } else { | |
307 service_->SetPassphrase(configuration.secondary_passphrase, true, false); | |
308 tried_setting_explicit_passphrase_ = true; | |
309 } | |
310 } | 307 } |
311 | 308 |
312 service_->OnUserChoseDatatypes(configuration.sync_everything, | 309 service_->OnUserChoseDatatypes(configuration.sync_everything, |
313 configuration.data_types); | 310 configuration.data_types); |
314 } | 311 } |
315 | 312 |
316 void SyncSetupFlow::OnPassphraseEntry(const std::string& passphrase) { | 313 void SyncSetupFlow::OnPassphraseEntry(const std::string& passphrase) { |
317 Advance(SyncSetupWizard::SETTING_UP); | 314 Advance(SyncSetupWizard::SETTING_UP); |
318 service_->SetPassphrase(passphrase, true, false); | 315 service_->SetPassphrase(passphrase, true); |
319 tried_setting_explicit_passphrase_ = true; | 316 tried_setting_explicit_passphrase_ = true; |
320 } | 317 } |
321 | 318 |
322 void SyncSetupFlow::OnPassphraseCancel() { | 319 void SyncSetupFlow::OnPassphraseCancel() { |
323 // If the user cancels when being asked for the passphrase, | 320 // If the user cancels when being asked for the passphrase, |
324 // just disable encrypted sync and continue setting up. | 321 // just disable encrypted sync and continue setting up. |
325 if (current_state_ == SyncSetupWizard::ENTER_PASSPHRASE) | 322 if (current_state_ == SyncSetupWizard::ENTER_PASSPHRASE) |
326 DisablePasswordSync(service_); | 323 DisablePasswordSync(service_); |
327 | 324 |
328 Advance(SyncSetupWizard::SETTING_UP); | 325 Advance(SyncSetupWizard::SETTING_UP); |
329 } | 326 } |
330 | 327 |
331 // Use static Run method to get an instance. | 328 // Use static Run method to get an instance. |
332 SyncSetupFlow::SyncSetupFlow(SyncSetupWizard::State start_state, | 329 SyncSetupFlow::SyncSetupFlow(SyncSetupWizard::State start_state, |
333 SyncSetupWizard::State end_state, | 330 SyncSetupWizard::State end_state, |
334 const std::string& args, | |
335 SyncSetupFlowContainer* container, | 331 SyncSetupFlowContainer* container, |
336 ProfileSyncService* service) | 332 ProfileSyncService* service) |
337 : container_(container), | 333 : container_(container), |
338 dialog_start_args_(args), | |
339 current_state_(start_state), | 334 current_state_(start_state), |
340 end_state_(end_state), | 335 end_state_(end_state), |
341 login_start_time_(base::TimeTicks::Now()), | 336 login_start_time_(base::TimeTicks::Now()), |
342 flow_handler_(NULL), | 337 flow_handler_(NULL), |
343 service_(service), | 338 service_(service), |
344 tried_creating_explicit_passphrase_(false), | 339 tried_creating_explicit_passphrase_(false), |
345 tried_setting_explicit_passphrase_(false) { | 340 tried_setting_explicit_passphrase_(false) { |
346 } | 341 } |
347 | 342 |
348 // Returns true if the flow should advance to |state| based on |current_state_|. | 343 // Returns true if the flow should advance to |state| based on |current_state_|. |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 } | 414 } |
420 case SyncSetupWizard::CONFIGURE: { | 415 case SyncSetupWizard::CONFIGURE: { |
421 DictionaryValue args; | 416 DictionaryValue args; |
422 SyncSetupFlow::GetArgsForConfigure(service_, &args); | 417 SyncSetupFlow::GetArgsForConfigure(service_, &args); |
423 flow_handler_->ShowConfigure(args); | 418 flow_handler_->ShowConfigure(args); |
424 break; | 419 break; |
425 } | 420 } |
426 case SyncSetupWizard::ENTER_PASSPHRASE: { | 421 case SyncSetupWizard::ENTER_PASSPHRASE: { |
427 DictionaryValue args; | 422 DictionaryValue args; |
428 SyncSetupFlow::GetArgsForConfigure(service_, &args); | 423 SyncSetupFlow::GetArgsForConfigure(service_, &args); |
429 GetArgsForEnterPassphrase(tried_creating_explicit_passphrase_, | 424 GetArgsForConfigure(service_, &args); |
430 tried_setting_explicit_passphrase_, | 425 // TODO(atwilson): Remove ShowPassphraseEntry in favor of using |
431 &args); | 426 // ShowConfigure() - http://crbug.com/90786. |
432 flow_handler_->ShowPassphraseEntry(args); | 427 flow_handler_->ShowPassphraseEntry(args); |
433 break; | 428 break; |
434 } | 429 } |
435 case SyncSetupWizard::SETUP_ABORTED_BY_PENDING_CLEAR: { | 430 case SyncSetupWizard::SETUP_ABORTED_BY_PENDING_CLEAR: { |
436 DictionaryValue args; | 431 DictionaryValue args; |
437 SyncSetupFlow::GetArgsForConfigure(service_, &args); | 432 SyncSetupFlow::GetArgsForConfigure(service_, &args); |
438 args.SetBoolean("was_aborted", true); | 433 args.SetBoolean("was_aborted", true); |
439 flow_handler_->ShowConfigure(args); | 434 flow_handler_->ShowConfigure(args); |
440 break; | 435 break; |
441 } | 436 } |
(...skipping 12 matching lines...) Expand all Loading... |
454 break; | 449 break; |
455 } | 450 } |
456 case SyncSetupWizard::DONE: | 451 case SyncSetupWizard::DONE: |
457 flow_handler_->ShowSetupDone( | 452 flow_handler_->ShowSetupDone( |
458 UTF16ToWide(service_->GetAuthenticatedUsername())); | 453 UTF16ToWide(service_->GetAuthenticatedUsername())); |
459 break; | 454 break; |
460 default: | 455 default: |
461 NOTREACHED() << "Invalid advance state: " << state; | 456 NOTREACHED() << "Invalid advance state: " << state; |
462 } | 457 } |
463 } | 458 } |
OLD | NEW |