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