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" |
(...skipping 11 matching lines...) Expand all Loading... |
71 using content::DownloadManager; | 72 using content::DownloadManager; |
72 using content::WebContents; | 73 using content::WebContents; |
73 using safe_browsing::DownloadProtectionService; | 74 using safe_browsing::DownloadProtectionService; |
74 | 75 |
75 namespace { | 76 namespace { |
76 | 77 |
77 // String pointer used for identifying safebrowing data associated with | 78 // String pointer used for identifying safebrowing data associated with |
78 // a download item. | 79 // a download item. |
79 static const char safe_browsing_id[] = "Safe Browsing ID"; | 80 static const char safe_browsing_id[] = "Safe Browsing ID"; |
80 | 81 |
| 82 // String pointer used to set the local file extension to be used when a |
| 83 // download is going to be dispatched with web intents. |
| 84 static const FilePath::CharType kWebIntentsFileExtension[] = |
| 85 FILE_PATH_LITERAL(".webintents"); |
| 86 |
81 // The state of a safebrowsing check. | 87 // The state of a safebrowsing check. |
82 class SafeBrowsingState : public DownloadCompletionBlocker { | 88 class SafeBrowsingState : public DownloadCompletionBlocker { |
83 public: | 89 public: |
84 SafeBrowsingState() | 90 SafeBrowsingState() |
85 : verdict_(DownloadProtectionService::SAFE) { | 91 : verdict_(DownloadProtectionService::SAFE) { |
86 } | 92 } |
87 | 93 |
88 virtual ~SafeBrowsingState(); | 94 virtual ~SafeBrowsingState(); |
89 | 95 |
90 // The verdict that we got from calling CheckClientDownload. Only valid to | 96 // The verdict that we got from calling CheckClientDownload. Only valid to |
(...skipping 24 matching lines...) Expand all Loading... |
115 l10n_util::GetStringUTF8(IDS_DEFAULT_DOWNLOAD_FILENAME)); | 121 l10n_util::GetStringUTF8(IDS_DEFAULT_DOWNLOAD_FILENAME)); |
116 | 122 |
117 *generated_name = net::GenerateFileName(download_item.GetURL(), | 123 *generated_name = net::GenerateFileName(download_item.GetURL(), |
118 download_item.GetContentDisposition(), | 124 download_item.GetContentDisposition(), |
119 download_item.GetReferrerCharset(), | 125 download_item.GetReferrerCharset(), |
120 download_item.GetSuggestedFilename(), | 126 download_item.GetSuggestedFilename(), |
121 download_item.GetMimeType(), | 127 download_item.GetMimeType(), |
122 default_file_name); | 128 default_file_name); |
123 } | 129 } |
124 | 130 |
| 131 // Needed to give PostTask a void closure in OnWebIntentDispatchCompleted. |
| 132 void DeleteFile(const FilePath& file_path) { |
| 133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 134 file_util::Delete(file_path, false); |
| 135 } |
| 136 |
| 137 // Called when the web intents dispatch has completed. Deletes the |file_path|. |
| 138 void OnWebIntentDispatchCompleted( |
| 139 const FilePath& file_path, |
| 140 webkit_glue::WebIntentReplyType intent_reply) { |
| 141 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 142 base::Bind(&DeleteFile, file_path)); |
| 143 } |
| 144 |
125 } // namespace | 145 } // namespace |
126 | 146 |
127 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) | 147 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) |
128 : profile_(profile), | 148 : profile_(profile), |
129 next_download_id_(0), | 149 next_download_id_(0), |
130 download_prefs_(new DownloadPrefs(profile)) { | 150 download_prefs_(new DownloadPrefs(profile)) { |
131 } | 151 } |
132 | 152 |
133 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { | 153 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { |
134 } | 154 } |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 return false; | 385 return false; |
366 } | 386 } |
367 | 387 |
368 return true; | 388 return true; |
369 } | 389 } |
370 | 390 |
371 bool ChromeDownloadManagerDelegate::ShouldOpenWithWebIntents( | 391 bool ChromeDownloadManagerDelegate::ShouldOpenWithWebIntents( |
372 const DownloadItem* item) { | 392 const DownloadItem* item) { |
373 if (!item->GetWebContents() || !item->GetWebContents()->GetDelegate()) | 393 if (!item->GetWebContents() || !item->GetWebContents()->GetDelegate()) |
374 return false; | 394 return false; |
| 395 if (!item->GetForcedFilePath().empty()) |
| 396 return false; |
375 | 397 |
376 std::string mime_type = item->GetMimeType(); | 398 std::string mime_type = item->GetMimeType(); |
377 if (mime_type == "application/rss+xml" || | 399 if (mime_type == "application/rss+xml" || |
378 mime_type == "application/atom+xml") { | 400 mime_type == "application/atom+xml") { |
379 return true; | 401 return true; |
380 } | 402 } |
381 | 403 |
382 #if defined(OS_CHROMEOS) | 404 #if defined(OS_CHROMEOS) |
383 if (mime_type == "application/msword" || | 405 if (mime_type == "application/msword" || |
384 mime_type == "application/vnd.ms-powerpoint" || | 406 mime_type == "application/vnd.ms-powerpoint" || |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
430 intent_data.extra_data.insert(make_pair( | 452 intent_data.extra_data.insert(make_pair( |
431 ASCIIToUTF16("url"), ASCIIToUTF16(item->GetURL().spec()))); | 453 ASCIIToUTF16("url"), ASCIIToUTF16(item->GetURL().spec()))); |
432 | 454 |
433 // Pass the downloaded filename to the service app as the name hint. | 455 // Pass the downloaded filename to the service app as the name hint. |
434 intent_data.extra_data.insert( | 456 intent_data.extra_data.insert( |
435 make_pair(ASCIIToUTF16("filename"), | 457 make_pair(ASCIIToUTF16("filename"), |
436 item->GetFileNameToReportUser().LossyDisplayName())); | 458 item->GetFileNameToReportUser().LossyDisplayName())); |
437 | 459 |
438 content::WebIntentsDispatcher* dispatcher = | 460 content::WebIntentsDispatcher* dispatcher = |
439 content::WebIntentsDispatcher::Create(intent_data); | 461 content::WebIntentsDispatcher::Create(intent_data); |
| 462 dispatcher->RegisterReplyNotification( |
| 463 base::Bind(&OnWebIntentDispatchCompleted, item->GetFullPath())); |
440 // TODO(gbillock): try to get this to be able to delegate to the Browser | 464 // TODO(gbillock): try to get this to be able to delegate to the Browser |
441 // object directly, passing a NULL WebContents? | 465 // object directly, passing a NULL WebContents? |
442 item->GetWebContents()->GetDelegate()->WebIntentDispatch( | 466 item->GetWebContents()->GetDelegate()->WebIntentDispatch( |
443 item->GetWebContents(), dispatcher); | 467 item->GetWebContents(), dispatcher); |
444 } | 468 } |
445 | 469 |
446 bool ChromeDownloadManagerDelegate::GenerateFileHash() { | 470 bool ChromeDownloadManagerDelegate::GenerateFileHash() { |
447 #if defined(ENABLE_SAFE_BROWSING) | 471 #if defined(ENABLE_SAFE_BROWSING) |
448 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && | 472 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && |
449 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); | 473 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
711 if (should_prompt && !last_download_path_.empty()) | 735 if (should_prompt && !last_download_path_.empty()) |
712 target_directory = last_download_path_; | 736 target_directory = last_download_path_; |
713 else | 737 else |
714 target_directory = download_prefs_->DownloadPath(); | 738 target_directory = download_prefs_->DownloadPath(); |
715 suggested_path = target_directory.Append(generated_name); | 739 suggested_path = target_directory.Append(generated_name); |
716 } else { | 740 } else { |
717 DCHECK(!should_prompt); | 741 DCHECK(!should_prompt); |
718 suggested_path = download->GetForcedFilePath(); | 742 suggested_path = download->GetForcedFilePath(); |
719 } | 743 } |
720 | 744 |
| 745 // If we will open the file with a web intents dispatch, |
| 746 // give it a name that will not allow the OS to open it using usual |
| 747 // associated apps. |
| 748 if (ShouldOpenWithWebIntents(download)) { |
| 749 download->SetDisplayName(suggested_path.BaseName()); |
| 750 suggested_path = suggested_path.AddExtension(kWebIntentsFileExtension); |
| 751 } |
| 752 |
721 // If the download hasn't already been marked dangerous (could be | 753 // If the download hasn't already been marked dangerous (could be |
722 // DANGEROUS_URL), check if it is a dangerous file. | 754 // DANGEROUS_URL), check if it is a dangerous file. |
723 if (danger_type == content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) { | 755 if (danger_type == content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) { |
724 if (!should_prompt && !is_forced_path && | 756 if (!should_prompt && !is_forced_path && |
725 IsDangerousFile(*download, suggested_path, visited_referrer_before)) { | 757 IsDangerousFile(*download, suggested_path, visited_referrer_before)) { |
726 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE; | 758 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE; |
727 } | 759 } |
728 | 760 |
729 #if defined(ENABLE_SAFE_BROWSING) | 761 #if defined(ENABLE_SAFE_BROWSING) |
730 DownloadProtectionService* service = GetDownloadProtectionService(); | 762 DownloadProtectionService* service = GetDownloadProtectionService(); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
850 int32 download_id, int64 db_handle) { | 882 int32 download_id, int64 db_handle) { |
851 // It's not immediately obvious, but HistoryBackend::CreateDownload() can | 883 // It's not immediately obvious, but HistoryBackend::CreateDownload() can |
852 // call this function with an invalid |db_handle|. For instance, this can | 884 // call this function with an invalid |db_handle|. For instance, this can |
853 // happen when the history database is offline. We cannot have multiple | 885 // happen when the history database is offline. We cannot have multiple |
854 // DownloadItems with the same invalid db_handle, so we need to assign a | 886 // DownloadItems with the same invalid db_handle, so we need to assign a |
855 // unique |db_handle| here. | 887 // unique |db_handle| here. |
856 if (db_handle == DownloadItem::kUninitializedHandle) | 888 if (db_handle == DownloadItem::kUninitializedHandle) |
857 db_handle = download_history_->GetNextFakeDbHandle(); | 889 db_handle = download_history_->GetNextFakeDbHandle(); |
858 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); | 890 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); |
859 } | 891 } |
OLD | NEW |