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" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
13 #include "base/prefs/public/pref_member.h" | 13 #include "base/prefs/public/pref_member.h" |
14 #include "base/rand_util.h" | 14 #include "base/rand_util.h" |
15 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
16 #include "base/time.h" | 16 #include "base/time.h" |
17 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
18 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
19 #include "chrome/browser/download/download_completion_blocker.h" | 19 #include "chrome/browser/download/download_completion_blocker.h" |
20 #include "chrome/browser/download/download_crx_util.h" | 20 #include "chrome/browser/download/download_crx_util.h" |
21 #include "chrome/browser/download/download_extensions.h" | 21 #include "chrome/browser/download/download_extensions.h" |
22 #include "chrome/browser/download/download_file_picker.h" | 22 #include "chrome/browser/download/download_file_picker.h" |
23 #include "chrome/browser/download/download_history.h" | 23 #include "chrome/browser/download/download_history.h" |
24 #include "chrome/browser/download/download_path_reservation_tracker.h" | 24 #include "chrome/browser/download/download_path_reservation_tracker.h" |
25 #include "chrome/browser/download/download_prefs.h" | 25 #include "chrome/browser/download/download_prefs.h" |
| 26 #include "chrome/browser/download/download_service.h" |
| 27 #include "chrome/browser/download/download_service_factory.h" |
26 #include "chrome/browser/download/download_status_updater.h" | 28 #include "chrome/browser/download/download_status_updater.h" |
27 #include "chrome/browser/download/download_util.h" | 29 #include "chrome/browser/download/download_util.h" |
28 #include "chrome/browser/download/save_package_file_picker.h" | 30 #include "chrome/browser/download/save_package_file_picker.h" |
29 #include "chrome/browser/extensions/api/downloads/downloads_api.h" | 31 #include "chrome/browser/extensions/api/downloads/downloads_api.h" |
30 #include "chrome/browser/extensions/crx_installer.h" | 32 #include "chrome/browser/extensions/crx_installer.h" |
31 #include "chrome/browser/extensions/extension_service.h" | 33 #include "chrome/browser/extensions/extension_service.h" |
32 #include "chrome/browser/extensions/extension_system.h" | 34 #include "chrome/browser/extensions/extension_system.h" |
33 #include "chrome/browser/history/history.h" | 35 #include "chrome/browser/history/history.h" |
34 #include "chrome/browser/history/history_service_factory.h" | 36 #include "chrome/browser/history/history_service_factory.h" |
35 #include "chrome/browser/intents/web_intents_util.h" | 37 #include "chrome/browser/intents/web_intents_util.h" |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 : profile_(profile), | 168 : profile_(profile), |
167 next_download_id_(0), | 169 next_download_id_(0), |
168 download_prefs_(new DownloadPrefs(profile)) { | 170 download_prefs_(new DownloadPrefs(profile)) { |
169 } | 171 } |
170 | 172 |
171 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { | 173 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { |
172 } | 174 } |
173 | 175 |
174 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) { | 176 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) { |
175 download_manager_ = dm; | 177 download_manager_ = dm; |
176 #if !defined(OS_ANDROID) | |
177 extension_event_router_.reset(new ExtensionDownloadsEventRouter( | |
178 profile_, download_manager_)); | |
179 #endif | |
180 } | 178 } |
181 | 179 |
182 void ChromeDownloadManagerDelegate::Shutdown() { | 180 void ChromeDownloadManagerDelegate::Shutdown() { |
183 download_prefs_.reset(); | 181 download_prefs_.reset(); |
184 #if !defined(OS_ANDROID) | |
185 extension_event_router_.reset(); | |
186 #endif | |
187 } | 182 } |
188 | 183 |
189 DownloadId ChromeDownloadManagerDelegate::GetNextId() { | 184 DownloadId ChromeDownloadManagerDelegate::GetNextId() { |
190 if (!profile_->IsOffTheRecord()) | 185 if (!profile_->IsOffTheRecord()) |
191 return DownloadId(this, next_download_id_++); | 186 return DownloadId(this, next_download_id_++); |
192 | 187 |
193 return BrowserContext::GetDownloadManager(profile_->GetOriginalProfile())-> | 188 return BrowserContext::GetDownloadManager(profile_->GetOriginalProfile())-> |
194 GetDelegate()->GetNextId(); | 189 GetDelegate()->GetNextId(); |
195 } | 190 } |
196 | 191 |
(...skipping 477 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
674 chrome::NOTIFICATION_CRX_INSTALLER_DONE, | 669 chrome::NOTIFICATION_CRX_INSTALLER_DONE, |
675 source); | 670 source); |
676 | 671 |
677 scoped_refptr<extensions::CrxInstaller> installer = | 672 scoped_refptr<extensions::CrxInstaller> installer = |
678 content::Source<extensions::CrxInstaller>(source).ptr(); | 673 content::Source<extensions::CrxInstaller>(source).ptr(); |
679 content::DownloadOpenDelayedCallback callback = crx_installers_[installer]; | 674 content::DownloadOpenDelayedCallback callback = crx_installers_[installer]; |
680 crx_installers_.erase(installer.get()); | 675 crx_installers_.erase(installer.get()); |
681 callback.Run(installer->did_handle_successfully()); | 676 callback.Run(installer->did_handle_successfully()); |
682 } | 677 } |
683 | 678 |
| 679 struct ChromeDownloadManagerDelegate::ContinueFilenameDeterminationInfo { |
| 680 ContinueFilenameDeterminationInfo(); |
| 681 ~ContinueFilenameDeterminationInfo(); |
| 682 |
| 683 int32 download_id; |
| 684 content::DownloadTargetCallback callback; |
| 685 content::DownloadDangerType danger_type; |
| 686 bool visited_referrer_before; |
| 687 bool should_prompt; |
| 688 }; |
| 689 |
| 690 ChromeDownloadManagerDelegate::ContinueFilenameDeterminationInfo:: |
| 691 ContinueFilenameDeterminationInfo() {} |
| 692 ChromeDownloadManagerDelegate::ContinueFilenameDeterminationInfo:: |
| 693 ~ContinueFilenameDeterminationInfo() {} |
| 694 |
684 void ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone( | 695 void ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone( |
685 int32 download_id, | 696 int32 download_id, |
686 const content::DownloadTargetCallback& callback, | 697 const content::DownloadTargetCallback& callback, |
687 content::DownloadDangerType danger_type, | 698 content::DownloadDangerType danger_type, |
688 bool visited_referrer_before) { | 699 bool visited_referrer_before) { |
689 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 700 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
690 | 701 |
691 DownloadItem* download = | 702 DownloadItem* download = |
692 download_manager_->GetDownload(download_id); | 703 download_manager_->GetDownload(download_id); |
693 if (!download || (download->GetState() != DownloadItem::IN_PROGRESS)) | 704 if (!download || (download->GetState() != DownloadItem::IN_PROGRESS)) |
694 return; | 705 return; |
695 | 706 |
696 bool should_prompt = (download->GetTargetDisposition() == | 707 bool should_prompt = (download->GetTargetDisposition() == |
697 DownloadItem::TARGET_DISPOSITION_PROMPT); | 708 DownloadItem::TARGET_DISPOSITION_PROMPT); |
698 bool is_forced_path = !download->GetForcedFilePath().empty(); | 709 bool is_forced_path = !download->GetForcedFilePath().empty(); |
| 710 FilePath generated_name; |
699 FilePath suggested_path; | 711 FilePath suggested_path; |
700 | 712 |
701 // Check whether this download is for an extension install or not. | 713 // Check whether this download is for an extension install or not. |
702 // Allow extensions to be explicitly saved. | 714 // Allow extensions to be explicitly saved. |
703 if (!is_forced_path) { | 715 if (!is_forced_path) { |
704 FilePath generated_name; | |
705 GenerateFileNameFromRequest( | 716 GenerateFileNameFromRequest( |
706 *download, | 717 *download, |
707 &generated_name, | 718 &generated_name, |
708 profile_->GetPrefs()->GetString(prefs::kDefaultCharset)); | 719 profile_->GetPrefs()->GetString(prefs::kDefaultCharset)); |
709 | 720 |
710 // Freeze the user's preference for showing a Save As dialog. We're going | 721 // Freeze the user's preference for showing a Save As dialog. We're going |
711 // to bounce around a bunch of threads and we don't want to worry about race | 722 // to bounce around a bunch of threads and we don't want to worry about race |
712 // conditions where the user changes this pref out from under us. | 723 // conditions where the user changes this pref out from under us. |
713 if (download_prefs_->PromptForDownload()) { | 724 if (download_prefs_->PromptForDownload()) { |
714 // But ignore the user's preference for the following scenarios: | 725 // But ignore the user's preference for the following scenarios: |
(...skipping 24 matching lines...) Expand all Loading... |
739 } | 750 } |
740 | 751 |
741 // If we will open the file with a web intents dispatch, | 752 // If we will open the file with a web intents dispatch, |
742 // give it a name that will not allow the OS to open it using usual | 753 // give it a name that will not allow the OS to open it using usual |
743 // associated apps. | 754 // associated apps. |
744 if (ShouldOpenWithWebIntents(download)) { | 755 if (ShouldOpenWithWebIntents(download)) { |
745 download->SetDisplayName(suggested_path.BaseName()); | 756 download->SetDisplayName(suggested_path.BaseName()); |
746 suggested_path = suggested_path.AddExtension(kWebIntentsFileExtension); | 757 suggested_path = suggested_path.AddExtension(kWebIntentsFileExtension); |
747 } | 758 } |
748 | 759 |
| 760 ContinueFilenameDeterminationInfo continue_info; |
| 761 continue_info.download_id = download_id; |
| 762 continue_info.callback = callback; |
| 763 continue_info.danger_type = danger_type; |
| 764 continue_info.visited_referrer_before = visited_referrer_before; |
| 765 continue_info.should_prompt = should_prompt; |
| 766 |
| 767 base::Closure filename_determined = base::Bind( |
| 768 &ChromeDownloadManagerDelegate::ContinueDeterminingFilename, |
| 769 this, |
| 770 continue_info, |
| 771 suggested_path, |
| 772 is_forced_path); |
| 773 #if !defined(OS_ANDROID) |
| 774 if (is_forced_path || |
| 775 ShouldOpenWithWebIntents(download) || |
| 776 !DownloadServiceFactory::GetForProfile(profile_) |
| 777 ->GetExtensionEventRouter()) { |
| 778 filename_determined.Run(); |
| 779 } else { |
| 780 DownloadService* service = DownloadServiceFactory::GetForProfile(profile_); |
| 781 ExtensionDownloadsEventRouter* router = service->GetExtensionEventRouter(); |
| 782 ExtensionDownloadsEventRouter::FilenameChangedCallback overriding = |
| 783 base::Bind(&ChromeDownloadManagerDelegate::OnExtensionOverridingFilename, |
| 784 this, continue_info); |
| 785 router->OnDeterminingFilename( |
| 786 download, generated_name, filename_determined, overriding); |
| 787 } |
| 788 #else |
| 789 filename_determined.Run(); |
| 790 #endif |
| 791 } |
| 792 |
| 793 void ChromeDownloadManagerDelegate::OnExtensionOverridingFilename( |
| 794 const ContinueFilenameDeterminationInfo& continue_info, |
| 795 const FilePath& changed_filename, |
| 796 bool overwrite) { |
| 797 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 798 DownloadItem* download = |
| 799 download_manager_->GetDownload(continue_info.download_id); |
| 800 if (!download || (download->GetState() != DownloadItem::IN_PROGRESS)) |
| 801 return; |
| 802 // If an extension overrides the filename, then the target directory will be |
| 803 // forced to download_prefs_->DownloadPath() since extensions cannot place |
| 804 // downloaded files anywhere except there. This prevents subdirectories from |
| 805 // accumulating: if an extension is allowed to say that a file should go in |
| 806 // last_download_path/music/foo.mp3, then last_download_path will accumulate |
| 807 // the subdirectory /music/ so that the next download may end up in |
| 808 // last_download_path/music/music/bar.mp3. |
| 809 FilePath temp_filename(download_prefs_->DownloadPath().Append( |
| 810 changed_filename)); |
| 811 net::GenerateSafeFileName(download->GetMimeType(), false, &temp_filename); |
| 812 // If |is_forced_path| were true, then extensions would not have been |
| 813 // consulted, so use |overwrite| instead of |is_forced_path|. This does NOT |
| 814 // set DownloadItem::GetForcedFilePath()! |
| 815 ContinueDeterminingFilename(continue_info, temp_filename, overwrite); |
| 816 } |
| 817 |
| 818 void ChromeDownloadManagerDelegate::ContinueDeterminingFilename( |
| 819 const ContinueFilenameDeterminationInfo& continue_info, |
| 820 const FilePath& suggested_path, |
| 821 bool is_forced_path) { |
| 822 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 823 int32 download_id = continue_info.download_id; |
| 824 const content::DownloadTargetCallback& callback = continue_info.callback; |
| 825 content::DownloadDangerType danger_type = continue_info.danger_type; |
| 826 bool visited_referrer_before = continue_info.visited_referrer_before; |
| 827 bool should_prompt = continue_info.should_prompt; |
| 828 DownloadItem* download = |
| 829 download_manager_->GetDownload(download_id); |
| 830 if (!download || (download->GetState() != DownloadItem::IN_PROGRESS)) |
| 831 return; |
| 832 |
749 // If the download hasn't already been marked dangerous (could be | 833 // If the download hasn't already been marked dangerous (could be |
750 // DANGEROUS_URL), check if it is a dangerous file. | 834 // DANGEROUS_URL), check if it is a dangerous file. |
751 if (danger_type == content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) { | 835 if (danger_type == content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) { |
752 if (!should_prompt && !is_forced_path && | 836 if (!should_prompt && !is_forced_path && |
753 IsDangerousFile(*download, suggested_path, visited_referrer_before)) { | 837 IsDangerousFile(*download, suggested_path, visited_referrer_before)) { |
754 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE; | 838 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE; |
755 } | 839 } |
756 | 840 |
757 #if defined(FULL_SAFE_BROWSING) | 841 #if defined(FULL_SAFE_BROWSING) |
758 DownloadProtectionService* service = GetDownloadProtectionService(); | 842 DownloadProtectionService* service = GetDownloadProtectionService(); |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
875 // TODO(asanka): This logic is a hack. DownloadFilePicker should give us a | 959 // TODO(asanka): This logic is a hack. DownloadFilePicker should give us a |
876 // directory to persist. Or perhaps, if the Drive path | 960 // directory to persist. Or perhaps, if the Drive path |
877 // substitution logic is moved here, then we would have a | 961 // substitution logic is moved here, then we would have a |
878 // persistable path after the DownloadFilePicker is done. | 962 // persistable path after the DownloadFilePicker is done. |
879 if (disposition == DownloadItem::TARGET_DISPOSITION_PROMPT && | 963 if (disposition == DownloadItem::TARGET_DISPOSITION_PROMPT && |
880 !download->IsTemporary()) | 964 !download->IsTemporary()) |
881 last_download_path_ = target_path.DirName(); | 965 last_download_path_ = target_path.DirName(); |
882 } | 966 } |
883 callback.Run(target_path, disposition, danger_type, intermediate_path); | 967 callback.Run(target_path, disposition, danger_type, intermediate_path); |
884 } | 968 } |
OLD | NEW |