Chromium Code Reviews| 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 #if defined(ENABLE_PLUGIN_INSTALLATION) | |
|
jam
2014/06/09 19:49:09
nit: OS_WIN implies ENABLE_PLUGIN_INSTALLATION...
Lei Zhang
2014/06/12 05:04:26
The PDF UI code we started with has defined(OS_WIN
| |
| 53 is_pdf_reader_up_to_date_ = false; | |
| 54 if (IsDownloadPdf() && IsAdobeReaderDefaultPDFViewer()) { | |
| 55 AdobeReaderPluginInfo plugin_info; | |
| 56 GetAdobeReaderPluginInfo(NULL, &plugin_info); | |
|
jam
2014/06/09 19:49:09
this won't work after we remove NPAPI, which is wh
Lei Zhang
2014/06/12 05:04:26
Ok, I changed this to check the version of the rea
| |
| 57 is_pdf_reader_up_to_date_ = | |
| 58 plugin_info.is_installed && plugin_info.is_secure; | |
| 59 } | |
| 60 #else | |
| 61 is_pdf_reader_up_to_date_ = true; | |
| 62 #endif // defined(ENABLE_PLUGIN_INSTALLATION) | |
| 63 #endif // defined(OS_WIN) | |
| 47 } | 64 } |
| 48 | 65 |
| 49 ui::SimpleMenuModel* DownloadShelfContextMenu::GetMenuModel() { | 66 ui::SimpleMenuModel* DownloadShelfContextMenu::GetMenuModel() { |
| 50 ui::SimpleMenuModel* model = NULL; | 67 ui::SimpleMenuModel* model = NULL; |
| 51 | 68 |
| 52 if (!download_item_) | 69 if (!download_item_) |
| 53 return NULL; | 70 return NULL; |
| 54 | 71 |
| 55 DownloadItemModel download_model(download_item_); | 72 DownloadItemModel download_model(download_item_); |
| 56 // We shouldn't be opening a context menu for a dangerous download, unless it | 73 // 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: | 107 case CANCEL: |
| 91 return !download_item_->IsDone(); | 108 return !download_item_->IsDone(); |
| 92 case TOGGLE_PAUSE: | 109 case TOGGLE_PAUSE: |
| 93 return !download_item_->IsDone(); | 110 return !download_item_->IsDone(); |
| 94 case DISCARD: | 111 case DISCARD: |
| 95 case KEEP: | 112 case KEEP: |
| 96 case LEARN_MORE_SCANNING: | 113 case LEARN_MORE_SCANNING: |
| 97 case LEARN_MORE_INTERRUPTED: | 114 case LEARN_MORE_INTERRUPTED: |
| 98 return true; | 115 return true; |
| 99 } | 116 } |
| 117 NOTREACHED(); | |
| 100 return false; | 118 return false; |
| 101 } | 119 } |
| 102 | 120 |
| 103 bool DownloadShelfContextMenu::IsCommandIdChecked(int command_id) const { | 121 bool DownloadShelfContextMenu::IsCommandIdChecked(int command_id) const { |
| 104 if (!download_item_) | 122 if (!download_item_) |
| 105 return false; | 123 return false; |
| 106 | 124 |
| 107 switch (command_id) { | 125 switch (command_id) { |
| 108 case OPEN_WHEN_COMPLETE: | 126 case OPEN_WHEN_COMPLETE: |
| 109 return download_item_->GetOpenWhenComplete() || | 127 return download_item_->GetOpenWhenComplete() || |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 125 download_item_->ShowDownloadInShell(); | 143 download_item_->ShowDownloadInShell(); |
| 126 break; | 144 break; |
| 127 case OPEN_WHEN_COMPLETE: | 145 case OPEN_WHEN_COMPLETE: |
| 128 download_item_->OpenDownload(); | 146 download_item_->OpenDownload(); |
| 129 break; | 147 break; |
| 130 case ALWAYS_OPEN_TYPE: { | 148 case ALWAYS_OPEN_TYPE: { |
| 131 DownloadPrefs* prefs = DownloadPrefs::FromBrowserContext( | 149 DownloadPrefs* prefs = DownloadPrefs::FromBrowserContext( |
| 132 download_item_->GetBrowserContext()); | 150 download_item_->GetBrowserContext()); |
| 133 base::FilePath path = download_item_->GetTargetFilePath(); | 151 base::FilePath path = download_item_->GetTargetFilePath(); |
| 134 if (!IsCommandIdChecked(ALWAYS_OPEN_TYPE)) | 152 if (!IsCommandIdChecked(ALWAYS_OPEN_TYPE)) |
| 135 prefs->EnableAutoOpenBasedOnExtension(path); | 153 prefs->EnableAutoOpenBasedOnExtension(path); |
|
Lei Zhang
2014/06/09 09:04:46
I still need to implement some special logic to im
| |
| 136 else | 154 else |
| 137 prefs->DisableAutoOpenBasedOnExtension(path); | 155 prefs->DisableAutoOpenBasedOnExtension(path); |
| 138 break; | 156 break; |
| 139 } | 157 } |
| 140 case PLATFORM_OPEN: | 158 case PLATFORM_OPEN: |
| 141 DownloadItemModel(download_item_).OpenUsingPlatformHandler(); | 159 DownloadItemModel(download_item_).OpenUsingPlatformHandler(); |
| 142 break; | 160 break; |
| 143 case CANCEL: | 161 case CANCEL: |
| 144 download_item_->Cancel(true /* Cancelled by user */); | 162 download_item_->Cancel(true /* Cancelled by user */); |
| 145 break; | 163 break; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 196 base::string16 DownloadShelfContextMenu::GetLabelForCommandId( | 214 base::string16 DownloadShelfContextMenu::GetLabelForCommandId( |
| 197 int command_id) const { | 215 int command_id) const { |
| 198 switch (static_cast<ContextMenuCommands>(command_id)) { | 216 switch (static_cast<ContextMenuCommands>(command_id)) { |
| 199 case SHOW_IN_FOLDER: | 217 case SHOW_IN_FOLDER: |
| 200 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_SHOW); | 218 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_SHOW); |
| 201 case OPEN_WHEN_COMPLETE: | 219 case OPEN_WHEN_COMPLETE: |
| 202 if (download_item_ && !download_item_->IsDone()) | 220 if (download_item_ && !download_item_->IsDone()) |
| 203 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE); | 221 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE); |
| 204 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_OPEN); | 222 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_OPEN); |
| 205 case ALWAYS_OPEN_TYPE: | 223 case ALWAYS_OPEN_TYPE: |
| 206 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE); | 224 return l10n_util::GetStringUTF16(GetAlwaysOpenCommandId()); |
| 207 case PLATFORM_OPEN: | 225 case PLATFORM_OPEN: |
| 208 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_PLATFORM_OPEN); | 226 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_PLATFORM_OPEN); |
| 209 case CANCEL: | 227 case CANCEL: |
| 210 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_CANCEL); | 228 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_CANCEL); |
| 211 case TOGGLE_PAUSE: | 229 case TOGGLE_PAUSE: |
| 212 if (download_item_ && | 230 if (download_item_ && |
| 213 download_item_->GetState() == DownloadItem::IN_PROGRESS && | 231 download_item_->GetState() == DownloadItem::IN_PROGRESS && |
| 214 !download_item_->IsPaused()) | 232 !download_item_->IsPaused()) |
| 215 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_PAUSE_ITEM); | 233 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_PAUSE_ITEM); |
| 216 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_RESUME_ITEM); | 234 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_MENU_RESUME_ITEM); |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 243 | 261 |
| 244 ui::SimpleMenuModel* DownloadShelfContextMenu::GetInProgressMenuModel() { | 262 ui::SimpleMenuModel* DownloadShelfContextMenu::GetInProgressMenuModel() { |
| 245 if (in_progress_download_menu_model_) | 263 if (in_progress_download_menu_model_) |
| 246 return in_progress_download_menu_model_.get(); | 264 return in_progress_download_menu_model_.get(); |
| 247 | 265 |
| 248 in_progress_download_menu_model_.reset(new ui::SimpleMenuModel(this)); | 266 in_progress_download_menu_model_.reset(new ui::SimpleMenuModel(this)); |
| 249 | 267 |
| 250 in_progress_download_menu_model_->AddCheckItemWithStringId( | 268 in_progress_download_menu_model_->AddCheckItemWithStringId( |
| 251 OPEN_WHEN_COMPLETE, IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE); | 269 OPEN_WHEN_COMPLETE, IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE); |
| 252 in_progress_download_menu_model_->AddCheckItemWithStringId( | 270 in_progress_download_menu_model_->AddCheckItemWithStringId( |
| 253 ALWAYS_OPEN_TYPE, IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE); | 271 ALWAYS_OPEN_TYPE, GetAlwaysOpenCommandId()); |
| 254 in_progress_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); | 272 in_progress_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); |
| 255 in_progress_download_menu_model_->AddItemWithStringId( | 273 in_progress_download_menu_model_->AddItemWithStringId( |
| 256 TOGGLE_PAUSE, IDS_DOWNLOAD_MENU_PAUSE_ITEM); | 274 TOGGLE_PAUSE, IDS_DOWNLOAD_MENU_PAUSE_ITEM); |
| 257 in_progress_download_menu_model_->AddItemWithStringId( | 275 in_progress_download_menu_model_->AddItemWithStringId( |
| 258 SHOW_IN_FOLDER, IDS_DOWNLOAD_MENU_SHOW); | 276 SHOW_IN_FOLDER, IDS_DOWNLOAD_MENU_SHOW); |
| 259 in_progress_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); | 277 in_progress_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); |
| 260 in_progress_download_menu_model_->AddItemWithStringId( | 278 in_progress_download_menu_model_->AddItemWithStringId( |
| 261 CANCEL, IDS_DOWNLOAD_MENU_CANCEL); | 279 CANCEL, IDS_DOWNLOAD_MENU_CANCEL); |
| 262 | 280 |
| 263 return in_progress_download_menu_model_.get(); | 281 return in_progress_download_menu_model_.get(); |
| 264 } | 282 } |
| 265 | 283 |
| 266 ui::SimpleMenuModel* DownloadShelfContextMenu::GetFinishedMenuModel() { | 284 ui::SimpleMenuModel* DownloadShelfContextMenu::GetFinishedMenuModel() { |
| 267 if (finished_download_menu_model_) | 285 if (finished_download_menu_model_) |
| 268 return finished_download_menu_model_.get(); | 286 return finished_download_menu_model_.get(); |
| 269 | 287 |
| 270 finished_download_menu_model_.reset(new ui::SimpleMenuModel(this)); | 288 finished_download_menu_model_.reset(new ui::SimpleMenuModel(this)); |
| 271 | 289 |
| 272 finished_download_menu_model_->AddItemWithStringId( | 290 finished_download_menu_model_->AddItemWithStringId( |
| 273 OPEN_WHEN_COMPLETE, IDS_DOWNLOAD_MENU_OPEN); | 291 OPEN_WHEN_COMPLETE, IDS_DOWNLOAD_MENU_OPEN); |
| 274 finished_download_menu_model_->AddCheckItemWithStringId( | 292 finished_download_menu_model_->AddCheckItemWithStringId( |
| 275 ALWAYS_OPEN_TYPE, IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE); | 293 ALWAYS_OPEN_TYPE, GetAlwaysOpenCommandId()); |
| 276 if (DownloadItemModel(download_item_).ShouldPreferOpeningInBrowser()) | 294 if (DownloadItemModel(download_item_).ShouldPreferOpeningInBrowser()) { |
| 277 finished_download_menu_model_->AddItemWithStringId( | 295 finished_download_menu_model_->AddItemWithStringId( |
| 278 PLATFORM_OPEN, IDS_DOWNLOAD_MENU_PLATFORM_OPEN); | 296 PLATFORM_OPEN, IDS_DOWNLOAD_MENU_PLATFORM_OPEN); |
| 297 } | |
| 279 finished_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); | 298 finished_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); |
| 280 finished_download_menu_model_->AddItemWithStringId( | 299 finished_download_menu_model_->AddItemWithStringId( |
| 281 SHOW_IN_FOLDER, IDS_DOWNLOAD_MENU_SHOW); | 300 SHOW_IN_FOLDER, IDS_DOWNLOAD_MENU_SHOW); |
| 282 finished_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); | 301 finished_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); |
| 283 finished_download_menu_model_->AddItemWithStringId( | 302 finished_download_menu_model_->AddItemWithStringId( |
| 284 CANCEL, IDS_DOWNLOAD_MENU_CANCEL); | 303 CANCEL, IDS_DOWNLOAD_MENU_CANCEL); |
| 285 | 304 |
| 286 return finished_download_menu_model_.get(); | 305 return finished_download_menu_model_.get(); |
| 287 } | 306 } |
| 288 | 307 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 326 maybe_malicious_download_menu_model_.reset(new ui::SimpleMenuModel(this)); | 345 maybe_malicious_download_menu_model_.reset(new ui::SimpleMenuModel(this)); |
| 327 | 346 |
| 328 maybe_malicious_download_menu_model_->AddItemWithStringId( | 347 maybe_malicious_download_menu_model_->AddItemWithStringId( |
| 329 KEEP, IDS_DOWNLOAD_MENU_KEEP); | 348 KEEP, IDS_DOWNLOAD_MENU_KEEP); |
| 330 maybe_malicious_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); | 349 maybe_malicious_download_menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); |
| 331 maybe_malicious_download_menu_model_->AddItemWithStringId( | 350 maybe_malicious_download_menu_model_->AddItemWithStringId( |
| 332 LEARN_MORE_SCANNING, IDS_DOWNLOAD_MENU_LEARN_MORE_SCANNING); | 351 LEARN_MORE_SCANNING, IDS_DOWNLOAD_MENU_LEARN_MORE_SCANNING); |
| 333 return maybe_malicious_download_menu_model_.get(); | 352 return maybe_malicious_download_menu_model_.get(); |
| 334 } | 353 } |
| 335 | 354 |
| 336 ui::SimpleMenuModel* | 355 ui::SimpleMenuModel* DownloadShelfContextMenu::GetMaliciousMenuModel() { |
| 337 DownloadShelfContextMenu::GetMaliciousMenuModel() { | |
| 338 if (malicious_download_menu_model_) | 356 if (malicious_download_menu_model_) |
| 339 return malicious_download_menu_model_.get(); | 357 return malicious_download_menu_model_.get(); |
| 340 | 358 |
| 341 malicious_download_menu_model_.reset(new ui::SimpleMenuModel(this)); | 359 malicious_download_menu_model_.reset(new ui::SimpleMenuModel(this)); |
| 342 | 360 |
| 343 DownloadItemModel download_model(download_item_); | 361 DownloadItemModel download_model(download_item_); |
| 344 malicious_download_menu_model_->AddItemWithStringId( | 362 malicious_download_menu_model_->AddItemWithStringId( |
| 345 LEARN_MORE_SCANNING, IDS_DOWNLOAD_MENU_LEARN_MORE_SCANNING); | 363 LEARN_MORE_SCANNING, IDS_DOWNLOAD_MENU_LEARN_MORE_SCANNING); |
| 346 | 364 |
| 347 return malicious_download_menu_model_.get(); | 365 return malicious_download_menu_model_.get(); |
| 348 } | 366 } |
| 367 | |
| 368 int DownloadShelfContextMenu::GetAlwaysOpenCommandId() const { | |
|
asanka
2014/06/09 21:22:22
Nit: This returns a string ID, not a command ID.
Lei Zhang
2014/06/12 05:04:26
Done.
| |
| 369 #if defined(OS_WIN) | |
| 370 if (is_pdf_reader_up_to_date_) | |
| 371 return IDS_DOWNLOAD_MENU_ALWAYS_OPEN_PDF_IN_READER; | |
| 372 #endif | |
| 373 return IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE; | |
| 374 } | |
| 375 | |
| 376 #if defined(OS_WIN) | |
| 377 bool DownloadShelfContextMenu::IsDownloadPdf() const { | |
| 378 base::FilePath path = download_item_->GetTargetFilePath(); | |
| 379 return path.Extension() == FILE_PATH_LITERAL(".pdf"); | |
| 380 } | |
| 381 #endif | |
| OLD | NEW |