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 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
741 | 755 |
742 void SigninManager::OnGoogleServicesUsernamePatternChanged() { | 756 void SigninManager::OnGoogleServicesUsernamePatternChanged() { |
743 if (!authenticated_username_.empty() && | 757 if (!authenticated_username_.empty() && |
744 !IsAllowedUsername(authenticated_username_)) { | 758 !IsAllowedUsername(authenticated_username_)) { |
745 // Signed in user is invalid according to the current policy so sign | 759 // Signed in user is invalid according to the current policy so sign |
746 // the user out. | 760 // the user out. |
747 SignOut(); | 761 SignOut(); |
748 } | 762 } |
749 } | 763 } |
750 | 764 |
| 765 void SigninManager::OnSigninAllowedPrefChanged() { |
| 766 if (!IsSigninAllowed()) |
| 767 SignOut(); |
| 768 } |
| 769 |
751 void SigninManager::AddSigninDiagnosticsObserver( | 770 void SigninManager::AddSigninDiagnosticsObserver( |
752 SigninDiagnosticsObserver* observer) { | 771 SigninDiagnosticsObserver* observer) { |
753 signin_diagnostics_observers_.AddObserver(observer); | 772 signin_diagnostics_observers_.AddObserver(observer); |
754 } | 773 } |
755 | 774 |
756 void SigninManager::RemoveSigninDiagnosticsObserver( | 775 void SigninManager::RemoveSigninDiagnosticsObserver( |
757 SigninDiagnosticsObserver* observer) { | 776 SigninDiagnosticsObserver* observer) { |
758 signin_diagnostics_observers_.RemoveObserver(observer); | 777 signin_diagnostics_observers_.RemoveObserver(observer); |
759 } | 778 } |
760 | 779 |
761 void SigninManager::NotifyDiagnosticsObservers( | 780 void SigninManager::NotifyDiagnosticsObservers( |
762 const UntimedSigninStatusField& field, | 781 const UntimedSigninStatusField& field, |
763 const std::string& value) { | 782 const std::string& value) { |
764 FOR_EACH_OBSERVER(SigninDiagnosticsObserver, | 783 FOR_EACH_OBSERVER(SigninDiagnosticsObserver, |
765 signin_diagnostics_observers_, | 784 signin_diagnostics_observers_, |
766 NotifySigninValueChanged(field, value)); | 785 NotifySigninValueChanged(field, value)); |
767 } | 786 } |
768 | 787 |
769 void SigninManager::NotifyDiagnosticsObservers( | 788 void SigninManager::NotifyDiagnosticsObservers( |
770 const TimedSigninStatusField& field, | 789 const TimedSigninStatusField& field, |
771 const std::string& value) { | 790 const std::string& value) { |
772 FOR_EACH_OBSERVER(SigninDiagnosticsObserver, | 791 FOR_EACH_OBSERVER(SigninDiagnosticsObserver, |
773 signin_diagnostics_observers_, | 792 signin_diagnostics_observers_, |
774 NotifySigninValueChanged(field, value)); | 793 NotifySigninValueChanged(field, value)); |
775 } | 794 } |
OLD | NEW |