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/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 Loading... | |
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 |
119 void DownloadShelfContextMenu::ExecuteCommand(int command_id, int event_flags) { | 136 void DownloadShelfContextMenu::ExecuteCommand(int command_id, int event_flags) { |
120 if (!download_item_) | 137 if (!download_item_) |
121 return; | 138 return; |
122 | 139 |
123 switch (static_cast<ContextMenuCommands>(command_id)) { | 140 switch (static_cast<ContextMenuCommands>(command_id)) { |
124 case SHOW_IN_FOLDER: | 141 case SHOW_IN_FOLDER: |
125 download_item_->ShowDownloadInShell(); | 142 download_item_->ShowDownloadInShell(); |
126 break; | 143 break; |
127 case OPEN_WHEN_COMPLETE: | 144 case OPEN_WHEN_COMPLETE: |
128 download_item_->OpenDownload(); | 145 download_item_->OpenDownload(); |
129 break; | 146 break; |
130 case ALWAYS_OPEN_TYPE: { | 147 case ALWAYS_OPEN_TYPE: { |
148 bool is_checked = !IsCommandIdChecked(ALWAYS_OPEN_TYPE); | |
131 DownloadPrefs* prefs = DownloadPrefs::FromBrowserContext( | 149 DownloadPrefs* prefs = DownloadPrefs::FromBrowserContext( |
132 download_item_->GetBrowserContext()); | 150 download_item_->GetBrowserContext()); |
151 #if defined(OS_WIN) | |
152 if (CanOpenPdfInReader()) { | |
153 prefs->SetShouldOpenPdfInAdobeReader(is_checked); | |
154 return; | |
155 } | |
156 #endif | |
133 base::FilePath path = download_item_->GetTargetFilePath(); | 157 base::FilePath path = download_item_->GetTargetFilePath(); |
134 if (!IsCommandIdChecked(ALWAYS_OPEN_TYPE)) | 158 if (is_checked) |
135 prefs->EnableAutoOpenBasedOnExtension(path); | 159 prefs->EnableAutoOpenBasedOnExtension(path); |
136 else | 160 else |
137 prefs->DisableAutoOpenBasedOnExtension(path); | 161 prefs->DisableAutoOpenBasedOnExtension(path); |
138 break; | 162 break; |
139 } | 163 } |
140 case PLATFORM_OPEN: | 164 case PLATFORM_OPEN: |
141 DownloadItemModel(download_item_).OpenUsingPlatformHandler(); | 165 DownloadItemModel(download_item_).OpenUsingPlatformHandler(); |
142 break; | 166 break; |
143 case CANCEL: | 167 case CANCEL: |
144 download_item_->Cancel(true /* Cancelled by user */); | 168 download_item_->Cancel(true /* Cancelled by user */); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
196 base::string16 DownloadShelfContextMenu::GetLabelForCommandId( | 220 base::string16 DownloadShelfContextMenu::GetLabelForCommandId( |
197 int command_id) const { | 221 int command_id) const { |
198 switch (static_cast<ContextMenuCommands>(command_id)) { | 222 switch (static_cast<ContextMenuCommands>(command_id)) { |
199 case SHOW_IN_FOLDER: | 223 case SHOW_IN_FOLDER: |
200 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_SHOW); | 224 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_SHOW); |
201 case OPEN_WHEN_COMPLETE: | 225 case OPEN_WHEN_COMPLETE: |
202 if (download_item_ && !download_item_->IsDone()) | 226 if (download_item_ && !download_item_->IsDone()) |
203 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE); | 227 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE); |
204 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_OPEN); | 228 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_OPEN); |
205 case ALWAYS_OPEN_TYPE: | 229 case ALWAYS_OPEN_TYPE: |
206 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE); | 230 return l10n_util::GetStringUTF16(GetAlwaysOpenStringId()); |
207 case PLATFORM_OPEN: | 231 case PLATFORM_OPEN: |
208 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_PLATFORM_OPEN); | 232 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_PLATFORM_OPEN); |
209 case CANCEL: | 233 case CANCEL: |
210 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_CANCEL); | 234 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_CANCEL); |
211 case TOGGLE_PAUSE: | 235 case TOGGLE_PAUSE: |
212 if (download_item_ && | 236 if (download_item_ && |
213 download_item_->GetState() == DownloadItem::IN_PROGRESS && | 237 download_item_->GetState() == DownloadItem::IN_PROGRESS && |
214 !download_item_->IsPaused()) | 238 !download_item_->IsPaused()) |
215 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_PAUSE_ITEM); | 239 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_PAUSE_ITEM); |
216 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_RESUME_ITEM); | 240 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_RESUME_ITEM); |
(...skipping 26 matching lines...) Expand all Loading... | |
243 | 267 |
244 ui::SimpleMenuModel* DownloadShelfContextMenu::GetInProgressMenuModel() { | 268 ui::SimpleMenuModel* DownloadShelfContextMenu::GetInProgressMenuModel() { |
245 if (in_progress_download_menu_model_) | 269 if (in_progress_download_menu_model_) |
246 return in_progress_download_menu_model_.get(); | 270 return in_progress_download_menu_model_.get(); |
247 | 271 |
248 in_progress_download_menu_model_.reset(new ui::SimpleMenuModel(this)); | 272 in_progress_download_menu_model_.reset(new ui::SimpleMenuModel(this)); |
249 | 273 |
250 in_progress_download_menu_model_->AddCheckItemWithStringId( | 274 in_progress_download_menu_model_->AddCheckItemWithStringId( |
251 OPEN_WHEN_COMPLETE, IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE); | 275 OPEN_WHEN_COMPLETE, IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE); |
252 in_progress_download_menu_model_->AddCheckItemWithStringId( | 276 in_progress_download_menu_model_->AddCheckItemWithStringId( |
253 ALWAYS_OPEN_TYPE, IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE); | 277 ALWAYS_OPEN_TYPE, GetAlwaysOpenStringId()); |
254 in_progress_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); | 278 in_progress_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); |
255 in_progress_download_menu_model_->AddItemWithStringId( | 279 in_progress_download_menu_model_->AddItemWithStringId( |
256 TOGGLE_PAUSE, IDS_DOWNLOAD_MENU_PAUSE_ITEM); | 280 TOGGLE_PAUSE, IDS_DOWNLOAD_MENU_PAUSE_ITEM); |
257 in_progress_download_menu_model_->AddItemWithStringId( | 281 in_progress_download_menu_model_->AddItemWithStringId( |
258 SHOW_IN_FOLDER, IDS_DOWNLOAD_MENU_SHOW); | 282 SHOW_IN_FOLDER, IDS_DOWNLOAD_MENU_SHOW); |
259 in_progress_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); | 283 in_progress_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); |
260 in_progress_download_menu_model_->AddItemWithStringId( | 284 in_progress_download_menu_model_->AddItemWithStringId( |
261 CANCEL, IDS_DOWNLOAD_MENU_CANCEL); | 285 CANCEL, IDS_DOWNLOAD_MENU_CANCEL); |
262 | 286 |
263 return in_progress_download_menu_model_.get(); | 287 return in_progress_download_menu_model_.get(); |
264 } | 288 } |
265 | 289 |
266 ui::SimpleMenuModel* DownloadShelfContextMenu::GetFinishedMenuModel() { | 290 ui::SimpleMenuModel* DownloadShelfContextMenu::GetFinishedMenuModel() { |
267 if (finished_download_menu_model_) | 291 if (finished_download_menu_model_) |
268 return finished_download_menu_model_.get(); | 292 return finished_download_menu_model_.get(); |
269 | 293 |
270 finished_download_menu_model_.reset(new ui::SimpleMenuModel(this)); | 294 finished_download_menu_model_.reset(new ui::SimpleMenuModel(this)); |
271 | 295 |
272 finished_download_menu_model_->AddItemWithStringId( | 296 finished_download_menu_model_->AddItemWithStringId( |
273 OPEN_WHEN_COMPLETE, IDS_DOWNLOAD_MENU_OPEN); | 297 OPEN_WHEN_COMPLETE, IDS_DOWNLOAD_MENU_OPEN); |
274 finished_download_menu_model_->AddCheckItemWithStringId( | 298 finished_download_menu_model_->AddCheckItemWithStringId( |
275 ALWAYS_OPEN_TYPE, IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE); | 299 ALWAYS_OPEN_TYPE, GetAlwaysOpenStringId()); |
276 if (DownloadItemModel(download_item_).ShouldPreferOpeningInBrowser()) | 300 if (DownloadItemModel(download_item_).ShouldPreferOpeningInBrowser()) { |
277 finished_download_menu_model_->AddItemWithStringId( | 301 finished_download_menu_model_->AddItemWithStringId( |
278 PLATFORM_OPEN, IDS_DOWNLOAD_MENU_PLATFORM_OPEN); | 302 PLATFORM_OPEN, IDS_DOWNLOAD_MENU_PLATFORM_OPEN); |
303 } | |
279 finished_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); | 304 finished_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); |
280 finished_download_menu_model_->AddItemWithStringId( | 305 finished_download_menu_model_->AddItemWithStringId( |
281 SHOW_IN_FOLDER, IDS_DOWNLOAD_MENU_SHOW); | 306 SHOW_IN_FOLDER, IDS_DOWNLOAD_MENU_SHOW); |
282 finished_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); | 307 finished_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); |
283 finished_download_menu_model_->AddItemWithStringId( | 308 finished_download_menu_model_->AddItemWithStringId( |
284 CANCEL, IDS_DOWNLOAD_MENU_CANCEL); | 309 CANCEL, IDS_DOWNLOAD_MENU_CANCEL); |
285 | 310 |
286 return finished_download_menu_model_.get(); | 311 return finished_download_menu_model_.get(); |
287 } | 312 } |
288 | 313 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
326 maybe_malicious_download_menu_model_.reset(new ui::SimpleMenuModel(this)); | 351 maybe_malicious_download_menu_model_.reset(new ui::SimpleMenuModel(this)); |
327 | 352 |
328 maybe_malicious_download_menu_model_->AddItemWithStringId( | 353 maybe_malicious_download_menu_model_->AddItemWithStringId( |
329 KEEP, IDS_DOWNLOAD_MENU_KEEP); | 354 KEEP, IDS_DOWNLOAD_MENU_KEEP); |
330 maybe_malicious_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); | 355 maybe_malicious_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); |
331 maybe_malicious_download_menu_model_->AddItemWithStringId( | 356 maybe_malicious_download_menu_model_->AddItemWithStringId( |
332 LEARN_MORE_SCANNING, IDS_DOWNLOAD_MENU_LEARN_MORE_SCANNING); | 357 LEARN_MORE_SCANNING, IDS_DOWNLOAD_MENU_LEARN_MORE_SCANNING); |
333 return maybe_malicious_download_menu_model_.get(); | 358 return maybe_malicious_download_menu_model_.get(); |
334 } | 359 } |
335 | 360 |
336 ui::SimpleMenuModel* | 361 ui::SimpleMenuModel* DownloadShelfContextMenu::GetMaliciousMenuModel() { |
337 DownloadShelfContextMenu::GetMaliciousMenuModel() { | |
338 if (malicious_download_menu_model_) | 362 if (malicious_download_menu_model_) |
339 return malicious_download_menu_model_.get(); | 363 return malicious_download_menu_model_.get(); |
340 | 364 |
341 malicious_download_menu_model_.reset(new ui::SimpleMenuModel(this)); | 365 malicious_download_menu_model_.reset(new ui::SimpleMenuModel(this)); |
342 | 366 |
343 DownloadItemModel download_model(download_item_); | 367 DownloadItemModel download_model(download_item_); |
344 malicious_download_menu_model_->AddItemWithStringId( | 368 malicious_download_menu_model_->AddItemWithStringId( |
345 LEARN_MORE_SCANNING, IDS_DOWNLOAD_MENU_LEARN_MORE_SCANNING); | 369 LEARN_MORE_SCANNING, IDS_DOWNLOAD_MENU_LEARN_MORE_SCANNING); |
346 | 370 |
347 return malicious_download_menu_model_.get(); | 371 return malicious_download_menu_model_.get(); |
348 } | 372 } |
373 | |
374 int DownloadShelfContextMenu::GetAlwaysOpenStringId() const { | |
375 #if defined(OS_WIN) | |
376 if (CanOpenPdfInReader()) | |
377 return IDS_DOWNLOAD_MENU_ALWAYS_OPEN_PDF_IN_READER; | |
378 #endif | |
379 return IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE; | |
380 } | |
381 | |
382 #if defined(OS_WIN) | |
383 bool DownloadShelfContextMenu::IsDownloadPdf() const { | |
384 base::FilePath path = download_item_->GetTargetFilePath(); | |
385 return path.Extension() == FILE_PATH_LITERAL(".pdf"); | |
asanka
2014/06/12 16:50:55
Nit: path.MatchesExtension(...)
Lei Zhang
2014/06/18 13:48:29
Done.
| |
386 } | |
387 | |
388 bool DownloadShelfContextMenu::CanOpenPdfInReader() const { | |
389 return (is_pdf_reader_up_to_date_ && IsDownloadPdf()); | |
390 } | |
391 #endif | |
OLD | NEW |