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/drive_download_observer.h" | 58 #include "chrome/browser/chromeos/gdata/drive_download_observer.h" |
58 #include "chrome/browser/chromeos/gdata/drive_file_system_util.h" | 59 #include "chrome/browser/chromeos/gdata/drive_file_system_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 FilePath::CharType kWebIntentsFileExtension[] = |
| 81 FILE_PATH_LITERAL(".webintents"); |
| 82 |
77 // The state of a safebrowsing check. | 83 // The state of a safebrowsing check. |
78 class SafeBrowsingState : public DownloadCompletionBlocker { | 84 class SafeBrowsingState : public DownloadCompletionBlocker { |
79 public: | 85 public: |
80 SafeBrowsingState() | 86 SafeBrowsingState() |
81 : verdict_(DownloadProtectionService::SAFE) { | 87 : verdict_(DownloadProtectionService::SAFE) { |
82 } | 88 } |
83 | 89 |
84 virtual ~SafeBrowsingState(); | 90 virtual ~SafeBrowsingState(); |
85 | 91 |
86 // The verdict that we got from calling CheckClientDownload. Only valid to | 92 // 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)); | 117 l10n_util::GetStringUTF8(IDS_DEFAULT_DOWNLOAD_FILENAME)); |
112 | 118 |
113 *generated_name = net::GenerateFileName(download_item.GetURL(), | 119 *generated_name = net::GenerateFileName(download_item.GetURL(), |
114 download_item.GetContentDisposition(), | 120 download_item.GetContentDisposition(), |
115 download_item.GetReferrerCharset(), | 121 download_item.GetReferrerCharset(), |
116 download_item.GetSuggestedFilename(), | 122 download_item.GetSuggestedFilename(), |
117 download_item.GetMimeType(), | 123 download_item.GetMimeType(), |
118 default_file_name); | 124 default_file_name); |
119 } | 125 } |
120 | 126 |
| 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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 intent_data.extra_data.insert(make_pair( | 451 intent_data.extra_data.insert(make_pair( |
430 ASCIIToUTF16("url"), ASCIIToUTF16(item->GetURL().spec()))); | 452 ASCIIToUTF16("url"), ASCIIToUTF16(item->GetURL().spec()))); |
431 | 453 |
432 // Pass the downloaded filename to the service app as the name hint. | 454 // Pass the downloaded filename to the service app as the name hint. |
433 intent_data.extra_data.insert( | 455 intent_data.extra_data.insert( |
434 make_pair(ASCIIToUTF16("filename"), | 456 make_pair(ASCIIToUTF16("filename"), |
435 item->GetFileNameToReportUser().LossyDisplayName())); | 457 item->GetFileNameToReportUser().LossyDisplayName())); |
436 | 458 |
437 content::WebIntentsDispatcher* dispatcher = | 459 content::WebIntentsDispatcher* dispatcher = |
438 content::WebIntentsDispatcher::Create(intent_data); | 460 content::WebIntentsDispatcher::Create(intent_data); |
| 461 dispatcher->RegisterReplyNotification( |
| 462 base::Bind(&OnWebIntentDispatchCompleted, item->GetFullPath())); |
439 // TODO(gbillock): try to get this to be able to delegate to the Browser | 463 // TODO(gbillock): try to get this to be able to delegate to the Browser |
440 // object directly, passing a NULL WebContents? | 464 // object directly, passing a NULL WebContents? |
441 item->GetWebContents()->GetDelegate()->WebIntentDispatch( | 465 item->GetWebContents()->GetDelegate()->WebIntentDispatch( |
442 item->GetWebContents(), dispatcher); | 466 item->GetWebContents(), dispatcher); |
443 } | 467 } |
444 | 468 |
445 bool ChromeDownloadManagerDelegate::GenerateFileHash() { | 469 bool ChromeDownloadManagerDelegate::GenerateFileHash() { |
446 #if defined(ENABLE_SAFE_BROWSING) | 470 #if defined(ENABLE_SAFE_BROWSING) |
447 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && | 471 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && |
448 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); | 472 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()) | 741 if (should_prompt && !last_download_path_.empty()) |
718 target_directory = last_download_path_; | 742 target_directory = last_download_path_; |
719 else | 743 else |
720 target_directory = download_prefs_->DownloadPath(); | 744 target_directory = download_prefs_->DownloadPath(); |
721 suggested_path = target_directory.Append(generated_name); | 745 suggested_path = target_directory.Append(generated_name); |
722 } else { | 746 } else { |
723 DCHECK(!should_prompt); | 747 DCHECK(!should_prompt); |
724 suggested_path = download->GetForcedFilePath(); | 748 suggested_path = download->GetForcedFilePath(); |
725 } | 749 } |
726 | 750 |
| 751 // If we will open the file with a web intents dispatch, |
| 752 // give it a name that will not allow the OS to open it using usual |
| 753 // associated apps. |
| 754 if (ShouldOpenWithWebIntents(download)) { |
| 755 download->SetDisplayName(suggested_path.BaseName()); |
| 756 suggested_path = suggested_path.AddExtension(kWebIntentsFileExtension); |
| 757 } |
| 758 |
727 // If the download hasn't already been marked dangerous (could be | 759 // If the download hasn't already been marked dangerous (could be |
728 // DANGEROUS_URL), check if it is a dangerous file. | 760 // DANGEROUS_URL), check if it is a dangerous file. |
729 if (danger_type == content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) { | 761 if (danger_type == content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) { |
730 if (!should_prompt && !is_forced_path && | 762 if (!should_prompt && !is_forced_path && |
731 IsDangerousFile(*download, suggested_path, visited_referrer_before)) { | 763 IsDangerousFile(*download, suggested_path, visited_referrer_before)) { |
732 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE; | 764 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE; |
733 } | 765 } |
734 | 766 |
735 #if defined(ENABLE_SAFE_BROWSING) | 767 #if defined(ENABLE_SAFE_BROWSING) |
736 DownloadProtectionService* service = GetDownloadProtectionService(); | 768 DownloadProtectionService* service = GetDownloadProtectionService(); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
856 int32 download_id, int64 db_handle) { | 888 int32 download_id, int64 db_handle) { |
857 // It's not immediately obvious, but HistoryBackend::CreateDownload() can | 889 // It's not immediately obvious, but HistoryBackend::CreateDownload() can |
858 // call this function with an invalid |db_handle|. For instance, this can | 890 // call this function with an invalid |db_handle|. For instance, this can |
859 // happen when the history database is offline. We cannot have multiple | 891 // 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 | 892 // DownloadItems with the same invalid db_handle, so we need to assign a |
861 // unique |db_handle| here. | 893 // unique |db_handle| here. |
862 if (db_handle == DownloadItem::kUninitializedHandle) | 894 if (db_handle == DownloadItem::kUninitializedHandle) |
863 db_handle = download_history_->GetNextFakeDbHandle(); | 895 db_handle = download_history_->GetNextFakeDbHandle(); |
864 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); | 896 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); |
865 } | 897 } |
OLD | NEW |