Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(93)

Side by Side Diff: chrome/browser/signin/signin_manager.cc

Issue 12088040: Add a SigninAllowed policy. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix indentation issues. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 signin_global_error_.get()); 214 signin_global_error_.get());
214 PrefService* local_state = g_browser_process->local_state(); 215 PrefService* local_state = g_browser_process->local_state();
215 // local_state can be null during unit tests. 216 // local_state can be null during unit tests.
216 if (local_state) { 217 if (local_state) {
217 local_state_pref_registrar_.Init(local_state); 218 local_state_pref_registrar_.Init(local_state);
218 local_state_pref_registrar_.Add( 219 local_state_pref_registrar_.Add(
219 prefs::kGoogleServicesUsernamePattern, 220 prefs::kGoogleServicesUsernamePattern,
220 base::Bind(&SigninManager::OnGoogleServicesUsernamePatternChanged, 221 base::Bind(&SigninManager::OnGoogleServicesUsernamePatternChanged,
221 weak_pointer_factory_.GetWeakPtr())); 222 weak_pointer_factory_.GetWeakPtr()));
222 } 223 }
224 signin_allowed_.Init(prefs::kSigninAllowed, profile_->GetPrefs(),
225 base::Bind(&SigninManager::OnSigninAllowedPrefChanged,
226 base::Unretained(this)));
223 227
224 // If the user is clearing the token service from the command line, then 228 // If the user is clearing the token service from the command line, then
225 // clear their login info also (not valid to be logged in without any 229 // clear their login info also (not valid to be logged in without any
226 // tokens). 230 // tokens).
227 CommandLine* cmd_line = CommandLine::ForCurrentProcess(); 231 CommandLine* cmd_line = CommandLine::ForCurrentProcess();
228 if (cmd_line->HasSwitch(switches::kClearTokenService)) 232 if (cmd_line->HasSwitch(switches::kClearTokenService))
229 profile->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername); 233 profile->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername);
230 234
231 std::string user = profile_->GetPrefs()->GetString( 235 std::string user = profile_->GetPrefs()->GetString(
232 prefs::kGoogleServicesUsername); 236 prefs::kGoogleServicesUsername);
233 if (!user.empty()) 237 if (!user.empty())
234 SetAuthenticatedUsername(user); 238 SetAuthenticatedUsername(user);
235 // TokenService can be null for unit tests. 239 // TokenService can be null for unit tests.
236 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); 240 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_);
237 if (token_service) { 241 if (token_service) {
238 token_service->Initialize(GaiaConstants::kChromeSource, profile_); 242 token_service->Initialize(GaiaConstants::kChromeSource, profile_);
239 // ChromeOS will kick off TokenService::LoadTokensFromDB from 243 // ChromeOS will kick off TokenService::LoadTokensFromDB from
240 // OAuthLoginManager once the rest of the Profile is fully initialized. 244 // OAuthLoginManager once the rest of the Profile is fully initialized.
241 // Starting it from here would cause OAuthLoginManager mismatch the origin 245 // Starting it from here would cause OAuthLoginManager mismatch the origin
242 // of OAuth2 tokens. 246 // of OAuth2 tokens.
243 #if !defined(OS_CHROMEOS) 247 #if !defined(OS_CHROMEOS)
244 if (!authenticated_username_.empty()) { 248 if (!authenticated_username_.empty()) {
245 token_service->LoadTokensFromDB(); 249 token_service->LoadTokensFromDB();
246 } 250 }
247 #endif 251 #endif
248 } 252 }
249 if (!user.empty() && !IsAllowedUsername(user)) { 253 if ((!user.empty() && !IsAllowedUsername(user)) || !IsSigninAllowed()) {
250 // User is signed in, but the username is invalid - the administrator must 254 // User is signed in, but the username is invalid - the administrator must
251 // have changed the policy since the last signin, so sign out the user. 255 // have changed the policy since the last signin, so sign out the user.
252 SignOut(); 256 SignOut();
253 } 257 }
254 } 258 }
255 259
256 bool SigninManager::IsInitialized() const { 260 bool SigninManager::IsInitialized() const {
257 return profile_ != NULL; 261 return profile_ != NULL;
258 } 262 }
259 263
260 bool SigninManager::IsAllowedUsername(const std::string& username) const { 264 bool SigninManager::IsAllowedUsername(const std::string& username) const {
261 PrefService* local_state = g_browser_process->local_state(); 265 PrefService* local_state = g_browser_process->local_state();
262 if (!local_state) 266 if (!local_state)
263 return true; // In a unit test with no local state - all names are allowed. 267 return true; // In a unit test with no local state - all names are allowed.
264 268
265 std::string pattern = local_state->GetString( 269 std::string pattern = local_state->GetString(
266 prefs::kGoogleServicesUsernamePattern); 270 prefs::kGoogleServicesUsernamePattern);
267 return IsAllowedUsername(username, pattern); 271 return IsAllowedUsername(username, pattern);
268 } 272 }
269 273
274 bool SigninManager::IsSigninAllowed() const {
275 return signin_allowed_.GetValue();
276 }
277
278 // static
279 bool SigninManager::IsSigninAllowedOnIOThread(ProfileIOData* io_data) {
280 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
281 return io_data->signin_allowed()->GetValue();
282 }
283
270 void SigninManager::CleanupNotificationRegistration() { 284 void SigninManager::CleanupNotificationRegistration() {
271 #if !defined(OS_CHROMEOS) 285 #if !defined(OS_CHROMEOS)
272 content::Source<TokenService> token_service( 286 content::Source<TokenService> token_service(
273 TokenServiceFactory::GetForProfile(profile_)); 287 TokenServiceFactory::GetForProfile(profile_));
274 if (registrar_.IsRegistered(this, 288 if (registrar_.IsRegistered(this,
275 chrome::NOTIFICATION_TOKEN_AVAILABLE, 289 chrome::NOTIFICATION_TOKEN_AVAILABLE,
276 token_service)) { 290 token_service)) {
277 registrar_.Remove(this, 291 registrar_.Remove(this,
278 chrome::NOTIFICATION_TOKEN_AVAILABLE, 292 chrome::NOTIFICATION_TOKEN_AVAILABLE,
279 token_service); 293 token_service);
(...skipping 611 matching lines...) Expand 10 before | Expand all | Expand 10 after
891 905
892 void SigninManager::OnGoogleServicesUsernamePatternChanged() { 906 void SigninManager::OnGoogleServicesUsernamePatternChanged() {
893 if (!authenticated_username_.empty() && 907 if (!authenticated_username_.empty() &&
894 !IsAllowedUsername(authenticated_username_)) { 908 !IsAllowedUsername(authenticated_username_)) {
895 // Signed in user is invalid according to the current policy so sign 909 // Signed in user is invalid according to the current policy so sign
896 // the user out. 910 // the user out.
897 SignOut(); 911 SignOut();
898 } 912 }
899 } 913 }
900 914
915 void SigninManager::OnSigninAllowedPrefChanged() {
916 if (!IsSigninAllowed())
917 SignOut();
918 }
919
901 void SigninManager::AddSigninDiagnosticsObserver( 920 void SigninManager::AddSigninDiagnosticsObserver(
902 SigninDiagnosticsObserver* observer) { 921 SigninDiagnosticsObserver* observer) {
903 signin_diagnostics_observers_.AddObserver(observer); 922 signin_diagnostics_observers_.AddObserver(observer);
904 } 923 }
905 924
906 void SigninManager::RemoveSigninDiagnosticsObserver( 925 void SigninManager::RemoveSigninDiagnosticsObserver(
907 SigninDiagnosticsObserver* observer) { 926 SigninDiagnosticsObserver* observer) {
908 signin_diagnostics_observers_.RemoveObserver(observer); 927 signin_diagnostics_observers_.RemoveObserver(observer);
909 } 928 }
910 929
911 void SigninManager::NotifyDiagnosticsObservers( 930 void SigninManager::NotifyDiagnosticsObservers(
912 const UntimedSigninStatusField& field, 931 const UntimedSigninStatusField& field,
913 const std::string& value) { 932 const std::string& value) {
914 FOR_EACH_OBSERVER(SigninDiagnosticsObserver, 933 FOR_EACH_OBSERVER(SigninDiagnosticsObserver,
915 signin_diagnostics_observers_, 934 signin_diagnostics_observers_,
916 NotifySigninValueChanged(field, value)); 935 NotifySigninValueChanged(field, value));
917 } 936 }
918 937
919 void SigninManager::NotifyDiagnosticsObservers( 938 void SigninManager::NotifyDiagnosticsObservers(
920 const TimedSigninStatusField& field, 939 const TimedSigninStatusField& field,
921 const std::string& value) { 940 const std::string& value) {
922 FOR_EACH_OBSERVER(SigninDiagnosticsObserver, 941 FOR_EACH_OBSERVER(SigninDiagnosticsObserver,
923 signin_diagnostics_observers_, 942 signin_diagnostics_observers_,
924 NotifySigninValueChanged(field, value)); 943 NotifySigninValueChanged(field, value));
925 } 944 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698