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

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: Addressed comments #27 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"
19 #include "components/mime_util/mime_util.h"
18 #include "content/public/browser/browser_context.h" 20 #include "content/public/browser/browser_context.h"
19 #include "content/public/browser/browser_thread.h" 21 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/download_interrupt_reasons.h" 22 #include "content/public/browser/download_interrupt_reasons.h"
21 #include "content/public/browser/download_item.h" 23 #include "content/public/browser/download_item.h"
22 #include "content/public/browser/page_navigator.h" 24 #include "content/public/browser/page_navigator.h"
23 #include "content/public/browser/web_contents.h" 25 #include "content/public/browser/web_contents.h"
24 #include "grit/theme_resources.h" 26 #include "grit/theme_resources.h"
27 #include "net/base/mime_util.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(const base::FilePath& file_path) {
44 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
45
46 std::string data;
47 bool ret = base::ReadFileToString(file_path, &data);
48 if (!ret)
49 return std::string();
50
51 DCHECK_NE(data.size(), static_cast<size_t>(kMaxImagePreviewSize));
Lei Zhang 2015/06/13 00:27:26 Did you mean DCHECK_LE() ?
yoshiki 2015/06/16 16:41:42 Oh, sorry. Done
52
53 return data;
54 }
55
34 } // anonymous namespace 56 } // anonymous namespace
35 57
36 // static 58 // static
37 const char DownloadNotificationItem::kDownloadNotificationOrigin[] = 59 const char DownloadNotificationItem::kDownloadNotificationOrigin[] =
38 "chrome://downloads"; 60 "chrome://downloads";
39 61
40 // static 62 // static
41 StubNotificationUIManager* 63 StubNotificationUIManager*
42 DownloadNotificationItem::stub_notification_ui_manager_for_testing_ = 64 DownloadNotificationItem::stub_notification_ui_manager_for_testing_ =
43 nullptr; 65 nullptr;
44 66
45 DownloadNotificationItem::NotificationWatcher::NotificationWatcher( 67 DownloadNotificationItem::NotificationWatcher::NotificationWatcher(
46 DownloadNotificationItem* item) 68 DownloadNotificationItem* item)
47 : item_(item) { 69 : item_(item) {
48 } 70 }
49 71
50 DownloadNotificationItem::NotificationWatcher::~NotificationWatcher() { 72 DownloadNotificationItem::NotificationWatcher::~NotificationWatcher() {
51 } 73 }
52 74
53 void DownloadNotificationItem::NotificationWatcher::Close(bool by_user) { 75 void DownloadNotificationItem::NotificationWatcher::Close(bool by_user) {
54 // Do nothing. 76 item_->OnNotificationClose(by_user);
55 } 77 }
56 78
57 void DownloadNotificationItem::NotificationWatcher::Click() { 79 void DownloadNotificationItem::NotificationWatcher::Click() {
58 item_->OnNotificationClick(); 80 item_->OnNotificationClick();
59 } 81 }
60 82
61 bool DownloadNotificationItem::NotificationWatcher::HasClickedListener() { 83 bool DownloadNotificationItem::NotificationWatcher::HasClickedListener() {
62 return true; 84 return true;
63 } 85 }
64 86
65 void DownloadNotificationItem::NotificationWatcher::ButtonClick( 87 void DownloadNotificationItem::NotificationWatcher::ButtonClick(
66 int button_index) { 88 int button_index) {
67 item_->OnNotificationButtonClick(button_index); 89 item_->OnNotificationButtonClick(button_index);
68 } 90 }
69 91
70 std::string DownloadNotificationItem::NotificationWatcher::id() const { 92 std::string DownloadNotificationItem::NotificationWatcher::id() const {
71 return base::UintToString(item_->item_->GetId()); 93 return base::UintToString(item_->item_->GetId());
72 } 94 }
73 95
74 DownloadNotificationItem::DownloadNotificationItem(content::DownloadItem* item, 96 DownloadNotificationItem::DownloadNotificationItem(content::DownloadItem* item,
75 Profile* profile, 97 Profile* profile,
76 Delegate* delegate) 98 Delegate* delegate)
77 : profile_(profile), 99 : profile_(profile),
78 watcher_(new NotificationWatcher(this)), 100 watcher_(new NotificationWatcher(this)),
79 item_(item), 101 item_(item),
80 delegate_(delegate) { 102 delegate_(delegate),
103 weak_factory_(this) {
81 item->AddObserver(this); 104 item->AddObserver(this);
82 105
83 // Notify that the instance is just created. 106 // Notify that the instance is just created.
84 delegate_->OnCreated(this); 107 delegate_->OnCreated(this);
85 108
86 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); 109 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
87 110
88 message_center::RichNotificationData data; 111 message_center::RichNotificationData data;
89 // Creates the notification instance. |title| and |body| will be overridden 112 // Creates the notification instance. |title| and |body| will be overridden
90 // by UpdateNotificationData() below. 113 // by UpdateNotificationData() below.
91 notification_.reset(new Notification( 114 notification_.reset(new Notification(
92 message_center::NOTIFICATION_TYPE_PROGRESS, 115 message_center::NOTIFICATION_TYPE_PROGRESS,
93 GURL(kDownloadNotificationOrigin), // origin_url 116 GURL(kDownloadNotificationOrigin), // origin_url
94 base::string16(), // title 117 base::string16(), // title
95 base::string16(), // body 118 base::string16(), // body
96 bundle.GetImageNamed(IDR_DOWNLOAD_NOTIFICATION_DOWNLOADING), 119 bundle.GetImageNamed(IDR_DOWNLOAD_NOTIFICATION_DOWNLOADING),
97 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, 120 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT,
98 kDownloadNotificationNotifierId), 121 kDownloadNotificationNotifierId),
99 base::string16(), // display_source 122 base::string16(), // display_source
100 base::UintToString(item_->GetId()), // tag 123 base::UintToString(item_->GetId()), // tag
101 data, watcher_.get())); 124 data, watcher_.get()));
102 125
103 notification_->set_progress(0); 126 notification_->set_progress(0);
104 notification_->set_never_timeout(false); 127 notification_->set_never_timeout(false);
105 128
106 UpdateNotificationData(ADD_NEW); 129 UpdateNotificationData(ADD_NEW);
107 } 130 }
108 131
109 DownloadNotificationItem::~DownloadNotificationItem() { 132 DownloadNotificationItem::~DownloadNotificationItem() {
133 if (image_decode_status_ == IN_PROGRESS)
134 ImageDecoder::Cancel(this);
135
110 if (item_) 136 if (item_)
111 item_->RemoveObserver(this); 137 item_->RemoveObserver(this);
112 } 138 }
113 139
114 void DownloadNotificationItem::OnNotificationClick() { 140 void DownloadNotificationItem::OnNotificationClick() {
115 if (item_->IsDangerous()) { 141 if (item_->IsDangerous()) {
116 #if defined(FULL_SAFE_BROWSING) 142 #if defined(FULL_SAFE_BROWSING)
117 DownloadCommands(item_).ExecuteCommand( 143 DownloadCommands(item_).ExecuteCommand(
118 DownloadCommands::LEARN_MORE_SCANNING); 144 DownloadCommands::LEARN_MORE_SCANNING);
119 #else 145 #else
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 CloseNotificationByUser(); 183 CloseNotificationByUser();
158 } 184 }
159 185
160 DownloadCommands(item_).ExecuteCommand(command); 186 DownloadCommands(item_).ExecuteCommand(command);
161 187
162 // Shows the notification again after clicking "Keep" on dangerous download. 188 // Shows the notification again after clicking "Keep" on dangerous download.
163 if (command == DownloadCommands::KEEP) 189 if (command == DownloadCommands::KEEP)
164 UpdateNotificationData(ADD_NEW); 190 UpdateNotificationData(ADD_NEW);
165 } 191 }
166 192
193 void DownloadNotificationItem::OnNotificationClose(bool by_user) {
194 if (image_decode_status_ == IN_PROGRESS) {
195 image_decode_status_ = NOT_STARTED;
196 ImageDecoder::Cancel(this);
197 }
198 }
199
167 // DownloadItem::Observer methods 200 // DownloadItem::Observer methods
168 void DownloadNotificationItem::OnDownloadUpdated(content::DownloadItem* item) { 201 void DownloadNotificationItem::OnDownloadUpdated(content::DownloadItem* item) {
169 DCHECK_EQ(item, item_); 202 DCHECK_EQ(item, item_);
170 203
171 UpdateNotificationData(UPDATE_EXISTING); 204 UpdateNotificationData(UPDATE_EXISTING);
172 } 205 }
173 206
174 void DownloadNotificationItem::CloseNotificationByNonUser() { 207 void DownloadNotificationItem::CloseNotificationByNonUser() {
175 const std::string& notification_id = watcher_->id(); 208 const std::string& notification_id = watcher_->id();
176 const ProfileID profile_id = NotificationUIManager::GetProfileID(profile_); 209 const ProfileID profile_id = NotificationUIManager::GetProfileID(profile_);
(...skipping 16 matching lines...) Expand all
193 // because it's by user action. So, we request closing of it directlly to 226 // because it's by user action. So, we request closing of it directlly to
194 // MessageCenter instance. 227 // MessageCenter instance.
195 // Note that: this calling has no side-effect even when the message center 228 // Note that: this calling has no side-effect even when the message center
196 // is not opened. 229 // is not opened.
197 g_browser_process->message_center()->RemoveNotification( 230 g_browser_process->message_center()->RemoveNotification(
198 notification_id_in_message_center, true /* by_user */); 231 notification_id_in_message_center, true /* by_user */);
199 } 232 }
200 233
201 void DownloadNotificationItem::UpdateNotificationData( 234 void DownloadNotificationItem::UpdateNotificationData(
202 NotificationUpdateType type) { 235 NotificationUpdateType type) {
236 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
237
203 DownloadItemModel model(item_); 238 DownloadItemModel model(item_);
204 DownloadCommands command(item_); 239 DownloadCommands command(item_);
205 240
206 if (previous_download_state_ != content::DownloadItem::IN_PROGRESS) { 241 if (previous_download_state_ != content::DownloadItem::IN_PROGRESS) {
207 if (item_->GetState() == content::DownloadItem::IN_PROGRESS) 242 if (item_->GetState() == content::DownloadItem::IN_PROGRESS)
208 delegate_->OnDownloadStarted(this); 243 delegate_->OnDownloadStarted(this);
209 } else { 244 } else {
210 if (item_->GetState() != content::DownloadItem::IN_PROGRESS) 245 if (item_->GetState() != content::DownloadItem::IN_PROGRESS)
211 delegate_->OnDownloadStopped(this); 246 delegate_->OnDownloadStopped(this);
212 } 247 }
213 248
214 if (item_->IsDangerous()) { 249 if (item_->IsDangerous()) {
215 notification_->set_type(message_center::NOTIFICATION_TYPE_BASE_FORMAT); 250 notification_->set_type(message_center::NOTIFICATION_TYPE_BASE_FORMAT);
216 notification_->set_title(GetTitle()); 251 notification_->set_title(GetTitle());
217 notification_->set_message(GetWarningText()); 252 notification_->set_message(GetWarningText());
218 253
219 // Show icon. 254 // Show icon.
220 SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_MALICIOUS); 255 SetNotificationIcon(IDR_DOWNLOAD_NOTIFICATION_MALICIOUS);
221 } else { 256 } else {
222 notification_->set_title(GetTitle()); 257 notification_->set_title(GetTitle());
223 notification_->set_message(model.GetStatusText()); 258 notification_->set_message(model.GetStatusText());
224 259
225 bool is_off_the_record = item_->GetBrowserContext() && 260 bool is_off_the_record = item_->GetBrowserContext() &&
226 item_->GetBrowserContext()->IsOffTheRecord(); 261 item_->GetBrowserContext()->IsOffTheRecord();
227 262
228 switch (item_->GetState()) { 263 switch (item_->GetState()) {
229 case content::DownloadItem::IN_PROGRESS: 264 case content::DownloadItem::IN_PROGRESS:
230 notification_->set_type(message_center::NOTIFICATION_TYPE_PROGRESS); 265 notification_->set_type(message_center::NOTIFICATION_TYPE_PROGRESS);
231 notification_->set_progress(item_->PercentComplete()); 266 notification_->set_progress(item_->PercentComplete());
232 if (is_off_the_record) { 267 if (is_off_the_record) {
233 // TODO(yoshiki): Replace the tentative image. 268 // TODO(yoshiki): Replace the tentative image.
234 SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_INCOGNITO); 269 SetNotificationIcon(IDR_DOWNLOAD_NOTIFICATION_INCOGNITO);
235 } else { 270 } else {
236 SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_DOWNLOADING); 271 SetNotificationIcon(IDR_DOWNLOAD_NOTIFICATION_DOWNLOADING);
237 } 272 }
238 break; 273 break;
239 case content::DownloadItem::COMPLETE: 274 case content::DownloadItem::COMPLETE:
240 DCHECK(item_->IsDone()); 275 DCHECK(item_->IsDone());
241 276
242 // Shows a notifiation as progress type once so the visible content will 277 // Shows a notifiation as progress type once so the visible content will
243 // be updated. 278 // be updated.
244 // Note: only progress-type notification's content will be updated 279 // Note: only progress-type notification's content will be updated
245 // immediately when the message center is visible. 280 // immediately when the message center is visible.
246 notification_->set_type(message_center::NOTIFICATION_TYPE_PROGRESS); 281 notification_->set_type(message_center::NOTIFICATION_TYPE_PROGRESS);
247 notification_->set_progress(100); 282 notification_->set_progress(100);
248 283
249 if (is_off_the_record) { 284 if (is_off_the_record) {
250 // TODO(yoshiki): Replace the tentative image. 285 // TODO(yoshiki): Replace the tentative image.
251 SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_INCOGNITO); 286 SetNotificationIcon(IDR_DOWNLOAD_NOTIFICATION_INCOGNITO);
252 } else { 287 } else {
253 SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_DOWNLOADING); 288 SetNotificationIcon(IDR_DOWNLOAD_NOTIFICATION_DOWNLOADING);
254 } 289 }
255 break; 290 break;
256 case content::DownloadItem::CANCELLED: 291 case content::DownloadItem::CANCELLED:
257 // Confgirms that a download is cancelled by user action. 292 // Confgirms that a download is cancelled by user action.
258 DCHECK(item_->GetLastReason() == 293 DCHECK(item_->GetLastReason() ==
259 content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED || 294 content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED ||
260 item_->GetLastReason() == 295 item_->GetLastReason() ==
261 content::DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN); 296 content::DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN);
262 297
263 CloseNotificationByUser(); 298 CloseNotificationByUser();
264 299
265 previous_download_state_ = item_->GetState(); 300 previous_download_state_ = item_->GetState();
266 return; // Skips the remaining since the notification has closed. 301 return; // Skips the remaining since the notification has closed.
267 case content::DownloadItem::INTERRUPTED: 302 case content::DownloadItem::INTERRUPTED:
268 // Shows a notifiation as progress type once so the visible content will 303 // Shows a notifiation as progress type once so the visible content will
269 // be updated. (same as the case of type = COMPLETE) 304 // be updated. (same as the case of type = COMPLETE)
270 notification_->set_type(message_center::NOTIFICATION_TYPE_PROGRESS); 305 notification_->set_type(message_center::NOTIFICATION_TYPE_PROGRESS);
271 notification_->set_progress(0); 306 notification_->set_progress(0);
272 SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_WARNING); 307 SetNotificationIcon(IDR_DOWNLOAD_NOTIFICATION_WARNING);
273 break; 308 break;
274 case content::DownloadItem::MAX_DOWNLOAD_STATE: // sentinel 309 case content::DownloadItem::MAX_DOWNLOAD_STATE: // sentinel
275 NOTREACHED(); 310 NOTREACHED();
276 } 311 }
277 } 312 }
278 313
279 std::vector<message_center::ButtonInfo> notification_actions; 314 std::vector<message_center::ButtonInfo> notification_actions;
280 scoped_ptr<std::vector<DownloadCommands::Command>> actions( 315 scoped_ptr<std::vector<DownloadCommands::Command>> actions(
281 GetExtraActions().Pass()); 316 GetExtraActions().Pass());
282 317
283 button_actions_.reset(new std::vector<DownloadCommands::Command>); 318 button_actions_.reset(new std::vector<DownloadCommands::Command>);
284 for (auto it = actions->begin(); it != actions->end(); it++) { 319 for (auto it = actions->begin(); it != actions->end(); it++) {
285 button_actions_->push_back(*it); 320 button_actions_->push_back(*it);
286 message_center::ButtonInfo button_info = 321 message_center::ButtonInfo button_info =
287 message_center::ButtonInfo(GetCommandLabel(*it)); 322 message_center::ButtonInfo(GetCommandLabel(*it));
288 button_info.icon = command.GetCommandIcon(*it); 323 button_info.icon = command.GetCommandIcon(*it);
289 notification_actions.push_back(button_info); 324 notification_actions.push_back(button_info);
290 } 325 }
291 notification_->set_buttons(notification_actions); 326 notification_->set_buttons(notification_actions);
292 327
293 if (item_->IsDone()) {
294 // TODO(yoshiki): If the downloaded file is an image, show the thumbnail.
295 }
296
297 if (type == ADD_NEW) { 328 if (type == ADD_NEW) {
298 notification_ui_manager()->Add(*notification_, profile_); 329 notification_ui_manager()->Add(*notification_, profile_);
299 } else if (type == UPDATE_EXISTING) { 330 } else if (type == UPDATE_EXISTING) {
300 notification_ui_manager()->Update(*notification_, profile_); 331 notification_ui_manager()->Update(*notification_, profile_);
301 332
302 // When the download is just completed (or interrupted), close the 333 // When the download is just completed (or interrupted), close the
303 // notification once and re-show it immediately so it'll pop up. 334 // notification once and re-show it immediately so it'll pop up.
304 if ((item_->GetState() == content::DownloadItem::COMPLETE && 335 if ((item_->GetState() == content::DownloadItem::COMPLETE &&
305 previous_download_state_ != content::DownloadItem::COMPLETE) || 336 previous_download_state_ != content::DownloadItem::COMPLETE) ||
306 (item_->GetState() == content::DownloadItem::INTERRUPTED && 337 (item_->GetState() == content::DownloadItem::INTERRUPTED &&
307 previous_download_state_ != content::DownloadItem::INTERRUPTED)) { 338 previous_download_state_ != content::DownloadItem::INTERRUPTED)) {
308 CloseNotificationByNonUser(); 339 CloseNotificationByNonUser();
309 // Changes the type from PROGRESS to BASE_FORMAT. 340 // Changes the type from PROGRESS to BASE_FORMAT.
310 notification_->set_type(message_center::NOTIFICATION_TYPE_BASE_FORMAT); 341 notification_->set_type(message_center::NOTIFICATION_TYPE_BASE_FORMAT);
311 notification_ui_manager()->Add(*notification_, profile_); 342 notification_ui_manager()->Add(*notification_, profile_);
312 } 343 }
313 } else { 344 } else {
314 NOTREACHED(); 345 NOTREACHED();
315 } 346 }
316 347
317 previous_download_state_ = item_->GetState(); 348 previous_download_state_ = item_->GetState();
349
350 if (item_->IsDone() && image_decode_status_ == NOT_STARTED) {
351 // TODO(yoshiki): Add a metrics to take the satistics of image file sizes.
352
353 if (item_->GetReceivedBytes() > kMaxImagePreviewSize)
354 return;
355
356 DCHECK(notification_->image().IsEmpty());
357
358 image_decode_status_ = IN_PROGRESS;
359
360 bool maybe_image = false;
361 if (mime_util::IsSupportedImageMimeType(item_->GetMimeType())) {
362 maybe_image = true;
363 } else {
364 std::string mime;
365 if (net::GetWellKnownMimeTypeFromExtension(
366 item_->GetTargetFilePath().FinalExtension(),
367 &mime) &&
368 mime_util::IsSupportedImageMimeType(mime)) {
369 maybe_image = true;
370 }
371 }
372
373 if (maybe_image) {
374 base::FilePath file_path = item_->GetFullPath();
375 base::PostTaskAndReplyWithResult(
376 content::BrowserThread::GetBlockingPool(),
377 FROM_HERE,
378 base::Bind(&ReadNotificationImage,
379 file_path),
380 base::Bind(&DownloadNotificationItem::OnImageLoaded,
381 weak_factory_.GetWeakPtr()));
382 }
383 }
318 } 384 }
319 385
320 void DownloadNotificationItem::OnDownloadOpened(content::DownloadItem* item) { 386 void DownloadNotificationItem::OnDownloadOpened(content::DownloadItem* item) {
321 DCHECK_EQ(item, item_); 387 DCHECK_EQ(item, item_);
322 // Do nothing. 388 // Do nothing.
323 } 389 }
324 390
325 void DownloadNotificationItem::OnDownloadRemoved(content::DownloadItem* item) { 391 void DownloadNotificationItem::OnDownloadRemoved(content::DownloadItem* item) {
326 DCHECK_EQ(item, item_); 392 DCHECK_EQ(item, item_);
327 393
328 // Removing the notification causes calling |NotificationDelegate::Close()|. 394 // Removing the notification causes calling |NotificationDelegate::Close()|.
329 notification_ui_manager()->CancelById( 395 notification_ui_manager()->CancelById(
330 watcher_->id(), NotificationUIManager::GetProfileID(profile_)); 396 watcher_->id(), NotificationUIManager::GetProfileID(profile_));
331 delegate_->OnDownloadRemoved(this); 397 delegate_->OnDownloadRemoved(this);
332 } 398 }
333 399
334 void DownloadNotificationItem::OnDownloadDestroyed( 400 void DownloadNotificationItem::OnDownloadDestroyed(
335 content::DownloadItem* item) { 401 content::DownloadItem* item) {
336 DCHECK_EQ(item, item_); 402 DCHECK_EQ(item, item_);
337 403
338 item_ = nullptr; 404 item_ = nullptr;
339 } 405 }
340 406
341 void DownloadNotificationItem::SetNotificationImage(int resource_id) { 407 void DownloadNotificationItem::SetNotificationIcon(int resource_id) {
342 if (image_resource_id_ == resource_id) 408 if (image_resource_id_ == resource_id)
343 return; 409 return;
344 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); 410 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
345 image_resource_id_ = resource_id; 411 image_resource_id_ = resource_id;
346 notification_->set_icon(bundle.GetImageNamed(image_resource_id_)); 412 notification_->set_icon(bundle.GetImageNamed(image_resource_id_));
347 } 413 }
348 414
415 void DownloadNotificationItem::OnImageLoaded(const std::string& image_data) {
416 if (image_data.empty())
417 return;
418
419 // TODO(yoshiki): Set option to reduce the image size to supress memory usage.
420 ImageDecoder::Start(this, image_data);
421 }
422
423 void DownloadNotificationItem::OnImageDecoded(const SkBitmap& decoded_image) {
424 gfx::Image image = gfx::Image::CreateFrom1xBitmap(decoded_image);
425 notification_->set_image(image);
426 image_decode_status_ = DONE;
427 UpdateNotificationData(UPDATE_EXISTING);
428 }
429
430 void DownloadNotificationItem::OnDecodeImageFailed() {
431 notification_->set_image(gfx::Image());
432 image_decode_status_ = FAILED;
433 UpdateNotificationData(UPDATE_EXISTING);
434 }
435
349 NotificationUIManager* DownloadNotificationItem::notification_ui_manager() 436 NotificationUIManager* DownloadNotificationItem::notification_ui_manager()
350 const { 437 const {
351 if (stub_notification_ui_manager_for_testing_) { 438 if (stub_notification_ui_manager_for_testing_) {
352 return stub_notification_ui_manager_for_testing_; 439 return stub_notification_ui_manager_for_testing_;
353 } 440 }
354 return g_browser_process->notification_ui_manager(); 441 return g_browser_process->notification_ui_manager();
355 } 442 }
356 443
357 scoped_ptr<std::vector<DownloadCommands::Command>> 444 scoped_ptr<std::vector<DownloadCommands::Command>>
358 DownloadNotificationItem::GetExtraActions() const { 445 DownloadNotificationItem::GetExtraActions() const {
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
497 NOTREACHED(); 584 NOTREACHED();
498 return base::string16(); 585 return base::string16();
499 } 586 }
500 587
501 Browser* DownloadNotificationItem::GetBrowser() { 588 Browser* DownloadNotificationItem::GetBrowser() {
502 chrome::ScopedTabbedBrowserDisplayer browser_displayer( 589 chrome::ScopedTabbedBrowserDisplayer browser_displayer(
503 profile_, chrome::GetActiveDesktop()); 590 profile_, chrome::GetActiveDesktop());
504 DCHECK(browser_displayer.browser()); 591 DCHECK(browser_displayer.browser());
505 return browser_displayer.browser(); 592 return browser_displayer.browser();
506 } 593 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698