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

Side by Side Diff: chrome/browser/download/notification/download_item_notification.cc

Issue 1389273004: Update first download notification correctly if there are multiple notifications (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 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_item_notification.h" 5 #include "chrome/browser/download/notification/download_item_notification.h"
6 6
7 #include "base/files/file_util.h" 7 #include "base/files/file_util.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/browser_process.h" 9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/download/download_crx_util.h" 10 #include "chrome/browser/download/download_crx_util.h"
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 break; 156 break;
157 } 157 }
158 } 158 }
159 159
160 } // anonymous namespace 160 } // anonymous namespace
161 161
162 DownloadItemNotification::DownloadItemNotification( 162 DownloadItemNotification::DownloadItemNotification(
163 content::DownloadItem* item, 163 content::DownloadItem* item,
164 DownloadNotificationManagerForProfile* manager) 164 DownloadNotificationManagerForProfile* manager)
165 : item_(item), 165 : item_(item),
166 message_center_(manager->message_center()),
166 weak_factory_(this) { 167 weak_factory_(this) {
168
167 // Creates the notification instance. |title|, |body| and |icon| will be 169 // Creates the notification instance. |title|, |body| and |icon| will be
168 // overridden by UpdateNotificationData() below. 170 // overridden by UpdateNotificationData() below.
169 notification_.reset(new Notification( 171 notification_.reset(new Notification(
170 message_center::NOTIFICATION_TYPE_PROGRESS, 172 message_center::NOTIFICATION_TYPE_PROGRESS,
171 base::string16(), // title 173 base::string16(), // title
172 base::string16(), // body 174 base::string16(), // body
173 gfx::Image(), // icon 175 gfx::Image(), // icon
174 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, 176 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT,
175 kDownloadNotificationNotifierId), 177 kDownloadNotificationNotifierId),
176 base::string16(), // display_source 178 base::string16(), // display_source
(...skipping 15 matching lines...) Expand all
192 194
193 bool DownloadItemNotification::HasNotificationClickedListener() { 195 bool DownloadItemNotification::HasNotificationClickedListener() {
194 if (item_->IsDangerous()) { 196 if (item_->IsDangerous()) {
195 // Dangerous notifications don't have a click handler. 197 // Dangerous notifications don't have a click handler.
196 return false; 198 return false;
197 } 199 }
198 return true; 200 return true;
199 } 201 }
200 202
201 void DownloadItemNotification::OnNotificationClose() { 203 void DownloadItemNotification::OnNotificationClose() {
202 visible_ = false;
203
204 if (item_ && item_->IsDangerous() && !item_->IsDone()) { 204 if (item_ && item_->IsDangerous() && !item_->IsDone()) {
205 content::RecordAction( 205 content::RecordAction(
206 UserMetricsAction("DownloadNotification.Close_Dangerous")); 206 UserMetricsAction("DownloadNotification.Close_Dangerous"));
207 item_->Cancel(true /* by_user */); 207 item_->Cancel(true /* by_user */);
208 return; 208 return;
209 } 209 }
210 210
211 if (image_decode_status_ == IN_PROGRESS) { 211 if (image_decode_status_ == IN_PROGRESS) {
212 image_decode_status_ = NOT_STARTED; 212 image_decode_status_ = NOT_STARTED;
213 ImageDecoder::Cancel(this); 213 ImageDecoder::Cancel(this);
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 g_browser_process->notification_ui_manager()-> 307 g_browser_process->notification_ui_manager()->
308 CancelById(notification_id, profile_id); 308 CancelById(notification_id, profile_id);
309 309
310 // When the message center is visible, |NotificationUIManager::CancelByID()| 310 // When the message center is visible, |NotificationUIManager::CancelByID()|
311 // delays the close hence the notification is not closed at this time. But 311 // delays the close hence the notification is not closed at this time. But
312 // from the viewpoint of UX of MessageCenter, we should close it immediately 312 // from the viewpoint of UX of MessageCenter, we should close it immediately
313 // because it's by user action. So, we request closing of it directlly to 313 // because it's by user action. So, we request closing of it directlly to
314 // MessageCenter instance. 314 // MessageCenter instance.
315 // Note that: this calling has no side-effect even when the message center 315 // Note that: this calling has no side-effect even when the message center
316 // is not opened. 316 // is not opened.
317 g_browser_process->message_center()->RemoveNotification( 317 message_center_->RemoveNotification(
318 notification_id_in_message_center, true /* by_user */); 318 notification_id_in_message_center, true /* by_user */);
319 } 319 }
320 320
321 void DownloadItemNotification::Update() { 321 void DownloadItemNotification::Update() {
322 auto download_state = item_->GetState(); 322 auto download_state = item_->GetState();
323 323
324 // When the download is just completed, interrupted or transitions to 324 // When the download is just completed, interrupted or transitions to
325 // dangerous, close the notification once and re-show it immediately so 325 // dangerous, close the notification once and re-show it immediately so
326 // it'll pop up. 326 // it'll pop up.
327 bool popup = 327 bool popup =
328 ((item_->IsDangerous() && !previous_dangerous_state_) || 328 ((item_->IsDangerous() && !previous_dangerous_state_) ||
329 (download_state == content::DownloadItem::COMPLETE && 329 (download_state == content::DownloadItem::COMPLETE &&
330 previous_download_state_ != content::DownloadItem::COMPLETE) || 330 previous_download_state_ != content::DownloadItem::COMPLETE) ||
331 (download_state == content::DownloadItem::INTERRUPTED && 331 (download_state == content::DownloadItem::INTERRUPTED &&
332 previous_download_state_ != content::DownloadItem::INTERRUPTED)); 332 previous_download_state_ != content::DownloadItem::INTERRUPTED));
333 333
334 if (visible_) { 334 if (IsNotificationVisible()) {
yoshiki 2015/10/10 05:31:13 The cause of the issue is that |visible_| is not u
335 UpdateNotificationData(popup ? UPDATE_AND_POPUP : UPDATE); 335 UpdateNotificationData(popup ? UPDATE_AND_POPUP : UPDATE);
336 } else { 336 } else {
337 if (show_next_ || popup) { 337 if (show_next_ || popup)
338 UpdateNotificationData(ADD); 338 UpdateNotificationData(ADD);
339 visible_ = true;
340 }
341 } 339 }
342 340
343 show_next_ = false; 341 show_next_ = false;
344 previous_download_state_ = item_->GetState(); 342 previous_download_state_ = item_->GetState();
345 previous_dangerous_state_ = item_->IsDangerous(); 343 previous_dangerous_state_ = item_->IsDangerous();
346 } 344 }
347 345
348 void DownloadItemNotification::UpdateNotificationData( 346 void DownloadItemNotification::UpdateNotificationData(
349 NotificationUpdateType type) { 347 NotificationUpdateType type) {
350 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 348 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 Add(*notification_, profile()); 420 Add(*notification_, profile());
423 } else if (type == UPDATE || 421 } else if (type == UPDATE ||
424 // If the notification is already visible as popup or in the 422 // If the notification is already visible as popup or in the
425 // notification center, doesn't pop it up. 423 // notification center, doesn't pop it up.
426 (type == UPDATE_AND_POPUP && IsNotificationVisible())) { 424 (type == UPDATE_AND_POPUP && IsNotificationVisible())) {
427 // Shows a notifiation as progress type once so the visible content will be 425 // Shows a notifiation as progress type once so the visible content will be
428 // updated. Only progress-type notification's content will be updated 426 // updated. Only progress-type notification's content will be updated
429 // immediately when the message center is visible. 427 // immediately when the message center is visible.
430 // See the comment in MessageCenterImpl::UpdateNotification() for detail. 428 // See the comment in MessageCenterImpl::UpdateNotification() for detail.
431 if (type == UPDATE_AND_POPUP && 429 if (type == UPDATE_AND_POPUP &&
432 g_browser_process->message_center()->IsMessageCenterVisible() && 430 message_center_->IsMessageCenterVisible() &&
433 (item_->GetState() == content::DownloadItem::COMPLETE || 431 (item_->GetState() == content::DownloadItem::COMPLETE ||
434 item_->GetState() == content::DownloadItem::INTERRUPTED)) { 432 item_->GetState() == content::DownloadItem::INTERRUPTED)) {
435 DCHECK_EQ(notification_->type(), 433 DCHECK_EQ(notification_->type(),
436 message_center::NOTIFICATION_TYPE_BASE_FORMAT); 434 message_center::NOTIFICATION_TYPE_BASE_FORMAT);
437 435
438 notification_->set_type(message_center::NOTIFICATION_TYPE_PROGRESS); 436 notification_->set_type(message_center::NOTIFICATION_TYPE_PROGRESS);
439 g_browser_process->notification_ui_manager()-> 437 g_browser_process->notification_ui_manager()->
440 Update(*notification_, profile()); 438 Update(*notification_, profile());
441 notification_->set_type(message_center::NOTIFICATION_TYPE_BASE_FORMAT); 439 notification_->set_type(message_center::NOTIFICATION_TYPE_BASE_FORMAT);
442 } 440 }
(...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after
894 return browser_displayer.browser(); 892 return browser_displayer.browser();
895 } 893 }
896 894
897 Profile* DownloadItemNotification::profile() const { 895 Profile* DownloadItemNotification::profile() const {
898 return Profile::FromBrowserContext(item_->GetBrowserContext()); 896 return Profile::FromBrowserContext(item_->GetBrowserContext());
899 } 897 }
900 898
901 bool DownloadItemNotification::IsNotificationVisible() const { 899 bool DownloadItemNotification::IsNotificationVisible() const {
902 const std::string& notification_id = watcher()->id(); 900 const std::string& notification_id = watcher()->id();
903 const ProfileID profile_id = NotificationUIManager::GetProfileID(profile()); 901 const ProfileID profile_id = NotificationUIManager::GetProfileID(profile());
904 const std::string notification_id_in_message_center = 902 if (!g_browser_process->notification_ui_manager())
905 ProfileNotification::GetProfileNotificationId(notification_id, 903 return false;
906 profile_id); 904 const Notification* notification = g_browser_process->
905 notification_ui_manager()->FindById(notification_id, profile_id);
yoshiki 2015/10/10 05:31:13 Instead of generating notification-id-in-message-c
906 if (!notification)
907 return false;
908
909 const std::string notification_id_in_message_center = notification->id();
907 910
908 message_center::NotificationList::Notifications visible_notifications = 911 message_center::NotificationList::Notifications visible_notifications =
909 g_browser_process->message_center()->GetVisibleNotifications(); 912 message_center_->GetVisibleNotifications();
910 for (const auto& notification : visible_notifications) { 913 for (const auto& notification : visible_notifications) {
911 if (notification->id() == notification_id_in_message_center) 914 if (notification->id() == notification_id_in_message_center)
912 return true; 915 return true;
913 } 916 }
914 return false; 917 return false;
915 } 918 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698