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

Side by Side Diff: chrome/browser/ui/webui/sync_setup_handler.cc

Issue 9295044: Start moving signin code out of browser/sync. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed unit test Created 8 years, 10 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 (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
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 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 { "promoVerboseServicesBody", IDS_SYNC_PROMO_V_SERVICES_BODY }, 349 { "promoVerboseServicesBody", IDS_SYNC_PROMO_V_SERVICES_BODY },
344 { "promoVerboseSignUp", IDS_SYNC_PROMO_V_SIGN_UP }, 350 { "promoVerboseSignUp", IDS_SYNC_PROMO_V_SIGN_UP },
345 { "promoTitleShort", IDS_SYNC_PROMO_MESSAGE_TITLE_SHORT }, 351 { "promoTitleShort", IDS_SYNC_PROMO_MESSAGE_TITLE_SHORT },
346 { "promoMessageBody", IDS_SYNC_PROMO_MESSAGE_BODY }, 352 { "promoMessageBody", IDS_SYNC_PROMO_MESSAGE_BODY },
347 }; 353 };
348 354
349 RegisterStrings(localized_strings, resources, arraysize(resources)); 355 RegisterStrings(localized_strings, resources, arraysize(resources));
350 } 356 }
351 357
352 void SyncSetupHandler::Initialize() { 358 void SyncSetupHandler::Initialize() {
359 OptionsPageUIHandler::Initialize();
James Hawkins 2012/02/10 23:08:01 What is this part of the change about?
Andrew T Wilson (Slow) 2012/02/11 01:50:45 Ah, I was originally registering for some notifica
360 }
361
362 void SyncSetupHandler::StartConfigureSync() {
363 DCHECK(!flow_);
364 // We only get here if we're signed in, so no longer need our SigninTracker.
365 signin_tracker_.reset();
366 ProfileSyncService* service = GetSyncService();
367 service->get_wizard().Step(
368 service->HasSyncSetupCompleted() ?
369 SyncSetupWizard::CONFIGURE : SyncSetupWizard::SYNC_EVERYTHING);
370
371 // Attach this as the sync setup handler.
372 if (!service->get_wizard().AttachSyncSetupHandler(this)) {
373 LOG(ERROR) << "SyncSetupHandler attach failed!";
374 CloseOverlay();
375 }
376 }
377
378 bool SyncSetupHandler::IsActiveLogin() const {
379 LoginUIService* service = GetLoginUIService();
380 return service->current_login_ui() == web_ui();
353 } 381 }
354 382
355 void SyncSetupHandler::OnGetOAuthTokenSuccess(const std::string& oauth_token) { 383 void SyncSetupHandler::OnGetOAuthTokenSuccess(const std::string& oauth_token) {
356 flow_->OnUserSubmittedOAuth(oauth_token); 384 Profile* profile = Profile::FromWebUI(web_ui());
385 SigninManager* signin = GetSignin();
386 GaiaOAuthFetcher* fetcher = new GaiaOAuthFetcher(
387 signin,
388 profile->GetRequestContext(),
389 profile,
390 GaiaConstants::kSyncServiceOAuth);
391 signin->StartOAuthSignIn(oauth_token, fetcher);
357 } 392 }
358 393
359 void SyncSetupHandler::OnGetOAuthTokenFailure( 394 void SyncSetupHandler::OnGetOAuthTokenFailure(
360 const GoogleServiceAuthError& error) { 395 const GoogleServiceAuthError& error) {
361 CloseSyncSetup(); 396 CloseOverlay();
362 } 397 }
363 398
364 void SyncSetupHandler::RegisterMessages() { 399 void SyncSetupHandler::RegisterMessages() {
365 web_ui()->RegisterMessageCallback("SyncSetupDidClosePage", 400 web_ui()->RegisterMessageCallback("SyncSetupDidClosePage",
366 base::Bind(&SyncSetupHandler::OnDidClosePage, 401 base::Bind(&SyncSetupHandler::OnDidClosePage,
367 base::Unretained(this))); 402 base::Unretained(this)));
368 web_ui()->RegisterMessageCallback("SyncSetupSubmitAuth", 403 web_ui()->RegisterMessageCallback("SyncSetupSubmitAuth",
369 base::Bind(&SyncSetupHandler::HandleSubmitAuth, 404 base::Bind(&SyncSetupHandler::HandleSubmitAuth,
370 base::Unretained(this))); 405 base::Unretained(this)));
371 web_ui()->RegisterMessageCallback("SyncSetupConfigure", 406 web_ui()->RegisterMessageCallback("SyncSetupConfigure",
372 base::Bind(&SyncSetupHandler::HandleConfigure, 407 base::Bind(&SyncSetupHandler::HandleConfigure,
373 base::Unretained(this))); 408 base::Unretained(this)));
374 web_ui()->RegisterMessageCallback("SyncSetupPassphrase", 409 web_ui()->RegisterMessageCallback("SyncSetupPassphrase",
375 base::Bind(&SyncSetupHandler::HandlePassphraseEntry, 410 base::Bind(&SyncSetupHandler::HandlePassphraseEntry,
376 base::Unretained(this))); 411 base::Unretained(this)));
377 web_ui()->RegisterMessageCallback("SyncSetupPassphraseCancel", 412 web_ui()->RegisterMessageCallback("SyncSetupPassphraseCancel",
378 base::Bind(&SyncSetupHandler::HandlePassphraseCancel, 413 base::Bind(&SyncSetupHandler::HandlePassphraseCancel,
379 base::Unretained(this))); 414 base::Unretained(this)));
380 web_ui()->RegisterMessageCallback("SyncSetupAttachHandler", 415 web_ui()->RegisterMessageCallback("SyncSetupAttachHandler",
381 base::Bind(&SyncSetupHandler::HandleAttachHandler, 416 base::Bind(&SyncSetupHandler::HandleAttachHandler,
382 base::Unretained(this))); 417 base::Unretained(this)));
383 web_ui()->RegisterMessageCallback("SyncSetupShowErrorUI", 418 web_ui()->RegisterMessageCallback("SyncSetupShowErrorUI",
384 base::Bind(&SyncSetupHandler::HandleShowErrorUI, 419 base::Bind(&SyncSetupHandler::HandleShowErrorUI,
385 base::Unretained(this))); 420 base::Unretained(this)));
386 web_ui()->RegisterMessageCallback("SyncSetupShowSetupUI", 421 web_ui()->RegisterMessageCallback("SyncSetupShowSetupUI",
387 base::Bind(&SyncSetupHandler::HandleShowSetupUI, 422 base::Bind(&SyncSetupHandler::HandleShowSetupUI,
388 base::Unretained(this))); 423 base::Unretained(this)));
389 } 424 }
390 425
391 // Ideal(?) solution here would be to mimic the ClientLogin overlay. Since 426 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()); 427 Profile* profile = Profile::FromWebUI(web_ui());
399 oauth_login_.reset(new GaiaOAuthFetcher(this, 428 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 } 429 }
406 430
407 void SyncSetupHandler::ShowGaiaLogin(const DictionaryValue& args) { 431 void SyncSetupHandler::DisplayGaiaLogin(bool fatal_error) {
432 DisplayGaiaLoginWithErrorMessage(string16(), fatal_error);
433 }
434
435 void SyncSetupHandler::DisplayGaiaLoginWithErrorMessage(
436 const string16& error_message, bool fatal_error) {
408 DCHECK(!browser_sync::IsUsingOAuth()); 437 DCHECK(!browser_sync::IsUsingOAuth());
438 // If we're exiting from sync config (due to some kind of error), notify
439 // SyncSetupFlow.
440 if (flow_) {
441 flow_->OnDialogClosed(std::string());
442 flow_ = NULL;
443 }
444
445 // Setup args for the GAIA login screen:
446 // error_message: custom error message to display
447 // error: GoogleServiceAuthError from previous login attempt (0 if none)
448 // user: The email the user most recently entered.
449 // editable_user: Whether the username field should be editable.
450 SigninManager* signin = GetSignin();
451 std::string user;
452 int error;
453 bool editable_user;
454 if (!last_attempted_user_email_.empty()) {
455 // This is a repeat of a login attempt.
456 user = last_attempted_user_email_;
457 error = signin->GetLoginAuthError().state();
458 editable_user = true;
459 } else {
460 // Fresh login attempt - lock in the authenticated username if there is
461 // one (don't let the user change it).
462 user = signin->GetAuthenticatedUsername();
463 error = 0;
464 editable_user = user.empty();
465 }
466 DictionaryValue args;
467 args.SetString("user", user);
468 args.SetInteger("error", error);
469 args.SetBoolean("editable_user", editable_user);
470 if (!error_message.empty())
471 args.SetString("error_message", error_message);
472 if (fatal_error)
473 args.SetBoolean("fatalError", true);
409 StringValue page("login"); 474 StringValue page("login");
410 web_ui()->CallJavascriptFunction( 475 web_ui()->CallJavascriptFunction(
411 "SyncSetupOverlay.showSyncSetupPage", page, args); 476 "SyncSetupOverlay.showSyncSetupPage", page, args);
412 } 477 }
413 478
414 void SyncSetupHandler::ShowGaiaSuccessAndClose() { 479 void SyncSetupHandler::RecordSignin() {
480 // By default, do nothing - subclasses can override.
481 }
482
483 void SyncSetupHandler::DisplayGaiaSuccessAndClose() {
484 // TODO(atwilson): Can we remove this now that we've changed the signin flow?
485 RecordSignin();
415 web_ui()->CallJavascriptFunction("SyncSetupOverlay.showSuccessAndClose"); 486 web_ui()->CallJavascriptFunction("SyncSetupOverlay.showSuccessAndClose");
416 } 487 }
417 488
418 void SyncSetupHandler::ShowGaiaSuccessAndSettingUp() { 489 void SyncSetupHandler::DisplayGaiaSuccessAndSettingUp() {
490 RecordSignin();
419 web_ui()->CallJavascriptFunction("SyncSetupOverlay.showSuccessAndSettingUp"); 491 web_ui()->CallJavascriptFunction("SyncSetupOverlay.showSuccessAndSettingUp");
420 } 492 }
421 493
494 void SyncSetupHandler::ShowFatalError() {
495 // For now, just send the user back to the login page. Ultimately we may want
496 // to give different feedback (especially for chromeos).
497 DisplayGaiaLogin(true);
498 }
499
422 void SyncSetupHandler::ShowConfigure(const DictionaryValue& args) { 500 void SyncSetupHandler::ShowConfigure(const DictionaryValue& args) {
423 StringValue page("configure"); 501 StringValue page("configure");
424 web_ui()->CallJavascriptFunction( 502 web_ui()->CallJavascriptFunction(
425 "SyncSetupOverlay.showSyncSetupPage", page, args); 503 "SyncSetupOverlay.showSyncSetupPage", page, args);
426 } 504 }
427 505
428 void SyncSetupHandler::ShowPassphraseEntry(const DictionaryValue& args) { 506 void SyncSetupHandler::ShowPassphraseEntry(const DictionaryValue& args) {
429 StringValue page("passphrase"); 507 StringValue page("passphrase");
430 web_ui()->CallJavascriptFunction( 508 web_ui()->CallJavascriptFunction(
431 "SyncSetupOverlay.showSyncSetupPage", page, args); 509 "SyncSetupOverlay.showSyncSetupPage", page, args);
432 } 510 }
433 511
434 void SyncSetupHandler::ShowSettingUp() { 512 void SyncSetupHandler::ShowSettingUp() {
435 StringValue page("settingUp"); 513 StringValue page("settingUp");
436 web_ui()->CallJavascriptFunction( 514 web_ui()->CallJavascriptFunction(
437 "SyncSetupOverlay.showSyncSetupPage", page); 515 "SyncSetupOverlay.showSyncSetupPage", page);
438 } 516 }
439 517
440 void SyncSetupHandler::ShowSetupDone(const string16& user) { 518 void SyncSetupHandler::ShowSetupDone(const string16& user) {
441 StringValue page("done"); 519 StringValue page("done");
442 web_ui()->CallJavascriptFunction( 520 web_ui()->CallJavascriptFunction(
443 "SyncSetupOverlay.showSyncSetupPage", page); 521 "SyncSetupOverlay.showSyncSetupPage", page);
444 522
445 // Suppress the sync promo once the user signs into sync. This way the user 523 // 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. 524 // doesn't see the sync promo even if they sign out of sync later on.
447 SyncPromoUI::SetUserSkippedSyncPromo(Profile::FromWebUI(web_ui())); 525 SyncPromoUI::SetUserSkippedSyncPromo(Profile::FromWebUI(web_ui()));
448 526
449 Profile* profile = Profile::FromWebUI(web_ui()); 527 Profile* profile = Profile::FromWebUI(web_ui());
450 ProfileSyncService* service = 528 ProfileSyncService* service = GetSyncService();
451 ProfileSyncServiceFactory::GetInstance()->GetForProfile(
452 profile);
453 if (!service->HasSyncSetupCompleted()) { 529 if (!service->HasSyncSetupCompleted()) {
454 FilePath profile_file_path = profile->GetPath(); 530 FilePath profile_file_path = profile->GetPath();
455 ProfileMetrics::LogProfileSyncSignIn(profile_file_path); 531 ProfileMetrics::LogProfileSyncSignIn(profile_file_path);
456 } 532 }
457 } 533 }
458 534
459 void SyncSetupHandler::SetFlow(SyncSetupFlow* flow) { 535 void SyncSetupHandler::SetFlow(SyncSetupFlow* flow) {
460 flow_ = flow; 536 flow_ = flow;
461 } 537 }
462 538
(...skipping 18 matching lines...) Expand all
481 std::string username, password, captcha, access_code; 557 std::string username, password, captcha, access_code;
482 if (!GetAuthData(json, &username, &password, &captcha, &access_code)) { 558 if (!GetAuthData(json, &username, &password, &captcha, &access_code)) {
483 // The page sent us something that we didn't understand. 559 // The page sent us something that we didn't understand.
484 // This probably indicates a programming error. 560 // This probably indicates a programming error.
485 NOTREACHED(); 561 NOTREACHED();
486 return; 562 return;
487 } 563 }
488 564
489 string16 error_message; 565 string16 error_message;
490 if (!IsLoginAuthDataValid(username, &error_message)) { 566 if (!IsLoginAuthDataValid(username, &error_message)) {
491 ShowLoginErrorMessage(error_message); 567 DisplayGaiaLoginWithErrorMessage(error_message, false);
492 return; 568 return;
493 } 569 }
494 570
495 if (flow_) 571 TryLogin(username, password, captcha, access_code);
496 flow_->OnUserSubmittedAuth(username, password, captcha, access_code); 572 }
573
574 void SyncSetupHandler::TryLogin(const std::string& username,
575 const std::string& password,
576 const std::string& captcha,
577 const std::string& access_code) {
578 DCHECK(IsActiveLogin());
579 // Make sure we are listening for signin traffic.
580 if (!signin_tracker_.get())
581 signin_tracker_.reset(new SigninTracker(Profile::FromWebUI(web_ui()),
582 this));
583
584 last_attempted_user_email_ = username;
585 // If we're just being called to provide an ASP, then pass it to the
586 // SigninManager and wait for the next step.
587 SigninManager* signin = GetSignin();
588 if (!access_code.empty()) {
589 signin->ProvideSecondFactorAccessCode(access_code);
590 return;
591 }
592
593 // Kick off a sign-in through the signin manager.
594 signin->StartSignIn(username,
595 password,
596 signin->GetLoginAuthError().captcha().token,
597 captcha);
598 }
599
600 void SyncSetupHandler::GaiaCredentialsValid() {
601 DCHECK(IsActiveLogin());
602 // Gaia credentials are valid - update the UI.
603 DisplayGaiaSuccessAndSettingUp();
604 }
605
606 void SyncSetupHandler::SigninFailed() {
607 // Got a failed signin - this is either just a typical auth error, or a
608 // sync error (treat sync errors as "fatal errors" - i.e. non-auth errors).
609 DisplayGaiaLogin(GetSyncService()->unrecoverable_error_detected());
610 }
611
612 ProfileSyncService* SyncSetupHandler::GetSyncService() const {
613 return ProfileSyncServiceFactory::GetForProfile(Profile::FromWebUI(web_ui()));
614 }
615
616 void SyncSetupHandler::SigninSuccess() {
617 DCHECK(GetSyncService()->sync_initialized());
618 StartConfigureSync();
497 } 619 }
498 620
499 void SyncSetupHandler::HandleConfigure(const ListValue* args) { 621 void SyncSetupHandler::HandleConfigure(const ListValue* args) {
500 std::string json; 622 std::string json;
501 if (!args->GetString(0, &json)) { 623 if (!args->GetString(0, &json)) {
502 NOTREACHED() << "Could not read JSON argument"; 624 NOTREACHED() << "Could not read JSON argument";
503 return; 625 return;
504 } 626 }
505 if (json.empty()) { 627 if (json.empty()) {
506 NOTREACHED(); 628 NOTREACHED();
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 flow_->OnPassphraseCancel(); 678 flow_->OnPassphraseCancel();
557 } 679 }
558 680
559 void SyncSetupHandler::HandleAttachHandler(const ListValue* args) { 681 void SyncSetupHandler::HandleAttachHandler(const ListValue* args) {
560 OpenSyncSetup(); 682 OpenSyncSetup();
561 } 683 }
562 684
563 void SyncSetupHandler::HandleShowErrorUI(const ListValue* args) { 685 void SyncSetupHandler::HandleShowErrorUI(const ListValue* args) {
564 DCHECK(!flow_); 686 DCHECK(!flow_);
565 687
566 Profile* profile = Profile::FromWebUI(web_ui()); 688 ProfileSyncService* service = GetSyncService();
567 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()->
568 GetForProfile(profile);
569 DCHECK(service); 689 DCHECK(service);
570 690
571 service->ShowErrorUI(); 691 service->ShowErrorUI();
572 } 692 }
573 693
574 void SyncSetupHandler::HandleShowSetupUI(const ListValue* args) { 694 void SyncSetupHandler::HandleShowSetupUI(const ListValue* args) {
575 DCHECK(!flow_); 695 DCHECK(!flow_);
576 if (FocusExistingWizard()) { 696 OpenSyncSetup();
697 }
698
699 void SyncSetupHandler::CloseSyncSetup() {
700 // TODO(atwilson): Move UMA tracking of signin events out of sync module.
701 if (IsActiveLogin()) {
702 if (signin_tracker_.get()) {
703 ProfileSyncService::SyncEvent(
704 ProfileSyncService::CANCEL_DURING_SIGNON);
705 } else if (!flow_) {
706 ProfileSyncService::SyncEvent(
707 ProfileSyncService::CANCEL_FROM_SIGNON_WITHOUT_AUTH);
708 }
709 }
710
711 if (flow_) {
712 flow_->OnDialogClosed(std::string());
713 flow_ = NULL;
714 }
715 signin_tracker_.reset();
716 GetLoginUIService()->ClearLoginUI(web_ui());
717 }
718
719 void SyncSetupHandler::OpenSyncSetup() {
720 ProfileSyncService* service = GetSyncService();
721 if (!service) {
722 // If there's no sync service, the user tried to manually invoke a syncSetup
723 // URL, but sync features are disabled. We need to close the overlay for
724 // this (rare) case.
725 DLOG(WARNING) << "Closing sync UI because sync is disabled";
577 CloseOverlay(); 726 CloseOverlay();
578 return; 727 return;
579 } 728 }
580 729
581 StepWizardForShowSetupUI(); 730 // If the wizard is already visible, just focus that one.
582 ShowSetupUI(); 731 if (FocusExistingWizard()) {
583 } 732 if (!IsActiveLogin())
584 733 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; 734 return;
604 } 735 }
605 736
606 // If the wizard is already visible, it must be attached to another flow 737 GetLoginUIService()->SetLoginUI(web_ui());
607 // handler.
608 if (FocusExistingWizard()) {
609 CloseOverlay();
610 return;
611 }
612 738
613 // The wizard must be stepped before attaching. Allow subclasses to step the 739 if (!SigninTracker::AreServicesSignedIn(Profile::FromWebUI(web_ui()))) {
614 // wizard to appropriate state. 740 // User is not logged in - need to display login UI.
615 StepWizardForShowSetupUI(); 741 DisplayGaiaLogin(false);
616 742 } else {
617 // Attach this as the sync setup handler, before calling ShowSetupUI(). 743 StartConfigureSync();
618 if (!service->get_wizard().AttachSyncSetupHandler(this)) {
619 LOG(ERROR) << "SyncSetupHandler attach failed!";
620 CloseOverlay();
621 return;
622 } 744 }
623 745
624 ShowSetupUI(); 746 ShowSetupUI();
625 } 747 }
626 748
627 // Private member functions. 749 // Private member functions.
628 750
629 bool SyncSetupHandler::FocusExistingWizard() { 751 bool SyncSetupHandler::IsUserLoggedIn() const {
630 Profile* profile = Profile::FromWebUI(web_ui()); 752 Profile* profile = Profile::FromWebUI(web_ui());
631 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> 753 std::string user = GetSignin()->GetAuthenticatedUsername();
632 GetForProfile(profile); 754 if (user.empty())
633 if (!service)
634 return false; 755 return false;
635 756
636 // If the wizard is already visible, focus it. 757 // Now, make sure the services all have proper auth tokens.
637 if (service->get_wizard().IsVisible()) { 758 // TODO(atwilson): Remove this dependency on the ProfileSyncService here.
638 service->get_wizard().Focus(); 759 // Ultimately, we'd like individual services like sync to report their auth
639 return true; 760 // errors via the TokenService, so we can query the TokenService to check for
640 } 761 // invalid credentials instead of asking the services themselves.
641 return false; 762 ProfileSyncService* service =
763 ProfileSyncServiceFactory::GetForProfile(profile);
764 // We're signed in if we have a token and our last attempt to access the
765 // server succeeded.
766 return !service->waiting_for_auth() &&
767 service->AreCredentialsAvailable() &&
768 service->GetAuthError().state() == GoogleServiceAuthError::NONE;
769 }
770
771 bool SyncSetupHandler::FocusExistingWizard() {
772 LoginUIService* service = GetLoginUIService();
773 if (!service->current_login_ui())
774 return false;
775 service->FocusLoginUI();
776 return true;
777 }
778
779 LoginUIService* SyncSetupHandler::GetLoginUIService() const {
780 return LoginUIServiceFactory::GetForProfile(Profile::FromWebUI(web_ui()));
642 } 781 }
643 782
644 void SyncSetupHandler::CloseOverlay() { 783 void SyncSetupHandler::CloseOverlay() {
784 CloseSyncSetup();
645 web_ui()->CallJavascriptFunction("OptionsPage.closeOverlay"); 785 web_ui()->CallJavascriptFunction("OptionsPage.closeOverlay");
646 } 786 }
647 787
648 bool SyncSetupHandler::IsLoginAuthDataValid(const std::string& username, 788 bool SyncSetupHandler::IsLoginAuthDataValid(const std::string& username,
649 string16* error_message) { 789 string16* error_message) {
650 // Happens during unit tests. 790 // Happens during unit tests.
651 if (!web_ui() || !profile_manager_) 791 if (!web_ui() || !profile_manager_)
652 return true; 792 return true;
653 793
654 if (username.empty()) 794 if (username.empty())
655 return true; 795 return true;
656 796
657 // Check if the username is already in use by another profile. 797 // Check if the username is already in use by another profile.
658 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); 798 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
659 size_t current_profile_index = cache.GetIndexOfProfileWithPath( 799 size_t current_profile_index = cache.GetIndexOfProfileWithPath(
660 Profile::FromWebUI(web_ui())->GetPath()); 800 Profile::FromWebUI(web_ui())->GetPath());
661 string16 username_utf16 = UTF8ToUTF16(username); 801 string16 username_utf16 = UTF8ToUTF16(username);
662 802
663 for (size_t i = 0; i < cache.GetNumberOfProfiles(); ++i) { 803 for (size_t i = 0; i < cache.GetNumberOfProfiles(); ++i) {
664 if (i != current_profile_index && AreUserNamesEqual( 804 if (i != current_profile_index && AreUserNamesEqual(
665 cache.GetUserNameOfProfileAtIndex(i), username_utf16)) { 805 cache.GetUserNameOfProfileAtIndex(i), username_utf16)) {
666 *error_message = l10n_util::GetStringUTF16( 806 *error_message = l10n_util::GetStringUTF16(
667 IDS_SYNC_USER_NAME_IN_USE_ERROR); 807 IDS_SYNC_USER_NAME_IN_USE_ERROR);
668 return false; 808 return false;
669 } 809 }
670 } 810 }
671 811
672 return true; 812 return true;
673 } 813 }
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698