BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / download / download_shelf_context_menu.cc
blobab4836219d39ae0f4c13b692c9fe8ee0edeb0734
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"
14 #if defined(OS_WIN)
15 #include "chrome/browser/ui/pdf/adobe_reader_info_win.h"
16 #endif
18 using content::DownloadItem;
20 namespace {
22 // Returns true if downloads resumption is enabled.
23 bool IsDownloadResumptionEnabled() {
24 return base::CommandLine::ForCurrentProcess()->HasSwitch(
25 switches::kEnableDownloadResumption);
28 } // namespace
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;
44 if (!download_item_)
45 return 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();
62 else
63 model = GetInProgressMenuModel();
64 return model;
67 bool DownloadShelfContextMenu::IsCommandIdEnabled(int command_id) const {
68 if (!download_commands_)
69 return false;
71 return download_commands_->IsCommandEnabled(
72 static_cast<DownloadCommands::Command>(command_id));
75 bool DownloadShelfContextMenu::IsCommandIdChecked(int command_id) const {
76 if (!download_commands_)
77 return false;
79 return download_commands_->IsCommandChecked(
80 static_cast<DownloadCommands::Command>(command_id));
83 bool DownloadShelfContextMenu::IsCommandIdVisible(int command_id) const {
84 if (!download_commands_)
85 return false;
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_)
93 return;
95 download_commands_->ExecuteCommand(
96 static_cast<DownloadCommands::Command>(command_id));
99 bool DownloadShelfContextMenu::GetAcceleratorForCommandId(
100 int command_id,
101 ui::Accelerator* accelerator) {
102 return false;
105 bool DownloadShelfContextMenu::IsItemForCommandIdDynamic(int command_id) const {
106 return false;
109 base::string16 DownloadShelfContextMenu::GetLabelForCommandId(
110 int command_id) const {
111 int id = -1;
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;
117 else
118 id = IDS_DOWNLOAD_MENU_OPEN;
119 break;
120 case DownloadCommands::PAUSE:
121 id = IDS_DOWNLOAD_MENU_PAUSE_ITEM;
122 break;
123 case DownloadCommands::RESUME:
124 id = IDS_DOWNLOAD_MENU_RESUME_ITEM;
125 break;
126 case DownloadCommands::SHOW_IN_FOLDER:
127 id = IDS_DOWNLOAD_MENU_SHOW;
128 break;
129 case DownloadCommands::DISCARD:
130 id = IDS_DOWNLOAD_MENU_DISCARD;
131 break;
132 case DownloadCommands::KEEP:
133 id = IDS_DOWNLOAD_MENU_KEEP;
134 break;
135 case DownloadCommands::ALWAYS_OPEN_TYPE: {
136 if (download_commands_) {
137 bool can_open_pdf_in_system_viewer =
138 download_commands_->CanOpenPdfInSystemViewer();
139 #if defined(OS_WIN)
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;
144 break;
146 #elif defined(OS_MACOSX) || defined(OS_LINUX)
147 if (can_open_pdf_in_system_viewer) {
148 id = IDS_DOWNLOAD_MENU_PLATFORM_OPEN_ALWAYS;
149 break;
151 #endif
153 id = IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE;
154 break;
156 case DownloadCommands::PLATFORM_OPEN:
157 id = IDS_DOWNLOAD_MENU_PLATFORM_OPEN;
158 break;
159 case DownloadCommands::CANCEL:
160 id = IDS_DOWNLOAD_MENU_CANCEL;
161 break;
162 case DownloadCommands::LEARN_MORE_SCANNING:
163 id = IDS_DOWNLOAD_MENU_LEARN_MORE_SCANNING;
164 break;
165 case DownloadCommands::LEARN_MORE_INTERRUPTED:
166 id = IDS_DOWNLOAD_MENU_LEARN_MORE_INTERRUPTED;
167 break;
169 CHECK(id != -1);
170 return l10n_util::GetStringUTF16(id);
173 void DownloadShelfContextMenu::DetachFromDownloadItem() {
174 if (!download_item_)
175 return;
177 download_commands_.reset();
178 download_item_->RemoveObserver(this);
179 download_item_ = NULL;
182 void DownloadShelfContextMenu::OnDownloadDestroyed(DownloadItem* download) {
183 DCHECK(download_item_ == download);
184 DetachFromDownloadItem();
187 ui::SimpleMenuModel* DownloadShelfContextMenu::GetInProgressMenuModel() {
188 if (in_progress_download_menu_model_)
189 return in_progress_download_menu_model_.get();
191 in_progress_download_menu_model_.reset(new ui::SimpleMenuModel(this));
193 in_progress_download_menu_model_->AddCheckItem(
194 DownloadCommands::OPEN_WHEN_COMPLETE,
195 GetLabelForCommandId(DownloadCommands::OPEN_WHEN_COMPLETE));
196 in_progress_download_menu_model_->AddCheckItem(
197 DownloadCommands::ALWAYS_OPEN_TYPE,
198 GetLabelForCommandId(DownloadCommands::ALWAYS_OPEN_TYPE));
199 in_progress_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR);
200 in_progress_download_menu_model_->AddItem(
201 DownloadCommands::PAUSE, GetLabelForCommandId(DownloadCommands::PAUSE));
202 in_progress_download_menu_model_->AddItem(
203 DownloadCommands::SHOW_IN_FOLDER,
204 GetLabelForCommandId(DownloadCommands::SHOW_IN_FOLDER));
205 in_progress_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR);
206 in_progress_download_menu_model_->AddItem(
207 DownloadCommands::CANCEL, GetLabelForCommandId(DownloadCommands::CANCEL));
209 return in_progress_download_menu_model_.get();
212 ui::SimpleMenuModel* DownloadShelfContextMenu::GetInProgressPausedMenuModel() {
213 if (in_progress_download_paused_menu_model_)
214 return in_progress_download_paused_menu_model_.get();
216 in_progress_download_paused_menu_model_.reset(new ui::SimpleMenuModel(this));
218 in_progress_download_paused_menu_model_->AddCheckItem(
219 DownloadCommands::OPEN_WHEN_COMPLETE,
220 GetLabelForCommandId(DownloadCommands::OPEN_WHEN_COMPLETE));
221 in_progress_download_paused_menu_model_->AddCheckItem(
222 DownloadCommands::ALWAYS_OPEN_TYPE,
223 GetLabelForCommandId(DownloadCommands::ALWAYS_OPEN_TYPE));
224 in_progress_download_paused_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR);
225 in_progress_download_paused_menu_model_->AddItem(
226 DownloadCommands::RESUME, GetLabelForCommandId(DownloadCommands::RESUME));
227 in_progress_download_paused_menu_model_->AddItem(
228 DownloadCommands::SHOW_IN_FOLDER,
229 GetLabelForCommandId(DownloadCommands::SHOW_IN_FOLDER));
230 in_progress_download_paused_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR);
231 in_progress_download_paused_menu_model_->AddItem(
232 DownloadCommands::CANCEL, GetLabelForCommandId(DownloadCommands::CANCEL));
234 return in_progress_download_paused_menu_model_.get();
237 ui::SimpleMenuModel* DownloadShelfContextMenu::GetFinishedMenuModel() {
238 if (finished_download_menu_model_)
239 return finished_download_menu_model_.get();
241 finished_download_menu_model_.reset(new ui::SimpleMenuModel(this));
243 finished_download_menu_model_->AddItem(
244 DownloadCommands::OPEN_WHEN_COMPLETE,
245 GetLabelForCommandId(DownloadCommands::OPEN_WHEN_COMPLETE));
246 finished_download_menu_model_->AddCheckItem(
247 DownloadCommands::ALWAYS_OPEN_TYPE,
248 GetLabelForCommandId(DownloadCommands::ALWAYS_OPEN_TYPE));
249 finished_download_menu_model_->AddItem(
250 DownloadCommands::PLATFORM_OPEN,
251 GetLabelForCommandId(DownloadCommands::PLATFORM_OPEN));
252 finished_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR);
253 finished_download_menu_model_->AddItem(
254 DownloadCommands::SHOW_IN_FOLDER,
255 GetLabelForCommandId(DownloadCommands::SHOW_IN_FOLDER));
256 finished_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR);
257 finished_download_menu_model_->AddItem(
258 DownloadCommands::CANCEL, GetLabelForCommandId(DownloadCommands::CANCEL));
260 return finished_download_menu_model_.get();
263 ui::SimpleMenuModel* DownloadShelfContextMenu::GetInterruptedMenuModel() {
264 #if !defined(OS_WIN)
265 // If resumption isn't enabled and we aren't on Windows, then none of the
266 // options here are applicable.
267 if (!IsDownloadResumptionEnabled())
268 return GetInProgressMenuModel();
269 #endif
271 if (interrupted_download_menu_model_)
272 return interrupted_download_menu_model_.get();
274 interrupted_download_menu_model_.reset(new ui::SimpleMenuModel(this));
276 if (IsDownloadResumptionEnabled()) {
277 interrupted_download_menu_model_->AddItem(
278 DownloadCommands::RESUME,
279 GetLabelForCommandId(DownloadCommands::RESUME));
281 #if defined(OS_WIN)
282 // The Help Center article is currently Windows specific.
283 // TODO(asanka): Enable this for other platforms when the article is expanded
284 // for other platforms.
285 interrupted_download_menu_model_->AddItem(
286 DownloadCommands::LEARN_MORE_INTERRUPTED,
287 GetLabelForCommandId(DownloadCommands::LEARN_MORE_INTERRUPTED));
288 #endif
289 if (IsDownloadResumptionEnabled()) {
290 interrupted_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR);
291 interrupted_download_menu_model_->AddItem(
292 DownloadCommands::CANCEL,
293 GetLabelForCommandId(DownloadCommands::CANCEL));
296 return interrupted_download_menu_model_.get();
299 ui::SimpleMenuModel* DownloadShelfContextMenu::GetMaybeMaliciousMenuModel() {
300 if (maybe_malicious_download_menu_model_)
301 return maybe_malicious_download_menu_model_.get();
303 maybe_malicious_download_menu_model_.reset(new ui::SimpleMenuModel(this));
305 maybe_malicious_download_menu_model_->AddItem(
306 DownloadCommands::KEEP, GetLabelForCommandId(DownloadCommands::KEEP));
307 maybe_malicious_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR);
308 maybe_malicious_download_menu_model_->AddItem(
309 DownloadCommands::LEARN_MORE_SCANNING,
310 GetLabelForCommandId(DownloadCommands::LEARN_MORE_SCANNING));
311 return maybe_malicious_download_menu_model_.get();
314 ui::SimpleMenuModel* DownloadShelfContextMenu::GetMaliciousMenuModel() {
315 if (malicious_download_menu_model_)
316 return malicious_download_menu_model_.get();
318 malicious_download_menu_model_.reset(new ui::SimpleMenuModel(this));
319 malicious_download_menu_model_->AddItem(
320 DownloadCommands::LEARN_MORE_SCANNING,
321 GetLabelForCommandId(DownloadCommands::LEARN_MORE_SCANNING));
323 return malicious_download_menu_model_.get();