Chromium Code Reviews| Index: chrome/browser/download/notification/download_notification_item.cc |
| diff --git a/chrome/browser/download/notification/download_notification_item.cc b/chrome/browser/download/notification/download_notification_item.cc |
| index 0e3c98ab060cade1e85b81d3e788889a9baa4817..84e57a3a80fe950227a475d0e13dddb650e2689d 100644 |
| --- a/chrome/browser/download/notification/download_notification_item.cc |
| +++ b/chrome/browser/download/notification/download_notification_item.cc |
| @@ -4,6 +4,7 @@ |
| #include "chrome/browser/download/notification/download_notification_item.h" |
| +#include "base/files/file_util.h" |
| #include "base/strings/string_number_conversions.h" |
| #include "chrome/browser/browser_process.h" |
| #include "chrome/browser/download/download_crx_util.h" |
| @@ -15,6 +16,7 @@ |
| #include "chrome/common/url_constants.h" |
| #include "chrome/grit/chromium_strings.h" |
| #include "chrome/grit/generated_resources.h" |
| +#include "components/mime_util/mime_util.h" |
| #include "content/public/browser/browser_context.h" |
| #include "content/public/browser/browser_thread.h" |
| #include "content/public/browser/download_interrupt_reasons.h" |
| @@ -22,8 +24,12 @@ |
| #include "content/public/browser/page_navigator.h" |
| #include "content/public/browser/web_contents.h" |
| #include "grit/theme_resources.h" |
| +#include "net/base/mime_util.h" |
| +#include "third_party/skia/include/core/SkBitmap.h" |
|
asanka
2015/06/11 03:30:51
Nit: Already included in download_notification_ite
yoshiki
2015/06/11 08:22:09
Done.
|
| #include "ui/base/l10n/l10n_util.h" |
| #include "ui/base/resource/resource_bundle.h" |
| +#include "ui/gfx/codec/jpeg_codec.h" |
| +#include "ui/gfx/image/image.h" |
| #include "ui/message_center/message_center.h" |
| namespace { |
| @@ -31,6 +37,10 @@ namespace { |
| const char kDownloadNotificationNotifierId[] = |
| "chrome://downloads/notification/id-notifier"; |
| +// Maximum size of preview image. If the image exceeds this size, don't show the |
| +// preview image. |
| +const int64 kMaxImagePreviewSize = 10 * 1024 * 1024; // 10 MB |
| + |
| } // anonymous namespace |
| // static |
| @@ -51,7 +61,7 @@ DownloadNotificationItem::NotificationWatcher::~NotificationWatcher() { |
| } |
| void DownloadNotificationItem::NotificationWatcher::Close(bool by_user) { |
| - // Do nothing. |
| + item_->OnNotificationClose(by_user); |
| } |
| void DownloadNotificationItem::NotificationWatcher::Click() { |
| @@ -77,7 +87,8 @@ DownloadNotificationItem::DownloadNotificationItem(content::DownloadItem* item, |
| : profile_(profile), |
| watcher_(new NotificationWatcher(this)), |
| item_(item), |
| - delegate_(delegate) { |
| + delegate_(delegate), |
| + weak_factory_(this) { |
| item->AddObserver(this); |
| // Notify that the instance is just created. |
| @@ -164,6 +175,13 @@ void DownloadNotificationItem::OnNotificationButtonClick(int button_index) { |
| UpdateNotificationData(ADD_NEW); |
| } |
| +void DownloadNotificationItem::OnNotificationClose(bool by_user) { |
| + if (image_decode_status_ == IN_PROGRESS) { |
| + image_decode_status_ = NOT_STARTED; |
| + ImageDecoder::Cancel(this); |
| + } |
| +} |
| + |
| // DownloadItem::Observer methods |
| void DownloadNotificationItem::OnDownloadUpdated(content::DownloadItem* item) { |
| DCHECK_EQ(item, item_); |
| @@ -200,6 +218,8 @@ void DownloadNotificationItem::CloseNotificationByUser() { |
| void DownloadNotificationItem::UpdateNotificationData( |
| NotificationUpdateType type) { |
| + CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
|
asanka
2015/06/11 03:30:51
DCHECK_CURRENTLY_ON
yoshiki
2015/06/11 08:22:09
Done.
|
| + |
| DownloadItemModel model(item_); |
| DownloadCommands command(item_); |
| @@ -217,7 +237,7 @@ void DownloadNotificationItem::UpdateNotificationData( |
| notification_->set_message(GetWarningText()); |
| // Show icon. |
| - SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_MALICIOUS); |
| + SetNotificationIcon(IDR_DOWNLOAD_NOTIFICATION_MALICIOUS); |
| } else { |
| notification_->set_title(GetTitle()); |
| notification_->set_message(model.GetStatusText()); |
| @@ -231,9 +251,9 @@ void DownloadNotificationItem::UpdateNotificationData( |
| notification_->set_progress(item_->PercentComplete()); |
| if (is_off_the_record) { |
| // TODO(yoshiki): Replace the tentative image. |
| - SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_INCOGNITO); |
| + SetNotificationIcon(IDR_DOWNLOAD_NOTIFICATION_INCOGNITO); |
| } else { |
| - SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_DOWNLOADING); |
| + SetNotificationIcon(IDR_DOWNLOAD_NOTIFICATION_DOWNLOADING); |
| } |
| break; |
| case content::DownloadItem::COMPLETE: |
| @@ -248,9 +268,9 @@ void DownloadNotificationItem::UpdateNotificationData( |
| if (is_off_the_record) { |
| // TODO(yoshiki): Replace the tentative image. |
| - SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_INCOGNITO); |
| + SetNotificationIcon(IDR_DOWNLOAD_NOTIFICATION_INCOGNITO); |
| } else { |
| - SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_DOWNLOADING); |
| + SetNotificationIcon(IDR_DOWNLOAD_NOTIFICATION_DOWNLOADING); |
| } |
| break; |
| case content::DownloadItem::CANCELLED: |
| @@ -269,7 +289,7 @@ void DownloadNotificationItem::UpdateNotificationData( |
| // be updated. (same as the case of type = COMPLETE) |
| notification_->set_type(message_center::NOTIFICATION_TYPE_PROGRESS); |
| notification_->set_progress(0); |
| - SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_WARNING); |
| + SetNotificationIcon(IDR_DOWNLOAD_NOTIFICATION_WARNING); |
| break; |
| case content::DownloadItem::MAX_DOWNLOAD_STATE: // sentinel |
| NOTREACHED(); |
| @@ -290,10 +310,6 @@ void DownloadNotificationItem::UpdateNotificationData( |
| } |
| notification_->set_buttons(notification_actions); |
| - if (item_->IsDone()) { |
| - // TODO(yoshiki): If the downloaded file is an image, show the thumbnail. |
| - } |
| - |
| if (type == ADD_NEW) { |
| notification_ui_manager()->Add(*notification_, profile_); |
| } else if (type == UPDATE_EXISTING) { |
| @@ -315,6 +331,34 @@ void DownloadNotificationItem::UpdateNotificationData( |
| } |
| previous_download_state_ = item_->GetState(); |
| + |
| + if (item_->IsDone() && image_decode_status_ == NOT_STARTED && |
| + item_->GetReceivedBytes() <= kMaxImagePreviewSize) { |
| + DCHECK(notification_->image().IsEmpty()); |
| + |
| + image_decode_status_ = IN_PROGRESS; |
| + |
| + bool maybe_image = false; |
| + if (mime_util::IsSupportedImageMimeType(item_->GetMimeType())) { |
| + maybe_image = true; |
| + } else { |
| + std::string mime; |
| + if (net::GetWellKnownMimeTypeFromExtension( |
| + item_->GetTargetFilePath().FinalExtension(), |
| + &mime) && |
| + mime_util::IsSupportedImageMimeType(mime)) { |
| + maybe_image = true; |
| + } |
| + } |
| + |
| + if (maybe_image) { |
| + base::FilePath file_path = item_->GetFullPath(); |
| + // TODO(yoshiki): Shrink the image size to supress memory usage. |
| + ImageDecoder::Start(this, file_path); |
| + } else { |
| + image_decode_status_ = NOT_IMAGE; |
| + } |
| + } |
| } |
| void DownloadNotificationItem::OnDownloadOpened(content::DownloadItem* item) { |
| @@ -338,7 +382,7 @@ void DownloadNotificationItem::OnDownloadDestroyed( |
| item_ = nullptr; |
| } |
| -void DownloadNotificationItem::SetNotificationImage(int resource_id) { |
| +void DownloadNotificationItem::SetNotificationIcon(int resource_id) { |
| if (image_resource_id_ == resource_id) |
| return; |
| ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); |
| @@ -346,6 +390,19 @@ void DownloadNotificationItem::SetNotificationImage(int resource_id) { |
| notification_->set_icon(bundle.GetImageNamed(image_resource_id_)); |
| } |
| +void DownloadNotificationItem::OnImageDecoded(const SkBitmap& decoded_image) { |
| + gfx::Image image = gfx::Image::CreateFrom1xBitmap(decoded_image); |
| + notification_->set_image(image); |
| + image_decode_status_ = DONE; |
| + UpdateNotificationData(UPDATE_EXISTING); |
| +} |
| + |
| +void DownloadNotificationItem::OnDecodeImageFailed() { |
| + notification_->set_image(gfx::Image()); |
| + image_decode_status_ = FAILED; |
| + UpdateNotificationData(UPDATE_EXISTING); |
| +} |
| + |
| NotificationUIManager* DownloadNotificationItem::notification_ui_manager() |
| const { |
| if (stub_notification_ui_manager_for_testing_) { |