OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // Download utility implementation | 5 // Download utility implementation |
6 | 6 |
7 #include "chrome/browser/download/download_util.h" | 7 #include "chrome/browser/download/download_util.h" |
8 | 8 |
9 #if defined(OS_WIN) | 9 #if defined(OS_WIN) |
10 #include <shobjidl.h> | 10 #include <shobjidl.h> |
11 #endif | 11 #endif |
12 #include <string> | 12 #include <string> |
13 | 13 |
14 #include "base/file_util.h" | 14 #include "base/file_util.h" |
15 #include "base/i18n/rtl.h" | 15 #include "base/i18n/rtl.h" |
16 #include "base/i18n/time_formatting.h" | 16 #include "base/i18n/time_formatting.h" |
17 #include "base/lazy_instance.h" | 17 #include "base/lazy_instance.h" |
18 #include "base/metrics/histogram.h" | 18 #include "base/metrics/histogram.h" |
19 #include "base/path_service.h" | 19 #include "base/path_service.h" |
20 #include "base/string16.h" | 20 #include "base/string16.h" |
21 #include "base/string_number_conversions.h" | 21 #include "base/string_number_conversions.h" |
22 #include "base/stringprintf.h" | 22 #include "base/stringprintf.h" |
23 #include "base/sys_string_conversions.h" | 23 #include "base/sys_string_conversions.h" |
24 #include "base/threading/thread_restrictions.h" | 24 #include "base/threading/thread_restrictions.h" |
25 #include "base/utf_string_conversions.h" | 25 #include "base/utf_string_conversions.h" |
26 #include "base/value_conversions.h" | 26 #include "base/value_conversions.h" |
27 #include "base/values.h" | 27 #include "base/values.h" |
28 #include "base/win/windows_version.h" | 28 #include "base/win/windows_version.h" |
| 29 #include "chrome/browser/download/download_create_info.h" |
29 #include "chrome/browser/download/download_extensions.h" | 30 #include "chrome/browser/download/download_extensions.h" |
30 #include "chrome/browser/download/download_item.h" | 31 #include "chrome/browser/download/download_item.h" |
31 #include "chrome/browser/download/download_item_model.h" | 32 #include "chrome/browser/download/download_item_model.h" |
32 #include "chrome/browser/download/download_manager.h" | 33 #include "chrome/browser/download/download_manager.h" |
33 #include "chrome/browser/download/download_types.h" | 34 #include "chrome/browser/download/download_types.h" |
34 #include "chrome/browser/extensions/crx_installer.h" | 35 #include "chrome/browser/extensions/crx_installer.h" |
35 #include "chrome/browser/extensions/extension_install_ui.h" | 36 #include "chrome/browser/extensions/extension_install_ui.h" |
36 #include "chrome/browser/extensions/extension_service.h" | 37 #include "chrome/browser/extensions/extension_service.h" |
37 #include "chrome/browser/history/download_create_info.h" | |
38 #include "chrome/browser/profiles/profile.h" | 38 #include "chrome/browser/profiles/profile.h" |
39 #include "chrome/browser/ui/browser.h" | 39 #include "chrome/browser/ui/browser.h" |
40 #include "chrome/common/chrome_paths.h" | 40 #include "chrome/common/chrome_paths.h" |
41 #include "chrome/common/time_format.h" | 41 #include "chrome/common/time_format.h" |
42 #include "content/browser/browser_thread.h" | 42 #include "content/browser/browser_thread.h" |
43 #include "content/browser/renderer_host/render_view_host.h" | 43 #include "content/browser/renderer_host/render_view_host.h" |
44 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 44 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
45 #include "content/browser/tab_contents/tab_contents.h" | 45 #include "content/browser/tab_contents/tab_contents.h" |
46 #include "content/common/notification_service.h" | 46 #include "content/common/notification_service.h" |
47 #include "grit/generated_resources.h" | 47 #include "grit/generated_resources.h" |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 // The GetPreferredExtensionForMimeType call will end up going to disk. Do | 274 // The GetPreferredExtensionForMimeType call will end up going to disk. Do |
275 // this on another thread to avoid slowing the IO thread. | 275 // this on another thread to avoid slowing the IO thread. |
276 // http://crbug.com/61827 | 276 // http://crbug.com/61827 |
277 base::ThreadRestrictions::ScopedAllowIO allow_io; | 277 base::ThreadRestrictions::ScopedAllowIO allow_io; |
278 net::GetPreferredExtensionForMimeType(mime_type, &extension); | 278 net::GetPreferredExtensionForMimeType(mime_type, &extension); |
279 } | 279 } |
280 | 280 |
281 generated_extension->swap(extension); | 281 generated_extension->swap(extension); |
282 } | 282 } |
283 | 283 |
284 void GenerateFileNameFromInfo(DownloadCreateInfo* info, | 284 void GenerateFileNameFromRequest(const GURL& url, |
285 FilePath* generated_name) { | 285 const std::string& content_disposition, |
286 GenerateFileNameInternal(GURL(info->url()), info->content_disposition, | 286 const std::string& referrer_charset, |
287 info->referrer_charset, std::string(), | 287 const std::string& mime_type, |
288 info->mime_type, generated_name); | 288 FilePath* generated_name) { |
| 289 GenerateFileNameInternal(url, |
| 290 content_disposition, |
| 291 referrer_charset, |
| 292 std::string(), |
| 293 mime_type, |
| 294 generated_name); |
289 } | 295 } |
290 | 296 |
291 void GenerateFileNameFromSuggestedName(const GURL& url, | 297 void GenerateFileNameFromSuggestedName(const GURL& url, |
292 const std::string& suggested_name, | 298 const std::string& suggested_name, |
293 const std::string& mime_type, | 299 const std::string& mime_type, |
294 FilePath* generated_name) { | 300 FilePath* generated_name) { |
295 GenerateFileNameInternal(url, std::string(), std::string(), | 301 GenerateFileNameInternal(url, std::string(), std::string(), |
296 suggested_name, mime_type, generated_name); | 302 suggested_name, mime_type, generated_name); |
297 } | 303 } |
298 | 304 |
(...skipping 602 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
901 FilePath GetCrDownloadPath(const FilePath& suggested_path) { | 907 FilePath GetCrDownloadPath(const FilePath& suggested_path) { |
902 FilePath::StringType file_name; | 908 FilePath::StringType file_name; |
903 base::SStringPrintf( | 909 base::SStringPrintf( |
904 &file_name, | 910 &file_name, |
905 PRFilePathLiteral FILE_PATH_LITERAL(".crdownload"), | 911 PRFilePathLiteral FILE_PATH_LITERAL(".crdownload"), |
906 suggested_path.value().c_str()); | 912 suggested_path.value().c_str()); |
907 return FilePath(file_name); | 913 return FilePath(file_name); |
908 } | 914 } |
909 | 915 |
910 // TODO(erikkay,phajdan.jr): This is apparently not being exercised in tests. | 916 // TODO(erikkay,phajdan.jr): This is apparently not being exercised in tests. |
911 bool IsDangerous(DownloadCreateInfo* info, Profile* profile, bool auto_open) { | 917 bool IsDangerous(const GURL& url, |
| 918 const GURL& referrer_url, |
| 919 const FilePath& suggested_path, |
| 920 bool has_user_gesture, |
| 921 bool is_extension_install, |
| 922 Profile* profile, |
| 923 bool auto_open) { |
912 DownloadDangerLevel danger_level = GetFileDangerLevel( | 924 DownloadDangerLevel danger_level = GetFileDangerLevel( |
913 info->suggested_path.BaseName()); | 925 suggested_path.BaseName()); |
914 if (danger_level == Dangerous) | 926 if (danger_level == Dangerous) |
915 return !(auto_open && info->has_user_gesture); | 927 return !(auto_open && has_user_gesture); |
916 if (danger_level == AllowOnUserGesture && !info->has_user_gesture) | 928 if (danger_level == AllowOnUserGesture && !has_user_gesture) |
917 return true; | 929 return true; |
918 if (info->is_extension_install) { | 930 if (is_extension_install) { |
919 // Extensions that are not from the gallery are considered dangerous. | 931 // Extensions that are not from the gallery are considered dangerous. |
920 ExtensionService* service = profile->GetExtensionService(); | 932 ExtensionService* service = profile->GetExtensionService(); |
921 if (!service || | 933 if (!service || !service->IsDownloadFromGallery(url, referrer_url)) |
922 !service->IsDownloadFromGallery(info->url(), info->referrer_url)) | |
923 return true; | 934 return true; |
924 } | 935 } |
925 return false; | 936 return false; |
926 } | 937 } |
927 | 938 |
928 } // namespace download_util | 939 } // namespace download_util |
OLD | NEW |