| 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/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
| 14 #include "base/string_split.h" | 14 #include "base/string_split.h" |
| 15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
| 16 #include "base/time.h" | 16 #include "base/time.h" |
| 17 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
| 18 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
| 19 #include "chrome/browser/content_settings/cookie_settings.h" | 19 #include "chrome/browser/content_settings/cookie_settings.h" |
| 20 #include "chrome/browser/profiles/profile_info_cache.h" | 20 #include "chrome/browser/profiles/profile_info_cache.h" |
| 21 #include "chrome/browser/profiles/profile_io_data.h" |
| 21 #include "chrome/browser/profiles/profile_manager.h" | 22 #include "chrome/browser/profiles/profile_manager.h" |
| 22 #include "chrome/browser/signin/about_signin_internals.h" | 23 #include "chrome/browser/signin/about_signin_internals.h" |
| 23 #include "chrome/browser/signin/about_signin_internals_factory.h" | 24 #include "chrome/browser/signin/about_signin_internals_factory.h" |
| 24 #include "chrome/browser/signin/signin_global_error.h" | 25 #include "chrome/browser/signin/signin_global_error.h" |
| 25 #include "chrome/browser/signin/signin_internals_util.h" | 26 #include "chrome/browser/signin/signin_internals_util.h" |
| 26 #include "chrome/browser/signin/signin_manager_factory.h" | 27 #include "chrome/browser/signin/signin_manager_factory.h" |
| 27 #include "chrome/browser/signin/token_service.h" | 28 #include "chrome/browser/signin/token_service.h" |
| 28 #include "chrome/browser/signin/token_service_factory.h" | 29 #include "chrome/browser/signin/token_service_factory.h" |
| 29 #include "chrome/browser/sync/profile_sync_service.h" | 30 #include "chrome/browser/sync/profile_sync_service.h" |
| 30 #include "chrome/browser/sync/sync_prefs.h" | 31 #include "chrome/browser/sync/sync_prefs.h" |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 signin_global_error_.get()); | 217 signin_global_error_.get()); |
| 217 PrefService* local_state = g_browser_process->local_state(); | 218 PrefService* local_state = g_browser_process->local_state(); |
| 218 // local_state can be null during unit tests. | 219 // local_state can be null during unit tests. |
| 219 if (local_state) { | 220 if (local_state) { |
| 220 local_state_pref_registrar_.Init(local_state); | 221 local_state_pref_registrar_.Init(local_state); |
| 221 local_state_pref_registrar_.Add( | 222 local_state_pref_registrar_.Add( |
| 222 prefs::kGoogleServicesUsernamePattern, | 223 prefs::kGoogleServicesUsernamePattern, |
| 223 base::Bind(&SigninManager::OnGoogleServicesUsernamePatternChanged, | 224 base::Bind(&SigninManager::OnGoogleServicesUsernamePatternChanged, |
| 224 weak_pointer_factory_.GetWeakPtr())); | 225 weak_pointer_factory_.GetWeakPtr())); |
| 225 } | 226 } |
| 227 signin_allowed_.Init(prefs::kSigninAllowed, profile_->GetPrefs(), |
| 228 base::Bind(&SigninManager::OnSigninAllowedPrefChanged, |
| 229 base::Unretained(this))); |
| 226 | 230 |
| 227 // If the user is clearing the token service from the command line, then | 231 // If the user is clearing the token service from the command line, then |
| 228 // clear their login info also (not valid to be logged in without any | 232 // clear their login info also (not valid to be logged in without any |
| 229 // tokens). | 233 // tokens). |
| 230 CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | 234 CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
| 231 if (cmd_line->HasSwitch(switches::kClearTokenService)) | 235 if (cmd_line->HasSwitch(switches::kClearTokenService)) |
| 232 profile->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername); | 236 profile->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername); |
| 233 | 237 |
| 234 std::string user = profile_->GetPrefs()->GetString( | 238 std::string user = profile_->GetPrefs()->GetString( |
| 235 prefs::kGoogleServicesUsername); | 239 prefs::kGoogleServicesUsername); |
| 236 if (!user.empty()) | 240 if (!user.empty()) |
| 237 SetAuthenticatedUsername(user); | 241 SetAuthenticatedUsername(user); |
| 238 // TokenService can be null for unit tests. | 242 // TokenService can be null for unit tests. |
| 239 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); | 243 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); |
| 240 if (token_service) { | 244 if (token_service) { |
| 241 token_service->Initialize(GaiaConstants::kChromeSource, profile_); | 245 token_service->Initialize(GaiaConstants::kChromeSource, profile_); |
| 242 // ChromeOS will kick off TokenService::LoadTokensFromDB from | 246 // ChromeOS will kick off TokenService::LoadTokensFromDB from |
| 243 // OAuthLoginManager once the rest of the Profile is fully initialized. | 247 // OAuthLoginManager once the rest of the Profile is fully initialized. |
| 244 // Starting it from here would cause OAuthLoginManager mismatch the origin | 248 // Starting it from here would cause OAuthLoginManager mismatch the origin |
| 245 // of OAuth2 tokens. | 249 // of OAuth2 tokens. |
| 246 #if !defined(OS_CHROMEOS) | 250 #if !defined(OS_CHROMEOS) |
| 247 if (!authenticated_username_.empty()) { | 251 if (!authenticated_username_.empty()) { |
| 248 token_service->LoadTokensFromDB(); | 252 token_service->LoadTokensFromDB(); |
| 249 } | 253 } |
| 250 #endif | 254 #endif |
| 251 } | 255 } |
| 252 if (!user.empty() && !IsAllowedUsername(user)) { | 256 if ((!user.empty() && !IsAllowedUsername(user)) || !IsSigninAllowed()) { |
| 253 // User is signed in, but the username is invalid - the administrator must | 257 // User is signed in, but the username is invalid - the administrator must |
| 254 // have changed the policy since the last signin, so sign out the user. | 258 // have changed the policy since the last signin, so sign out the user. |
| 255 SignOut(); | 259 SignOut(); |
| 256 } | 260 } |
| 257 } | 261 } |
| 258 | 262 |
| 259 bool SigninManager::IsInitialized() const { | 263 bool SigninManager::IsInitialized() const { |
| 260 return profile_ != NULL; | 264 return profile_ != NULL; |
| 261 } | 265 } |
| 262 | 266 |
| 263 bool SigninManager::IsAllowedUsername(const std::string& username) const { | 267 bool SigninManager::IsAllowedUsername(const std::string& username) const { |
| 264 PrefService* local_state = g_browser_process->local_state(); | 268 PrefService* local_state = g_browser_process->local_state(); |
| 265 if (!local_state) | 269 if (!local_state) |
| 266 return true; // In a unit test with no local state - all names are allowed. | 270 return true; // In a unit test with no local state - all names are allowed. |
| 267 | 271 |
| 268 std::string pattern = local_state->GetString( | 272 std::string pattern = local_state->GetString( |
| 269 prefs::kGoogleServicesUsernamePattern); | 273 prefs::kGoogleServicesUsernamePattern); |
| 270 return IsAllowedUsername(username, pattern); | 274 return IsAllowedUsername(username, pattern); |
| 271 } | 275 } |
| 272 | 276 |
| 277 bool SigninManager::IsSigninAllowed() const { |
| 278 return signin_allowed_.GetValue(); |
| 279 } |
| 280 |
| 281 // static |
| 282 bool SigninManager::IsSigninAllowedOnIOThread(ProfileIOData* io_data) { |
| 283 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 284 return io_data->signin_allowed()->GetValue(); |
| 285 } |
| 286 |
| 273 void SigninManager::CleanupNotificationRegistration() { | 287 void SigninManager::CleanupNotificationRegistration() { |
| 274 #if !defined(OS_CHROMEOS) | 288 #if !defined(OS_CHROMEOS) |
| 275 content::Source<TokenService> token_service( | 289 content::Source<TokenService> token_service( |
| 276 TokenServiceFactory::GetForProfile(profile_)); | 290 TokenServiceFactory::GetForProfile(profile_)); |
| 277 if (registrar_.IsRegistered(this, | 291 if (registrar_.IsRegistered(this, |
| 278 chrome::NOTIFICATION_TOKEN_AVAILABLE, | 292 chrome::NOTIFICATION_TOKEN_AVAILABLE, |
| 279 token_service)) { | 293 token_service)) { |
| 280 registrar_.Remove(this, | 294 registrar_.Remove(this, |
| 281 chrome::NOTIFICATION_TOKEN_AVAILABLE, | 295 chrome::NOTIFICATION_TOKEN_AVAILABLE, |
| 282 token_service); | 296 token_service); |
| (...skipping 626 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 909 | 923 |
| 910 void SigninManager::OnGoogleServicesUsernamePatternChanged() { | 924 void SigninManager::OnGoogleServicesUsernamePatternChanged() { |
| 911 if (!authenticated_username_.empty() && | 925 if (!authenticated_username_.empty() && |
| 912 !IsAllowedUsername(authenticated_username_)) { | 926 !IsAllowedUsername(authenticated_username_)) { |
| 913 // Signed in user is invalid according to the current policy so sign | 927 // Signed in user is invalid according to the current policy so sign |
| 914 // the user out. | 928 // the user out. |
| 915 SignOut(); | 929 SignOut(); |
| 916 } | 930 } |
| 917 } | 931 } |
| 918 | 932 |
| 933 void SigninManager::OnSigninAllowedPrefChanged() { |
| 934 if (!IsSigninAllowed()) |
| 935 SignOut(); |
| 936 } |
| 937 |
| 919 void SigninManager::AddSigninDiagnosticsObserver( | 938 void SigninManager::AddSigninDiagnosticsObserver( |
| 920 SigninDiagnosticsObserver* observer) { | 939 SigninDiagnosticsObserver* observer) { |
| 921 signin_diagnostics_observers_.AddObserver(observer); | 940 signin_diagnostics_observers_.AddObserver(observer); |
| 922 } | 941 } |
| 923 | 942 |
| 924 void SigninManager::RemoveSigninDiagnosticsObserver( | 943 void SigninManager::RemoveSigninDiagnosticsObserver( |
| 925 SigninDiagnosticsObserver* observer) { | 944 SigninDiagnosticsObserver* observer) { |
| 926 signin_diagnostics_observers_.RemoveObserver(observer); | 945 signin_diagnostics_observers_.RemoveObserver(observer); |
| 927 } | 946 } |
| 928 | 947 |
| 929 void SigninManager::NotifyDiagnosticsObservers( | 948 void SigninManager::NotifyDiagnosticsObservers( |
| 930 const UntimedSigninStatusField& field, | 949 const UntimedSigninStatusField& field, |
| 931 const std::string& value) { | 950 const std::string& value) { |
| 932 FOR_EACH_OBSERVER(SigninDiagnosticsObserver, | 951 FOR_EACH_OBSERVER(SigninDiagnosticsObserver, |
| 933 signin_diagnostics_observers_, | 952 signin_diagnostics_observers_, |
| 934 NotifySigninValueChanged(field, value)); | 953 NotifySigninValueChanged(field, value)); |
| 935 } | 954 } |
| 936 | 955 |
| 937 void SigninManager::NotifyDiagnosticsObservers( | 956 void SigninManager::NotifyDiagnosticsObservers( |
| 938 const TimedSigninStatusField& field, | 957 const TimedSigninStatusField& field, |
| 939 const std::string& value) { | 958 const std::string& value) { |
| 940 FOR_EACH_OBSERVER(SigninDiagnosticsObserver, | 959 FOR_EACH_OBSERVER(SigninDiagnosticsObserver, |
| 941 signin_diagnostics_observers_, | 960 signin_diagnostics_observers_, |
| 942 NotifySigninValueChanged(field, value)); | 961 NotifySigninValueChanged(field, value)); |
| 943 } | 962 } |
| OLD | NEW |