| 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/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 #include "content/public/browser/download_item.h" | 40 #include "content/public/browser/download_item.h" |
| 41 #include "content/public/browser/download_manager.h" | 41 #include "content/public/browser/download_manager.h" |
| 42 #include "content/public/browser/notification_source.h" | 42 #include "content/public/browser/notification_source.h" |
| 43 #include "content/public/browser/web_contents.h" | 43 #include "content/public/browser/web_contents.h" |
| 44 #include "content/public/browser/web_contents_delegate.h" | 44 #include "content/public/browser/web_contents_delegate.h" |
| 45 #include "content/public/browser/web_intents_dispatcher.h" | 45 #include "content/public/browser/web_intents_dispatcher.h" |
| 46 #include "grit/generated_resources.h" | 46 #include "grit/generated_resources.h" |
| 47 #include "net/base/net_util.h" | 47 #include "net/base/net_util.h" |
| 48 #include "ui/base/l10n/l10n_util.h" | 48 #include "ui/base/l10n/l10n_util.h" |
| 49 #include "webkit/glue/web_intent_data.h" | 49 #include "webkit/glue/web_intent_data.h" |
| 50 #include "webkit/glue/web_intent_reply_data.h" |
| 50 | 51 |
| 51 #if !defined(OS_ANDROID) | 52 #if !defined(OS_ANDROID) |
| 52 #include "chrome/browser/ui/browser.h" | 53 #include "chrome/browser/ui/browser.h" |
| 53 #include "chrome/browser/ui/browser_finder.h" | 54 #include "chrome/browser/ui/browser_finder.h" |
| 54 #endif | 55 #endif |
| 55 | 56 |
| 56 #if defined(OS_CHROMEOS) | 57 #if defined(OS_CHROMEOS) |
| 57 #include "chrome/browser/chromeos/gdata/gdata_download_observer.h" | 58 #include "chrome/browser/chromeos/gdata/gdata_download_observer.h" |
| 58 #include "chrome/browser/chromeos/gdata/gdata_util.h" | 59 #include "chrome/browser/chromeos/gdata/gdata_util.h" |
| 59 #include "chrome/browser/download/download_file_picker_chromeos.h" | 60 #include "chrome/browser/download/download_file_picker_chromeos.h" |
| 60 #include "chrome/browser/download/save_package_file_picker_chromeos.h" | 61 #include "chrome/browser/download/save_package_file_picker_chromeos.h" |
| 61 #endif | 62 #endif |
| 62 | 63 |
| 63 using content::BrowserContext; | 64 using content::BrowserContext; |
| 64 using content::BrowserThread; | 65 using content::BrowserThread; |
| 65 using content::DownloadId; | 66 using content::DownloadId; |
| 66 using content::DownloadItem; | 67 using content::DownloadItem; |
| 67 using content::DownloadManager; | 68 using content::DownloadManager; |
| 68 using content::WebContents; | 69 using content::WebContents; |
| 69 using safe_browsing::DownloadProtectionService; | 70 using safe_browsing::DownloadProtectionService; |
| 70 | 71 |
| 71 namespace { | 72 namespace { |
| 72 | 73 |
| 73 // String pointer used for identifying safebrowing data associated with | 74 // String pointer used for identifying safebrowing data associated with |
| 74 // a download item. | 75 // a download item. |
| 75 static const char safe_browsing_id[] = "Safe Browsing ID"; | 76 static const char safe_browsing_id[] = "Safe Browsing ID"; |
| 76 | 77 |
| 78 // String pointer used to set the local file extension to be used when a |
| 79 // download is going to be dispatched with web intents. |
| 80 static const char kWebIntentsFileExtension[] = ".webintents"; |
| 81 |
| 77 // The state of a safebrowsing check. | 82 // The state of a safebrowsing check. |
| 78 class SafeBrowsingState : public DownloadCompletionBlocker { | 83 class SafeBrowsingState : public DownloadCompletionBlocker { |
| 79 public: | 84 public: |
| 80 SafeBrowsingState() | 85 SafeBrowsingState() |
| 81 : verdict_(DownloadProtectionService::SAFE) { | 86 : verdict_(DownloadProtectionService::SAFE) { |
| 82 } | 87 } |
| 83 | 88 |
| 84 virtual ~SafeBrowsingState(); | 89 virtual ~SafeBrowsingState(); |
| 85 | 90 |
| 86 // The verdict that we got from calling CheckClientDownload. Only valid to | 91 // The verdict that we got from calling CheckClientDownload. Only valid to |
| (...skipping 24 matching lines...) Expand all Loading... |
| 111 l10n_util::GetStringUTF8(IDS_DEFAULT_DOWNLOAD_FILENAME)); | 116 l10n_util::GetStringUTF8(IDS_DEFAULT_DOWNLOAD_FILENAME)); |
| 112 | 117 |
| 113 *generated_name = net::GenerateFileName(download_item.GetURL(), | 118 *generated_name = net::GenerateFileName(download_item.GetURL(), |
| 114 download_item.GetContentDisposition(), | 119 download_item.GetContentDisposition(), |
| 115 download_item.GetReferrerCharset(), | 120 download_item.GetReferrerCharset(), |
| 116 download_item.GetSuggestedFilename(), | 121 download_item.GetSuggestedFilename(), |
| 117 download_item.GetMimeType(), | 122 download_item.GetMimeType(), |
| 118 default_file_name); | 123 default_file_name); |
| 119 } | 124 } |
| 120 | 125 |
| 126 // Needed to give PostTask a void closure in OnWebIntentDispatchCompleted. |
| 127 void DeleteFile(const FilePath& file_path) { |
| 128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 129 file_util::Delete(file_path, false); |
| 130 } |
| 131 |
| 132 // Called when the web intents dispatch has completed. Deletes the |file_path|. |
| 133 void OnWebIntentDispatchCompleted( |
| 134 const FilePath& file_path, |
| 135 webkit_glue::WebIntentReplyType intent_reply) { |
| 136 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 137 base::Bind(&DeleteFile, file_path)); |
| 138 } |
| 139 |
| 121 } // namespace | 140 } // namespace |
| 122 | 141 |
| 123 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) | 142 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) |
| 124 : profile_(profile), | 143 : profile_(profile), |
| 125 next_download_id_(0), | 144 next_download_id_(0), |
| 126 download_prefs_(new DownloadPrefs(profile)) { | 145 download_prefs_(new DownloadPrefs(profile)) { |
| 127 } | 146 } |
| 128 | 147 |
| 129 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { | 148 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { |
| 130 } | 149 } |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 return false; | 383 return false; |
| 365 } | 384 } |
| 366 | 385 |
| 367 return true; | 386 return true; |
| 368 } | 387 } |
| 369 | 388 |
| 370 bool ChromeDownloadManagerDelegate::ShouldOpenWithWebIntents( | 389 bool ChromeDownloadManagerDelegate::ShouldOpenWithWebIntents( |
| 371 const DownloadItem* item) { | 390 const DownloadItem* item) { |
| 372 if (!item->GetWebContents() || !item->GetWebContents()->GetDelegate()) | 391 if (!item->GetWebContents() || !item->GetWebContents()->GetDelegate()) |
| 373 return false; | 392 return false; |
| 393 if (!item->GetForcedFilePath().empty()) |
| 394 return false; |
| 374 | 395 |
| 375 std::string mime_type = item->GetMimeType(); | 396 std::string mime_type = item->GetMimeType(); |
| 376 if (mime_type == "application/rss+xml" || | 397 if (mime_type == "application/rss+xml" || |
| 377 mime_type == "application/atom+xml") { | 398 mime_type == "application/atom+xml") { |
| 378 return true; | 399 return true; |
| 379 } | 400 } |
| 380 | 401 |
| 381 #if defined(OS_CHROMEOS) | 402 #if defined(OS_CHROMEOS) |
| 382 if (mime_type == "application/msword" || | 403 if (mime_type == "application/msword" || |
| 383 mime_type == "application/vnd.ms-powerpoint" || | 404 mime_type == "application/vnd.ms-powerpoint" || |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 intent_data.extra_data.insert(make_pair( | 450 intent_data.extra_data.insert(make_pair( |
| 430 ASCIIToUTF16("url"), ASCIIToUTF16(item->GetURL().spec()))); | 451 ASCIIToUTF16("url"), ASCIIToUTF16(item->GetURL().spec()))); |
| 431 | 452 |
| 432 // Pass the downloaded filename to the service app as the name hint. | 453 // Pass the downloaded filename to the service app as the name hint. |
| 433 intent_data.extra_data.insert( | 454 intent_data.extra_data.insert( |
| 434 make_pair(ASCIIToUTF16("filename"), | 455 make_pair(ASCIIToUTF16("filename"), |
| 435 item->GetFileNameToReportUser().LossyDisplayName())); | 456 item->GetFileNameToReportUser().LossyDisplayName())); |
| 436 | 457 |
| 437 content::WebIntentsDispatcher* dispatcher = | 458 content::WebIntentsDispatcher* dispatcher = |
| 438 content::WebIntentsDispatcher::Create(intent_data); | 459 content::WebIntentsDispatcher::Create(intent_data); |
| 460 dispatcher->RegisterReplyNotification( |
| 461 base::Bind(&OnWebIntentDispatchCompleted, item->GetFullPath())); |
| 439 // TODO(gbillock): try to get this to be able to delegate to the Browser | 462 // TODO(gbillock): try to get this to be able to delegate to the Browser |
| 440 // object directly, passing a NULL WebContents? | 463 // object directly, passing a NULL WebContents? |
| 441 item->GetWebContents()->GetDelegate()->WebIntentDispatch( | 464 item->GetWebContents()->GetDelegate()->WebIntentDispatch( |
| 442 item->GetWebContents(), dispatcher); | 465 item->GetWebContents(), dispatcher); |
| 443 } | 466 } |
| 444 | 467 |
| 445 bool ChromeDownloadManagerDelegate::GenerateFileHash() { | 468 bool ChromeDownloadManagerDelegate::GenerateFileHash() { |
| 446 #if defined(ENABLE_SAFE_BROWSING) | 469 #if defined(ENABLE_SAFE_BROWSING) |
| 447 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && | 470 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && |
| 448 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); | 471 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 717 if (should_prompt && !last_download_path_.empty()) | 740 if (should_prompt && !last_download_path_.empty()) |
| 718 target_directory = last_download_path_; | 741 target_directory = last_download_path_; |
| 719 else | 742 else |
| 720 target_directory = download_prefs_->DownloadPath(); | 743 target_directory = download_prefs_->DownloadPath(); |
| 721 suggested_path = target_directory.Append(generated_name); | 744 suggested_path = target_directory.Append(generated_name); |
| 722 } else { | 745 } else { |
| 723 DCHECK(!should_prompt); | 746 DCHECK(!should_prompt); |
| 724 suggested_path = download->GetForcedFilePath(); | 747 suggested_path = download->GetForcedFilePath(); |
| 725 } | 748 } |
| 726 | 749 |
| 750 // If we will open the file with a web intents dispatch, |
| 751 // give it a name that will not allow the OS to open it using usual |
| 752 // associated apps. |
| 753 if (ShouldOpenWithWebIntents(download)) { |
| 754 download->SetDisplayName(suggested_path.BaseName()); |
| 755 suggested_path = suggested_path.AddExtension(kWebIntentsFileExtension); |
| 756 } |
| 757 |
| 727 // If the download hasn't already been marked dangerous (could be | 758 // If the download hasn't already been marked dangerous (could be |
| 728 // DANGEROUS_URL), check if it is a dangerous file. | 759 // DANGEROUS_URL), check if it is a dangerous file. |
| 729 if (danger_type == content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) { | 760 if (danger_type == content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) { |
| 730 if (!should_prompt && !is_forced_path && | 761 if (!should_prompt && !is_forced_path && |
| 731 IsDangerousFile(*download, suggested_path, visited_referrer_before)) { | 762 IsDangerousFile(*download, suggested_path, visited_referrer_before)) { |
| 732 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE; | 763 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE; |
| 733 } | 764 } |
| 734 | 765 |
| 735 #if defined(ENABLE_SAFE_BROWSING) | 766 #if defined(ENABLE_SAFE_BROWSING) |
| 736 DownloadProtectionService* service = GetDownloadProtectionService(); | 767 DownloadProtectionService* service = GetDownloadProtectionService(); |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 856 int32 download_id, int64 db_handle) { | 887 int32 download_id, int64 db_handle) { |
| 857 // It's not immediately obvious, but HistoryBackend::CreateDownload() can | 888 // It's not immediately obvious, but HistoryBackend::CreateDownload() can |
| 858 // call this function with an invalid |db_handle|. For instance, this can | 889 // call this function with an invalid |db_handle|. For instance, this can |
| 859 // happen when the history database is offline. We cannot have multiple | 890 // happen when the history database is offline. We cannot have multiple |
| 860 // DownloadItems with the same invalid db_handle, so we need to assign a | 891 // DownloadItems with the same invalid db_handle, so we need to assign a |
| 861 // unique |db_handle| here. | 892 // unique |db_handle| here. |
| 862 if (db_handle == DownloadItem::kUninitializedHandle) | 893 if (db_handle == DownloadItem::kUninitializedHandle) |
| 863 db_handle = download_history_->GetNextFakeDbHandle(); | 894 db_handle = download_history_->GetNextFakeDbHandle(); |
| 864 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); | 895 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); |
| 865 } | 896 } |
| OLD | NEW |