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..6891a955cd618f0325be790147473734406909d9 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,11 @@ |
| #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 "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 +36,23 @@ 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 |
| + |
| +std::string ReadNotificationImage(const base::FilePath& file_path) { |
| + DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| + |
| + std::string data; |
| + bool ret = base::ReadFileToString(file_path, &data); |
| + if (!ret) |
| + return std::string(); |
| + |
| + DCHECK_LE(data.size(), static_cast<size_t>(kMaxImagePreviewSize)); |
| + |
| + return data; |
| +} |
| + |
| } // anonymous namespace |
| // static |
| @@ -51,7 +73,7 @@ DownloadNotificationItem::NotificationWatcher::~NotificationWatcher() { |
| } |
| void DownloadNotificationItem::NotificationWatcher::Close(bool by_user) { |
| - // Do nothing. |
| + item_->OnNotificationClose(by_user); |
| } |
| void DownloadNotificationItem::NotificationWatcher::Click() { |
| @@ -77,7 +99,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. |
| @@ -107,6 +130,9 @@ DownloadNotificationItem::DownloadNotificationItem(content::DownloadItem* item, |
| } |
| DownloadNotificationItem::~DownloadNotificationItem() { |
| + if (image_decode_status_ == IN_PROGRESS) |
| + ImageDecoder::Cancel(this); |
| + |
| if (item_) |
| item_->RemoveObserver(this); |
| } |
| @@ -164,6 +190,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 +233,8 @@ void DownloadNotificationItem::CloseNotificationByUser() { |
| void DownloadNotificationItem::UpdateNotificationData( |
| NotificationUpdateType type) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + |
| DownloadItemModel model(item_); |
| DownloadCommands command(item_); |
| @@ -217,7 +252,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 +266,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 +283,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 +304,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 +325,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 +346,44 @@ void DownloadNotificationItem::UpdateNotificationData( |
| } |
| previous_download_state_ = item_->GetState(); |
| + |
| + if (item_->IsDone() && image_decode_status_ == NOT_STARTED) { |
| + // TODO(yoshiki): Add a metrics to take the statistics of image file sizes. |
| + |
| + if (item_->GetReceivedBytes() > kMaxImagePreviewSize) |
| + return; |
| + |
| + 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; |
| + std::string extension_with_dot = |
|
asanka
2015/06/16 15:10:20
base::FilePath::StringType
yoshiki
2015/06/16 16:41:42
Done.
|
| + item_->GetTargetFilePath().FinalExtension(); |
| + if (!extension_with_dot.empty() && |
| + net::GetWellKnownMimeTypeFromExtension( |
| + extension_with_dot.substr(1), |
| + &mime) && |
| + mime_util::IsSupportedImageMimeType(mime)) { |
| + maybe_image = true; |
| + } |
| + } |
| + |
| + if (maybe_image) { |
| + base::FilePath file_path = item_->GetFullPath(); |
| + base::PostTaskAndReplyWithResult( |
| + content::BrowserThread::GetBlockingPool(), |
| + FROM_HERE, |
| + base::Bind(&ReadNotificationImage, |
| + file_path), |
| + base::Bind(&DownloadNotificationItem::OnImageLoaded, |
| + weak_factory_.GetWeakPtr())); |
| + } |
| + } |
| } |
| void DownloadNotificationItem::OnDownloadOpened(content::DownloadItem* item) { |
| @@ -338,7 +407,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 +415,27 @@ void DownloadNotificationItem::SetNotificationImage(int resource_id) { |
| notification_->set_icon(bundle.GetImageNamed(image_resource_id_)); |
| } |
| +void DownloadNotificationItem::OnImageLoaded(const std::string& image_data) { |
| + if (image_data.empty()) |
| + return; |
| + |
| + // TODO(yoshiki): Set option to reduce the image size to supress memory usage. |
| + ImageDecoder::Start(this, image_data); |
| +} |
| + |
| +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_) { |