| 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/chromeos/login/user_manager_impl.h" | 5 #include "chrome/browser/chromeos/login/user_manager_impl.h" |
| 6 | 6 |
| 7 #include <cstddef> | |
| 8 #include <set> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "ash/shell.h" | 7 #include "ash/shell.h" |
| 12 #include "base/bind.h" | 8 #include "base/bind.h" |
| 13 #include "base/chromeos/chromeos_version.h" | 9 #include "base/chromeos/chromeos_version.h" |
| 14 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 15 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 16 #include "base/file_path.h" | 12 #include "base/file_path.h" |
| 17 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 18 #include "base/logging.h" | 14 #include "base/logging.h" |
| 19 #include "base/rand_util.h" | 15 #include "base/rand_util.h" |
| 20 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 21 #include "base/values.h" | 17 #include "base/values.h" |
| 22 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
| 23 #include "chrome/browser/chromeos/cros/cert_library.h" | 19 #include "chrome/browser/chromeos/cros/cert_library.h" |
| 24 #include "chrome/browser/chromeos/cros/cros_library.h" | 20 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 25 #include "chrome/browser/chromeos/input_method/input_method_manager.h" | 21 #include "chrome/browser/chromeos/input_method/input_method_manager.h" |
| 26 #include "chrome/browser/chromeos/login/login_display.h" | 22 #include "chrome/browser/chromeos/login/login_display.h" |
| 27 #include "chrome/browser/chromeos/login/remove_user_delegate.h" | 23 #include "chrome/browser/chromeos/login/remove_user_delegate.h" |
| 28 #include "chrome/browser/chromeos/login/user_image_manager_impl.h" | 24 #include "chrome/browser/chromeos/login/user_image_manager_impl.h" |
| 29 #include "chrome/browser/chromeos/login/wizard_controller.h" | 25 #include "chrome/browser/chromeos/login/wizard_controller.h" |
| 26 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 30 #include "chrome/browser/policy/browser_policy_connector.h" | 27 #include "chrome/browser/policy/browser_policy_connector.h" |
| 31 #include "chrome/browser/prefs/pref_service.h" | 28 #include "chrome/browser/prefs/pref_service.h" |
| 32 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 29 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 33 #include "chrome/browser/profiles/profile_manager.h" | 30 #include "chrome/browser/profiles/profile_manager.h" |
| 34 #include "chrome/browser/sync/profile_sync_service.h" | 31 #include "chrome/browser/sync/profile_sync_service.h" |
| 35 #include "chrome/browser/sync/profile_sync_service_factory.h" | 32 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 36 #include "chrome/common/chrome_notification_types.h" | 33 #include "chrome/common/chrome_notification_types.h" |
| 37 #include "chrome/common/chrome_switches.h" | 34 #include "chrome/common/chrome_switches.h" |
| 38 #include "chrome/common/pref_names.h" | 35 #include "chrome/common/pref_names.h" |
| 39 #include "chromeos/cryptohome/async_method_caller.h" | 36 #include "chromeos/cryptohome/async_method_caller.h" |
| 40 #include "content/public/browser/browser_thread.h" | 37 #include "content/public/browser/browser_thread.h" |
| 41 #include "content/public/browser/notification_service.h" | 38 #include "content/public/browser/notification_service.h" |
| 42 #include "google_apis/gaia/google_service_auth_error.h" | 39 #include "google_apis/gaia/google_service_auth_error.h" |
| 43 | 40 |
| 44 using content::BrowserThread; | 41 using content::BrowserThread; |
| 45 | 42 |
| 46 namespace chromeos { | 43 namespace chromeos { |
| 47 | 44 |
| 48 namespace { | 45 namespace { |
| 49 | 46 |
| 50 // A vector pref of the the regular users known on this device, arranged in LRU | 47 // A vector pref of the users who have logged into the device. |
| 51 // order. | 48 const char kLoggedInUsers[] = "LoggedInUsers"; |
| 52 const char kRegularUsers[] = "LoggedInUsers"; | |
| 53 | |
| 54 // A vector pref of the public accounts defined on this device. | |
| 55 const char kPublicAccounts[] = "PublicAccounts"; | |
| 56 | |
| 57 // A string pref that gets set when a public account is removed but a user is | |
| 58 // currently logged into that account, requiring the account's data to be | |
| 59 // removed after logout. | |
| 60 const char kPublicAccountPendingDataRemoval[] = | |
| 61 "PublicAccountPendingDataRemoval"; | |
| 62 | 49 |
| 63 // A dictionary that maps usernames to the displayed name. | 50 // A dictionary that maps usernames to the displayed name. |
| 64 const char kUserDisplayName[] = "UserDisplayName"; | 51 const char kUserDisplayName[] = "UserDisplayName"; |
| 65 | 52 |
| 66 // A dictionary that maps usernames to the displayed (non-canonical) emails. | 53 // A dictionary that maps usernames to the displayed (non-canonical) emails. |
| 67 const char kUserDisplayEmail[] = "UserDisplayEmail"; | 54 const char kUserDisplayEmail[] = "UserDisplayEmail"; |
| 68 | 55 |
| 69 // A dictionary that maps usernames to OAuth token presence flag. | 56 // A dictionary that maps usernames to OAuth token presence flag. |
| 70 const char kUserOAuthTokenStatus[] = "OAuthTokenStatus"; | 57 const char kUserOAuthTokenStatus[] = "OAuthTokenStatus"; |
| 71 | 58 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 delegate->OnBeforeUserRemoved(user_email); | 90 delegate->OnBeforeUserRemoved(user_email); |
| 104 | 91 |
| 105 chromeos::UserManager::Get()->RemoveUserFromList(user_email); | 92 chromeos::UserManager::Get()->RemoveUserFromList(user_email); |
| 106 cryptohome::AsyncMethodCaller::GetInstance()->AsyncRemove( | 93 cryptohome::AsyncMethodCaller::GetInstance()->AsyncRemove( |
| 107 user_email, base::Bind(&OnRemoveUserComplete, user_email)); | 94 user_email, base::Bind(&OnRemoveUserComplete, user_email)); |
| 108 | 95 |
| 109 if (delegate) | 96 if (delegate) |
| 110 delegate->OnUserRemoved(user_email); | 97 delegate->OnUserRemoved(user_email); |
| 111 } | 98 } |
| 112 | 99 |
| 113 // Helper function that copies users from |users_list| to |users_vector| and | |
| 114 // |users_set|. Duplicates and users already present in |existing_users| are | |
| 115 // skipped. The |logged_in_user| is also skipped and the return value | |
| 116 // indicates whether that user was found in |users_list|. | |
| 117 bool ParseUserList(const ListValue& users_list, | |
| 118 const std::set<std::string>& existing_users, | |
| 119 const std::string& logged_in_user, | |
| 120 std::vector<std::string>* users_vector, | |
| 121 std::set<std::string>* users_set) { | |
| 122 users_vector->clear(); | |
| 123 users_set->clear(); | |
| 124 bool logged_in_user_on_list = false; | |
| 125 for (size_t i = 0; i < users_list.GetSize(); ++i) { | |
| 126 std::string email; | |
| 127 if (!users_list.GetString(i, &email) || email.empty()) { | |
| 128 LOG(ERROR) << "Corrupt entry in user list at index " << i << "."; | |
| 129 continue; | |
| 130 } | |
| 131 if (existing_users.find(email) != existing_users.end() || | |
| 132 !users_set->insert(email).second) { | |
| 133 LOG(ERROR) << "Duplicate user: " << email; | |
| 134 continue; | |
| 135 } | |
| 136 if (email == logged_in_user) { | |
| 137 logged_in_user_on_list = true; | |
| 138 continue; | |
| 139 } | |
| 140 users_vector->push_back(email); | |
| 141 } | |
| 142 users_set->erase(logged_in_user); | |
| 143 return logged_in_user_on_list; | |
| 144 } | |
| 145 | |
| 146 } // namespace | 100 } // namespace |
| 147 | 101 |
| 148 // static | 102 // static |
| 149 void UserManager::RegisterPrefs(PrefService* local_state) { | 103 void UserManager::RegisterPrefs(PrefService* local_state) { |
| 150 local_state->RegisterListPref(kRegularUsers, PrefService::UNSYNCABLE_PREF); | 104 local_state->RegisterListPref(kLoggedInUsers, PrefService::UNSYNCABLE_PREF); |
| 151 local_state->RegisterListPref(kPublicAccounts, PrefService::UNSYNCABLE_PREF); | |
| 152 local_state->RegisterStringPref(kPublicAccountPendingDataRemoval, "", | |
| 153 PrefService::UNSYNCABLE_PREF); | |
| 154 local_state->RegisterDictionaryPref(kUserOAuthTokenStatus, | 105 local_state->RegisterDictionaryPref(kUserOAuthTokenStatus, |
| 155 PrefService::UNSYNCABLE_PREF); | 106 PrefService::UNSYNCABLE_PREF); |
| 156 local_state->RegisterDictionaryPref(kUserDisplayName, | 107 local_state->RegisterDictionaryPref(kUserDisplayName, |
| 157 PrefService::UNSYNCABLE_PREF); | 108 PrefService::UNSYNCABLE_PREF); |
| 158 local_state->RegisterDictionaryPref(kUserDisplayEmail, | 109 local_state->RegisterDictionaryPref(kUserDisplayEmail, |
| 159 PrefService::UNSYNCABLE_PREF); | 110 PrefService::UNSYNCABLE_PREF); |
| 160 } | 111 } |
| 161 | 112 |
| 162 UserManagerImpl::UserManagerImpl() | 113 UserManagerImpl::UserManagerImpl() |
| 163 : cros_settings_(CrosSettings::Get()), | 114 : logged_in_user_(NULL), |
| 164 users_loaded_(false), | |
| 165 logged_in_user_(NULL), | |
| 166 session_started_(false), | 115 session_started_(false), |
| 167 is_current_user_owner_(false), | 116 is_current_user_owner_(false), |
| 168 is_current_user_new_(false), | 117 is_current_user_new_(false), |
| 169 is_current_user_ephemeral_(false), | 118 is_current_user_ephemeral_(false), |
| 170 ephemeral_users_enabled_(false), | 119 ephemeral_users_enabled_(false), |
| 171 observed_sync_service_(NULL), | 120 observed_sync_service_(NULL), |
| 172 user_image_manager_(new UserImageManagerImpl) { | 121 user_image_manager_(new UserImageManagerImpl) { |
| 173 // UserManager instance should be used only on UI thread. | 122 // UserManager instance should be used only on UI thread. |
| 174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 175 registrar_.Add(this, chrome::NOTIFICATION_OWNERSHIP_STATUS_CHANGED, | 124 registrar_.Add(this, chrome::NOTIFICATION_OWNERSHIP_STATUS_CHANGED, |
| 176 content::NotificationService::AllSources()); | 125 content::NotificationService::AllSources()); |
| 177 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_ADDED, | 126 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_ADDED, |
| 178 content::NotificationService::AllSources()); | 127 content::NotificationService::AllSources()); |
| 179 RetrieveTrustedDevicePolicies(); | 128 RetrieveTrustedDevicePolicies(); |
| 180 } | 129 } |
| 181 | 130 |
| 182 UserManagerImpl::~UserManagerImpl() { | 131 UserManagerImpl::~UserManagerImpl() { |
| 183 // Can't use STLDeleteElements because of the private destructor of User. | 132 // Can't use STLDeleteElements because of the private destructor of User. |
| 184 for (size_t i = 0; i < users_.size(); ++i) | 133 for (size_t i = 0; i < users_.size(); ++i) |
| 185 delete users_[i]; | 134 delete users_[i]; |
| 186 users_.clear(); | 135 users_.clear(); |
| 187 if (is_current_user_ephemeral_) | 136 if (is_current_user_ephemeral_) |
| 188 delete logged_in_user_; | 137 delete logged_in_user_; |
| 189 } | 138 } |
| 190 | 139 |
| 191 void UserManagerImpl::Shutdown() { | |
| 192 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 193 cros_settings_->RemoveSettingsObserver(kAccountsPrefDeviceLocalAccounts, | |
| 194 this); | |
| 195 } | |
| 196 | |
| 197 UserImageManager* UserManagerImpl::GetUserImageManager() { | 140 UserImageManager* UserManagerImpl::GetUserImageManager() { |
| 198 return user_image_manager_.get(); | 141 return user_image_manager_.get(); |
| 199 } | 142 } |
| 200 | 143 |
| 201 const UserList& UserManagerImpl::GetUsers() const { | 144 const UserList& UserManagerImpl::GetUsers() const { |
| 202 const_cast<UserManagerImpl*>(this)->EnsureUsersLoaded(); | 145 const_cast<UserManagerImpl*>(this)->EnsureUsersLoaded(); |
| 203 return users_; | 146 return users_; |
| 204 } | 147 } |
| 205 | 148 |
| 206 void UserManagerImpl::UserLoggedIn(const std::string& email, | 149 void UserManagerImpl::UserLoggedIn(const std::string& email, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 218 return; | 161 return; |
| 219 } | 162 } |
| 220 | 163 |
| 221 if (IsEphemeralUser(email)) { | 164 if (IsEphemeralUser(email)) { |
| 222 EphemeralUserLoggedIn(email); | 165 EphemeralUserLoggedIn(email); |
| 223 return; | 166 return; |
| 224 } | 167 } |
| 225 | 168 |
| 226 EnsureUsersLoaded(); | 169 EnsureUsersLoaded(); |
| 227 | 170 |
| 228 // Remove the user from the user list. | 171 // Clear the prefs view of the users. |
| 229 logged_in_user_ = RemoveRegularUserFromList(email); | 172 PrefService* prefs = g_browser_process->local_state(); |
| 173 ListPrefUpdate prefs_users_update(prefs, kLoggedInUsers); |
| 174 prefs_users_update->Clear(); |
| 230 | 175 |
| 231 // Add the user to the front of the persistent user list. | 176 // Make sure this user is first. |
| 232 ListPrefUpdate prefs_users_update(g_browser_process->local_state(), | 177 prefs_users_update->Append(new base::StringValue(email)); |
| 233 kRegularUsers); | 178 UserList::iterator logged_in_user = users_.end(); |
| 234 prefs_users_update->Insert(0, new base::StringValue(email)); | 179 for (UserList::iterator it = users_.begin(); it != users_.end(); ++it) { |
| 180 std::string user_email = (*it)->email(); |
| 181 // Skip the most recent user. |
| 182 if (email != user_email) |
| 183 prefs_users_update->Append(new base::StringValue(user_email)); |
| 184 else |
| 185 logged_in_user = it; |
| 186 } |
| 235 | 187 |
| 236 // If the user was not found on the user list, create a new user. | 188 if (logged_in_user == users_.end()) { |
| 237 if (!logged_in_user_) { | |
| 238 is_current_user_new_ = true; | 189 is_current_user_new_ = true; |
| 239 logged_in_user_ = User::CreateRegularUser(email); | 190 logged_in_user_ = User::CreateRegularUser(email); |
| 240 logged_in_user_->set_oauth_token_status(LoadUserOAuthStatus(email)); | 191 logged_in_user_->set_oauth_token_status(LoadUserOAuthStatus(email)); |
| 192 } else { |
| 193 logged_in_user_ = *logged_in_user; |
| 194 users_.erase(logged_in_user); |
| 195 } |
| 196 // This user must be in the front of the user list. |
| 197 users_.insert(users_.begin(), logged_in_user_); |
| 198 |
| 199 if (is_current_user_new_) { |
| 241 SaveUserDisplayName(logged_in_user_->email(), | 200 SaveUserDisplayName(logged_in_user_->email(), |
| 242 UTF8ToUTF16(logged_in_user_->GetAccountName(true))); | 201 UTF8ToUTF16(logged_in_user_->GetAccountName(true))); |
| 243 WallpaperManager::Get()->SetInitialUserWallpaper(email, true); | 202 WallpaperManager::Get()->SetInitialUserWallpaper(email, true); |
| 244 } | 203 } |
| 245 | 204 |
| 246 user_image_manager_->UserLoggedIn(email, is_current_user_new_); | 205 user_image_manager_->UserLoggedIn(email, is_current_user_new_); |
| 247 | 206 |
| 248 if (!browser_restart) { | 207 if (!browser_restart) { |
| 249 // For GAIA login flow, logged in user wallpaper may not be loaded. | 208 // For GAIA login flow, logged in user wallpaper may not be loaded. |
| 250 WallpaperManager::Get()->EnsureLoggedInUserWallpaperLoaded(); | 209 WallpaperManager::Get()->EnsureLoggedInUserWallpaperLoaded(); |
| 251 } | 210 } |
| 252 | 211 |
| 253 // Make sure that new data is persisted to Local State. | 212 // Make sure we persist new user data to Local State. |
| 254 g_browser_process->local_state()->CommitPendingWrite(); | 213 prefs->CommitPendingWrite(); |
| 255 | 214 |
| 256 NotifyOnLogin(); | 215 NotifyOnLogin(); |
| 257 } | 216 } |
| 258 | 217 |
| 259 void UserManagerImpl::RetailModeUserLoggedIn() { | 218 void UserManagerImpl::RetailModeUserLoggedIn() { |
| 260 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 219 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 261 is_current_user_new_ = true; | 220 is_current_user_new_ = true; |
| 262 is_current_user_ephemeral_ = true; | 221 is_current_user_ephemeral_ = true; |
| 263 logged_in_user_ = User::CreateRetailModeUser(); | 222 logged_in_user_ = User::CreateRetailModeUser(); |
| 264 user_image_manager_->UserLoggedIn(kRetailModeUserEMail, | 223 user_image_manager_->UserLoggedIn(kRetailModeUserEMail, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 287 } | 246 } |
| 288 | 247 |
| 289 void UserManagerImpl::SessionStarted() { | 248 void UserManagerImpl::SessionStarted() { |
| 290 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 249 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 291 session_started_ = true; | 250 session_started_ = true; |
| 292 content::NotificationService::current()->Notify( | 251 content::NotificationService::current()->Notify( |
| 293 chrome::NOTIFICATION_SESSION_STARTED, | 252 chrome::NOTIFICATION_SESSION_STARTED, |
| 294 content::NotificationService::AllSources(), | 253 content::NotificationService::AllSources(), |
| 295 content::NotificationService::NoDetails()); | 254 content::NotificationService::NoDetails()); |
| 296 if (is_current_user_new_) { | 255 if (is_current_user_new_) { |
| 297 // Make sure that the new user's data is persisted to Local State. | 256 // Make sure we persist new user data to Local State. |
| 298 g_browser_process->local_state()->CommitPendingWrite(); | 257 g_browser_process->local_state()->CommitPendingWrite(); |
| 299 } | 258 } |
| 300 } | 259 } |
| 301 | 260 |
| 302 void UserManagerImpl::RemoveUser(const std::string& email, | 261 void UserManagerImpl::RemoveUser(const std::string& email, |
| 303 RemoveUserDelegate* delegate) { | 262 RemoveUserDelegate* delegate) { |
| 304 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 263 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 305 | 264 |
| 306 const User* user = FindUser(email); | 265 if (!IsKnownUser(email)) |
| 307 if (!user || user->GetType() != User::USER_TYPE_REGULAR) | |
| 308 return; | 266 return; |
| 309 | 267 |
| 310 // Sanity check: we must not remove single user. This check may seem | 268 // Sanity check: we must not remove single user. This check may seem |
| 311 // redundant at a first sight because this single user must be an owner and | 269 // redundant at a first sight because this single user must be an owner and |
| 312 // we perform special check later in order not to remove an owner. However | 270 // we perform special check later in order not to remove an owner. However |
| 313 // due to non-instant nature of ownership assignment this later check may | 271 // due to non-instant nature of ownership assignment this later check may |
| 314 // sometimes fail. See http://crosbug.com/12723 | 272 // sometimes fail. See http://crosbug.com/12723 |
| 315 if (users_.size() < 2) | 273 if (users_.size() < 2) |
| 316 return; | 274 return; |
| 317 | 275 |
| 318 // Sanity check: do not allow the logged-in user to remove himself. | 276 // Sanity check: do not allow the logged-in user to remove himself. |
| 319 if (logged_in_user_ && logged_in_user_->email() == email) | 277 if (logged_in_user_ && logged_in_user_->email() == email) |
| 320 return; | 278 return; |
| 321 | 279 |
| 322 RemoveUserInternal(email, delegate); | 280 RemoveUserInternal(email, delegate); |
| 323 } | 281 } |
| 324 | 282 |
| 325 void UserManagerImpl::RemoveUserFromList(const std::string& email) { | 283 void UserManagerImpl::RemoveUserFromList(const std::string& email) { |
| 326 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 284 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 327 EnsureUsersLoaded(); | 285 EnsureUsersLoaded(); |
| 328 RemoveNonCryptohomeData(email); | 286 RemoveUserFromListInternal(email); |
| 329 delete RemoveRegularUserFromList(email); | |
| 330 // Make sure that new data is persisted to Local State. | |
| 331 g_browser_process->local_state()->CommitPendingWrite(); | |
| 332 } | 287 } |
| 333 | 288 |
| 334 bool UserManagerImpl::IsKnownUser(const std::string& email) const { | 289 bool UserManagerImpl::IsKnownUser(const std::string& email) const { |
| 335 return FindUser(email) != NULL; | 290 return FindUser(email) != NULL; |
| 336 } | 291 } |
| 337 | 292 |
| 338 const User* UserManagerImpl::FindUser(const std::string& email) const { | 293 const User* UserManagerImpl::FindUser(const std::string& email) const { |
| 339 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 294 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 340 if (logged_in_user_ && logged_in_user_->email() == email) | 295 if (logged_in_user_ && logged_in_user_->email() == email) |
| 341 return logged_in_user_; | 296 return logged_in_user_; |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 if (!profile->IsOffTheRecord() && | 422 if (!profile->IsOffTheRecord() && |
| 468 profile == ProfileManager::GetDefaultProfile()) { | 423 profile == ProfileManager::GetDefaultProfile()) { |
| 469 DCHECK(NULL == observed_sync_service_); | 424 DCHECK(NULL == observed_sync_service_); |
| 470 observed_sync_service_ = | 425 observed_sync_service_ = |
| 471 ProfileSyncServiceFactory::GetForProfile(profile); | 426 ProfileSyncServiceFactory::GetForProfile(profile); |
| 472 if (observed_sync_service_) | 427 if (observed_sync_service_) |
| 473 observed_sync_service_->AddObserver(this); | 428 observed_sync_service_->AddObserver(this); |
| 474 } | 429 } |
| 475 } | 430 } |
| 476 break; | 431 break; |
| 477 case chrome::NOTIFICATION_SYSTEM_SETTING_CHANGED: | |
| 478 DCHECK_EQ(*content::Details<const std::string>(details).ptr(), | |
| 479 kAccountsPrefDeviceLocalAccounts); | |
| 480 RetrieveTrustedDevicePolicies(); | |
| 481 break; | |
| 482 default: | 432 default: |
| 483 NOTREACHED(); | 433 NOTREACHED(); |
| 484 } | 434 } |
| 485 } | 435 } |
| 486 | 436 |
| 487 void UserManagerImpl::OnStateChanged() { | 437 void UserManagerImpl::OnStateChanged() { |
| 488 DCHECK(IsLoggedInAsRegularUser()); | 438 DCHECK(IsUserLoggedIn() && !IsLoggedInAsGuest()); |
| 489 GoogleServiceAuthError::State state = | 439 GoogleServiceAuthError::State state = |
| 490 observed_sync_service_->GetAuthError().state(); | 440 observed_sync_service_->GetAuthError().state(); |
| 491 if (state != GoogleServiceAuthError::NONE && | 441 if (state != GoogleServiceAuthError::NONE && |
| 492 state != GoogleServiceAuthError::CONNECTION_FAILED && | 442 state != GoogleServiceAuthError::CONNECTION_FAILED && |
| 493 state != GoogleServiceAuthError::SERVICE_UNAVAILABLE && | 443 state != GoogleServiceAuthError::SERVICE_UNAVAILABLE && |
| 494 state != GoogleServiceAuthError::REQUEST_CANCELED) { | 444 state != GoogleServiceAuthError::REQUEST_CANCELED) { |
| 495 // Invalidate OAuth token to force Gaia sign-in flow. This is needed | 445 // Invalidate OAuth token to force Gaia sign-in flow. This is needed |
| 496 // because sign-out/sign-in solution is suggested to the user. | 446 // because sign-out/sign-in solution is suggested to the user. |
| 497 // TODO(altimofeev): this code isn't needed after crosbug.com/25978 is | 447 // TODO(altimofeev): this code isn't needed after crosbug.com/25978 is |
| 498 // implemented. | 448 // implemented. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 527 bool UserManagerImpl::CanCurrentUserLock() const { | 477 bool UserManagerImpl::CanCurrentUserLock() const { |
| 528 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 478 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 529 return IsUserLoggedIn() && logged_in_user_->can_lock(); | 479 return IsUserLoggedIn() && logged_in_user_->can_lock(); |
| 530 } | 480 } |
| 531 | 481 |
| 532 bool UserManagerImpl::IsUserLoggedIn() const { | 482 bool UserManagerImpl::IsUserLoggedIn() const { |
| 533 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 483 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 534 return logged_in_user_; | 484 return logged_in_user_; |
| 535 } | 485 } |
| 536 | 486 |
| 537 bool UserManagerImpl::IsLoggedInAsRegularUser() const { | |
| 538 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 539 return IsUserLoggedIn() && | |
| 540 logged_in_user_->GetType() == User::USER_TYPE_REGULAR; | |
| 541 } | |
| 542 | |
| 543 bool UserManagerImpl::IsLoggedInAsDemoUser() const { | 487 bool UserManagerImpl::IsLoggedInAsDemoUser() const { |
| 544 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 488 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 545 return IsUserLoggedIn() && | 489 return IsUserLoggedIn() && |
| 546 logged_in_user_->GetType() == User::USER_TYPE_RETAIL_MODE; | 490 logged_in_user_->GetType() == User::USER_TYPE_RETAIL_MODE; |
| 547 } | 491 } |
| 548 | 492 |
| 549 bool UserManagerImpl::IsLoggedInAsPublicAccount() const { | 493 bool UserManagerImpl::IsLoggedInAsPublicAccount() const { |
| 550 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 494 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 551 return IsUserLoggedIn() && | 495 return IsUserLoggedIn() && |
| 552 logged_in_user_->GetType() == User::USER_TYPE_PUBLIC_ACCOUNT; | 496 logged_in_user_->GetType() == User::USER_TYPE_PUBLIC_ACCOUNT; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 602 } | 546 } |
| 603 | 547 |
| 604 void UserManagerImpl::NotifyLocalStateChanged() { | 548 void UserManagerImpl::NotifyLocalStateChanged() { |
| 605 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 549 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 606 FOR_EACH_OBSERVER(UserManager::Observer, observer_list_, | 550 FOR_EACH_OBSERVER(UserManager::Observer, observer_list_, |
| 607 LocalStateChanged(this)); | 551 LocalStateChanged(this)); |
| 608 } | 552 } |
| 609 | 553 |
| 610 void UserManagerImpl::EnsureUsersLoaded() { | 554 void UserManagerImpl::EnsureUsersLoaded() { |
| 611 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 555 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 556 if (!users_.empty()) |
| 557 return; |
| 612 if (!g_browser_process) | 558 if (!g_browser_process) |
| 613 return; | 559 return; |
| 614 | 560 |
| 615 if (users_loaded_) | |
| 616 return; | |
| 617 users_loaded_ = true; | |
| 618 | |
| 619 PrefService* local_state = g_browser_process->local_state(); | 561 PrefService* local_state = g_browser_process->local_state(); |
| 620 const ListValue* prefs_regular_users = local_state->GetList(kRegularUsers); | 562 const ListValue* prefs_users = |
| 621 const ListValue* prefs_public_accounts = | 563 local_state->GetList(kLoggedInUsers); |
| 622 local_state->GetList(kPublicAccounts); | |
| 623 const DictionaryValue* prefs_display_names = | 564 const DictionaryValue* prefs_display_names = |
| 624 local_state->GetDictionary(kUserDisplayName); | 565 local_state->GetDictionary(kUserDisplayName); |
| 625 const DictionaryValue* prefs_display_emails = | 566 const DictionaryValue* prefs_display_emails = |
| 626 local_state->GetDictionary(kUserDisplayEmail); | 567 local_state->GetDictionary(kUserDisplayEmail); |
| 627 | 568 |
| 628 // Load regular users. | 569 if (!prefs_users) |
| 629 std::vector<std::string> regular_users; | 570 return; |
| 630 std::set<std::string> regular_users_set; | |
| 631 ParseUserList(*prefs_regular_users, std::set<std::string>(), "", | |
| 632 ®ular_users, ®ular_users_set); | |
| 633 for (std::vector<std::string>::const_iterator it = regular_users.begin(); | |
| 634 it != regular_users.end(); ++it) { | |
| 635 User* user = User::CreateRegularUser(*it); | |
| 636 user->set_oauth_token_status(LoadUserOAuthStatus(*it)); | |
| 637 users_.push_back(user); | |
| 638 | 571 |
| 639 string16 display_name; | 572 for (ListValue::const_iterator it = prefs_users->begin(); |
| 640 if (prefs_display_names->GetStringWithoutPathExpansion(*it, | 573 it != prefs_users->end(); ++it) { |
| 641 &display_name)) { | 574 std::string email; |
| 642 user->set_display_name(display_name); | 575 if ((*it)->GetAsString(&email)) { |
| 576 User* user = User::CreateRegularUser(email); |
| 577 user->set_oauth_token_status(LoadUserOAuthStatus(email)); |
| 578 users_.push_back(user); |
| 579 |
| 580 string16 display_name; |
| 581 if (prefs_display_names && |
| 582 prefs_display_names->GetStringWithoutPathExpansion( |
| 583 email, &display_name)) { |
| 584 user->set_display_name(display_name); |
| 585 } |
| 586 |
| 587 std::string display_email; |
| 588 if (prefs_display_emails && |
| 589 prefs_display_emails->GetStringWithoutPathExpansion( |
| 590 email, &display_email)) { |
| 591 user->set_display_email(display_email); |
| 592 } |
| 643 } | 593 } |
| 644 | |
| 645 std::string display_email; | |
| 646 if (prefs_display_emails->GetStringWithoutPathExpansion(*it, | |
| 647 &display_email)) { | |
| 648 user->set_display_email(display_email); | |
| 649 } | |
| 650 } | |
| 651 | |
| 652 // Load public accounts. | |
| 653 std::vector<std::string> public_accounts; | |
| 654 std::set<std::string> public_accounts_set; | |
| 655 ParseUserList(*prefs_public_accounts, regular_users_set, "", | |
| 656 &public_accounts, &public_accounts_set); | |
| 657 for (std::vector<std::string>::const_iterator it = public_accounts.begin(); | |
| 658 it != public_accounts.end(); ++it) { | |
| 659 users_.push_back(User::CreatePublicAccountUser(*it)); | |
| 660 } | 594 } |
| 661 | 595 |
| 662 user_image_manager_->LoadUserImages(users_); | 596 user_image_manager_->LoadUserImages(users_); |
| 663 } | 597 } |
| 664 | 598 |
| 665 void UserManagerImpl::RetrieveTrustedDevicePolicies() { | 599 void UserManagerImpl::RetrieveTrustedDevicePolicies() { |
| 666 ephemeral_users_enabled_ = false; | 600 ephemeral_users_enabled_ = false; |
| 667 owner_email_ = ""; | 601 owner_email_ = ""; |
| 668 | 602 |
| 603 CrosSettings* cros_settings = CrosSettings::Get(); |
| 669 // Schedule a callback if device policy has not yet been verified. | 604 // Schedule a callback if device policy has not yet been verified. |
| 670 if (CrosSettingsProvider::TRUSTED != cros_settings_->PrepareTrustedValues( | 605 if (CrosSettingsProvider::TRUSTED != cros_settings->PrepareTrustedValues( |
| 671 base::Bind(&UserManagerImpl::RetrieveTrustedDevicePolicies, | 606 base::Bind(&UserManagerImpl::RetrieveTrustedDevicePolicies, |
| 672 base::Unretained(this)))) { | 607 base::Unretained(this)))) { |
| 673 return; | 608 return; |
| 674 } | 609 } |
| 675 | 610 |
| 676 cros_settings_->GetBoolean(kAccountsPrefEphemeralUsersEnabled, | 611 cros_settings->GetBoolean(kAccountsPrefEphemeralUsersEnabled, |
| 677 &ephemeral_users_enabled_); | 612 &ephemeral_users_enabled_); |
| 678 cros_settings_->GetString(kDeviceOwner, &owner_email_); | 613 cros_settings->GetString(kDeviceOwner, &owner_email_); |
| 679 const base::ListValue* public_accounts; | |
| 680 cros_settings_->GetList(kAccountsPrefDeviceLocalAccounts, &public_accounts); | |
| 681 | |
| 682 EnsureUsersLoaded(); | |
| 683 | |
| 684 bool changed = UpdateAndCleanUpPublicAccounts(*public_accounts); | |
| 685 | 614 |
| 686 // If ephemeral users are enabled and we are on the login screen, take this | 615 // If ephemeral users are enabled and we are on the login screen, take this |
| 687 // opportunity to clean up by removing all regular users except the owner. | 616 // opportunity to clean up by removing all users except the owner. |
| 688 if (ephemeral_users_enabled_ && !IsUserLoggedIn()) { | 617 if (ephemeral_users_enabled_ && !IsUserLoggedIn()) { |
| 689 ListPrefUpdate prefs_users_update(g_browser_process->local_state(), | 618 scoped_ptr<base::ListValue> users( |
| 690 kRegularUsers); | 619 g_browser_process->local_state()->GetList(kLoggedInUsers)->DeepCopy()); |
| 691 prefs_users_update->Clear(); | 620 |
| 692 for (UserList::iterator it = users_.begin(); it != users_.end(); ) { | 621 bool changed = false; |
| 693 const std::string user_email = (*it)->email(); | 622 for (base::ListValue::const_iterator user = users->begin(); |
| 694 if ((*it)->GetType() == User::USER_TYPE_REGULAR && | 623 user != users->end(); ++user) { |
| 695 user_email != owner_email_) { | 624 std::string user_email; |
| 696 RemoveNonCryptohomeData(user_email); | 625 (*user)->GetAsString(&user_email); |
| 697 delete *it; | 626 if (user_email != owner_email_) { |
| 698 it = users_.erase(it); | 627 RemoveUserFromListInternal(user_email); |
| 699 changed = true; | 628 changed = true; |
| 700 } else { | |
| 701 prefs_users_update->Append(new base::StringValue(user_email)); | |
| 702 ++it; | |
| 703 } | 629 } |
| 704 } | 630 } |
| 631 |
| 632 if (changed) { |
| 633 content::NotificationService::current()->Notify( |
| 634 chrome::NOTIFICATION_POLICY_USER_LIST_CHANGED, |
| 635 content::Source<UserManager>(this), |
| 636 content::NotificationService::NoDetails()); |
| 637 } |
| 705 } | 638 } |
| 706 | |
| 707 if (changed) { | |
| 708 content::NotificationService::current()->Notify( | |
| 709 chrome::NOTIFICATION_POLICY_USER_LIST_CHANGED, | |
| 710 content::Source<UserManager>(this), | |
| 711 content::NotificationService::NoDetails()); | |
| 712 } | |
| 713 | |
| 714 cros_settings_->AddSettingsObserver(kAccountsPrefDeviceLocalAccounts, | |
| 715 this); | |
| 716 } | 639 } |
| 717 | 640 |
| 718 bool UserManagerImpl::AreEphemeralUsersEnabled() const { | 641 bool UserManagerImpl::AreEphemeralUsersEnabled() const { |
| 719 return ephemeral_users_enabled_ && | 642 return ephemeral_users_enabled_ && |
| 720 (g_browser_process->browser_policy_connector()->IsEnterpriseManaged() || | 643 (g_browser_process->browser_policy_connector()->IsEnterpriseManaged() || |
| 721 !owner_email_.empty()); | 644 !owner_email_.empty()); |
| 722 } | 645 } |
| 723 | 646 |
| 724 const User* UserManagerImpl::FindUserInList(const std::string& email) const { | 647 const User* UserManagerImpl::FindUserInList(const std::string& email) const { |
| 725 const UserList& users = GetUsers(); | 648 const UserList& users = GetUsers(); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 751 | 674 |
| 752 SetCurrentUserIsOwner(is_owner); | 675 SetCurrentUserIsOwner(is_owner); |
| 753 } | 676 } |
| 754 | 677 |
| 755 void UserManagerImpl::CheckOwnership() { | 678 void UserManagerImpl::CheckOwnership() { |
| 756 DeviceSettingsService::Get()->GetOwnershipStatusAsync( | 679 DeviceSettingsService::Get()->GetOwnershipStatusAsync( |
| 757 base::Bind(&UserManagerImpl::UpdateOwnership, | 680 base::Bind(&UserManagerImpl::UpdateOwnership, |
| 758 base::Unretained(this))); | 681 base::Unretained(this))); |
| 759 } | 682 } |
| 760 | 683 |
| 761 void UserManagerImpl::RemoveNonCryptohomeData(const std::string& email) { | 684 void UserManagerImpl::RemoveUserFromListInternal(const std::string& email) { |
| 685 // Clear the prefs view of the users. |
| 686 PrefService* prefs = g_browser_process->local_state(); |
| 687 ListPrefUpdate prefs_users_update(prefs, kLoggedInUsers); |
| 688 prefs_users_update->Clear(); |
| 689 |
| 690 UserList::iterator user_to_remove = users_.end(); |
| 691 for (UserList::iterator it = users_.begin(); it != users_.end(); ++it) { |
| 692 std::string user_email = (*it)->email(); |
| 693 // Skip user that we would like to delete. |
| 694 if (email != user_email) |
| 695 prefs_users_update->Append(new base::StringValue(user_email)); |
| 696 else |
| 697 user_to_remove = it; |
| 698 } |
| 699 |
| 762 WallpaperManager::Get()->RemoveUserWallpaperInfo(email); | 700 WallpaperManager::Get()->RemoveUserWallpaperInfo(email); |
| 763 user_image_manager_->DeleteUserImage(email); | |
| 764 | 701 |
| 765 PrefService* prefs = g_browser_process->local_state(); | |
| 766 DictionaryPrefUpdate prefs_oauth_update(prefs, kUserOAuthTokenStatus); | 702 DictionaryPrefUpdate prefs_oauth_update(prefs, kUserOAuthTokenStatus); |
| 767 int oauth_status; | 703 int oauth_status; |
| 768 prefs_oauth_update->GetIntegerWithoutPathExpansion(email, &oauth_status); | 704 prefs_oauth_update->GetIntegerWithoutPathExpansion(email, &oauth_status); |
| 769 prefs_oauth_update->RemoveWithoutPathExpansion(email, NULL); | 705 prefs_oauth_update->RemoveWithoutPathExpansion(email, NULL); |
| 770 | 706 |
| 771 DictionaryPrefUpdate prefs_display_name_update(prefs, kUserDisplayName); | 707 DictionaryPrefUpdate prefs_display_name_update(prefs, kUserDisplayName); |
| 772 prefs_display_name_update->RemoveWithoutPathExpansion(email, NULL); | 708 prefs_display_name_update->RemoveWithoutPathExpansion(email, NULL); |
| 773 | 709 |
| 774 DictionaryPrefUpdate prefs_display_email_update(prefs, kUserDisplayEmail); | 710 DictionaryPrefUpdate prefs_display_email_update(prefs, kUserDisplayEmail); |
| 775 prefs_display_email_update->RemoveWithoutPathExpansion(email, NULL); | 711 prefs_display_email_update->RemoveWithoutPathExpansion(email, NULL); |
| 776 } | |
| 777 | 712 |
| 778 User *UserManagerImpl::RemoveRegularUserFromList(const std::string& email) { | 713 if (user_to_remove != users_.end()) { |
| 779 ListPrefUpdate prefs_users_update(g_browser_process->local_state(), | 714 delete *user_to_remove; |
| 780 kRegularUsers); | 715 users_.erase(user_to_remove); |
| 781 prefs_users_update->Clear(); | |
| 782 User* user = NULL; | |
| 783 for (UserList::iterator it = users_.begin(); it != users_.end(); ) { | |
| 784 const std::string user_email = (*it)->email(); | |
| 785 if (user_email == email) { | |
| 786 user = *it; | |
| 787 it = users_.erase(it); | |
| 788 } else { | |
| 789 if ((*it)->GetType() == User::USER_TYPE_REGULAR) | |
| 790 prefs_users_update->Append(new base::StringValue(user_email)); | |
| 791 ++it; | |
| 792 } | |
| 793 } | 716 } |
| 794 return user; | |
| 795 } | |
| 796 | |
| 797 bool UserManagerImpl::UpdateAndCleanUpPublicAccounts( | |
| 798 const base::ListValue& public_accounts) { | |
| 799 PrefService* local_state = g_browser_process->local_state(); | |
| 800 | |
| 801 // Determine the currently logged-in user's email. | |
| 802 std::string logged_in_user_email; | |
| 803 if (IsUserLoggedIn()) | |
| 804 logged_in_user_email = GetLoggedInUser()->email(); | |
| 805 | |
| 806 // If there is a public account whose data is pending removal and the user is | |
| 807 // not currently logged in with that account, take this opportunity to remove | |
| 808 // the data. | |
| 809 std::string public_account_pending_data_removal = | |
| 810 local_state->GetString(kPublicAccountPendingDataRemoval); | |
| 811 if (!public_account_pending_data_removal.empty() && | |
| 812 public_account_pending_data_removal != logged_in_user_email) { | |
| 813 RemoveNonCryptohomeData(public_account_pending_data_removal); | |
| 814 local_state->ClearPref(kPublicAccountPendingDataRemoval); | |
| 815 } | |
| 816 | |
| 817 // Split the current user list public accounts and regular users. | |
| 818 std::vector<std::string> old_public_accounts; | |
| 819 std::set<std::string> regular_users; | |
| 820 for (UserList::const_iterator it = users_.begin(); it != users_.end(); ++it) { | |
| 821 if ((*it)->GetType() == User::USER_TYPE_PUBLIC_ACCOUNT) | |
| 822 old_public_accounts.push_back((*it)->email()); | |
| 823 else | |
| 824 regular_users.insert((*it)->email()); | |
| 825 } | |
| 826 | |
| 827 // Get the new list of public accounts from policy. | |
| 828 std::vector<std::string> new_public_accounts; | |
| 829 std::set<std::string> new_public_accounts_set; | |
| 830 if (!ParseUserList(public_accounts, regular_users, logged_in_user_email, | |
| 831 &new_public_accounts, &new_public_accounts_set) && | |
| 832 IsUserLoggedIn()) { | |
| 833 User* user = GetLoggedInUser(); | |
| 834 // If the user is currently logged into a public account that has been | |
| 835 // removed from the list, mark the account's data as pending removal after | |
| 836 // logout. | |
| 837 if (user->GetType() == User::USER_TYPE_PUBLIC_ACCOUNT) { | |
| 838 local_state->SetString(kPublicAccountPendingDataRemoval, | |
| 839 logged_in_user_email); | |
| 840 } | |
| 841 } | |
| 842 | |
| 843 // Persist the new list of public accounts in a pref. | |
| 844 ListPrefUpdate prefs_public_accounts_update(local_state, kPublicAccounts); | |
| 845 scoped_ptr<base::ListValue> prefs_public_accounts(public_accounts.DeepCopy()); | |
| 846 prefs_public_accounts_update->Swap(prefs_public_accounts.get()); | |
| 847 | |
| 848 // If the list of public accounts has not changed, return. | |
| 849 if (new_public_accounts.size() == old_public_accounts.size()) { | |
| 850 bool changed = false; | |
| 851 for (size_t i = 0; i < new_public_accounts.size(); ++i) { | |
| 852 if (new_public_accounts[i] != old_public_accounts[i]) { | |
| 853 changed = true; | |
| 854 break; | |
| 855 } | |
| 856 } | |
| 857 if (!changed) | |
| 858 return false; | |
| 859 } | |
| 860 | |
| 861 // Remove the old public accounts from the user list. | |
| 862 for (UserList::iterator it = users_.begin(); it != users_.end(); ) { | |
| 863 if ((*it)->GetType() == User::USER_TYPE_PUBLIC_ACCOUNT) { | |
| 864 delete *it; | |
| 865 it = users_.erase(it); | |
| 866 } else { | |
| 867 ++it; | |
| 868 } | |
| 869 } | |
| 870 | |
| 871 // Add the new public accounts to the front of the user list. | |
| 872 for (std::vector<std::string>::const_reverse_iterator | |
| 873 it = new_public_accounts.rbegin(); | |
| 874 it != new_public_accounts.rend(); ++it) { | |
| 875 users_.insert(users_.begin(), User::CreatePublicAccountUser(*it)); | |
| 876 } | |
| 877 | |
| 878 user_image_manager_->LoadUserImages( | |
| 879 UserList(users_.begin(), users_.begin() + new_public_accounts.size())); | |
| 880 | |
| 881 return true; | |
| 882 } | 717 } |
| 883 | 718 |
| 884 } // namespace chromeos | 719 } // namespace chromeos |
| OLD | NEW |