Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(126)

Side by Side Diff: chrome/browser/download/download_command.cc

Issue 852043002: Initial Implementation of Download Notification (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Minor bug fix. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/download/download_command.h"
6
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/safe_browsing/download_protection_service.h"
12 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
13 #include "chrome/common/url_constants.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "grit/theme_resources.h"
16 #include "ui/base/l10n/l10n_util.h"
17
18 #if defined(OS_WIN)
19 #include "chrome/browser/download/download_target_determiner.h"
20 #include "chrome/browser/ui/pdf/adobe_reader_info_win.h"
21 #endif
22
23 DownloadCommand::DownloadCommand(content::DownloadItem* download_item) :
24 download_item_(download_item) {}
25
26 int DownloadCommand::GetCommandIconId(Commands type) {
27 switch (type) {
28 case PAUSE:
29 return IDR_DOWNLOAD_NOTIFICATION_MENU_PAUSE;
30 case RESUME:
31 return IDR_DOWNLOAD_NOTIFICATION_MENU_RESUME;
32 case SHOW_IN_FOLDER:
33 return IDR_DOWNLOAD_NOTIFICATION_MENU_FOLDER;
34 case RETRY:
35 return IDR_DOWNLOAD_NOTIFICATION_MENU_DOWNLOAD;
36 case DISCARD:
37 return IDR_DOWNLOAD_NOTIFICATION_MENU_DELETE;
38 case OPEN_WHEN_COMPLETE:
39 case ALWAYS_OPEN_TYPE:
40 case PLATFORM_OPEN:
41 case CANCEL:
42 case KEEP:
43 case LEARN_MORE_SCANNING:
44 case LEARN_MORE_INTERRUPTED:
45 return -1;
46 }
47 NOTREACHED();
48 return -1;
49 }
50
51 int DownloadCommand::GetCommandStringId(Commands type) const {
52 return GetCommandStringIdInternal(type, false);
53 }
54
55 int DownloadCommand::GetCommandStringIdForMenu(
56 Commands type) const {
57 return GetCommandStringIdInternal(type, true);
58 }
59
60 bool DownloadCommand::IsCommandIdEnabled(Commands command) const {
61 if (!download_item_)
62 return false;
63
64 switch (command) {
65 case SHOW_IN_FOLDER:
66 return download_item_->CanShowInFolder();
67 case OPEN_WHEN_COMPLETE:
68 case PLATFORM_OPEN:
69 return download_item_->CanOpenDownload() &&
70 !download_crx_util::IsExtensionDownload(*download_item_);
71 case ALWAYS_OPEN_TYPE:
72 // For temporary downloads, the target filename might be a temporary
73 // filename. Don't base an "Always open" decision based on it. Also
74 // exclude extensions.
75 return download_item_->CanOpenDownload() &&
76 !download_crx_util::IsExtensionDownload(*download_item_);
77 case CANCEL:
78 return !download_item_->IsDone();
79 case PAUSE:
80 return !download_item_->IsDone() &&
81 !download_item_->IsPaused() &&
82 download_item_->GetState() == content::DownloadItem::IN_PROGRESS;
83 case RESUME:
84 return !download_item_->IsDone() &&
85 (download_item_->IsPaused() ||
86 download_item_->GetState() != content::DownloadItem::IN_PROGRESS);
87 case DISCARD:
88 case KEEP:
89 case LEARN_MORE_SCANNING:
90 case LEARN_MORE_INTERRUPTED:
91 case RETRY:
92 return true;
93 }
94 NOTREACHED();
95 return false;
96 }
97
98 bool DownloadCommand::IsCommandIdChecked(Commands command) const {
99 if (!download_item_)
100 return false;
101
102 switch (command) {
103 case OPEN_WHEN_COMPLETE:
104 return download_item_->GetOpenWhenComplete() ||
105 download_crx_util::IsExtensionDownload(*download_item_);
106 case ALWAYS_OPEN_TYPE:
107 #if defined(OS_WIN) || defined(OS_LINUX) || \
108 (defined(OS_MACOSX) && !defined(OS_IOS))
109 if (CanOpenPdfInSystemViewer()) {
110 DownloadPrefs* prefs = DownloadPrefs::FromBrowserContext(
111 download_item_->GetBrowserContext());
112 return prefs->ShouldOpenPdfInSystemReader();
113 }
114 #endif
115 return download_item_->ShouldOpenFileBasedOnExtension();
116 case PAUSE:
117 case RESUME:
118 return download_item_->IsPaused();
119 case SHOW_IN_FOLDER:
120 case PLATFORM_OPEN:
121 case CANCEL:
122 case DISCARD:
123 case KEEP:
124 case RETRY:
125 case LEARN_MORE_SCANNING:
126 case LEARN_MORE_INTERRUPTED:
127 return false;
128 }
129 return false;
130 }
131
132 bool DownloadCommand::IsCommandIdVisible(Commands command) const {
133 if (!download_item_)
134 return false;
135
136 if (command == PLATFORM_OPEN)
137 return (DownloadItemModel(download_item_).ShouldPreferOpeningInBrowser());
138
139 return true;
140 }
141
142 void DownloadCommand::ExecuteCommand(Commands command,
143 content::PageNavigator* navigator) {
144 if (!download_item_)
145 return;
146
147 switch (command) {
148 case SHOW_IN_FOLDER:
149 download_item_->ShowDownloadInShell();
150 break;
151 case OPEN_WHEN_COMPLETE:
152 download_item_->OpenDownload();
153 break;
154 case ALWAYS_OPEN_TYPE: {
155 bool is_checked = IsCommandIdChecked(ALWAYS_OPEN_TYPE);
156 DownloadPrefs* prefs = DownloadPrefs::FromBrowserContext(
157 download_item_->GetBrowserContext());
158 #if defined(OS_WIN) || defined(OS_LINUX) || \
159 (defined(OS_MACOSX) && !defined(OS_IOS))
160 if (CanOpenPdfInSystemViewer()) {
161 prefs->SetShouldOpenPdfInSystemReader(!is_checked);
162 DownloadItemModel(download_item_).SetShouldPreferOpeningInBrowser(
163 is_checked);
164 break;
165 }
166 #endif
167 base::FilePath path = download_item_->GetTargetFilePath();
168 if (is_checked)
169 prefs->DisableAutoOpenBasedOnExtension(path);
170 else
171 prefs->EnableAutoOpenBasedOnExtension(path);
172 break;
173 }
174 case PLATFORM_OPEN:
175 DownloadItemModel(download_item_).OpenUsingPlatformHandler();
176 break;
177 case CANCEL:
178 download_item_->Cancel(true /* Cancelled by user */);
179 break;
180 case DISCARD:
181 download_item_->Remove();
182 break;
183 case KEEP:
184 download_item_->ValidateDangerousDownload();
185 break;
186 case LEARN_MORE_SCANNING: {
187 #if defined(FULL_SAFE_BROWSING)
188 using safe_browsing::DownloadProtectionService;
189 SafeBrowsingService* sb_service =
190 g_browser_process->safe_browsing_service();
191 DownloadProtectionService* protection_service =
192 (sb_service ? sb_service->download_protection_service() : NULL);
193 if (protection_service) {
194 protection_service->ShowDetailsForDownload(*download_item_, navigator);
195 }
196 #else
197 // Should only be getting invoked if we are using safe browsing.
198 NOTREACHED();
199 #endif
200 break;
201 }
202 case LEARN_MORE_INTERRUPTED:
203 navigator->OpenURL(
204 content::OpenURLParams(GURL(chrome::kDownloadInterruptedLearnMoreURL),
205 content::Referrer(),
206 NEW_FOREGROUND_TAB,
207 ui::PAGE_TRANSITION_LINK,
208 false));
209 break;
210 case PAUSE:
211 download_item_->Pause();
212 break;
213 case RESUME:
214 download_item_->Resume();
215 break;
216 case RETRY:
217 download_item_->Resume();
218 break;
219 }
220 }
221
222 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)
223 bool DownloadCommand::IsDownloadPdf() const {
224 base::FilePath path = download_item_->GetTargetFilePath();
225 return path.MatchesExtension(FILE_PATH_LITERAL(".pdf"));
226 }
227 #endif
228
229 bool DownloadCommand::CanOpenPdfInSystemViewer() const {
230 #if defined(OS_WIN)
231 bool is_adobe_pdf_reader_up_to_date = false;
232 if (IsDownloadPdf() && IsAdobeReaderDefaultPDFViewer()) {
233 is_adobe_pdf_reader_up_to_date =
234 DownloadTargetDeterminer::IsAdobeReaderUpToDate();
235 }
236 return IsDownloadPdf() &&
237 (IsAdobeReaderDefaultPDFViewer() ? is_adobe_pdf_reader_up_to_date :
238 true);
239 #elif defined(OS_MACOSX) || defined(OS_LINUX)
240 return IsDownloadPdf();
241 #endif
242 }
243
244 int DownloadCommand::GetCommandStringIdInternal(
245 Commands type, bool for_menu) const {
246 switch (type) {
247 case OPEN_WHEN_COMPLETE:
248 if (download_item_ && !download_item_->IsDone()) {
249 return for_menu ? IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE :
250 IDS_DOWNLOAD_STATUS_OPEN_WHEN_COMPLETE;
251 } else {
252 return for_menu ? IDS_DOWNLOAD_MENU_OPEN:
253 IDS_DOWNLOAD_STATUS_OPEN_WHEN_COMPLETE;
254 }
255 case PAUSE:
256 // Only for non menu.
257 return IDS_DOWNLOAD_LINK_PAUSE;
258 case RESUME:
259 // Only for non menu.
260 return IDS_DOWNLOAD_LINK_RESUME;
261 case SHOW_IN_FOLDER:
262 return for_menu ? IDS_DOWNLOAD_MENU_SHOW : IDS_DOWNLOAD_LINK_SHOW;
263 case RETRY:
264 // Only for non menu.
265 return IDS_DOWNLOAD_LINK_RETRY;
266 case DISCARD:
267 return for_menu ? IDS_DOWNLOAD_MENU_DISCARD : IDS_DISCARD_DOWNLOAD;
268 case KEEP:
269 // Only for menu.
270 return IDS_DOWNLOAD_MENU_KEEP;
271 case ALWAYS_OPEN_TYPE:
272 // Only for menu.
273 return GetAlwaysOpenStringId();
274 case PLATFORM_OPEN:
275 // Only for menu.
276 return IDS_DOWNLOAD_MENU_PLATFORM_OPEN;
277 case CANCEL:
278 // Only for menu.
279 return IDS_DOWNLOAD_MENU_CANCEL;
280 case LEARN_MORE_SCANNING:
281 // Only for menu.
282 return IDS_DOWNLOAD_MENU_LEARN_MORE_SCANNING;
283 case LEARN_MORE_INTERRUPTED:
284 // Only for menu.
285 return IDS_DOWNLOAD_MENU_LEARN_MORE_INTERRUPTED;
286 }
287 NOTREACHED();
288 return -1;
289 }
290
291 int DownloadCommand::GetAlwaysOpenStringId() const {
292 bool can_open_pdf_in_system_viewer =
293 DownloadCommand(download_item_).CanOpenPdfInSystemViewer();
294 #if defined(OS_WIN)
295 if (can_open_pdf_in_system_viewer)
296 return IsAdobeReaderDefaultPDFViewer()
297 ? IDS_DOWNLOAD_MENU_ALWAYS_OPEN_PDF_IN_READER
298 : IDS_DOWNLOAD_MENU_PLATFORM_OPEN_ALWAYS;
299 #elif defined(OS_MACOSX) || defined(OS_LINUX)
300 if (can_open_pdf_in_system_viewer)
301 return IDS_DOWNLOAD_MENU_PLATFORM_OPEN_ALWAYS;
302 #endif
303 return IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE;
304 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698