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" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
14 #include "base/time.h" | 14 #include "base/time.h" |
15 #include "base/utf_string_conversions.h" | |
16 #include "base/values.h" | 15 #include "base/values.h" |
17 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
18 #include "chrome/browser/chrome_thread.h" | 17 #include "chrome/browser/chrome_thread.h" |
19 #include "chrome/browser/chromeos/wm_ipc.h" | 18 #include "chrome/browser/chromeos/wm_ipc.h" |
20 #include "chrome/browser/pref_service.h" | 19 #include "chrome/browser/pref_service.h" |
21 #include "chrome/common/chrome_paths.h" | 20 #include "chrome/common/chrome_paths.h" |
22 #include "chrome/common/notification_service.h" | 21 #include "chrome/common/notification_service.h" |
23 #include "gfx/codec/png_codec.h" | 22 #include "gfx/codec/png_codec.h" |
24 #include "grit/theme_resources.h" | 23 #include "grit/theme_resources.h" |
25 | 24 |
26 namespace chromeos { | 25 namespace chromeos { |
27 | 26 |
28 namespace { | 27 namespace { |
29 | 28 |
30 // A vector pref of the users who have logged into the device. | 29 // A vector pref of the users who have logged into the device. |
31 const wchar_t kLoggedInUsers[] = L"LoggedInUsers"; | 30 const wchar_t kLoggedInUsers[] = L"LoggedInUsers"; |
32 // A dictionary that maps usernames to file paths to their images. | 31 // A dictionary that maps usernames to file paths to their images. |
33 const wchar_t kUserImages[] = L"UserImages"; | 32 const wchar_t kUserImages[] = L"UserImages"; |
34 | 33 |
35 // The one true UserManager. | 34 // The one true UserManager. |
36 static UserManager* user_manager_ = NULL; | 35 static UserManager* user_manager_ = NULL; |
37 | 36 |
38 // Stores path to the image in local state. Runs on UI thread. | |
39 void save_path_to_local_state(const std::string& username, | |
40 const std::string& image_path) { | |
41 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | |
42 PrefService* local_state = g_browser_process->local_state(); | |
43 DictionaryValue* images = | |
44 local_state->GetMutableDictionary(kUserImages); | |
45 images->SetWithoutPathExpansion(UTF8ToWide(username), | |
46 new StringValue(image_path)); | |
47 LOG(INFO) << "Saving path to user image in Local State."; | |
48 local_state->ScheduleSavePersistentPrefs(); | |
49 } | |
50 | |
51 // Saves image to file with specified path. Runs on FILE thread. | |
52 // Posts task for saving image path to local state on UI thread. | |
53 void save_image_to_file(const SkBitmap& image, | |
54 const FilePath& image_path, | |
55 const std::string& username) { | |
56 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); | |
57 std::vector<unsigned char> encoded_image; | |
58 if (!gfx::PNGCodec::EncodeBGRASkBitmap(image, true, &encoded_image)) { | |
59 LOG(ERROR) << "Failed to PNG encode the image."; | |
60 return; | |
61 } | |
62 | |
63 if (file_util::WriteFile(image_path, | |
64 reinterpret_cast<char*>(&encoded_image[0]), | |
65 encoded_image.size()) == -1) { | |
66 LOG(ERROR) << "Failed to save image to file."; | |
67 return; | |
68 } | |
69 | |
70 ChromeThread::PostTask( | |
71 ChromeThread::UI, | |
72 FROM_HERE, | |
73 NewRunnableFunction(&save_path_to_local_state, | |
74 username, image_path.value())); | |
75 } | |
76 | |
77 } // namespace | 37 } // namespace |
78 | 38 |
79 UserManager::User::User() { | 39 UserManager::User::User() { |
80 image_ = *ResourceBundle::GetSharedInstance().GetBitmapNamed( | 40 image_ = *ResourceBundle::GetSharedInstance().GetBitmapNamed( |
81 IDR_LOGIN_DEFAULT_USER); | 41 IDR_LOGIN_DEFAULT_USER); |
82 } | 42 } |
83 | 43 |
84 std::string UserManager::User::GetDisplayName() const { | 44 std::string UserManager::User::GetDisplayName() const { |
85 size_t i = email_.find('@'); | 45 size_t i = email_.find('@'); |
86 if (i == 0 || i == std::string::npos) { | 46 if (i == 0 || i == std::string::npos) { |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 } else { | 128 } else { |
169 logged_in_user_ = *it; | 129 logged_in_user_ = *it; |
170 } | 130 } |
171 } | 131 } |
172 prefs->ScheduleSavePersistentPrefs(); | 132 prefs->ScheduleSavePersistentPrefs(); |
173 NotifyOnLogin(); | 133 NotifyOnLogin(); |
174 } | 134 } |
175 | 135 |
176 void UserManager::SaveUserImage(const std::string& username, | 136 void UserManager::SaveUserImage(const std::string& username, |
177 const SkBitmap& image) { | 137 const SkBitmap& image) { |
178 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | |
179 std::string filename = username + ".png"; | 138 std::string filename = username + ".png"; |
180 FilePath user_data_dir; | 139 FilePath user_data_dir; |
181 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); | 140 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
182 FilePath image_path = user_data_dir.AppendASCII(filename); | 141 FilePath image_path = user_data_dir.AppendASCII(filename); |
183 LOG(INFO) << "Saving user image to " << image_path.value(); | 142 std::vector<unsigned char> encoded_image; |
| 143 if (!gfx::PNGCodec::EncodeBGRASkBitmap(image, true, &encoded_image)) |
| 144 return; |
184 | 145 |
185 ChromeThread::PostTask( | 146 ChromeThread::PostTask( |
186 ChromeThread::FILE, | 147 ChromeThread::FILE, |
187 FROM_HERE, | 148 FROM_HERE, |
188 NewRunnableFunction(&save_image_to_file, | 149 NewRunnableFunction<int(*)(const FilePath&, const char*, int)>( |
189 image, image_path, username)); | 150 &file_util::WriteFile, |
| 151 image_path, |
| 152 reinterpret_cast<char*>(&encoded_image[0]), |
| 153 encoded_image.size())); |
| 154 PrefService* local_state = g_browser_process->local_state(); |
| 155 DictionaryValue* images = |
| 156 local_state->GetMutableDictionary(kUserImages); |
| 157 images->SetWithoutPathExpansion(ASCIIToWide(username), |
| 158 new StringValue(image_path.value())); |
| 159 local_state->ScheduleSavePersistentPrefs(); |
190 } | 160 } |
191 | 161 |
192 void UserManager::OnImageLoaded(const std::string& username, | 162 void UserManager::OnImageLoaded(const std::string& username, |
193 const SkBitmap& image) { | 163 const SkBitmap& image) { |
194 LOG(INFO) << "Loaded image for " << username; | 164 LOG(INFO) << "Loaded image for " << username; |
195 user_images_[username] = image; | 165 user_images_[username] = image; |
196 User user; | 166 User user; |
197 user.set_email(username); | 167 user.set_email(username); |
198 user.set_image(image); | 168 user.set_image(image); |
199 NotificationService::current()->Notify( | 169 NotificationService::current()->Notify( |
(...skipping 15 matching lines...) Expand all Loading... |
215 NotificationService::current()->Notify( | 185 NotificationService::current()->Notify( |
216 NotificationType::LOGIN_USER_CHANGED, | 186 NotificationType::LOGIN_USER_CHANGED, |
217 Source<UserManager>(this), | 187 Source<UserManager>(this), |
218 Details<const User>(&logged_in_user_)); | 188 Details<const User>(&logged_in_user_)); |
219 | 189 |
220 // Let the window manager know that we're logged in now. | 190 // Let the window manager know that we're logged in now. |
221 WmIpc::instance()->SetLoggedInProperty(true); | 191 WmIpc::instance()->SetLoggedInProperty(true); |
222 } | 192 } |
223 | 193 |
224 } // namespace chromeos | 194 } // namespace chromeos |
OLD | NEW |