Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / download / download_commands.cc
blob332129a97a7f0a41bb987e5ce65b2794ce018485
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_RESUME;
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 // 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:
55 case PLATFORM_OPEN:
56 case LEARN_MORE_SCANNING:
57 case LEARN_MORE_INTERRUPTED:
58 return -1;
60 NOTREACHED();
61 return -1;
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 {
79 switch (command) {
80 case SHOW_IN_FOLDER:
81 return download_item_->CanShowInFolder();
82 case OPEN_WHEN_COMPLETE:
83 case PLATFORM_OPEN:
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_);
94 case CANCEL:
95 return !download_item_->IsDone();
96 case PAUSE:
97 return !download_item_->IsDone() && !download_item_->IsPaused() &&
98 download_item_->GetState() == content::DownloadItem::IN_PROGRESS;
99 case RESUME:
100 return download_item_->CanResume() &&
101 (download_item_->IsPaused() ||
102 download_item_->GetState() != content::DownloadItem::IN_PROGRESS);
103 case DISCARD:
104 case KEEP:
105 case LEARN_MORE_SCANNING:
106 case LEARN_MORE_INTERRUPTED:
107 return true;
109 NOTREACHED();
110 return false;
113 bool DownloadCommands::IsCommandChecked(Command command) const {
114 switch (command) {
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();
126 #endif
127 return download_item_->ShouldOpenFileBasedOnExtension();
128 case PAUSE:
129 case RESUME:
130 return download_item_->IsPaused();
131 case SHOW_IN_FOLDER:
132 case PLATFORM_OPEN:
133 case CANCEL:
134 case DISCARD:
135 case KEEP:
136 case LEARN_MORE_SCANNING:
137 case LEARN_MORE_INTERRUPTED:
138 return false;
140 return false;
143 bool DownloadCommands::IsCommandVisible(Command command) const {
144 if (command == PLATFORM_OPEN)
145 return (DownloadItemModel(download_item_).ShouldPreferOpeningInBrowser());
147 return true;
150 void DownloadCommands::ExecuteCommand(Command command) {
151 switch (command) {
152 case SHOW_IN_FOLDER:
153 download_item_->ShowDownloadInShell();
154 break;
155 case OPEN_WHEN_COMPLETE:
156 download_item_->OpenDownload();
157 break;
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);
168 break;
170 #endif
171 base::FilePath path = download_item_->GetTargetFilePath();
172 if (is_checked)
173 prefs->DisableAutoOpenBasedOnExtension(path);
174 else
175 prefs->EnableAutoOpenBasedOnExtension(path);
176 break;
178 case PLATFORM_OPEN:
179 DownloadItemModel(download_item_).OpenUsingPlatformHandler();
180 break;
181 case CANCEL:
182 download_item_->Cancel(true /* Cancelled by user */);
183 break;
184 case DISCARD:
185 download_item_->Remove();
186 break;
187 case KEEP:
188 download_item_->ValidateDangerousDownload();
189 break;
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_,
200 GetBrowser());
201 #else
202 // Should only be getting invoked if we are using safe browsing.
203 NOTREACHED();
204 #endif
205 break;
207 case LEARN_MORE_INTERRUPTED:
208 GetBrowser()->OpenURL(content::OpenURLParams(
209 GetLearnMoreURLForInterruptedDownload(), content::Referrer(),
210 NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_LINK, false));
211 break;
212 case PAUSE:
213 download_item_->Pause();
214 break;
215 case RESUME:
216 download_item_->Resume();
217 break;
221 Browser* DownloadCommands::GetBrowser() const {
222 Profile* profile =
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"));
235 #endif
237 bool DownloadCommands::CanOpenPdfInSystemViewer() const {
238 #if defined(OS_WIN)
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
246 : true);
247 #elif defined(OS_MACOSX) || defined(OS_LINUX)
248 return IsDownloadPdf();
249 #endif