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