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 "base/strings/string_number_conversions.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/download/download_crx_util.h"
10 #include "chrome/browser/download/download_extensions.h"
11 #include "chrome/browser/download/download_item_model.h"
12 #include "chrome/browser/download/download_prefs.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/browser/safe_browsing/download_protection_service.h"
15 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
16 #include "chrome/browser/ui/browser_finder.h"
17 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
18 #include "chrome/common/url_constants.h"
19 #include "chrome/grit/generated_resources.h"
20 #include "components/google/core/browser/google_util.h"
21 #include "grit/theme_resources.h"
22 #include "net/base/url_util.h"
23 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/base/resource/resource_bundle.h"
27 #include "chrome/browser/download/download_target_determiner.h"
28 #include "chrome/browser/ui/pdf/adobe_reader_info_win.h"
31 DownloadCommands::DownloadCommands(content::DownloadItem
* download_item
)
32 : download_item_(download_item
) {
33 DCHECK(download_item
);
36 int DownloadCommands::GetCommandIconId(Command command
) const {
39 return IDR_DOWNLOAD_NOTIFICATION_MENU_PAUSE
;
41 return IDR_DOWNLOAD_NOTIFICATION_MENU_DOWNLOAD
;
43 return IDR_DOWNLOAD_NOTIFICATION_MENU_FOLDER
;
45 return IDR_DOWNLOAD_NOTIFICATION_MENU_DOWNLOAD
;
47 return IDR_DOWNLOAD_NOTIFICATION_MENU_DELETE
;
49 return IDR_DOWNLOAD_NOTIFICATION_MENU_CANCEL
;
50 case LEARN_MORE_SCANNING
:
51 return IDR_NOTIFICATION_WELCOME_LEARN_MORE
;
52 case OPEN_WHEN_COMPLETE
:
53 case ALWAYS_OPEN_TYPE
:
55 case LEARN_MORE_INTERRUPTED
:
62 GURL
DownloadCommands::GetLearnMoreURLForInterruptedDownload() const {
63 GURL
learn_more_url(chrome::kDownloadInterruptedLearnMoreURL
);
64 learn_more_url
= google_util::AppendGoogleLocaleParam(
65 learn_more_url
, g_browser_process
->GetApplicationLocale());
66 return net::AppendQueryParameter(
67 learn_more_url
, "ctx",
68 base::IntToString(static_cast<int>(download_item_
->GetLastReason())));
71 gfx::Image
DownloadCommands::GetCommandIcon(Command command
) {
72 ResourceBundle
& bundle
= ResourceBundle::GetSharedInstance();
73 return bundle
.GetImageNamed(GetCommandIconId(command
));
76 bool DownloadCommands::IsCommandEnabled(Command command
) const {
79 return download_item_
->CanShowInFolder();
80 case OPEN_WHEN_COMPLETE
:
82 return download_item_
->CanOpenDownload() &&
83 !download_crx_util::IsExtensionDownload(*download_item_
);
84 case ALWAYS_OPEN_TYPE
:
85 // For temporary downloads, the target filename might be a temporary
86 // filename. Don't base an "Always open" decision based on it. Also
87 // exclude extensions.
88 return download_item_
->CanOpenDownload() &&
89 download_util::IsAllowedToOpenAutomatically(
90 download_item_
->GetTargetFilePath()) &&
91 !download_crx_util::IsExtensionDownload(*download_item_
);
93 return !download_item_
->IsDone();
95 return !download_item_
->IsDone() && !download_item_
->IsPaused() &&
96 download_item_
->GetState() == content::DownloadItem::IN_PROGRESS
;
98 return download_item_
->CanResume() &&
99 (download_item_
->IsPaused() ||
100 download_item_
->GetState() != content::DownloadItem::IN_PROGRESS
);
103 case LEARN_MORE_SCANNING
:
104 case LEARN_MORE_INTERRUPTED
:
111 bool DownloadCommands::IsCommandChecked(Command command
) const {
113 case OPEN_WHEN_COMPLETE
:
114 return download_item_
->GetOpenWhenComplete() ||
115 download_crx_util::IsExtensionDownload(*download_item_
);
116 case ALWAYS_OPEN_TYPE
:
117 #if defined(OS_WIN) || defined(OS_LINUX) || \
118 (defined(OS_MACOSX) && !defined(OS_IOS))
119 if (CanOpenPdfInSystemViewer()) {
120 DownloadPrefs
* prefs
= DownloadPrefs::FromBrowserContext(
121 download_item_
->GetBrowserContext());
122 return prefs
->ShouldOpenPdfInSystemReader();
125 return download_item_
->ShouldOpenFileBasedOnExtension();
128 return download_item_
->IsPaused();
134 case LEARN_MORE_SCANNING
:
135 case LEARN_MORE_INTERRUPTED
:
141 bool DownloadCommands::IsCommandVisible(Command command
) const {
142 if (command
== PLATFORM_OPEN
)
143 return (DownloadItemModel(download_item_
).ShouldPreferOpeningInBrowser());
148 void DownloadCommands::ExecuteCommand(Command command
) {
151 download_item_
->ShowDownloadInShell();
153 case OPEN_WHEN_COMPLETE
:
154 download_item_
->OpenDownload();
156 case ALWAYS_OPEN_TYPE
: {
157 bool is_checked
= IsCommandChecked(ALWAYS_OPEN_TYPE
);
158 DownloadPrefs
* prefs
= DownloadPrefs::FromBrowserContext(
159 download_item_
->GetBrowserContext());
160 #if defined(OS_WIN) || defined(OS_LINUX) || \
161 (defined(OS_MACOSX) && !defined(OS_IOS))
162 if (CanOpenPdfInSystemViewer()) {
163 prefs
->SetShouldOpenPdfInSystemReader(!is_checked
);
164 DownloadItemModel(download_item_
)
165 .SetShouldPreferOpeningInBrowser(is_checked
);
169 base::FilePath path
= download_item_
->GetTargetFilePath();
171 prefs
->DisableAutoOpenBasedOnExtension(path
);
173 prefs
->EnableAutoOpenBasedOnExtension(path
);
177 DownloadItemModel(download_item_
).OpenUsingPlatformHandler();
180 download_item_
->Cancel(true /* Cancelled by user */);
183 download_item_
->Remove();
186 download_item_
->ValidateDangerousDownload();
188 case LEARN_MORE_SCANNING
: {
189 #if defined(FULL_SAFE_BROWSING)
190 using safe_browsing::DownloadProtectionService
;
192 SafeBrowsingService
* sb_service
=
193 g_browser_process
->safe_browsing_service();
194 DownloadProtectionService
* protection_service
=
195 (sb_service
? sb_service
->download_protection_service() : nullptr);
196 if (protection_service
)
197 protection_service
->ShowDetailsForDownload(*download_item_
,
200 // Should only be getting invoked if we are using safe browsing.
205 case LEARN_MORE_INTERRUPTED
:
206 GetBrowser()->OpenURL(content::OpenURLParams(
207 GetLearnMoreURLForInterruptedDownload(), content::Referrer(),
208 NEW_FOREGROUND_TAB
, ui::PAGE_TRANSITION_LINK
, false));
211 download_item_
->Pause();
214 download_item_
->Resume();
219 Browser
* DownloadCommands::GetBrowser() const {
221 Profile::FromBrowserContext(download_item_
->GetBrowserContext());
222 chrome::ScopedTabbedBrowserDisplayer
browser_displayer(
223 profile
, chrome::GetActiveDesktop());
224 DCHECK(browser_displayer
.browser());
225 return browser_displayer
.browser();
228 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)
229 bool DownloadCommands::IsDownloadPdf() const {
230 base::FilePath path
= download_item_
->GetTargetFilePath();
231 return path
.MatchesExtension(FILE_PATH_LITERAL(".pdf"));
235 bool DownloadCommands::CanOpenPdfInSystemViewer() const {
237 bool is_adobe_pdf_reader_up_to_date
= false;
238 if (IsDownloadPdf() && IsAdobeReaderDefaultPDFViewer()) {
239 is_adobe_pdf_reader_up_to_date
=
240 DownloadTargetDeterminer::IsAdobeReaderUpToDate();
242 return IsDownloadPdf() &&
243 (IsAdobeReaderDefaultPDFViewer() ? is_adobe_pdf_reader_up_to_date
245 #elif defined(OS_MACOSX) || defined(OS_LINUX)
246 return IsDownloadPdf();