| 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 |
| 115 // DownloadItem implementation ------------------------------------------------- | 129 // DownloadItem implementation ------------------------------------------------- |
| 116 | 130 |
| 117 // Constructor for reading from the history service. | 131 // Constructor for reading from the history service. |
| 118 DownloadItem::DownloadItem(const DownloadCreateInfo& info) | 132 DownloadItem::DownloadItem(const DownloadCreateInfo& info) |
| 119 : id_(-1), | 133 : id_(-1), |
| 120 full_path_(info.path), | 134 full_path_(info.path), |
| 121 url_(info.url), | 135 url_(info.url), |
| 122 referrer_url_(info.referrer_url), | 136 referrer_url_(info.referrer_url), |
| 123 mime_type_(info.mime_type), | 137 mime_type_(info.mime_type), |
| 124 total_bytes_(info.total_bytes), | 138 total_bytes_(info.total_bytes), |
| (...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 569 else | 583 else |
| 570 info->suggested_path = download_path(); | 584 info->suggested_path = download_path(); |
| 571 info->suggested_path = info->suggested_path.Append(generated_name); | 585 info->suggested_path = info->suggested_path.Append(generated_name); |
| 572 | 586 |
| 573 if (!info->save_as) { | 587 if (!info->save_as) { |
| 574 // Downloads can be marked as dangerous for two reasons: | 588 // Downloads can be marked as dangerous for two reasons: |
| 575 // a) They have a dangerous-looking filename | 589 // a) They have a dangerous-looking filename |
| 576 // b) They are an extension that is not from the gallery | 590 // b) They are an extension that is not from the gallery |
| 577 if (IsDangerous(info->suggested_path.BaseName())) | 591 if (IsDangerous(info->suggested_path.BaseName())) |
| 578 info->is_dangerous = true; | 592 info->is_dangerous = true; |
| 579 else if (info->mime_type == Extension::kMimeType && | 593 else if (IsChromeExtension(info->path, info->mime_type) && |
| 580 !ExtensionsService::IsDownloadFromGallery(info->url, | 594 !ExtensionsService::IsDownloadFromGallery(info->url, |
| 581 info->referrer_url)) { | 595 info->referrer_url)) { |
| 582 info->is_dangerous = true; | 596 info->is_dangerous = true; |
| 583 } | 597 } |
| 584 } | 598 } |
| 585 | 599 |
| 586 // We need to move over to the download thread because we don't want to stat | 600 // We need to move over to the download thread because we don't want to stat |
| 587 // the suggested path on the UI thread. | 601 // the suggested path on the UI thread. |
| 588 file_loop_->PostTask(FROM_HERE, | 602 file_loop_->PostTask(FROM_HERE, |
| 589 NewRunnableMethod(this, | 603 NewRunnableMethod(this, |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 849 if (it != dangerous_finished_.end()) | 863 if (it != dangerous_finished_.end()) |
| 850 dangerous_finished_.erase(it); | 864 dangerous_finished_.erase(it); |
| 851 | 865 |
| 852 // Open the download if the user or user prefs indicate it should be. | 866 // Open the download if the user or user prefs indicate it should be. |
| 853 FilePath::StringType extension = download->full_path().Extension(); | 867 FilePath::StringType extension = download->full_path().Extension(); |
| 854 // Drop the leading period. (The auto-open list is period-less.) | 868 // Drop the leading period. (The auto-open list is period-less.) |
| 855 if (extension.size() > 0) | 869 if (extension.size() > 0) |
| 856 extension = extension.substr(1); | 870 extension = extension.substr(1); |
| 857 | 871 |
| 858 // Handle chrome extensions explicitly and skip the shell execute. | 872 // Handle chrome extensions explicitly and skip the shell execute. |
| 859 if (download->mime_type() == Extension::kMimeType) { | 873 if (IsChromeExtension(download->full_path(), download->mime_type())) { |
| 860 OpenChromeExtension(download->full_path(), download->url(), | 874 OpenChromeExtension(download->full_path(), download->url(), |
| 861 download->referrer_url()); | 875 download->referrer_url()); |
| 862 download->set_auto_opened(true); | 876 download->set_auto_opened(true); |
| 863 } else if (download->open_when_complete() || | 877 } else if (download->open_when_complete() || |
| 864 ShouldOpenFileExtension(extension)) { | 878 ShouldOpenFileExtension(extension)) { |
| 865 OpenDownloadInShell(download, NULL); | 879 OpenDownloadInShell(download, NULL); |
| 866 download->set_auto_opened(true); | 880 download->set_auto_opened(true); |
| 867 } | 881 } |
| 868 | 882 |
| 869 // Notify our observers that we are complete (the call to Finished() set the | 883 // Notify our observers that we are complete (the call to Finished() set the |
| (...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1219 file_loop_->PostTask(FROM_HERE, | 1233 file_loop_->PostTask(FROM_HERE, |
| 1220 NewRunnableMethod(file_manager_, | 1234 NewRunnableMethod(file_manager_, |
| 1221 &DownloadFileManager::OnShowDownloadInShell, | 1235 &DownloadFileManager::OnShowDownloadInShell, |
| 1222 FilePath(download->full_path()))); | 1236 FilePath(download->full_path()))); |
| 1223 } | 1237 } |
| 1224 | 1238 |
| 1225 void DownloadManager::OpenDownload(const DownloadItem* download, | 1239 void DownloadManager::OpenDownload(const DownloadItem* download, |
| 1226 gfx::NativeView parent_window) { | 1240 gfx::NativeView parent_window) { |
| 1227 // Open Chrome extensions with ExtensionsService. For everything else do shell | 1241 // Open Chrome extensions with ExtensionsService. For everything else do shell |
| 1228 // execute. | 1242 // execute. |
| 1229 if (download->mime_type() == Extension::kMimeType) { | 1243 if (IsChromeExtension(download->full_path(), download->mime_type())) { |
| 1230 OpenChromeExtension(download->full_path(), download->url(), | 1244 OpenChromeExtension(download->full_path(), download->url(), |
| 1231 download->referrer_url()); | 1245 download->referrer_url()); |
| 1232 } else { | 1246 } else { |
| 1233 OpenDownloadInShell(download, parent_window); | 1247 OpenDownloadInShell(download, parent_window); |
| 1234 } | 1248 } |
| 1235 } | 1249 } |
| 1236 | 1250 |
| 1237 void DownloadManager::OpenChromeExtension(const FilePath& full_path, | 1251 void DownloadManager::OpenChromeExtension(const FilePath& full_path, |
| 1238 const GURL& download_url, | 1252 const GURL& download_url, |
| 1239 const GURL& referrer_url) { | 1253 const GURL& referrer_url) { |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1512 | 1526 |
| 1513 if (contents) | 1527 if (contents) |
| 1514 contents->OnStartDownload(download); | 1528 contents->OnStartDownload(download); |
| 1515 } | 1529 } |
| 1516 | 1530 |
| 1517 // Clears the last download path, used to initialize "save as" dialogs. | 1531 // Clears the last download path, used to initialize "save as" dialogs. |
| 1518 void DownloadManager::ClearLastDownloadPath() { | 1532 void DownloadManager::ClearLastDownloadPath() { |
| 1519 last_download_path_ = FilePath(); | 1533 last_download_path_ = FilePath(); |
| 1520 } | 1534 } |
| 1521 | 1535 |
| OLD | NEW |