| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/command_line.h" | 7 #include "base/command_line.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" |
| 11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
| 15 #include "base/rand_util.h" |
| 15 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 16 #include "base/stringprintf.h" | 17 #include "base/stringprintf.h" |
| 17 #include "base/time.h" | 18 #include "base/time.h" |
| 18 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
| 19 #include "base/values.h" | 20 #include "base/values.h" |
| 20 #include "crypto/nss_util.h" | 21 #include "crypto/nss_util.h" |
| 21 #include "chrome/browser/browser_process.h" | 22 #include "chrome/browser/browser_process.h" |
| 22 #include "chrome/browser/chromeos/cros/cros_library.h" | 23 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 23 #include "chrome/browser/chromeos/cros/cryptohome_library.h" | 24 #include "chrome/browser/chromeos/cros/cryptohome_library.h" |
| 24 #include "chrome/browser/chromeos/input_method/input_method_manager.h" | 25 #include "chrome/browser/chromeos/input_method/input_method_manager.h" |
| (...skipping 512 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 537 | 538 |
| 538 void UserManager::SaveUserImagePath(const std::string& username, | 539 void UserManager::SaveUserImagePath(const std::string& username, |
| 539 const std::string& image_path) { | 540 const std::string& image_path) { |
| 540 SavePathToLocalState(username, image_path); | 541 SavePathToLocalState(username, image_path); |
| 541 } | 542 } |
| 542 | 543 |
| 543 void UserManager::SetDefaultUserImage(const std::string& username) { | 544 void UserManager::SetDefaultUserImage(const std::string& username) { |
| 544 if (!g_browser_process) | 545 if (!g_browser_process) |
| 545 return; | 546 return; |
| 546 | 547 |
| 547 PrefService* local_state = g_browser_process->local_state(); | 548 // Choose a random default image. |
| 548 DCHECK(local_state); | 549 int image_id = base::RandInt(0, kDefaultImagesCount - 1); |
| 549 const ListValue* prefs_users = local_state->GetList(kLoggedInUsers); | 550 std::string user_image_path = GetDefaultImagePath(image_id); |
| 550 DCHECK(prefs_users); | 551 int resource_id = kDefaultImageResources[image_id]; |
| 551 const DictionaryValue* prefs_images = | |
| 552 local_state->GetDictionary(kUserImages); | |
| 553 DCHECK(prefs_images); | |
| 554 | |
| 555 // We want to distribute default images between users uniformly so that if | |
| 556 // there're more users with red image, we won't add red one for sure. | |
| 557 // Thus we count how many default images of each color are used and choose | |
| 558 // the first color with minimal usage. | |
| 559 std::vector<int> colors_count(kDefaultImagesCount, 0); | |
| 560 for (ListValue::const_iterator it = prefs_users->begin(); | |
| 561 it != prefs_users->end(); | |
| 562 ++it) { | |
| 563 std::string email; | |
| 564 if ((*it)->GetAsString(&email)) { | |
| 565 std::string image_path; | |
| 566 int default_image_id = kDefaultImagesCount; | |
| 567 if (prefs_images->GetStringWithoutPathExpansion(email, &image_path) && | |
| 568 IsDefaultImagePath(image_path, &default_image_id)) { | |
| 569 DCHECK(default_image_id < kDefaultImagesCount); | |
| 570 ++colors_count[default_image_id]; | |
| 571 } | |
| 572 } | |
| 573 } | |
| 574 std::vector<int>::const_iterator min_it = | |
| 575 std::min_element(colors_count.begin(), colors_count.end()); | |
| 576 int selected_id = min_it - colors_count.begin(); | |
| 577 std::string user_image_path = | |
| 578 GetDefaultImagePath(selected_id); | |
| 579 int resource_id = kDefaultImageResources[selected_id]; | |
| 580 SkBitmap user_image = *ResourceBundle::GetSharedInstance().GetBitmapNamed( | 552 SkBitmap user_image = *ResourceBundle::GetSharedInstance().GetBitmapNamed( |
| 581 resource_id); | 553 resource_id); |
| 582 | 554 |
| 583 SavePathToLocalState(username, user_image_path); | 555 SavePathToLocalState(username, user_image_path); |
| 584 SetLoggedInUserImage(user_image); | 556 SetLoggedInUserImage(user_image); |
| 585 } | 557 } |
| 586 | 558 |
| 587 int UserManager::GetUserDefaultImageIndex(const std::string& username) { | 559 int UserManager::GetUserDefaultImageIndex(const std::string& username) { |
| 588 if (!g_browser_process) | 560 if (!g_browser_process) |
| 589 return -1; | 561 return -1; |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 737 } | 709 } |
| 738 | 710 |
| 739 void UserManager::NotifyLocalStateChanged() { | 711 void UserManager::NotifyLocalStateChanged() { |
| 740 FOR_EACH_OBSERVER( | 712 FOR_EACH_OBSERVER( |
| 741 Observer, | 713 Observer, |
| 742 observer_list_, | 714 observer_list_, |
| 743 LocalStateChanged(this)); | 715 LocalStateChanged(this)); |
| 744 } | 716 } |
| 745 | 717 |
| 746 } // namespace chromeos | 718 } // namespace chromeos |
| OLD | NEW |