| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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 "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 | 105 |
| 106 static bool DownloadPathIsDangerous(const FilePath& download_path) { | 106 static bool DownloadPathIsDangerous(const FilePath& download_path) { |
| 107 FilePath desktop_dir; | 107 FilePath desktop_dir; |
| 108 if (!PathService::Get(chrome::DIR_USER_DESKTOP, &desktop_dir)) { | 108 if (!PathService::Get(chrome::DIR_USER_DESKTOP, &desktop_dir)) { |
| 109 NOTREACHED(); | 109 NOTREACHED(); |
| 110 return false; | 110 return false; |
| 111 } | 111 } |
| 112 return (download_path == desktop_dir); | 112 return (download_path == desktop_dir); |
| 113 } | 113 } |
| 114 | 114 |
| 115 // Helper to determine if a download is a Chrome extension. We should be able to | |
| 116 // just use the mime type, but even our own servers are not setup to serve the | |
| 117 // right headers yet, so we have a short-term file extension heuristic, too. | |
| 118 static bool IsChromeExtension(const FilePath& path, | |
| 119 const std::string& mime_type) { | |
| 120 // If the server says it is an extension, it is definitely an extension. | |
| 121 if (mime_type == Extension::kMimeType) | |
| 122 return true; | |
| 123 | |
| 124 // Otherwise, it is an extension if it has the right, err, extension. | |
| 125 return path.Extension().size() > 1 && | |
| 126 path.Extension().substr(1) == chrome::kExtensionFileExtension; | |
| 127 } | |
| 128 | |
| 129 // DownloadItem implementation ------------------------------------------------- | 115 // DownloadItem implementation ------------------------------------------------- |
| 130 | 116 |
| 131 // Constructor for reading from the history service. | 117 // Constructor for reading from the history service. |
| 132 DownloadItem::DownloadItem(const DownloadCreateInfo& info) | 118 DownloadItem::DownloadItem(const DownloadCreateInfo& info) |
| 133 : id_(-1), | 119 : id_(-1), |
| 134 full_path_(info.path), | 120 full_path_(info.path), |
| 135 url_(info.url), | 121 url_(info.url), |
| 136 referrer_url_(info.referrer_url), | 122 referrer_url_(info.referrer_url), |
| 137 mime_type_(info.mime_type), | 123 mime_type_(info.mime_type), |
| 138 total_bytes_(info.total_bytes), | 124 total_bytes_(info.total_bytes), |
| (...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 583 else | 569 else |
| 584 info->suggested_path = download_path(); | 570 info->suggested_path = download_path(); |
| 585 info->suggested_path = info->suggested_path.Append(generated_name); | 571 info->suggested_path = info->suggested_path.Append(generated_name); |
| 586 | 572 |
| 587 if (!info->save_as) { | 573 if (!info->save_as) { |
| 588 // Downloads can be marked as dangerous for two reasons: | 574 // Downloads can be marked as dangerous for two reasons: |
| 589 // a) They have a dangerous-looking filename | 575 // a) They have a dangerous-looking filename |
| 590 // b) They are an extension that is not from the gallery | 576 // b) They are an extension that is not from the gallery |
| 591 if (IsDangerous(info->suggested_path.BaseName())) | 577 if (IsDangerous(info->suggested_path.BaseName())) |
| 592 info->is_dangerous = true; | 578 info->is_dangerous = true; |
| 593 else if (IsChromeExtension(info->path, info->mime_type) && | 579 else if (info->mime_type == Extension::kMimeType && |
| 594 !ExtensionsService::IsDownloadFromGallery(info->url, | 580 !ExtensionsService::IsDownloadFromGallery(info->url, |
| 595 info->referrer_url)) { | 581 info->referrer_url)) { |
| 596 info->is_dangerous = true; | 582 info->is_dangerous = true; |
| 597 } | 583 } |
| 598 } | 584 } |
| 599 | 585 |
| 600 // We need to move over to the download thread because we don't want to stat | 586 // We need to move over to the download thread because we don't want to stat |
| 601 // the suggested path on the UI thread. | 587 // the suggested path on the UI thread. |
| 602 file_loop_->PostTask(FROM_HERE, | 588 file_loop_->PostTask(FROM_HERE, |
| 603 NewRunnableMethod(this, | 589 NewRunnableMethod(this, |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 863 if (it != dangerous_finished_.end()) | 849 if (it != dangerous_finished_.end()) |
| 864 dangerous_finished_.erase(it); | 850 dangerous_finished_.erase(it); |
| 865 | 851 |
| 866 // Open the download if the user or user prefs indicate it should be. | 852 // Open the download if the user or user prefs indicate it should be. |
| 867 FilePath::StringType extension = download->full_path().Extension(); | 853 FilePath::StringType extension = download->full_path().Extension(); |
| 868 // Drop the leading period. (The auto-open list is period-less.) | 854 // Drop the leading period. (The auto-open list is period-less.) |
| 869 if (extension.size() > 0) | 855 if (extension.size() > 0) |
| 870 extension = extension.substr(1); | 856 extension = extension.substr(1); |
| 871 | 857 |
| 872 // Handle chrome extensions explicitly and skip the shell execute. | 858 // Handle chrome extensions explicitly and skip the shell execute. |
| 873 if (IsChromeExtension(download->full_path(), download->mime_type())) { | 859 if (download->mime_type() == Extension::kMimeType) { |
| 874 OpenChromeExtension(download->full_path(), download->url(), | 860 OpenChromeExtension(download->full_path(), download->url(), |
| 875 download->referrer_url()); | 861 download->referrer_url()); |
| 876 download->set_auto_opened(true); | 862 download->set_auto_opened(true); |
| 877 } else if (download->open_when_complete() || | 863 } else if (download->open_when_complete() || |
| 878 ShouldOpenFileExtension(extension)) { | 864 ShouldOpenFileExtension(extension)) { |
| 879 OpenDownloadInShell(download, NULL); | 865 OpenDownloadInShell(download, NULL); |
| 880 download->set_auto_opened(true); | 866 download->set_auto_opened(true); |
| 881 } | 867 } |
| 882 | 868 |
| 883 // Notify our observers that we are complete (the call to Finished() set the | 869 // Notify our observers that we are complete (the call to Finished() set the |
| (...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1242 file_loop_->PostTask(FROM_HERE, | 1228 file_loop_->PostTask(FROM_HERE, |
| 1243 NewRunnableMethod(file_manager_, | 1229 NewRunnableMethod(file_manager_, |
| 1244 &DownloadFileManager::OnShowDownloadInShell, | 1230 &DownloadFileManager::OnShowDownloadInShell, |
| 1245 FilePath(download->full_path()))); | 1231 FilePath(download->full_path()))); |
| 1246 } | 1232 } |
| 1247 | 1233 |
| 1248 void DownloadManager::OpenDownload(const DownloadItem* download, | 1234 void DownloadManager::OpenDownload(const DownloadItem* download, |
| 1249 gfx::NativeView parent_window) { | 1235 gfx::NativeView parent_window) { |
| 1250 // Open Chrome extensions with ExtensionsService. For everything else do shell | 1236 // Open Chrome extensions with ExtensionsService. For everything else do shell |
| 1251 // execute. | 1237 // execute. |
| 1252 if (IsChromeExtension(download->full_path(), download->mime_type())) { | 1238 if (download->mime_type() == Extension::kMimeType) { |
| 1253 OpenChromeExtension(download->full_path(), download->url(), | 1239 OpenChromeExtension(download->full_path(), download->url(), |
| 1254 download->referrer_url()); | 1240 download->referrer_url()); |
| 1255 } else { | 1241 } else { |
| 1256 OpenDownloadInShell(download, parent_window); | 1242 OpenDownloadInShell(download, parent_window); |
| 1257 } | 1243 } |
| 1258 } | 1244 } |
| 1259 | 1245 |
| 1260 void DownloadManager::OpenChromeExtension(const FilePath& full_path, | 1246 void DownloadManager::OpenChromeExtension(const FilePath& full_path, |
| 1261 const GURL& download_url, | 1247 const GURL& download_url, |
| 1262 const GURL& referrer_url) { | 1248 const GURL& referrer_url) { |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1535 | 1521 |
| 1536 if (contents) | 1522 if (contents) |
| 1537 contents->OnStartDownload(download); | 1523 contents->OnStartDownload(download); |
| 1538 } | 1524 } |
| 1539 | 1525 |
| 1540 // Clears the last download path, used to initialize "save as" dialogs. | 1526 // Clears the last download path, used to initialize "save as" dialogs. |
| 1541 void DownloadManager::ClearLastDownloadPath() { | 1527 void DownloadManager::ClearLastDownloadPath() { |
| 1542 last_download_path_ = FilePath(); | 1528 last_download_path_ = FilePath(); |
| 1543 } | 1529 } |
| 1544 | 1530 |
| OLD | NEW |