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
3 // found in the LICENSE file.
5 #include "chrome/browser/download/download_shelf_context_menu.h"
7 #include "base/command_line.h"
8 #include "chrome/browser/download/download_item_model.h"
9 #include "chrome/grit/generated_resources.h"
10 #include "content/public/common/content_switches.h"
11 #include "extensions/common/extension.h"
12 #include "ui/base/l10n/l10n_util.h"
15 #include "chrome/browser/ui/pdf/adobe_reader_info_win.h"
18 using content::DownloadItem
;
22 // Returns true if downloads resumption is enabled.
23 bool IsDownloadResumptionEnabled() {
24 return base::CommandLine::ForCurrentProcess()->HasSwitch(
25 switches::kEnableDownloadResumption
);
30 DownloadShelfContextMenu::~DownloadShelfContextMenu() {
31 DetachFromDownloadItem();
34 DownloadShelfContextMenu::DownloadShelfContextMenu(DownloadItem
* download_item
)
35 : download_item_(download_item
),
36 download_commands_(new DownloadCommands(download_item
)) {
37 DCHECK(download_item_
);
38 download_item_
->AddObserver(this);
41 ui::SimpleMenuModel
* DownloadShelfContextMenu::GetMenuModel() {
42 ui::SimpleMenuModel
* model
= NULL
;
47 DownloadItemModel
download_model(download_item_
);
48 // We shouldn't be opening a context menu for a dangerous download, unless it
49 // is a malicious download.
50 DCHECK(!download_model
.IsDangerous() || download_model
.MightBeMalicious());
52 if (download_model
.IsMalicious())
53 model
= GetMaliciousMenuModel();
54 else if (download_model
.MightBeMalicious())
55 model
= GetMaybeMaliciousMenuModel();
56 else if (download_item_
->GetState() == DownloadItem::COMPLETE
)
57 model
= GetFinishedMenuModel();
58 else if (download_item_
->GetState() == DownloadItem::INTERRUPTED
)
59 model
= GetInterruptedMenuModel();
60 else if (download_item_
->IsPaused())
61 model
= GetInProgressPausedMenuModel();
63 model
= GetInProgressMenuModel();
67 bool DownloadShelfContextMenu::IsCommandIdEnabled(int command_id
) const {
68 if (!download_commands_
)
71 return download_commands_
->IsCommandEnabled(
72 static_cast<DownloadCommands::Command
>(command_id
));
75 bool DownloadShelfContextMenu::IsCommandIdChecked(int command_id
) const {
76 if (!download_commands_
)
79 return download_commands_
->IsCommandChecked(
80 static_cast<DownloadCommands::Command
>(command_id
));
83 bool DownloadShelfContextMenu::IsCommandIdVisible(int command_id
) const {
84 if (!download_commands_
)
87 return download_commands_
->IsCommandVisible(
88 static_cast<DownloadCommands::Command
>(command_id
));
91 void DownloadShelfContextMenu::ExecuteCommand(int command_id
, int event_flags
) {
92 if (!download_commands_
)
95 download_commands_
->ExecuteCommand(
96 static_cast<DownloadCommands::Command
>(command_id
));
99 bool DownloadShelfContextMenu::GetAcceleratorForCommandId(
101 ui::Accelerator
* accelerator
) {
105 bool DownloadShelfContextMenu::IsItemForCommandIdDynamic(int command_id
) const {
109 base::string16
DownloadShelfContextMenu::GetLabelForCommandId(
110 int command_id
) const {
113 switch (static_cast<DownloadCommands::Command
>(command_id
)) {
114 case DownloadCommands::OPEN_WHEN_COMPLETE
:
115 if (download_item_
&& !download_item_
->IsDone())
116 id
= IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE
;
118 id
= IDS_DOWNLOAD_MENU_OPEN
;
120 case DownloadCommands::PAUSE
:
121 id
= IDS_DOWNLOAD_MENU_PAUSE_ITEM
;
123 case DownloadCommands::RESUME
:
124 id
= IDS_DOWNLOAD_MENU_RESUME_ITEM
;
126 case DownloadCommands::SHOW_IN_FOLDER
:
127 id
= IDS_DOWNLOAD_MENU_SHOW
;
129 case DownloadCommands::DISCARD
:
130 id
= IDS_DOWNLOAD_MENU_DISCARD
;
132 case DownloadCommands::KEEP
:
133 id
= IDS_DOWNLOAD_MENU_KEEP
;
135 case DownloadCommands::ALWAYS_OPEN_TYPE
: {
136 if (download_commands_
) {
137 bool can_open_pdf_in_system_viewer
=
138 download_commands_
->CanOpenPdfInSystemViewer();
140 if (can_open_pdf_in_system_viewer
) {
141 id
= IsAdobeReaderDefaultPDFViewer()
142 ? IDS_DOWNLOAD_MENU_ALWAYS_OPEN_PDF_IN_READER
143 : IDS_DOWNLOAD_MENU_PLATFORM_OPEN_ALWAYS
;
146 #elif defined(OS_MACOSX) || defined(OS_LINUX)
147 if (can_open_pdf_in_system_viewer
) {
148 id
= IDS_DOWNLOAD_MENU_PLATFORM_OPEN_ALWAYS
;
153 id
= IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE
;
156 case DownloadCommands::PLATFORM_OPEN
:
157 id
= IDS_DOWNLOAD_MENU_PLATFORM_OPEN
;
159 case DownloadCommands::CANCEL
:
160 id
= IDS_DOWNLOAD_MENU_CANCEL
;
162 case DownloadCommands::LEARN_MORE_SCANNING
:
163 id
= IDS_DOWNLOAD_MENU_LEARN_MORE_SCANNING
;
165 case DownloadCommands::LEARN_MORE_INTERRUPTED
:
166 id
= IDS_DOWNLOAD_MENU_LEARN_MORE_INTERRUPTED
;
168 case DownloadCommands::RETRY
:
170 return base::string16();
173 return l10n_util::GetStringUTF16(id
);
176 void DownloadShelfContextMenu::DetachFromDownloadItem() {
180 download_commands_
.reset();
181 download_item_
->RemoveObserver(this);
182 download_item_
= NULL
;
185 void DownloadShelfContextMenu::OnDownloadDestroyed(DownloadItem
* download
) {
186 DCHECK(download_item_
== download
);
187 DetachFromDownloadItem();
190 ui::SimpleMenuModel
* DownloadShelfContextMenu::GetInProgressMenuModel() {
191 if (in_progress_download_menu_model_
)
192 return in_progress_download_menu_model_
.get();
194 in_progress_download_menu_model_
.reset(new ui::SimpleMenuModel(this));
196 in_progress_download_menu_model_
->AddCheckItem(
197 DownloadCommands::OPEN_WHEN_COMPLETE
,
198 GetLabelForCommandId(DownloadCommands::OPEN_WHEN_COMPLETE
));
199 in_progress_download_menu_model_
->AddCheckItem(
200 DownloadCommands::ALWAYS_OPEN_TYPE
,
201 GetLabelForCommandId(DownloadCommands::ALWAYS_OPEN_TYPE
));
202 in_progress_download_menu_model_
->AddSeparator(ui::NORMAL_SEPARATOR
);
203 in_progress_download_menu_model_
->AddItem(
204 DownloadCommands::PAUSE
, GetLabelForCommandId(DownloadCommands::PAUSE
));
205 in_progress_download_menu_model_
->AddItem(
206 DownloadCommands::SHOW_IN_FOLDER
,
207 GetLabelForCommandId(DownloadCommands::SHOW_IN_FOLDER
));
208 in_progress_download_menu_model_
->AddSeparator(ui::NORMAL_SEPARATOR
);
209 in_progress_download_menu_model_
->AddItem(
210 DownloadCommands::CANCEL
, GetLabelForCommandId(DownloadCommands::CANCEL
));
212 return in_progress_download_menu_model_
.get();
215 ui::SimpleMenuModel
* DownloadShelfContextMenu::GetInProgressPausedMenuModel() {
216 if (in_progress_download_paused_menu_model_
)
217 return in_progress_download_paused_menu_model_
.get();
219 in_progress_download_paused_menu_model_
.reset(new ui::SimpleMenuModel(this));
221 in_progress_download_paused_menu_model_
->AddCheckItem(
222 DownloadCommands::OPEN_WHEN_COMPLETE
,
223 GetLabelForCommandId(DownloadCommands::OPEN_WHEN_COMPLETE
));
224 in_progress_download_paused_menu_model_
->AddCheckItem(
225 DownloadCommands::ALWAYS_OPEN_TYPE
,
226 GetLabelForCommandId(DownloadCommands::ALWAYS_OPEN_TYPE
));
227 in_progress_download_paused_menu_model_
->AddSeparator(ui::NORMAL_SEPARATOR
);
228 in_progress_download_paused_menu_model_
->AddItem(
229 DownloadCommands::RESUME
, GetLabelForCommandId(DownloadCommands::RESUME
));
230 in_progress_download_paused_menu_model_
->AddItem(
231 DownloadCommands::SHOW_IN_FOLDER
,
232 GetLabelForCommandId(DownloadCommands::SHOW_IN_FOLDER
));
233 in_progress_download_paused_menu_model_
->AddSeparator(ui::NORMAL_SEPARATOR
);
234 in_progress_download_paused_menu_model_
->AddItem(
235 DownloadCommands::CANCEL
, GetLabelForCommandId(DownloadCommands::CANCEL
));
237 return in_progress_download_paused_menu_model_
.get();
240 ui::SimpleMenuModel
* DownloadShelfContextMenu::GetFinishedMenuModel() {
241 if (finished_download_menu_model_
)
242 return finished_download_menu_model_
.get();
244 finished_download_menu_model_
.reset(new ui::SimpleMenuModel(this));
246 finished_download_menu_model_
->AddItem(
247 DownloadCommands::OPEN_WHEN_COMPLETE
,
248 GetLabelForCommandId(DownloadCommands::OPEN_WHEN_COMPLETE
));
249 finished_download_menu_model_
->AddCheckItem(
250 DownloadCommands::ALWAYS_OPEN_TYPE
,
251 GetLabelForCommandId(DownloadCommands::ALWAYS_OPEN_TYPE
));
252 finished_download_menu_model_
->AddItem(
253 DownloadCommands::PLATFORM_OPEN
,
254 GetLabelForCommandId(DownloadCommands::PLATFORM_OPEN
));
255 finished_download_menu_model_
->AddSeparator(ui::NORMAL_SEPARATOR
);
256 finished_download_menu_model_
->AddItem(
257 DownloadCommands::SHOW_IN_FOLDER
,
258 GetLabelForCommandId(DownloadCommands::SHOW_IN_FOLDER
));
259 finished_download_menu_model_
->AddSeparator(ui::NORMAL_SEPARATOR
);
260 finished_download_menu_model_
->AddItem(
261 DownloadCommands::CANCEL
, GetLabelForCommandId(DownloadCommands::CANCEL
));
263 return finished_download_menu_model_
.get();
266 ui::SimpleMenuModel
* DownloadShelfContextMenu::GetInterruptedMenuModel() {
268 // If resumption isn't enabled and we aren't on Windows, then none of the
269 // options here are applicable.
270 if (!IsDownloadResumptionEnabled())
271 return GetInProgressMenuModel();
274 if (interrupted_download_menu_model_
)
275 return interrupted_download_menu_model_
.get();
277 interrupted_download_menu_model_
.reset(new ui::SimpleMenuModel(this));
279 if (IsDownloadResumptionEnabled()) {
280 interrupted_download_menu_model_
->AddItem(
281 DownloadCommands::RESUME
,
282 GetLabelForCommandId(DownloadCommands::RESUME
));
285 // The Help Center article is currently Windows specific.
286 // TODO(asanka): Enable this for other platforms when the article is expanded
287 // for other platforms.
288 interrupted_download_menu_model_
->AddItem(
289 DownloadCommands::LEARN_MORE_INTERRUPTED
,
290 GetLabelForCommandId(DownloadCommands::LEARN_MORE_INTERRUPTED
));
292 if (IsDownloadResumptionEnabled()) {
293 interrupted_download_menu_model_
->AddSeparator(ui::NORMAL_SEPARATOR
);
294 interrupted_download_menu_model_
->AddItem(
295 DownloadCommands::CANCEL
,
296 GetLabelForCommandId(DownloadCommands::CANCEL
));
299 return interrupted_download_menu_model_
.get();
302 ui::SimpleMenuModel
* DownloadShelfContextMenu::GetMaybeMaliciousMenuModel() {
303 if (maybe_malicious_download_menu_model_
)
304 return maybe_malicious_download_menu_model_
.get();
306 maybe_malicious_download_menu_model_
.reset(new ui::SimpleMenuModel(this));
308 maybe_malicious_download_menu_model_
->AddItem(
309 DownloadCommands::KEEP
, GetLabelForCommandId(DownloadCommands::KEEP
));
310 maybe_malicious_download_menu_model_
->AddSeparator(ui::NORMAL_SEPARATOR
);
311 maybe_malicious_download_menu_model_
->AddItem(
312 DownloadCommands::LEARN_MORE_SCANNING
,
313 GetLabelForCommandId(DownloadCommands::LEARN_MORE_SCANNING
));
314 return maybe_malicious_download_menu_model_
.get();
317 ui::SimpleMenuModel
* DownloadShelfContextMenu::GetMaliciousMenuModel() {
318 if (malicious_download_menu_model_
)
319 return malicious_download_menu_model_
.get();
321 malicious_download_menu_model_
.reset(new ui::SimpleMenuModel(this));
322 malicious_download_menu_model_
->AddItem(
323 DownloadCommands::LEARN_MORE_SCANNING
,
324 GetLabelForCommandId(DownloadCommands::LEARN_MORE_SCANNING
));
326 return malicious_download_menu_model_
.get();