OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 5 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/basictypes.h" | |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
11 #include "base/callback.h" | 12 #include "base/callback.h" |
12 #include "base/file_util.h" | 13 #include "base/file_util.h" |
13 #include "base/prefs/pref_member.h" | 14 #include "base/prefs/pref_member.h" |
14 #include "base/prefs/pref_service.h" | 15 #include "base/prefs/pref_service.h" |
15 #include "base/rand_util.h" | 16 #include "base/rand_util.h" |
16 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
17 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
18 #include "base/time/time.h" | 19 #include "base/time/time.h" |
19 #include "chrome/browser/browser_process.h" | 20 #include "chrome/browser/browser_process.h" |
20 #include "chrome/browser/chrome_notification_types.h" | 21 #include "chrome/browser/chrome_notification_types.h" |
21 #include "chrome/browser/download/download_completion_blocker.h" | 22 #include "chrome/browser/download/download_completion_blocker.h" |
22 #include "chrome/browser/download/download_crx_util.h" | 23 #include "chrome/browser/download/download_crx_util.h" |
23 #include "chrome/browser/download/download_file_picker.h" | 24 #include "chrome/browser/download/download_file_picker.h" |
24 #include "chrome/browser/download/download_history.h" | 25 #include "chrome/browser/download/download_history.h" |
26 #include "chrome/browser/download/download_item_model.h" | |
25 #include "chrome/browser/download/download_path_reservation_tracker.h" | 27 #include "chrome/browser/download/download_path_reservation_tracker.h" |
26 #include "chrome/browser/download/download_prefs.h" | 28 #include "chrome/browser/download/download_prefs.h" |
27 #include "chrome/browser/download/download_service.h" | 29 #include "chrome/browser/download/download_service.h" |
28 #include "chrome/browser/download/download_service_factory.h" | 30 #include "chrome/browser/download/download_service_factory.h" |
29 #include "chrome/browser/download/download_target_determiner.h" | 31 #include "chrome/browser/download/download_target_determiner.h" |
30 #include "chrome/browser/download/save_package_file_picker.h" | 32 #include "chrome/browser/download/save_package_file_picker.h" |
31 #include "chrome/browser/extensions/api/downloads/downloads_api.h" | 33 #include "chrome/browser/extensions/api/downloads/downloads_api.h" |
32 #include "chrome/browser/extensions/crx_installer.h" | 34 #include "chrome/browser/extensions/crx_installer.h" |
33 #include "chrome/browser/platform_util.h" | 35 #include "chrome/browser/platform_util.h" |
34 #include "chrome/browser/profiles/profile.h" | 36 #include "chrome/browser/profiles/profile.h" |
35 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 37 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
36 #include "chrome/common/chrome_constants.h" | 38 #include "chrome/common/chrome_constants.h" |
37 #include "chrome/common/pref_names.h" | 39 #include "chrome/common/pref_names.h" |
38 #include "components/user_prefs/pref_registry_syncable.h" | 40 #include "components/user_prefs/pref_registry_syncable.h" |
39 #include "content/public/browser/download_item.h" | 41 #include "content/public/browser/download_item.h" |
40 #include "content/public/browser/download_manager.h" | 42 #include "content/public/browser/download_manager.h" |
41 #include "content/public/browser/notification_source.h" | 43 #include "content/public/browser/notification_source.h" |
42 #include "extensions/common/constants.h" | 44 #include "extensions/common/constants.h" |
45 #include "net/base/mime_util.h" | |
43 | 46 |
44 #if defined(OS_CHROMEOS) | 47 #if defined(OS_CHROMEOS) |
45 #include "chrome/browser/chromeos/drive/download_handler.h" | 48 #include "chrome/browser/chromeos/drive/download_handler.h" |
46 #include "chrome/browser/chromeos/drive/file_system_util.h" | 49 #include "chrome/browser/chromeos/drive/file_system_util.h" |
47 #endif | 50 #endif |
48 | 51 |
49 using content::BrowserThread; | 52 using content::BrowserThread; |
50 using content::DownloadItem; | 53 using content::DownloadItem; |
51 using content::DownloadManager; | 54 using content::DownloadManager; |
52 using safe_browsing::DownloadProtectionService; | 55 using safe_browsing::DownloadProtectionService; |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
154 } else { | 157 } else { |
155 // If the URL is malicious, we'll use that as the danger type. The results | 158 // If the URL is malicious, we'll use that as the danger type. The results |
156 // of the content check, if one is performed, will be ignored. | 159 // of the content check, if one is performed, will be ignored. |
157 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL; | 160 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL; |
158 } | 161 } |
159 callback.Run(danger_type); | 162 callback.Run(danger_type); |
160 } | 163 } |
161 | 164 |
162 #endif // FULL_SAFE_BROWSING | 165 #endif // FULL_SAFE_BROWSING |
163 | 166 |
167 bool IsOpenInBrowserPreferreredForFile(const base::FilePath& path, | |
168 const std::string& mime_type) { | |
169 if (net::IsSupportedImageMimeType(mime_type)) | |
170 return true; | |
171 | |
172 #if ENABLE_PLUGINS | |
173 if (path.MatchesExtension(FILE_PATH_LITERAL(".pdf"))) | |
palmer
2013/10/31 21:09:15
Ideally, we should check by the website-declared M
asanka
2013/11/01 14:29:36
I'm ignoring the website-declared MIME type and th
| |
174 return true; | |
175 #endif | |
176 return false; | |
177 } | |
178 | |
164 } // namespace | 179 } // namespace |
165 | 180 |
166 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) | 181 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) |
167 : profile_(profile), | 182 : profile_(profile), |
168 next_download_id_(content::DownloadItem::kInvalidId), | 183 next_download_id_(content::DownloadItem::kInvalidId), |
169 download_prefs_(new DownloadPrefs(profile)) { | 184 download_prefs_(new DownloadPrefs(profile)) { |
170 } | 185 } |
171 | 186 |
172 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { | 187 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { |
173 } | 188 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
213 const content::DownloadIdCallback& callback) { | 228 const content::DownloadIdCallback& callback) { |
214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 229 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
215 DCHECK(!profile_->IsOffTheRecord()); | 230 DCHECK(!profile_->IsOffTheRecord()); |
216 DCHECK_NE(content::DownloadItem::kInvalidId, next_download_id_); | 231 DCHECK_NE(content::DownloadItem::kInvalidId, next_download_id_); |
217 callback.Run(next_download_id_++); | 232 callback.Run(next_download_id_++); |
218 } | 233 } |
219 | 234 |
220 bool ChromeDownloadManagerDelegate::DetermineDownloadTarget( | 235 bool ChromeDownloadManagerDelegate::DetermineDownloadTarget( |
221 DownloadItem* download, | 236 DownloadItem* download, |
222 const content::DownloadTargetCallback& callback) { | 237 const content::DownloadTargetCallback& callback) { |
238 DownloadTargetDeterminer::CompletionCallback target_determined_callback = | |
239 base::Bind(&ChromeDownloadManagerDelegate::OnDownloadTargetDetermined, | |
240 this, | |
241 download->GetId(), | |
242 callback); | |
223 DownloadTargetDeterminer::Start( | 243 DownloadTargetDeterminer::Start( |
224 download, | 244 download, |
225 GetPlatformDownloadPath( | 245 GetPlatformDownloadPath(profile_, download, PLATFORM_TARGET_PATH), |
226 profile_, download, PLATFORM_TARGET_PATH), | |
227 download_prefs_.get(), | 246 download_prefs_.get(), |
228 this, | 247 this, |
229 callback); | 248 target_determined_callback); |
230 return true; | 249 return true; |
231 } | 250 } |
232 | 251 |
233 bool ChromeDownloadManagerDelegate::ShouldOpenFileBasedOnExtension( | 252 bool ChromeDownloadManagerDelegate::ShouldOpenFileBasedOnExtension( |
234 const base::FilePath& path) { | 253 const base::FilePath& path) { |
235 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
236 if (path.Extension().empty()) | 255 if (path.Extension().empty()) |
237 return false; | 256 return false; |
238 // TODO(asanka): This determination is done based on |path|, while | 257 // TODO(asanka): This determination is done based on |path|, while |
239 // ShouldOpenDownload() detects extension downloads based on the | 258 // ShouldOpenDownload() detects extension downloads based on the |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
364 // Deletes itself. | 383 // Deletes itself. |
365 new SavePackageFilePicker( | 384 new SavePackageFilePicker( |
366 web_contents, | 385 web_contents, |
367 suggested_path, | 386 suggested_path, |
368 default_extension, | 387 default_extension, |
369 can_save_as_complete, | 388 can_save_as_complete, |
370 download_prefs_.get(), | 389 download_prefs_.get(), |
371 callback); | 390 callback); |
372 } | 391 } |
373 | 392 |
393 void ChromeDownloadManagerDelegate::OpenDownloadUsingPlatformHandler( | |
394 DownloadItem* download) { | |
395 base::FilePath platform_path( | |
396 GetPlatformDownloadPath(profile_, download, PLATFORM_TARGET_PATH)); | |
397 DCHECK(!platform_path.empty()); | |
398 platform_util::OpenItem(platform_path); | |
399 } | |
400 | |
374 void ChromeDownloadManagerDelegate::OpenDownload(DownloadItem* download) { | 401 void ChromeDownloadManagerDelegate::OpenDownload(DownloadItem* download) { |
375 DCHECK_EQ(DownloadItem::COMPLETE, download->GetState()); | 402 DCHECK_EQ(DownloadItem::COMPLETE, download->GetState()); |
376 if (!download->CanOpenDownload()) | 403 if (!download->CanOpenDownload()) |
377 return; | 404 return; |
378 base::FilePath platform_path( | 405 if (DownloadItemModel(download).ShouldPreferOpeningInBrowser()) { |
379 GetPlatformDownloadPath(profile_, download, PLATFORM_TARGET_PATH)); | 406 // Open a new tab with for the file:// URL for the target path. |
380 DCHECK(!platform_path.empty()); | 407 return; |
381 platform_util::OpenItem(platform_path); | 408 } |
409 OpenDownloadUsingPlatformHandler(download); | |
382 } | 410 } |
383 | 411 |
384 void ChromeDownloadManagerDelegate::ShowDownloadInShell( | 412 void ChromeDownloadManagerDelegate::ShowDownloadInShell( |
385 DownloadItem* download) { | 413 DownloadItem* download) { |
386 if (!download->CanShowInFolder()) | 414 if (!download->CanShowInFolder()) |
387 return; | 415 return; |
388 base::FilePath platform_path( | 416 base::FilePath platform_path( |
389 GetPlatformDownloadPath(profile_, download, PLATFORM_CURRENT_PATH)); | 417 GetPlatformDownloadPath(profile_, download, PLATFORM_CURRENT_PATH)); |
390 DCHECK(!platform_path.empty()); | 418 DCHECK(!platform_path.empty()); |
391 platform_util::ShowItemInFolder(platform_path); | 419 platform_util::ShowItemInFolder(platform_path); |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
579 chrome::NOTIFICATION_CRX_INSTALLER_DONE, | 607 chrome::NOTIFICATION_CRX_INSTALLER_DONE, |
580 source); | 608 source); |
581 | 609 |
582 scoped_refptr<extensions::CrxInstaller> installer = | 610 scoped_refptr<extensions::CrxInstaller> installer = |
583 content::Source<extensions::CrxInstaller>(source).ptr(); | 611 content::Source<extensions::CrxInstaller>(source).ptr(); |
584 content::DownloadOpenDelayedCallback callback = | 612 content::DownloadOpenDelayedCallback callback = |
585 crx_installers_[installer.get()]; | 613 crx_installers_[installer.get()]; |
586 crx_installers_.erase(installer.get()); | 614 crx_installers_.erase(installer.get()); |
587 callback.Run(installer->did_handle_successfully()); | 615 callback.Run(installer->did_handle_successfully()); |
588 } | 616 } |
617 | |
618 void ChromeDownloadManagerDelegate::OnDownloadTargetDetermined( | |
619 int32 download_id, | |
620 const content::DownloadTargetCallback& callback, | |
621 scoped_ptr<DownloadTargetInfo> target_info) { | |
622 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
623 DownloadItem* item = download_manager_->GetDownload(download_id); | |
624 if (!target_info->target_path.empty() && item && | |
625 IsOpenInBrowserPreferreredForFile(target_info->target_path, | |
626 target_info->mime_type) && | |
627 target_info->is_handled_by_browser) | |
628 DownloadItemModel(item).SetShouldPreferOpeningInBrowser(true); | |
629 callback.Run(target_info->target_path, | |
630 target_info->target_disposition, | |
631 target_info->danger_type, | |
632 target_info->intermediate_path); | |
633 } | |
OLD | NEW |