OLD | NEW |
---|---|
(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_commands.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/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/common/url_constants.h" | |
16 #include "chrome/grit/generated_resources.h" | |
17 #include "grit/theme_resources.h" | |
18 #include "ui/base/l10n/l10n_util.h" | |
19 #include "ui/base/resource/resource_bundle.h" | |
20 | |
21 #if defined(OS_WIN) | |
22 #include "chrome/browser/download/download_target_determiner.h" | |
23 #include "chrome/browser/ui/pdf/adobe_reader_info_win.h" | |
24 #endif | |
25 | |
26 DownloadCommands::DownloadCommands(content::DownloadItem* download_item) | |
27 : download_item_(download_item) { | |
28 } | |
29 | |
30 int DownloadCommands::GetCommandIconId(Command command) { | |
31 switch (command) { | |
32 case PAUSE: | |
33 return IDR_DOWNLOAD_NOTIFICATION_MENU_PAUSE; | |
34 case RESUME: | |
35 return IDR_DOWNLOAD_NOTIFICATION_MENU_RESUME; | |
36 case SHOW_IN_FOLDER: | |
37 return IDR_DOWNLOAD_NOTIFICATION_MENU_FOLDER; | |
38 case RETRY: | |
39 case KEEP: | |
40 return IDR_DOWNLOAD_NOTIFICATION_MENU_DOWNLOAD; | |
41 case DISCARD: | |
42 return IDR_DOWNLOAD_NOTIFICATION_MENU_DELETE; | |
43 case OPEN_WHEN_COMPLETE: | |
44 case ALWAYS_OPEN_TYPE: | |
45 case PLATFORM_OPEN: | |
46 case CANCEL: | |
47 case LEARN_MORE_SCANNING: | |
48 case LEARN_MORE_INTERRUPTED: | |
49 return -1; | |
50 } | |
51 NOTREACHED(); | |
52 return -1; | |
53 } | |
54 | |
55 gfx::Image DownloadCommands::GetCommandIcon(Command command) { | |
56 ResourceBundle& bundle = ResourceBundle::GetSharedInstance(); | |
57 return bundle.GetImageNamed(GetCommandIconId(command)); | |
58 } | |
59 | |
60 bool DownloadCommands::IsCommandEnabled(Command command) const { | |
61 if (!download_item_) | |
asanka
2015/02/27 01:26:48
download_item_ should never be nullptr. Here and e
yoshiki
2015/02/28 10:50:53
Done.
| |
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() && !download_item_->IsPaused() && | |
81 download_item_->GetState() == content::DownloadItem::IN_PROGRESS; | |
82 case RESUME: | |
83 return !download_item_->IsDone() && | |
asanka
2015/02/27 01:26:48
Use download_item_->CanResume()
yoshiki
2015/02/28 10:50:53
Done.
| |
84 (download_item_->IsPaused() || | |
85 download_item_->GetState() != content::DownloadItem::IN_PROGRESS); | |
86 case DISCARD: | |
87 case KEEP: | |
88 case LEARN_MORE_SCANNING: | |
89 case LEARN_MORE_INTERRUPTED: | |
90 case RETRY: | |
91 return true; | |
92 } | |
93 NOTREACHED(); | |
94 return false; | |
95 } | |
96 | |
97 bool DownloadCommands::IsCommandChecked(Command command) const { | |
98 if (!download_item_) | |
99 return false; | |
100 | |
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(); | |
112 } | |
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; | |
127 } | |
128 return false; | |
129 } | |
130 | |
131 bool DownloadCommands::IsCommandVisible(Command command) const { | |
132 if (!download_item_) | |
133 return false; | |
134 | |
135 if (command == PLATFORM_OPEN) | |
136 return (DownloadItemModel(download_item_).ShouldPreferOpeningInBrowser()); | |
137 | |
138 return true; | |
139 } | |
140 | |
141 void DownloadCommands::ExecuteCommand(Command command, Profile* profile) { | |
142 if (!download_item_) | |
143 return; | |
144 | |
145 switch (command) { | |
146 case SHOW_IN_FOLDER: | |
147 download_item_->ShowDownloadInShell(); | |
148 break; | |
149 case OPEN_WHEN_COMPLETE: | |
150 download_item_->OpenDownload(); | |
151 break; | |
152 case ALWAYS_OPEN_TYPE: { | |
153 bool is_checked = IsCommandChecked(ALWAYS_OPEN_TYPE); | |
154 DownloadPrefs* prefs = DownloadPrefs::FromBrowserContext( | |
155 download_item_->GetBrowserContext()); | |
156 #if defined(OS_WIN) || defined(OS_LINUX) || \ | |
157 (defined(OS_MACOSX) && !defined(OS_IOS)) | |
158 if (CanOpenPdfInSystemViewer()) { | |
159 prefs->SetShouldOpenPdfInSystemReader(!is_checked); | |
160 DownloadItemModel(download_item_) | |
161 .SetShouldPreferOpeningInBrowser(is_checked); | |
162 break; | |
163 } | |
164 #endif | |
165 base::FilePath path = download_item_->GetTargetFilePath(); | |
166 if (is_checked) | |
167 prefs->DisableAutoOpenBasedOnExtension(path); | |
168 else | |
169 prefs->EnableAutoOpenBasedOnExtension(path); | |
170 break; | |
171 } | |
172 case PLATFORM_OPEN: | |
173 DownloadItemModel(download_item_).OpenUsingPlatformHandler(); | |
174 break; | |
175 case CANCEL: | |
176 download_item_->Cancel(true /* Cancelled by user */); | |
177 break; | |
178 case DISCARD: | |
179 download_item_->Remove(); | |
180 break; | |
181 case KEEP: | |
182 download_item_->ValidateDangerousDownload(); | |
183 break; | |
184 case LEARN_MORE_SCANNING: { | |
185 #if defined(FULL_SAFE_BROWSING) | |
186 using safe_browsing::DownloadProtectionService; | |
187 | |
188 Browser* browser = | |
189 g_browser_process->profile_manager()->IsValidProfile(profile) | |
asanka
2015/02/27 01:26:48
Something is seriously wrong if the profile isn't
yoshiki
2015/02/28 10:50:53
Thanks for guidance. I do so. Done.
| |
190 ? chrome::FindBrowserWithProfile(profile, | |
asanka
2015/02/27 01:26:48
This can return nullptr if there are no active bro
yoshiki
2015/02/28 10:50:53
ScopedTabbedBrowserDisplayer looks good in this ca
| |
191 chrome::GetActiveDesktop()) | |
192 : NULL; | |
193 if (browser) { | |
194 SafeBrowsingService* sb_service = | |
195 g_browser_process->safe_browsing_service(); | |
196 DownloadProtectionService* protection_service = | |
197 (sb_service ? sb_service->download_protection_service() : NULL); | |
198 if (protection_service) { | |
asanka
2015/02/27 01:26:48
Nit: no {
yoshiki
2015/02/28 10:50:53
Done.
| |
199 protection_service->ShowDetailsForDownload(*download_item_, browser); | |
200 } | |
201 } | |
202 #else | |
203 // Should only be getting invoked if we are using safe browsing. | |
204 NOTREACHED(); | |
205 #endif | |
206 break; | |
207 } | |
208 case LEARN_MORE_INTERRUPTED: { | |
209 Browser* browser = | |
210 g_browser_process->profile_manager()->IsValidProfile(profile) | |
asanka
2015/02/27 01:26:48
Same comments as above. Probably move the Browser*
yoshiki
2015/02/28 10:50:53
Done.
| |
211 ? chrome::FindBrowserWithProfile(profile, | |
212 chrome::GetActiveDesktop()) | |
213 : NULL; | |
214 if (browser) { | |
215 browser->OpenURL(content::OpenURLParams( | |
216 GURL(chrome::kDownloadInterruptedLearnMoreURL), content::Referrer(), | |
217 NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_LINK, false)); | |
218 } | |
219 break; | |
220 } | |
221 case PAUSE: | |
222 download_item_->Pause(); | |
223 break; | |
224 case RESUME: | |
225 download_item_->Resume(); | |
226 break; | |
227 case RETRY: | |
asanka
2015/02/27 01:26:49
What do you expect the RETRY command to do? And wh
yoshiki
2015/02/28 10:50:53
It's used when user wants to retry a cancelled or
asanka
2015/03/02 22:17:25
What's the difference between RESUME and RETRY?
yoshiki
2015/03/03 04:48:38
I meant:
- Resume: suspended download
- Retry: can
| |
228 download_item_->Resume(); | |
229 break; | |
230 } | |
231 } | |
232 | |
233 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) | |
234 bool DownloadCommands::IsDownloadPdf() const { | |
235 base::FilePath path = download_item_->GetTargetFilePath(); | |
236 return path.MatchesExtension(FILE_PATH_LITERAL(".pdf")); | |
237 } | |
238 #endif | |
239 | |
240 bool DownloadCommands::CanOpenPdfInSystemViewer() const { | |
241 #if defined(OS_WIN) | |
242 bool is_adobe_pdf_reader_up_to_date = false; | |
243 if (IsDownloadPdf() && IsAdobeReaderDefaultPDFViewer()) { | |
244 is_adobe_pdf_reader_up_to_date = | |
245 DownloadTargetDeterminer::IsAdobeReaderUpToDate(); | |
246 } | |
247 return IsDownloadPdf() && | |
248 (IsAdobeReaderDefaultPDFViewer() ? is_adobe_pdf_reader_up_to_date | |
249 : true); | |
250 #elif defined(OS_MACOSX) || defined(OS_LINUX) | |
251 return IsDownloadPdf(); | |
252 #endif | |
253 } | |
OLD | NEW |