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/string_split.h" | 13 #include "base/string_split.h" |
14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
15 #include "base/time.h" | 15 #include "base/time.h" |
16 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
17 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
18 #include "chrome/browser/content_settings/cookie_settings.h" | 18 #include "chrome/browser/content_settings/cookie_settings.h" |
19 #include "chrome/browser/prefs/pref_service.h" | 19 #include "chrome/browser/prefs/pref_service.h" |
20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/profiles/profile_io_data.h" |
21 #include "chrome/browser/signin/about_signin_internals.h" | 22 #include "chrome/browser/signin/about_signin_internals.h" |
22 #include "chrome/browser/signin/about_signin_internals_factory.h" | 23 #include "chrome/browser/signin/about_signin_internals_factory.h" |
23 #include "chrome/browser/signin/signin_global_error.h" | 24 #include "chrome/browser/signin/signin_global_error.h" |
24 #include "chrome/browser/signin/signin_internals_util.h" | 25 #include "chrome/browser/signin/signin_internals_util.h" |
25 #include "chrome/browser/signin/token_service.h" | 26 #include "chrome/browser/signin/token_service.h" |
26 #include "chrome/browser/signin/token_service_factory.h" | 27 #include "chrome/browser/signin/token_service_factory.h" |
27 #include "chrome/browser/sync/profile_sync_service.h" | 28 #include "chrome/browser/sync/profile_sync_service.h" |
28 #include "chrome/browser/ui/global_error/global_error_service.h" | 29 #include "chrome/browser/ui/global_error/global_error_service.h" |
29 #include "chrome/browser/ui/global_error/global_error_service_factory.h" | 30 #include "chrome/browser/ui/global_error/global_error_service_factory.h" |
30 #include "chrome/common/chrome_notification_types.h" | 31 #include "chrome/common/chrome_notification_types.h" |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 signin_global_error_.get()); | 205 signin_global_error_.get()); |
205 PrefService* local_state = g_browser_process->local_state(); | 206 PrefService* local_state = g_browser_process->local_state(); |
206 // local_state can be null during unit tests. | 207 // local_state can be null during unit tests. |
207 if (local_state) { | 208 if (local_state) { |
208 local_state_pref_registrar_.Init(local_state); | 209 local_state_pref_registrar_.Init(local_state); |
209 local_state_pref_registrar_.Add( | 210 local_state_pref_registrar_.Add( |
210 prefs::kGoogleServicesUsernamePattern, | 211 prefs::kGoogleServicesUsernamePattern, |
211 base::Bind(&SigninManager::OnGoogleServicesUsernamePatternChanged, | 212 base::Bind(&SigninManager::OnGoogleServicesUsernamePatternChanged, |
212 base::Unretained(this))); | 213 base::Unretained(this))); |
213 } | 214 } |
| 215 signin_allowed_.Init(prefs::kSigninAllowed, profile_->GetPrefs(), |
| 216 base::Bind(&SigninManager::OnSigninAllowedPrefChanged, |
| 217 base::Unretained(this))); |
214 | 218 |
215 // If the user is clearing the token service from the command line, then | 219 // If the user is clearing the token service from the command line, then |
216 // clear their login info also (not valid to be logged in without any | 220 // clear their login info also (not valid to be logged in without any |
217 // tokens). | 221 // tokens). |
218 CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | 222 CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
219 if (cmd_line->HasSwitch(switches::kClearTokenService)) | 223 if (cmd_line->HasSwitch(switches::kClearTokenService)) |
220 profile->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername); | 224 profile->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername); |
221 | 225 |
222 std::string user = profile_->GetPrefs()->GetString( | 226 std::string user = profile_->GetPrefs()->GetString( |
223 prefs::kGoogleServicesUsername); | 227 prefs::kGoogleServicesUsername); |
224 if (!user.empty()) | 228 if (!user.empty()) |
225 SetAuthenticatedUsername(user); | 229 SetAuthenticatedUsername(user); |
226 // TokenService can be null for unit tests. | 230 // TokenService can be null for unit tests. |
227 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); | 231 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); |
228 if (token_service) { | 232 if (token_service) { |
229 token_service->Initialize(GaiaConstants::kChromeSource, profile_); | 233 token_service->Initialize(GaiaConstants::kChromeSource, profile_); |
230 // ChromeOS will kick off TokenService::LoadTokensFromDB from | 234 // ChromeOS will kick off TokenService::LoadTokensFromDB from |
231 // OAuthLoginManager once the rest of the Profile is fully initialized. | 235 // OAuthLoginManager once the rest of the Profile is fully initialized. |
232 // Starting it from here would cause OAuthLoginManager mismatch the origin | 236 // Starting it from here would cause OAuthLoginManager mismatch the origin |
233 // of OAuth2 tokens. | 237 // of OAuth2 tokens. |
234 #if !defined(OS_CHROMEOS) | 238 #if !defined(OS_CHROMEOS) |
235 if (!authenticated_username_.empty()) { | 239 if (!authenticated_username_.empty()) { |
236 token_service->LoadTokensFromDB(); | 240 token_service->LoadTokensFromDB(); |
237 } | 241 } |
238 #endif | 242 #endif |
239 } | 243 } |
240 if (!user.empty() && !IsAllowedUsername(user)) { | 244 if ((!user.empty() && !IsAllowedUsername(user)) || !IsSigninAllowed()) { |
241 // User is signed in, but the username is invalid - the administrator must | 245 // User is signed in, but the username is invalid - the administrator must |
242 // have changed the policy since the last signin, so sign out the user. | 246 // have changed the policy since the last signin, so sign out the user. |
243 SignOut(); | 247 SignOut(); |
244 } | 248 } |
245 } | 249 } |
246 | 250 |
247 bool SigninManager::IsInitialized() const { | 251 bool SigninManager::IsInitialized() const { |
248 return profile_ != NULL; | 252 return profile_ != NULL; |
249 } | 253 } |
250 | 254 |
251 bool SigninManager::IsAllowedUsername(const std::string& username) const { | 255 bool SigninManager::IsAllowedUsername(const std::string& username) const { |
252 PrefService* local_state = g_browser_process->local_state(); | 256 PrefService* local_state = g_browser_process->local_state(); |
253 if (!local_state) | 257 if (!local_state) |
254 return true; // In a unit test with no local state - all names are allowed. | 258 return true; // In a unit test with no local state - all names are allowed. |
255 | 259 |
256 std::string pattern = local_state->GetString( | 260 std::string pattern = local_state->GetString( |
257 prefs::kGoogleServicesUsernamePattern); | 261 prefs::kGoogleServicesUsernamePattern); |
258 return IsAllowedUsername(username, pattern); | 262 return IsAllowedUsername(username, pattern); |
259 } | 263 } |
260 | 264 |
| 265 bool SigninManager::IsSigninAllowed() const { |
| 266 return signin_allowed_.GetValue(); |
| 267 } |
| 268 |
| 269 // static |
| 270 bool SigninManager::IsSigninAllowedOnIOThread(ProfileIOData* io_data) { |
| 271 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 272 return io_data->signin_allowed()->GetValue(); |
| 273 } |
| 274 |
261 void SigninManager::CleanupNotificationRegistration() { | 275 void SigninManager::CleanupNotificationRegistration() { |
262 #if !defined(OS_CHROMEOS) | 276 #if !defined(OS_CHROMEOS) |
263 content::Source<TokenService> token_service( | 277 content::Source<TokenService> token_service( |
264 TokenServiceFactory::GetForProfile(profile_)); | 278 TokenServiceFactory::GetForProfile(profile_)); |
265 if (registrar_.IsRegistered(this, | 279 if (registrar_.IsRegistered(this, |
266 chrome::NOTIFICATION_TOKEN_AVAILABLE, | 280 chrome::NOTIFICATION_TOKEN_AVAILABLE, |
267 token_service)) { | 281 token_service)) { |
268 registrar_.Remove(this, | 282 registrar_.Remove(this, |
269 chrome::NOTIFICATION_TOKEN_AVAILABLE, | 283 chrome::NOTIFICATION_TOKEN_AVAILABLE, |
270 token_service); | 284 token_service); |
(...skipping 482 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
753 | 767 |
754 void SigninManager::OnGoogleServicesUsernamePatternChanged() { | 768 void SigninManager::OnGoogleServicesUsernamePatternChanged() { |
755 if (!authenticated_username_.empty() && | 769 if (!authenticated_username_.empty() && |
756 !IsAllowedUsername(authenticated_username_)) { | 770 !IsAllowedUsername(authenticated_username_)) { |
757 // Signed in user is invalid according to the current policy so sign | 771 // Signed in user is invalid according to the current policy so sign |
758 // the user out. | 772 // the user out. |
759 SignOut(); | 773 SignOut(); |
760 } | 774 } |
761 } | 775 } |
762 | 776 |
| 777 void SigninManager::OnSigninAllowedPrefChanged() { |
| 778 if (!IsSigninAllowed()) |
| 779 SignOut(); |
| 780 } |
| 781 |
763 void SigninManager::AddSigninDiagnosticsObserver( | 782 void SigninManager::AddSigninDiagnosticsObserver( |
764 SigninDiagnosticsObserver* observer) { | 783 SigninDiagnosticsObserver* observer) { |
765 signin_diagnostics_observers_.AddObserver(observer); | 784 signin_diagnostics_observers_.AddObserver(observer); |
766 } | 785 } |
767 | 786 |
768 void SigninManager::RemoveSigninDiagnosticsObserver( | 787 void SigninManager::RemoveSigninDiagnosticsObserver( |
769 SigninDiagnosticsObserver* observer) { | 788 SigninDiagnosticsObserver* observer) { |
770 signin_diagnostics_observers_.RemoveObserver(observer); | 789 signin_diagnostics_observers_.RemoveObserver(observer); |
771 } | 790 } |
772 | 791 |
773 void SigninManager::NotifyDiagnosticsObservers( | 792 void SigninManager::NotifyDiagnosticsObservers( |
774 const UntimedSigninStatusField& field, | 793 const UntimedSigninStatusField& field, |
775 const std::string& value) { | 794 const std::string& value) { |
776 FOR_EACH_OBSERVER(SigninDiagnosticsObserver, | 795 FOR_EACH_OBSERVER(SigninDiagnosticsObserver, |
777 signin_diagnostics_observers_, | 796 signin_diagnostics_observers_, |
778 NotifySigninValueChanged(field, value)); | 797 NotifySigninValueChanged(field, value)); |
779 } | 798 } |
780 | 799 |
781 void SigninManager::NotifyDiagnosticsObservers( | 800 void SigninManager::NotifyDiagnosticsObservers( |
782 const TimedSigninStatusField& field, | 801 const TimedSigninStatusField& field, |
783 const std::string& value) { | 802 const std::string& value) { |
784 FOR_EACH_OBSERVER(SigninDiagnosticsObserver, | 803 FOR_EACH_OBSERVER(SigninDiagnosticsObserver, |
785 signin_diagnostics_observers_, | 804 signin_diagnostics_observers_, |
786 NotifySigninValueChanged(field, value)); | 805 NotifySigninValueChanged(field, value)); |
787 } | 806 } |
OLD | NEW |