OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/ui/webui/options2/chromeos/change_picture_options_handl
er.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/metrics/histogram.h" | |
10 #include "base/path_service.h" | |
11 #include "base/string_util.h" | |
12 #include "base/values.h" | |
13 #include "chrome/browser/chromeos/login/camera_detector.h" | |
14 #include "chrome/browser/chromeos/login/default_user_images.h" | |
15 #include "chrome/browser/chromeos/login/user_manager.h" | |
16 #include "chrome/browser/chromeos/options2/take_photo_dialog.h" | |
17 #include "chrome/browser/profiles/profile.h" | |
18 #include "chrome/browser/ui/browser_list.h" | |
19 #include "chrome/browser/ui/browser_window.h" | |
20 #include "chrome/browser/ui/dialog_style.h" | |
21 #include "chrome/browser/ui/views/window.h" | |
22 #include "chrome/browser/ui/webui/web_ui_util.h" | |
23 #include "chrome/common/chrome_notification_types.h" | |
24 #include "chrome/common/chrome_paths.h" | |
25 #include "chrome/common/url_constants.h" | |
26 #include "content/public/browser/notification_service.h" | |
27 #include "content/public/common/url_constants.h" | |
28 #include "grit/generated_resources.h" | |
29 #include "grit/theme_resources.h" | |
30 #include "third_party/skia/include/core/SkBitmap.h" | |
31 #include "ui/base/l10n/l10n_util.h" | |
32 #include "ui/base/resource/resource_bundle.h" | |
33 #include "ui/views/widget/widget.h" | |
34 | |
35 namespace chromeos { | |
36 | |
37 namespace { | |
38 | |
39 // Returns info about extensions for files we support as user images. | |
40 SelectFileDialog::FileTypeInfo GetUserImageFileTypeInfo() { | |
41 SelectFileDialog::FileTypeInfo file_type_info; | |
42 file_type_info.extensions.resize(5); | |
43 | |
44 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("bmp")); | |
45 | |
46 file_type_info.extensions[1].push_back(FILE_PATH_LITERAL("gif")); | |
47 | |
48 file_type_info.extensions[2].push_back(FILE_PATH_LITERAL("jpg")); | |
49 file_type_info.extensions[2].push_back(FILE_PATH_LITERAL("jpeg")); | |
50 | |
51 file_type_info.extensions[3].push_back(FILE_PATH_LITERAL("png")); | |
52 | |
53 file_type_info.extensions[4].push_back(FILE_PATH_LITERAL("tif")); | |
54 file_type_info.extensions[4].push_back(FILE_PATH_LITERAL("tiff")); | |
55 | |
56 return file_type_info; | |
57 } | |
58 | |
59 } // namespace | |
60 | |
61 ChangePictureOptionsHandler::ChangePictureOptionsHandler() | |
62 : previous_image_data_url_(chrome::kAboutBlankURL), | |
63 previous_image_index_(User::kInvalidImageIndex), | |
64 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | |
65 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_IMAGE_UPDATED, | |
66 content::NotificationService::AllSources()); | |
67 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_IMAGE_UPDATE_FAILED, | |
68 content::NotificationService::AllSources()); | |
69 } | |
70 | |
71 ChangePictureOptionsHandler::~ChangePictureOptionsHandler() { | |
72 if (select_file_dialog_.get()) | |
73 select_file_dialog_->ListenerDestroyed(); | |
74 } | |
75 | |
76 void ChangePictureOptionsHandler::GetLocalizedValues( | |
77 DictionaryValue* localized_strings) { | |
78 DCHECK(localized_strings); | |
79 localized_strings->SetString("changePicturePage", | |
80 l10n_util::GetStringUTF16(IDS_OPTIONS_CHANGE_PICTURE_DIALOG_TITLE)); | |
81 localized_strings->SetString("changePicturePageDescription", | |
82 l10n_util::GetStringUTF16(IDS_OPTIONS_CHANGE_PICTURE_DIALOG_TEXT)); | |
83 localized_strings->SetString("takePhoto", | |
84 l10n_util::GetStringUTF16(IDS_OPTIONS_CHANGE_PICTURE_TAKE_PHOTO)); | |
85 localized_strings->SetString("chooseFile", | |
86 l10n_util::GetStringUTF16(IDS_OPTIONS_CHANGE_PICTURE_CHOOSE_FILE)); | |
87 localized_strings->SetString("profilePhoto", | |
88 l10n_util::GetStringUTF16(IDS_OPTIONS_CHANGE_PICTURE_PROFILE_PHOTO)); | |
89 localized_strings->SetString("profilePhotoLoading", | |
90 l10n_util::GetStringUTF16( | |
91 IDS_OPTIONS_CHANGE_PICTURE_PROFILE_LOADING_PHOTO)); | |
92 } | |
93 | |
94 void ChangePictureOptionsHandler::RegisterMessages() { | |
95 DCHECK(web_ui_); | |
96 web_ui_->RegisterMessageCallback("chooseFile", | |
97 base::Bind(&ChangePictureOptionsHandler::HandleChooseFile, | |
98 base::Unretained(this))); | |
99 web_ui_->RegisterMessageCallback("takePhoto", | |
100 base::Bind(&ChangePictureOptionsHandler::HandleTakePhoto, | |
101 base::Unretained(this))); | |
102 web_ui_->RegisterMessageCallback("onChangePicturePageShown", | |
103 base::Bind(&ChangePictureOptionsHandler::HandlePageShown, | |
104 base::Unretained(this))); | |
105 web_ui_->RegisterMessageCallback("onChangePicturePageInitialized", | |
106 base::Bind(&ChangePictureOptionsHandler::HandlePageInitialized, | |
107 base::Unretained(this))); | |
108 web_ui_->RegisterMessageCallback("selectImage", | |
109 base::Bind(&ChangePictureOptionsHandler::HandleSelectImage, | |
110 base::Unretained(this))); | |
111 } | |
112 | |
113 void ChangePictureOptionsHandler::SendDefaultImages() { | |
114 ListValue image_urls; | |
115 for (int i = 0; i < kDefaultImagesCount; ++i) { | |
116 image_urls.Append(new StringValue(GetDefaultImageUrl(i))); | |
117 } | |
118 web_ui_->CallJavascriptFunction("ChangePictureOptions.setDefaultImages", | |
119 image_urls); | |
120 } | |
121 | |
122 void ChangePictureOptionsHandler::HandleChooseFile(const ListValue* args) { | |
123 DCHECK(args && args->empty()); | |
124 if (!select_file_dialog_.get()) | |
125 select_file_dialog_ = SelectFileDialog::Create(this); | |
126 | |
127 FilePath downloads_path; | |
128 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &downloads_path)) { | |
129 NOTREACHED(); | |
130 return; | |
131 } | |
132 | |
133 // Static so we initialize it only once. | |
134 CR_DEFINE_STATIC_LOCAL(SelectFileDialog::FileTypeInfo, file_type_info, | |
135 (GetUserImageFileTypeInfo())); | |
136 | |
137 select_file_dialog_->SelectFile( | |
138 SelectFileDialog::SELECT_OPEN_FILE, | |
139 l10n_util::GetStringUTF16(IDS_DOWNLOAD_TITLE), | |
140 downloads_path, | |
141 &file_type_info, | |
142 0, | |
143 FILE_PATH_LITERAL(""), | |
144 web_ui_->tab_contents(), | |
145 GetBrowserWindow(), | |
146 NULL); | |
147 } | |
148 | |
149 void ChangePictureOptionsHandler::HandleTakePhoto(const ListValue* args) { | |
150 DCHECK(args && args->empty()); | |
151 views::Widget* window = browser::CreateViewsWindow( | |
152 GetBrowserWindow(), | |
153 new TakePhotoDialog(this), | |
154 STYLE_GENERIC); | |
155 window->SetAlwaysOnTop(true); | |
156 window->Show(); | |
157 } | |
158 | |
159 void ChangePictureOptionsHandler::HandlePageInitialized( | |
160 const base::ListValue* args) { | |
161 DCHECK(args && args->empty()); | |
162 // If no camera presence check has been performed in this session, | |
163 // start one now. | |
164 if (CameraDetector::camera_presence() == | |
165 CameraDetector::kCameraPresenceUnknown) { | |
166 CheckCameraPresence(); | |
167 } | |
168 | |
169 // While the check is in progress, use previous camera presence state and | |
170 // presume it is present if no check has been performed yet. | |
171 SetCameraPresent(CameraDetector::camera_presence() != | |
172 CameraDetector::kCameraAbsent); | |
173 | |
174 SendDefaultImages(); | |
175 } | |
176 | |
177 void ChangePictureOptionsHandler::HandlePageShown(const base::ListValue* args) { | |
178 DCHECK(args && args->empty()); | |
179 // TODO(ivankr): If user opens settings and goes to Change Picture page right | |
180 // after the check started |HandlePageInitialized| has been completed, | |
181 // |CheckCameraPresence| will be called twice, should be throttled. | |
182 CheckCameraPresence(); | |
183 SendSelectedImage(); | |
184 UpdateProfileImage(); | |
185 } | |
186 | |
187 void ChangePictureOptionsHandler::SendSelectedImage() { | |
188 const User& user = UserManager::Get()->logged_in_user(); | |
189 DCHECK(!user.email().empty()); | |
190 | |
191 previous_image_index_ = user.image_index(); | |
192 switch (previous_image_index_) { | |
193 case User::kExternalImageIndex: { | |
194 // User has image from camera/file, record it and add to the image list. | |
195 previous_image_ = user.image(); | |
196 previous_image_data_url_ = web_ui_util::GetImageDataUrl(previous_image_); | |
197 web_ui_->CallJavascriptFunction("ChangePictureOptions.setOldImage"); | |
198 break; | |
199 } | |
200 case User::kProfileImageIndex: { | |
201 // User has his/her Profile image as the current image. | |
202 SendProfileImage(user.image(), true); | |
203 break; | |
204 } | |
205 default: { | |
206 DCHECK(previous_image_index_ >= 0 && | |
207 previous_image_index_ < kDefaultImagesCount); | |
208 // User has image from the set of default images. | |
209 base::StringValue image_url(GetDefaultImageUrl(previous_image_index_)); | |
210 web_ui_->CallJavascriptFunction("ChangePictureOptions.setSelectedImage", | |
211 image_url); | |
212 } | |
213 } | |
214 } | |
215 | |
216 void ChangePictureOptionsHandler::SendProfileImage(const SkBitmap& image, | |
217 bool should_select) { | |
218 base::StringValue data_url(web_ui_util::GetImageDataUrl(image)); | |
219 base::FundamentalValue select(should_select); | |
220 web_ui_->CallJavascriptFunction("ChangePictureOptions.setProfileImage", | |
221 data_url, select); | |
222 } | |
223 | |
224 void ChangePictureOptionsHandler::UpdateProfileImage() { | |
225 UserManager* user_manager = UserManager::Get(); | |
226 | |
227 // If we have a downloaded profile image and haven't sent it in | |
228 // |SendSelectedImage|, send it now (without selecting). | |
229 if (previous_image_index_ != User::kProfileImageIndex && | |
230 !user_manager->downloaded_profile_image().empty()) | |
231 SendProfileImage(user_manager->downloaded_profile_image(), false); | |
232 | |
233 user_manager->DownloadProfileImage(); | |
234 } | |
235 | |
236 void ChangePictureOptionsHandler::HandleSelectImage(const ListValue* args) { | |
237 std::string image_url; | |
238 if (!args || | |
239 args->GetSize() != 1 || | |
240 !args->GetString(0, &image_url)) { | |
241 NOTREACHED(); | |
242 return; | |
243 } | |
244 DCHECK(!image_url.empty()); | |
245 | |
246 UserManager* user_manager = UserManager::Get(); | |
247 const User& user = user_manager->logged_in_user(); | |
248 int image_index = User::kInvalidImageIndex; | |
249 | |
250 if (StartsWithASCII(image_url, chrome::kChromeUIUserImageURL, false)) { | |
251 // Image from file/camera uses kChromeUIUserImageURL as URL while | |
252 // current profile image always has a full data URL. | |
253 // This way transition from (current profile image) to | |
254 // (profile image, current image from file) is easier. | |
255 | |
256 DCHECK(!previous_image_.empty()); | |
257 user_manager->SaveUserImage(user.email(), previous_image_); | |
258 | |
259 UMA_HISTOGRAM_ENUMERATION("UserImage.ChangeChoice", | |
260 kHistogramImageOld, | |
261 kHistogramImagesCount); | |
262 VLOG(1) << "Selected old user image"; | |
263 } else if (IsDefaultImageUrl(image_url, &image_index)) { | |
264 // One of the default user images. | |
265 user_manager->SaveUserDefaultImageIndex(user.email(), image_index); | |
266 | |
267 UMA_HISTOGRAM_ENUMERATION("UserImage.ChangeChoice", | |
268 image_index, | |
269 kHistogramImagesCount); | |
270 VLOG(1) << "Selected default user image: " << image_index; | |
271 } else { | |
272 // Profile image selected. Could be previous (old) user image. | |
273 user_manager->SaveUserImageFromProfileImage(user.email()); | |
274 | |
275 if (previous_image_index_ == User::kProfileImageIndex) { | |
276 UMA_HISTOGRAM_ENUMERATION("UserImage.ChangeChoice", | |
277 kHistogramImageOld, | |
278 kHistogramImagesCount); | |
279 VLOG(1) << "Selected old (profile) user image"; | |
280 } else { | |
281 UMA_HISTOGRAM_ENUMERATION("UserImage.ChangeChoice", | |
282 kHistogramImageFromProfile, | |
283 kHistogramImagesCount); | |
284 VLOG(1) << "Selected profile image"; | |
285 } | |
286 } | |
287 } | |
288 | |
289 void ChangePictureOptionsHandler::FileSelected(const FilePath& path, | |
290 int index, | |
291 void* params) { | |
292 UserManager* user_manager = UserManager::Get(); | |
293 user_manager->SaveUserImageFromFile(user_manager->logged_in_user().email(), | |
294 path); | |
295 UMA_HISTOGRAM_ENUMERATION("UserImage.ChangeChoice", | |
296 kHistogramImageFromFile, | |
297 kHistogramImagesCount); | |
298 } | |
299 | |
300 void ChangePictureOptionsHandler::OnPhotoAccepted(const SkBitmap& photo) { | |
301 UserManager* user_manager = UserManager::Get(); | |
302 user_manager->SaveUserImage(user_manager->logged_in_user().email(), photo); | |
303 UMA_HISTOGRAM_ENUMERATION("UserImage.ChangeChoice", | |
304 kHistogramImageFromCamera, | |
305 kHistogramImagesCount); | |
306 } | |
307 | |
308 void ChangePictureOptionsHandler::CheckCameraPresence() { | |
309 CameraDetector::StartPresenceCheck( | |
310 base::Bind(&ChangePictureOptionsHandler::OnCameraPresenceCheckDone, | |
311 weak_factory_.GetWeakPtr())); | |
312 } | |
313 | |
314 void ChangePictureOptionsHandler::SetCameraPresent(bool present) { | |
315 base::FundamentalValue present_value(present); | |
316 web_ui_->CallJavascriptFunction("ChangePictureOptions.setCameraPresent", | |
317 present_value); | |
318 } | |
319 | |
320 void ChangePictureOptionsHandler::OnCameraPresenceCheckDone() { | |
321 SetCameraPresent(CameraDetector::camera_presence() == | |
322 CameraDetector::kCameraPresent); | |
323 } | |
324 | |
325 void ChangePictureOptionsHandler::Observe( | |
326 int type, | |
327 const content::NotificationSource& source, | |
328 const content::NotificationDetails& details) { | |
329 OptionsPageUIHandler::Observe(type, source, details); | |
330 if (type == chrome::NOTIFICATION_PROFILE_IMAGE_UPDATED) { | |
331 // User profile image has been updated. | |
332 SendProfileImage(*content::Details<const SkBitmap>(details).ptr(), false); | |
333 } | |
334 } | |
335 | |
336 gfx::NativeWindow ChangePictureOptionsHandler::GetBrowserWindow() const { | |
337 Browser* browser = | |
338 BrowserList::FindBrowserWithProfile(Profile::FromWebUI(web_ui_)); | |
339 if (!browser) | |
340 return NULL; | |
341 return browser->window()->GetNativeHandle(); | |
342 } | |
343 | |
344 } // namespace chromeos | |
OLD | NEW |