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/chromeos/login/login_utils.h" | 5 #include "chrome/browser/chromeos/login/login_utils.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/chromeos/chromeos_version.h" | 10 #include "base/chromeos/chromeos_version.h" |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 : public LoginUtils, | 98 : public LoginUtils, |
99 public OAuth2LoginManager::Observer, | 99 public OAuth2LoginManager::Observer, |
100 public net::NetworkChangeNotifier::ConnectionTypeObserver, | 100 public net::NetworkChangeNotifier::ConnectionTypeObserver, |
101 public base::SupportsWeakPtr<LoginUtilsImpl> { | 101 public base::SupportsWeakPtr<LoginUtilsImpl> { |
102 public: | 102 public: |
103 LoginUtilsImpl() | 103 LoginUtilsImpl() |
104 : using_oauth_(false), | 104 : using_oauth_(false), |
105 has_web_auth_cookies_(false), | 105 has_web_auth_cookies_(false), |
106 delegate_(NULL), | 106 delegate_(NULL), |
107 should_restore_auth_session_(false), | 107 should_restore_auth_session_(false), |
| 108 exit_after_session_restore_(false), |
108 session_restore_strategy_( | 109 session_restore_strategy_( |
109 OAuth2LoginManager::RESTORE_FROM_SAVED_OAUTH2_REFRESH_TOKEN) { | 110 OAuth2LoginManager::RESTORE_FROM_SAVED_OAUTH2_REFRESH_TOKEN) { |
110 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); | 111 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); |
111 } | 112 } |
112 | 113 |
113 virtual ~LoginUtilsImpl() { | 114 virtual ~LoginUtilsImpl() { |
114 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | 115 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
115 } | 116 } |
116 | 117 |
117 // LoginUtils implementation: | 118 // LoginUtils implementation: |
118 virtual void DoBrowserLaunch(Profile* profile, | 119 virtual void DoBrowserLaunch(Profile* profile, |
119 LoginDisplayHost* login_host) OVERRIDE; | 120 LoginDisplayHost* login_host) OVERRIDE; |
120 virtual void PrepareProfile( | 121 virtual void PrepareProfile( |
121 const UserContext& user_context, | 122 const UserContext& user_context, |
122 const std::string& display_email, | 123 const std::string& display_email, |
123 bool using_oauth, | 124 bool using_oauth, |
124 bool has_cookies, | 125 bool has_cookies, |
125 bool has_active_session, | 126 bool has_active_session, |
126 LoginUtils::Delegate* delegate) OVERRIDE; | 127 LoginUtils::Delegate* delegate) OVERRIDE; |
127 virtual void DelegateDeleted(LoginUtils::Delegate* delegate) OVERRIDE; | 128 virtual void DelegateDeleted(LoginUtils::Delegate* delegate) OVERRIDE; |
128 virtual void CompleteOffTheRecordLogin(const GURL& start_url) OVERRIDE; | 129 virtual void CompleteOffTheRecordLogin(const GURL& start_url) OVERRIDE; |
129 virtual void SetFirstLoginPrefs(PrefService* prefs) OVERRIDE; | 130 virtual void SetFirstLoginPrefs(PrefService* prefs) OVERRIDE; |
130 virtual scoped_refptr<Authenticator> CreateAuthenticator( | 131 virtual scoped_refptr<Authenticator> CreateAuthenticator( |
131 LoginStatusConsumer* consumer) OVERRIDE; | 132 LoginStatusConsumer* consumer) OVERRIDE; |
132 virtual void RestoreAuthenticationSession(Profile* profile) OVERRIDE; | 133 virtual void RestoreAuthenticationSession(Profile* profile) OVERRIDE; |
133 virtual void InitRlzDelayed(Profile* user_profile) OVERRIDE; | 134 virtual void InitRlzDelayed(Profile* user_profile) OVERRIDE; |
134 | 135 |
135 // OAuth2LoginManager::Delegate overrides. | 136 // OAuth2LoginManager::Observer overrides. |
136 virtual void OnSessionRestoreStateChanged( | 137 virtual void OnSessionRestoreStateChanged( |
137 Profile* user_profile, | 138 Profile* user_profile, |
138 OAuth2LoginManager::SessionRestoreState state) OVERRIDE; | 139 OAuth2LoginManager::SessionRestoreState state) OVERRIDE; |
| 140 virtual void OnNewRefreshTokenAvaiable(Profile* user_profile) OVERRIDE; |
139 virtual void OnSessionAuthenticated(Profile* user_profile) OVERRIDE; | 141 virtual void OnSessionAuthenticated(Profile* user_profile) OVERRIDE; |
140 | 142 |
141 // net::NetworkChangeNotifier::ConnectionTypeObserver overrides. | 143 // net::NetworkChangeNotifier::ConnectionTypeObserver overrides. |
142 virtual void OnConnectionTypeChanged( | 144 virtual void OnConnectionTypeChanged( |
143 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE; | 145 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE; |
144 | 146 |
145 private: | 147 private: |
146 // Restarts OAuth session authentication check. | 148 // Restarts OAuth session authentication check. |
147 void KickStartAuthentication(Profile* profile); | 149 void KickStartAuthentication(Profile* profile); |
148 | 150 |
(...skipping 27 matching lines...) Expand all Loading... |
176 // Restores GAIA auth cookies for the created user profile from OAuth2 token. | 178 // Restores GAIA auth cookies for the created user profile from OAuth2 token. |
177 void RestoreAuthSession(Profile* user_profile, | 179 void RestoreAuthSession(Profile* user_profile, |
178 bool restore_from_auth_cookies); | 180 bool restore_from_auth_cookies); |
179 | 181 |
180 // Initializes RLZ. If |disabled| is true, RLZ pings are disabled. | 182 // Initializes RLZ. If |disabled| is true, RLZ pings are disabled. |
181 void InitRlz(Profile* user_profile, bool disabled); | 183 void InitRlz(Profile* user_profile, bool disabled); |
182 | 184 |
183 // Starts signing related services. Initiates TokenService token retrieval. | 185 // Starts signing related services. Initiates TokenService token retrieval. |
184 void StartSignedInServices(Profile* profile); | 186 void StartSignedInServices(Profile* profile); |
185 | 187 |
| 188 // Attempts exiting browser process and esures this does not happen |
| 189 // while we are still fetching new OAuth refresh tokens. |
| 190 void AttemptExit(Profile* profile); |
| 191 |
186 UserContext user_context_; | 192 UserContext user_context_; |
187 bool using_oauth_; | 193 bool using_oauth_; |
188 | 194 |
189 // True if the authentication profile's cookie jar should contain | 195 // True if the authentication profile's cookie jar should contain |
190 // authentication cookies from the authentication extension log in flow. | 196 // authentication cookies from the authentication extension log in flow. |
191 bool has_web_auth_cookies_; | 197 bool has_web_auth_cookies_; |
192 // Has to be scoped_refptr, see comment for CreateAuthenticator(...). | 198 // Has to be scoped_refptr, see comment for CreateAuthenticator(...). |
193 scoped_refptr<Authenticator> authenticator_; | 199 scoped_refptr<Authenticator> authenticator_; |
194 | 200 |
195 // Delegate to be fired when the profile will be prepared. | 201 // Delegate to be fired when the profile will be prepared. |
196 LoginUtils::Delegate* delegate_; | 202 LoginUtils::Delegate* delegate_; |
197 | 203 |
198 // True if should restore authentication session when notified about | 204 // True if should restore authentication session when notified about |
199 // online state change. | 205 // online state change. |
200 bool should_restore_auth_session_; | 206 bool should_restore_auth_session_; |
201 | 207 |
| 208 // True if we should restart chrome right after session restore. |
| 209 bool exit_after_session_restore_; |
| 210 |
202 // Sesion restore strategy. | 211 // Sesion restore strategy. |
203 OAuth2LoginManager::SessionRestoreStrategy session_restore_strategy_; | 212 OAuth2LoginManager::SessionRestoreStrategy session_restore_strategy_; |
204 // OAuth2 refresh token for session restore. | 213 // OAuth2 refresh token for session restore. |
205 std::string oauth2_refresh_token_; | 214 std::string oauth2_refresh_token_; |
206 | 215 |
207 DISALLOW_COPY_AND_ASSIGN(LoginUtilsImpl); | 216 DISALLOW_COPY_AND_ASSIGN(LoginUtilsImpl); |
208 }; | 217 }; |
209 | 218 |
210 class LoginUtilsWrapper { | 219 class LoginUtilsWrapper { |
211 public: | 220 public: |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 if (UserManager::Get()->GetLoggedInUsers().size() == 1 && | 263 if (UserManager::Get()->GetLoggedInUsers().size() == 1 && |
255 !UserManager::Get()->IsLoggedInAsLocallyManagedUser() && | 264 !UserManager::Get()->IsLoggedInAsLocallyManagedUser() && |
256 !about_flags::AreSwitchesIdenticalToCurrentCommandLine( | 265 !about_flags::AreSwitchesIdenticalToCurrentCommandLine( |
257 user_flags, *CommandLine::ForCurrentProcess())) { | 266 user_flags, *CommandLine::ForCurrentProcess())) { |
258 CommandLine::StringVector flags; | 267 CommandLine::StringVector flags; |
259 // argv[0] is the program name |CommandLine::NO_PROGRAM|. | 268 // argv[0] is the program name |CommandLine::NO_PROGRAM|. |
260 flags.assign(user_flags.argv().begin() + 1, user_flags.argv().end()); | 269 flags.assign(user_flags.argv().begin() + 1, user_flags.argv().end()); |
261 VLOG(1) << "Restarting to apply per-session flags..."; | 270 VLOG(1) << "Restarting to apply per-session flags..."; |
262 DBusThreadManager::Get()->GetSessionManagerClient()->SetFlagsForUser( | 271 DBusThreadManager::Get()->GetSessionManagerClient()->SetFlagsForUser( |
263 UserManager::Get()->GetActiveUser()->email(), flags); | 272 UserManager::Get()->GetActiveUser()->email(), flags); |
264 chrome::AttemptExit(); | 273 AttemptExit(profile); |
265 return; | 274 return; |
266 } | 275 } |
267 | 276 |
268 if (login_host) { | 277 if (login_host) { |
269 login_host->SetStatusAreaVisible(true); | 278 login_host->SetStatusAreaVisible(true); |
270 login_host->BeforeSessionStart(); | 279 login_host->BeforeSessionStart(); |
271 } | 280 } |
272 | 281 |
273 BootTimesLoader::Get()->AddLoginTimeMarker("BrowserLaunched", false); | 282 BootTimesLoader::Get()->AddLoginTimeMarker("BrowserLaunched", false); |
274 | 283 |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 void LoginUtilsImpl::RestoreAuthSession(Profile* user_profile, | 478 void LoginUtilsImpl::RestoreAuthSession(Profile* user_profile, |
470 bool restore_from_auth_cookies) { | 479 bool restore_from_auth_cookies) { |
471 CHECK((authenticator_.get() && authenticator_->authentication_profile()) || | 480 CHECK((authenticator_.get() && authenticator_->authentication_profile()) || |
472 !restore_from_auth_cookies); | 481 !restore_from_auth_cookies); |
473 | 482 |
474 if (chrome::IsRunningInForcedAppMode() || | 483 if (chrome::IsRunningInForcedAppMode() || |
475 CommandLine::ForCurrentProcess()->HasSwitch( | 484 CommandLine::ForCurrentProcess()->HasSwitch( |
476 chromeos::switches::kOobeSkipPostLogin)) | 485 chromeos::switches::kOobeSkipPostLogin)) |
477 return; | 486 return; |
478 | 487 |
| 488 exit_after_session_restore_ = false; |
479 // Remove legacy OAuth1 token if we have one. If it's valid, we should already | 489 // Remove legacy OAuth1 token if we have one. If it's valid, we should already |
480 // have OAuth2 refresh token in TokenService that could be used to retrieve | 490 // have OAuth2 refresh token in TokenService that could be used to retrieve |
481 // all other tokens and user_context. | 491 // all other tokens and user_context. |
482 OAuth2LoginManager* login_manager = | 492 OAuth2LoginManager* login_manager = |
483 OAuth2LoginManagerFactory::GetInstance()->GetForProfile(user_profile); | 493 OAuth2LoginManagerFactory::GetInstance()->GetForProfile(user_profile); |
484 login_manager->AddObserver(this); | 494 login_manager->AddObserver(this); |
485 login_manager->RestoreSession( | 495 login_manager->RestoreSession( |
486 authenticator_.get() && authenticator_->authentication_profile() | 496 authenticator_.get() && authenticator_->authentication_profile() |
487 ? authenticator_->authentication_profile()->GetRequestContext() | 497 ? authenticator_->authentication_profile()->GetRequestContext() |
488 : NULL, | 498 : NULL, |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
700 // OnConnectionTypeChanged() call. Otherwise starting fetchers too early may | 710 // OnConnectionTypeChanged() call. Otherwise starting fetchers too early may |
701 // end up canceling all request when initial network connection type is | 711 // end up canceling all request when initial network connection type is |
702 // processed. See http://crbug.com/121643. | 712 // processed. See http://crbug.com/121643. |
703 should_restore_auth_session_ = true; | 713 should_restore_auth_session_ = true; |
704 } | 714 } |
705 } | 715 } |
706 | 716 |
707 void LoginUtilsImpl::OnSessionRestoreStateChanged( | 717 void LoginUtilsImpl::OnSessionRestoreStateChanged( |
708 Profile* user_profile, | 718 Profile* user_profile, |
709 OAuth2LoginManager::SessionRestoreState state) { | 719 OAuth2LoginManager::SessionRestoreState state) { |
| 720 User::OAuthTokenStatus user_status = User::OAUTH_TOKEN_STATUS_UNKNOWN; |
710 OAuth2LoginManager* login_manager = | 721 OAuth2LoginManager* login_manager = |
711 OAuth2LoginManagerFactory::GetInstance()->GetForProfile(user_profile); | 722 OAuth2LoginManagerFactory::GetInstance()->GetForProfile(user_profile); |
712 switch (state) { | 723 switch (state) { |
713 case OAuth2LoginManager::SESSION_RESTORE_NOT_STARTED: | |
714 break; | |
715 case OAuth2LoginManager::SESSION_RESTORE_PREPARING: | |
716 break; | |
717 case OAuth2LoginManager::SESSION_RESTORE_IN_PROGRESS: | |
718 break; | |
719 case OAuth2LoginManager::SESSION_RESTORE_DONE: | 724 case OAuth2LoginManager::SESSION_RESTORE_DONE: |
720 UserManager::Get()->SaveUserOAuthStatus( | 725 user_status = User::OAUTH2_TOKEN_STATUS_VALID; |
721 UserManager::Get()->GetLoggedInUser()->email(), | |
722 User::OAUTH2_TOKEN_STATUS_VALID); | |
723 login_manager->RemoveObserver(this); | |
724 break; | 726 break; |
725 case OAuth2LoginManager::SESSION_RESTORE_FAILED: | 727 case OAuth2LoginManager::SESSION_RESTORE_FAILED: |
726 UserManager::Get()->SaveUserOAuthStatus( | 728 user_status = User::OAUTH2_TOKEN_STATUS_INVALID; |
727 UserManager::Get()->GetLoggedInUser()->email(), | |
728 User::OAUTH2_TOKEN_STATUS_INVALID); | |
729 login_manager->RemoveObserver(this); | |
730 break; | 729 break; |
| 730 case OAuth2LoginManager::SESSION_RESTORE_NOT_STARTED: |
| 731 return; |
| 732 case OAuth2LoginManager::SESSION_RESTORE_PREPARING: |
| 733 return; |
| 734 case OAuth2LoginManager::SESSION_RESTORE_IN_PROGRESS: |
| 735 return; |
731 } | 736 } |
| 737 |
| 738 // We are in one of "done" states here. |
| 739 UserManager::Get()->SaveUserOAuthStatus( |
| 740 UserManager::Get()->GetLoggedInUser()->email(), |
| 741 user_status); |
| 742 login_manager->RemoveObserver(this); |
| 743 } |
| 744 |
| 745 void LoginUtilsImpl::OnNewRefreshTokenAvaiable(Profile* user_profile) { |
| 746 // Check if we were waiting to restart chrome. |
| 747 if (!exit_after_session_restore_) |
| 748 return; |
| 749 |
| 750 OAuth2LoginManager* login_manager = |
| 751 OAuth2LoginManagerFactory::GetInstance()->GetForProfile(user_profile); |
| 752 login_manager->RemoveObserver(this); |
| 753 |
| 754 // Mark user auth token status as valid. |
| 755 UserManager::Get()->SaveUserOAuthStatus( |
| 756 UserManager::Get()->GetLoggedInUser()->email(), |
| 757 User::OAUTH2_TOKEN_STATUS_VALID); |
| 758 |
| 759 LOG(WARNING) << "Exiting after new refresh token fetched"; |
| 760 // We need to exit cleanly in this case to make sure OAuth2 RT is actually |
| 761 // saved. |
| 762 chrome::ExitCleanly(); |
732 } | 763 } |
733 | 764 |
734 void LoginUtilsImpl::OnSessionAuthenticated(Profile* user_profile) { | 765 void LoginUtilsImpl::OnSessionAuthenticated(Profile* user_profile) { |
735 StartSignedInServices(user_profile); | 766 StartSignedInServices(user_profile); |
736 } | 767 } |
737 | 768 |
738 void LoginUtilsImpl::OnConnectionTypeChanged( | 769 void LoginUtilsImpl::OnConnectionTypeChanged( |
739 net::NetworkChangeNotifier::ConnectionType type) { | 770 net::NetworkChangeNotifier::ConnectionType type) { |
740 Profile* user_profile = ProfileManager::GetDefaultProfile(); | 771 Profile* user_profile = ProfileManager::GetDefaultProfile(); |
741 OAuth2LoginManager* login_manager = | 772 OAuth2LoginManager* login_manager = |
742 OAuth2LoginManagerFactory::GetInstance()->GetForProfile(user_profile); | 773 OAuth2LoginManagerFactory::GetInstance()->GetForProfile(user_profile); |
743 | 774 |
744 if (type != net::NetworkChangeNotifier::CONNECTION_NONE && | 775 if (type != net::NetworkChangeNotifier::CONNECTION_NONE && |
745 UserManager::Get()->IsUserLoggedIn()) { | 776 UserManager::Get()->IsUserLoggedIn()) { |
746 if (login_manager->state() == | 777 if (login_manager->state() == |
747 OAuth2LoginManager::SESSION_RESTORE_IN_PROGRESS) { | 778 OAuth2LoginManager::SESSION_RESTORE_IN_PROGRESS) { |
748 // If we come online for the first time after successful offline login, | 779 // If we come online for the first time after successful offline login, |
749 // we need to kick off OAuth token verification process again. | 780 // we need to kick off OAuth token verification process again. |
750 login_manager->ContinueSessionRestore(); | 781 login_manager->ContinueSessionRestore(); |
751 } else if (should_restore_auth_session_) { | 782 } else if (should_restore_auth_session_) { |
752 should_restore_auth_session_ = false; | 783 should_restore_auth_session_ = false; |
753 RestoreAuthSession(user_profile, has_web_auth_cookies_); | 784 RestoreAuthSession(user_profile, has_web_auth_cookies_); |
754 } | 785 } |
755 } | 786 } |
756 } | 787 } |
757 | 788 |
| 789 void LoginUtilsImpl::AttemptExit(Profile* profile) { |
| 790 if (session_restore_strategy_ != |
| 791 OAuth2LoginManager::RESTORE_FROM_COOKIE_JAR) { |
| 792 chrome::AttemptExit(); |
| 793 return; |
| 794 } |
| 795 |
| 796 // We can't really quit if the session restore process that mints new |
| 797 // refresh token is still in progress. |
| 798 OAuth2LoginManager* login_manager = |
| 799 OAuth2LoginManagerFactory::GetInstance()->GetForProfile(profile); |
| 800 if (login_manager->state() != |
| 801 OAuth2LoginManager::SESSION_RESTORE_PREPARING && |
| 802 login_manager->state() != |
| 803 OAuth2LoginManager::SESSION_RESTORE_IN_PROGRESS) { |
| 804 chrome::AttemptExit(); |
| 805 return; |
| 806 } |
| 807 |
| 808 LOG(WARNING) << "Attempting browser restart during session restore."; |
| 809 exit_after_session_restore_ = true; |
| 810 } |
| 811 |
758 // static | 812 // static |
759 void LoginUtils::RegisterPrefs(PrefRegistrySimple* registry) { | 813 void LoginUtils::RegisterPrefs(PrefRegistrySimple* registry) { |
760 registry->RegisterBooleanPref(prefs::kFactoryResetRequested, false); | 814 registry->RegisterBooleanPref(prefs::kFactoryResetRequested, false); |
761 registry->RegisterStringPref(prefs::kRLZBrand, std::string()); | 815 registry->RegisterStringPref(prefs::kRLZBrand, std::string()); |
762 registry->RegisterBooleanPref(prefs::kRLZDisabled, false); | 816 registry->RegisterBooleanPref(prefs::kRLZDisabled, false); |
763 } | 817 } |
764 | 818 |
765 // static | 819 // static |
766 LoginUtils* LoginUtils::Get() { | 820 LoginUtils* LoginUtils::Get() { |
767 return LoginUtilsWrapper::GetInstance()->get(); | 821 return LoginUtilsWrapper::GetInstance()->get(); |
768 } | 822 } |
769 | 823 |
770 // static | 824 // static |
771 void LoginUtils::Set(LoginUtils* mock) { | 825 void LoginUtils::Set(LoginUtils* mock) { |
772 LoginUtilsWrapper::GetInstance()->reset(mock); | 826 LoginUtilsWrapper::GetInstance()->reset(mock); |
773 } | 827 } |
774 | 828 |
775 // static | 829 // static |
776 bool LoginUtils::IsWhitelisted(const std::string& username) { | 830 bool LoginUtils::IsWhitelisted(const std::string& username) { |
777 CrosSettings* cros_settings = CrosSettings::Get(); | 831 CrosSettings* cros_settings = CrosSettings::Get(); |
778 bool allow_new_user = false; | 832 bool allow_new_user = false; |
779 cros_settings->GetBoolean(kAccountsPrefAllowNewUser, &allow_new_user); | 833 cros_settings->GetBoolean(kAccountsPrefAllowNewUser, &allow_new_user); |
780 if (allow_new_user) | 834 if (allow_new_user) |
781 return true; | 835 return true; |
782 return cros_settings->FindEmailInList(kAccountsPrefUsers, username); | 836 return cros_settings->FindEmailInList(kAccountsPrefUsers, username); |
783 } | 837 } |
784 | 838 |
785 } // namespace chromeos | 839 } // namespace chromeos |
OLD | NEW |