Chromium Code Reviews| 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 | |
|
asanka
2012/08/17 15:26:42
Nit: extra space.
Greg Billock
2012/08/17 23:22:21
Done.
| |
| 127 // Needed to give PostTask a void closure in OnWebIntentDispatchCompleted. | |
| 128 void DeleteFile(const FilePath& file_path) { | |
| 129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 130 file_util::Delete(file_path, false); | |
| 131 } | |
| 132 | |
| 133 // Called when the web intents dispatch has completed. Deletes the |file_path|. | |
| 134 void OnWebIntentDispatchCompleted( | |
| 135 const FilePath& file_path, | |
| 136 webkit_glue::WebIntentReplyType intent_reply) { | |
| 137 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 138 base::Bind(&DeleteFile, file_path)); | |
| 139 } | |
| 140 | |
| 121 } // namespace | 141 } // namespace |
| 122 | 142 |
| 123 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) | 143 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) |
| 124 : profile_(profile), | 144 : profile_(profile), |
| 125 next_download_id_(0), | 145 next_download_id_(0), |
| 126 download_prefs_(new DownloadPrefs(profile)) { | 146 download_prefs_(new DownloadPrefs(profile)) { |
| 127 } | 147 } |
| 128 | 148 |
| 129 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { | 149 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { |
| 130 } | 150 } |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 364 return false; | 384 return false; |
| 365 } | 385 } |
| 366 | 386 |
| 367 return true; | 387 return true; |
| 368 } | 388 } |
| 369 | 389 |
| 370 bool ChromeDownloadManagerDelegate::ShouldOpenWithWebIntents( | 390 bool ChromeDownloadManagerDelegate::ShouldOpenWithWebIntents( |
| 371 const DownloadItem* item) { | 391 const DownloadItem* item) { |
| 372 if (!item->GetWebContents() || !item->GetWebContents()->GetDelegate()) | 392 if (!item->GetWebContents() || !item->GetWebContents()->GetDelegate()) |
| 373 return false; | 393 return false; |
| 394 if (!item->GetForcedFilePath().empty()) | |
| 395 return false; | |
| 374 | 396 |
| 375 std::string mime_type = item->GetMimeType(); | 397 std::string mime_type = item->GetMimeType(); |
| 376 if (mime_type == "application/rss+xml" || | 398 if (mime_type == "application/rss+xml" || |
| 377 mime_type == "application/atom+xml") { | 399 mime_type == "application/atom+xml") { |
| 378 return true; | 400 return true; |
| 379 } | 401 } |
| 380 | 402 |
| 381 #if defined(OS_CHROMEOS) | 403 #if defined(OS_CHROMEOS) |
| 382 if (mime_type == "application/msword" || | 404 if (mime_type == "application/msword" || |
| 383 mime_type == "application/vnd.ms-powerpoint" || | 405 mime_type == "application/vnd.ms-powerpoint" || |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 408 intent_data.extra_data.insert(make_pair( | 430 intent_data.extra_data.insert(make_pair( |
| 409 ASCIIToUTF16("url"), ASCIIToUTF16(item->GetURL().spec()))); | 431 ASCIIToUTF16("url"), ASCIIToUTF16(item->GetURL().spec()))); |
| 410 | 432 |
| 411 // Pass the downloaded filename to the service app as the name hint. | 433 // Pass the downloaded filename to the service app as the name hint. |
| 412 intent_data.extra_data.insert( | 434 intent_data.extra_data.insert( |
| 413 make_pair(ASCIIToUTF16("filename"), | 435 make_pair(ASCIIToUTF16("filename"), |
| 414 item->GetFileNameToReportUser().LossyDisplayName())); | 436 item->GetFileNameToReportUser().LossyDisplayName())); |
| 415 | 437 |
| 416 content::WebIntentsDispatcher* dispatcher = | 438 content::WebIntentsDispatcher* dispatcher = |
| 417 content::WebIntentsDispatcher::Create(intent_data); | 439 content::WebIntentsDispatcher::Create(intent_data); |
| 440 dispatcher->RegisterReplyNotification( | |
| 441 base::Bind(&OnWebIntentDispatchCompleted, item->GetFullPath())); | |
| 418 // TODO(gbillock): try to get this to be able to delegate to the Browser | 442 // TODO(gbillock): try to get this to be able to delegate to the Browser |
| 419 // object directly, passing a NULL WebContents? | 443 // object directly, passing a NULL WebContents? |
| 420 item->GetWebContents()->GetDelegate()->WebIntentDispatch( | 444 item->GetWebContents()->GetDelegate()->WebIntentDispatch( |
| 421 item->GetWebContents(), dispatcher); | 445 item->GetWebContents(), dispatcher); |
| 422 } | 446 } |
| 423 | 447 |
| 424 bool ChromeDownloadManagerDelegate::GenerateFileHash() { | 448 bool ChromeDownloadManagerDelegate::GenerateFileHash() { |
| 425 #if defined(ENABLE_SAFE_BROWSING) | 449 #if defined(ENABLE_SAFE_BROWSING) |
| 426 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && | 450 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && |
| 427 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); | 451 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 696 if (should_prompt && !last_download_path_.empty()) | 720 if (should_prompt && !last_download_path_.empty()) |
| 697 target_directory = last_download_path_; | 721 target_directory = last_download_path_; |
| 698 else | 722 else |
| 699 target_directory = download_prefs_->DownloadPath(); | 723 target_directory = download_prefs_->DownloadPath(); |
| 700 suggested_path = target_directory.Append(generated_name); | 724 suggested_path = target_directory.Append(generated_name); |
| 701 } else { | 725 } else { |
| 702 DCHECK(!should_prompt); | 726 DCHECK(!should_prompt); |
| 703 suggested_path = download->GetForcedFilePath(); | 727 suggested_path = download->GetForcedFilePath(); |
| 704 } | 728 } |
| 705 | 729 |
| 730 // If we will open the file with a web intents dispatch, | |
| 731 // give it a name that will not allow the OS to open it using usual | |
| 732 // associated apps. | |
| 733 if (ShouldOpenWithWebIntents(download)) { | |
| 734 download->SetDisplayName(suggested_path.BaseName()); | |
| 735 suggested_path = suggested_path.AddExtension(kWebIntentsFileExtension); | |
| 736 } | |
| 737 | |
| 706 // If the download hasn't already been marked dangerous (could be | 738 // If the download hasn't already been marked dangerous (could be |
| 707 // DANGEROUS_URL), check if it is a dangerous file. | 739 // DANGEROUS_URL), check if it is a dangerous file. |
| 708 if (danger_type == content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) { | 740 if (danger_type == content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) { |
| 709 if (!should_prompt && !is_forced_path && | 741 if (!should_prompt && !is_forced_path && |
| 710 IsDangerousFile(*download, suggested_path, visited_referrer_before)) { | 742 IsDangerousFile(*download, suggested_path, visited_referrer_before)) { |
| 711 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE; | 743 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE; |
| 712 } | 744 } |
| 713 | 745 |
| 714 #if defined(ENABLE_SAFE_BROWSING) | 746 #if defined(ENABLE_SAFE_BROWSING) |
| 715 DownloadProtectionService* service = GetDownloadProtectionService(); | 747 DownloadProtectionService* service = GetDownloadProtectionService(); |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 835 int32 download_id, int64 db_handle) { | 867 int32 download_id, int64 db_handle) { |
| 836 // It's not immediately obvious, but HistoryBackend::CreateDownload() can | 868 // It's not immediately obvious, but HistoryBackend::CreateDownload() can |
| 837 // call this function with an invalid |db_handle|. For instance, this can | 869 // call this function with an invalid |db_handle|. For instance, this can |
| 838 // happen when the history database is offline. We cannot have multiple | 870 // happen when the history database is offline. We cannot have multiple |
| 839 // DownloadItems with the same invalid db_handle, so we need to assign a | 871 // DownloadItems with the same invalid db_handle, so we need to assign a |
| 840 // unique |db_handle| here. | 872 // unique |db_handle| here. |
| 841 if (db_handle == DownloadItem::kUninitializedHandle) | 873 if (db_handle == DownloadItem::kUninitializedHandle) |
| 842 db_handle = download_history_->GetNextFakeDbHandle(); | 874 db_handle = download_history_->GetNextFakeDbHandle(); |
| 843 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); | 875 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); |
| 844 } | 876 } |
| OLD | NEW |