OLD | NEW |
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_item_notification.h" | 5 #include "chrome/browser/download/notification/download_item_notification.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "base/task_scheduler/post_task.h" | 12 #include "base/task_scheduler/post_task.h" |
13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 #include "chrome/app/vector_icons/vector_icons.h" |
14 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
15 #include "chrome/browser/download/download_crx_util.h" | 16 #include "chrome/browser/download/download_crx_util.h" |
16 #include "chrome/browser/download/download_item_model.h" | 17 #include "chrome/browser/download/download_item_model.h" |
17 #include "chrome/browser/download/notification/download_notification_manager.h" | 18 #include "chrome/browser/download/notification/download_notification_manager.h" |
18 #include "chrome/browser/notifications/notification.h" | 19 #include "chrome/browser/notifications/notification.h" |
19 #include "chrome/browser/notifications/notification_ui_manager.h" | 20 #include "chrome/browser/notifications/notification_ui_manager.h" |
20 #include "chrome/browser/notifications/profile_notification.h" | 21 #include "chrome/browser/notifications/profile_notification.h" |
21 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" | 22 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" |
22 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
23 #include "chrome/grit/chromium_strings.h" | 24 #include "chrome/grit/chromium_strings.h" |
(...skipping 11 matching lines...) Expand all Loading... |
35 #include "net/base/mime_util.h" | 36 #include "net/base/mime_util.h" |
36 #include "third_party/skia/include/core/SkCanvas.h" | 37 #include "third_party/skia/include/core/SkCanvas.h" |
37 #include "ui/base/l10n/l10n_util.h" | 38 #include "ui/base/l10n/l10n_util.h" |
38 #include "ui/base/l10n/time_format.h" | 39 #include "ui/base/l10n/time_format.h" |
39 #include "ui/base/resource/resource_bundle.h" | 40 #include "ui/base/resource/resource_bundle.h" |
40 #include "ui/base/text/bytes_formatting.h" | 41 #include "ui/base/text/bytes_formatting.h" |
41 #include "ui/gfx/codec/jpeg_codec.h" | 42 #include "ui/gfx/codec/jpeg_codec.h" |
42 #include "ui/gfx/color_palette.h" | 43 #include "ui/gfx/color_palette.h" |
43 #include "ui/gfx/image/image.h" | 44 #include "ui/gfx/image/image.h" |
44 #include "ui/gfx/paint_vector_icon.h" | 45 #include "ui/gfx/paint_vector_icon.h" |
45 #include "ui/gfx/vector_icons_public.h" | |
46 #include "ui/message_center/message_center.h" | 46 #include "ui/message_center/message_center.h" |
47 #include "ui/message_center/message_center_style.h" | 47 #include "ui/message_center/message_center_style.h" |
| 48 #include "ui/vector_icons/vector_icons.h" |
48 | 49 |
49 #if defined(OS_CHROMEOS) | 50 #if defined(OS_CHROMEOS) |
50 #include "chrome/browser/chromeos/note_taking_helper.h" | 51 #include "chrome/browser/chromeos/note_taking_helper.h" |
51 #endif // defined(OS_CHROMEOS) | 52 #endif // defined(OS_CHROMEOS) |
52 | 53 |
53 using base::UserMetricsAction; | 54 using base::UserMetricsAction; |
54 | 55 |
55 namespace { | 56 namespace { |
56 | 57 |
57 const char kDownloadNotificationNotifierId[] = | 58 const char kDownloadNotificationNotifierId[] = |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 } | 176 } |
176 | 177 |
177 } // anonymous namespace | 178 } // anonymous namespace |
178 | 179 |
179 DownloadItemNotification::DownloadItemNotification( | 180 DownloadItemNotification::DownloadItemNotification( |
180 content::DownloadItem* item, | 181 content::DownloadItem* item, |
181 DownloadNotificationManagerForProfile* manager) | 182 DownloadNotificationManagerForProfile* manager) |
182 : item_(item), | 183 : item_(item), |
183 message_center_(manager->message_center()), | 184 message_center_(manager->message_center()), |
184 weak_factory_(this) { | 185 weak_factory_(this) { |
185 | |
186 // Creates the notification instance. |title|, |body| and |icon| will be | 186 // Creates the notification instance. |title|, |body| and |icon| will be |
187 // overridden by UpdateNotificationData() below. | 187 // overridden by UpdateNotificationData() below. |
188 notification_.reset(new Notification( | 188 notification_.reset(new Notification( |
189 message_center::NOTIFICATION_TYPE_PROGRESS, | 189 message_center::NOTIFICATION_TYPE_PROGRESS, |
190 base::string16(), // title | 190 base::string16(), // title |
191 base::string16(), // body | 191 base::string16(), // body |
192 gfx::Image(), // icon | 192 gfx::Image(), // icon |
193 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, | 193 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, |
194 kDownloadNotificationNotifierId), | 194 kDownloadNotificationNotifierId), |
195 base::string16(), // display_source | 195 base::string16(), // display_source |
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
491 } | 491 } |
492 | 492 |
493 void DownloadItemNotification::UpdateNotificationIcon() { | 493 void DownloadItemNotification::UpdateNotificationIcon() { |
494 if (item_->IsDangerous()) { | 494 if (item_->IsDangerous()) { |
495 DownloadItemModel model(item_); | 495 DownloadItemModel model(item_); |
496 #if defined(OS_MACOSX) | 496 #if defined(OS_MACOSX) |
497 SetNotificationIcon(model.MightBeMalicious() | 497 SetNotificationIcon(model.MightBeMalicious() |
498 ? IDR_DOWNLOAD_NOTIFICATION_WARNING_BAD | 498 ? IDR_DOWNLOAD_NOTIFICATION_WARNING_BAD |
499 : IDR_DOWNLOAD_NOTIFICATION_WARNING_UNWANTED); | 499 : IDR_DOWNLOAD_NOTIFICATION_WARNING_UNWANTED); |
500 #else | 500 #else |
501 SetNotificationVectorIcon( | 501 SetNotificationVectorIcon(ui::kWarningIcon, model.MightBeMalicious() |
502 gfx::VectorIconId::WARNING, | 502 ? gfx::kGoogleRed700 |
503 model.MightBeMalicious() ? gfx::kGoogleRed700 : gfx::kGoogleYellow700); | 503 : gfx::kGoogleYellow700); |
504 #endif | 504 #endif |
505 return; | 505 return; |
506 } | 506 } |
507 | 507 |
508 bool is_off_the_record = item_->GetBrowserContext() && | 508 bool is_off_the_record = item_->GetBrowserContext() && |
509 item_->GetBrowserContext()->IsOffTheRecord(); | 509 item_->GetBrowserContext()->IsOffTheRecord(); |
510 switch (item_->GetState()) { | 510 switch (item_->GetState()) { |
511 case content::DownloadItem::IN_PROGRESS: | 511 case content::DownloadItem::IN_PROGRESS: |
512 case content::DownloadItem::COMPLETE: | 512 case content::DownloadItem::COMPLETE: |
513 if (is_off_the_record) { | 513 if (is_off_the_record) { |
514 #if defined(OS_MACOSX) | 514 #if defined(OS_MACOSX) |
515 SetNotificationIcon(IDR_DOWNLOAD_NOTIFICATION_INCOGNITO); | 515 SetNotificationIcon(IDR_DOWNLOAD_NOTIFICATION_INCOGNITO); |
516 #else | 516 #else |
517 SetNotificationVectorIcon(gfx::VectorIconId::FILE_DOWNLOAD_INCOGNITO, | 517 SetNotificationVectorIcon(kFileDownloadIncognitoIcon, |
518 gfx::kChromeIconGrey); | 518 gfx::kChromeIconGrey); |
519 #endif | 519 #endif |
520 } else { | 520 } else { |
521 SetNotificationVectorIcon(gfx::VectorIconId::FILE_DOWNLOAD, | 521 SetNotificationVectorIcon(kFileDownloadIcon, gfx::kGoogleBlue500); |
522 gfx::kGoogleBlue500); | |
523 } | 522 } |
524 break; | 523 break; |
525 | 524 |
526 case content::DownloadItem::INTERRUPTED: | 525 case content::DownloadItem::INTERRUPTED: |
527 #if defined(OS_MACOSX) | 526 #if defined(OS_MACOSX) |
528 SetNotificationIcon(IDR_DOWNLOAD_NOTIFICATION_ERROR); | 527 SetNotificationIcon(IDR_DOWNLOAD_NOTIFICATION_ERROR); |
529 #else | 528 #else |
530 SetNotificationVectorIcon(gfx::VectorIconId::ERROR_CIRCLE, | 529 SetNotificationVectorIcon(ui::kErrorCircleIcon, gfx::kGoogleRed700); |
531 gfx::kGoogleRed700); | |
532 #endif | 530 #endif |
533 break; | 531 break; |
534 | 532 |
535 case content::DownloadItem::CANCELLED: | 533 case content::DownloadItem::CANCELLED: |
536 break; | 534 break; |
537 | 535 |
538 case content::DownloadItem::MAX_DOWNLOAD_STATE: | 536 case content::DownloadItem::MAX_DOWNLOAD_STATE: |
539 NOTREACHED(); | 537 NOTREACHED(); |
540 break; | 538 break; |
541 } | 539 } |
542 } | 540 } |
543 | 541 |
544 void DownloadItemNotification::OnDownloadRemoved(content::DownloadItem* item) { | 542 void DownloadItemNotification::OnDownloadRemoved(content::DownloadItem* item) { |
545 // The given |item| may be already free'd. | 543 // The given |item| may be already free'd. |
546 DCHECK_EQ(item, item_); | 544 DCHECK_EQ(item, item_); |
547 | 545 |
548 // Removing the notification causes calling |NotificationDelegate::Close()|. | 546 // Removing the notification causes calling |NotificationDelegate::Close()|. |
549 if (g_browser_process->notification_ui_manager()) { | 547 if (g_browser_process->notification_ui_manager()) { |
550 g_browser_process->notification_ui_manager()->CancelById( | 548 g_browser_process->notification_ui_manager()->CancelById( |
551 watcher()->id(), NotificationUIManager::GetProfileID(profile())); | 549 watcher()->id(), NotificationUIManager::GetProfileID(profile())); |
552 } | 550 } |
553 | 551 |
554 item_ = nullptr; | 552 item_ = nullptr; |
555 } | 553 } |
556 | 554 |
557 void DownloadItemNotification::SetNotificationIcon(int resource_id) { | 555 void DownloadItemNotification::SetNotificationIcon(int resource_id) { |
558 if (image_resource_id_ == resource_id) | |
559 return; | |
560 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | 556 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); |
561 image_resource_id_ = resource_id; | 557 notification_->set_icon(bundle.GetImageNamed(resource_id)); |
562 notification_->set_icon(bundle.GetImageNamed(image_resource_id_)); | |
563 } | 558 } |
564 | 559 |
565 void DownloadItemNotification::SetNotificationVectorIcon(gfx::VectorIconId id, | 560 void DownloadItemNotification::SetNotificationVectorIcon( |
566 SkColor color) { | 561 const gfx::VectorIcon& icon, |
567 if (vector_icon_params_ == std::make_pair(id, color)) | 562 SkColor color) { |
568 return; | 563 notification_->set_icon(gfx::Image(gfx::CreateVectorIcon(icon, 40, color))); |
569 vector_icon_params_ = std::make_pair(id, color); | |
570 image_resource_id_ = 0; | |
571 notification_->set_icon(gfx::Image(gfx::CreateVectorIcon(id, 40, color))); | |
572 } | 564 } |
573 | 565 |
574 void DownloadItemNotification::DisablePopup() { | 566 void DownloadItemNotification::DisablePopup() { |
575 if (notification_->priority() == message_center::LOW_PRIORITY) | 567 if (notification_->priority() == message_center::LOW_PRIORITY) |
576 return; | 568 return; |
577 // Hides a notification from popup notifications if it's a pop-up, by | 569 // Hides a notification from popup notifications if it's a pop-up, by |
578 // decreasing its priority and reshowing itself. Low-priority notifications | 570 // decreasing its priority and reshowing itself. Low-priority notifications |
579 // doesn't pop-up itself so this logic works as disabling pop-up. | 571 // doesn't pop-up itself so this logic works as disabling pop-up. |
580 CloseNotificationByNonUser(); | 572 CloseNotificationByNonUser(); |
581 notification_->set_priority(message_center::LOW_PRIORITY); | 573 notification_->set_priority(message_center::LOW_PRIORITY); |
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
937 const std::string notification_id_in_message_center = notification->id(); | 929 const std::string notification_id_in_message_center = notification->id(); |
938 | 930 |
939 message_center::NotificationList::Notifications visible_notifications = | 931 message_center::NotificationList::Notifications visible_notifications = |
940 message_center_->GetVisibleNotifications(); | 932 message_center_->GetVisibleNotifications(); |
941 for (auto* notification : visible_notifications) { | 933 for (auto* notification : visible_notifications) { |
942 if (notification->id() == notification_id_in_message_center) | 934 if (notification->id() == notification_id_in_message_center) |
943 return true; | 935 return true; |
944 } | 936 } |
945 return false; | 937 return false; |
946 } | 938 } |
OLD | NEW |