Chromium Code Reviews| Index: chrome/browser/download/download_commands.cc |
| diff --git a/chrome/browser/download/download_commands.cc b/chrome/browser/download/download_commands.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4deaa3c604ec7b6c8ee33dc45286f61eaf0205f7 |
| --- /dev/null |
| +++ b/chrome/browser/download/download_commands.cc |
| @@ -0,0 +1,310 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/download/download_commands.h" |
| + |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/download/download_crx_util.h" |
| +#include "chrome/browser/download/download_item_model.h" |
| +#include "chrome/browser/download/download_prefs.h" |
| +#include "chrome/browser/safe_browsing/download_protection_service.h" |
| +#include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| +#include "chrome/common/url_constants.h" |
| +#include "chrome/grit/generated_resources.h" |
| +#include "grit/theme_resources.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| +#include "ui/base/resource/resource_bundle.h" |
| + |
| +#if defined(OS_WIN) |
| +#include "chrome/browser/download/download_target_determiner.h" |
| +#include "chrome/browser/ui/pdf/adobe_reader_info_win.h" |
| +#endif |
| + |
| +DownloadCommands::DownloadCommands(content::DownloadItem* download_item) : |
| + download_item_(download_item) {} |
| + |
| +int DownloadCommands::GetCommandIconId(Command command) { |
| + switch (command) { |
| + case PAUSE: |
| + return IDR_DOWNLOAD_NOTIFICATION_MENU_PAUSE; |
| + case RESUME: |
| + return IDR_DOWNLOAD_NOTIFICATION_MENU_RESUME; |
| + case SHOW_IN_FOLDER: |
| + return IDR_DOWNLOAD_NOTIFICATION_MENU_FOLDER; |
| + case RETRY: |
| + return IDR_DOWNLOAD_NOTIFICATION_MENU_DOWNLOAD; |
| + case DISCARD: |
| + return IDR_DOWNLOAD_NOTIFICATION_MENU_DELETE; |
| + case OPEN_WHEN_COMPLETE: |
| + case ALWAYS_OPEN_TYPE: |
| + case PLATFORM_OPEN: |
| + case CANCEL: |
| + case KEEP: |
| + case LEARN_MORE_SCANNING: |
| + case LEARN_MORE_INTERRUPTED: |
| + return -1; |
| + } |
| + NOTREACHED(); |
| + return -1; |
| +} |
| + |
| +gfx::Image DownloadCommands::GetCommandIcon(Command command) { |
| + ResourceBundle& bundle = ResourceBundle::GetSharedInstance(); |
| + return bundle.GetImageNamed(GetCommandIconId(command)); |
| +} |
| + |
| +base::string16 DownloadCommands::GetCommandString(Command command) const { |
| + return l10n_util::GetStringUTF16(GetCommandStringIdInternal(command, false)); |
| +} |
| + |
| +base::string16 DownloadCommands::GetCommandStringForMenu( |
| + Command command) const { |
| + return l10n_util::GetStringUTF16(GetCommandStringIdInternal(command, true)); |
| +} |
| + |
| +bool DownloadCommands::IsCommandEnabled(Command command) const { |
| + if (!download_item_) |
| + return false; |
| + |
| + switch (command) { |
| + case SHOW_IN_FOLDER: |
| + return download_item_->CanShowInFolder(); |
| + case OPEN_WHEN_COMPLETE: |
| + case PLATFORM_OPEN: |
| + return download_item_->CanOpenDownload() && |
| + !download_crx_util::IsExtensionDownload(*download_item_); |
| + case ALWAYS_OPEN_TYPE: |
| + // For temporary downloads, the target filename might be a temporary |
| + // filename. Don't base an "Always open" decision based on it. Also |
| + // exclude extensions. |
| + return download_item_->CanOpenDownload() && |
| + !download_crx_util::IsExtensionDownload(*download_item_); |
| + case CANCEL: |
| + return !download_item_->IsDone(); |
| + case PAUSE: |
| + return !download_item_->IsDone() && |
| + !download_item_->IsPaused() && |
| + download_item_->GetState() == content::DownloadItem::IN_PROGRESS; |
| + case RESUME: |
| + return !download_item_->IsDone() && |
| + (download_item_->IsPaused() || |
| + download_item_->GetState() != content::DownloadItem::IN_PROGRESS); |
| + case DISCARD: |
| + case KEEP: |
| + case LEARN_MORE_SCANNING: |
| + case LEARN_MORE_INTERRUPTED: |
| + case RETRY: |
| + return true; |
| + } |
| + NOTREACHED(); |
| + return false; |
| +} |
| + |
| +bool DownloadCommands::IsCommandChecked(Command command) const { |
| + if (!download_item_) |
| + return false; |
| + |
| + switch (command) { |
| + case OPEN_WHEN_COMPLETE: |
| + return download_item_->GetOpenWhenComplete() || |
| + download_crx_util::IsExtensionDownload(*download_item_); |
| + case ALWAYS_OPEN_TYPE: |
| +#if defined(OS_WIN) || defined(OS_LINUX) || \ |
| + (defined(OS_MACOSX) && !defined(OS_IOS)) |
| + if (CanOpenPdfInSystemViewer()) { |
| + DownloadPrefs* prefs = DownloadPrefs::FromBrowserContext( |
| + download_item_->GetBrowserContext()); |
| + return prefs->ShouldOpenPdfInSystemReader(); |
| + } |
| +#endif |
| + return download_item_->ShouldOpenFileBasedOnExtension(); |
| + case PAUSE: |
| + case RESUME: |
| + return download_item_->IsPaused(); |
| + case SHOW_IN_FOLDER: |
| + case PLATFORM_OPEN: |
| + case CANCEL: |
| + case DISCARD: |
| + case KEEP: |
| + case RETRY: |
| + case LEARN_MORE_SCANNING: |
| + case LEARN_MORE_INTERRUPTED: |
| + return false; |
| + } |
| + return false; |
| +} |
| + |
| +bool DownloadCommands::IsCommandVisible(Command command) const { |
| + if (!download_item_) |
| + return false; |
| + |
| + if (command == PLATFORM_OPEN) |
| + return (DownloadItemModel(download_item_).ShouldPreferOpeningInBrowser()); |
| + |
| + return true; |
| +} |
| + |
| +void DownloadCommands::ExecuteCommand(Command command, |
| + content::PageNavigator* navigator) { |
| + if (!download_item_) |
| + return; |
| + |
| + switch (command) { |
| + case SHOW_IN_FOLDER: |
| + download_item_->ShowDownloadInShell(); |
| + break; |
| + case OPEN_WHEN_COMPLETE: |
| + download_item_->OpenDownload(); |
| + break; |
| + case ALWAYS_OPEN_TYPE: { |
| + bool is_checked = IsCommandChecked(ALWAYS_OPEN_TYPE); |
| + DownloadPrefs* prefs = DownloadPrefs::FromBrowserContext( |
| + download_item_->GetBrowserContext()); |
| +#if defined(OS_WIN) || defined(OS_LINUX) || \ |
| + (defined(OS_MACOSX) && !defined(OS_IOS)) |
| + if (CanOpenPdfInSystemViewer()) { |
| + prefs->SetShouldOpenPdfInSystemReader(!is_checked); |
| + DownloadItemModel(download_item_).SetShouldPreferOpeningInBrowser( |
| + is_checked); |
| + break; |
| + } |
| +#endif |
| + base::FilePath path = download_item_->GetTargetFilePath(); |
| + if (is_checked) |
| + prefs->DisableAutoOpenBasedOnExtension(path); |
| + else |
| + prefs->EnableAutoOpenBasedOnExtension(path); |
| + break; |
| + } |
| + case PLATFORM_OPEN: |
| + DownloadItemModel(download_item_).OpenUsingPlatformHandler(); |
| + break; |
| + case CANCEL: |
| + download_item_->Cancel(true /* Cancelled by user */); |
| + break; |
| + case DISCARD: |
| + download_item_->Remove(); |
| + break; |
| + case KEEP: |
| + download_item_->ValidateDangerousDownload(); |
| + break; |
| + case LEARN_MORE_SCANNING: { |
| +#if defined(FULL_SAFE_BROWSING) |
| + using safe_browsing::DownloadProtectionService; |
| + SafeBrowsingService* sb_service = |
| + g_browser_process->safe_browsing_service(); |
| + DownloadProtectionService* protection_service = |
| + (sb_service ? sb_service->download_protection_service() : NULL); |
| + if (protection_service) { |
| + protection_service->ShowDetailsForDownload(*download_item_, navigator); |
| + } |
| +#else |
| + // Should only be getting invoked if we are using safe browsing. |
| + NOTREACHED(); |
| +#endif |
| + break; |
| + } |
| + case LEARN_MORE_INTERRUPTED: |
| + navigator->OpenURL( |
| + content::OpenURLParams(GURL(chrome::kDownloadInterruptedLearnMoreURL), |
| + content::Referrer(), |
| + NEW_FOREGROUND_TAB, |
| + ui::PAGE_TRANSITION_LINK, |
| + false)); |
| + break; |
| + case PAUSE: |
| + download_item_->Pause(); |
| + break; |
| + case RESUME: |
| + download_item_->Resume(); |
| + break; |
| + case RETRY: |
| + download_item_->Resume(); |
| + break; |
| + } |
| +} |
| + |
| +#if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) |
| +bool DownloadCommands::IsDownloadPdf() const { |
| + base::FilePath path = download_item_->GetTargetFilePath(); |
| + return path.MatchesExtension(FILE_PATH_LITERAL(".pdf")); |
| +} |
| +#endif |
| + |
| +bool DownloadCommands::CanOpenPdfInSystemViewer() const { |
| +#if defined(OS_WIN) |
| + bool is_adobe_pdf_reader_up_to_date = false; |
| + if (IsDownloadPdf() && IsAdobeReaderDefaultPDFViewer()) { |
| + is_adobe_pdf_reader_up_to_date = |
| + DownloadTargetDeterminer::IsAdobeReaderUpToDate(); |
| + } |
| + return IsDownloadPdf() && |
| + (IsAdobeReaderDefaultPDFViewer() ? is_adobe_pdf_reader_up_to_date : |
| + true); |
| +#elif defined(OS_MACOSX) || defined(OS_LINUX) |
| + return IsDownloadPdf(); |
| +#endif |
| +} |
| + |
| +int DownloadCommands::GetCommandStringIdInternal( |
| + Command command, bool for_menu) const { |
| + switch (command) { |
| + case OPEN_WHEN_COMPLETE: |
| + if (download_item_ && !download_item_->IsDone()) { |
| + return for_menu ? IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE : |
|
asanka
2015/02/13 21:17:05
Rather than making each cast conditional, shall we
yoshiki
2015/02/19 15:07:01
Done. I moved this to DownloadShelfContextMenu and
|
| + IDS_DOWNLOAD_STATUS_OPEN_WHEN_COMPLETE; |
| + } else { |
| + return for_menu ? IDS_DOWNLOAD_MENU_OPEN: |
| + IDS_DOWNLOAD_STATUS_OPEN_WHEN_COMPLETE; |
| + } |
| + case PAUSE: |
| + // Only for non menu. |
| + return IDS_DOWNLOAD_LINK_PAUSE; |
| + case RESUME: |
| + // Only for non menu. |
| + return IDS_DOWNLOAD_LINK_RESUME; |
| + case SHOW_IN_FOLDER: |
| + return for_menu ? IDS_DOWNLOAD_MENU_SHOW : IDS_DOWNLOAD_LINK_SHOW; |
| + case RETRY: |
| + // Only for non menu. |
| + return IDS_DOWNLOAD_LINK_RETRY; |
| + case DISCARD: |
| + return for_menu ? IDS_DOWNLOAD_MENU_DISCARD : IDS_DISCARD_DOWNLOAD; |
| + case KEEP: |
| + // Only for menu. |
| + return IDS_DOWNLOAD_MENU_KEEP; |
| + case ALWAYS_OPEN_TYPE: |
| + // Only for menu. |
| + return GetAlwaysOpenStringId(); |
| + case PLATFORM_OPEN: |
| + // Only for menu. |
| + return IDS_DOWNLOAD_MENU_PLATFORM_OPEN; |
| + case CANCEL: |
| + // Only for menu. |
| + return IDS_DOWNLOAD_MENU_CANCEL; |
| + case LEARN_MORE_SCANNING: |
| + // Only for menu. |
| + return IDS_DOWNLOAD_MENU_LEARN_MORE_SCANNING; |
| + case LEARN_MORE_INTERRUPTED: |
| + // Only for menu. |
| + return IDS_DOWNLOAD_MENU_LEARN_MORE_INTERRUPTED; |
| + } |
| + NOTREACHED(); |
| + return -1; |
| +} |
| + |
| +int DownloadCommands::GetAlwaysOpenStringId() const { |
| + bool can_open_pdf_in_system_viewer = |
| + DownloadCommands(download_item_).CanOpenPdfInSystemViewer(); |
| +#if defined(OS_WIN) |
| + if (can_open_pdf_in_system_viewer) |
| + return IsAdobeReaderDefaultPDFViewer() |
| + ? IDS_DOWNLOAD_MENU_ALWAYS_OPEN_PDF_IN_READER |
| + : IDS_DOWNLOAD_MENU_PLATFORM_OPEN_ALWAYS; |
| +#elif defined(OS_MACOSX) || defined(OS_LINUX) |
| + if (can_open_pdf_in_system_viewer) |
| + return IDS_DOWNLOAD_MENU_PLATFORM_OPEN_ALWAYS; |
| +#endif |
| + return IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE; |
| +} |