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

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

Issue 1105953002: Pop up the notification when the download is interrupted or completed (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@dn-xxxx-close-bug
Patch Set: Addressed comments #7 Created 5 years, 7 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
« no previous file with comments | « chrome/browser/download/notification/download_notification_item.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/strings/string_number_conversions.h" 7 #include "base/strings/string_number_conversions.h"
8 #include "chrome/browser/browser_process.h" 8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/download/download_crx_util.h" 9 #include "chrome/browser/download/download_crx_util.h"
10 #include "chrome/browser/download/download_item_model.h" 10 #include "chrome/browser/download/download_item_model.h"
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 UpdateNotificationData(ADD_NEW); 152 UpdateNotificationData(ADD_NEW);
153 } 153 }
154 154
155 // DownloadItem::Observer methods 155 // DownloadItem::Observer methods
156 void DownloadNotificationItem::OnDownloadUpdated(content::DownloadItem* item) { 156 void DownloadNotificationItem::OnDownloadUpdated(content::DownloadItem* item) {
157 DCHECK_EQ(item, item_); 157 DCHECK_EQ(item, item_);
158 158
159 UpdateNotificationData(UPDATE_EXISTING); 159 UpdateNotificationData(UPDATE_EXISTING);
160 } 160 }
161 161
162 void DownloadNotificationItem::CloseNotificationByNonUser() {
163 const std::string& notification_id = watcher_->id();
164 const ProfileID profile_id = NotificationUIManager::GetProfileID(profile_);
165
166 notification_ui_manager()->CancelById(notification_id, profile_id);
167 }
168
162 void DownloadNotificationItem::CloseNotificationByUser() { 169 void DownloadNotificationItem::CloseNotificationByUser() {
163 const std::string& notification_id = watcher_->id(); 170 const std::string& notification_id = watcher_->id();
164 const ProfileID profile_id = NotificationUIManager::GetProfileID(profile_); 171 const ProfileID profile_id = NotificationUIManager::GetProfileID(profile_);
165 const std::string notification_id_in_message_center = 172 const std::string notification_id_in_message_center =
166 ProfileNotification::GetProfileNotificationId(notification_id, 173 ProfileNotification::GetProfileNotificationId(notification_id,
167 profile_id); 174 profile_id);
168 175
169 notification_ui_manager()->CancelById(notification_id, profile_id); 176 notification_ui_manager()->CancelById(notification_id, profile_id);
170 177
171 // When the message center is visible, |NotificationUIManager::CancelByID()| 178 // When the message center is visible, |NotificationUIManager::CancelByID()|
172 // delays the close hence the notification is not closed at this time. But 179 // delays the close hence the notification is not closed at this time. But
173 // from the viewpoint of UX of MessageCenter, we should close it immediately 180 // from the viewpoint of UX of MessageCenter, we should close it immediately
174 // because it's by user action. So, we request closing of it directlly to 181 // because it's by user action. So, we request closing of it directlly to
175 // MessageCenter instance. 182 // MessageCenter instance.
176 // Note that: this calling has no side-effect even when the message center 183 // Note that: this calling has no side-effect even when the message center
177 // is not opened. 184 // is not opened.
178 g_browser_process->message_center()->RemoveNotification( 185 g_browser_process->message_center()->RemoveNotification(
179 notification_id_in_message_center, true /* by_user */); 186 notification_id_in_message_center, true /* by_user */);
180 } 187 }
181 188
182 void DownloadNotificationItem::UpdateNotificationData( 189 void DownloadNotificationItem::UpdateNotificationData(
183 NotificationUpdateType type) { 190 NotificationUpdateType type) {
184 DownloadItemModel model(item_); 191 DownloadItemModel model(item_);
185 DownloadCommands command(item_); 192 DownloadCommands command(item_);
186 193
187 if (!downloading_) { 194 if (previous_download_state_ != content::DownloadItem::IN_PROGRESS) {
188 if (item_->GetState() == content::DownloadItem::IN_PROGRESS) { 195 if (item_->GetState() == content::DownloadItem::IN_PROGRESS)
189 delegate_->OnDownloadStarted(this); 196 delegate_->OnDownloadStarted(this);
190 downloading_ = true;
191 }
192 } else { 197 } else {
193 if (item_->GetState() != content::DownloadItem::IN_PROGRESS) { 198 if (item_->GetState() != content::DownloadItem::IN_PROGRESS)
194 delegate_->OnDownloadStopped(this); 199 delegate_->OnDownloadStopped(this);
195 downloading_ = false;
196 }
197 } 200 }
198 201
199 if (item_->IsDangerous()) { 202 if (item_->IsDangerous()) {
200 notification_->set_type(message_center::NOTIFICATION_TYPE_SIMPLE); 203 notification_->set_type(message_center::NOTIFICATION_TYPE_SIMPLE);
201 notification_->set_title(GetTitle()); 204 notification_->set_title(GetTitle());
202 notification_->set_message(GetWarningText()); 205 notification_->set_message(GetWarningText());
203 206
204 // Show icon. 207 // Show icon.
205 SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_MALICIOUS); 208 SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_MALICIOUS);
206 } else { 209 } else {
207 notification_->set_title(GetTitle()); 210 notification_->set_title(GetTitle());
208 notification_->set_message(model.GetStatusText()); 211 notification_->set_message(model.GetStatusText());
209 212
210 bool is_off_the_record = item_->GetBrowserContext() && 213 bool is_off_the_record = item_->GetBrowserContext() &&
211 item_->GetBrowserContext()->IsOffTheRecord(); 214 item_->GetBrowserContext()->IsOffTheRecord();
212 215
213 switch (item_->GetState()) { 216 switch (item_->GetState()) {
214 case content::DownloadItem::IN_PROGRESS: 217 case content::DownloadItem::IN_PROGRESS:
215 notification_->set_type(message_center::NOTIFICATION_TYPE_PROGRESS); 218 notification_->set_type(message_center::NOTIFICATION_TYPE_PROGRESS);
216 notification_->set_progress(item_->PercentComplete()); 219 notification_->set_progress(item_->PercentComplete());
217 if (is_off_the_record) { 220 if (is_off_the_record) {
218 // TODO(yoshiki): Replace the tentative image. 221 // TODO(yoshiki): Replace the tentative image.
219 SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_INCOGNITO); 222 SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_INCOGNITO);
220 } else { 223 } else {
221 SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_DOWNLOADING); 224 SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_DOWNLOADING);
222 } 225 }
223 break; 226 break;
224 case content::DownloadItem::COMPLETE: 227 case content::DownloadItem::COMPLETE:
225 notification_->set_type(message_center::NOTIFICATION_TYPE_SIMPLE); 228 DCHECK(item_->IsDone());
229
230 // Shows a notifiation as progress type once so the visible content will
231 // be updated.
232 // Note: only progress-type notification's content will be updated
233 // immediately when the message center is visible.
234 notification_->set_type(message_center::NOTIFICATION_TYPE_PROGRESS);
235 notification_->set_progress(100);
236
226 if (is_off_the_record) { 237 if (is_off_the_record) {
227 // TODO(yoshiki): Replace the tentative image. 238 // TODO(yoshiki): Replace the tentative image.
228 SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_INCOGNITO); 239 SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_INCOGNITO);
229 } else { 240 } else {
230 SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_DOWNLOADING); 241 SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_DOWNLOADING);
231 } 242 }
232
233 // TODO(yoshiki): Popup a notification again.
234 break; 243 break;
235 case content::DownloadItem::CANCELLED: 244 case content::DownloadItem::CANCELLED:
236 // Confgirms that a download is cancelled by user action. 245 // Confgirms that a download is cancelled by user action.
237 DCHECK(item_->GetLastReason() == 246 DCHECK(item_->GetLastReason() ==
238 content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED || 247 content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED ||
239 item_->GetLastReason() == 248 item_->GetLastReason() ==
240 content::DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN); 249 content::DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN);
241 250
242 CloseNotificationByUser(); 251 CloseNotificationByUser();
252
253 previous_download_state_ = item_->GetState();
243 return; // Skips the remaining since the notification has closed. 254 return; // Skips the remaining since the notification has closed.
244 case content::DownloadItem::INTERRUPTED: 255 case content::DownloadItem::INTERRUPTED:
245 notification_->set_type(message_center::NOTIFICATION_TYPE_SIMPLE); 256 // Shows a notifiation as progress type once so the visible content will
257 // be updated. (same as the case of type = COMPLETE)
258 notification_->set_type(message_center::NOTIFICATION_TYPE_PROGRESS);
259 notification_->set_progress(0);
246 SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_WARNING); 260 SetNotificationImage(IDR_DOWNLOAD_NOTIFICATION_WARNING);
247
248 // TODO(yoshiki): Popup a notification again.
249 break; 261 break;
250 case content::DownloadItem::MAX_DOWNLOAD_STATE: // sentinel 262 case content::DownloadItem::MAX_DOWNLOAD_STATE: // sentinel
251 NOTREACHED(); 263 NOTREACHED();
252 } 264 }
253 } 265 }
254 266
255 std::vector<message_center::ButtonInfo> notification_actions; 267 std::vector<message_center::ButtonInfo> notification_actions;
256 scoped_ptr<std::vector<DownloadCommands::Command>> actions( 268 scoped_ptr<std::vector<DownloadCommands::Command>> actions(
257 GetPossibleActions().Pass()); 269 GetPossibleActions().Pass());
258 270
259 openable_ = false; 271 openable_ = false;
260 button_actions_.reset(new std::vector<DownloadCommands::Command>); 272 button_actions_.reset(new std::vector<DownloadCommands::Command>);
261 for (auto it = actions->begin(); it != actions->end(); it++) { 273 for (auto it = actions->begin(); it != actions->end(); it++) {
262 if (*it == DownloadCommands::OPEN_WHEN_COMPLETE) { 274 if (*it == DownloadCommands::OPEN_WHEN_COMPLETE) {
263 openable_ = true; 275 openable_ = true;
264 } else { 276 } else {
265 button_actions_->push_back(*it); 277 button_actions_->push_back(*it);
266 message_center::ButtonInfo button_info = 278 message_center::ButtonInfo button_info =
267 message_center::ButtonInfo(GetCommandLabel(*it)); 279 message_center::ButtonInfo(GetCommandLabel(*it));
268 button_info.icon = command.GetCommandIcon(*it); 280 button_info.icon = command.GetCommandIcon(*it);
269 notification_actions.push_back(button_info); 281 notification_actions.push_back(button_info);
270 } 282 }
271 } 283 }
272 notification_->set_buttons(notification_actions); 284 notification_->set_buttons(notification_actions);
273 285
274 if (item_->IsDone()) { 286 if (item_->IsDone()) {
275 // TODO(yoshiki): If the downloaded file is an image, show the thumbnail. 287 // TODO(yoshiki): If the downloaded file is an image, show the thumbnail.
276 } 288 }
277 289
278 if (type == ADD_NEW) 290 if (type == ADD_NEW) {
279 notification_ui_manager()->Add(*notification_, profile_); 291 notification_ui_manager()->Add(*notification_, profile_);
280 else if (type == UPDATE_EXISTING) 292 } else if (type == UPDATE_EXISTING) {
281 notification_ui_manager()->Update(*notification_, profile_); 293 notification_ui_manager()->Update(*notification_, profile_);
282 else 294
295 // When the download is just completed (or interrupted), close the
296 // notification once and re-show it immediately so it'll pop up.
297 if ((item_->GetState() == content::DownloadItem::COMPLETE &&
298 previous_download_state_ != content::DownloadItem::COMPLETE) ||
299 (item_->GetState() == content::DownloadItem::INTERRUPTED &&
300 previous_download_state_ != content::DownloadItem::INTERRUPTED)) {
301 CloseNotificationByNonUser();
302 // Changes the type from PROGRESS to SIMPLE.
303 notification_->set_type(message_center::NOTIFICATION_TYPE_SIMPLE);
304 notification_ui_manager()->Add(*notification_, profile_);
305 }
306 } else {
283 NOTREACHED(); 307 NOTREACHED();
308 }
309
310 previous_download_state_ = item_->GetState();
284 } 311 }
285 312
286 void DownloadNotificationItem::OnDownloadOpened(content::DownloadItem* item) { 313 void DownloadNotificationItem::OnDownloadOpened(content::DownloadItem* item) {
287 DCHECK_EQ(item, item_); 314 DCHECK_EQ(item, item_);
288 // Do nothing. 315 // Do nothing.
289 } 316 }
290 317
291 void DownloadNotificationItem::OnDownloadRemoved(content::DownloadItem* item) { 318 void DownloadNotificationItem::OnDownloadRemoved(content::DownloadItem* item) {
292 DCHECK_EQ(item, item_); 319 DCHECK_EQ(item, item_);
293 320
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 base::string16 file_name = 387 base::string16 file_name =
361 item_->GetFileNameToReportUser().LossyDisplayName(); 388 item_->GetFileNameToReportUser().LossyDisplayName();
362 switch (item_->GetState()) { 389 switch (item_->GetState()) {
363 case content::DownloadItem::IN_PROGRESS: 390 case content::DownloadItem::IN_PROGRESS:
364 title_text = l10n_util::GetStringFUTF16( 391 title_text = l10n_util::GetStringFUTF16(
365 IDS_DOWNLOAD_STATUS_IN_PROGRESS_TITLE, file_name); 392 IDS_DOWNLOAD_STATUS_IN_PROGRESS_TITLE, file_name);
366 break; 393 break;
367 case content::DownloadItem::COMPLETE: 394 case content::DownloadItem::COMPLETE:
368 title_text = l10n_util::GetStringFUTF16( 395 title_text = l10n_util::GetStringFUTF16(
369 IDS_DOWNLOAD_STATUS_DOWNLOADED_TITLE, file_name); 396 IDS_DOWNLOAD_STATUS_DOWNLOADED_TITLE, file_name);
397 break;
370 case content::DownloadItem::INTERRUPTED: 398 case content::DownloadItem::INTERRUPTED:
371 title_text = l10n_util::GetStringFUTF16( 399 title_text = l10n_util::GetStringFUTF16(
372 IDS_DOWNLOAD_STATUS_DOWNLOADED_TITLE, file_name); 400 IDS_DOWNLOAD_STATUS_DOWNLOAD_FAILED_TITLE, file_name);
373 break; 401 break;
374 case content::DownloadItem::CANCELLED: 402 case content::DownloadItem::CANCELLED:
375 title_text = l10n_util::GetStringFUTF16( 403 title_text = l10n_util::GetStringFUTF16(
376 IDS_DOWNLOAD_STATUS_DOWNLOAD_FAILED_TITLE, file_name); 404 IDS_DOWNLOAD_STATUS_DOWNLOAD_FAILED_TITLE, file_name);
377 break; 405 break;
378 case content::DownloadItem::MAX_DOWNLOAD_STATE: 406 case content::DownloadItem::MAX_DOWNLOAD_STATE:
379 NOTREACHED(); 407 NOTREACHED();
380 } 408 }
381 return title_text; 409 return title_text;
382 } 410 }
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 NOTREACHED(); 492 NOTREACHED();
465 return base::string16(); 493 return base::string16();
466 } 494 }
467 495
468 Browser* DownloadNotificationItem::GetBrowser() { 496 Browser* DownloadNotificationItem::GetBrowser() {
469 chrome::ScopedTabbedBrowserDisplayer browser_displayer( 497 chrome::ScopedTabbedBrowserDisplayer browser_displayer(
470 profile_, chrome::GetActiveDesktop()); 498 profile_, chrome::GetActiveDesktop());
471 DCHECK(browser_displayer.browser()); 499 DCHECK(browser_displayer.browser());
472 return browser_displayer.browser(); 500 return browser_displayer.browser();
473 } 501 }
OLDNEW
« no previous file with comments | « chrome/browser/download/notification/download_notification_item.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698