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 "ash/ash_switches.h" | 10 #include "ash/ash_switches.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 #include "chrome/common/logging_chrome.h" | 66 #include "chrome/common/logging_chrome.h" |
67 #include "chrome/common/net/gaia/gaia_auth_consumer.h" | 67 #include "chrome/common/net/gaia/gaia_auth_consumer.h" |
68 #include "chrome/common/net/gaia/gaia_urls.h" | 68 #include "chrome/common/net/gaia/gaia_urls.h" |
69 #include "chrome/common/pref_names.h" | 69 #include "chrome/common/pref_names.h" |
70 #include "chrome/common/url_constants.h" | 70 #include "chrome/common/url_constants.h" |
71 #include "chromeos/chromeos_switches.h" | 71 #include "chromeos/chromeos_switches.h" |
72 #include "chromeos/dbus/dbus_thread_manager.h" | 72 #include "chromeos/dbus/dbus_thread_manager.h" |
73 #include "chromeos/dbus/session_manager_client.h" | 73 #include "chromeos/dbus/session_manager_client.h" |
74 #include "content/public/browser/browser_thread.h" | 74 #include "content/public/browser/browser_thread.h" |
75 #include "content/public/browser/notification_service.h" | 75 #include "content/public/browser/notification_service.h" |
76 #include "content/public/browser/notification_observer.h" | |
77 #include "googleurl/src/gurl.h" | 76 #include "googleurl/src/gurl.h" |
78 #include "media/base/media_switches.h" | 77 #include "media/base/media_switches.h" |
79 #include "net/base/network_change_notifier.h" | 78 #include "net/base/network_change_notifier.h" |
80 #include "net/cookies/cookie_monster.h" | 79 #include "net/cookies/cookie_monster.h" |
81 #include "net/cookies/cookie_store.h" | 80 #include "net/cookies/cookie_store.h" |
82 #include "net/http/http_auth_cache.h" | 81 #include "net/http/http_auth_cache.h" |
83 #include "net/http/http_network_session.h" | 82 #include "net/http/http_network_session.h" |
84 #include "net/http/http_transaction_factory.h" | 83 #include "net/http/http_transaction_factory.h" |
85 #include "net/url_request/url_request_context.h" | 84 #include "net/url_request/url_request_context.h" |
86 #include "net/url_request/url_request_context_getter.h" | 85 #include "net/url_request/url_request_context_getter.h" |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 std::string command_line_; | 219 std::string command_line_; |
221 PrefService* local_state_; | 220 PrefService* local_state_; |
222 base::OneShotTimer<JobRestartRequest> timer_; | 221 base::OneShotTimer<JobRestartRequest> timer_; |
223 }; | 222 }; |
224 | 223 |
225 class LoginUtilsImpl | 224 class LoginUtilsImpl |
226 : public LoginUtils, | 225 : public LoginUtils, |
227 public OAuth1TokenFetcher::Delegate, | 226 public OAuth1TokenFetcher::Delegate, |
228 public OAuthLoginVerifier::Delegate, | 227 public OAuthLoginVerifier::Delegate, |
229 public net::NetworkChangeNotifier::ConnectionTypeObserver, | 228 public net::NetworkChangeNotifier::ConnectionTypeObserver, |
230 public content::NotificationObserver, | |
231 public base::SupportsWeakPtr<LoginUtilsImpl> { | 229 public base::SupportsWeakPtr<LoginUtilsImpl> { |
232 public: | 230 public: |
233 LoginUtilsImpl() | 231 LoginUtilsImpl() |
234 : pending_requests_(false), | 232 : pending_requests_(false), |
235 using_oauth_(false), | 233 using_oauth_(false), |
236 has_cookies_(false), | 234 has_cookies_(false), |
237 delegate_(NULL), | 235 delegate_(NULL), |
238 job_restart_request_(NULL), | 236 job_restart_request_(NULL), |
239 should_restore_auth_session_(false), | 237 should_restore_auth_session_(false) { |
240 url_request_context_getter_(NULL) { | |
241 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); | 238 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); |
242 registrar_.Add( | |
243 this, | |
244 chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED, | |
245 content::Source<Profile>(ProfileManager::GetDefaultProfile())); | |
246 } | 239 } |
247 | 240 |
248 virtual ~LoginUtilsImpl() { | 241 virtual ~LoginUtilsImpl() { |
249 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | 242 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
250 } | 243 } |
251 | 244 |
252 // LoginUtils implementation: | 245 // LoginUtils implementation: |
253 virtual void DoBrowserLaunch(Profile* profile, | 246 virtual void DoBrowserLaunch(Profile* profile, |
254 LoginDisplayHost* login_host) OVERRIDE; | 247 LoginDisplayHost* login_host) OVERRIDE; |
255 virtual void PrepareProfile( | 248 virtual void PrepareProfile( |
(...skipping 30 matching lines...) Expand all Loading... |
286 virtual void OnOAuthVerificationSucceeded(const std::string& user_name, | 279 virtual void OnOAuthVerificationSucceeded(const std::string& user_name, |
287 const std::string& sid, | 280 const std::string& sid, |
288 const std::string& lsid, | 281 const std::string& lsid, |
289 const std::string& auth) OVERRIDE; | 282 const std::string& auth) OVERRIDE; |
290 virtual void OnOAuthVerificationFailed(const std::string& user_name) OVERRIDE; | 283 virtual void OnOAuthVerificationFailed(const std::string& user_name) OVERRIDE; |
291 | 284 |
292 // net::NetworkChangeNotifier::ConnectionTypeObserver overrides. | 285 // net::NetworkChangeNotifier::ConnectionTypeObserver overrides. |
293 virtual void OnConnectionTypeChanged( | 286 virtual void OnConnectionTypeChanged( |
294 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE; | 287 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE; |
295 | 288 |
296 // content::NotificationObserver overrides. | |
297 virtual void Observe(int type, | |
298 const content::NotificationSource& source, | |
299 const content::NotificationDetails& details) OVERRIDE; | |
300 | |
301 protected: | 289 protected: |
302 virtual std::string GetOffTheRecordCommandLine( | 290 virtual std::string GetOffTheRecordCommandLine( |
303 const GURL& start_url, | 291 const GURL& start_url, |
304 const CommandLine& base_command_line, | 292 const CommandLine& base_command_line, |
305 CommandLine *command_line); | 293 CommandLine *command_line); |
306 | 294 |
307 private: | 295 private: |
308 // Restarts OAuth session authentication check. | 296 // Restarts OAuth session authentication check. |
309 void KickStartAuthentication(Profile* profile); | 297 void KickStartAuthentication(Profile* profile); |
310 | 298 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 // Delegate to be fired when the profile will be prepared. | 347 // Delegate to be fired when the profile will be prepared. |
360 LoginUtils::Delegate* delegate_; | 348 LoginUtils::Delegate* delegate_; |
361 | 349 |
362 // Used to restart Chrome to switch to the guest mode. | 350 // Used to restart Chrome to switch to the guest mode. |
363 JobRestartRequest* job_restart_request_; | 351 JobRestartRequest* job_restart_request_; |
364 | 352 |
365 // True if should restore authentication session when notified about | 353 // True if should restore authentication session when notified about |
366 // online state change. | 354 // online state change. |
367 bool should_restore_auth_session_; | 355 bool should_restore_auth_session_; |
368 | 356 |
369 content::NotificationRegistrar registrar_; | |
370 | |
371 // This is set via a notification after the profile has initialized the | |
372 // getter. | |
373 net::URLRequestContextGetter* url_request_context_getter_; | |
374 | |
375 DISALLOW_COPY_AND_ASSIGN(LoginUtilsImpl); | 357 DISALLOW_COPY_AND_ASSIGN(LoginUtilsImpl); |
376 }; | 358 }; |
377 | 359 |
378 class LoginUtilsWrapper { | 360 class LoginUtilsWrapper { |
379 public: | 361 public: |
380 static LoginUtilsWrapper* GetInstance() { | 362 static LoginUtilsWrapper* GetInstance() { |
381 return Singleton<LoginUtilsWrapper>::get(); | 363 return Singleton<LoginUtilsWrapper>::get(); |
382 } | 364 } |
383 | 365 |
384 LoginUtils* get() { | 366 LoginUtils* get() { |
(...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
873 authenticator_ = NULL; | 855 authenticator_ = NULL; |
874 | 856 |
875 if (authenticator_ == NULL) | 857 if (authenticator_ == NULL) |
876 authenticator_ = new ParallelAuthenticator(consumer); | 858 authenticator_ = new ParallelAuthenticator(consumer); |
877 return authenticator_; | 859 return authenticator_; |
878 } | 860 } |
879 | 861 |
880 // We use a special class for this so that it can be safely leaked if we | 862 // We use a special class for this so that it can be safely leaked if we |
881 // never connect. At shutdown the order is not well defined, and it's possible | 863 // never connect. At shutdown the order is not well defined, and it's possible |
882 // for the infrastructure needed to unregister might be unstable and crash. | 864 // for the infrastructure needed to unregister might be unstable and crash. |
883 class WarmingObserver : public NetworkLibrary::NetworkManagerObserver, | 865 class WarmingObserver : public NetworkLibrary::NetworkManagerObserver { |
884 public content::NotificationObserver { | |
885 public: | 866 public: |
886 WarmingObserver() | 867 WarmingObserver() { |
887 : url_request_context_getter_(NULL) { | |
888 NetworkLibrary *netlib = CrosLibrary::Get()->GetNetworkLibrary(); | 868 NetworkLibrary *netlib = CrosLibrary::Get()->GetNetworkLibrary(); |
889 netlib->AddNetworkManagerObserver(this); | 869 netlib->AddNetworkManagerObserver(this); |
890 } | 870 } |
891 | 871 |
892 virtual ~WarmingObserver() {} | 872 virtual ~WarmingObserver() {} |
893 | 873 |
894 // If we're now connected, prewarm the auth url. | 874 // If we're now connected, prewarm the auth url. |
895 virtual void OnNetworkManagerChanged(NetworkLibrary* netlib) { | 875 virtual void OnNetworkManagerChanged(NetworkLibrary* netlib) { |
896 if (netlib->Connected()) { | 876 if (netlib->Connected()) { |
897 const int kConnectionsNeeded = 1; | 877 const int kConnectionsNeeded = 1; |
898 chrome_browser_net::PreconnectOnUIThread( | 878 chrome_browser_net::PreconnectOnUIThread( |
899 GURL(GaiaUrls::GetInstance()->client_login_url()), | 879 GURL(GaiaUrls::GetInstance()->client_login_url()), |
900 chrome_browser_net::UrlInfo::EARLY_LOAD_MOTIVATED, | 880 chrome_browser_net::UrlInfo::EARLY_LOAD_MOTIVATED, |
901 kConnectionsNeeded, | 881 kConnectionsNeeded, |
902 url_request_context_getter_); | 882 make_scoped_refptr(Profile::GetDefaultRequestContextDeprecated())); |
903 netlib->RemoveNetworkManagerObserver(this); | 883 netlib->RemoveNetworkManagerObserver(this); |
904 delete this; | 884 delete this; |
905 } | 885 } |
906 } | 886 } |
907 | |
908 // content::NotificationObserver overrides. | |
909 virtual void Observe(int type, | |
910 const content::NotificationSource& source, | |
911 const content::NotificationDetails& details) { | |
912 switch (type) { | |
913 case chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED: { | |
914 Profile* profile = content::Source<Profile>(source).ptr(); | |
915 url_request_context_getter_ = profile->GetRequestContext(); | |
916 break; | |
917 } | |
918 default: | |
919 NOTREACHED(); | |
920 } | |
921 } | |
922 private: | |
923 net::URLRequestContextGetter* url_request_context_getter_; | |
924 }; | 887 }; |
925 | 888 |
926 void LoginUtilsImpl::PrewarmAuthentication() { | 889 void LoginUtilsImpl::PrewarmAuthentication() { |
927 NetworkLibrary *network = CrosLibrary::Get()->GetNetworkLibrary(); | 890 NetworkLibrary *network = CrosLibrary::Get()->GetNetworkLibrary(); |
928 if (network->Connected()) { | 891 if (network->Connected()) { |
929 const int kConnectionsNeeded = 1; | 892 const int kConnectionsNeeded = 1; |
930 chrome_browser_net::PreconnectOnUIThread( | 893 chrome_browser_net::PreconnectOnUIThread( |
931 GURL(GaiaUrls::GetInstance()->client_login_url()), | 894 GURL(GaiaUrls::GetInstance()->client_login_url()), |
932 chrome_browser_net::UrlInfo::EARLY_LOAD_MOTIVATED, | 895 chrome_browser_net::UrlInfo::EARLY_LOAD_MOTIVATED, |
933 kConnectionsNeeded, | 896 kConnectionsNeeded, |
934 url_request_context_getter_); | 897 make_scoped_refptr(Profile::GetDefaultRequestContextDeprecated())); |
935 } else { | 898 } else { |
936 new WarmingObserver(); | 899 new WarmingObserver(); |
937 } | 900 } |
938 } | 901 } |
939 | 902 |
940 void LoginUtilsImpl::RestoreAuthenticationSession(Profile* user_profile) { | 903 void LoginUtilsImpl::RestoreAuthenticationSession(Profile* user_profile) { |
941 // We don't need to restore session for demo/guest users. | 904 // We don't need to restore session for demo/guest users. |
942 if (!UserManager::Get()->IsUserLoggedIn() || | 905 if (!UserManager::Get()->IsUserLoggedIn() || |
943 UserManager::Get()->IsLoggedInAsGuest() || | 906 UserManager::Get()->IsLoggedInAsGuest() || |
944 UserManager::Get()->IsLoggedInAsDemoUser()) { | 907 UserManager::Get()->IsLoggedInAsDemoUser()) { |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1138 // we need to kick off OAuth token verification process again. | 1101 // we need to kick off OAuth token verification process again. |
1139 oauth_login_verifier_->ContinueVerification(); | 1102 oauth_login_verifier_->ContinueVerification(); |
1140 } else if (should_restore_auth_session_) { | 1103 } else if (should_restore_auth_session_) { |
1141 should_restore_auth_session_ = false; | 1104 should_restore_auth_session_ = false; |
1142 Profile* user_profile = ProfileManager::GetDefaultProfile(); | 1105 Profile* user_profile = ProfileManager::GetDefaultProfile(); |
1143 KickStartAuthentication(user_profile); | 1106 KickStartAuthentication(user_profile); |
1144 } | 1107 } |
1145 } | 1108 } |
1146 } | 1109 } |
1147 | 1110 |
1148 void LoginUtilsImpl::Observe(int type, | |
1149 const content::NotificationSource& source, | |
1150 const content::NotificationDetails& details) { | |
1151 switch (type) { | |
1152 case chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED: { | |
1153 Profile* profile = content::Source<Profile>(source).ptr(); | |
1154 url_request_context_getter_ = profile->GetRequestContext(); | |
1155 break; | |
1156 } | |
1157 default: | |
1158 NOTREACHED(); | |
1159 } | |
1160 } | |
1161 | |
1162 // static | 1111 // static |
1163 LoginUtils* LoginUtils::Get() { | 1112 LoginUtils* LoginUtils::Get() { |
1164 return LoginUtilsWrapper::GetInstance()->get(); | 1113 return LoginUtilsWrapper::GetInstance()->get(); |
1165 } | 1114 } |
1166 | 1115 |
1167 // static | 1116 // static |
1168 void LoginUtils::Set(LoginUtils* mock) { | 1117 void LoginUtils::Set(LoginUtils* mock) { |
1169 LoginUtilsWrapper::GetInstance()->reset(mock); | 1118 LoginUtilsWrapper::GetInstance()->reset(mock); |
1170 } | 1119 } |
1171 | 1120 |
1172 // static | 1121 // static |
1173 bool LoginUtils::IsWhitelisted(const std::string& username) { | 1122 bool LoginUtils::IsWhitelisted(const std::string& username) { |
1174 CrosSettings* cros_settings = CrosSettings::Get(); | 1123 CrosSettings* cros_settings = CrosSettings::Get(); |
1175 bool allow_new_user = false; | 1124 bool allow_new_user = false; |
1176 cros_settings->GetBoolean(kAccountsPrefAllowNewUser, &allow_new_user); | 1125 cros_settings->GetBoolean(kAccountsPrefAllowNewUser, &allow_new_user); |
1177 if (allow_new_user) | 1126 if (allow_new_user) |
1178 return true; | 1127 return true; |
1179 return cros_settings->FindEmailInList(kAccountsPrefUsers, username); | 1128 return cros_settings->FindEmailInList(kAccountsPrefUsers, username); |
1180 } | 1129 } |
1181 | 1130 |
1182 } // namespace chromeos | 1131 } // namespace chromeos |
OLD | NEW |