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
;
46 // TODO(yoshiki): This is a temporary image for Download Notification
47 // feature behind the flag. We have to replace the image with proper one
48 // before the feature launch. http://crbug.com/468559
49 return IDR_DOWNLOAD_NOTIFICATION_MENU_DELETE
;
50 case OPEN_WHEN_COMPLETE
:
51 case ALWAYS_OPEN_TYPE
:
53 case LEARN_MORE_SCANNING
:
54 case LEARN_MORE_INTERRUPTED
:
61 gfx::Image
DownloadCommands::GetCommandIcon(Command command
) {
62 ResourceBundle
& bundle
= ResourceBundle::GetSharedInstance();
63 return bundle
.GetImageNamed(GetCommandIconId(command
));
66 bool DownloadCommands::IsCommandEnabled(Command command
) const {
69 return download_item_
->CanShowInFolder();
70 case OPEN_WHEN_COMPLETE
:
72 return download_item_
->CanOpenDownload() &&
73 !download_crx_util::IsExtensionDownload(*download_item_
);
74 case ALWAYS_OPEN_TYPE
:
75 // For temporary downloads, the target filename might be a temporary
76 // filename. Don't base an "Always open" decision based on it. Also
77 // exclude extensions.
78 return download_item_
->CanOpenDownload() &&
79 !download_crx_util::IsExtensionDownload(*download_item_
);
81 return !download_item_
->IsDone();
83 return !download_item_
->IsDone() && !download_item_
->IsPaused() &&
84 download_item_
->GetState() == content::DownloadItem::IN_PROGRESS
;
86 return download_item_
->CanResume() &&
87 (download_item_
->IsPaused() ||
88 download_item_
->GetState() != content::DownloadItem::IN_PROGRESS
);
91 case LEARN_MORE_SCANNING
:
92 case LEARN_MORE_INTERRUPTED
:
100 bool DownloadCommands::IsCommandChecked(Command command
) const {
102 case OPEN_WHEN_COMPLETE
:
103 return download_item_
->GetOpenWhenComplete() ||
104 download_crx_util::IsExtensionDownload(*download_item_
);
105 case ALWAYS_OPEN_TYPE
:
106 #if defined(OS_WIN) || defined(OS_LINUX) || \
107 (defined(OS_MACOSX) && !defined(OS_IOS))
108 if (CanOpenPdfInSystemViewer()) {
109 DownloadPrefs
* prefs
= DownloadPrefs::FromBrowserContext(
110 download_item_
->GetBrowserContext());
111 return prefs
->ShouldOpenPdfInSystemReader();
114 return download_item_
->ShouldOpenFileBasedOnExtension();
117 return download_item_
->IsPaused();
124 case LEARN_MORE_SCANNING
:
125 case LEARN_MORE_INTERRUPTED
:
131 bool DownloadCommands::IsCommandVisible(Command command
) const {
132 if (command
== PLATFORM_OPEN
)
133 return (DownloadItemModel(download_item_
).ShouldPreferOpeningInBrowser());
138 void DownloadCommands::ExecuteCommand(Command command
) {
141 download_item_
->ShowDownloadInShell();
143 case OPEN_WHEN_COMPLETE
:
144 download_item_
->OpenDownload();
146 case ALWAYS_OPEN_TYPE
: {
147 bool is_checked
= IsCommandChecked(ALWAYS_OPEN_TYPE
);
148 DownloadPrefs
* prefs
= DownloadPrefs::FromBrowserContext(
149 download_item_
->GetBrowserContext());
150 #if defined(OS_WIN) || defined(OS_LINUX) || \
151 (defined(OS_MACOSX) && !defined(OS_IOS))
152 if (CanOpenPdfInSystemViewer()) {
153 prefs
->SetShouldOpenPdfInSystemReader(!is_checked
);
154 DownloadItemModel(download_item_
)
155 .SetShouldPreferOpeningInBrowser(is_checked
);
159 base::FilePath path
= download_item_
->GetTargetFilePath();
161 prefs
->DisableAutoOpenBasedOnExtension(path
);
163 prefs
->EnableAutoOpenBasedOnExtension(path
);
167 DownloadItemModel(download_item_
).OpenUsingPlatformHandler();
170 download_item_
->Cancel(true /* Cancelled by user */);
173 download_item_
->Remove();
176 download_item_
->ValidateDangerousDownload();
178 case LEARN_MORE_SCANNING
: {
179 #if defined(FULL_SAFE_BROWSING)
180 using safe_browsing::DownloadProtectionService
;
182 SafeBrowsingService
* sb_service
=
183 g_browser_process
->safe_browsing_service();
184 DownloadProtectionService
* protection_service
=
185 (sb_service
? sb_service
->download_protection_service() : nullptr);
186 if (protection_service
)
187 protection_service
->ShowDetailsForDownload(*download_item_
,
190 // Should only be getting invoked if we are using safe browsing.
195 case LEARN_MORE_INTERRUPTED
:
196 GetBrowser()->OpenURL(content::OpenURLParams(
197 GURL(chrome::kDownloadInterruptedLearnMoreURL
), content::Referrer(),
198 NEW_FOREGROUND_TAB
, ui::PAGE_TRANSITION_LINK
, false));
201 download_item_
->Pause();
204 download_item_
->Resume();
207 if (download_item_
->CanResume()) {
208 download_item_
->Resume();
210 // TODO(yoshiki): Implement retry logic.
216 Browser
* DownloadCommands::GetBrowser() const {
218 Profile::FromBrowserContext(download_item_
->GetBrowserContext());
219 chrome::ScopedTabbedBrowserDisplayer
browser_displayer(
220 profile
, chrome::GetActiveDesktop());
221 DCHECK(browser_displayer
.browser());
222 return browser_displayer
.browser();
225 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)
226 bool DownloadCommands::IsDownloadPdf() const {
227 base::FilePath path
= download_item_
->GetTargetFilePath();
228 return path
.MatchesExtension(FILE_PATH_LITERAL(".pdf"));
232 bool DownloadCommands::CanOpenPdfInSystemViewer() const {
234 bool is_adobe_pdf_reader_up_to_date
= false;
235 if (IsDownloadPdf() && IsAdobeReaderDefaultPDFViewer()) {
236 is_adobe_pdf_reader_up_to_date
=
237 DownloadTargetDeterminer::IsAdobeReaderUpToDate();
239 return IsDownloadPdf() &&
240 (IsAdobeReaderDefaultPDFViewer() ? is_adobe_pdf_reader_up_to_date
242 #elif defined(OS_MACOSX) || defined(OS_LINUX)
243 return IsDownloadPdf();