| 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" | |
| 10 #include "base/bind.h" | 9 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 12 #include "base/callback.h" | 11 #include "base/callback.h" |
| 13 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 14 #include "base/prefs/pref_member.h" | 13 #include "base/prefs/pref_member.h" |
| 15 #include "base/prefs/pref_service.h" | 14 #include "base/prefs/pref_service.h" |
| 16 #include "base/rand_util.h" | 15 #include "base/rand_util.h" |
| 17 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 19 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 20 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
| 21 #include "chrome/browser/chrome_notification_types.h" | 20 #include "chrome/browser/chrome_notification_types.h" |
| 22 #include "chrome/browser/download/download_completion_blocker.h" | 21 #include "chrome/browser/download/download_completion_blocker.h" |
| 23 #include "chrome/browser/download/download_crx_util.h" | 22 #include "chrome/browser/download/download_crx_util.h" |
| 24 #include "chrome/browser/download/download_file_picker.h" | 23 #include "chrome/browser/download/download_file_picker.h" |
| 25 #include "chrome/browser/download/download_history.h" | 24 #include "chrome/browser/download/download_history.h" |
| 26 #include "chrome/browser/download/download_item_model.h" | |
| 27 #include "chrome/browser/download/download_path_reservation_tracker.h" | 25 #include "chrome/browser/download/download_path_reservation_tracker.h" |
| 28 #include "chrome/browser/download/download_prefs.h" | 26 #include "chrome/browser/download/download_prefs.h" |
| 29 #include "chrome/browser/download/download_service.h" | 27 #include "chrome/browser/download/download_service.h" |
| 30 #include "chrome/browser/download/download_service_factory.h" | 28 #include "chrome/browser/download/download_service_factory.h" |
| 31 #include "chrome/browser/download/download_stats.h" | |
| 32 #include "chrome/browser/download/download_target_determiner.h" | 29 #include "chrome/browser/download/download_target_determiner.h" |
| 33 #include "chrome/browser/download/save_package_file_picker.h" | 30 #include "chrome/browser/download/save_package_file_picker.h" |
| 34 #include "chrome/browser/extensions/api/downloads/downloads_api.h" | 31 #include "chrome/browser/extensions/api/downloads/downloads_api.h" |
| 35 #include "chrome/browser/extensions/crx_installer.h" | 32 #include "chrome/browser/extensions/crx_installer.h" |
| 36 #include "chrome/browser/platform_util.h" | 33 #include "chrome/browser/platform_util.h" |
| 37 #include "chrome/browser/profiles/profile.h" | 34 #include "chrome/browser/profiles/profile.h" |
| 38 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 35 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 39 #include "chrome/browser/ui/browser.h" | |
| 40 #include "chrome/browser/ui/browser_finder.h" | |
| 41 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" | |
| 42 #include "chrome/common/chrome_constants.h" | 36 #include "chrome/common/chrome_constants.h" |
| 43 #include "chrome/common/pref_names.h" | 37 #include "chrome/common/pref_names.h" |
| 44 #include "components/user_prefs/pref_registry_syncable.h" | 38 #include "components/user_prefs/pref_registry_syncable.h" |
| 45 #include "content/public/browser/download_item.h" | 39 #include "content/public/browser/download_item.h" |
| 46 #include "content/public/browser/download_manager.h" | 40 #include "content/public/browser/download_manager.h" |
| 47 #include "content/public/browser/notification_source.h" | 41 #include "content/public/browser/notification_source.h" |
| 48 #include "content/public/browser/page_navigator.h" | |
| 49 #include "extensions/common/constants.h" | 42 #include "extensions/common/constants.h" |
| 50 #include "net/base/mime_util.h" | |
| 51 #include "net/base/net_util.h" | |
| 52 | 43 |
| 53 #if defined(OS_CHROMEOS) | 44 #if defined(OS_CHROMEOS) |
| 54 #include "chrome/browser/chromeos/drive/download_handler.h" | 45 #include "chrome/browser/chromeos/drive/download_handler.h" |
| 55 #include "chrome/browser/chromeos/drive/file_system_util.h" | 46 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 56 #endif | 47 #endif |
| 57 | 48 |
| 58 using content::BrowserThread; | 49 using content::BrowserThread; |
| 59 using content::DownloadItem; | 50 using content::DownloadItem; |
| 60 using content::DownloadManager; | 51 using content::DownloadManager; |
| 61 using safe_browsing::DownloadProtectionService; | 52 using safe_browsing::DownloadProtectionService; |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 } else { | 154 } else { |
| 164 // If the URL is malicious, we'll use that as the danger type. The results | 155 // If the URL is malicious, we'll use that as the danger type. The results |
| 165 // of the content check, if one is performed, will be ignored. | 156 // of the content check, if one is performed, will be ignored. |
| 166 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL; | 157 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL; |
| 167 } | 158 } |
| 168 callback.Run(danger_type); | 159 callback.Run(danger_type); |
| 169 } | 160 } |
| 170 | 161 |
| 171 #endif // FULL_SAFE_BROWSING | 162 #endif // FULL_SAFE_BROWSING |
| 172 | 163 |
| 173 // Called on the blocking pool to determine the MIME type for |path|. | |
| 174 void GetMimeTypeAndReplyOnUIThread( | |
| 175 const base::FilePath& path, | |
| 176 const base::Callback<void(const std::string&)>& callback) { | |
| 177 std::string mime_type; | |
| 178 net::GetMimeTypeFromFile(path, &mime_type); | |
| 179 BrowserThread::PostTask( | |
| 180 BrowserThread::UI, FROM_HERE, base::Bind(callback, mime_type)); | |
| 181 } | |
| 182 | |
| 183 bool IsOpenInBrowserPreferreredForFile(const base::FilePath& path) { | |
| 184 // On Android, always prefer opening with an external app. | |
| 185 #if !defined(OS_ANDROID) && defined(ENABLE_PLUGINS) | |
| 186 // TODO(asanka): Consider other file types and MIME types. | |
| 187 if (path.MatchesExtension(FILE_PATH_LITERAL(".pdf"))) | |
| 188 return true; | |
| 189 #endif | |
| 190 return false; | |
| 191 } | |
| 192 | |
| 193 } // namespace | 164 } // namespace |
| 194 | 165 |
| 195 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) | 166 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) |
| 196 : profile_(profile), | 167 : profile_(profile), |
| 197 next_download_id_(content::DownloadItem::kInvalidId), | 168 next_download_id_(content::DownloadItem::kInvalidId), |
| 198 download_prefs_(new DownloadPrefs(profile)) { | 169 download_prefs_(new DownloadPrefs(profile)) { |
| 199 } | 170 } |
| 200 | 171 |
| 201 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { | 172 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { |
| 202 } | 173 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 const content::DownloadIdCallback& callback) { | 213 const content::DownloadIdCallback& callback) { |
| 243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 244 DCHECK(!profile_->IsOffTheRecord()); | 215 DCHECK(!profile_->IsOffTheRecord()); |
| 245 DCHECK_NE(content::DownloadItem::kInvalidId, next_download_id_); | 216 DCHECK_NE(content::DownloadItem::kInvalidId, next_download_id_); |
| 246 callback.Run(next_download_id_++); | 217 callback.Run(next_download_id_++); |
| 247 } | 218 } |
| 248 | 219 |
| 249 bool ChromeDownloadManagerDelegate::DetermineDownloadTarget( | 220 bool ChromeDownloadManagerDelegate::DetermineDownloadTarget( |
| 250 DownloadItem* download, | 221 DownloadItem* download, |
| 251 const content::DownloadTargetCallback& callback) { | 222 const content::DownloadTargetCallback& callback) { |
| 252 DownloadTargetDeterminer::CompletionCallback target_determined_callback = | |
| 253 base::Bind(&ChromeDownloadManagerDelegate::OnDownloadTargetDetermined, | |
| 254 this, | |
| 255 download->GetId(), | |
| 256 callback); | |
| 257 DownloadTargetDeterminer::Start( | 223 DownloadTargetDeterminer::Start( |
| 258 download, | 224 download, |
| 259 GetPlatformDownloadPath(profile_, download, PLATFORM_TARGET_PATH), | 225 GetPlatformDownloadPath( |
| 226 profile_, download, PLATFORM_TARGET_PATH), |
| 260 download_prefs_.get(), | 227 download_prefs_.get(), |
| 261 this, | 228 this, |
| 262 target_determined_callback); | 229 callback); |
| 263 return true; | 230 return true; |
| 264 } | 231 } |
| 265 | 232 |
| 266 bool ChromeDownloadManagerDelegate::ShouldOpenFileBasedOnExtension( | 233 bool ChromeDownloadManagerDelegate::ShouldOpenFileBasedOnExtension( |
| 267 const base::FilePath& path) { | 234 const base::FilePath& path) { |
| 268 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 235 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 269 if (path.Extension().empty()) | 236 if (path.Extension().empty()) |
| 270 return false; | 237 return false; |
| 271 // TODO(asanka): This determination is done based on |path|, while | 238 // TODO(asanka): This determination is done based on |path|, while |
| 272 // ShouldOpenDownload() detects extension downloads based on the | 239 // ShouldOpenDownload() detects extension downloads based on the |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 // Deletes itself. | 364 // Deletes itself. |
| 398 new SavePackageFilePicker( | 365 new SavePackageFilePicker( |
| 399 web_contents, | 366 web_contents, |
| 400 suggested_path, | 367 suggested_path, |
| 401 default_extension, | 368 default_extension, |
| 402 can_save_as_complete, | 369 can_save_as_complete, |
| 403 download_prefs_.get(), | 370 download_prefs_.get(), |
| 404 callback); | 371 callback); |
| 405 } | 372 } |
| 406 | 373 |
| 407 void ChromeDownloadManagerDelegate::OpenDownloadUsingPlatformHandler( | 374 void ChromeDownloadManagerDelegate::OpenDownload(DownloadItem* download) { |
| 408 DownloadItem* download) { | 375 DCHECK_EQ(DownloadItem::COMPLETE, download->GetState()); |
| 376 if (!download->CanOpenDownload()) |
| 377 return; |
| 409 base::FilePath platform_path( | 378 base::FilePath platform_path( |
| 410 GetPlatformDownloadPath(profile_, download, PLATFORM_TARGET_PATH)); | 379 GetPlatformDownloadPath(profile_, download, PLATFORM_TARGET_PATH)); |
| 411 DCHECK(!platform_path.empty()); | 380 DCHECK(!platform_path.empty()); |
| 412 platform_util::OpenItem(platform_path); | 381 platform_util::OpenItem(platform_path); |
| 413 } | 382 } |
| 414 | 383 |
| 415 void ChromeDownloadManagerDelegate::OpenDownload(DownloadItem* download) { | |
| 416 DCHECK_EQ(DownloadItem::COMPLETE, download->GetState()); | |
| 417 DCHECK(!download->GetTargetFilePath().empty()); | |
| 418 if (!download->CanOpenDownload()) | |
| 419 return; | |
| 420 | |
| 421 if (!DownloadItemModel(download).ShouldPreferOpeningInBrowser()) { | |
| 422 RecordDownloadOpenMethod(DOWNLOAD_OPEN_METHOD_DEFAULT_PLATFORM); | |
| 423 OpenDownloadUsingPlatformHandler(download); | |
| 424 return; | |
| 425 } | |
| 426 | |
| 427 #if !defined(OS_ANDROID) | |
| 428 content::WebContents* web_contents = download->GetWebContents(); | |
| 429 Browser* browser = | |
| 430 web_contents ? chrome::FindBrowserWithWebContents(web_contents) : NULL; | |
| 431 scoped_ptr<chrome::ScopedTabbedBrowserDisplayer> browser_displayer; | |
| 432 if (!browser || | |
| 433 !browser->CanSupportWindowFeature(Browser::FEATURE_TABSTRIP)) { | |
| 434 browser_displayer.reset(new chrome::ScopedTabbedBrowserDisplayer( | |
| 435 profile_, chrome::GetActiveDesktop())); | |
| 436 browser = browser_displayer->browser(); | |
| 437 } | |
| 438 content::OpenURLParams params( | |
| 439 net::FilePathToFileURL(download->GetTargetFilePath()), | |
| 440 content::Referrer(), | |
| 441 NEW_FOREGROUND_TAB, | |
| 442 content::PAGE_TRANSITION_LINK, | |
| 443 false); | |
| 444 browser->OpenURL(params); | |
| 445 RecordDownloadOpenMethod(DOWNLOAD_OPEN_METHOD_DEFAULT_BROWSER); | |
| 446 #else | |
| 447 // ShouldPreferOpeningInBrowser() should never be true on Android. | |
| 448 NOTREACHED(); | |
| 449 #endif | |
| 450 } | |
| 451 | |
| 452 void ChromeDownloadManagerDelegate::ShowDownloadInShell( | 384 void ChromeDownloadManagerDelegate::ShowDownloadInShell( |
| 453 DownloadItem* download) { | 385 DownloadItem* download) { |
| 454 if (!download->CanShowInFolder()) | 386 if (!download->CanShowInFolder()) |
| 455 return; | 387 return; |
| 456 base::FilePath platform_path( | 388 base::FilePath platform_path( |
| 457 GetPlatformDownloadPath(profile_, download, PLATFORM_CURRENT_PATH)); | 389 GetPlatformDownloadPath(profile_, download, PLATFORM_CURRENT_PATH)); |
| 458 DCHECK(!platform_path.empty()); | 390 DCHECK(!platform_path.empty()); |
| 459 platform_util::ShowItemInFolder(platform_path); | 391 platform_util::ShowItemInFolder(platform_path); |
| 460 } | 392 } |
| 461 | 393 |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 584 service->CheckDownloadUrl(*download, | 516 service->CheckDownloadUrl(*download, |
| 585 base::Bind(&CheckDownloadUrlDone, | 517 base::Bind(&CheckDownloadUrlDone, |
| 586 callback, | 518 callback, |
| 587 is_content_check_supported)); | 519 is_content_check_supported)); |
| 588 return; | 520 return; |
| 589 } | 521 } |
| 590 #endif | 522 #endif |
| 591 callback.Run(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 523 callback.Run(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 592 } | 524 } |
| 593 | 525 |
| 594 void ChromeDownloadManagerDelegate::GetFileMimeType( | |
| 595 const base::FilePath& path, | |
| 596 const GetFileMimeTypeCallback& callback) { | |
| 597 BrowserThread::PostBlockingPoolTask( | |
| 598 FROM_HERE, | |
| 599 base::Bind(&GetMimeTypeAndReplyOnUIThread, path, callback)); | |
| 600 } | |
| 601 | |
| 602 #if defined(FULL_SAFE_BROWSING) | 526 #if defined(FULL_SAFE_BROWSING) |
| 603 void ChromeDownloadManagerDelegate::CheckClientDownloadDone( | 527 void ChromeDownloadManagerDelegate::CheckClientDownloadDone( |
| 604 uint32 download_id, | 528 uint32 download_id, |
| 605 DownloadProtectionService::DownloadCheckResult result) { | 529 DownloadProtectionService::DownloadCheckResult result) { |
| 606 DownloadItem* item = download_manager_->GetDownload(download_id); | 530 DownloadItem* item = download_manager_->GetDownload(download_id); |
| 607 if (!item || (item->GetState() != DownloadItem::IN_PROGRESS)) | 531 if (!item || (item->GetState() != DownloadItem::IN_PROGRESS)) |
| 608 return; | 532 return; |
| 609 | 533 |
| 610 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false) | 534 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false) |
| 611 << " verdict = " << result; | 535 << " verdict = " << result; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 655 chrome::NOTIFICATION_CRX_INSTALLER_DONE, | 579 chrome::NOTIFICATION_CRX_INSTALLER_DONE, |
| 656 source); | 580 source); |
| 657 | 581 |
| 658 scoped_refptr<extensions::CrxInstaller> installer = | 582 scoped_refptr<extensions::CrxInstaller> installer = |
| 659 content::Source<extensions::CrxInstaller>(source).ptr(); | 583 content::Source<extensions::CrxInstaller>(source).ptr(); |
| 660 content::DownloadOpenDelayedCallback callback = | 584 content::DownloadOpenDelayedCallback callback = |
| 661 crx_installers_[installer.get()]; | 585 crx_installers_[installer.get()]; |
| 662 crx_installers_.erase(installer.get()); | 586 crx_installers_.erase(installer.get()); |
| 663 callback.Run(installer->did_handle_successfully()); | 587 callback.Run(installer->did_handle_successfully()); |
| 664 } | 588 } |
| 665 | |
| 666 void ChromeDownloadManagerDelegate::OnDownloadTargetDetermined( | |
| 667 int32 download_id, | |
| 668 const content::DownloadTargetCallback& callback, | |
| 669 scoped_ptr<DownloadTargetInfo> target_info) { | |
| 670 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 671 DownloadItem* item = download_manager_->GetDownload(download_id); | |
| 672 if (!target_info->target_path.empty() && item && | |
| 673 IsOpenInBrowserPreferreredForFile(target_info->target_path) && | |
| 674 target_info->is_filetype_handled_securely) | |
| 675 DownloadItemModel(item).SetShouldPreferOpeningInBrowser(true); | |
| 676 callback.Run(target_info->target_path, | |
| 677 target_info->target_disposition, | |
| 678 target_info->danger_type, | |
| 679 target_info->intermediate_path); | |
| 680 } | |
| OLD | NEW |