OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/download_manager.h" | 5 #include "chrome/browser/download/download_manager.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
11 #include "base/rand_util.h" | 11 #include "base/rand_util.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "base/sys_string_conversions.h" | 13 #include "base/sys_string_conversions.h" |
14 #include "base/task.h" | 14 #include "base/task.h" |
15 #include "base/thread.h" | 15 #include "base/thread.h" |
16 #include "base/timer.h" | 16 #include "base/timer.h" |
17 #include "chrome/browser/browser_list.h" | 17 #include "chrome/browser/browser_list.h" |
18 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
19 #include "chrome/browser/download/download_file.h" | 19 #include "chrome/browser/download/download_file.h" |
20 #include "chrome/browser/extensions/extension.h" | 20 #include "chrome/browser/extensions/extension.h" |
| 21 #include "chrome/browser/extensions/extensions_service.h" |
21 #include "chrome/browser/profile.h" | 22 #include "chrome/browser/profile.h" |
22 #include "chrome/browser/renderer_host/render_process_host.h" | 23 #include "chrome/browser/renderer_host/render_process_host.h" |
23 #include "chrome/browser/renderer_host/render_view_host.h" | 24 #include "chrome/browser/renderer_host/render_view_host.h" |
24 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" | 25 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" |
25 #include "chrome/browser/tab_contents/tab_util.h" | 26 #include "chrome/browser/tab_contents/tab_util.h" |
26 #include "chrome/browser/tab_contents/web_contents.h" | 27 #include "chrome/browser/tab_contents/web_contents.h" |
27 #include "chrome/common/chrome_constants.h" | 28 #include "chrome/common/chrome_constants.h" |
28 #include "chrome/common/chrome_paths.h" | 29 #include "chrome/common/chrome_paths.h" |
29 #include "chrome/common/l10n_util.h" | 30 #include "chrome/common/l10n_util.h" |
30 #include "chrome/common/notification_service.h" | 31 #include "chrome/common/notification_service.h" |
(...skipping 652 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
683 NOTREACHED(); // Should not exist! | 684 NOTREACHED(); // Should not exist! |
684 return; | 685 return; |
685 } | 686 } |
686 | 687 |
687 // Called before DownloadFinished in order to avoid a race condition where we | 688 // Called before DownloadFinished in order to avoid a race condition where we |
688 // attempt to open a completed download before it has been renamed. | 689 // attempt to open a completed download before it has been renamed. |
689 file_loop_->PostTask(FROM_HERE, | 690 file_loop_->PostTask(FROM_HERE, |
690 NewRunnableMethod(file_manager_, | 691 NewRunnableMethod(file_manager_, |
691 &DownloadFileManager::OnFinalDownloadName, | 692 &DownloadFileManager::OnFinalDownloadName, |
692 download->id(), | 693 download->id(), |
693 target_path)); | 694 target_path, |
| 695 this)); |
694 | 696 |
695 // If the download already completed by the time we reached this point, then | 697 // If the download already completed by the time we reached this point, then |
696 // notify observers that it did. | 698 // notify observers that it did. |
697 PendingFinishedMap::iterator pending_it = | 699 PendingFinishedMap::iterator pending_it = |
698 pending_finished_downloads_.find(info->download_id); | 700 pending_finished_downloads_.find(info->download_id); |
699 if (pending_it != pending_finished_downloads_.end()) | 701 if (pending_it != pending_finished_downloads_.end()) |
700 DownloadFinished(pending_it->first, pending_it->second); | 702 DownloadFinished(pending_it->first, pending_it->second); |
701 | 703 |
702 download->Rename(target_path); | 704 download->Rename(target_path); |
703 | 705 |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
810 file_loop_->PostTask(FROM_HERE, | 812 file_loop_->PostTask(FROM_HERE, |
811 NewRunnableMethod( | 813 NewRunnableMethod( |
812 this, &DownloadManager::ProceedWithFinishedDangerousDownload, | 814 this, &DownloadManager::ProceedWithFinishedDangerousDownload, |
813 download->db_handle(), | 815 download->db_handle(), |
814 download->full_path(), download->original_name())); | 816 download->full_path(), download->original_name())); |
815 return; | 817 return; |
816 } | 818 } |
817 ContinueDownloadFinished(download); | 819 ContinueDownloadFinished(download); |
818 } | 820 } |
819 | 821 |
| 822 void DownloadManager::DownloadRenamedToFinalName(int download_id, |
| 823 const FilePath& full_path) { |
| 824 FilePath::StringType extension = full_path.Extension(); |
| 825 // Drop the leading period. |
| 826 if (extension.size() > 0) |
| 827 extension = extension.substr(1); |
| 828 |
| 829 if (extension == chrome::kExtensionFileExtension) { |
| 830 OpenChromeExtension(full_path); |
| 831 } |
| 832 } |
| 833 |
820 void DownloadManager::ContinueDownloadFinished(DownloadItem* download) { | 834 void DownloadManager::ContinueDownloadFinished(DownloadItem* download) { |
821 // If this was a dangerous download, it has now been approved and must be | 835 // If this was a dangerous download, it has now been approved and must be |
822 // removed from dangerous_finished_ so it does not get deleted on shutdown. | 836 // removed from dangerous_finished_ so it does not get deleted on shutdown. |
823 DownloadMap::iterator it = dangerous_finished_.find(download->id()); | 837 DownloadMap::iterator it = dangerous_finished_.find(download->id()); |
824 if (it != dangerous_finished_.end()) | 838 if (it != dangerous_finished_.end()) |
825 dangerous_finished_.erase(it); | 839 dangerous_finished_.erase(it); |
826 | 840 |
827 // Notify our observers that we are complete (the call to Finished() set the | 841 // Notify our observers that we are complete (the call to Finished() set the |
828 // state to complete but did not notify). | 842 // state to complete but did not notify). |
829 download->UpdateObservers(); | 843 download->UpdateObservers(); |
830 | 844 |
831 // Open the download if the user or user prefs indicate it should be. | 845 // Open the download if the user or user prefs indicate it should be. |
832 FilePath::StringType extension = download->full_path().Extension(); | 846 FilePath::StringType extension = download->full_path().Extension(); |
833 // Drop the leading period. | 847 // Drop the leading period. |
834 if (extension.size() > 0) | 848 if (extension.size() > 0) |
835 extension = extension.substr(1); | 849 extension = extension.substr(1); |
| 850 |
| 851 // Handle chrome extensions explicitly and skip the shell execute. |
| 852 if (extension == chrome::kExtensionFileExtension) { |
| 853 // Skip the shell execute. This will be handled in |
| 854 // DownloadRenamedToFinalName |
| 855 return; |
| 856 } |
| 857 |
836 if (download->open_when_complete() || ShouldOpenFileExtension(extension)) | 858 if (download->open_when_complete() || ShouldOpenFileExtension(extension)) |
837 OpenDownloadInShell(download, NULL); | 859 OpenDownloadInShell(download, NULL); |
838 } | 860 } |
839 | 861 |
840 // Called on the file thread. Renames the downloaded file to its original name. | 862 // Called on the file thread. Renames the downloaded file to its original name. |
841 void DownloadManager::ProceedWithFinishedDangerousDownload( | 863 void DownloadManager::ProceedWithFinishedDangerousDownload( |
842 int64 download_handle, | 864 int64 download_handle, |
843 const FilePath& path, | 865 const FilePath& path, |
844 const FilePath& original_name) { | 866 const FilePath& original_name) { |
845 bool success = false; | 867 bool success = false; |
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1185 // Post Windows Shell operations to the Download thread, to avoid blocking the | 1207 // Post Windows Shell operations to the Download thread, to avoid blocking the |
1186 // user interface. | 1208 // user interface. |
1187 void DownloadManager::ShowDownloadInShell(const DownloadItem* download) { | 1209 void DownloadManager::ShowDownloadInShell(const DownloadItem* download) { |
1188 DCHECK(file_manager_); | 1210 DCHECK(file_manager_); |
1189 file_loop_->PostTask(FROM_HERE, | 1211 file_loop_->PostTask(FROM_HERE, |
1190 NewRunnableMethod(file_manager_, | 1212 NewRunnableMethod(file_manager_, |
1191 &DownloadFileManager::OnShowDownloadInShell, | 1213 &DownloadFileManager::OnShowDownloadInShell, |
1192 FilePath(download->full_path()))); | 1214 FilePath(download->full_path()))); |
1193 } | 1215 } |
1194 | 1216 |
| 1217 void DownloadManager::OpenDownload(const DownloadItem* download, |
| 1218 gfx::NativeView parent_window) { |
| 1219 FilePath::StringType extension = download->full_path().Extension(); |
| 1220 // Drop the leading period. |
| 1221 if (extension.size() > 0) |
| 1222 extension = extension.substr(1); |
| 1223 |
| 1224 // Open Chrome extensions with ExtenstionsService. For everthing else do shell |
| 1225 // execute. |
| 1226 if (extension == chrome::kExtensionFileExtension) { |
| 1227 OpenChromeExtension(download->full_path()); |
| 1228 } else { |
| 1229 OpenDownloadInShell(download, parent_window); |
| 1230 } |
| 1231 } |
| 1232 |
| 1233 void DownloadManager::OpenChromeExtension(const FilePath& full_path) { |
| 1234 ExtensionsService* extensions_service = profile_->GetExtensionsService(); |
| 1235 extensions_service->InstallExtension(full_path); |
| 1236 } |
| 1237 |
1195 void DownloadManager::OpenDownloadInShell(const DownloadItem* download, | 1238 void DownloadManager::OpenDownloadInShell(const DownloadItem* download, |
1196 gfx::NativeView parent_window) { | 1239 gfx::NativeView parent_window) { |
1197 DCHECK(file_manager_); | 1240 DCHECK(file_manager_); |
1198 file_loop_->PostTask(FROM_HERE, | 1241 file_loop_->PostTask(FROM_HERE, |
1199 NewRunnableMethod(file_manager_, | 1242 NewRunnableMethod(file_manager_, |
1200 &DownloadFileManager::OnOpenDownloadInShell, | 1243 &DownloadFileManager::OnOpenDownloadInShell, |
1201 download->full_path(), download->url(), parent_window)); | 1244 download->full_path(), download->url(), parent_window)); |
1202 } | 1245 } |
1203 | 1246 |
1204 void DownloadManager::OpenFilesOfExtension( | 1247 void DownloadManager::OpenFilesOfExtension( |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1446 searched_downloads.push_back(dit->second); | 1489 searched_downloads.push_back(dit->second); |
1447 } | 1490 } |
1448 | 1491 |
1449 requestor->SetDownloads(searched_downloads); | 1492 requestor->SetDownloads(searched_downloads); |
1450 } | 1493 } |
1451 | 1494 |
1452 // Clears the last download path, used to initialize "save as" dialogs. | 1495 // Clears the last download path, used to initialize "save as" dialogs. |
1453 void DownloadManager::ClearLastDownloadPath() { | 1496 void DownloadManager::ClearLastDownloadPath() { |
1454 last_download_path_ = FilePath(); | 1497 last_download_path_ = FilePath(); |
1455 } | 1498 } |
OLD | NEW |