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_RESUME
;
43 return IDR_DOWNLOAD_NOTIFICATION_MENU_FOLDER
;
45 return IDR_DOWNLOAD_NOTIFICATION_MENU_DOWNLOAD
;
47 return IDR_DOWNLOAD_NOTIFICATION_MENU_DELETE
;
49 // TODO(yoshiki): This is a temporary image for Download Notification
50 // feature behind the flag. We have to replace the image with proper one
51 // before the feature launch. http://crbug.com/468559
52 return IDR_DOWNLOAD_NOTIFICATION_MENU_DELETE
;
53 case OPEN_WHEN_COMPLETE
:
54 case ALWAYS_OPEN_TYPE
:
56 case LEARN_MORE_SCANNING
:
57 case LEARN_MORE_INTERRUPTED
:
64 GURL
DownloadCommands::GetLearnMoreURLForInterruptedDownload() const {
65 GURL
learn_more_url(chrome::kDownloadInterruptedLearnMoreURL
);
66 learn_more_url
= google_util::AppendGoogleLocaleParam(
67 learn_more_url
, g_browser_process
->GetApplicationLocale());
68 return net::AppendQueryParameter(
69 learn_more_url
, "ctx",
70 base::IntToString(static_cast<int>(download_item_
->GetLastReason())));
73 gfx::Image
DownloadCommands::GetCommandIcon(Command command
) {
74 ResourceBundle
& bundle
= ResourceBundle::GetSharedInstance();
75 return bundle
.GetImageNamed(GetCommandIconId(command
));
78 bool DownloadCommands::IsCommandEnabled(Command command
) const {
81 return download_item_
->CanShowInFolder();
82 case OPEN_WHEN_COMPLETE
:
84 return download_item_
->CanOpenDownload() &&
85 !download_crx_util::IsExtensionDownload(*download_item_
);
86 case ALWAYS_OPEN_TYPE
:
87 // For temporary downloads, the target filename might be a temporary
88 // filename. Don't base an "Always open" decision based on it. Also
89 // exclude extensions.
90 return download_item_
->CanOpenDownload() &&
91 download_util::IsAllowedToOpenAutomatically(
92 download_item_
->GetTargetFilePath()) &&
93 !download_crx_util::IsExtensionDownload(*download_item_
);
95 return !download_item_
->IsDone();
97 return !download_item_
->IsDone() && !download_item_
->IsPaused() &&
98 download_item_
->GetState() == content::DownloadItem::IN_PROGRESS
;
100 return download_item_
->CanResume() &&
101 (download_item_
->IsPaused() ||
102 download_item_
->GetState() != content::DownloadItem::IN_PROGRESS
);
105 case LEARN_MORE_SCANNING
:
106 case LEARN_MORE_INTERRUPTED
:
113 bool DownloadCommands::IsCommandChecked(Command command
) const {
115 case OPEN_WHEN_COMPLETE
:
116 return download_item_
->GetOpenWhenComplete() ||
117 download_crx_util::IsExtensionDownload(*download_item_
);
118 case ALWAYS_OPEN_TYPE
:
119 #if defined(OS_WIN) || defined(OS_LINUX) || \
120 (defined(OS_MACOSX) && !defined(OS_IOS))
121 if (CanOpenPdfInSystemViewer()) {
122 DownloadPrefs
* prefs
= DownloadPrefs::FromBrowserContext(
123 download_item_
->GetBrowserContext());
124 return prefs
->ShouldOpenPdfInSystemReader();
127 return download_item_
->ShouldOpenFileBasedOnExtension();
130 return download_item_
->IsPaused();
136 case LEARN_MORE_SCANNING
:
137 case LEARN_MORE_INTERRUPTED
:
143 bool DownloadCommands::IsCommandVisible(Command command
) const {
144 if (command
== PLATFORM_OPEN
)
145 return (DownloadItemModel(download_item_
).ShouldPreferOpeningInBrowser());
150 void DownloadCommands::ExecuteCommand(Command command
) {
153 download_item_
->ShowDownloadInShell();
155 case OPEN_WHEN_COMPLETE
:
156 download_item_
->OpenDownload();
158 case ALWAYS_OPEN_TYPE
: {
159 bool is_checked
= IsCommandChecked(ALWAYS_OPEN_TYPE
);
160 DownloadPrefs
* prefs
= DownloadPrefs::FromBrowserContext(
161 download_item_
->GetBrowserContext());
162 #if defined(OS_WIN) || defined(OS_LINUX) || \
163 (defined(OS_MACOSX) && !defined(OS_IOS))
164 if (CanOpenPdfInSystemViewer()) {
165 prefs
->SetShouldOpenPdfInSystemReader(!is_checked
);
166 DownloadItemModel(download_item_
)
167 .SetShouldPreferOpeningInBrowser(is_checked
);
171 base::FilePath path
= download_item_
->GetTargetFilePath();
173 prefs
->DisableAutoOpenBasedOnExtension(path
);
175 prefs
->EnableAutoOpenBasedOnExtension(path
);
179 DownloadItemModel(download_item_
).OpenUsingPlatformHandler();
182 download_item_
->Cancel(true /* Cancelled by user */);
185 download_item_
->Remove();
188 download_item_
->ValidateDangerousDownload();
190 case LEARN_MORE_SCANNING
: {
191 #if defined(FULL_SAFE_BROWSING)
192 using safe_browsing::DownloadProtectionService
;
194 SafeBrowsingService
* sb_service
=
195 g_browser_process
->safe_browsing_service();
196 DownloadProtectionService
* protection_service
=
197 (sb_service
? sb_service
->download_protection_service() : nullptr);
198 if (protection_service
)
199 protection_service
->ShowDetailsForDownload(*download_item_
,
202 // Should only be getting invoked if we are using safe browsing.
207 case LEARN_MORE_INTERRUPTED
:
208 GetBrowser()->OpenURL(content::OpenURLParams(
209 GetLearnMoreURLForInterruptedDownload(), content::Referrer(),
210 NEW_FOREGROUND_TAB
, ui::PAGE_TRANSITION_LINK
, false));
213 download_item_
->Pause();
216 download_item_
->Resume();
221 Browser
* DownloadCommands::GetBrowser() const {
223 Profile::FromBrowserContext(download_item_
->GetBrowserContext());
224 chrome::ScopedTabbedBrowserDisplayer
browser_displayer(
225 profile
, chrome::GetActiveDesktop());
226 DCHECK(browser_displayer
.browser());
227 return browser_displayer
.browser();
230 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)
231 bool DownloadCommands::IsDownloadPdf() const {
232 base::FilePath path
= download_item_
->GetTargetFilePath();
233 return path
.MatchesExtension(FILE_PATH_LITERAL(".pdf"));
237 bool DownloadCommands::CanOpenPdfInSystemViewer() const {
239 bool is_adobe_pdf_reader_up_to_date
= false;
240 if (IsDownloadPdf() && IsAdobeReaderDefaultPDFViewer()) {
241 is_adobe_pdf_reader_up_to_date
=
242 DownloadTargetDeterminer::IsAdobeReaderUpToDate();
244 return IsDownloadPdf() &&
245 (IsAdobeReaderDefaultPDFViewer() ? is_adobe_pdf_reader_up_to_date
247 #elif defined(OS_MACOSX) || defined(OS_LINUX)
248 return IsDownloadPdf();