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

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

Powered by Google App Engine
This is Rietveld 408576698