| 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/sync/one_click_signin_helper.h" | 5 #include "chrome/browser/ui/sync/one_click_signin_helper.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 #include "chrome/browser/signin/signin_manager.h" | 25 #include "chrome/browser/signin/signin_manager.h" |
| 26 #include "chrome/browser/signin/signin_manager_factory.h" | 26 #include "chrome/browser/signin/signin_manager_factory.h" |
| 27 #include "chrome/browser/signin/signin_names_io_thread.h" | 27 #include "chrome/browser/signin/signin_names_io_thread.h" |
| 28 #include "chrome/browser/sync/profile_sync_service.h" | 28 #include "chrome/browser/sync/profile_sync_service.h" |
| 29 #include "chrome/browser/sync/profile_sync_service_factory.h" | 29 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 30 #include "chrome/browser/tab_contents/tab_util.h" | 30 #include "chrome/browser/tab_contents/tab_util.h" |
| 31 #include "chrome/browser/ui/browser_finder.h" | 31 #include "chrome/browser/ui/browser_finder.h" |
| 32 #include "chrome/browser/ui/browser_window.h" | 32 #include "chrome/browser/ui/browser_window.h" |
| 33 #include "chrome/browser/ui/sync/one_click_signin_histogram.h" | 33 #include "chrome/browser/ui/sync/one_click_signin_histogram.h" |
| 34 #include "chrome/browser/ui/sync/one_click_signin_sync_starter.h" | 34 #include "chrome/browser/ui/sync/one_click_signin_sync_starter.h" |
| 35 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 36 #include "chrome/common/chrome_notification_types.h" |
| 35 #include "chrome/common/chrome_switches.h" | 37 #include "chrome/common/chrome_switches.h" |
| 36 #include "chrome/common/chrome_version_info.h" | 38 #include "chrome/common/chrome_version_info.h" |
| 37 #include "chrome/common/net/url_util.h" | 39 #include "chrome/common/net/url_util.h" |
| 38 #include "chrome/common/pref_names.h" | 40 #include "chrome/common/pref_names.h" |
| 39 #include "chrome/common/url_constants.h" | 41 #include "chrome/common/url_constants.h" |
| 40 #include "content/public/browser/browser_thread.h" | 42 #include "content/public/browser/browser_thread.h" |
| 41 #include "content/public/browser/page_navigator.h" | 43 #include "content/public/browser/page_navigator.h" |
| 42 #include "content/public/browser/web_contents.h" | 44 #include "content/public/browser/web_contents.h" |
| 43 #include "content/public/browser/web_contents_view.h" | 45 #include "content/public/browser/web_contents_view.h" |
| 44 #include "content/public/common/frame_navigate_params.h" | 46 #include "content/public/common/frame_navigate_params.h" |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 | 368 |
| 367 // static | 369 // static |
| 368 void OneClickSigninHelper::AssociateWithRequestForTesting( | 370 void OneClickSigninHelper::AssociateWithRequestForTesting( |
| 369 base::SupportsUserData* request, | 371 base::SupportsUserData* request, |
| 370 const std::string& email) { | 372 const std::string& email) { |
| 371 OneClickSigninRequestUserData::AssociateWithRequest(request, email); | 373 OneClickSigninRequestUserData::AssociateWithRequest(request, email); |
| 372 } | 374 } |
| 373 | 375 |
| 374 // static | 376 // static |
| 375 bool OneClickSigninHelper::CanOffer(content::WebContents* web_contents, | 377 bool OneClickSigninHelper::CanOffer(content::WebContents* web_contents, |
| 378 CanOfferFor can_offer_for, |
| 376 const std::string& email, | 379 const std::string& email, |
| 377 bool check_connected) { | 380 int* error_message_id) { |
| 378 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 381 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 379 | 382 |
| 383 if (error_message_id) |
| 384 *error_message_id = 0; |
| 385 |
| 380 if (!web_contents) | 386 if (!web_contents) |
| 381 return false; | 387 return false; |
| 382 | 388 |
| 383 if (web_contents->GetBrowserContext()->IsOffTheRecord()) | 389 if (web_contents->GetBrowserContext()->IsOffTheRecord()) |
| 384 return false; | 390 return false; |
| 385 | 391 |
| 386 if (!ProfileSyncService::IsSyncEnabled()) | 392 if (!ProfileSyncService::IsSyncEnabled()) |
| 387 return false; | 393 return false; |
| 388 | 394 |
| 389 Profile* profile = | 395 Profile* profile = |
| 390 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 396 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 391 if (!profile) | 397 if (!profile) |
| 392 return false; | 398 return false; |
| 393 | 399 |
| 394 if (!profile->GetPrefs()->GetBoolean(prefs::kReverseAutologinEnabled)) | 400 if (can_offer_for == CAN_OFFER_FOR_INTERSTITAL_ONLY && |
| 401 !profile->GetPrefs()->GetBoolean(prefs::kReverseAutologinEnabled)) |
| 395 return false; | 402 return false; |
| 396 | 403 |
| 397 if (!SigninManager::AreSigninCookiesAllowed(profile)) | 404 if (!SigninManager::AreSigninCookiesAllowed(profile)) |
| 398 return false; | 405 return false; |
| 399 | 406 |
| 400 if (check_connected) { | 407 if (!email.empty()) { |
| 401 SigninManager* manager = | 408 SigninManager* manager = |
| 402 SigninManagerFactory::GetForProfile(profile); | 409 SigninManagerFactory::GetForProfile(profile); |
| 403 if (!manager) | 410 if (!manager) |
| 404 return false; | 411 return false; |
| 405 | 412 |
| 406 if (!manager->GetAuthenticatedUsername().empty()) | 413 if (!manager->GetAuthenticatedUsername().empty()) { |
| 414 if (error_message_id) |
| 415 *error_message_id = IDS_SYNC_SETUP_ERROR; |
| 407 return false; | 416 return false; |
| 417 } |
| 408 | 418 |
| 409 // Make sure this username is not prohibited by policy. | 419 // Make sure this username is not prohibited by policy. |
| 410 if (!manager->IsAllowedUsername(email)) | 420 if (!manager->IsAllowedUsername(email)) { |
| 421 if (error_message_id) |
| 422 *error_message_id = IDS_SYNC_LOGIN_NAME_PROHIBITED; |
| 411 return false; | 423 return false; |
| 424 } |
| 412 | 425 |
| 413 // If some profile, not just the current one, is already connected to this | 426 // If some profile, not just the current one, is already connected to this |
| 414 // account, don't show the infobar. | 427 // account, don't show the infobar. |
| 415 if (g_browser_process) { | 428 if (g_browser_process) { |
| 416 ProfileManager* manager = g_browser_process->profile_manager(); | 429 ProfileManager* manager = g_browser_process->profile_manager(); |
| 417 if (manager) { | 430 if (manager) { |
| 418 string16 email16 = UTF8ToUTF16(email); | 431 string16 email16 = UTF8ToUTF16(email); |
| 419 ProfileInfoCache& cache = manager->GetProfileInfoCache(); | 432 ProfileInfoCache& cache = manager->GetProfileInfoCache(); |
| 420 | 433 |
| 421 for (size_t i = 0; i < cache.GetNumberOfProfiles(); ++i) { | 434 for (size_t i = 0; i < cache.GetNumberOfProfiles(); ++i) { |
| 422 if (email16 == cache.GetUserNameOfProfileAtIndex(i)) | 435 if (email16 == cache.GetUserNameOfProfileAtIndex(i)) { |
| 436 if (error_message_id) |
| 437 *error_message_id = IDS_SYNC_USER_NAME_IN_USE_ERROR; |
| 423 return false; | 438 return false; |
| 439 } |
| 424 } | 440 } |
| 425 } | 441 } |
| 426 } | 442 } |
| 427 | 443 |
| 428 // If email was already rejected by this profile for one-click sign-in. | 444 // If email was already rejected by this profile for one-click sign-in. |
| 429 if (!email.empty()) { | 445 if (can_offer_for == CAN_OFFER_FOR_INTERSTITAL_ONLY) { |
| 430 const ListValue* rejected_emails = profile->GetPrefs()->GetList( | 446 const ListValue* rejected_emails = profile->GetPrefs()->GetList( |
| 431 prefs::kReverseAutologinRejectedEmailList); | 447 prefs::kReverseAutologinRejectedEmailList); |
| 432 if (!rejected_emails->empty()) { | 448 if (!rejected_emails->empty()) { |
| 433 base::ListValue::const_iterator iter = rejected_emails->Find( | 449 base::ListValue::const_iterator iter = rejected_emails->Find( |
| 434 base::StringValue(email)); | 450 base::StringValue(email)); |
| 435 if (iter != rejected_emails->end()) | 451 if (iter != rejected_emails->end()) |
| 436 return false; | 452 return false; |
| 437 } | 453 } |
| 438 } | 454 } |
| 439 | 455 |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 656 SyncPromoUI::Source source, | 672 SyncPromoUI::Source source, |
| 657 int child_id, | 673 int child_id, |
| 658 int route_id) { | 674 int route_id) { |
| 659 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 675 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 660 | 676 |
| 661 content::WebContents* web_contents = tab_util::GetWebContentsByID(child_id, | 677 content::WebContents* web_contents = tab_util::GetWebContentsByID(child_id, |
| 662 route_id); | 678 route_id); |
| 663 | 679 |
| 664 // TODO(mathp): The appearance of this infobar should be tested using a | 680 // TODO(mathp): The appearance of this infobar should be tested using a |
| 665 // browser_test. | 681 // browser_test. |
| 666 if (!web_contents || !CanOffer(web_contents, email, !email.empty())) { | 682 OneClickSigninHelper* helper = |
| 683 OneClickSigninHelper::FromWebContents(web_contents); |
| 684 int error_message_id = 0; |
| 685 |
| 686 CanOfferFor can_offer_for = |
| 687 (auto_accept != AUTO_ACCEPT_EXPLICIT && |
| 688 helper->auto_accept_ != AUTO_ACCEPT_EXPLICIT) ? |
| 689 CAN_OFFER_FOR_INTERSTITAL_ONLY : CAN_OFFER_FOR_ALL; |
| 690 |
| 691 if (!web_contents || !CanOffer(web_contents, can_offer_for, email, |
| 692 &error_message_id)) { |
| 667 VLOG(1) << "OneClickSigninHelper::ShowInfoBarUIThread: not offering"; | 693 VLOG(1) << "OneClickSigninHelper::ShowInfoBarUIThread: not offering"; |
| 694 if (helper && helper->error_message_.empty() && error_message_id != 0) |
| 695 helper->error_message_ = l10n_util::GetStringUTF8(error_message_id); |
| 696 |
| 668 return; | 697 return; |
| 669 } | 698 } |
| 670 | 699 |
| 671 // Save the email in the one-click signin manager. The manager may | 700 // Save the email in the one-click signin manager. The manager may |
| 672 // not exist if the contents is incognito or if the profile is already | 701 // not exist if the contents is incognito or if the profile is already |
| 673 // connected to a Google account. | 702 // connected to a Google account. |
| 674 OneClickSigninHelper* helper = | |
| 675 OneClickSigninHelper::FromWebContents(web_contents); | |
| 676 if (helper) { | 703 if (helper) { |
| 677 if (!session_index.empty()) | 704 if (!session_index.empty()) |
| 678 helper->session_index_ = session_index; | 705 helper->session_index_ = session_index; |
| 679 | 706 |
| 680 if (!email.empty()) | 707 if (!email.empty()) |
| 681 helper->email_ = email; | 708 helper->email_ = email; |
| 682 | 709 |
| 683 helper->auto_accept_ = auto_accept; | 710 if (auto_accept != NO_AUTO_ACCEPT) { |
| 684 helper->source_ = source; | 711 helper->auto_accept_ = auto_accept; |
| 712 helper->source_ = source; |
| 713 } |
| 685 } | 714 } |
| 686 } | 715 } |
| 687 | 716 |
| 717 void OneClickSigninHelper::RedirectToNTP() { |
| 718 // Redirect to NTP with sign in bubble visible. |
| 719 content::WebContents* contents = web_contents(); |
| 720 Profile* profile = |
| 721 Profile::FromBrowserContext(contents->GetBrowserContext()); |
| 722 PrefService* pref_service = profile->GetPrefs(); |
| 723 pref_service->SetBoolean(prefs::kSyncPromoShowNTPBubble, true); |
| 724 pref_service->SetString(prefs::kSyncPromoErrorMessage, error_message_); |
| 725 |
| 726 contents->GetController().LoadURL(GURL(chrome::kChromeUINewTabURL), |
| 727 content::Referrer(), |
| 728 content::PAGE_TRANSITION_AUTO_TOPLEVEL, |
| 729 std::string()); |
| 730 |
| 731 error_message_.clear(); |
| 732 signin_tracker_.reset(); |
| 733 } |
| 734 |
| 735 void OneClickSigninHelper::CleanTransientState() { |
| 736 email_.clear(); |
| 737 password_.clear(); |
| 738 auto_accept_ = NO_AUTO_ACCEPT; |
| 739 source_ = SyncPromoUI::SOURCE_UNKNOWN; |
| 740 } |
| 741 |
| 688 void OneClickSigninHelper::DidNavigateAnyFrame( | 742 void OneClickSigninHelper::DidNavigateAnyFrame( |
| 689 const content::LoadCommittedDetails& details, | 743 const content::LoadCommittedDetails& details, |
| 690 const content::FrameNavigateParams& params) { | 744 const content::FrameNavigateParams& params) { |
| 691 // We only need to scrape the password for Gaia logins. | 745 // We only need to scrape the password for Gaia logins. |
| 692 const content::PasswordForm& form = params.password_form; | 746 const content::PasswordForm& form = params.password_form; |
| 693 if (form.origin.is_valid() && | 747 if (form.origin.is_valid() && |
| 694 gaia::IsGaiaSignonRealm(GURL(form.signon_realm))) { | 748 gaia::IsGaiaSignonRealm(GURL(form.signon_realm))) { |
| 695 VLOG(1) << "OneClickSigninHelper::DidNavigateAnyFrame: got password"; | 749 VLOG(1) << "OneClickSigninHelper::DidNavigateAnyFrame: got password"; |
| 696 password_ = UTF16ToUTF8(params.password_form.password_value); | 750 password_ = UTF16ToUTF8(params.password_form.password_value); |
| 697 } | 751 } |
| 698 } | 752 } |
| 699 | 753 |
| 700 void OneClickSigninHelper::DidStopLoading( | 754 void OneClickSigninHelper::DidStopLoading( |
| 701 content::RenderViewHost* render_view_host) { | 755 content::RenderViewHost* render_view_host) { |
| 756 // If the user left the sign in process, clear all members. |
| 757 // TODO(rogerta): might need to allow some youtube URLs. |
| 758 content::WebContents* contents = web_contents(); |
| 759 if (!gaia::IsGaiaSignonRealm(contents->GetURL().GetOrigin())) { |
| 760 CleanTransientState(); |
| 761 return; |
| 762 } |
| 763 |
| 764 if (!error_message_.empty()) { |
| 765 RedirectToNTP(); |
| 766 return; |
| 767 } |
| 768 |
| 702 if (email_.empty() || password_.empty()) | 769 if (email_.empty() || password_.empty()) |
| 703 return; | 770 return; |
| 704 | 771 |
| 705 content::WebContents* contents = web_contents(); | |
| 706 Browser* browser = chrome::FindBrowserWithWebContents(contents); | 772 Browser* browser = chrome::FindBrowserWithWebContents(contents); |
| 707 InfoBarTabHelper* infobar_tab_helper = | 773 InfoBarTabHelper* infobar_tab_helper = |
| 708 InfoBarTabHelper::FromWebContents(contents); | 774 InfoBarTabHelper::FromWebContents(contents); |
| 709 | 775 |
| 710 switch (auto_accept_) { | 776 switch (auto_accept_) { |
| 711 case AUTO_ACCEPT: | 777 case AUTO_ACCEPT: |
| 712 browser->window()->ShowOneClickSigninBubble( | 778 browser->window()->ShowOneClickSigninBubble( |
| 713 base::Bind(&StartSync, browser, auto_accept_, session_index_, | 779 base::Bind(&StartSync, browser, auto_accept_, session_index_, |
| 714 email_, password_)); | 780 email_, password_)); |
| 715 break; | 781 break; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 741 // If this explicit sign in is not from settings page, show the NTP after | 807 // If this explicit sign in is not from settings page, show the NTP after |
| 742 // sign in completes. In the case of the settings page, it will get closed | 808 // sign in completes. In the case of the settings page, it will get closed |
| 743 // by SyncSetupHandler. | 809 // by SyncSetupHandler. |
| 744 if (auto_accept_ == AUTO_ACCEPT_EXPLICIT && | 810 if (auto_accept_ == AUTO_ACCEPT_EXPLICIT && |
| 745 source_ != SyncPromoUI::SOURCE_SETTINGS) { | 811 source_ != SyncPromoUI::SOURCE_SETTINGS) { |
| 746 Profile* profile = | 812 Profile* profile = |
| 747 Profile::FromBrowserContext(contents->GetBrowserContext()); | 813 Profile::FromBrowserContext(contents->GetBrowserContext()); |
| 748 signin_tracker_.reset(new SigninTracker(profile, this)); | 814 signin_tracker_.reset(new SigninTracker(profile, this)); |
| 749 } | 815 } |
| 750 | 816 |
| 751 email_.clear(); | 817 CleanTransientState(); |
| 752 password_.clear(); | |
| 753 auto_accept_ = NO_AUTO_ACCEPT; | |
| 754 source_ = SyncPromoUI::SOURCE_UNKNOWN; | |
| 755 } | 818 } |
| 756 | 819 |
| 757 void OneClickSigninHelper::GaiaCredentialsValid() { | 820 void OneClickSigninHelper::GaiaCredentialsValid() { |
| 758 // Redirect to NTP with sign in bubble visible. | |
| 759 content::WebContents* contents = web_contents(); | |
| 760 Profile* profile = | |
| 761 Profile::FromBrowserContext(contents->GetBrowserContext()); | |
| 762 PrefService* pref_service = profile->GetPrefs(); | |
| 763 pref_service->SetBoolean(prefs::kSyncPromoShowNTPBubble, true); | |
| 764 | |
| 765 contents->GetController().LoadURL(GURL(chrome::kChromeUINewTabURL), | |
| 766 content::Referrer(), | |
| 767 content::PAGE_TRANSITION_AUTO_TOPLEVEL, | |
| 768 std::string()); | |
| 769 } | 821 } |
| 770 | 822 |
| 771 void OneClickSigninHelper::SigninFailed(const GoogleServiceAuthError& error) { | 823 void OneClickSigninHelper::SigninFailed(const GoogleServiceAuthError& error) { |
| 772 signin_tracker_.reset(); | 824 if (error_message_.empty() && !error.error_message().empty()) |
| 825 error_message_ = error.error_message(); |
| 826 |
| 827 if (error_message_.empty()) { |
| 828 switch (error.state()) { |
| 829 case GoogleServiceAuthError::NONE: |
| 830 error_message_.clear(); |
| 831 break; |
| 832 case GoogleServiceAuthError::SERVICE_UNAVAILABLE: |
| 833 error_message_ = l10n_util::GetStringUTF8(IDS_SYNC_UNRECOVERABLE_ERROR); |
| 834 break; |
| 835 default: |
| 836 error_message_ = l10n_util::GetStringUTF8(IDS_SYNC_ERROR_SIGNING_IN); |
| 837 break; |
| 838 } |
| 839 } |
| 840 |
| 841 RedirectToNTP(); |
| 773 } | 842 } |
| 774 | 843 |
| 775 void OneClickSigninHelper::SigninSuccess() { | 844 void OneClickSigninHelper::SigninSuccess() { |
| 776 signin_tracker_.reset(); | 845 RedirectToNTP(); |
| 777 } | 846 } |
| OLD | NEW |