| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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.h" | 5 #include "chrome/browser/chromeos/login/user_manager.h" |
| 6 | 6 |
| 7 #include "app/resource_bundle.h" | 7 #include "app/resource_bundle.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 return; | 92 return; |
| 93 } | 93 } |
| 94 | 94 |
| 95 BrowserThread::PostTask( | 95 BrowserThread::PostTask( |
| 96 BrowserThread::UI, | 96 BrowserThread::UI, |
| 97 FROM_HERE, | 97 FROM_HERE, |
| 98 NewRunnableFunction(&SavePathToLocalState, | 98 NewRunnableFunction(&SavePathToLocalState, |
| 99 username, image_path.value())); | 99 username, image_path.value())); |
| 100 } | 100 } |
| 101 | 101 |
| 102 // Deletes user's image file. Runs on FILE thread. |
| 103 void DeleteUserImage(const FilePath& image_path) { |
| 104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 105 if (!file_util::Delete(image_path, false)) { |
| 106 LOG(ERROR) << "Failed to remove user image."; |
| 107 return; |
| 108 } |
| 109 } |
| 110 |
| 102 // Checks if given path is one of the default ones. If it is, returns true | 111 // Checks if given path is one of the default ones. If it is, returns true |
| 103 // and its index in kDefaultImageNames through |image_id|. If not, returns | 112 // and its index in kDefaultImageNames through |image_id|. If not, returns |
| 104 // false. | 113 // false. |
| 105 bool IsDefaultImagePath(const std::string& path, size_t* image_id) { | 114 bool IsDefaultImagePath(const std::string& path, size_t* image_id) { |
| 106 DCHECK(image_id); | 115 DCHECK(image_id); |
| 107 for (size_t i = 0; i < arraysize(kDefaultImageNames); ++i) { | 116 for (size_t i = 0; i < arraysize(kDefaultImageNames); ++i) { |
| 108 if (path == kDefaultImageNames[i]) { | 117 if (path == kDefaultImageNames[i]) { |
| 109 *image_id = i; | 118 *image_id = i; |
| 110 return true; | 119 return true; |
| 111 } | 120 } |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 NotifyOnLogin(); | 264 NotifyOnLogin(); |
| 256 } | 265 } |
| 257 | 266 |
| 258 void UserManager::RemoveUser(const std::string& email) { | 267 void UserManager::RemoveUser(const std::string& email) { |
| 259 // Get a copy of the current users. | 268 // Get a copy of the current users. |
| 260 std::vector<User> users = GetUsers(); | 269 std::vector<User> users = GetUsers(); |
| 261 | 270 |
| 262 // Clear the prefs view of the users. | 271 // Clear the prefs view of the users. |
| 263 PrefService* prefs = g_browser_process->local_state(); | 272 PrefService* prefs = g_browser_process->local_state(); |
| 264 ListValue* prefs_users = prefs->GetMutableList(kLoggedInUsers); | 273 ListValue* prefs_users = prefs->GetMutableList(kLoggedInUsers); |
| 274 DCHECK(prefs_users); |
| 265 prefs_users->Clear(); | 275 prefs_users->Clear(); |
| 266 | 276 |
| 267 for (std::vector<User>::iterator it = users.begin(); | 277 for (std::vector<User>::iterator it = users.begin(); |
| 268 it != users.end(); | 278 it != users.end(); |
| 269 ++it) { | 279 ++it) { |
| 270 std::string user_email = it->email(); | 280 std::string user_email = it->email(); |
| 271 // Skip user that we would like to delete. | 281 // Skip user that we would like to delete. |
| 272 if (email != user_email) | 282 if (email != user_email) |
| 273 prefs_users->Append(Value::CreateStringValue(user_email)); | 283 prefs_users->Append(Value::CreateStringValue(user_email)); |
| 274 } | 284 } |
| 285 |
| 286 DictionaryValue* prefs_images = prefs->GetMutableDictionary(kUserImages); |
| 287 DCHECK(prefs_images); |
| 288 std::string image_path_string; |
| 289 prefs_images->GetStringWithoutPathExpansion(email, &image_path_string); |
| 290 prefs_images->RemoveWithoutPathExpansion(email, NULL); |
| 291 |
| 275 prefs->SavePersistentPrefs(); | 292 prefs->SavePersistentPrefs(); |
| 293 |
| 294 size_t default_image_id; |
| 295 if (!IsDefaultImagePath(image_path_string, &default_image_id)) { |
| 296 FilePath image_path(image_path_string); |
| 297 BrowserThread::PostTask( |
| 298 BrowserThread::FILE, |
| 299 FROM_HERE, |
| 300 NewRunnableFunction(&DeleteUserImage, |
| 301 image_path)); |
| 302 } |
| 276 } | 303 } |
| 277 | 304 |
| 278 bool UserManager::IsKnownUser(const std::string& email) { | 305 bool UserManager::IsKnownUser(const std::string& email) { |
| 279 std::vector<User> users = GetUsers(); | 306 std::vector<User> users = GetUsers(); |
| 280 for (std::vector<User>::iterator it = users.begin(); | 307 for (std::vector<User>::iterator it = users.begin(); |
| 281 it < users.end(); | 308 it < users.end(); |
| 282 ++it) { | 309 ++it) { |
| 283 if (it->email() == email) | 310 if (it->email() == email) |
| 284 return true; | 311 return true; |
| 285 } | 312 } |
| 286 | 313 |
| 287 return false; | 314 return false; |
| 288 } | 315 } |
| 289 | 316 |
| 290 void UserManager::SetLoggedInUserImage(const SkBitmap& image) { | 317 void UserManager::SetLoggedInUserImage(const SkBitmap& image) { |
| 291 if (logged_in_user_.email().empty()) | 318 if (logged_in_user_.email().empty()) |
| 292 return; | 319 return; |
| 293 logged_in_user_.set_image(image); | 320 logged_in_user_.set_image(image); |
| 294 OnImageLoaded(logged_in_user_.email(), image); | 321 OnImageLoaded(logged_in_user_.email(), image); |
| 295 } | 322 } |
| 296 | 323 |
| 297 void UserManager::SaveUserImage(const std::string& username, | 324 void UserManager::SaveUserImage(const std::string& username, |
| 298 const SkBitmap& image) { | 325 const SkBitmap& image) { |
| 299 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 326 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 300 std::string filename = username + ".png"; | 327 FilePath image_path = GetImagePathForUser(username); |
| 301 FilePath user_data_dir; | |
| 302 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); | |
| 303 FilePath image_path = user_data_dir.AppendASCII(filename); | |
| 304 DVLOG(1) << "Saving user image to " << image_path.value(); | 328 DVLOG(1) << "Saving user image to " << image_path.value(); |
| 305 | 329 |
| 306 BrowserThread::PostTask( | 330 BrowserThread::PostTask( |
| 307 BrowserThread::FILE, | 331 BrowserThread::FILE, |
| 308 FROM_HERE, | 332 FROM_HERE, |
| 309 NewRunnableFunction(&SaveImageToFile, | 333 NewRunnableFunction(&SaveImageToFile, |
| 310 image, image_path, username)); | 334 image, image_path, username)); |
| 311 } | 335 } |
| 312 | 336 |
| 313 void UserManager::SetDefaultUserImage(const std::string& username) { | 337 void UserManager::SetDefaultUserImage(const std::string& username) { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 371 : ALLOW_THIS_IN_INITIALIZER_LIST(image_loader_(new UserImageLoader(this))), | 395 : ALLOW_THIS_IN_INITIALIZER_LIST(image_loader_(new UserImageLoader(this))), |
| 372 current_user_is_owner_(false) { | 396 current_user_is_owner_(false) { |
| 373 registrar_.Add(this, NotificationType::OWNER_KEY_FETCH_ATTEMPT_SUCCEEDED, | 397 registrar_.Add(this, NotificationType::OWNER_KEY_FETCH_ATTEMPT_SUCCEEDED, |
| 374 NotificationService::AllSources()); | 398 NotificationService::AllSources()); |
| 375 } | 399 } |
| 376 | 400 |
| 377 UserManager::~UserManager() { | 401 UserManager::~UserManager() { |
| 378 image_loader_->set_delegate(NULL); | 402 image_loader_->set_delegate(NULL); |
| 379 } | 403 } |
| 380 | 404 |
| 405 FilePath UserManager::GetImagePathForUser(const std::string& username) { |
| 406 std::string filename = username + ".png"; |
| 407 FilePath user_data_dir; |
| 408 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
| 409 return user_data_dir.AppendASCII(filename); |
| 410 } |
| 411 |
| 381 void UserManager::NotifyOnLogin() { | 412 void UserManager::NotifyOnLogin() { |
| 382 NotificationService::current()->Notify( | 413 NotificationService::current()->Notify( |
| 383 NotificationType::LOGIN_USER_CHANGED, | 414 NotificationType::LOGIN_USER_CHANGED, |
| 384 Source<UserManager>(this), | 415 Source<UserManager>(this), |
| 385 Details<const User>(&logged_in_user_)); | 416 Details<const User>(&logged_in_user_)); |
| 386 | 417 |
| 387 chromeos::CrosLibrary::Get()->GetInputMethodLibrary()-> | 418 chromeos::CrosLibrary::Get()->GetInputMethodLibrary()-> |
| 388 SetDeferImeStartup(false); | 419 SetDeferImeStartup(false); |
| 389 // Shut down the IME so that it will reload the user's settings. | 420 // Shut down the IME so that it will reload the user's settings. |
| 390 chromeos::CrosLibrary::Get()->GetInputMethodLibrary()-> | 421 chromeos::CrosLibrary::Get()->GetInputMethodLibrary()-> |
| (...skipping 11 matching lines...) Expand all Loading... |
| 402 void UserManager::Observe(NotificationType type, | 433 void UserManager::Observe(NotificationType type, |
| 403 const NotificationSource& source, | 434 const NotificationSource& source, |
| 404 const NotificationDetails& details) { | 435 const NotificationDetails& details) { |
| 405 if (type == NotificationType::OWNER_KEY_FETCH_ATTEMPT_SUCCEEDED) { | 436 if (type == NotificationType::OWNER_KEY_FETCH_ATTEMPT_SUCCEEDED) { |
| 406 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 437 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 407 NewRunnableFunction(&CheckOwnership)); | 438 NewRunnableFunction(&CheckOwnership)); |
| 408 } | 439 } |
| 409 } | 440 } |
| 410 | 441 |
| 411 } // namespace chromeos | 442 } // namespace chromeos |
| OLD | NEW |