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

Side by Side Diff: chrome/browser/download/download_shelf_context_menu.cc

Issue 324593004: Windows: Add an "Open in Adobe Reader" menu item for PDF files in the download shelf. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: leave content/ alone Created 6 years, 6 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 "base/command_line.h" 7 #include "base/command_line.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"
11 #include "chrome/browser/download/download_prefs.h" 11 #include "chrome/browser/download/download_prefs.h"
12 #include "chrome/browser/safe_browsing/download_protection_service.h" 12 #include "chrome/browser/safe_browsing/download_protection_service.h"
13 #include "chrome/browser/safe_browsing/safe_browsing_service.h" 13 #include "chrome/browser/safe_browsing/safe_browsing_service.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 "content/public/common/content_switches.h" 18 #include "content/public/common/content_switches.h"
19 #include "extensions/common/extension.h" 19 #include "extensions/common/extension.h"
20 #include "grit/generated_resources.h" 20 #include "grit/generated_resources.h"
21 #include "ui/base/l10n/l10n_util.h" 21 #include "ui/base/l10n/l10n_util.h"
22 22
23 #if defined(OS_WIN)
24 #include "chrome/browser/ui/pdf/adobe_reader_info_win.h"
25 #endif
26
23 using content::DownloadItem; 27 using content::DownloadItem;
24 using extensions::Extension;
25 28
26 namespace { 29 namespace {
27 30
28 // Returns true if downloads resumption is enabled. 31 // Returns true if downloads resumption is enabled.
29 bool IsDownloadResumptionEnabled() { 32 bool IsDownloadResumptionEnabled() {
30 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 33 return base::CommandLine::ForCurrentProcess()->HasSwitch(
31 return command_line.HasSwitch(switches::kEnableDownloadResumption); 34 switches::kEnableDownloadResumption);
32 } 35 }
33 36
34 } // namespace 37 } // namespace
35 38
36 DownloadShelfContextMenu::~DownloadShelfContextMenu() { 39 DownloadShelfContextMenu::~DownloadShelfContextMenu() {
37 DetachFromDownloadItem(); 40 DetachFromDownloadItem();
38 } 41 }
39 42
40 DownloadShelfContextMenu::DownloadShelfContextMenu( 43 DownloadShelfContextMenu::DownloadShelfContextMenu(
41 DownloadItem* download_item, 44 DownloadItem* download_item,
42 content::PageNavigator* navigator) 45 content::PageNavigator* navigator)
43 : download_item_(download_item), 46 : download_item_(download_item),
44 navigator_(navigator) { 47 navigator_(navigator) {
45 DCHECK(download_item_); 48 DCHECK(download_item_);
46 download_item_->AddObserver(this); 49 download_item_->AddObserver(this);
50
51 #if defined(OS_WIN)
52 is_pdf_reader_up_to_date_ = false;
53 if (IsDownloadPdf() && IsAdobeReaderDefaultPDFViewer())
54 is_pdf_reader_up_to_date_ = DownloadPrefs::IsAdobeReaderUpToDate();
55 #endif // defined(OS_WIN)
47 } 56 }
48 57
49 ui::SimpleMenuModel* DownloadShelfContextMenu::GetMenuModel() { 58 ui::SimpleMenuModel* DownloadShelfContextMenu::GetMenuModel() {
50 ui::SimpleMenuModel* model = NULL; 59 ui::SimpleMenuModel* model = NULL;
51 60
52 if (!download_item_) 61 if (!download_item_)
53 return NULL; 62 return NULL;
54 63
55 DownloadItemModel download_model(download_item_); 64 DownloadItemModel download_model(download_item_);
56 // We shouldn't be opening a context menu for a dangerous download, unless it 65 // We shouldn't be opening a context menu for a dangerous download, unless it
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 case CANCEL: 99 case CANCEL:
91 return !download_item_->IsDone(); 100 return !download_item_->IsDone();
92 case TOGGLE_PAUSE: 101 case TOGGLE_PAUSE:
93 return !download_item_->IsDone(); 102 return !download_item_->IsDone();
94 case DISCARD: 103 case DISCARD:
95 case KEEP: 104 case KEEP:
96 case LEARN_MORE_SCANNING: 105 case LEARN_MORE_SCANNING:
97 case LEARN_MORE_INTERRUPTED: 106 case LEARN_MORE_INTERRUPTED:
98 return true; 107 return true;
99 } 108 }
109 NOTREACHED();
100 return false; 110 return false;
101 } 111 }
102 112
103 bool DownloadShelfContextMenu::IsCommandIdChecked(int command_id) const { 113 bool DownloadShelfContextMenu::IsCommandIdChecked(int command_id) const {
104 if (!download_item_) 114 if (!download_item_)
105 return false; 115 return false;
106 116
107 switch (command_id) { 117 switch (command_id) {
108 case OPEN_WHEN_COMPLETE: 118 case OPEN_WHEN_COMPLETE:
109 return download_item_->GetOpenWhenComplete() || 119 return download_item_->GetOpenWhenComplete() ||
110 download_crx_util::IsExtensionDownload(*download_item_); 120 download_crx_util::IsExtensionDownload(*download_item_);
111 case ALWAYS_OPEN_TYPE: 121 case ALWAYS_OPEN_TYPE:
122 #if defined(OS_WIN)
123 if (CanOpenPdfInReader()) {
124 DownloadPrefs* prefs = DownloadPrefs::FromBrowserContext(
125 download_item_->GetBrowserContext());
126 return prefs->ShouldOpenPdfInAdobeReader();
127 }
128 #endif
112 return download_item_->ShouldOpenFileBasedOnExtension(); 129 return download_item_->ShouldOpenFileBasedOnExtension();
113 case TOGGLE_PAUSE: 130 case TOGGLE_PAUSE:
114 return download_item_->IsPaused(); 131 return download_item_->IsPaused();
115 } 132 }
116 return false; 133 return false;
117 } 134 }
118 135
136 bool DownloadShelfContextMenu::IsCommandIdVisible(int command_id) const {
137 if (!download_item_)
138 return false;
139
140 if (command_id == PLATFORM_OPEN)
141 return (DownloadItemModel(download_item_).ShouldPreferOpeningInBrowser());
142
143 return true;
144 }
145
119 void DownloadShelfContextMenu::ExecuteCommand(int command_id, int event_flags) { 146 void DownloadShelfContextMenu::ExecuteCommand(int command_id, int event_flags) {
120 if (!download_item_) 147 if (!download_item_)
121 return; 148 return;
122 149
123 switch (static_cast<ContextMenuCommands>(command_id)) { 150 switch (static_cast<ContextMenuCommands>(command_id)) {
124 case SHOW_IN_FOLDER: 151 case SHOW_IN_FOLDER:
125 download_item_->ShowDownloadInShell(); 152 download_item_->ShowDownloadInShell();
126 break; 153 break;
127 case OPEN_WHEN_COMPLETE: 154 case OPEN_WHEN_COMPLETE:
128 download_item_->OpenDownload(); 155 download_item_->OpenDownload();
129 break; 156 break;
130 case ALWAYS_OPEN_TYPE: { 157 case ALWAYS_OPEN_TYPE: {
158 bool is_checked = IsCommandIdChecked(ALWAYS_OPEN_TYPE);
131 DownloadPrefs* prefs = DownloadPrefs::FromBrowserContext( 159 DownloadPrefs* prefs = DownloadPrefs::FromBrowserContext(
132 download_item_->GetBrowserContext()); 160 download_item_->GetBrowserContext());
161 #if defined(OS_WIN)
162 if (CanOpenPdfInReader()) {
163 prefs->SetShouldOpenPdfInAdobeReader(!is_checked);
164 DownloadItemModel(download_item_).SetShouldPreferOpeningInBrowser(
165 is_checked);
166 break;
167 }
168 #endif
133 base::FilePath path = download_item_->GetTargetFilePath(); 169 base::FilePath path = download_item_->GetTargetFilePath();
134 if (!IsCommandIdChecked(ALWAYS_OPEN_TYPE)) 170 if (is_checked)
171 prefs->DisableAutoOpenBasedOnExtension(path);
172 else
135 prefs->EnableAutoOpenBasedOnExtension(path); 173 prefs->EnableAutoOpenBasedOnExtension(path);
136 else
137 prefs->DisableAutoOpenBasedOnExtension(path);
138 break; 174 break;
139 } 175 }
140 case PLATFORM_OPEN: 176 case PLATFORM_OPEN:
141 DownloadItemModel(download_item_).OpenUsingPlatformHandler(); 177 DownloadItemModel(download_item_).OpenUsingPlatformHandler();
142 break; 178 break;
143 case CANCEL: 179 case CANCEL:
144 download_item_->Cancel(true /* Cancelled by user */); 180 download_item_->Cancel(true /* Cancelled by user */);
145 break; 181 break;
146 case TOGGLE_PAUSE: 182 case TOGGLE_PAUSE:
147 if (download_item_->GetState() == DownloadItem::IN_PROGRESS && 183 if (download_item_->GetState() == DownloadItem::IN_PROGRESS &&
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 base::string16 DownloadShelfContextMenu::GetLabelForCommandId( 232 base::string16 DownloadShelfContextMenu::GetLabelForCommandId(
197 int command_id) const { 233 int command_id) const {
198 switch (static_cast<ContextMenuCommands>(command_id)) { 234 switch (static_cast<ContextMenuCommands>(command_id)) {
199 case SHOW_IN_FOLDER: 235 case SHOW_IN_FOLDER:
200 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_SHOW); 236 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_SHOW);
201 case OPEN_WHEN_COMPLETE: 237 case OPEN_WHEN_COMPLETE:
202 if (download_item_ && !download_item_->IsDone()) 238 if (download_item_ && !download_item_->IsDone())
203 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE); 239 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE);
204 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_OPEN); 240 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_OPEN);
205 case ALWAYS_OPEN_TYPE: 241 case ALWAYS_OPEN_TYPE:
206 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE); 242 return l10n_util::GetStringUTF16(GetAlwaysOpenStringId());
207 case PLATFORM_OPEN: 243 case PLATFORM_OPEN:
208 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_PLATFORM_OPEN); 244 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_PLATFORM_OPEN);
209 case CANCEL: 245 case CANCEL:
210 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_CANCEL); 246 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_CANCEL);
211 case TOGGLE_PAUSE: 247 case TOGGLE_PAUSE:
212 if (download_item_ && 248 if (download_item_ &&
213 download_item_->GetState() == DownloadItem::IN_PROGRESS && 249 download_item_->GetState() == DownloadItem::IN_PROGRESS &&
214 !download_item_->IsPaused()) 250 !download_item_->IsPaused())
215 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_PAUSE_ITEM); 251 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_PAUSE_ITEM);
216 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_RESUME_ITEM); 252 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_RESUME_ITEM);
(...skipping 26 matching lines...) Expand all
243 279
244 ui::SimpleMenuModel* DownloadShelfContextMenu::GetInProgressMenuModel() { 280 ui::SimpleMenuModel* DownloadShelfContextMenu::GetInProgressMenuModel() {
245 if (in_progress_download_menu_model_) 281 if (in_progress_download_menu_model_)
246 return in_progress_download_menu_model_.get(); 282 return in_progress_download_menu_model_.get();
247 283
248 in_progress_download_menu_model_.reset(new ui::SimpleMenuModel(this)); 284 in_progress_download_menu_model_.reset(new ui::SimpleMenuModel(this));
249 285
250 in_progress_download_menu_model_->AddCheckItemWithStringId( 286 in_progress_download_menu_model_->AddCheckItemWithStringId(
251 OPEN_WHEN_COMPLETE, IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE); 287 OPEN_WHEN_COMPLETE, IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE);
252 in_progress_download_menu_model_->AddCheckItemWithStringId( 288 in_progress_download_menu_model_->AddCheckItemWithStringId(
253 ALWAYS_OPEN_TYPE, IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE); 289 ALWAYS_OPEN_TYPE, GetAlwaysOpenStringId());
254 in_progress_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); 290 in_progress_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR);
255 in_progress_download_menu_model_->AddItemWithStringId( 291 in_progress_download_menu_model_->AddItemWithStringId(
256 TOGGLE_PAUSE, IDS_DOWNLOAD_MENU_PAUSE_ITEM); 292 TOGGLE_PAUSE, IDS_DOWNLOAD_MENU_PAUSE_ITEM);
257 in_progress_download_menu_model_->AddItemWithStringId( 293 in_progress_download_menu_model_->AddItemWithStringId(
258 SHOW_IN_FOLDER, IDS_DOWNLOAD_MENU_SHOW); 294 SHOW_IN_FOLDER, IDS_DOWNLOAD_MENU_SHOW);
259 in_progress_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); 295 in_progress_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR);
260 in_progress_download_menu_model_->AddItemWithStringId( 296 in_progress_download_menu_model_->AddItemWithStringId(
261 CANCEL, IDS_DOWNLOAD_MENU_CANCEL); 297 CANCEL, IDS_DOWNLOAD_MENU_CANCEL);
262 298
263 return in_progress_download_menu_model_.get(); 299 return in_progress_download_menu_model_.get();
264 } 300 }
265 301
266 ui::SimpleMenuModel* DownloadShelfContextMenu::GetFinishedMenuModel() { 302 ui::SimpleMenuModel* DownloadShelfContextMenu::GetFinishedMenuModel() {
267 if (finished_download_menu_model_) 303 if (finished_download_menu_model_)
268 return finished_download_menu_model_.get(); 304 return finished_download_menu_model_.get();
269 305
270 finished_download_menu_model_.reset(new ui::SimpleMenuModel(this)); 306 finished_download_menu_model_.reset(new ui::SimpleMenuModel(this));
271 307
272 finished_download_menu_model_->AddItemWithStringId( 308 finished_download_menu_model_->AddItemWithStringId(
273 OPEN_WHEN_COMPLETE, IDS_DOWNLOAD_MENU_OPEN); 309 OPEN_WHEN_COMPLETE, IDS_DOWNLOAD_MENU_OPEN);
274 finished_download_menu_model_->AddCheckItemWithStringId( 310 finished_download_menu_model_->AddCheckItemWithStringId(
275 ALWAYS_OPEN_TYPE, IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE); 311 ALWAYS_OPEN_TYPE, GetAlwaysOpenStringId());
276 if (DownloadItemModel(download_item_).ShouldPreferOpeningInBrowser()) 312 finished_download_menu_model_->AddItemWithStringId(
277 finished_download_menu_model_->AddItemWithStringId( 313 PLATFORM_OPEN, IDS_DOWNLOAD_MENU_PLATFORM_OPEN);
278 PLATFORM_OPEN, IDS_DOWNLOAD_MENU_PLATFORM_OPEN);
279 finished_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); 314 finished_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR);
280 finished_download_menu_model_->AddItemWithStringId( 315 finished_download_menu_model_->AddItemWithStringId(
281 SHOW_IN_FOLDER, IDS_DOWNLOAD_MENU_SHOW); 316 SHOW_IN_FOLDER, IDS_DOWNLOAD_MENU_SHOW);
282 finished_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); 317 finished_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR);
283 finished_download_menu_model_->AddItemWithStringId( 318 finished_download_menu_model_->AddItemWithStringId(
284 CANCEL, IDS_DOWNLOAD_MENU_CANCEL); 319 CANCEL, IDS_DOWNLOAD_MENU_CANCEL);
285 320
286 return finished_download_menu_model_.get(); 321 return finished_download_menu_model_.get();
287 } 322 }
288 323
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 maybe_malicious_download_menu_model_.reset(new ui::SimpleMenuModel(this)); 361 maybe_malicious_download_menu_model_.reset(new ui::SimpleMenuModel(this));
327 362
328 maybe_malicious_download_menu_model_->AddItemWithStringId( 363 maybe_malicious_download_menu_model_->AddItemWithStringId(
329 KEEP, IDS_DOWNLOAD_MENU_KEEP); 364 KEEP, IDS_DOWNLOAD_MENU_KEEP);
330 maybe_malicious_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); 365 maybe_malicious_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR);
331 maybe_malicious_download_menu_model_->AddItemWithStringId( 366 maybe_malicious_download_menu_model_->AddItemWithStringId(
332 LEARN_MORE_SCANNING, IDS_DOWNLOAD_MENU_LEARN_MORE_SCANNING); 367 LEARN_MORE_SCANNING, IDS_DOWNLOAD_MENU_LEARN_MORE_SCANNING);
333 return maybe_malicious_download_menu_model_.get(); 368 return maybe_malicious_download_menu_model_.get();
334 } 369 }
335 370
336 ui::SimpleMenuModel* 371 ui::SimpleMenuModel* DownloadShelfContextMenu::GetMaliciousMenuModel() {
337 DownloadShelfContextMenu::GetMaliciousMenuModel() {
338 if (malicious_download_menu_model_) 372 if (malicious_download_menu_model_)
339 return malicious_download_menu_model_.get(); 373 return malicious_download_menu_model_.get();
340 374
341 malicious_download_menu_model_.reset(new ui::SimpleMenuModel(this)); 375 malicious_download_menu_model_.reset(new ui::SimpleMenuModel(this));
342 376
343 DownloadItemModel download_model(download_item_); 377 DownloadItemModel download_model(download_item_);
344 malicious_download_menu_model_->AddItemWithStringId( 378 malicious_download_menu_model_->AddItemWithStringId(
345 LEARN_MORE_SCANNING, IDS_DOWNLOAD_MENU_LEARN_MORE_SCANNING); 379 LEARN_MORE_SCANNING, IDS_DOWNLOAD_MENU_LEARN_MORE_SCANNING);
346 380
347 return malicious_download_menu_model_.get(); 381 return malicious_download_menu_model_.get();
348 } 382 }
383
384 int DownloadShelfContextMenu::GetAlwaysOpenStringId() const {
385 #if defined(OS_WIN)
386 if (CanOpenPdfInReader())
387 return IDS_DOWNLOAD_MENU_ALWAYS_OPEN_PDF_IN_READER;
388 #endif
389 return IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE;
390 }
391
392 #if defined(OS_WIN)
393 bool DownloadShelfContextMenu::IsDownloadPdf() const {
394 base::FilePath path = download_item_->GetTargetFilePath();
395 return path.MatchesExtension(FILE_PATH_LITERAL(".pdf"));
396 }
397
398 bool DownloadShelfContextMenu::CanOpenPdfInReader() const {
399 return (is_pdf_reader_up_to_date_ && IsDownloadPdf());
400 }
401 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698