Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "net/base/filename_util.h" | 5 #include "net/base/filename_util.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 222 ReplaceIllegalCharactersCallback replace_illegal_characters_callback) { | 222 ReplaceIllegalCharactersCallback replace_illegal_characters_callback) { |
| 223 // TODO: this function to be updated to match the httpbis recommendations. | 223 // TODO: this function to be updated to match the httpbis recommendations. |
| 224 // Talk to abarth for the latest news. | 224 // Talk to abarth for the latest news. |
| 225 | 225 |
| 226 // We don't translate this fallback string, "download". If localization is | 226 // We don't translate this fallback string, "download". If localization is |
| 227 // needed, the caller should provide localized fallback in |default_name|. | 227 // needed, the caller should provide localized fallback in |default_name|. |
| 228 static const base::FilePath::CharType kFinalFallbackName[] = | 228 static const base::FilePath::CharType kFinalFallbackName[] = |
| 229 FILE_PATH_LITERAL("download"); | 229 FILE_PATH_LITERAL("download"); |
| 230 std::string filename; // In UTF-8 | 230 std::string filename; // In UTF-8 |
| 231 bool overwrite_extension = false; | 231 bool overwrite_extension = false; |
| 232 | 232 bool is_name_from_content_disposition = false; |
| 233 // Try to extract a filename from content-disposition first. | 233 // Try to extract a filename from content-disposition first. |
| 234 if (!content_disposition.empty()) { | 234 if (!content_disposition.empty()) { |
| 235 HttpContentDisposition header(content_disposition, referrer_charset); | 235 HttpContentDisposition header(content_disposition, referrer_charset); |
| 236 filename = header.filename(); | 236 filename = header.filename(); |
| 237 if (!filename.empty()) | |
| 238 is_name_from_content_disposition = true; | |
| 237 } | 239 } |
| 238 | 240 |
| 239 // Then try to use the suggested name. | 241 // Then try to use the suggested name. |
| 240 if (filename.empty() && !suggested_name.empty()) | 242 if (filename.empty() && !suggested_name.empty()) |
| 241 filename = suggested_name; | 243 filename = suggested_name; |
| 242 | 244 |
| 243 // Now try extracting the filename from the URL. GetFileNameFromURL() only | 245 // Now try extracting the filename from the URL. GetFileNameFromURL() only |
| 244 // looks at the last component of the URL and doesn't return the hostname as a | 246 // looks at the last component of the URL and doesn't return the hostname as a |
| 245 // failover. | 247 // failover. |
| 246 if (filename.empty()) | 248 if (filename.empty()) |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 268 SanitizeGeneratedFileName(&result_str, replace_trailing); | 270 SanitizeGeneratedFileName(&result_str, replace_trailing); |
| 269 if (result_str.find_last_not_of(FILE_PATH_LITERAL("-_")) == | 271 if (result_str.find_last_not_of(FILE_PATH_LITERAL("-_")) == |
| 270 base::FilePath::StringType::npos) { | 272 base::FilePath::StringType::npos) { |
| 271 result_str = !default_name_str.empty() | 273 result_str = !default_name_str.empty() |
| 272 ? default_name_str | 274 ? default_name_str |
| 273 : base::FilePath::StringType(kFinalFallbackName); | 275 : base::FilePath::StringType(kFinalFallbackName); |
| 274 overwrite_extension = false; | 276 overwrite_extension = false; |
| 275 } | 277 } |
| 276 replace_illegal_characters_callback.Run(&result_str, '-'); | 278 replace_illegal_characters_callback.Run(&result_str, '-'); |
| 277 base::FilePath result(result_str); | 279 base::FilePath result(result_str); |
| 278 GenerateSafeFileName(mime_type, overwrite_extension, &result); | 280 // extension is not appended to filename derived from content-disposition, |
| 281 // if it doesnot have one | |
| 282 if (!is_name_from_content_disposition) | |
| 283 GenerateSafeFileName(mime_type, overwrite_extension, &result); | |
|
asanka
2014/10/08 15:58:29
This step isn't optional. The fact that a filename
| |
| 279 | 284 |
| 280 base::string16 result16; | 285 base::string16 result16; |
| 281 if (!FilePathToString16(result, &result16)) { | 286 if (!FilePathToString16(result, &result16)) { |
| 282 result = base::FilePath(default_name_str); | 287 result = base::FilePath(default_name_str); |
| 283 if (!FilePathToString16(result, &result16)) { | 288 if (!FilePathToString16(result, &result16)) { |
| 284 result = base::FilePath(kFinalFallbackName); | 289 result = base::FilePath(kFinalFallbackName); |
| 285 FilePathToString16(result, &result16); | 290 FilePathToString16(result, &result16); |
| 286 } | 291 } |
| 287 } | 292 } |
| 288 return result16; | 293 return result16; |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 311 base::FilePath generated_name( | 316 base::FilePath generated_name( |
| 312 base::SysWideToNativeMB(base::UTF16ToWide(file_name))); | 317 base::SysWideToNativeMB(base::UTF16ToWide(file_name))); |
| 313 #endif | 318 #endif |
| 314 | 319 |
| 315 DCHECK(!generated_name.empty()); | 320 DCHECK(!generated_name.empty()); |
| 316 | 321 |
| 317 return generated_name; | 322 return generated_name; |
| 318 } | 323 } |
| 319 | 324 |
| 320 } // namespace net | 325 } // namespace net |
| OLD | NEW |