| 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/signin/signin_manager.h" | 5 #include "chrome/browser/signin/signin_manager.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| 11 #include "base/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "chrome/browser/chrome_notification_types.h" | 15 #include "chrome/browser/chrome_notification_types.h" |
| 16 #include "chrome/browser/profiles/profile_io_data.h" | |
| 17 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
| 18 #include "chrome/browser/signin/signin_account_id_helper.h" | 16 #include "chrome/browser/signin/signin_account_id_helper.h" |
| 19 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 20 #include "chrome/common/profile_management_switches.h" | 18 #include "chrome/common/profile_management_switches.h" |
| 21 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 19 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 22 #include "components/signin/core/browser/signin_client.h" | 20 #include "components/signin/core/browser/signin_client.h" |
| 23 #include "components/signin/core/browser/signin_internals_util.h" | 21 #include "components/signin/core/browser/signin_internals_util.h" |
| 24 #include "components/signin/core/browser/signin_manager_cookie_helper.h" | 22 #include "components/signin/core/browser/signin_manager_cookie_helper.h" |
| 23 #include "components/signin/core/common/signin_pref_names.h" |
| 25 #include "content/public/browser/notification_service.h" | 24 #include "content/public/browser/notification_service.h" |
| 26 #include "google_apis/gaia/gaia_auth_util.h" | 25 #include "google_apis/gaia/gaia_auth_util.h" |
| 27 #include "google_apis/gaia/gaia_urls.h" | 26 #include "google_apis/gaia/gaia_urls.h" |
| 28 #include "net/base/escape.h" | 27 #include "net/base/escape.h" |
| 29 #include "third_party/icu/source/i18n/unicode/regex.h" | 28 #include "third_party/icu/source/i18n/unicode/regex.h" |
| 30 | 29 |
| 31 using namespace signin_internals_util; | 30 using namespace signin_internals_util; |
| 32 | 31 |
| 33 namespace { | 32 namespace { |
| 34 | 33 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 54 return false; | 53 return false; |
| 55 | 54 |
| 56 // Any login UI URLs with signin=chromiumsync should be considered a web | 55 // Any login UI URLs with signin=chromiumsync should be considered a web |
| 57 // URL (relies on GAIA keeping the "service=chromiumsync" query string | 56 // URL (relies on GAIA keeping the "service=chromiumsync" query string |
| 58 // fragment present even when embedding inside a "continue" parameter). | 57 // fragment present even when embedding inside a "continue" parameter). |
| 59 return net::UnescapeURLComponent( | 58 return net::UnescapeURLComponent( |
| 60 url.query(), net::UnescapeRule::URL_SPECIAL_CHARS) | 59 url.query(), net::UnescapeRule::URL_SPECIAL_CHARS) |
| 61 .find(kChromiumSyncService) != std::string::npos; | 60 .find(kChromiumSyncService) != std::string::npos; |
| 62 } | 61 } |
| 63 | 62 |
| 64 SigninManager::SigninManager(SigninClient* client) | 63 SigninManager::SigninManager(SigninClient* client, |
| 64 ProfileOAuth2TokenService* token_service) |
| 65 : SigninManagerBase(client), | 65 : SigninManagerBase(client), |
| 66 profile_(NULL), | 66 profile_(NULL), |
| 67 prohibit_signout_(false), | 67 prohibit_signout_(false), |
| 68 type_(SIGNIN_TYPE_NONE), | 68 type_(SIGNIN_TYPE_NONE), |
| 69 weak_pointer_factory_(this), | 69 weak_pointer_factory_(this), |
| 70 client_(client) {} | 70 client_(client), |
| 71 token_service_(token_service) {} |
| 71 | 72 |
| 72 void SigninManager::AddMergeSessionObserver( | 73 void SigninManager::AddMergeSessionObserver( |
| 73 MergeSessionHelper::Observer* observer) { | 74 MergeSessionHelper::Observer* observer) { |
| 74 if (merge_session_helper_) | 75 if (merge_session_helper_) |
| 75 merge_session_helper_->AddObserver(observer); | 76 merge_session_helper_->AddObserver(observer); |
| 76 } | 77 } |
| 77 | 78 |
| 78 void SigninManager::RemoveMergeSessionObserver( | 79 void SigninManager::RemoveMergeSessionObserver( |
| 79 MergeSessionHelper::Observer* observer) { | 80 MergeSessionHelper::Observer* observer) { |
| 80 if (merge_session_helper_) | 81 if (merge_session_helper_) |
| 81 merge_session_helper_->RemoveObserver(observer); | 82 merge_session_helper_->RemoveObserver(observer); |
| 82 } | 83 } |
| 83 | 84 |
| 84 SigninManager::~SigninManager() { | 85 SigninManager::~SigninManager() { |
| 85 } | 86 } |
| 86 | 87 |
| 87 void SigninManager::InitTokenService() { | 88 void SigninManager::InitTokenService() { |
| 88 ProfileOAuth2TokenService* token_service = | |
| 89 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | |
| 90 const std::string& account_id = GetAuthenticatedUsername(); | 89 const std::string& account_id = GetAuthenticatedUsername(); |
| 91 if (token_service && !account_id.empty()) | 90 if (token_service_ && !account_id.empty()) |
| 92 token_service->LoadCredentials(account_id); | 91 token_service_->LoadCredentials(account_id); |
| 93 } | 92 } |
| 94 | 93 |
| 95 std::string SigninManager::SigninTypeToString( | 94 std::string SigninManager::SigninTypeToString( |
| 96 SigninManager::SigninType type) { | 95 SigninManager::SigninType type) { |
| 97 switch (type) { | 96 switch (type) { |
| 98 case SIGNIN_TYPE_NONE: | 97 case SIGNIN_TYPE_NONE: |
| 99 return "No Signin"; | 98 return "No Signin"; |
| 100 case SIGNIN_TYPE_WITH_REFRESH_TOKEN: | 99 case SIGNIN_TYPE_WITH_REFRESH_TOKEN: |
| 101 return "Signin with refresh token"; | 100 return "Signin with refresh token"; |
| 102 } | 101 } |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 | 206 |
| 208 if (prohibit_signout_) { | 207 if (prohibit_signout_) { |
| 209 DVLOG(1) << "Ignoring attempt to sign out while signout is prohibited"; | 208 DVLOG(1) << "Ignoring attempt to sign out while signout is prohibited"; |
| 210 return; | 209 return; |
| 211 } | 210 } |
| 212 | 211 |
| 213 ClearTransientSigninData(); | 212 ClearTransientSigninData(); |
| 214 | 213 |
| 215 const std::string& username = GetAuthenticatedUsername(); | 214 const std::string& username = GetAuthenticatedUsername(); |
| 216 clear_authenticated_username(); | 215 clear_authenticated_username(); |
| 217 profile_->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername); | 216 client_->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername); |
| 218 | 217 |
| 219 // Erase (now) stale information from AboutSigninInternals. | 218 // Erase (now) stale information from AboutSigninInternals. |
| 220 NotifyDiagnosticsObservers(USERNAME, ""); | 219 NotifyDiagnosticsObservers(USERNAME, ""); |
| 221 | 220 |
| 222 // Revoke all tokens before sending signed_out notification, because there | 221 // Revoke all tokens before sending signed_out notification, because there |
| 223 // may be components that don't listen for token service events when the | 222 // may be components that don't listen for token service events when the |
| 224 // profile is not connected to an account. | 223 // profile is not connected to an account. |
| 225 ProfileOAuth2TokenService* token_service = | |
| 226 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | |
| 227 LOG(WARNING) << "Revoking refresh token on server. Reason: sign out, " | 224 LOG(WARNING) << "Revoking refresh token on server. Reason: sign out, " |
| 228 << "IsSigninAllowed: " << IsSigninAllowed(); | 225 << "IsSigninAllowed: " << IsSigninAllowed(); |
| 229 token_service->RevokeAllCredentials(); | 226 token_service_->RevokeAllCredentials(); |
| 230 | 227 |
| 231 // TODO(blundell): Eliminate this notification send once crbug.com/333997 is | 228 // TODO(blundell): Eliminate this notification send once crbug.com/333997 is |
| 232 // fixed. | 229 // fixed. |
| 233 GoogleServiceSignoutDetails details(username); | 230 GoogleServiceSignoutDetails details(username); |
| 234 content::NotificationService::current()->Notify( | 231 content::NotificationService::current()->Notify( |
| 235 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, | 232 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, |
| 236 content::Source<Profile>(profile_), | 233 content::Source<Profile>(profile_), |
| 237 content::Details<const GoogleServiceSignoutDetails>(&details)); | 234 content::Details<const GoogleServiceSignoutDetails>(&details)); |
| 238 | 235 |
| 239 FOR_EACH_OBSERVER(Observer, observer_list_, GoogleSignedOut(username)); | 236 FOR_EACH_OBSERVER(Observer, observer_list_, GoogleSignedOut(username)); |
| 240 } | 237 } |
| 241 | 238 |
| 242 void SigninManager::Initialize(Profile* profile, PrefService* local_state) { | 239 void SigninManager::Initialize(Profile* profile, PrefService* local_state) { |
| 243 profile_ = profile; | 240 profile_ = profile; |
| 244 SigninManagerBase::Initialize(profile, local_state); | 241 SigninManagerBase::Initialize(profile, local_state); |
| 245 | 242 |
| 246 // local_state can be null during unit tests. | 243 // local_state can be null during unit tests. |
| 247 if (local_state) { | 244 if (local_state) { |
| 248 local_state_pref_registrar_.Init(local_state); | 245 local_state_pref_registrar_.Init(local_state); |
| 249 local_state_pref_registrar_.Add( | 246 local_state_pref_registrar_.Add( |
| 250 prefs::kGoogleServicesUsernamePattern, | 247 prefs::kGoogleServicesUsernamePattern, |
| 251 base::Bind(&SigninManager::OnGoogleServicesUsernamePatternChanged, | 248 base::Bind(&SigninManager::OnGoogleServicesUsernamePatternChanged, |
| 252 weak_pointer_factory_.GetWeakPtr())); | 249 weak_pointer_factory_.GetWeakPtr())); |
| 253 } | 250 } |
| 254 signin_allowed_.Init(prefs::kSigninAllowed, profile_->GetPrefs(), | 251 signin_allowed_.Init(prefs::kSigninAllowed, |
| 255 base::Bind(&SigninManager::OnSigninAllowedPrefChanged, | 252 client_->GetPrefs(), |
| 256 base::Unretained(this))); | 253 base::Bind(&SigninManager::OnSigninAllowedPrefChanged, |
| 254 base::Unretained(this))); |
| 257 | 255 |
| 258 std::string user = profile_->GetPrefs()->GetString( | 256 std::string user = |
| 259 prefs::kGoogleServicesUsername); | 257 client_->GetPrefs()->GetString(prefs::kGoogleServicesUsername); |
| 260 if ((!user.empty() && !IsAllowedUsername(user)) || !IsSigninAllowed()) { | 258 if ((!user.empty() && !IsAllowedUsername(user)) || !IsSigninAllowed()) { |
| 261 // User is signed in, but the username is invalid - the administrator must | 259 // User is signed in, but the username is invalid - the administrator must |
| 262 // have changed the policy since the last signin, so sign out the user. | 260 // have changed the policy since the last signin, so sign out the user. |
| 263 SignOut(); | 261 SignOut(); |
| 264 } | 262 } |
| 265 | 263 |
| 266 InitTokenService(); | 264 InitTokenService(); |
| 267 account_id_helper_.reset(new SigninAccountIdHelper( | 265 account_id_helper_.reset( |
| 268 client_, | 266 new SigninAccountIdHelper(client_, token_service_, this)); |
| 269 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_), | |
| 270 this)); | |
| 271 } | 267 } |
| 272 | 268 |
| 273 void SigninManager::Shutdown() { | 269 void SigninManager::Shutdown() { |
| 274 if (merge_session_helper_) | 270 if (merge_session_helper_) |
| 275 merge_session_helper_->CancelAll(); | 271 merge_session_helper_->CancelAll(); |
| 276 | 272 |
| 277 local_state_pref_registrar_.RemoveAll(); | 273 local_state_pref_registrar_.RemoveAll(); |
| 278 account_id_helper_.reset(); | 274 account_id_helper_.reset(); |
| 279 SigninManagerBase::Shutdown(); | 275 SigninManagerBase::Shutdown(); |
| 280 } | 276 } |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 } | 337 } |
| 342 | 338 |
| 343 bool SigninManager::AuthInProgress() const { | 339 bool SigninManager::AuthInProgress() const { |
| 344 return !possibly_invalid_username_.empty(); | 340 return !possibly_invalid_username_.empty(); |
| 345 } | 341 } |
| 346 | 342 |
| 347 const std::string& SigninManager::GetUsernameForAuthInProgress() const { | 343 const std::string& SigninManager::GetUsernameForAuthInProgress() const { |
| 348 return possibly_invalid_username_; | 344 return possibly_invalid_username_; |
| 349 } | 345 } |
| 350 | 346 |
| 351 void SigninManager::DisableOneClickSignIn(Profile* profile) { | 347 void SigninManager::DisableOneClickSignIn(PrefService* prefs) { |
| 352 PrefService* pref_service = profile->GetPrefs(); | 348 prefs->SetBoolean(prefs::kReverseAutologinEnabled, false); |
| 353 pref_service->SetBoolean(prefs::kReverseAutologinEnabled, false); | |
| 354 } | 349 } |
| 355 | 350 |
| 356 void SigninManager::CompletePendingSignin() { | 351 void SigninManager::CompletePendingSignin() { |
| 357 DCHECK(!possibly_invalid_username_.empty()); | 352 DCHECK(!possibly_invalid_username_.empty()); |
| 358 OnSignedIn(possibly_invalid_username_); | 353 OnSignedIn(possibly_invalid_username_); |
| 359 | 354 |
| 360 ProfileOAuth2TokenService* token_service = | |
| 361 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | |
| 362 | |
| 363 // If inline sign in is enabled, but new profile management is not, perform a | 355 // If inline sign in is enabled, but new profile management is not, perform a |
| 364 // merge session now to push the user's credentials into the cookie jar. | 356 // merge session now to push the user's credentials into the cookie jar. |
| 365 bool do_merge_session_in_signin_manager = | 357 bool do_merge_session_in_signin_manager = |
| 366 !switches::IsEnableWebBasedSignin() && | 358 !switches::IsEnableWebBasedSignin() && |
| 367 !switches::IsNewProfileManagement(); | 359 !switches::IsNewProfileManagement(); |
| 368 | 360 |
| 369 if (do_merge_session_in_signin_manager) { | 361 if (do_merge_session_in_signin_manager) { |
| 370 merge_session_helper_.reset(new MergeSessionHelper( | 362 merge_session_helper_.reset(new MergeSessionHelper( |
| 371 token_service, profile_->GetRequestContext(), NULL)); | 363 token_service_, client_->GetURLRequestContext(), NULL)); |
| 372 } | 364 } |
| 373 | 365 |
| 374 DCHECK(!temp_refresh_token_.empty()); | 366 DCHECK(!temp_refresh_token_.empty()); |
| 375 DCHECK(!GetAuthenticatedUsername().empty()); | 367 DCHECK(!GetAuthenticatedUsername().empty()); |
| 376 token_service->UpdateCredentials(GetAuthenticatedUsername(), | 368 token_service_->UpdateCredentials(GetAuthenticatedUsername(), |
| 377 temp_refresh_token_); | 369 temp_refresh_token_); |
| 378 temp_refresh_token_.clear(); | 370 temp_refresh_token_.clear(); |
| 379 | 371 |
| 380 if (do_merge_session_in_signin_manager) | 372 if (do_merge_session_in_signin_manager) |
| 381 merge_session_helper_->LogIn(GetAuthenticatedUsername()); | 373 merge_session_helper_->LogIn(GetAuthenticatedUsername()); |
| 382 } | 374 } |
| 383 | 375 |
| 384 void SigninManager::OnExternalSigninCompleted(const std::string& username) { | 376 void SigninManager::OnExternalSigninCompleted(const std::string& username) { |
| 385 OnSignedIn(username); | 377 OnSignedIn(username); |
| 386 } | 378 } |
| 387 | 379 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 398 content::Source<Profile>(profile_), | 390 content::Source<Profile>(profile_), |
| 399 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); | 391 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); |
| 400 | 392 |
| 401 FOR_EACH_OBSERVER(Observer, observer_list_, | 393 FOR_EACH_OBSERVER(Observer, observer_list_, |
| 402 GoogleSigninSucceeded(GetAuthenticatedUsername(), | 394 GoogleSigninSucceeded(GetAuthenticatedUsername(), |
| 403 password_)); | 395 password_)); |
| 404 | 396 |
| 405 client_->GoogleSigninSucceeded(GetAuthenticatedUsername(), password_); | 397 client_->GoogleSigninSucceeded(GetAuthenticatedUsername(), password_); |
| 406 | 398 |
| 407 password_.clear(); // Don't need it anymore. | 399 password_.clear(); // Don't need it anymore. |
| 408 DisableOneClickSignIn(profile_); // Don't ever offer again. | 400 DisableOneClickSignIn(client_->GetPrefs()); // Don't ever offer again. |
| 409 } | 401 } |
| 410 | 402 |
| 411 void SigninManager::ProhibitSignout(bool prohibit_signout) { | 403 void SigninManager::ProhibitSignout(bool prohibit_signout) { |
| 412 prohibit_signout_ = prohibit_signout; | 404 prohibit_signout_ = prohibit_signout; |
| 413 } | 405 } |
| 414 | 406 |
| 415 bool SigninManager::IsSignoutProhibited() const { | 407 bool SigninManager::IsSignoutProhibited() const { |
| 416 return prohibit_signout_; | 408 return prohibit_signout_; |
| 417 } | 409 } |
| OLD | NEW |