Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(547)

Side by Side Diff: chrome/browser/download/notification/download_notification_item.cc

Issue 1159363002: [Download Notification] Show preview if downloaded file is image (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use ImageLoader Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/download/notification/download_notification_item.h" 5 #include "chrome/browser/download/notification/download_notification_item.h"
6 6
7 #include "base/files/file_util.h"
7 #include "base/strings/string_number_conversions.h" 8 #include "base/strings/string_number_conversions.h"
8 #include "chrome/browser/browser_process.h" 9 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/download/download_crx_util.h" 10 #include "chrome/browser/download/download_crx_util.h"
10 #include "chrome/browser/download/download_item_model.h" 11 #include "chrome/browser/download/download_item_model.h"
11 #include "chrome/browser/notifications/notification.h" 12 #include "chrome/browser/notifications/notification.h"
12 #include "chrome/browser/notifications/notification_ui_manager.h" 13 #include "chrome/browser/notifications/notification_ui_manager.h"
13 #include "chrome/browser/notifications/profile_notification.h" 14 #include "chrome/browser/notifications/profile_notification.h"
14 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" 15 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
15 #include "chrome/common/url_constants.h" 16 #include "chrome/common/url_constants.h"
16 #include "chrome/grit/chromium_strings.h" 17 #include "chrome/grit/chromium_strings.h"
17 #include "chrome/grit/generated_resources.h" 18 #include "chrome/grit/generated_resources.h"
18 #include "content/public/browser/browser_context.h" 19 #include "content/public/browser/browser_context.h"
19 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/download_interrupt_reasons.h" 21 #include "content/public/browser/download_interrupt_reasons.h"
21 #include "content/public/browser/download_item.h" 22 #include "content/public/browser/download_item.h"
22 #include "content/public/browser/page_navigator.h" 23 #include "content/public/browser/page_navigator.h"
23 #include "content/public/browser/web_contents.h" 24 #include "content/public/browser/web_contents.h"
24 #include "grit/theme_resources.h" 25 #include "grit/theme_resources.h"
26 #include "net/base/mime_util.h"
27 #include "third_party/skia/include/core/SkBitmap.h"
25 #include "ui/base/l10n/l10n_util.h" 28 #include "ui/base/l10n/l10n_util.h"
26 #include "ui/base/resource/resource_bundle.h" 29 #include "ui/base/resource/resource_bundle.h"
30 #include "ui/gfx/codec/jpeg_codec.h"
31 #include "ui/gfx/image/image.h"
27 #include "ui/message_center/message_center.h" 32 #include "ui/message_center/message_center.h"
28 33
29 namespace { 34 namespace {
30 35
31 const char kDownloadNotificationNotifierId[] = 36 const char kDownloadNotificationNotifierId[] =
32 "chrome://downloads/notification/id-notifier"; 37 "chrome://downloads/notification/id-notifier";
33 38
39 // Maximum size of preview image. If the image exceeds this size, don't show the
40 // preview image.
41 const int64 kMaxImagePreviewSize = 10 * 1024 * 1024; // 10 MB
42
43 std::string ReadNotificationImage(
44 const std::string type, const base::FilePath file_path) {
45 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
46
47 gfx::Image image;
48
49 std::string data;
50 bool ret = base::ReadFileToString(file_path, &data);
51 if (!ret)
52 return std::string();
53
54 if (data.size() > kMaxImagePreviewSize)
asanka 2015/06/09 00:13:38 Check the size of the file rather than checking th
yoshiki 2015/06/10 15:05:04 The file size is checked at l.336 in the patchset
55 return std::string();
56
57 return data;
58 }
59
34 } // anonymous namespace 60 } // anonymous namespace
35 61
36 // static 62 // static
37 const char DownloadNotificationItem::kDownloadNotificationOrigin[] = 63 const char DownloadNotificationItem::kDownloadNotificationOrigin[] =
38 "chrome://downloads"; 64 "chrome://downloads";
39 65
40 // static 66 // static
41 StubNotificationUIManager* 67 StubNotificationUIManager*
42 DownloadNotificationItem::stub_notification_ui_manager_for_testing_ = 68 DownloadNotificationItem::stub_notification_ui_manager_for_testing_ =
43 nullptr; 69 nullptr;
(...skipping 26 matching lines...) Expand all
70 std::string DownloadNotificationItem::NotificationWatcher::id() const { 96 std::string DownloadNotificationItem::NotificationWatcher::id() const {
71 return base::UintToString(item_->item_->GetId()); 97 return base::UintToString(item_->item_->GetId());
72 } 98 }
73 99
74 DownloadNotificationItem::DownloadNotificationItem(content::DownloadItem* item, 100 DownloadNotificationItem::DownloadNotificationItem(content::DownloadItem* item,
75 Profile* profile, 101 Profile* profile,
76 Delegate* delegate) 102 Delegate* delegate)
77 : profile_(profile), 103 : profile_(profile),
78 watcher_(new NotificationWatcher(this)), 104 watcher_(new NotificationWatcher(this)),
79 item_(item), 105 item_(item),
80 delegate_(delegate) { 106 delegate_(delegate),
107 weak_factory_(this) {
81 item->AddObserver(this); 108 item->AddObserver(this);
82 109
83 // Notify that the instance is just created. 110 // Notify that the instance is just created.
84 delegate_->OnCreated(this); 111 delegate_->OnCreated(this);
85 112
86 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); 113 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
87 114
88 message_center::RichNotificationData data; 115 message_center::RichNotificationData data;
89 // Creates the notification instance. |title| and |body| will be overridden 116 // Creates the notification instance. |title| and |body| will be overridden
90 // by UpdateNotificationData() below. 117 // by UpdateNotificationData() below.
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 // because it's by user action. So, we request closing of it directlly to 220 // because it's by user action. So, we request closing of it directlly to
194 // MessageCenter instance. 221 // MessageCenter instance.
195 // Note that: this calling has no side-effect even when the message center 222 // Note that: this calling has no side-effect even when the message center
196 // is not opened. 223 // is not opened.
197 g_browser_process->message_center()->RemoveNotification( 224 g_browser_process->message_center()->RemoveNotification(
198 notification_id_in_message_center, true /* by_user */); 225 notification_id_in_message_center, true /* by_user */);
199 } 226 }
200 227
201 void DownloadNotificationItem::UpdateNotificationData( 228 void DownloadNotificationItem::UpdateNotificationData(
202 NotificationUpdateType type) { 229 NotificationUpdateType type) {
230 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
231
203 DownloadItemModel model(item_); 232 DownloadItemModel model(item_);
204 DownloadCommands command(item_); 233 DownloadCommands command(item_);
205 234
206 if (previous_download_state_ != content::DownloadItem::IN_PROGRESS) { 235 if (previous_download_state_ != content::DownloadItem::IN_PROGRESS) {
207 if (item_->GetState() == content::DownloadItem::IN_PROGRESS) 236 if (item_->GetState() == content::DownloadItem::IN_PROGRESS)
208 delegate_->OnDownloadStarted(this); 237 delegate_->OnDownloadStarted(this);
209 } else { 238 } else {
210 if (item_->GetState() != content::DownloadItem::IN_PROGRESS) 239 if (item_->GetState() != content::DownloadItem::IN_PROGRESS)
211 delegate_->OnDownloadStopped(this); 240 delegate_->OnDownloadStopped(this);
212 } 241 }
213 242
214 if (item_->IsDangerous()) { 243 if (item_->IsDangerous()) {
215 notification_->set_type(message_center::NOTIFICATION_TYPE_BASE_FORMAT); 244 notification_->set_type(message_center::NOTIFICATION_TYPE_BASE_FORMAT);
216 notification_->set_title(GetTitle()); 245 notification_->set_title(GetTitle());
217 notification_->set_message(GetWarningText()); 246 notification_->set_message(GetWarningText());
218 247
219 // Show icon. 248 // Show icon.
220 SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_MALICIOUS); 249 SetNotificationIcon(IDR_DOWNLOAD_NOTIFICATION_MALICIOUS);
221 } else { 250 } else {
222 notification_->set_title(GetTitle()); 251 notification_->set_title(GetTitle());
223 notification_->set_message(model.GetStatusText()); 252 notification_->set_message(model.GetStatusText());
224 253
225 bool is_off_the_record = item_->GetBrowserContext() && 254 bool is_off_the_record = item_->GetBrowserContext() &&
226 item_->GetBrowserContext()->IsOffTheRecord(); 255 item_->GetBrowserContext()->IsOffTheRecord();
227 256
228 switch (item_->GetState()) { 257 switch (item_->GetState()) {
229 case content::DownloadItem::IN_PROGRESS: 258 case content::DownloadItem::IN_PROGRESS:
230 notification_->set_type(message_center::NOTIFICATION_TYPE_PROGRESS); 259 notification_->set_type(message_center::NOTIFICATION_TYPE_PROGRESS);
231 notification_->set_progress(item_->PercentComplete()); 260 notification_->set_progress(item_->PercentComplete());
232 if (is_off_the_record) { 261 if (is_off_the_record) {
233 // TODO(yoshiki): Replace the tentative image. 262 // TODO(yoshiki): Replace the tentative image.
234 SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_INCOGNITO); 263 SetNotificationIcon(IDR_DOWNLOAD_NOTIFICATION_INCOGNITO);
235 } else { 264 } else {
236 SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_DOWNLOADING); 265 SetNotificationIcon(IDR_DOWNLOAD_NOTIFICATION_DOWNLOADING);
237 } 266 }
238 break; 267 break;
239 case content::DownloadItem::COMPLETE: 268 case content::DownloadItem::COMPLETE:
240 DCHECK(item_->IsDone()); 269 DCHECK(item_->IsDone());
241 270
242 // Shows a notifiation as progress type once so the visible content will 271 // Shows a notifiation as progress type once so the visible content will
243 // be updated. 272 // be updated.
244 // Note: only progress-type notification's content will be updated 273 // Note: only progress-type notification's content will be updated
245 // immediately when the message center is visible. 274 // immediately when the message center is visible.
246 notification_->set_type(message_center::NOTIFICATION_TYPE_PROGRESS); 275 notification_->set_type(message_center::NOTIFICATION_TYPE_PROGRESS);
247 notification_->set_progress(100); 276 notification_->set_progress(100);
248 277
249 if (is_off_the_record) { 278 if (is_off_the_record) {
250 // TODO(yoshiki): Replace the tentative image. 279 // TODO(yoshiki): Replace the tentative image.
251 SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_INCOGNITO); 280 SetNotificationIcon(IDR_DOWNLOAD_NOTIFICATION_INCOGNITO);
252 } else { 281 } else {
253 SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_DOWNLOADING); 282 SetNotificationIcon(IDR_DOWNLOAD_NOTIFICATION_DOWNLOADING);
254 } 283 }
255 break; 284 break;
256 case content::DownloadItem::CANCELLED: 285 case content::DownloadItem::CANCELLED:
257 // Confgirms that a download is cancelled by user action. 286 // Confgirms that a download is cancelled by user action.
258 DCHECK(item_->GetLastReason() == 287 DCHECK(item_->GetLastReason() ==
259 content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED || 288 content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED ||
260 item_->GetLastReason() == 289 item_->GetLastReason() ==
261 content::DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN); 290 content::DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN);
262 291
263 CloseNotificationByUser(); 292 CloseNotificationByUser();
264 293
265 previous_download_state_ = item_->GetState(); 294 previous_download_state_ = item_->GetState();
266 return; // Skips the remaining since the notification has closed. 295 return; // Skips the remaining since the notification has closed.
267 case content::DownloadItem::INTERRUPTED: 296 case content::DownloadItem::INTERRUPTED:
268 // Shows a notifiation as progress type once so the visible content will 297 // Shows a notifiation as progress type once so the visible content will
269 // be updated. (same as the case of type = COMPLETE) 298 // be updated. (same as the case of type = COMPLETE)
270 notification_->set_type(message_center::NOTIFICATION_TYPE_PROGRESS); 299 notification_->set_type(message_center::NOTIFICATION_TYPE_PROGRESS);
271 notification_->set_progress(0); 300 notification_->set_progress(0);
272 SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_WARNING); 301 SetNotificationIcon(IDR_DOWNLOAD_NOTIFICATION_WARNING);
273 break; 302 break;
274 case content::DownloadItem::MAX_DOWNLOAD_STATE: // sentinel 303 case content::DownloadItem::MAX_DOWNLOAD_STATE: // sentinel
275 NOTREACHED(); 304 NOTREACHED();
276 } 305 }
277 } 306 }
278 307
279 std::vector<message_center::ButtonInfo> notification_actions; 308 std::vector<message_center::ButtonInfo> notification_actions;
280 scoped_ptr<std::vector<DownloadCommands::Command>> actions( 309 scoped_ptr<std::vector<DownloadCommands::Command>> actions(
281 GetExtraActions().Pass()); 310 GetExtraActions().Pass());
282 311
283 button_actions_.reset(new std::vector<DownloadCommands::Command>); 312 button_actions_.reset(new std::vector<DownloadCommands::Command>);
284 for (auto it = actions->begin(); it != actions->end(); it++) { 313 for (auto it = actions->begin(); it != actions->end(); it++) {
285 button_actions_->push_back(*it); 314 button_actions_->push_back(*it);
286 message_center::ButtonInfo button_info = 315 message_center::ButtonInfo button_info =
287 message_center::ButtonInfo(GetCommandLabel(*it)); 316 message_center::ButtonInfo(GetCommandLabel(*it));
288 button_info.icon = command.GetCommandIcon(*it); 317 button_info.icon = command.GetCommandIcon(*it);
289 notification_actions.push_back(button_info); 318 notification_actions.push_back(button_info);
290 } 319 }
291 notification_->set_buttons(notification_actions); 320 notification_->set_buttons(notification_actions);
292 321
293 if (item_->IsDone()) {
294 // TODO(yoshiki): If the downloaded file is an image, show the thumbnail.
295 }
296
297 if (type == ADD_NEW) { 322 if (type == ADD_NEW) {
298 notification_ui_manager()->Add(*notification_, profile_); 323 notification_ui_manager()->Add(*notification_, profile_);
299 } else if (type == UPDATE_EXISTING) { 324 } else if (type == UPDATE_EXISTING) {
300 notification_ui_manager()->Update(*notification_, profile_); 325 notification_ui_manager()->Update(*notification_, profile_);
301 326
302 // When the download is just completed (or interrupted), close the 327 // When the download is just completed (or interrupted), close the
303 // notification once and re-show it immediately so it'll pop up. 328 // notification once and re-show it immediately so it'll pop up.
304 if ((item_->GetState() == content::DownloadItem::COMPLETE && 329 if ((item_->GetState() == content::DownloadItem::COMPLETE &&
305 previous_download_state_ != content::DownloadItem::COMPLETE) || 330 previous_download_state_ != content::DownloadItem::COMPLETE) ||
306 (item_->GetState() == content::DownloadItem::INTERRUPTED && 331 (item_->GetState() == content::DownloadItem::INTERRUPTED &&
307 previous_download_state_ != content::DownloadItem::INTERRUPTED)) { 332 previous_download_state_ != content::DownloadItem::INTERRUPTED)) {
308 CloseNotificationByNonUser(); 333 CloseNotificationByNonUser();
309 // Changes the type from PROGRESS to BASE_FORMAT. 334 // Changes the type from PROGRESS to BASE_FORMAT.
310 notification_->set_type(message_center::NOTIFICATION_TYPE_BASE_FORMAT); 335 notification_->set_type(message_center::NOTIFICATION_TYPE_BASE_FORMAT);
311 notification_ui_manager()->Add(*notification_, profile_); 336 notification_ui_manager()->Add(*notification_, profile_);
312 } 337 }
313 } else { 338 } else {
314 NOTREACHED(); 339 NOTREACHED();
315 } 340 }
316 341
317 previous_download_state_ = item_->GetState(); 342 previous_download_state_ = item_->GetState();
343
344 if (item_->IsDone() && !set_image_ &&
345 item_->GetTotalBytes() <= kMaxImagePreviewSize) {
346 DCHECK(notification_->image().IsEmpty());
347
348 set_image_ = true;
349
350 std::string type;
351 std::string mime_topleveltype;
352 std::string mime_subtype;
353 if (net::ParseMimeTypeWithoutParameter(
asanka 2015/06/09 00:13:38 Use mime_util::IsSupportedImageMimeType()
yoshiki 2015/06/10 15:05:04 Done.
354 item_->GetMimeType(), &mime_topleveltype, &mime_subtype) &&
355 mime_topleveltype == "image" &&
356 (mime_subtype == "png" || mime_subtype == "jpg" ||
357 mime_subtype == "jpeg")) {
358 type = mime_subtype;
359 } else {
360 std::string dot_extension = item_->GetTargetFilePath().FinalExtension();
asanka 2015/06/09 00:13:38 Use net::GetMimeTypeFromFile(). Note that this can
yoshiki 2015/06/10 15:05:04 I think we don't need to check the file contents f
361 if (!dot_extension.empty()) {
362 std::string extension = dot_extension.substr(1);
363 if (extension == "png" || extension == "jpg" || extension == "jpeg")
364 type = extension;
365 }
366 }
367
368 if (!type.empty()) {
369 base::FilePath file_path = item_->GetFullPath();
370 content::BrowserThread::PostTaskAndReplyWithResult(
371 content::BrowserThread::FILE,
372 FROM_HERE,
373 base::Bind(&ReadNotificationImage,
374 type,
375 file_path),
376 base::Bind(&DownloadNotificationItem::OnImageLoaded,
377 weak_factory_.GetWeakPtr()));
378 }
379 }
318 } 380 }
319 381
320 void DownloadNotificationItem::OnDownloadOpened(content::DownloadItem* item) { 382 void DownloadNotificationItem::OnDownloadOpened(content::DownloadItem* item) {
321 DCHECK_EQ(item, item_); 383 DCHECK_EQ(item, item_);
322 // Do nothing. 384 // Do nothing.
323 } 385 }
324 386
325 void DownloadNotificationItem::OnDownloadRemoved(content::DownloadItem* item) { 387 void DownloadNotificationItem::OnDownloadRemoved(content::DownloadItem* item) {
326 DCHECK_EQ(item, item_); 388 DCHECK_EQ(item, item_);
327 389
328 // Removing the notification causes calling |NotificationDelegate::Close()|. 390 // Removing the notification causes calling |NotificationDelegate::Close()|.
329 notification_ui_manager()->CancelById( 391 notification_ui_manager()->CancelById(
330 watcher_->id(), NotificationUIManager::GetProfileID(profile_)); 392 watcher_->id(), NotificationUIManager::GetProfileID(profile_));
331 delegate_->OnDownloadRemoved(this); 393 delegate_->OnDownloadRemoved(this);
332 } 394 }
333 395
334 void DownloadNotificationItem::OnDownloadDestroyed( 396 void DownloadNotificationItem::OnDownloadDestroyed(
335 content::DownloadItem* item) { 397 content::DownloadItem* item) {
336 DCHECK_EQ(item, item_); 398 DCHECK_EQ(item, item_);
337 399
338 item_ = nullptr; 400 item_ = nullptr;
339 } 401 }
340 402
341 void DownloadNotificationItem::SetNotificationImage(int resource_id) { 403 void DownloadNotificationItem::SetNotificationIcon(int resource_id) {
342 if (image_resource_id_ == resource_id) 404 if (image_resource_id_ == resource_id)
343 return; 405 return;
344 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); 406 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
345 image_resource_id_ = resource_id; 407 image_resource_id_ = resource_id;
346 notification_->set_icon(bundle.GetImageNamed(image_resource_id_)); 408 notification_->set_icon(bundle.GetImageNamed(image_resource_id_));
347 } 409 }
348 410
411 void DownloadNotificationItem::OnImageLoaded(std::string image_data) {
412 if (image_data.empty())
413 return;
414
415 // TODO(yoshiki): Set option to reduce the image size to supress memory usage.
asanka 2015/06/09 00:13:38 No. We should be passing a file handle over to the
yoshiki 2015/06/10 15:05:04 Done. Resizing will be implemented later.
416 ImageDecoder::Start(this, image_data);
asanka 2015/06/09 00:13:38 You should call ImageDecoder::Cancel() if the noti
yoshiki 2015/06/10 15:05:04 Done.
417 }
418
419 void DownloadNotificationItem::OnImageDecoded(const SkBitmap& decoded_image) {
420 gfx::Image image = gfx::Image::CreateFrom1xBitmap(decoded_image);
421 notification_->set_image(image);
422 UpdateNotificationData(UPDATE_EXISTING);
423 }
424
425 void DownloadNotificationItem::OnDecodeImageFailed() {
426 notification_->set_image(gfx::Image());
427 UpdateNotificationData(UPDATE_EXISTING);
428 }
429
349 NotificationUIManager* DownloadNotificationItem::notification_ui_manager() 430 NotificationUIManager* DownloadNotificationItem::notification_ui_manager()
350 const { 431 const {
351 if (stub_notification_ui_manager_for_testing_) { 432 if (stub_notification_ui_manager_for_testing_) {
352 return stub_notification_ui_manager_for_testing_; 433 return stub_notification_ui_manager_for_testing_;
353 } 434 }
354 return g_browser_process->notification_ui_manager(); 435 return g_browser_process->notification_ui_manager();
355 } 436 }
356 437
357 scoped_ptr<std::vector<DownloadCommands::Command>> 438 scoped_ptr<std::vector<DownloadCommands::Command>>
358 DownloadNotificationItem::GetExtraActions() const { 439 DownloadNotificationItem::GetExtraActions() const {
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
497 NOTREACHED(); 578 NOTREACHED();
498 return base::string16(); 579 return base::string16();
499 } 580 }
500 581
501 Browser* DownloadNotificationItem::GetBrowser() { 582 Browser* DownloadNotificationItem::GetBrowser() {
502 chrome::ScopedTabbedBrowserDisplayer browser_displayer( 583 chrome::ScopedTabbedBrowserDisplayer browser_displayer(
503 profile_, chrome::GetActiveDesktop()); 584 profile_, chrome::GetActiveDesktop());
504 DCHECK(browser_displayer.browser()); 585 DCHECK(browser_displayer.browser());
505 return browser_displayer.browser(); 586 return browser_displayer.browser();
506 } 587 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698