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 14 matching lines...) Expand all Loading... |
25 #include "chrome/browser/download/download_prefs.h" | 25 #include "chrome/browser/download/download_prefs.h" |
26 #include "chrome/browser/download/download_status_updater.h" | 26 #include "chrome/browser/download/download_status_updater.h" |
27 #include "chrome/browser/download/download_util.h" | 27 #include "chrome/browser/download/download_util.h" |
28 #include "chrome/browser/download/save_package_file_picker.h" | 28 #include "chrome/browser/download/save_package_file_picker.h" |
29 #include "chrome/browser/extensions/api/downloads/downloads_api.h" | 29 #include "chrome/browser/extensions/api/downloads/downloads_api.h" |
30 #include "chrome/browser/extensions/crx_installer.h" | 30 #include "chrome/browser/extensions/crx_installer.h" |
31 #include "chrome/browser/extensions/extension_service.h" | 31 #include "chrome/browser/extensions/extension_service.h" |
32 #include "chrome/browser/extensions/extension_system.h" | 32 #include "chrome/browser/extensions/extension_system.h" |
33 #include "chrome/browser/history/history_service.h" | 33 #include "chrome/browser/history/history_service.h" |
34 #include "chrome/browser/history/history_service_factory.h" | 34 #include "chrome/browser/history/history_service_factory.h" |
35 #include "chrome/browser/intents/web_intents_util.h" | |
36 #include "chrome/browser/platform_util.h" | 35 #include "chrome/browser/platform_util.h" |
37 #include "chrome/browser/prefs/pref_service.h" | 36 #include "chrome/browser/prefs/pref_service.h" |
38 #include "chrome/browser/profiles/profile.h" | 37 #include "chrome/browser/profiles/profile.h" |
39 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 38 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
40 #include "chrome/browser/ui/browser.h" | 39 #include "chrome/browser/ui/browser.h" |
41 #include "chrome/browser/ui/host_desktop.h" | 40 #include "chrome/browser/ui/host_desktop.h" |
42 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 41 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
43 #include "chrome/common/chrome_notification_types.h" | 42 #include "chrome/common/chrome_notification_types.h" |
44 #include "chrome/common/extensions/feature_switch.h" | 43 #include "chrome/common/extensions/feature_switch.h" |
45 #include "chrome/common/extensions/user_script.h" | 44 #include "chrome/common/extensions/user_script.h" |
46 #include "chrome/common/pref_names.h" | 45 #include "chrome/common/pref_names.h" |
47 #include "content/public/browser/download_item.h" | 46 #include "content/public/browser/download_item.h" |
48 #include "content/public/browser/download_manager.h" | 47 #include "content/public/browser/download_manager.h" |
49 #include "content/public/browser/notification_source.h" | 48 #include "content/public/browser/notification_source.h" |
50 #include "content/public/browser/web_contents.h" | 49 #include "content/public/browser/web_contents.h" |
51 #include "content/public/browser/web_contents_delegate.h" | 50 #include "content/public/browser/web_contents_delegate.h" |
52 #include "content/public/browser/web_intents_dispatcher.h" | |
53 #include "grit/generated_resources.h" | 51 #include "grit/generated_resources.h" |
54 #include "net/base/net_util.h" | 52 #include "net/base/net_util.h" |
55 #include "ui/base/l10n/l10n_util.h" | 53 #include "ui/base/l10n/l10n_util.h" |
56 #include "webkit/glue/web_intent_data.h" | |
57 #include "webkit/glue/web_intent_reply_data.h" | |
58 | 54 |
59 #if !defined(OS_ANDROID) | 55 #if !defined(OS_ANDROID) |
60 #include "chrome/browser/ui/browser.h" | 56 #include "chrome/browser/ui/browser.h" |
61 #include "chrome/browser/ui/browser_finder.h" | 57 #include "chrome/browser/ui/browser_finder.h" |
62 #endif | 58 #endif |
63 | 59 |
64 #if defined(OS_CHROMEOS) | 60 #if defined(OS_CHROMEOS) |
65 #include "chrome/browser/chromeos/drive/drive_download_handler.h" | 61 #include "chrome/browser/chromeos/drive/drive_download_handler.h" |
66 #include "chrome/browser/chromeos/drive/drive_file_system_util.h" | 62 #include "chrome/browser/chromeos/drive/drive_file_system_util.h" |
67 #include "chrome/browser/download/download_file_picker_chromeos.h" | 63 #include "chrome/browser/download/download_file_picker_chromeos.h" |
68 #include "chrome/browser/download/save_package_file_picker_chromeos.h" | 64 #include "chrome/browser/download/save_package_file_picker_chromeos.h" |
69 #endif | 65 #endif |
70 | 66 |
71 using content::BrowserContext; | 67 using content::BrowserContext; |
72 using content::BrowserThread; | 68 using content::BrowserThread; |
73 using content::DownloadId; | 69 using content::DownloadId; |
74 using content::DownloadItem; | 70 using content::DownloadItem; |
75 using content::DownloadManager; | 71 using content::DownloadManager; |
76 using content::WebContents; | 72 using content::WebContents; |
77 using safe_browsing::DownloadProtectionService; | 73 using safe_browsing::DownloadProtectionService; |
78 | 74 |
79 namespace { | 75 namespace { |
80 | 76 |
81 // String pointer used for identifying safebrowing data associated with | 77 // String pointer used for identifying safebrowing data associated with |
82 // a download item. | 78 // a download item. |
83 static const char safe_browsing_id[] = "Safe Browsing ID"; | 79 static const char safe_browsing_id[] = "Safe Browsing ID"; |
84 | 80 |
85 // String pointer used to set the local file extension to be used when a | |
86 // download is going to be dispatched with web intents. | |
87 static const FilePath::CharType kWebIntentsFileExtension[] = | |
88 FILE_PATH_LITERAL(".webintents"); | |
89 | |
90 // The state of a safebrowsing check. | 81 // The state of a safebrowsing check. |
91 class SafeBrowsingState : public DownloadCompletionBlocker { | 82 class SafeBrowsingState : public DownloadCompletionBlocker { |
92 public: | 83 public: |
93 SafeBrowsingState() | 84 SafeBrowsingState() |
94 : verdict_(DownloadProtectionService::SAFE) { | 85 : verdict_(DownloadProtectionService::SAFE) { |
95 } | 86 } |
96 | 87 |
97 virtual ~SafeBrowsingState(); | 88 virtual ~SafeBrowsingState(); |
98 | 89 |
99 // The verdict that we got from calling CheckClientDownload. Only valid to | 90 // The verdict that we got from calling CheckClientDownload. Only valid to |
(...skipping 25 matching lines...) Expand all Loading... |
125 l10n_util::GetStringUTF8(IDS_DEFAULT_DOWNLOAD_FILENAME)); | 116 l10n_util::GetStringUTF8(IDS_DEFAULT_DOWNLOAD_FILENAME)); |
126 | 117 |
127 *generated_name = net::GenerateFileName(download_item.GetURL(), | 118 *generated_name = net::GenerateFileName(download_item.GetURL(), |
128 download_item.GetContentDisposition(), | 119 download_item.GetContentDisposition(), |
129 referrer_charset, | 120 referrer_charset, |
130 download_item.GetSuggestedFilename(), | 121 download_item.GetSuggestedFilename(), |
131 download_item.GetMimeType(), | 122 download_item.GetMimeType(), |
132 default_file_name); | 123 default_file_name); |
133 } | 124 } |
134 | 125 |
135 // Needed to give PostTask a void closure in OnWebIntentDispatchCompleted. | |
136 void DeleteFile(const FilePath& file_path) { | |
137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
138 file_util::Delete(file_path, false); | |
139 } | |
140 | |
141 // Called when the web intents dispatch has completed. Deletes the |file_path|. | |
142 void OnWebIntentDispatchCompleted( | |
143 const FilePath& file_path, | |
144 webkit_glue::WebIntentReplyType intent_reply) { | |
145 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
146 base::Bind(&DeleteFile, file_path)); | |
147 } | |
148 | |
149 typedef base::Callback<void(bool)> VisitedBeforeCallback; | 126 typedef base::Callback<void(bool)> VisitedBeforeCallback; |
150 | 127 |
151 // Condenses the results from HistoryService::GetVisibleVisitCountToHost() to a | 128 // Condenses the results from HistoryService::GetVisibleVisitCountToHost() to a |
152 // single bool so that VisitedBeforeCallback can curry up to 5 other parameters | 129 // single bool so that VisitedBeforeCallback can curry up to 5 other parameters |
153 // without a struct. | 130 // without a struct. |
154 void VisitCountsToVisitedBefore( | 131 void VisitCountsToVisitedBefore( |
155 const VisitedBeforeCallback& callback, | 132 const VisitedBeforeCallback& callback, |
156 HistoryService::Handle unused_handle, | 133 HistoryService::Handle unused_handle, |
157 bool found_visits, | 134 bool found_visits, |
158 int count, | 135 int count, |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 content::Source<extensions::CrxInstaller>(crx_installer.get())); | 361 content::Source<extensions::CrxInstaller>(crx_installer.get())); |
385 | 362 |
386 crx_installers_[crx_installer.get()] = callback; | 363 crx_installers_[crx_installer.get()] = callback; |
387 // The status text and percent complete indicator will change now | 364 // The status text and percent complete indicator will change now |
388 // that we are installing a CRX. Update observers so that they pick | 365 // that we are installing a CRX. Update observers so that they pick |
389 // up the change. | 366 // up the change. |
390 item->UpdateObservers(); | 367 item->UpdateObservers(); |
391 return false; | 368 return false; |
392 } | 369 } |
393 | 370 |
394 #if defined(ENABLE_WEB_INTENTS) | |
395 if (ShouldOpenWithWebIntents(item)) { | |
396 OpenWithWebIntent(item); | |
397 callback.Run(true); | |
398 return false; | |
399 } | |
400 #endif | |
401 | |
402 return true; | 371 return true; |
403 } | 372 } |
404 | 373 |
405 #if defined(ENABLE_WEB_INTENTS) | |
406 bool ChromeDownloadManagerDelegate::ShouldOpenWithWebIntents( | |
407 const DownloadItem* item) { | |
408 if (!web_intents::IsWebIntentsEnabledForProfile(profile_)) | |
409 return false; | |
410 | |
411 if ((item->GetWebContents() && !item->GetWebContents()->GetDelegate()) && | |
412 !web_intents::GetBrowserForBackgroundWebIntentDelivery(profile_)) { | |
413 return false; | |
414 } | |
415 if (!item->GetForcedFilePath().empty()) | |
416 return false; | |
417 if (item->GetTargetDisposition() == DownloadItem::TARGET_DISPOSITION_PROMPT) | |
418 return false; | |
419 | |
420 #if !defined(OS_CHROMEOS) | |
421 std::string mime_type = item->GetMimeType(); | |
422 | |
423 // If QuickOffice extension is installed, and we're not on ChromeOS,use web | |
424 // intents to handle the downloaded file. | |
425 const char kQuickOfficeExtensionId[] = "gbkeegbaiigmenfmjfclcdgdpimamgkj"; | |
426 const char kQuickOfficeDevExtensionId[] = "ionpfmkccalenbmnddpbmocokhaknphg"; | |
427 ExtensionServiceInterface* extension_service = | |
428 extensions::ExtensionSystem::Get(profile_)->extension_service(); | |
429 | |
430 bool use_quickoffice = false; | |
431 if (extension_service && | |
432 extension_service->GetInstalledExtension(kQuickOfficeExtensionId) && | |
433 extension_service->IsExtensionEnabled(kQuickOfficeExtensionId)) | |
434 use_quickoffice = true; | |
435 | |
436 if (extension_service && | |
437 extension_service->GetInstalledExtension(kQuickOfficeDevExtensionId) && | |
438 extension_service->IsExtensionEnabled(kQuickOfficeDevExtensionId)) | |
439 use_quickoffice = true; | |
440 | |
441 if (use_quickoffice) { | |
442 if (mime_type == "application/msword" || | |
443 mime_type == "application/vnd.ms-powerpoint" || | |
444 mime_type == "application/vnd.ms-excel" || | |
445 mime_type == "application/vnd.openxmlformats-officedocument." | |
446 "wordprocessingml.document" || | |
447 mime_type == "application/vnd.openxmlformats-officedocument." | |
448 "presentationml.presentation" || | |
449 mime_type == "application/vnd.openxmlformats-officedocument." | |
450 "spreadsheetml.sheet") { | |
451 return true; | |
452 } | |
453 } | |
454 #endif // !defined(OS_CHROMEOS) | |
455 | |
456 return false; | |
457 } | |
458 | |
459 void ChromeDownloadManagerDelegate::OpenWithWebIntent( | |
460 const DownloadItem* item) { | |
461 webkit_glue::WebIntentData intent_data( | |
462 ASCIIToUTF16("http://webintents.org/view"), | |
463 ASCIIToUTF16(item->GetMimeType()), | |
464 item->GetFullPath(), | |
465 item->GetReceivedBytes()); | |
466 | |
467 // RCH specifies that the receiver gets the url, but with Web Intents | |
468 // it isn't really needed. | |
469 intent_data.extra_data.insert(make_pair( | |
470 ASCIIToUTF16("url"), ASCIIToUTF16(item->GetURL().spec()))); | |
471 | |
472 // Pass the downloaded filename to the service app as the name hint. | |
473 intent_data.extra_data.insert( | |
474 make_pair(ASCIIToUTF16("filename"), | |
475 item->GetFileNameToReportUser().LossyDisplayName())); | |
476 | |
477 content::WebIntentsDispatcher* dispatcher = | |
478 content::WebIntentsDispatcher::Create(intent_data); | |
479 dispatcher->RegisterReplyNotification( | |
480 base::Bind(&OnWebIntentDispatchCompleted, item->GetFullPath())); | |
481 | |
482 content::WebContentsDelegate* delegate = NULL; | |
483 #if !defined(OS_ANDROID) | |
484 if (item->GetWebContents() && item->GetWebContents()->GetDelegate()) { | |
485 delegate = item->GetWebContents()->GetDelegate(); | |
486 } else { | |
487 delegate = web_intents::GetBrowserForBackgroundWebIntentDelivery(profile_); | |
488 } | |
489 #endif // !defined(OS_ANDROID) | |
490 DCHECK(delegate); | |
491 delegate->WebIntentDispatch(NULL, dispatcher); | |
492 } | |
493 #endif | |
494 | |
495 bool ChromeDownloadManagerDelegate::GenerateFileHash() { | 374 bool ChromeDownloadManagerDelegate::GenerateFileHash() { |
496 #if defined(FULL_SAFE_BROWSING) | 375 #if defined(FULL_SAFE_BROWSING) |
497 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && | 376 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && |
498 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); | 377 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); |
499 #else | 378 #else |
500 return false; | 379 return false; |
501 #endif | 380 #endif |
502 } | 381 } |
503 | 382 |
504 void ChromeDownloadManagerDelegate::GetSaveDir(BrowserContext* browser_context, | 383 void ChromeDownloadManagerDelegate::GetSaveDir(BrowserContext* browser_context, |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
775 if (should_prompt && !last_download_path_.empty()) | 654 if (should_prompt && !last_download_path_.empty()) |
776 target_directory = last_download_path_; | 655 target_directory = last_download_path_; |
777 else | 656 else |
778 target_directory = download_prefs_->DownloadPath(); | 657 target_directory = download_prefs_->DownloadPath(); |
779 suggested_path = target_directory.Append(generated_name); | 658 suggested_path = target_directory.Append(generated_name); |
780 } else { | 659 } else { |
781 DCHECK(!should_prompt); | 660 DCHECK(!should_prompt); |
782 suggested_path = download->GetForcedFilePath(); | 661 suggested_path = download->GetForcedFilePath(); |
783 } | 662 } |
784 | 663 |
785 #if defined(ENABLE_WEB_INTENTS) | |
786 // If we will open the file with a web intents dispatch, | |
787 // give it a name that will not allow the OS to open it using usual | |
788 // associated apps. | |
789 if (ShouldOpenWithWebIntents(download)) { | |
790 download->SetDisplayName(suggested_path.BaseName()); | |
791 suggested_path = suggested_path.AddExtension(kWebIntentsFileExtension); | |
792 } | |
793 #endif | |
794 | |
795 // If the download hasn't already been marked dangerous (could be | 664 // If the download hasn't already been marked dangerous (could be |
796 // DANGEROUS_URL), check if it is a dangerous file. | 665 // DANGEROUS_URL), check if it is a dangerous file. |
797 if (danger_type == content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) { | 666 if (danger_type == content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) { |
798 if (!should_prompt && !is_forced_path && | 667 if (!should_prompt && !is_forced_path && |
799 IsDangerousFile(*download, suggested_path, visited_referrer_before)) { | 668 IsDangerousFile(*download, suggested_path, visited_referrer_before)) { |
800 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE; | 669 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE; |
801 } | 670 } |
802 | 671 |
803 #if defined(FULL_SAFE_BROWSING) | 672 #if defined(FULL_SAFE_BROWSING) |
804 DownloadProtectionService* service = GetDownloadProtectionService(); | 673 DownloadProtectionService* service = GetDownloadProtectionService(); |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
925 // TODO(asanka): This logic is a hack. DownloadFilePicker should give us a | 794 // TODO(asanka): This logic is a hack. DownloadFilePicker should give us a |
926 // directory to persist. Or perhaps, if the Drive path | 795 // directory to persist. Or perhaps, if the Drive path |
927 // substitution logic is moved here, then we would have a | 796 // substitution logic is moved here, then we would have a |
928 // persistable path after the DownloadFilePicker is done. | 797 // persistable path after the DownloadFilePicker is done. |
929 if (disposition == DownloadItem::TARGET_DISPOSITION_PROMPT && | 798 if (disposition == DownloadItem::TARGET_DISPOSITION_PROMPT && |
930 !download->IsTemporary()) | 799 !download->IsTemporary()) |
931 last_download_path_ = target_path.DirName(); | 800 last_download_path_ = target_path.DirName(); |
932 } | 801 } |
933 callback.Run(target_path, disposition, danger_type, intermediate_path); | 802 callback.Run(target_path, disposition, danger_type, intermediate_path); |
934 } | 803 } |
OLD | NEW |