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