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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/download_shelf_context_menu.h" 5 #include "chrome/browser/download/download_shelf_context_menu.h"
6 6
7 #include "chrome/browser/browser_process.h" 7 #include "chrome/browser/browser_process.h"
8 #include "chrome/browser/download/download_crx_util.h" 8 #include "chrome/browser/download/download_crx_util.h"
9 #include "chrome/browser/download/download_item_model.h" 9 #include "chrome/browser/download/download_item_model.h"
10 #include "chrome/browser/download/download_prefs.h" 10 #include "chrome/browser/download/download_prefs.h"
11 #include "chrome/browser/safe_browsing/download_protection_service.h" 11 #include "chrome/browser/safe_browsing/download_protection_service.h"
12 #include "chrome/browser/safe_browsing/safe_browsing_service.h" 12 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
13 #include "chrome/common/extensions/extension.h" 13 #include "chrome/common/extensions/extension.h"
14 #include "chrome/common/url_constants.h" 14 #include "chrome/common/url_constants.h"
15 #include "content/public/browser/download_item.h" 15 #include "content/public/browser/download_item.h"
16 #include "content/public/browser/download_manager.h" 16 #include "content/public/browser/download_manager.h"
17 #include "content/public/browser/page_navigator.h" 17 #include "content/public/browser/page_navigator.h"
18 #include "grit/generated_resources.h" 18 #include "grit/generated_resources.h"
19 #include "ui/base/l10n/l10n_util.h" 19 #include "ui/base/l10n/l10n_util.h"
20 20
21 using content::DownloadItem; 21 using content::DownloadItem;
22 using extensions::Extension; 22 using extensions::Extension;
23 23
24 DownloadShelfContextMenu::~DownloadShelfContextMenu() {} 24 DownloadShelfContextMenu::~DownloadShelfContextMenu() {
25 DetachFromDownloadItem();
26 }
25 27
26 DownloadShelfContextMenu::DownloadShelfContextMenu( 28 DownloadShelfContextMenu::DownloadShelfContextMenu(
27 DownloadItemModel* download_model, 29 DownloadItem* download_item,
28 content::PageNavigator* navigator) 30 content::PageNavigator* navigator)
29 : download_model_(download_model), 31 : download_item_(download_item),
30 download_item_(download_model->download()),
31 navigator_(navigator) { 32 navigator_(navigator) {
33 DCHECK(download_item_);
34 download_item_->AddObserver(this);
32 } 35 }
33 36
34 ui::SimpleMenuModel* DownloadShelfContextMenu::GetMenuModel() { 37 ui::SimpleMenuModel* DownloadShelfContextMenu::GetMenuModel() {
35 ui::SimpleMenuModel* model = NULL; 38 ui::SimpleMenuModel* model = NULL;
39
40 if (!download_item_)
41 return NULL;
42
43 DownloadItemModel download_model(download_item_);
36 // We shouldn't be opening a context menu for a dangerous download, unless it 44 // We shouldn't be opening a context menu for a dangerous download, unless it
37 // is a malicious download. 45 // is a malicious download.
38 DCHECK(!download_model_->IsDangerous() || download_model_->IsMalicious()); 46 DCHECK(!download_model.IsDangerous() || download_model.IsMalicious());
39 47
40 if (download_model_->IsMalicious()) 48 if (download_model.IsMalicious())
41 model = GetMaliciousMenuModel(); 49 model = GetMaliciousMenuModel();
42 else if (download_item_->IsComplete()) 50 else if (download_item_->IsComplete())
43 model = GetFinishedMenuModel(); 51 model = GetFinishedMenuModel();
44 else if (download_item_->IsInterrupted()) 52 else if (download_item_->IsInterrupted())
45 model = GetInterruptedMenuModel(); 53 model = GetInterruptedMenuModel();
46 else 54 else
47 model = GetInProgressMenuModel(); 55 model = GetInProgressMenuModel();
48 return model; 56 return model;
49 } 57 }
50 58
51 bool DownloadShelfContextMenu::IsCommandIdEnabled(int command_id) const { 59 bool DownloadShelfContextMenu::IsCommandIdEnabled(int command_id) const {
60 if (!download_item_)
61 return false;
62
52 switch (static_cast<ContextMenuCommands>(command_id)) { 63 switch (static_cast<ContextMenuCommands>(command_id)) {
53 case SHOW_IN_FOLDER: 64 case SHOW_IN_FOLDER:
54 return download_item_->CanShowInFolder() && 65 return download_item_->CanShowInFolder() &&
55 !download_item_->IsTemporary(); 66 !download_item_->IsTemporary();
56 case OPEN_WHEN_COMPLETE: 67 case OPEN_WHEN_COMPLETE:
57 return download_item_->CanShowInFolder() && 68 return download_item_->CanShowInFolder() &&
58 !download_item_->IsTemporary() && 69 !download_item_->IsTemporary() &&
59 (!download_crx_util::IsExtensionDownload(*download_item_) || 70 (!download_crx_util::IsExtensionDownload(*download_item_) ||
60 download_item_->IsComplete()); 71 download_item_->IsComplete());
61 case ALWAYS_OPEN_TYPE: 72 case ALWAYS_OPEN_TYPE:
(...skipping 10 matching lines...) Expand all
72 case DISCARD: 83 case DISCARD:
73 case KEEP: 84 case KEEP:
74 case LEARN_MORE_SCANNING: 85 case LEARN_MORE_SCANNING:
75 case LEARN_MORE_INTERRUPTED: 86 case LEARN_MORE_INTERRUPTED:
76 return true; 87 return true;
77 } 88 }
78 return false; 89 return false;
79 } 90 }
80 91
81 bool DownloadShelfContextMenu::IsCommandIdChecked(int command_id) const { 92 bool DownloadShelfContextMenu::IsCommandIdChecked(int command_id) const {
93 if (!download_item_)
94 return false;
95
82 switch (command_id) { 96 switch (command_id) {
83 case OPEN_WHEN_COMPLETE: 97 case OPEN_WHEN_COMPLETE:
84 return download_item_->GetOpenWhenComplete() || 98 return download_item_->GetOpenWhenComplete() ||
85 download_crx_util::IsExtensionDownload(*download_item_); 99 download_crx_util::IsExtensionDownload(*download_item_);
86 case ALWAYS_OPEN_TYPE: 100 case ALWAYS_OPEN_TYPE:
87 return download_item_->ShouldOpenFileBasedOnExtension(); 101 return download_item_->ShouldOpenFileBasedOnExtension();
88 case TOGGLE_PAUSE: 102 case TOGGLE_PAUSE:
89 return download_item_->IsPaused(); 103 return download_item_->IsPaused();
90 } 104 }
91 return false; 105 return false;
92 } 106 }
93 107
94 void DownloadShelfContextMenu::ExecuteCommand(int command_id) { 108 void DownloadShelfContextMenu::ExecuteCommand(int command_id) {
109 if (!download_item_)
110 return;
111
95 switch (static_cast<ContextMenuCommands>(command_id)) { 112 switch (static_cast<ContextMenuCommands>(command_id)) {
96 case SHOW_IN_FOLDER: 113 case SHOW_IN_FOLDER:
97 download_item_->ShowDownloadInShell(); 114 download_item_->ShowDownloadInShell();
98 break; 115 break;
99 case OPEN_WHEN_COMPLETE: 116 case OPEN_WHEN_COMPLETE:
100 download_item_->OpenDownload(); 117 download_item_->OpenDownload();
101 break; 118 break;
102 case ALWAYS_OPEN_TYPE: { 119 case ALWAYS_OPEN_TYPE: {
103 DownloadPrefs* prefs = DownloadPrefs::FromBrowserContext( 120 DownloadPrefs* prefs = DownloadPrefs::FromBrowserContext(
104 download_item_->GetBrowserContext()); 121 download_item_->GetBrowserContext());
105 FilePath path = download_item_->GetUserVerifiedFilePath(); 122 FilePath path = download_item_->GetUserVerifiedFilePath();
106 if (!IsCommandIdChecked(ALWAYS_OPEN_TYPE)) 123 if (!IsCommandIdChecked(ALWAYS_OPEN_TYPE))
107 prefs->EnableAutoOpenBasedOnExtension(path); 124 prefs->EnableAutoOpenBasedOnExtension(path);
108 else 125 else
109 prefs->DisableAutoOpenBasedOnExtension(path); 126 prefs->DisableAutoOpenBasedOnExtension(path);
110 break; 127 break;
111 } 128 }
112 case CANCEL: 129 case CANCEL:
113 download_model_->CancelTask(); 130 download_item_->Cancel(true /* Cancelled by user */);
114 break; 131 break;
115 case TOGGLE_PAUSE: 132 case TOGGLE_PAUSE:
116 // It is possible for the download to complete before the user clicks the 133 // It is possible for the download to complete before the user clicks the
117 // menu item, recheck if the download is in progress state before toggling 134 // menu item, recheck if the download is in progress state before toggling
118 // pause. 135 // pause.
119 if (download_item_->IsPartialDownload()) 136 if (download_item_->IsPartialDownload())
120 download_item_->TogglePause(); 137 download_item_->TogglePause();
121 break; 138 break;
122 case DISCARD: 139 case DISCARD:
123 download_item_->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); 140 download_item_->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 179
163 bool DownloadShelfContextMenu::IsItemForCommandIdDynamic(int command_id) const { 180 bool DownloadShelfContextMenu::IsItemForCommandIdDynamic(int command_id) const {
164 return command_id == TOGGLE_PAUSE; 181 return command_id == TOGGLE_PAUSE;
165 } 182 }
166 183
167 string16 DownloadShelfContextMenu::GetLabelForCommandId(int command_id) const { 184 string16 DownloadShelfContextMenu::GetLabelForCommandId(int command_id) const {
168 switch (static_cast<ContextMenuCommands>(command_id)) { 185 switch (static_cast<ContextMenuCommands>(command_id)) {
169 case SHOW_IN_FOLDER: 186 case SHOW_IN_FOLDER:
170 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_SHOW); 187 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_SHOW);
171 case OPEN_WHEN_COMPLETE: 188 case OPEN_WHEN_COMPLETE:
172 if (download_item_->IsInProgress()) 189 if (download_item_ && download_item_->IsInProgress())
173 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE); 190 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE);
174 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_OPEN); 191 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_OPEN);
175 case ALWAYS_OPEN_TYPE: 192 case ALWAYS_OPEN_TYPE:
176 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE); 193 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE);
177 case CANCEL: 194 case CANCEL:
178 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_CANCEL); 195 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_CANCEL);
179 case TOGGLE_PAUSE: { 196 case TOGGLE_PAUSE:
180 if (download_item_->IsPaused()) 197 if (download_item_ && download_item_->IsPaused())
181 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_RESUME_ITEM); 198 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_RESUME_ITEM);
182 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_PAUSE_ITEM); 199 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_PAUSE_ITEM);
183 }
184 case DISCARD: 200 case DISCARD:
185 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_DISCARD); 201 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_DISCARD);
186 case KEEP: 202 case KEEP:
187 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_KEEP); 203 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_KEEP);
188 case LEARN_MORE_SCANNING: 204 case LEARN_MORE_SCANNING:
189 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_LEARN_MORE_SCANNING); 205 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_LEARN_MORE_SCANNING);
190 case LEARN_MORE_INTERRUPTED: 206 case LEARN_MORE_INTERRUPTED:
191 return l10n_util::GetStringUTF16( 207 return l10n_util::GetStringUTF16(
192 IDS_DOWNLOAD_MENU_LEARN_MORE_INTERRUPTED); 208 IDS_DOWNLOAD_MENU_LEARN_MORE_INTERRUPTED);
193 } 209 }
194 NOTREACHED(); 210 NOTREACHED();
195 return string16(); 211 return string16();
196 } 212 }
197 213
214 void DownloadShelfContextMenu::DetachFromDownloadItem() {
215 if (!download_item_)
216 return;
217
218 download_item_->RemoveObserver(this);
219 download_item_ = NULL;
220 }
221
222 void DownloadShelfContextMenu::OnDownloadDestroyed(DownloadItem* download) {
223 DCHECK(download_item_ == download);
224 DetachFromDownloadItem();
225 }
226
198 ui::SimpleMenuModel* DownloadShelfContextMenu::GetInProgressMenuModel() { 227 ui::SimpleMenuModel* DownloadShelfContextMenu::GetInProgressMenuModel() {
199 if (in_progress_download_menu_model_.get()) 228 if (in_progress_download_menu_model_.get())
200 return in_progress_download_menu_model_.get(); 229 return in_progress_download_menu_model_.get();
201 230
202 in_progress_download_menu_model_.reset(new ui::SimpleMenuModel(this)); 231 in_progress_download_menu_model_.reset(new ui::SimpleMenuModel(this));
203 232
204 in_progress_download_menu_model_->AddCheckItemWithStringId( 233 in_progress_download_menu_model_->AddCheckItemWithStringId(
205 OPEN_WHEN_COMPLETE, IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE); 234 OPEN_WHEN_COMPLETE, IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE);
206 in_progress_download_menu_model_->AddCheckItemWithStringId( 235 in_progress_download_menu_model_->AddCheckItemWithStringId(
207 ALWAYS_OPEN_TYPE, IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE); 236 ALWAYS_OPEN_TYPE, IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE);
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 malicious_download_menu_model_->AddItemWithStringId( 294 malicious_download_menu_model_->AddItemWithStringId(
266 DISCARD, IDS_DOWNLOAD_MENU_DISCARD); 295 DISCARD, IDS_DOWNLOAD_MENU_DISCARD);
267 malicious_download_menu_model_->AddItemWithStringId( 296 malicious_download_menu_model_->AddItemWithStringId(
268 KEEP, IDS_DOWNLOAD_MENU_KEEP); 297 KEEP, IDS_DOWNLOAD_MENU_KEEP);
269 malicious_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); 298 malicious_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR);
270 malicious_download_menu_model_->AddItemWithStringId( 299 malicious_download_menu_model_->AddItemWithStringId(
271 LEARN_MORE_SCANNING, IDS_DOWNLOAD_MENU_LEARN_MORE_SCANNING); 300 LEARN_MORE_SCANNING, IDS_DOWNLOAD_MENU_LEARN_MORE_SCANNING);
272 301
273 return malicious_download_menu_model_.get(); 302 return malicious_download_menu_model_.get();
274 } 303 }
OLDNEW
« 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