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

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

Powered by Google App Engine
This is Rietveld 408576698