| OLD | NEW |
| 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/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "chrome/browser/browser_process.h" |
| 8 #include "chrome/browser/download/download_crx_util.h" | 9 #include "chrome/browser/download/download_crx_util.h" |
| 9 #include "chrome/browser/download/download_item_model.h" | 10 #include "chrome/browser/download/download_item_model.h" |
| 11 #include "chrome/browser/notifications/notification.h" |
| 12 #include "chrome/browser/notifications/notification_ui_manager.h" |
| 10 #include "chrome/grit/chromium_strings.h" | 13 #include "chrome/grit/chromium_strings.h" |
| 11 #include "chrome/grit/generated_resources.h" | 14 #include "chrome/grit/generated_resources.h" |
| 12 #include "content/public/browser/browser_context.h" | 15 #include "content/public/browser/browser_context.h" |
| 13 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/public/browser/download_item.h" | 17 #include "content/public/browser/download_item.h" |
| 15 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
| 16 #include "grit/theme_resources.h" | 19 #include "grit/theme_resources.h" |
| 17 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
| 18 #include "ui/base/resource/resource_bundle.h" | 21 #include "ui/base/resource/resource_bundle.h" |
| 19 #include "ui/message_center/message_center.h" | 22 #include "ui/message_center/message_center.h" |
| 20 #include "ui/message_center/notification.h" | |
| 21 #include "ui/message_center/notification_delegate.h" | |
| 22 | |
| 23 using message_center::Notification; | |
| 24 | 23 |
| 25 namespace { | 24 namespace { |
| 26 | 25 |
| 27 const char kDownloadNotificationNotifierId[] = | 26 const char kDownloadNotificationNotifierId[] = |
| 28 "chrome://settings/display/notification/id-notifier"; | 27 "chrome://downloads/notification/id-notifier"; |
| 29 const char kDownloadNotificationIdBase[] = | |
| 30 "chrome://settings/display/notification/id-"; | |
| 31 | 28 |
| 32 } // anonymous namespace | 29 } // anonymous namespace |
| 33 | 30 |
| 31 // static |
| 32 const char DownloadNotificationItem::kDownloadNotificationOrigin[] = |
| 33 "chrome://downloads"; |
| 34 |
| 35 // static |
| 36 StubNotificationUIManager* |
| 37 DownloadNotificationItem::stub_notification_ui_manager_for_testing_ = |
| 38 nullptr; |
| 39 |
| 34 DownloadNotificationItem::NotificationWatcher::NotificationWatcher( | 40 DownloadNotificationItem::NotificationWatcher::NotificationWatcher( |
| 35 DownloadNotificationItem* item) | 41 DownloadNotificationItem* item) |
| 36 : item_(item) { | 42 : item_(item) { |
| 37 } | 43 } |
| 38 | 44 |
| 39 DownloadNotificationItem::NotificationWatcher::~NotificationWatcher() { | 45 DownloadNotificationItem::NotificationWatcher::~NotificationWatcher() { |
| 40 } | 46 } |
| 41 | 47 |
| 42 void DownloadNotificationItem::NotificationWatcher::Close(bool by_user) { | 48 void DownloadNotificationItem::NotificationWatcher::Close(bool by_user) { |
| 43 item_->OnNotificationClose(by_user); | 49 // Do nothing. |
| 44 } | 50 } |
| 45 | 51 |
| 46 void DownloadNotificationItem::NotificationWatcher::Click() { | 52 void DownloadNotificationItem::NotificationWatcher::Click() { |
| 47 item_->OnNotificationClick(); | 53 item_->OnNotificationClick(); |
| 48 } | 54 } |
| 49 | 55 |
| 50 bool DownloadNotificationItem::NotificationWatcher::HasClickedListener() { | 56 bool DownloadNotificationItem::NotificationWatcher::HasClickedListener() { |
| 51 return true; | 57 return true; |
| 52 } | 58 } |
| 53 | 59 |
| 54 void DownloadNotificationItem::NotificationWatcher::ButtonClick( | 60 void DownloadNotificationItem::NotificationWatcher::ButtonClick( |
| 55 int button_index) { | 61 int button_index) { |
| 56 item_->OnNotificationButtonClick(button_index); | 62 item_->OnNotificationButtonClick(button_index); |
| 57 } | 63 } |
| 58 | 64 |
| 59 void DownloadNotificationItem::NotificationWatcher::OnNotificationRemoved( | 65 std::string DownloadNotificationItem::NotificationWatcher::id() const { |
| 60 const std::string& id, | 66 return base::UintToString(item_->item_->GetId()); |
| 61 bool by_user) { | |
| 62 if (id != item_->notification_->id()) | |
| 63 return; | |
| 64 item_->OnNotificationRemoved(by_user); | |
| 65 } | 67 } |
| 66 | 68 |
| 67 DownloadNotificationItem::DownloadNotificationItem(content::DownloadItem* item, | 69 DownloadNotificationItem::DownloadNotificationItem(content::DownloadItem* item, |
| 70 Profile* profile, |
| 68 Delegate* delegate) | 71 Delegate* delegate) |
| 69 : openable_(false), | 72 : openable_(false), |
| 70 downloading_(false), | 73 downloading_(false), |
| 71 reshow_after_remove_(false), | |
| 72 image_resource_id_(0), | 74 image_resource_id_(0), |
| 75 profile_(profile), |
| 73 watcher_(new NotificationWatcher(this)), | 76 watcher_(new NotificationWatcher(this)), |
| 74 item_(item), | 77 item_(item), |
| 75 delegate_(delegate) { | 78 delegate_(delegate) { |
| 76 item->AddObserver(this); | 79 item->AddObserver(this); |
| 77 | 80 |
| 78 message_center_ = message_center::MessageCenter::Get(); | |
| 79 message_center_->AddObserver(watcher_.get()); | |
| 80 | |
| 81 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | 81 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); |
| 82 | 82 |
| 83 const base::string16 timeout_message = | |
| 84 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_CRX_INSTALL_RUNNING); | |
| 85 const base::string16 message = | |
| 86 l10n_util::GetStringUTF16(IDS_PROMPT_MALICIOUS_DOWNLOAD_URL); | |
| 87 | |
| 88 std::string id(kDownloadNotificationIdBase); | |
| 89 id += base::UintToString(item_->GetId()); | |
| 90 | |
| 91 message_center::RichNotificationData data; | 83 message_center::RichNotificationData data; |
| 84 // Creates the notification instance. |title| and |body| will be overridden |
| 85 // by UpdateNotificationData() below. |
| 92 notification_.reset(new Notification( | 86 notification_.reset(new Notification( |
| 93 message_center::NOTIFICATION_TYPE_PROGRESS, id, message, timeout_message, | 87 message_center::NOTIFICATION_TYPE_PROGRESS, |
| 88 GURL(kDownloadNotificationOrigin), // origin_url |
| 89 base::string16(), // title |
| 90 base::string16(), // body |
| 94 bundle.GetImageNamed(IDR_DOWNLOAD_NOTIFICATION_DOWNLOADING), | 91 bundle.GetImageNamed(IDR_DOWNLOAD_NOTIFICATION_DOWNLOADING), |
| 95 base::string16() /* display_source */, | |
| 96 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, | 92 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, |
| 97 kDownloadNotificationNotifierId), | 93 kDownloadNotificationNotifierId), |
| 94 base::string16(), // display_source |
| 95 base::UintToString(item_->GetId()), // tag |
| 98 data, watcher_.get())); | 96 data, watcher_.get())); |
| 99 | 97 |
| 100 notification_->set_progress(0); | 98 notification_->set_progress(0); |
| 101 notification_->set_never_timeout(false); | 99 notification_->set_never_timeout(false); |
| 102 | 100 |
| 103 UpdateNotificationData(); | 101 UpdateNotificationData(); |
| 104 | 102 |
| 105 scoped_ptr<Notification> notification(new Notification(*notification_)); | 103 notification_ui_manager()->Add(*notification_, profile); |
| 106 message_center_->AddNotification(notification.Pass()); | |
| 107 } | 104 } |
| 108 | 105 |
| 109 DownloadNotificationItem::~DownloadNotificationItem() { | 106 DownloadNotificationItem::~DownloadNotificationItem() { |
| 110 if (item_) | 107 if (item_) |
| 111 item_->RemoveObserver(this); | 108 item_->RemoveObserver(this); |
| 112 message_center_->RemoveObserver(watcher_.get()); | |
| 113 } | |
| 114 | |
| 115 void DownloadNotificationItem::OnNotificationClose(bool by_user) { | |
| 116 if (item_->GetState() != content::DownloadItem::IN_PROGRESS) { | |
| 117 reshow_after_remove_ = false; | |
| 118 } else { | |
| 119 bool popup = false; | |
| 120 | |
| 121 const std::string id = notification_->id(); | |
| 122 message_center::NotificationList::PopupNotifications popups = | |
| 123 message_center_->GetPopupNotifications(); | |
| 124 for (auto it = popups.begin(); it != popups.end(); it++) { | |
| 125 if ((*it)->id() == id) { | |
| 126 popup = true; | |
| 127 break; | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 // Reshows the notification in the notification center, if the download is | |
| 132 // in progress and the notifitation being closed is a popup. | |
| 133 reshow_after_remove_ = popup; | |
| 134 } | |
| 135 | |
| 136 // OnNotificationRemoved() will be called soon, just after the notification | |
| 137 // is removed. | |
| 138 } | |
| 139 | |
| 140 void DownloadNotificationItem::OnNotificationRemoved(bool by_user) { | |
| 141 if (reshow_after_remove_) { | |
| 142 // Sets the notification as read. | |
| 143 notification_->set_is_read(true); | |
| 144 | |
| 145 // Reshows the notification. | |
| 146 scoped_ptr<Notification> notification(new Notification(*notification_)); | |
| 147 message_center_->AddNotification(notification.Pass()); | |
| 148 // Show the reshown notification as a non-popup. | |
| 149 message_center_->MarkSinglePopupAsShown(notification_->id(), true); | |
| 150 | |
| 151 reshow_after_remove_ = false; | |
| 152 } else { | |
| 153 // Cancels the download. | |
| 154 item_->Cancel(by_user); | |
| 155 delegate_->OnDownloadRemoved(this); | |
| 156 } | |
| 157 } | 109 } |
| 158 | 110 |
| 159 void DownloadNotificationItem::OnNotificationClick() { | 111 void DownloadNotificationItem::OnNotificationClick() { |
| 160 if (openable_) { | 112 if (openable_) { |
| 161 if (item_->IsDone()) | 113 if (item_->IsDone()) |
| 162 item_->OpenDownload(); | 114 item_->OpenDownload(); |
| 163 else | 115 else |
| 164 item_->SetOpenWhenComplete(!item_->GetOpenWhenComplete()); // Toggle | 116 item_->SetOpenWhenComplete(!item_->GetOpenWhenComplete()); // Toggle |
| 165 } | 117 } |
| 166 | 118 |
| 167 if (item_->IsDone()) | 119 if (item_->IsDone()) { |
| 168 message_center_->RemoveNotification(notification_->id(), true); | 120 notification_ui_manager()->CancelById( |
| 121 watcher_->id(), NotificationUIManager::GetProfileID(profile_)); |
| 122 } |
| 169 } | 123 } |
| 170 | 124 |
| 171 void DownloadNotificationItem::OnNotificationButtonClick(int button_index) { | 125 void DownloadNotificationItem::OnNotificationButtonClick(int button_index) { |
| 172 if (button_index < 0 || | 126 if (button_index < 0 || |
| 173 static_cast<size_t>(button_index) >= button_actions_->size()) { | 127 static_cast<size_t>(button_index) >= button_actions_->size()) { |
| 174 // Out of boundary. | 128 // Out of boundary. |
| 175 NOTREACHED(); | 129 NOTREACHED(); |
| 176 return; | 130 return; |
| 177 } | 131 } |
| 178 | 132 |
| 179 DownloadCommands::Command command = button_actions_->at(button_index); | 133 DownloadCommands::Command command = button_actions_->at(button_index); |
| 180 DownloadCommands(item_).ExecuteCommand(command); | 134 DownloadCommands(item_).ExecuteCommand(command); |
| 181 } | 135 } |
| 182 | 136 |
| 183 // DownloadItem::Observer methods | 137 // DownloadItem::Observer methods |
| 184 void DownloadNotificationItem::OnDownloadUpdated(content::DownloadItem* item) { | 138 void DownloadNotificationItem::OnDownloadUpdated(content::DownloadItem* item) { |
| 185 DCHECK_EQ(item, item_); | 139 DCHECK_EQ(item, item_); |
| 186 | 140 |
| 187 UpdateNotificationData(); | 141 UpdateNotificationData(); |
| 188 | 142 |
| 189 // Updates notification. | 143 // Updates notification. |
| 190 scoped_ptr<Notification> notification(new Notification(*notification_)); | 144 notification_ui_manager()->Update(*notification_, profile_); |
| 191 std::string id = notification->id(); | |
| 192 message_center_->UpdateNotification(id, notification.Pass()); | |
| 193 } | 145 } |
| 194 | 146 |
| 195 void DownloadNotificationItem::UpdateNotificationData() { | 147 void DownloadNotificationItem::UpdateNotificationData() { |
| 196 DownloadItemModel model(item_); | 148 DownloadItemModel model(item_); |
| 197 DownloadCommands command(item_); | 149 DownloadCommands command(item_); |
| 198 | 150 |
| 199 if (!downloading_) { | 151 if (!downloading_) { |
| 200 if (item_->GetState() == content::DownloadItem::IN_PROGRESS) { | 152 if (item_->GetState() == content::DownloadItem::IN_PROGRESS) { |
| 201 delegate_->OnDownloadStarted(this); | 153 delegate_->OnDownloadStarted(this); |
| 202 downloading_ = true; | 154 downloading_ = true; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 } | 236 } |
| 285 | 237 |
| 286 void DownloadNotificationItem::OnDownloadOpened(content::DownloadItem* item) { | 238 void DownloadNotificationItem::OnDownloadOpened(content::DownloadItem* item) { |
| 287 DCHECK_EQ(item, item_); | 239 DCHECK_EQ(item, item_); |
| 288 // Do nothing. | 240 // Do nothing. |
| 289 } | 241 } |
| 290 | 242 |
| 291 void DownloadNotificationItem::OnDownloadRemoved(content::DownloadItem* item) { | 243 void DownloadNotificationItem::OnDownloadRemoved(content::DownloadItem* item) { |
| 292 DCHECK_EQ(item, item_); | 244 DCHECK_EQ(item, item_); |
| 293 | 245 |
| 294 // Removing the notification causes calling both |OnNotificationClose()| and | 246 // Removing the notification causes calling |NotificationDelegate::Close()|. |
| 295 // |OnNotificationRemoved()|. | 247 notification_ui_manager()->CancelById( |
| 296 message_center_->RemoveNotification(notification_->id(), false); | 248 watcher_->id(), NotificationUIManager::GetProfileID(profile_)); |
| 249 delegate_->OnDownloadRemoved(this); |
| 297 } | 250 } |
| 298 | 251 |
| 299 void DownloadNotificationItem::OnDownloadDestroyed( | 252 void DownloadNotificationItem::OnDownloadDestroyed( |
| 300 content::DownloadItem* item) { | 253 content::DownloadItem* item) { |
| 301 DCHECK_EQ(item, item_); | 254 DCHECK_EQ(item, item_); |
| 302 | 255 |
| 303 item_ = nullptr; | 256 item_ = nullptr; |
| 304 } | 257 } |
| 305 | 258 |
| 306 void DownloadNotificationItem::SetNotificationImage(int resource_id) { | 259 void DownloadNotificationItem::SetNotificationImage(int resource_id) { |
| 307 if (image_resource_id_ == resource_id) | 260 if (image_resource_id_ == resource_id) |
| 308 return; | 261 return; |
| 309 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | 262 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); |
| 310 image_resource_id_ = resource_id; | 263 image_resource_id_ = resource_id; |
| 311 notification_->set_icon(bundle.GetImageNamed(image_resource_id_)); | 264 notification_->set_icon(bundle.GetImageNamed(image_resource_id_)); |
| 312 } | 265 } |
| 313 | 266 |
| 267 NotificationUIManager* DownloadNotificationItem::notification_ui_manager() |
| 268 const { |
| 269 if (stub_notification_ui_manager_for_testing_) { |
| 270 return stub_notification_ui_manager_for_testing_; |
| 271 } |
| 272 return g_browser_process->notification_ui_manager(); |
| 273 } |
| 274 |
| 314 scoped_ptr<std::vector<DownloadCommands::Command>> | 275 scoped_ptr<std::vector<DownloadCommands::Command>> |
| 315 DownloadNotificationItem::GetPossibleActions() const { | 276 DownloadNotificationItem::GetPossibleActions() const { |
| 316 scoped_ptr<std::vector<DownloadCommands::Command>> actions( | 277 scoped_ptr<std::vector<DownloadCommands::Command>> actions( |
| 317 new std::vector<DownloadCommands::Command>()); | 278 new std::vector<DownloadCommands::Command>()); |
| 318 | 279 |
| 319 if (item_->IsDangerous()) { | 280 if (item_->IsDangerous()) { |
| 320 actions->push_back(DownloadCommands::DISCARD); | 281 actions->push_back(DownloadCommands::DISCARD); |
| 321 actions->push_back(DownloadCommands::KEEP); | 282 actions->push_back(DownloadCommands::KEEP); |
| 322 return actions.Pass(); | 283 return actions.Pass(); |
| 323 } | 284 } |
| 324 | 285 |
| 325 switch (item_->GetState()) { | 286 switch (item_->GetState()) { |
| 326 case content::DownloadItem::IN_PROGRESS: | 287 case content::DownloadItem::IN_PROGRESS: |
| 327 actions->push_back(DownloadCommands::OPEN_WHEN_COMPLETE); | 288 actions->push_back(DownloadCommands::OPEN_WHEN_COMPLETE); |
| 328 if (!item_->IsPaused()) | 289 if (!item_->IsPaused()) |
| 329 actions->push_back(DownloadCommands::PAUSE); | 290 actions->push_back(DownloadCommands::PAUSE); |
| 330 else | 291 else |
| 331 actions->push_back(DownloadCommands::RESUME); | 292 actions->push_back(DownloadCommands::RESUME); |
| 293 actions->push_back(DownloadCommands::CANCEL); |
| 332 break; | 294 break; |
| 333 case content::DownloadItem::CANCELLED: | 295 case content::DownloadItem::CANCELLED: |
| 334 case content::DownloadItem::INTERRUPTED: | 296 case content::DownloadItem::INTERRUPTED: |
| 335 actions->push_back(DownloadCommands::RETRY); | 297 actions->push_back(DownloadCommands::RETRY); |
| 336 break; | 298 break; |
| 337 case content::DownloadItem::COMPLETE: | 299 case content::DownloadItem::COMPLETE: |
| 338 actions->push_back(DownloadCommands::OPEN_WHEN_COMPLETE); | 300 actions->push_back(DownloadCommands::OPEN_WHEN_COMPLETE); |
| 339 actions->push_back(DownloadCommands::SHOW_IN_FOLDER); | 301 actions->push_back(DownloadCommands::SHOW_IN_FOLDER); |
| 340 break; | 302 break; |
| 341 case content::DownloadItem::MAX_DOWNLOAD_STATE: | 303 case content::DownloadItem::MAX_DOWNLOAD_STATE: |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 case DownloadCommands::RETRY: | 356 case DownloadCommands::RETRY: |
| 395 // Only for non menu. | 357 // Only for non menu. |
| 396 id = IDS_DOWNLOAD_LINK_RETRY; | 358 id = IDS_DOWNLOAD_LINK_RETRY; |
| 397 break; | 359 break; |
| 398 case DownloadCommands::DISCARD: | 360 case DownloadCommands::DISCARD: |
| 399 id = IDS_DISCARD_DOWNLOAD; | 361 id = IDS_DISCARD_DOWNLOAD; |
| 400 break; | 362 break; |
| 401 case DownloadCommands::KEEP: | 363 case DownloadCommands::KEEP: |
| 402 id = IDS_CONFIRM_DOWNLOAD; | 364 id = IDS_CONFIRM_DOWNLOAD; |
| 403 break; | 365 break; |
| 366 case DownloadCommands::CANCEL: |
| 367 id = IDS_DOWNLOAD_LINK_CANCEL; |
| 368 break; |
| 404 case DownloadCommands::ALWAYS_OPEN_TYPE: | 369 case DownloadCommands::ALWAYS_OPEN_TYPE: |
| 405 case DownloadCommands::PLATFORM_OPEN: | 370 case DownloadCommands::PLATFORM_OPEN: |
| 406 case DownloadCommands::CANCEL: | |
| 407 case DownloadCommands::LEARN_MORE_SCANNING: | 371 case DownloadCommands::LEARN_MORE_SCANNING: |
| 408 case DownloadCommands::LEARN_MORE_INTERRUPTED: | 372 case DownloadCommands::LEARN_MORE_INTERRUPTED: |
| 409 // Only for menu. | 373 // Only for menu. |
| 410 NOTREACHED(); | 374 NOTREACHED(); |
| 411 return base::string16(); | 375 return base::string16(); |
| 412 } | 376 } |
| 413 CHECK(id != -1); | 377 CHECK(id != -1); |
| 414 return l10n_util::GetStringUTF16(id); | 378 return l10n_util::GetStringUTF16(id); |
| 415 } | 379 } |
| 416 | 380 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 case content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS: | 412 case content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS: |
| 449 case content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT: | 413 case content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT: |
| 450 case content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED: | 414 case content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED: |
| 451 case content::DOWNLOAD_DANGER_TYPE_MAX: { | 415 case content::DOWNLOAD_DANGER_TYPE_MAX: { |
| 452 break; | 416 break; |
| 453 } | 417 } |
| 454 } | 418 } |
| 455 NOTREACHED(); | 419 NOTREACHED(); |
| 456 return base::string16(); | 420 return base::string16(); |
| 457 } | 421 } |
| OLD | NEW |