Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / download / download_commands.cc
blobabc6a05ee7c0c41d558debe5cb1da3590c0fe719
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"
26 #if defined(OS_WIN)
27 #include "chrome/browser/download/download_target_determiner.h"
28 #include "chrome/browser/ui/pdf/adobe_reader_info_win.h"
29 #endif
31 DownloadCommands::DownloadCommands(content::DownloadItem* download_item)
32 : download_item_(download_item) {
33 DCHECK(download_item);
36 int DownloadCommands::GetCommandIconId(Command command) const {
37 switch (command) {
38 case PAUSE:
39 return IDR_DOWNLOAD_NOTIFICATION_MENU_PAUSE;
40 case RESUME:
41 return IDR_DOWNLOAD_NOTIFICATION_MENU_DOWNLOAD;
42 case SHOW_IN_FOLDER:
43 return IDR_DOWNLOAD_NOTIFICATION_MENU_FOLDER;
44 case KEEP:
45 return IDR_DOWNLOAD_NOTIFICATION_MENU_DOWNLOAD;
46 case DISCARD:
47 return IDR_DOWNLOAD_NOTIFICATION_MENU_DELETE;
48 case CANCEL:
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:
54 case PLATFORM_OPEN:
55 case LEARN_MORE_INTERRUPTED:
56 return -1;
58 NOTREACHED();
59 return -1;
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 {
77 switch (command) {
78 case SHOW_IN_FOLDER:
79 return download_item_->CanShowInFolder();
80 case OPEN_WHEN_COMPLETE:
81 case PLATFORM_OPEN:
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_);
92 case CANCEL:
93 return !download_item_->IsDone();
94 case PAUSE:
95 return !download_item_->IsDone() && !download_item_->IsPaused() &&
96 download_item_->GetState() == content::DownloadItem::IN_PROGRESS;
97 case RESUME:
98 return download_item_->CanResume() &&
99 (download_item_->IsPaused() ||
100 download_item_->GetState() != content::DownloadItem::IN_PROGRESS);
101 case DISCARD:
102 case KEEP:
103 case LEARN_MORE_SCANNING:
104 case LEARN_MORE_INTERRUPTED:
105 return true;
107 NOTREACHED();
108 return false;
111 bool DownloadCommands::IsCommandChecked(Command command) const {
112 switch (command) {
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();
124 #endif
125 return download_item_->ShouldOpenFileBasedOnExtension();
126 case PAUSE:
127 case RESUME:
128 return download_item_->IsPaused();
129 case SHOW_IN_FOLDER:
130 case PLATFORM_OPEN:
131 case CANCEL:
132 case DISCARD:
133 case KEEP:
134 case LEARN_MORE_SCANNING:
135 case LEARN_MORE_INTERRUPTED:
136 return false;
138 return false;
141 bool DownloadCommands::IsCommandVisible(Command command) const {
142 if (command == PLATFORM_OPEN)
143 return (DownloadItemModel(download_item_).ShouldPreferOpeningInBrowser());
145 return true;
148 void DownloadCommands::ExecuteCommand(Command command) {
149 switch (command) {
150 case SHOW_IN_FOLDER:
151 download_item_->ShowDownloadInShell();
152 break;
153 case OPEN_WHEN_COMPLETE:
154 download_item_->OpenDownload();
155 break;
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);
166 break;
168 #endif
169 base::FilePath path = download_item_->GetTargetFilePath();
170 if (is_checked)
171 prefs->DisableAutoOpenBasedOnExtension(path);
172 else
173 prefs->EnableAutoOpenBasedOnExtension(path);
174 break;
176 case PLATFORM_OPEN:
177 DownloadItemModel(download_item_).OpenUsingPlatformHandler();
178 break;
179 case CANCEL:
180 download_item_->Cancel(true /* Cancelled by user */);
181 break;
182 case DISCARD:
183 download_item_->Remove();
184 break;
185 case KEEP:
186 download_item_->ValidateDangerousDownload();
187 break;
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_,
198 GetBrowser());
199 #else
200 // Should only be getting invoked if we are using safe browsing.
201 NOTREACHED();
202 #endif
203 break;
205 case LEARN_MORE_INTERRUPTED:
206 GetBrowser()->OpenURL(content::OpenURLParams(
207 GetLearnMoreURLForInterruptedDownload(), content::Referrer(),
208 NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_LINK, false));
209 break;
210 case PAUSE:
211 download_item_->Pause();
212 break;
213 case RESUME:
214 download_item_->Resume();
215 break;
219 Browser* DownloadCommands::GetBrowser() const {
220 Profile* profile =
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"));
233 #endif
235 bool DownloadCommands::CanOpenPdfInSystemViewer() const {
236 #if defined(OS_WIN)
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
244 : true);
245 #elif defined(OS_MACOSX) || defined(OS_LINUX)
246 return IsDownloadPdf();
247 #endif