[Download Notification] Use NotificationUIManager instead of MessageCenter
[chromium-blink-merge.git] / chrome / browser / download / download_commands.cc
blob97784977e39b20f3f88b82ed0210bc26bebca988
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"
22 #if defined(OS_WIN)
23 #include "chrome/browser/download/download_target_determiner.h"
24 #include "chrome/browser/ui/pdf/adobe_reader_info_win.h"
25 #endif
27 DownloadCommands::DownloadCommands(content::DownloadItem* download_item)
28 : download_item_(download_item) {
29 DCHECK(download_item);
32 int DownloadCommands::GetCommandIconId(Command command) {
33 switch (command) {
34 case PAUSE:
35 return IDR_DOWNLOAD_NOTIFICATION_MENU_PAUSE;
36 case RESUME:
37 return IDR_DOWNLOAD_NOTIFICATION_MENU_RESUME;
38 case SHOW_IN_FOLDER:
39 return IDR_DOWNLOAD_NOTIFICATION_MENU_FOLDER;
40 case RETRY:
41 case KEEP:
42 return IDR_DOWNLOAD_NOTIFICATION_MENU_DOWNLOAD;
43 case DISCARD:
44 return IDR_DOWNLOAD_NOTIFICATION_MENU_DELETE;
45 case CANCEL:
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:
52 case PLATFORM_OPEN:
53 case LEARN_MORE_SCANNING:
54 case LEARN_MORE_INTERRUPTED:
55 return -1;
57 NOTREACHED();
58 return -1;
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 {
67 switch (command) {
68 case SHOW_IN_FOLDER:
69 return download_item_->CanShowInFolder();
70 case OPEN_WHEN_COMPLETE:
71 case PLATFORM_OPEN:
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_);
80 case CANCEL:
81 return !download_item_->IsDone();
82 case PAUSE:
83 return !download_item_->IsDone() && !download_item_->IsPaused() &&
84 download_item_->GetState() == content::DownloadItem::IN_PROGRESS;
85 case RESUME:
86 return download_item_->CanResume() &&
87 (download_item_->IsPaused() ||
88 download_item_->GetState() != content::DownloadItem::IN_PROGRESS);
89 case DISCARD:
90 case KEEP:
91 case LEARN_MORE_SCANNING:
92 case LEARN_MORE_INTERRUPTED:
93 case RETRY:
94 return true;
96 NOTREACHED();
97 return false;
100 bool DownloadCommands::IsCommandChecked(Command command) const {
101 switch (command) {
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();
113 #endif
114 return download_item_->ShouldOpenFileBasedOnExtension();
115 case PAUSE:
116 case RESUME:
117 return download_item_->IsPaused();
118 case SHOW_IN_FOLDER:
119 case PLATFORM_OPEN:
120 case CANCEL:
121 case DISCARD:
122 case KEEP:
123 case RETRY:
124 case LEARN_MORE_SCANNING:
125 case LEARN_MORE_INTERRUPTED:
126 return false;
128 return false;
131 bool DownloadCommands::IsCommandVisible(Command command) const {
132 if (command == PLATFORM_OPEN)
133 return (DownloadItemModel(download_item_).ShouldPreferOpeningInBrowser());
135 return true;
138 void DownloadCommands::ExecuteCommand(Command command) {
139 switch (command) {
140 case SHOW_IN_FOLDER:
141 download_item_->ShowDownloadInShell();
142 break;
143 case OPEN_WHEN_COMPLETE:
144 download_item_->OpenDownload();
145 break;
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);
156 break;
158 #endif
159 base::FilePath path = download_item_->GetTargetFilePath();
160 if (is_checked)
161 prefs->DisableAutoOpenBasedOnExtension(path);
162 else
163 prefs->EnableAutoOpenBasedOnExtension(path);
164 break;
166 case PLATFORM_OPEN:
167 DownloadItemModel(download_item_).OpenUsingPlatformHandler();
168 break;
169 case CANCEL:
170 download_item_->Cancel(true /* Cancelled by user */);
171 break;
172 case DISCARD:
173 download_item_->Remove();
174 break;
175 case KEEP:
176 download_item_->ValidateDangerousDownload();
177 break;
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_,
188 GetBrowser());
189 #else
190 // Should only be getting invoked if we are using safe browsing.
191 NOTREACHED();
192 #endif
193 break;
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));
199 break;
200 case PAUSE:
201 download_item_->Pause();
202 break;
203 case RESUME:
204 download_item_->Resume();
205 break;
206 case RETRY:
207 if (download_item_->CanResume()) {
208 download_item_->Resume();
209 } else {
210 // TODO(yoshiki): Implement retry logic.
212 break;
216 Browser* DownloadCommands::GetBrowser() const {
217 Profile* profile =
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"));
230 #endif
232 bool DownloadCommands::CanOpenPdfInSystemViewer() const {
233 #if defined(OS_WIN)
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
241 : true);
242 #elif defined(OS_MACOSX) || defined(OS_LINUX)
243 return IsDownloadPdf();
244 #endif