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

Unified Diff: chrome/browser/download/download_shelf_context_menu.cc

Issue 11673004: No need to pass DownloadItemModel ownership. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: DownloadShelfContextMenu class cleanup and require GetMenuModel() to return non-NULL Created 7 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/download/download_shelf_context_menu.cc
diff --git a/chrome/browser/download/download_shelf_context_menu.cc b/chrome/browser/download/download_shelf_context_menu.cc
index 3e13650a0082a45a96ec1f1d6439a3b37ea51444..0e70100524a593c6b1b253206ff1c754ce2d5ba2 100644
--- a/chrome/browser/download/download_shelf_context_menu.cc
+++ b/chrome/browser/download/download_shelf_context_menu.cc
@@ -21,23 +21,31 @@
using content::DownloadItem;
using extensions::Extension;
-DownloadShelfContextMenu::~DownloadShelfContextMenu() {}
+DownloadShelfContextMenu::~DownloadShelfContextMenu() {
+ DetachFromDownloadItem();
+}
DownloadShelfContextMenu::DownloadShelfContextMenu(
- DownloadItemModel* download_model,
+ DownloadItem* download_item,
content::PageNavigator* navigator)
- : download_model_(download_model),
- download_item_(download_model->download()),
+ : download_item_(download_item),
navigator_(navigator) {
+ DCHECK(download_item_);
+ download_item_->AddObserver(this);
}
ui::SimpleMenuModel* DownloadShelfContextMenu::GetMenuModel() {
ui::SimpleMenuModel* model = NULL;
+
+ if (!download_item_)
+ return NULL;
+
+ DownloadItemModel download_model(download_item_);
// We shouldn't be opening a context menu for a dangerous download, unless it
// is a malicious download.
- DCHECK(!download_model_->IsDangerous() || download_model_->IsMalicious());
+ DCHECK(!download_model.IsDangerous() || download_model.IsMalicious());
- if (download_model_->IsMalicious())
+ if (download_model.IsMalicious())
model = GetMaliciousMenuModel();
else if (download_item_->IsComplete())
model = GetFinishedMenuModel();
@@ -49,6 +57,9 @@ ui::SimpleMenuModel* DownloadShelfContextMenu::GetMenuModel() {
}
bool DownloadShelfContextMenu::IsCommandIdEnabled(int command_id) const {
+ if (!download_item_)
+ return false;
+
switch (static_cast<ContextMenuCommands>(command_id)) {
case SHOW_IN_FOLDER:
return download_item_->CanShowInFolder() &&
@@ -79,6 +90,9 @@ bool DownloadShelfContextMenu::IsCommandIdEnabled(int command_id) const {
}
bool DownloadShelfContextMenu::IsCommandIdChecked(int command_id) const {
+ if (!download_item_)
+ return false;
+
switch (command_id) {
case OPEN_WHEN_COMPLETE:
return download_item_->GetOpenWhenComplete() ||
@@ -92,6 +106,9 @@ bool DownloadShelfContextMenu::IsCommandIdChecked(int command_id) const {
}
void DownloadShelfContextMenu::ExecuteCommand(int command_id) {
+ if (!download_item_)
+ return;
+
switch (static_cast<ContextMenuCommands>(command_id)) {
case SHOW_IN_FOLDER:
download_item_->ShowDownloadInShell();
@@ -110,7 +127,7 @@ void DownloadShelfContextMenu::ExecuteCommand(int command_id) {
break;
}
case CANCEL:
- download_model_->CancelTask();
+ download_item_->Cancel(true /* Cancelled by user */);
break;
case TOGGLE_PAUSE:
// It is possible for the download to complete before the user clicks the
@@ -169,18 +186,17 @@ string16 DownloadShelfContextMenu::GetLabelForCommandId(int command_id) const {
case SHOW_IN_FOLDER:
return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_SHOW);
case OPEN_WHEN_COMPLETE:
- if (download_item_->IsInProgress())
+ if (download_item_ && download_item_->IsInProgress())
return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE);
return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_OPEN);
case ALWAYS_OPEN_TYPE:
return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE);
case CANCEL:
return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_CANCEL);
- case TOGGLE_PAUSE: {
- if (download_item_->IsPaused())
+ case TOGGLE_PAUSE:
+ if (download_item_ && download_item_->IsPaused())
return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_RESUME_ITEM);
return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_PAUSE_ITEM);
- }
case DISCARD:
return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_DISCARD);
case KEEP:
@@ -195,6 +211,19 @@ string16 DownloadShelfContextMenu::GetLabelForCommandId(int command_id) const {
return string16();
}
+void DownloadShelfContextMenu::DetachFromDownloadItem() {
+ if (!download_item_)
+ return;
+
+ download_item_->RemoveObserver(this);
+ download_item_ = NULL;
+}
+
+void DownloadShelfContextMenu::OnDownloadDestroyed(DownloadItem* download) {
+ DCHECK(download_item_ == download);
+ DetachFromDownloadItem();
+}
+
ui::SimpleMenuModel* DownloadShelfContextMenu::GetInProgressMenuModel() {
if (in_progress_download_menu_model_.get())
return in_progress_download_menu_model_.get();
« no previous file with comments | « chrome/browser/download/download_shelf_context_menu.h ('k') | chrome/browser/download/test_download_shelf.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698