| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/save_package.h" | 5 #include "chrome/browser/download/save_package.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/i18n/file_util_icu.h" | 10 #include "base/i18n/file_util_icu.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "base/stl_util-inl.h" | 13 #include "base/stl_util-inl.h" |
| 14 #include "base/string_piece.h" | 14 #include "base/string_piece.h" |
| 15 #include "base/string_split.h" | 15 #include "base/string_split.h" |
| 16 #include "base/thread_restrictions.h" |
| 16 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
| 17 #include "base/task.h" | 18 #include "base/task.h" |
| 18 #include "base/thread.h" | 19 #include "base/thread.h" |
| 19 #include "chrome/browser/browser_process.h" | 20 #include "chrome/browser/browser_process.h" |
| 20 #include "chrome/browser/browser_thread.h" | 21 #include "chrome/browser/browser_thread.h" |
| 21 #include "chrome/browser/download/download_item.h" | 22 #include "chrome/browser/download/download_item.h" |
| 22 #include "chrome/browser/download/download_item_model.h" | 23 #include "chrome/browser/download/download_item_model.h" |
| 23 #include "chrome/browser/download/download_manager.h" | 24 #include "chrome/browser/download/download_manager.h" |
| 24 #include "chrome/browser/download/download_shelf.h" | 25 #include "chrome/browser/download/download_shelf.h" |
| 25 #include "chrome/browser/download/download_util.h" | 26 #include "chrome/browser/download/download_util.h" |
| (...skipping 1125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1151 // Adjust extension for complete types. | 1152 // Adjust extension for complete types. |
| 1152 if (can_save_as_complete) | 1153 if (can_save_as_complete) |
| 1153 name_with_proper_ext = EnsureHtmlExtension(name_with_proper_ext); | 1154 name_with_proper_ext = EnsureHtmlExtension(name_with_proper_ext); |
| 1154 | 1155 |
| 1155 FilePath::StringType file_name = name_with_proper_ext.value(); | 1156 FilePath::StringType file_name = name_with_proper_ext.value(); |
| 1156 file_util::ReplaceIllegalCharactersInPath(&file_name, ' '); | 1157 file_util::ReplaceIllegalCharactersInPath(&file_name, ' '); |
| 1157 return FilePath(file_name); | 1158 return FilePath(file_name); |
| 1158 } | 1159 } |
| 1159 | 1160 |
| 1160 FilePath SavePackage::EnsureHtmlExtension(const FilePath& name) { | 1161 FilePath SavePackage::EnsureHtmlExtension(const FilePath& name) { |
| 1162 // The GetMimeTypeFromExtension call will end up going to disk. Do this on |
| 1163 // another thread to avoid slowing the UI thread. http://crbug.com/61775 |
| 1164 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 1165 |
| 1161 // If the file name doesn't have an extension suitable for HTML files, | 1166 // If the file name doesn't have an extension suitable for HTML files, |
| 1162 // append one. | 1167 // append one. |
| 1163 FilePath::StringType ext = name.Extension(); | 1168 FilePath::StringType ext = name.Extension(); |
| 1164 if (!ext.empty()) | 1169 if (!ext.empty()) |
| 1165 ext.erase(ext.begin()); // Erase preceding '.'. | 1170 ext.erase(ext.begin()); // Erase preceding '.'. |
| 1166 std::string mime_type; | 1171 std::string mime_type; |
| 1167 if (!net::GetMimeTypeFromExtension(ext, &mime_type) || | 1172 if (!net::GetMimeTypeFromExtension(ext, &mime_type) || |
| 1168 !CanSaveAsComplete(mime_type)) { | 1173 !CanSaveAsComplete(mime_type)) { |
| 1169 return FilePath(name.value() + FILE_PATH_LITERAL(".") + | 1174 return FilePath(name.value() + FILE_PATH_LITERAL(".") + |
| 1170 kDefaultHtmlExtension); | 1175 kDefaultHtmlExtension); |
| 1171 } | 1176 } |
| 1172 return name; | 1177 return name; |
| 1173 } | 1178 } |
| 1174 | 1179 |
| 1175 FilePath SavePackage::EnsureMimeExtension(const FilePath& name, | 1180 FilePath SavePackage::EnsureMimeExtension(const FilePath& name, |
| 1176 const FilePath::StringType& contents_mime_type) { | 1181 const FilePath::StringType& contents_mime_type) { |
| 1182 // The GetMimeTypeFromExtension call will end up going to disk. Do this on |
| 1183 // another thread to avoid slowing the UI thread. http://crbug.com/61775 |
| 1184 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 1185 |
| 1177 // Start extension at 1 to skip over period if non-empty. | 1186 // Start extension at 1 to skip over period if non-empty. |
| 1178 FilePath::StringType ext = name.Extension().length() ? | 1187 FilePath::StringType ext = name.Extension().length() ? |
| 1179 name.Extension().substr(1) : name.Extension(); | 1188 name.Extension().substr(1) : name.Extension(); |
| 1180 FilePath::StringType suggested_extension = | 1189 FilePath::StringType suggested_extension = |
| 1181 ExtensionForMimeType(contents_mime_type); | 1190 ExtensionForMimeType(contents_mime_type); |
| 1182 std::string mime_type; | 1191 std::string mime_type; |
| 1183 if (!suggested_extension.empty() && | 1192 if (!suggested_extension.empty() && |
| 1184 (!net::GetMimeTypeFromExtension(ext, &mime_type) || | 1193 (!net::GetMimeTypeFromExtension(ext, &mime_type) || |
| 1185 !IsSavableContents(mime_type))) { | 1194 !IsSavableContents(mime_type))) { |
| 1186 // Extension is absent or needs to be updated. | 1195 // Extension is absent or needs to be updated. |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1395 int index, void* params) { | 1404 int index, void* params) { |
| 1396 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); | 1405 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); |
| 1397 ContinueSave(save_params, path, index); | 1406 ContinueSave(save_params, path, index); |
| 1398 delete save_params; | 1407 delete save_params; |
| 1399 } | 1408 } |
| 1400 | 1409 |
| 1401 void SavePackage::FileSelectionCanceled(void* params) { | 1410 void SavePackage::FileSelectionCanceled(void* params) { |
| 1402 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); | 1411 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); |
| 1403 delete save_params; | 1412 delete save_params; |
| 1404 } | 1413 } |
| OLD | NEW |