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 <algorithm> | 7 #include <algorithm> |
8 #include <functional> | 8 #include <functional> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 #include "chrome/browser/sync/profile_sync_service.h" | 45 #include "chrome/browser/sync/profile_sync_service.h" |
46 #include "chrome/browser/sync/profile_sync_service_factory.h" | 46 #include "chrome/browser/sync/profile_sync_service_factory.h" |
47 #include "chrome/browser/sync/sync_prefs.h" | 47 #include "chrome/browser/sync/sync_prefs.h" |
48 #include "chrome/browser/tab_contents/tab_util.h" | 48 #include "chrome/browser/tab_contents/tab_util.h" |
49 #include "chrome/browser/ui/browser_finder.h" | 49 #include "chrome/browser/ui/browser_finder.h" |
50 #include "chrome/browser/ui/browser_list.h" | 50 #include "chrome/browser/ui/browser_list.h" |
51 #include "chrome/browser/ui/browser_tabstrip.h" | 51 #include "chrome/browser/ui/browser_tabstrip.h" |
52 #include "chrome/browser/ui/browser_window.h" | 52 #include "chrome/browser/ui/browser_window.h" |
53 #include "chrome/browser/ui/chrome_pages.h" | 53 #include "chrome/browser/ui/chrome_pages.h" |
54 #include "chrome/browser/ui/sync/one_click_signin_histogram.h" | 54 #include "chrome/browser/ui/sync/one_click_signin_histogram.h" |
| 55 #include "chrome/browser/ui/sync/one_click_signin_sync_observer.h" |
55 #include "chrome/browser/ui/sync/one_click_signin_sync_starter.h" | 56 #include "chrome/browser/ui/sync/one_click_signin_sync_starter.h" |
56 #include "chrome/browser/ui/sync/signin_histogram.h" | 57 #include "chrome/browser/ui/sync/signin_histogram.h" |
57 #include "chrome/browser/ui/tab_modal_confirm_dialog.h" | 58 #include "chrome/browser/ui/tab_modal_confirm_dialog.h" |
58 #include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h" | 59 #include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h" |
59 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 60 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
60 #include "chrome/common/chrome_version_info.h" | 61 #include "chrome/common/chrome_version_info.h" |
61 #include "chrome/common/net/url_util.h" | 62 #include "chrome/common/net/url_util.h" |
62 #include "chrome/common/pref_names.h" | 63 #include "chrome/common/pref_names.h" |
63 #include "chrome/common/profile_management_switches.h" | 64 #include "chrome/common/profile_management_switches.h" |
64 #include "chrome/common/url_constants.h" | 65 #include "chrome/common/url_constants.h" |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 } | 246 } |
246 | 247 |
247 // Start syncing with the given user information. | 248 // Start syncing with the given user information. |
248 void StartSync(const OneClickSigninHelper::StartSyncArgs& args, | 249 void StartSync(const OneClickSigninHelper::StartSyncArgs& args, |
249 OneClickSigninSyncStarter::StartSyncMode start_mode) { | 250 OneClickSigninSyncStarter::StartSyncMode start_mode) { |
250 if (start_mode == OneClickSigninSyncStarter::UNDO_SYNC) { | 251 if (start_mode == OneClickSigninSyncStarter::UNDO_SYNC) { |
251 LogOneClickHistogramValue(one_click_signin::HISTOGRAM_UNDO); | 252 LogOneClickHistogramValue(one_click_signin::HISTOGRAM_UNDO); |
252 return; | 253 return; |
253 } | 254 } |
254 | 255 |
255 // The wrapper deletes itself once its done. | 256 // The wrapper deletes itself once it's done. |
256 OneClickSigninHelper::SyncStarterWrapper* wrapper = | 257 OneClickSigninHelper::SyncStarterWrapper* wrapper = |
257 new OneClickSigninHelper::SyncStarterWrapper(args, start_mode); | 258 new OneClickSigninHelper::SyncStarterWrapper(args, start_mode); |
258 wrapper->Start(); | 259 wrapper->Start(); |
259 | 260 |
260 int action = one_click_signin::HISTOGRAM_MAX; | 261 int action = one_click_signin::HISTOGRAM_MAX; |
261 switch (args.auto_accept) { | 262 switch (args.auto_accept) { |
262 case OneClickSigninHelper::AUTO_ACCEPT_EXPLICIT: | 263 case OneClickSigninHelper::AUTO_ACCEPT_EXPLICIT: |
263 break; | 264 break; |
264 case OneClickSigninHelper::AUTO_ACCEPT_ACCEPTED: | 265 case OneClickSigninHelper::AUTO_ACCEPT_ACCEPTED: |
265 action = | 266 action = |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 } | 324 } |
324 } | 325 } |
325 } | 326 } |
326 | 327 |
327 void ClearPendingEmailOnIOThread(content::ResourceContext* context) { | 328 void ClearPendingEmailOnIOThread(content::ResourceContext* context) { |
328 ProfileIOData* io_data = ProfileIOData::FromResourceContext(context); | 329 ProfileIOData* io_data = ProfileIOData::FromResourceContext(context); |
329 DCHECK(io_data); | 330 DCHECK(io_data); |
330 io_data->set_reverse_autologin_pending_email(std::string()); | 331 io_data->set_reverse_autologin_pending_email(std::string()); |
331 } | 332 } |
332 | 333 |
333 // Determines the source of the sign in and the continue URL. Its either one | 334 // Determines the source of the sign in and the continue URL. It's either one |
334 // of the known sign in access point (first run, NTP, Apps page, menu, settings) | 335 // of the known sign in access points (first run, NTP, Apps page, menu, or |
335 // or its an implicit sign in via another Google property. In the former case, | 336 // settings) or its an implicit sign in via another Google property. In the |
336 // "service" is also checked to make sure its "chromiumsync". | 337 // former case, "service" is also checked to make sure its "chromiumsync". |
337 signin::Source GetSigninSource(const GURL& url, GURL* continue_url) { | 338 signin::Source GetSigninSource(const GURL& url, GURL* continue_url) { |
338 DCHECK(url.is_valid()); | 339 DCHECK(url.is_valid()); |
339 std::string value; | 340 std::string value; |
340 net::GetValueForKeyInQuery(url, "service", &value); | 341 net::GetValueForKeyInQuery(url, "service", &value); |
341 bool possibly_an_explicit_signin = value == "chromiumsync"; | 342 bool possibly_an_explicit_signin = value == "chromiumsync"; |
342 | 343 |
343 // Find the final continue URL for this sign in. In some cases, Gaia can | 344 // Find the final continue URL for this sign in. In some cases, Gaia can |
344 // continue to itself, with the original continue URL buried under a couple | 345 // continue to itself, with the original continue URL buried under a couple |
345 // of layers of indirection. Peel those layers away. The final destination | 346 // of layers of indirection. Peel those layers away. The final destination |
346 // can also be "IsGaiaSignonRealm" so stop if we get to the end (but be sure | 347 // can also be "IsGaiaSignonRealm" so stop if we get to the end (but be sure |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
475 delete this; // Success. | 476 delete this; // Success. |
476 } | 477 } |
477 } | 478 } |
478 } | 479 } |
479 | 480 |
480 void CurrentHistoryCleaner::WebContentsDestroyed( | 481 void CurrentHistoryCleaner::WebContentsDestroyed( |
481 content::WebContents* contents) { | 482 content::WebContents* contents) { |
482 delete this; // Failure. | 483 delete this; // Failure. |
483 } | 484 } |
484 | 485 |
485 void CloseTab(content::WebContents* tab) { | |
486 Browser* browser = chrome::FindBrowserWithWebContents(tab); | |
487 if (browser) { | |
488 TabStripModel* tab_strip_model = browser->tab_strip_model(); | |
489 if (tab_strip_model) { | |
490 int index = tab_strip_model->GetIndexOfWebContents(tab); | |
491 if (index != TabStripModel::kNoTab) { | |
492 tab_strip_model->ExecuteContextMenuCommand( | |
493 index, TabStripModel::CommandCloseTab); | |
494 } | |
495 } | |
496 } | |
497 } | |
498 | |
499 } // namespace | 486 } // namespace |
500 | 487 |
501 | 488 |
502 // StartSyncArgs -------------------------------------------------------------- | 489 // StartSyncArgs -------------------------------------------------------------- |
503 | 490 |
504 OneClickSigninHelper::StartSyncArgs::StartSyncArgs() | 491 OneClickSigninHelper::StartSyncArgs::StartSyncArgs() |
505 : profile(NULL), | 492 : profile(NULL), |
506 browser(NULL), | 493 browser(NULL), |
507 auto_accept(AUTO_ACCEPT_NONE), | 494 auto_accept(AUTO_ACCEPT_NONE), |
508 web_contents(NULL), | 495 web_contents(NULL), |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
662 void OneClickSigninHelper::SyncStarterWrapper::StartSigninOAuthHelper() { | 649 void OneClickSigninHelper::SyncStarterWrapper::StartSigninOAuthHelper() { |
663 signin_oauth_helper_.reset( | 650 signin_oauth_helper_.reset( |
664 new SigninOAuthHelper(args_.profile->GetRequestContext(), | 651 new SigninOAuthHelper(args_.profile->GetRequestContext(), |
665 args_.session_index, this)); | 652 args_.session_index, this)); |
666 } | 653 } |
667 | 654 |
668 void | 655 void |
669 OneClickSigninHelper::SyncStarterWrapper::StartOneClickSigninSyncStarter( | 656 OneClickSigninHelper::SyncStarterWrapper::StartOneClickSigninSyncStarter( |
670 const std::string& email, | 657 const std::string& email, |
671 const std::string& refresh_token) { | 658 const std::string& refresh_token) { |
672 // The starter deletes itself once its done. | 659 // The starter deletes itself once it's done. |
673 new OneClickSigninSyncStarter(args_.profile, args_.browser, | 660 new OneClickSigninSyncStarter(args_.profile, args_.browser, |
674 email, args_.password, | 661 email, args_.password, |
675 refresh_token, start_mode_, | 662 refresh_token, start_mode_, |
676 args_.web_contents, | 663 args_.web_contents, |
677 args_.confirmation_required, | 664 args_.confirmation_required, |
| 665 GURL(), |
678 args_.callback); | 666 args_.callback); |
679 } | 667 } |
680 | 668 |
681 | 669 |
682 // OneClickSigninHelper ------------------------------------------------------- | 670 // OneClickSigninHelper ------------------------------------------------------- |
683 | 671 |
684 DEFINE_WEB_CONTENTS_USER_DATA_KEY(OneClickSigninHelper); | 672 DEFINE_WEB_CONTENTS_USER_DATA_KEY(OneClickSigninHelper); |
685 | 673 |
686 // static | 674 // static |
687 const int OneClickSigninHelper::kMaxNavigationsSince = 10; | 675 const int OneClickSigninHelper::kMaxNavigationsSince = 10; |
(...skipping 11 matching lines...) Expand all Loading... |
699 do_not_start_sync_for_testing_(false), | 687 do_not_start_sync_for_testing_(false), |
700 weak_pointer_factory_(this) { | 688 weak_pointer_factory_(this) { |
701 // May be NULL during testing. | 689 // May be NULL during testing. |
702 if (password_manager) { | 690 if (password_manager) { |
703 password_manager->AddSubmissionCallback( | 691 password_manager->AddSubmissionCallback( |
704 base::Bind(&OneClickSigninHelper::PasswordSubmitted, | 692 base::Bind(&OneClickSigninHelper::PasswordSubmitted, |
705 weak_pointer_factory_.GetWeakPtr())); | 693 weak_pointer_factory_.GetWeakPtr())); |
706 } | 694 } |
707 } | 695 } |
708 | 696 |
709 OneClickSigninHelper::~OneClickSigninHelper() { | 697 OneClickSigninHelper::~OneClickSigninHelper() {} |
710 // WebContentsDestroyed() should always be called before the object is | |
711 // deleted. | |
712 DCHECK(!web_contents()); | |
713 } | |
714 | 698 |
715 // static | 699 // static |
716 void OneClickSigninHelper::LogHistogramValue( | 700 void OneClickSigninHelper::LogHistogramValue( |
717 signin::Source source, int action) { | 701 signin::Source source, int action) { |
718 switch (source) { | 702 switch (source) { |
719 case signin::SOURCE_START_PAGE: | 703 case signin::SOURCE_START_PAGE: |
720 UMA_HISTOGRAM_ENUMERATION("Signin.StartPageActions", action, | 704 UMA_HISTOGRAM_ENUMERATION("Signin.StartPageActions", action, |
721 one_click_signin::HISTOGRAM_MAX); | 705 one_click_signin::HISTOGRAM_MAX); |
722 break; | 706 break; |
723 case signin::SOURCE_NTP_LINK: | 707 case signin::SOURCE_NTP_LINK: |
(...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1247 VLOG(1) << "OneClickSigninHelper::CleanTransientState"; | 1231 VLOG(1) << "OneClickSigninHelper::CleanTransientState"; |
1248 showing_signin_ = false; | 1232 showing_signin_ = false; |
1249 email_.clear(); | 1233 email_.clear(); |
1250 password_.clear(); | 1234 password_.clear(); |
1251 auto_accept_ = AUTO_ACCEPT_NONE; | 1235 auto_accept_ = AUTO_ACCEPT_NONE; |
1252 source_ = signin::SOURCE_UNKNOWN; | 1236 source_ = signin::SOURCE_UNKNOWN; |
1253 switched_to_advanced_ = false; | 1237 switched_to_advanced_ = false; |
1254 continue_url_ = GURL(); | 1238 continue_url_ = GURL(); |
1255 untrusted_navigations_since_signin_visit_ = 0; | 1239 untrusted_navigations_since_signin_visit_ = 0; |
1256 untrusted_confirmation_required_ = false; | 1240 untrusted_confirmation_required_ = false; |
| 1241 original_continue_url_ = GURL(); |
1257 error_message_.clear(); | 1242 error_message_.clear(); |
1258 | 1243 |
1259 // Post to IO thread to clear pending email. | 1244 // Post to IO thread to clear pending email. |
1260 if (!do_not_clear_pending_email_) { | 1245 if (!do_not_clear_pending_email_) { |
1261 Profile* profile = | 1246 Profile* profile = |
1262 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 1247 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
1263 content::BrowserThread::PostTask( | 1248 content::BrowserThread::PostTask( |
1264 content::BrowserThread::IO, FROM_HERE, | 1249 content::BrowserThread::IO, FROM_HERE, |
1265 base::Bind(&ClearPendingEmailOnIOThread, | 1250 base::Bind(&ClearPendingEmailOnIOThread, |
1266 base::Unretained(profile->GetResourceContext()))); | 1251 base::Unretained(profile->GetResourceContext()))); |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1563 RedirectToNtpOrAppsPageIfNecessary(web_contents(), source_); | 1548 RedirectToNtpOrAppsPageIfNecessary(web_contents(), source_); |
1564 } | 1549 } |
1565 | 1550 |
1566 // Observe the sync service if the Webstore tab or the settings tab | 1551 // Observe the sync service if the Webstore tab or the settings tab |
1567 // requested a gaia sign in, so that when sign in and sync setup are | 1552 // requested a gaia sign in, so that when sign in and sync setup are |
1568 // successful, we can redirect to the correct URL, or auto-close the gaia | 1553 // successful, we can redirect to the correct URL, or auto-close the gaia |
1569 // sign in tab. | 1554 // sign in tab. |
1570 if (original_source == signin::SOURCE_SETTINGS || | 1555 if (original_source == signin::SOURCE_SETTINGS || |
1571 (original_source == signin::SOURCE_WEBSTORE_INSTALL && | 1556 (original_source == signin::SOURCE_WEBSTORE_INSTALL && |
1572 source_ == signin::SOURCE_SETTINGS)) { | 1557 source_ == signin::SOURCE_SETTINGS)) { |
1573 ProfileSyncService* sync_service = | 1558 // The observer deletes itself once it's done. |
1574 ProfileSyncServiceFactory::GetForProfile(profile); | 1559 new OneClickSigninSyncObserver(contents, original_continue_url_); |
1575 if (sync_service) | |
1576 sync_service->AddObserver(this); | |
1577 } | 1560 } |
1578 break; | 1561 break; |
1579 } | 1562 } |
1580 case AUTO_ACCEPT_REJECTED_FOR_PROFILE: | 1563 case AUTO_ACCEPT_REJECTED_FOR_PROFILE: |
1581 AddEmailToOneClickRejectedList(profile, email_); | 1564 AddEmailToOneClickRejectedList(profile, email_); |
1582 LogOneClickHistogramValue(one_click_signin::HISTOGRAM_REJECTED); | 1565 LogOneClickHistogramValue(one_click_signin::HISTOGRAM_REJECTED); |
1583 break; | 1566 break; |
1584 default: | 1567 default: |
1585 NOTREACHED() << "Invalid auto_accept=" << auto_accept_; | 1568 NOTREACHED() << "Invalid auto_accept=" << auto_accept_; |
1586 break; | 1569 break; |
1587 } | 1570 } |
1588 | 1571 |
1589 CleanTransientState(); | 1572 CleanTransientState(); |
1590 } | 1573 } |
1591 | 1574 |
1592 // It is guaranteed that this method is called before the object is deleted. | |
1593 void OneClickSigninHelper::WebContentsDestroyed( | |
1594 content::WebContents* contents) { | |
1595 Profile* profile = | |
1596 Profile::FromBrowserContext(contents->GetBrowserContext()); | |
1597 ProfileSyncService* sync_service = | |
1598 ProfileSyncServiceFactory::GetForProfile(profile); | |
1599 if (sync_service) | |
1600 sync_service->RemoveObserver(this); | |
1601 } | |
1602 | |
1603 void OneClickSigninHelper::OnStateChanged() { | |
1604 // We only add observer for ProfileSyncService when original_continue_url_ is | |
1605 // not empty. | |
1606 DCHECK(!original_continue_url_.is_empty()); | |
1607 | |
1608 content::WebContents* contents = web_contents(); | |
1609 Profile* profile = | |
1610 Profile::FromBrowserContext(contents->GetBrowserContext()); | |
1611 ProfileSyncService* sync_service = | |
1612 ProfileSyncServiceFactory::GetForProfile(profile); | |
1613 | |
1614 // At this point, the sign in process is complete, and control has been handed | |
1615 // back to the sync engine. Close the gaia sign in tab if | |
1616 // |original_continue_url_| contains the |auto_close| parameter. Otherwise, | |
1617 // wait for sync setup to complete and then navigate to | |
1618 // |original_continue_url_|. | |
1619 if (signin::IsAutoCloseEnabledInURL(original_continue_url_)) { | |
1620 // Close the gaia sign in tab via a task to make sure we aren't in the | |
1621 // middle of any webui handler code. | |
1622 base::MessageLoop::current()->PostTask( | |
1623 FROM_HERE, | |
1624 base::Bind(&CloseTab, base::Unretained(contents))); | |
1625 } else { | |
1626 // Sync setup not completed yet. | |
1627 if (sync_service->FirstSetupInProgress()) | |
1628 return; | |
1629 | |
1630 if (sync_service->sync_initialized() && | |
1631 signin::GetSourceForPromoURL(original_continue_url_) | |
1632 != signin::SOURCE_SETTINGS) { | |
1633 contents->GetController().LoadURL(original_continue_url_, | |
1634 content::Referrer(), | |
1635 content::PAGE_TRANSITION_AUTO_TOPLEVEL, | |
1636 std::string()); | |
1637 } | |
1638 } | |
1639 | |
1640 // Clears |original_continue_url_| here instead of in CleanTransientState, | |
1641 // because it is used in OnStateChanged which occurs later. | |
1642 original_continue_url_ = GURL(); | |
1643 sync_service->RemoveObserver(this); | |
1644 } | |
1645 | |
1646 OneClickSigninSyncStarter::Callback | 1575 OneClickSigninSyncStarter::Callback |
1647 OneClickSigninHelper::CreateSyncStarterCallback() { | 1576 OneClickSigninHelper::CreateSyncStarterCallback() { |
1648 // The callback will only be invoked if this object is still alive when sync | 1577 // The callback will only be invoked if this object is still alive when sync |
1649 // setup is completed. This is correct because this object is only deleted | 1578 // setup is completed. This is correct because this object is only deleted |
1650 // when the web contents that potentially shows a blank page is deleted. | 1579 // when the web contents that potentially shows a blank page is deleted. |
1651 return base::Bind(&OneClickSigninHelper::SyncSetupCompletedCallback, | 1580 return base::Bind(&OneClickSigninHelper::SyncSetupCompletedCallback, |
1652 weak_pointer_factory_.GetWeakPtr()); | 1581 weak_pointer_factory_.GetWeakPtr()); |
1653 } | 1582 } |
1654 | 1583 |
1655 void OneClickSigninHelper::SyncSetupCompletedCallback( | 1584 void OneClickSigninHelper::SyncSetupCompletedCallback( |
1656 OneClickSigninSyncStarter::SyncSetupResult result) { | 1585 OneClickSigninSyncStarter::SyncSetupResult result) { |
1657 if (result == OneClickSigninSyncStarter::SYNC_SETUP_FAILURE && | 1586 if (result == OneClickSigninSyncStarter::SYNC_SETUP_FAILURE && |
1658 web_contents()) { | 1587 web_contents()) { |
1659 GURL current_url = web_contents()->GetVisibleURL(); | 1588 GURL current_url = web_contents()->GetVisibleURL(); |
1660 | 1589 |
1661 // If the web contents is showing a blank page and not about to be closed, | 1590 // If the web contents is showing a blank page and not about to be closed, |
1662 // redirect to the NTP or apps page. | 1591 // redirect to the NTP or apps page. |
1663 if (signin::IsContinueUrlForWebBasedSigninFlow(current_url) && | 1592 if (signin::IsContinueUrlForWebBasedSigninFlow(current_url) && |
1664 !signin::IsAutoCloseEnabledInURL(original_continue_url_)) { | 1593 !signin::IsAutoCloseEnabledInURL(original_continue_url_)) { |
1665 RedirectToNtpOrAppsPage( | 1594 RedirectToNtpOrAppsPage( |
1666 web_contents(), | 1595 web_contents(), |
1667 signin::GetSourceForPromoURL(original_continue_url_)); | 1596 signin::GetSourceForPromoURL(original_continue_url_)); |
1668 } | 1597 } |
1669 } | 1598 } |
1670 } | 1599 } |
OLD | NEW |