OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/ui/webui/sync_setup_handler.h" | 5 #include "chrome/browser/ui/webui/sync_setup_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.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" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/google/google_util.h" | 13 #include "chrome/browser/google/google_util.h" |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/profiles/profile_info_cache.h" | 15 #include "chrome/browser/profiles/profile_info_cache.h" |
16 #include "chrome/browser/profiles/profile_manager.h" | 16 #include "chrome/browser/profiles/profile_manager.h" |
17 #include "chrome/browser/profiles/profile_metrics.h" | 17 #include "chrome/browser/profiles/profile_metrics.h" |
18 #include "chrome/browser/signin/signin_manager.h" | 18 #include "chrome/browser/signin/signin_manager.h" |
| 19 #include "chrome/browser/signin/signin_manager_factory.h" |
19 #include "chrome/browser/sync/profile_sync_service.h" | 20 #include "chrome/browser/sync/profile_sync_service.h" |
20 #include "chrome/browser/sync/profile_sync_service_factory.h" | 21 #include "chrome/browser/sync/profile_sync_service_factory.h" |
21 #include "chrome/browser/sync/protocol/service_constants.h" | 22 #include "chrome/browser/sync/protocol/service_constants.h" |
22 #include "chrome/browser/sync/sync_setup_flow.h" | 23 #include "chrome/browser/sync/sync_setup_flow.h" |
23 #include "chrome/browser/sync/util/oauth.h" | 24 #include "chrome/browser/sync/util/oauth.h" |
24 #include "chrome/browser/ui/browser_list.h" | 25 #include "chrome/browser/ui/browser_list.h" |
| 26 #include "chrome/browser/ui/webui/signin/login_ui_service.h" |
| 27 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" |
25 #include "chrome/browser/ui/webui/sync_promo/sync_promo_ui.h" | 28 #include "chrome/browser/ui/webui/sync_promo/sync_promo_ui.h" |
26 #include "chrome/common/net/gaia/gaia_constants.h" | 29 #include "chrome/common/net/gaia/gaia_constants.h" |
27 #include "chrome/common/url_constants.h" | 30 #include "chrome/common/url_constants.h" |
28 #include "content/browser/renderer_host/render_view_host.h" | 31 #include "content/browser/renderer_host/render_view_host.h" |
29 #include "content/public/browser/render_view_host_delegate.h" | 32 #include "content/public/browser/render_view_host_delegate.h" |
30 #include "content/public/browser/web_contents.h" | 33 #include "content/public/browser/web_contents.h" |
31 #include "grit/chromium_strings.h" | 34 #include "grit/chromium_strings.h" |
32 #include "grit/generated_resources.h" | 35 #include "grit/generated_resources.h" |
33 #include "grit/locale_settings.h" | 36 #include "grit/locale_settings.h" |
34 #include "ui/base/l10n/l10n_util.h" | 37 #include "ui/base/l10n/l10n_util.h" |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 } | 178 } |
176 | 179 |
177 } // namespace | 180 } // namespace |
178 | 181 |
179 SyncSetupHandler::SyncSetupHandler(ProfileManager* profile_manager) | 182 SyncSetupHandler::SyncSetupHandler(ProfileManager* profile_manager) |
180 : flow_(NULL), | 183 : flow_(NULL), |
181 profile_manager_(profile_manager) { | 184 profile_manager_(profile_manager) { |
182 } | 185 } |
183 | 186 |
184 SyncSetupHandler::~SyncSetupHandler() { | 187 SyncSetupHandler::~SyncSetupHandler() { |
| 188 // Just exit if running unit tests (no actual WebUI is attached). |
| 189 if (!web_ui()) |
| 190 return; |
| 191 |
185 // This case is hit when the user performs a back navigation. | 192 // This case is hit when the user performs a back navigation. |
186 if (flow_) | 193 CloseSyncSetup(); |
187 flow_->OnDialogClosed(""); | |
188 } | 194 } |
189 | 195 |
190 void SyncSetupHandler::GetLocalizedValues(DictionaryValue* localized_strings) { | 196 void SyncSetupHandler::GetLocalizedValues(DictionaryValue* localized_strings) { |
191 GetStaticLocalizedValues(localized_strings, web_ui()); | 197 GetStaticLocalizedValues(localized_strings, web_ui()); |
192 } | 198 } |
193 | 199 |
194 void SyncSetupHandler::GetStaticLocalizedValues( | 200 void SyncSetupHandler::GetStaticLocalizedValues( |
195 DictionaryValue* localized_strings, | 201 DictionaryValue* localized_strings, |
196 content::WebUI* web_ui) { | 202 content::WebUI* web_ui) { |
197 DCHECK(localized_strings); | 203 DCHECK(localized_strings); |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 { "promoVerboseServicesBody", IDS_SYNC_PROMO_V_SERVICES_BODY }, | 348 { "promoVerboseServicesBody", IDS_SYNC_PROMO_V_SERVICES_BODY }, |
343 { "promoVerboseSignUp", IDS_SYNC_PROMO_V_SIGN_UP }, | 349 { "promoVerboseSignUp", IDS_SYNC_PROMO_V_SIGN_UP }, |
344 { "promoTitleShort", IDS_SYNC_PROMO_MESSAGE_TITLE_SHORT }, | 350 { "promoTitleShort", IDS_SYNC_PROMO_MESSAGE_TITLE_SHORT }, |
345 { "promoMessageBody", IDS_SYNC_PROMO_MESSAGE_BODY }, | 351 { "promoMessageBody", IDS_SYNC_PROMO_MESSAGE_BODY }, |
346 }; | 352 }; |
347 | 353 |
348 RegisterStrings(localized_strings, resources, arraysize(resources)); | 354 RegisterStrings(localized_strings, resources, arraysize(resources)); |
349 RegisterTitle(localized_strings, "syncSetupOverlay", IDS_SYNC_SETUP_TITLE); | 355 RegisterTitle(localized_strings, "syncSetupOverlay", IDS_SYNC_SETUP_TITLE); |
350 } | 356 } |
351 | 357 |
352 void SyncSetupHandler::Initialize() { | 358 void SyncSetupHandler::StartConfigureSync() { |
| 359 DCHECK(!flow_); |
| 360 // We only get here if we're signed in, so no longer need our SigninTracker. |
| 361 signin_tracker_.reset(); |
| 362 ProfileSyncService* service = GetSyncService(); |
| 363 service->get_wizard().Step( |
| 364 service->HasSyncSetupCompleted() ? |
| 365 SyncSetupWizard::CONFIGURE : SyncSetupWizard::SYNC_EVERYTHING); |
| 366 |
| 367 // Attach this as the sync setup handler. |
| 368 if (!service->get_wizard().AttachSyncSetupHandler(this)) { |
| 369 LOG(ERROR) << "SyncSetupHandler attach failed!"; |
| 370 CloseOverlay(); |
| 371 } |
| 372 } |
| 373 |
| 374 bool SyncSetupHandler::IsActiveLogin() const { |
| 375 LoginUIService* service = GetLoginUIService(); |
| 376 return service->current_login_ui() == web_ui(); |
353 } | 377 } |
354 | 378 |
355 void SyncSetupHandler::OnGetOAuthTokenSuccess(const std::string& oauth_token) { | 379 void SyncSetupHandler::OnGetOAuthTokenSuccess(const std::string& oauth_token) { |
356 flow_->OnUserSubmittedOAuth(oauth_token); | 380 Profile* profile = Profile::FromWebUI(web_ui()); |
| 381 SigninManager* signin = GetSignin(); |
| 382 GaiaOAuthFetcher* fetcher = new GaiaOAuthFetcher( |
| 383 signin, |
| 384 profile->GetRequestContext(), |
| 385 profile, |
| 386 GaiaConstants::kSyncServiceOAuth); |
| 387 signin->StartOAuthSignIn(oauth_token, fetcher); |
357 } | 388 } |
358 | 389 |
359 void SyncSetupHandler::OnGetOAuthTokenFailure( | 390 void SyncSetupHandler::OnGetOAuthTokenFailure( |
360 const GoogleServiceAuthError& error) { | 391 const GoogleServiceAuthError& error) { |
361 CloseSyncSetup(); | 392 CloseOverlay(); |
362 } | 393 } |
363 | 394 |
364 void SyncSetupHandler::RegisterMessages() { | 395 void SyncSetupHandler::RegisterMessages() { |
365 web_ui()->RegisterMessageCallback("SyncSetupDidClosePage", | 396 web_ui()->RegisterMessageCallback("SyncSetupDidClosePage", |
366 base::Bind(&SyncSetupHandler::OnDidClosePage, | 397 base::Bind(&SyncSetupHandler::OnDidClosePage, |
367 base::Unretained(this))); | 398 base::Unretained(this))); |
368 web_ui()->RegisterMessageCallback("SyncSetupSubmitAuth", | 399 web_ui()->RegisterMessageCallback("SyncSetupSubmitAuth", |
369 base::Bind(&SyncSetupHandler::HandleSubmitAuth, | 400 base::Bind(&SyncSetupHandler::HandleSubmitAuth, |
370 base::Unretained(this))); | 401 base::Unretained(this))); |
371 web_ui()->RegisterMessageCallback("SyncSetupConfigure", | 402 web_ui()->RegisterMessageCallback("SyncSetupConfigure", |
372 base::Bind(&SyncSetupHandler::HandleConfigure, | 403 base::Bind(&SyncSetupHandler::HandleConfigure, |
373 base::Unretained(this))); | 404 base::Unretained(this))); |
374 web_ui()->RegisterMessageCallback("SyncSetupPassphrase", | 405 web_ui()->RegisterMessageCallback("SyncSetupPassphrase", |
375 base::Bind(&SyncSetupHandler::HandlePassphraseEntry, | 406 base::Bind(&SyncSetupHandler::HandlePassphraseEntry, |
376 base::Unretained(this))); | 407 base::Unretained(this))); |
377 web_ui()->RegisterMessageCallback("SyncSetupPassphraseCancel", | 408 web_ui()->RegisterMessageCallback("SyncSetupPassphraseCancel", |
378 base::Bind(&SyncSetupHandler::HandlePassphraseCancel, | 409 base::Bind(&SyncSetupHandler::HandlePassphraseCancel, |
379 base::Unretained(this))); | 410 base::Unretained(this))); |
380 web_ui()->RegisterMessageCallback("SyncSetupAttachHandler", | 411 web_ui()->RegisterMessageCallback("SyncSetupAttachHandler", |
381 base::Bind(&SyncSetupHandler::HandleAttachHandler, | 412 base::Bind(&SyncSetupHandler::HandleAttachHandler, |
382 base::Unretained(this))); | 413 base::Unretained(this))); |
383 web_ui()->RegisterMessageCallback("SyncSetupShowErrorUI", | 414 web_ui()->RegisterMessageCallback("SyncSetupShowErrorUI", |
384 base::Bind(&SyncSetupHandler::HandleShowErrorUI, | 415 base::Bind(&SyncSetupHandler::HandleShowErrorUI, |
385 base::Unretained(this))); | 416 base::Unretained(this))); |
386 web_ui()->RegisterMessageCallback("SyncSetupShowSetupUI", | 417 web_ui()->RegisterMessageCallback("SyncSetupShowSetupUI", |
387 base::Bind(&SyncSetupHandler::HandleShowSetupUI, | 418 base::Bind(&SyncSetupHandler::HandleShowSetupUI, |
388 base::Unretained(this))); | 419 base::Unretained(this))); |
389 } | 420 } |
390 | 421 |
391 // Ideal(?) solution here would be to mimic the ClientLogin overlay. Since | 422 SigninManager* SyncSetupHandler::GetSignin() const { |
392 // this UI must render an external URL, that overlay cannot be used directly. | |
393 // The current implementation is functional, but fails asthetically. | |
394 // TODO(rickcam): Bug 90711: Update UI for OAuth sign-in flow | |
395 void SyncSetupHandler::ShowOAuthLogin() { | |
396 DCHECK(browser_sync::IsUsingOAuth()); | |
397 | |
398 Profile* profile = Profile::FromWebUI(web_ui()); | 423 Profile* profile = Profile::FromWebUI(web_ui()); |
399 oauth_login_.reset(new GaiaOAuthFetcher(this, | 424 return SigninManagerFactory::GetForProfile(profile); |
400 profile->GetRequestContext(), | |
401 profile, | |
402 GaiaConstants::kSyncServiceOAuth)); | |
403 oauth_login_->SetAutoFetchLimit(GaiaOAuthFetcher::OAUTH1_REQUEST_TOKEN); | |
404 oauth_login_->StartGetOAuthToken(); | |
405 } | 425 } |
406 | 426 |
407 void SyncSetupHandler::ShowGaiaLogin(const DictionaryValue& args) { | 427 void SyncSetupHandler::DisplayGaiaLogin(bool fatal_error) { |
| 428 DisplayGaiaLoginWithErrorMessage(string16(), fatal_error); |
| 429 } |
| 430 |
| 431 void SyncSetupHandler::DisplayGaiaLoginWithErrorMessage( |
| 432 const string16& error_message, bool fatal_error) { |
408 DCHECK(!browser_sync::IsUsingOAuth()); | 433 DCHECK(!browser_sync::IsUsingOAuth()); |
| 434 // If we're exiting from sync config (due to some kind of error), notify |
| 435 // SyncSetupFlow. |
| 436 if (flow_) { |
| 437 flow_->OnDialogClosed(std::string()); |
| 438 flow_ = NULL; |
| 439 } |
| 440 |
| 441 // Setup args for the GAIA login screen: |
| 442 // error_message: custom error message to display |
| 443 // error: GoogleServiceAuthError from previous login attempt (0 if none) |
| 444 // user: The email the user most recently entered. |
| 445 // editable_user: Whether the username field should be editable. |
| 446 SigninManager* signin = GetSignin(); |
| 447 std::string user; |
| 448 int error; |
| 449 bool editable_user; |
| 450 if (!last_attempted_user_email_.empty()) { |
| 451 // This is a repeat of a login attempt. |
| 452 user = last_attempted_user_email_; |
| 453 error = signin->GetLoginAuthError().state(); |
| 454 editable_user = true; |
| 455 } else { |
| 456 // Fresh login attempt - lock in the authenticated username if there is |
| 457 // one (don't let the user change it). |
| 458 user = signin->GetAuthenticatedUsername(); |
| 459 error = 0; |
| 460 editable_user = user.empty(); |
| 461 } |
| 462 DictionaryValue args; |
| 463 args.SetString("user", user); |
| 464 args.SetInteger("error", error); |
| 465 args.SetBoolean("editable_user", editable_user); |
| 466 if (!error_message.empty()) |
| 467 args.SetString("error_message", error_message); |
| 468 if (fatal_error) |
| 469 args.SetBoolean("fatalError", true); |
409 StringValue page("login"); | 470 StringValue page("login"); |
410 web_ui()->CallJavascriptFunction( | 471 web_ui()->CallJavascriptFunction( |
411 "SyncSetupOverlay.showSyncSetupPage", page, args); | 472 "SyncSetupOverlay.showSyncSetupPage", page, args); |
412 } | 473 } |
413 | 474 |
414 void SyncSetupHandler::ShowGaiaSuccessAndClose() { | 475 void SyncSetupHandler::RecordSignin() { |
| 476 // By default, do nothing - subclasses can override. |
| 477 } |
| 478 |
| 479 void SyncSetupHandler::DisplayGaiaSuccessAndClose() { |
| 480 // TODO(atwilson): Can we remove this now that we've changed the signin flow? |
| 481 RecordSignin(); |
415 web_ui()->CallJavascriptFunction("SyncSetupOverlay.showSuccessAndClose"); | 482 web_ui()->CallJavascriptFunction("SyncSetupOverlay.showSuccessAndClose"); |
416 } | 483 } |
417 | 484 |
418 void SyncSetupHandler::ShowGaiaSuccessAndSettingUp() { | 485 void SyncSetupHandler::DisplayGaiaSuccessAndSettingUp() { |
| 486 RecordSignin(); |
419 web_ui()->CallJavascriptFunction("SyncSetupOverlay.showSuccessAndSettingUp"); | 487 web_ui()->CallJavascriptFunction("SyncSetupOverlay.showSuccessAndSettingUp"); |
420 } | 488 } |
421 | 489 |
| 490 void SyncSetupHandler::ShowFatalError() { |
| 491 // For now, just send the user back to the login page. Ultimately we may want |
| 492 // to give different feedback (especially for chromeos). |
| 493 DisplayGaiaLogin(true); |
| 494 } |
| 495 |
422 void SyncSetupHandler::ShowConfigure(const DictionaryValue& args) { | 496 void SyncSetupHandler::ShowConfigure(const DictionaryValue& args) { |
423 StringValue page("configure"); | 497 StringValue page("configure"); |
424 web_ui()->CallJavascriptFunction( | 498 web_ui()->CallJavascriptFunction( |
425 "SyncSetupOverlay.showSyncSetupPage", page, args); | 499 "SyncSetupOverlay.showSyncSetupPage", page, args); |
426 } | 500 } |
427 | 501 |
428 void SyncSetupHandler::ShowPassphraseEntry(const DictionaryValue& args) { | 502 void SyncSetupHandler::ShowPassphraseEntry(const DictionaryValue& args) { |
429 StringValue page("passphrase"); | 503 StringValue page("passphrase"); |
430 web_ui()->CallJavascriptFunction( | 504 web_ui()->CallJavascriptFunction( |
431 "SyncSetupOverlay.showSyncSetupPage", page, args); | 505 "SyncSetupOverlay.showSyncSetupPage", page, args); |
432 } | 506 } |
433 | 507 |
434 void SyncSetupHandler::ShowSettingUp() { | 508 void SyncSetupHandler::ShowSettingUp() { |
435 StringValue page("settingUp"); | 509 StringValue page("settingUp"); |
436 web_ui()->CallJavascriptFunction( | 510 web_ui()->CallJavascriptFunction( |
437 "SyncSetupOverlay.showSyncSetupPage", page); | 511 "SyncSetupOverlay.showSyncSetupPage", page); |
438 } | 512 } |
439 | 513 |
440 void SyncSetupHandler::ShowSetupDone(const string16& user) { | 514 void SyncSetupHandler::ShowSetupDone(const string16& user) { |
441 StringValue page("done"); | 515 StringValue page("done"); |
442 web_ui()->CallJavascriptFunction( | 516 web_ui()->CallJavascriptFunction( |
443 "SyncSetupOverlay.showSyncSetupPage", page); | 517 "SyncSetupOverlay.showSyncSetupPage", page); |
444 | 518 |
445 // Suppress the sync promo once the user signs into sync. This way the user | 519 // Suppress the sync promo once the user signs into sync. This way the user |
446 // doesn't see the sync promo even if they sign out of sync later on. | 520 // doesn't see the sync promo even if they sign out of sync later on. |
447 SyncPromoUI::SetUserSkippedSyncPromo(Profile::FromWebUI(web_ui())); | 521 SyncPromoUI::SetUserSkippedSyncPromo(Profile::FromWebUI(web_ui())); |
448 | 522 |
449 Profile* profile = Profile::FromWebUI(web_ui()); | 523 Profile* profile = Profile::FromWebUI(web_ui()); |
450 ProfileSyncService* service = | 524 ProfileSyncService* service = GetSyncService(); |
451 ProfileSyncServiceFactory::GetInstance()->GetForProfile( | |
452 profile); | |
453 if (!service->HasSyncSetupCompleted()) { | 525 if (!service->HasSyncSetupCompleted()) { |
454 FilePath profile_file_path = profile->GetPath(); | 526 FilePath profile_file_path = profile->GetPath(); |
455 ProfileMetrics::LogProfileSyncSignIn(profile_file_path); | 527 ProfileMetrics::LogProfileSyncSignIn(profile_file_path); |
456 } | 528 } |
457 } | 529 } |
458 | 530 |
459 void SyncSetupHandler::SetFlow(SyncSetupFlow* flow) { | 531 void SyncSetupHandler::SetFlow(SyncSetupFlow* flow) { |
460 flow_ = flow; | 532 flow_ = flow; |
461 } | 533 } |
462 | 534 |
(...skipping 18 matching lines...) Expand all Loading... |
481 std::string username, password, captcha, access_code; | 553 std::string username, password, captcha, access_code; |
482 if (!GetAuthData(json, &username, &password, &captcha, &access_code)) { | 554 if (!GetAuthData(json, &username, &password, &captcha, &access_code)) { |
483 // The page sent us something that we didn't understand. | 555 // The page sent us something that we didn't understand. |
484 // This probably indicates a programming error. | 556 // This probably indicates a programming error. |
485 NOTREACHED(); | 557 NOTREACHED(); |
486 return; | 558 return; |
487 } | 559 } |
488 | 560 |
489 string16 error_message; | 561 string16 error_message; |
490 if (!IsLoginAuthDataValid(username, &error_message)) { | 562 if (!IsLoginAuthDataValid(username, &error_message)) { |
491 ShowLoginErrorMessage(error_message); | 563 DisplayGaiaLoginWithErrorMessage(error_message, false); |
492 return; | 564 return; |
493 } | 565 } |
494 | 566 |
495 if (flow_) | 567 TryLogin(username, password, captcha, access_code); |
496 flow_->OnUserSubmittedAuth(username, password, captcha, access_code); | 568 } |
| 569 |
| 570 void SyncSetupHandler::TryLogin(const std::string& username, |
| 571 const std::string& password, |
| 572 const std::string& captcha, |
| 573 const std::string& access_code) { |
| 574 DCHECK(IsActiveLogin()); |
| 575 // Make sure we are listening for signin traffic. |
| 576 if (!signin_tracker_.get()) |
| 577 signin_tracker_.reset(new SigninTracker(Profile::FromWebUI(web_ui()), |
| 578 this)); |
| 579 |
| 580 last_attempted_user_email_ = username; |
| 581 // If we're just being called to provide an ASP, then pass it to the |
| 582 // SigninManager and wait for the next step. |
| 583 SigninManager* signin = GetSignin(); |
| 584 if (!access_code.empty()) { |
| 585 signin->ProvideSecondFactorAccessCode(access_code); |
| 586 return; |
| 587 } |
| 588 |
| 589 // Kick off a sign-in through the signin manager. |
| 590 signin->StartSignIn(username, |
| 591 password, |
| 592 signin->GetLoginAuthError().captcha().token, |
| 593 captcha); |
| 594 } |
| 595 |
| 596 void SyncSetupHandler::GaiaCredentialsValid() { |
| 597 DCHECK(IsActiveLogin()); |
| 598 // Gaia credentials are valid - update the UI. |
| 599 DisplayGaiaSuccessAndSettingUp(); |
| 600 } |
| 601 |
| 602 void SyncSetupHandler::SigninFailed() { |
| 603 // Got a failed signin - this is either just a typical auth error, or a |
| 604 // sync error (treat sync errors as "fatal errors" - i.e. non-auth errors). |
| 605 DisplayGaiaLogin(GetSyncService()->unrecoverable_error_detected()); |
| 606 } |
| 607 |
| 608 ProfileSyncService* SyncSetupHandler::GetSyncService() const { |
| 609 return ProfileSyncServiceFactory::GetForProfile(Profile::FromWebUI(web_ui())); |
| 610 } |
| 611 |
| 612 void SyncSetupHandler::SigninSuccess() { |
| 613 DCHECK(GetSyncService()->sync_initialized()); |
| 614 StartConfigureSync(); |
497 } | 615 } |
498 | 616 |
499 void SyncSetupHandler::HandleConfigure(const ListValue* args) { | 617 void SyncSetupHandler::HandleConfigure(const ListValue* args) { |
500 std::string json; | 618 std::string json; |
501 if (!args->GetString(0, &json)) { | 619 if (!args->GetString(0, &json)) { |
502 NOTREACHED() << "Could not read JSON argument"; | 620 NOTREACHED() << "Could not read JSON argument"; |
503 return; | 621 return; |
504 } | 622 } |
505 if (json.empty()) { | 623 if (json.empty()) { |
506 NOTREACHED(); | 624 NOTREACHED(); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
556 flow_->OnPassphraseCancel(); | 674 flow_->OnPassphraseCancel(); |
557 } | 675 } |
558 | 676 |
559 void SyncSetupHandler::HandleAttachHandler(const ListValue* args) { | 677 void SyncSetupHandler::HandleAttachHandler(const ListValue* args) { |
560 OpenSyncSetup(); | 678 OpenSyncSetup(); |
561 } | 679 } |
562 | 680 |
563 void SyncSetupHandler::HandleShowErrorUI(const ListValue* args) { | 681 void SyncSetupHandler::HandleShowErrorUI(const ListValue* args) { |
564 DCHECK(!flow_); | 682 DCHECK(!flow_); |
565 | 683 |
566 Profile* profile = Profile::FromWebUI(web_ui()); | 684 ProfileSyncService* service = GetSyncService(); |
567 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> | |
568 GetForProfile(profile); | |
569 DCHECK(service); | 685 DCHECK(service); |
570 | 686 |
571 service->ShowErrorUI(); | 687 service->ShowErrorUI(); |
572 } | 688 } |
573 | 689 |
574 void SyncSetupHandler::HandleShowSetupUI(const ListValue* args) { | 690 void SyncSetupHandler::HandleShowSetupUI(const ListValue* args) { |
575 DCHECK(!flow_); | 691 DCHECK(!flow_); |
576 if (FocusExistingWizard()) { | 692 OpenSyncSetup(); |
| 693 } |
| 694 |
| 695 void SyncSetupHandler::CloseSyncSetup() { |
| 696 // TODO(atwilson): Move UMA tracking of signin events out of sync module. |
| 697 if (IsActiveLogin()) { |
| 698 if (signin_tracker_.get()) { |
| 699 ProfileSyncService::SyncEvent( |
| 700 ProfileSyncService::CANCEL_DURING_SIGNON); |
| 701 } else if (!flow_) { |
| 702 ProfileSyncService::SyncEvent( |
| 703 ProfileSyncService::CANCEL_FROM_SIGNON_WITHOUT_AUTH); |
| 704 } |
| 705 } |
| 706 |
| 707 if (flow_) { |
| 708 flow_->OnDialogClosed(std::string()); |
| 709 flow_ = NULL; |
| 710 } |
| 711 signin_tracker_.reset(); |
| 712 GetLoginUIService()->LoginUIClosed(web_ui()); |
| 713 } |
| 714 |
| 715 void SyncSetupHandler::OpenSyncSetup() { |
| 716 ProfileSyncService* service = GetSyncService(); |
| 717 if (!service) { |
| 718 // If there's no sync service, the user tried to manually invoke a syncSetup |
| 719 // URL, but sync features are disabled. We need to close the overlay for |
| 720 // this (rare) case. |
| 721 DLOG(WARNING) << "Closing sync UI because sync is disabled"; |
577 CloseOverlay(); | 722 CloseOverlay(); |
578 return; | 723 return; |
579 } | 724 } |
580 | 725 |
581 StepWizardForShowSetupUI(); | 726 // If the wizard is already visible, just focus that one. |
582 ShowSetupUI(); | 727 if (FocusExistingWizardIfPresent()) { |
583 } | 728 if (!IsActiveLogin()) |
584 | 729 CloseOverlay(); |
585 void SyncSetupHandler::CloseSyncSetup() { | |
586 if (flow_) { | |
587 flow_->OnDialogClosed(std::string()); | |
588 flow_ = NULL; | |
589 } | |
590 } | |
591 | |
592 void SyncSetupHandler::OpenSyncSetup() { | |
593 DCHECK(!flow_); | |
594 | |
595 Profile* profile = Profile::FromWebUI(web_ui()); | |
596 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> | |
597 GetForProfile(profile); | |
598 if (!service) { | |
599 // If there's no sync service, the user tried to manually invoke a syncSetup | |
600 // URL, but sync features are disabled. We need to close the overlay for | |
601 // this (rare) case. | |
602 CloseOverlay(); | |
603 return; | 730 return; |
604 } | 731 } |
605 | 732 |
606 // If the wizard is already visible, it must be attached to another flow | 733 GetLoginUIService()->SetLoginUI(web_ui()); |
607 // handler. | |
608 if (FocusExistingWizard()) { | |
609 CloseOverlay(); | |
610 return; | |
611 } | |
612 | 734 |
613 // The wizard must be stepped before attaching. Allow subclasses to step the | 735 if (!SigninTracker::AreServicesSignedIn(Profile::FromWebUI(web_ui()))) { |
614 // wizard to appropriate state. | 736 // User is not logged in - need to display login UI. |
615 StepWizardForShowSetupUI(); | 737 DisplayGaiaLogin(false); |
616 | 738 } else { |
617 // Attach this as the sync setup handler, before calling ShowSetupUI(). | 739 // User is already logged in. They must have brought up the config wizard |
618 if (!service->get_wizard().AttachSyncSetupHandler(this)) { | 740 // via the "Advanced..." button or the wrench menu. |
619 LOG(ERROR) << "SyncSetupHandler attach failed!"; | 741 StartConfigureSync(); |
620 CloseOverlay(); | |
621 return; | |
622 } | 742 } |
623 | 743 |
624 ShowSetupUI(); | 744 ShowSetupUI(); |
625 } | 745 } |
626 | 746 |
627 // Private member functions. | 747 // Private member functions. |
628 | 748 |
629 bool SyncSetupHandler::FocusExistingWizard() { | 749 bool SyncSetupHandler::FocusExistingWizardIfPresent() { |
630 Profile* profile = Profile::FromWebUI(web_ui()); | 750 LoginUIService* service = GetLoginUIService(); |
631 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> | 751 if (!service->current_login_ui()) |
632 GetForProfile(profile); | |
633 if (!service) | |
634 return false; | 752 return false; |
| 753 service->FocusLoginUI(); |
| 754 return true; |
| 755 } |
635 | 756 |
636 // If the wizard is already visible, focus it. | 757 LoginUIService* SyncSetupHandler::GetLoginUIService() const { |
637 if (service->get_wizard().IsVisible()) { | 758 return LoginUIServiceFactory::GetForProfile(Profile::FromWebUI(web_ui())); |
638 service->get_wizard().Focus(); | |
639 return true; | |
640 } | |
641 return false; | |
642 } | 759 } |
643 | 760 |
644 void SyncSetupHandler::CloseOverlay() { | 761 void SyncSetupHandler::CloseOverlay() { |
| 762 CloseSyncSetup(); |
645 web_ui()->CallJavascriptFunction("OptionsPage.closeOverlay"); | 763 web_ui()->CallJavascriptFunction("OptionsPage.closeOverlay"); |
646 } | 764 } |
647 | 765 |
648 bool SyncSetupHandler::IsLoginAuthDataValid(const std::string& username, | 766 bool SyncSetupHandler::IsLoginAuthDataValid(const std::string& username, |
649 string16* error_message) { | 767 string16* error_message) { |
650 // Happens during unit tests. | 768 // Happens during unit tests. |
651 if (!web_ui() || !profile_manager_) | 769 if (!web_ui() || !profile_manager_) |
652 return true; | 770 return true; |
653 | 771 |
654 if (username.empty()) | 772 if (username.empty()) |
655 return true; | 773 return true; |
656 | 774 |
657 // Check if the username is already in use by another profile. | 775 // Check if the username is already in use by another profile. |
658 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); | 776 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); |
659 size_t current_profile_index = cache.GetIndexOfProfileWithPath( | 777 size_t current_profile_index = cache.GetIndexOfProfileWithPath( |
660 Profile::FromWebUI(web_ui())->GetPath()); | 778 Profile::FromWebUI(web_ui())->GetPath()); |
661 string16 username_utf16 = UTF8ToUTF16(username); | 779 string16 username_utf16 = UTF8ToUTF16(username); |
662 | 780 |
663 for (size_t i = 0; i < cache.GetNumberOfProfiles(); ++i) { | 781 for (size_t i = 0; i < cache.GetNumberOfProfiles(); ++i) { |
664 if (i != current_profile_index && AreUserNamesEqual( | 782 if (i != current_profile_index && AreUserNamesEqual( |
665 cache.GetUserNameOfProfileAtIndex(i), username_utf16)) { | 783 cache.GetUserNameOfProfileAtIndex(i), username_utf16)) { |
666 *error_message = l10n_util::GetStringUTF16( | 784 *error_message = l10n_util::GetStringUTF16( |
667 IDS_SYNC_USER_NAME_IN_USE_ERROR); | 785 IDS_SYNC_USER_NAME_IN_USE_ERROR); |
668 return false; | 786 return false; |
669 } | 787 } |
670 } | 788 } |
671 | 789 |
672 return true; | 790 return true; |
673 } | 791 } |
674 | |
675 void SyncSetupHandler::ShowLoginErrorMessage(const string16& error_message) { | |
676 DCHECK(flow_); | |
677 DictionaryValue args; | |
678 flow_->GetArgsForGaiaLogin(&args); | |
679 args.SetString("error_message", error_message); | |
680 ShowGaiaLogin(args); | |
681 } | |
OLD | NEW |