1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/download/download_commands.h"
7 #include "chrome/browser/browser_process.h"
8 #include "chrome/browser/download/download_crx_util.h"
9 #include "chrome/browser/download/download_item_model.h"
10 #include "chrome/browser/download/download_prefs.h"
11 #include "chrome/browser/profiles/profile_manager.h"
12 #include "chrome/browser/safe_browsing/download_protection_service.h"
13 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
14 #include "chrome/browser/ui/browser_finder.h"
15 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
16 #include "chrome/common/url_constants.h"
17 #include "chrome/grit/generated_resources.h"
18 #include "grit/theme_resources.h"
19 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/base/resource/resource_bundle.h"
23 #include "chrome/browser/download/download_target_determiner.h"
24 #include "chrome/browser/ui/pdf/adobe_reader_info_win.h"
27 DownloadCommands::DownloadCommands(content::DownloadItem
* download_item
)
28 : download_item_(download_item
) {
29 DCHECK(download_item
);
32 int DownloadCommands::GetCommandIconId(Command command
) {
35 return IDR_DOWNLOAD_NOTIFICATION_MENU_PAUSE
;
37 return IDR_DOWNLOAD_NOTIFICATION_MENU_RESUME
;
39 return IDR_DOWNLOAD_NOTIFICATION_MENU_FOLDER
;
42 return IDR_DOWNLOAD_NOTIFICATION_MENU_DOWNLOAD
;
44 return IDR_DOWNLOAD_NOTIFICATION_MENU_DELETE
;
45 case OPEN_WHEN_COMPLETE
:
46 case ALWAYS_OPEN_TYPE
:
49 case LEARN_MORE_SCANNING
:
50 case LEARN_MORE_INTERRUPTED
:
57 gfx::Image
DownloadCommands::GetCommandIcon(Command command
) {
58 ResourceBundle
& bundle
= ResourceBundle::GetSharedInstance();
59 return bundle
.GetImageNamed(GetCommandIconId(command
));
62 bool DownloadCommands::IsCommandEnabled(Command command
) const {
65 return download_item_
->CanShowInFolder();
66 case OPEN_WHEN_COMPLETE
:
68 return download_item_
->CanOpenDownload() &&
69 !download_crx_util::IsExtensionDownload(*download_item_
);
70 case ALWAYS_OPEN_TYPE
:
71 // For temporary downloads, the target filename might be a temporary
72 // filename. Don't base an "Always open" decision based on it. Also
73 // exclude extensions.
74 return download_item_
->CanOpenDownload() &&
75 !download_crx_util::IsExtensionDownload(*download_item_
);
77 return !download_item_
->IsDone();
79 return !download_item_
->IsDone() && !download_item_
->IsPaused() &&
80 download_item_
->GetState() == content::DownloadItem::IN_PROGRESS
;
82 return download_item_
->CanResume() &&
83 (download_item_
->IsPaused() ||
84 download_item_
->GetState() != content::DownloadItem::IN_PROGRESS
);
87 case LEARN_MORE_SCANNING
:
88 case LEARN_MORE_INTERRUPTED
:
96 bool DownloadCommands::IsCommandChecked(Command command
) const {
98 case OPEN_WHEN_COMPLETE
:
99 return download_item_
->GetOpenWhenComplete() ||
100 download_crx_util::IsExtensionDownload(*download_item_
);
101 case ALWAYS_OPEN_TYPE
:
102 #if defined(OS_WIN) || defined(OS_LINUX) || \
103 (defined(OS_MACOSX) && !defined(OS_IOS))
104 if (CanOpenPdfInSystemViewer()) {
105 DownloadPrefs
* prefs
= DownloadPrefs::FromBrowserContext(
106 download_item_
->GetBrowserContext());
107 return prefs
->ShouldOpenPdfInSystemReader();
110 return download_item_
->ShouldOpenFileBasedOnExtension();
113 return download_item_
->IsPaused();
120 case LEARN_MORE_SCANNING
:
121 case LEARN_MORE_INTERRUPTED
:
127 bool DownloadCommands::IsCommandVisible(Command command
) const {
128 if (command
== PLATFORM_OPEN
)
129 return (DownloadItemModel(download_item_
).ShouldPreferOpeningInBrowser());
134 void DownloadCommands::ExecuteCommand(Command command
) {
137 download_item_
->ShowDownloadInShell();
139 case OPEN_WHEN_COMPLETE
:
140 download_item_
->OpenDownload();
142 case ALWAYS_OPEN_TYPE
: {
143 bool is_checked
= IsCommandChecked(ALWAYS_OPEN_TYPE
);
144 DownloadPrefs
* prefs
= DownloadPrefs::FromBrowserContext(
145 download_item_
->GetBrowserContext());
146 #if defined(OS_WIN) || defined(OS_LINUX) || \
147 (defined(OS_MACOSX) && !defined(OS_IOS))
148 if (CanOpenPdfInSystemViewer()) {
149 prefs
->SetShouldOpenPdfInSystemReader(!is_checked
);
150 DownloadItemModel(download_item_
)
151 .SetShouldPreferOpeningInBrowser(is_checked
);
155 base::FilePath path
= download_item_
->GetTargetFilePath();
157 prefs
->DisableAutoOpenBasedOnExtension(path
);
159 prefs
->EnableAutoOpenBasedOnExtension(path
);
163 DownloadItemModel(download_item_
).OpenUsingPlatformHandler();
166 download_item_
->Cancel(true /* Cancelled by user */);
169 download_item_
->Remove();
172 download_item_
->ValidateDangerousDownload();
174 case LEARN_MORE_SCANNING
: {
175 #if defined(FULL_SAFE_BROWSING)
176 using safe_browsing::DownloadProtectionService
;
178 SafeBrowsingService
* sb_service
=
179 g_browser_process
->safe_browsing_service();
180 DownloadProtectionService
* protection_service
=
181 (sb_service
? sb_service
->download_protection_service() : nullptr);
182 if (protection_service
)
183 protection_service
->ShowDetailsForDownload(*download_item_
,
186 // Should only be getting invoked if we are using safe browsing.
191 case LEARN_MORE_INTERRUPTED
:
192 GetBrowser()->OpenURL(content::OpenURLParams(
193 GURL(chrome::kDownloadInterruptedLearnMoreURL
), content::Referrer(),
194 NEW_FOREGROUND_TAB
, ui::PAGE_TRANSITION_LINK
, false));
197 download_item_
->Pause();
200 download_item_
->Resume();
203 if (download_item_
->CanResume()) {
204 download_item_
->Resume();
206 // TODO(yoshiki): Implement retry logic.
212 Browser
* DownloadCommands::GetBrowser() const {
214 Profile::FromBrowserContext(download_item_
->GetBrowserContext());
215 chrome::ScopedTabbedBrowserDisplayer
browser_displayer(
216 profile
, chrome::GetActiveDesktop());
217 DCHECK(browser_displayer
.browser());
218 return browser_displayer
.browser();
221 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)
222 bool DownloadCommands::IsDownloadPdf() const {
223 base::FilePath path
= download_item_
->GetTargetFilePath();
224 return path
.MatchesExtension(FILE_PATH_LITERAL(".pdf"));
228 bool DownloadCommands::CanOpenPdfInSystemViewer() const {
230 bool is_adobe_pdf_reader_up_to_date
= false;
231 if (IsDownloadPdf() && IsAdobeReaderDefaultPDFViewer()) {
232 is_adobe_pdf_reader_up_to_date
=
233 DownloadTargetDeterminer::IsAdobeReaderUpToDate();
235 return IsDownloadPdf() &&
236 (IsAdobeReaderDefaultPDFViewer() ? is_adobe_pdf_reader_up_to_date
238 #elif defined(OS_MACOSX) || defined(OS_LINUX)
239 return IsDownloadPdf();