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_unsafe.h" | 5 #include "net/base/filename_util_unsafe.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "net/base/filename_util.h" |
9 #include "net/base/filename_util_internal.h" | 10 #include "net/base/filename_util_internal.h" |
10 | 11 |
11 namespace { | 12 namespace { |
12 | 13 |
13 // Local ICU-independent implementation of filename sanitizing functions defined | 14 // Local ICU-independent implementation of filename sanitizing functions defined |
14 // in base/i18n/file_util_icu.h. Does not require ICU because on POSIX systems | 15 // in base/i18n/file_util_icu.h. Does not require ICU because on POSIX systems |
15 // all international characters are considered legal, so only control and | 16 // all international characters are considered legal, so only control and |
16 // special characters have to be replaced. | 17 // special characters have to be replaced. |
17 const base::FilePath::CharType illegal_characters[] = | 18 const base::FilePath::CharType illegal_characters[] = |
18 FILE_PATH_LITERAL("\"*/:<>?\\\\|\001\002\003\004\005\006\007\010\011\012") | 19 FILE_PATH_LITERAL("\"*/:<>?\\\\|\001\002\003\004\005\006\007\010\011\012") |
19 FILE_PATH_LITERAL("\013\014\015\016\017\020\021\022\023\024\025\025\027"); | 20 FILE_PATH_LITERAL("\013\014\015\016\017\020\021\022\023\024\025\025\027"); |
20 | 21 |
21 void ReplaceIllegalCharactersInPath(base::FilePath::StringType* file_name, | 22 void ReplaceIllegalCharactersInPath(base::FilePath* file_name, |
22 char replace_char) { | 23 char replace_char) { |
23 base::ReplaceChars(*file_name, | 24 base::FilePath::StringType filename_string = file_name->value(); |
24 illegal_characters, | 25 base::ReplaceChars(filename_string, illegal_characters, |
25 base::FilePath::StringType(1, replace_char), | 26 base::FilePath::StringType(1, replace_char), |
26 file_name); | 27 &filename_string); |
| 28 *file_name = base::FilePath(filename_string); |
27 } | 29 } |
28 | 30 |
29 } // namespace | 31 } // namespace |
30 | 32 |
31 namespace net { | 33 namespace net { |
32 | 34 |
33 base::FilePath::StringType GenerateFileExtensionUnsafe( | 35 base::FilePath::StringType GenerateFileExtensionUnsafe( |
34 const GURL& url, | 36 const GURL& url, |
35 const std::string& content_disposition, | 37 const std::string& content_disposition, |
36 const std::string& referrer_charset, | 38 const std::string& referrer_charset, |
37 const std::string& suggested_name, | 39 const std::string& suggested_name, |
38 const std::string& mime_type, | 40 const std::string& mime_type, |
39 const std::string& default_file_name) { | 41 const std::string& default_file_name) { |
40 base::FilePath filepath = | 42 ExtensionGenerationOption extension_option = EXTENSION_OPTION_KEEP_EXISTING; |
41 GenerateFileNameImpl(url, | 43 std::string unsafe_filename = SelectUnsafeDownloadFilename( |
42 content_disposition, | 44 url, content_disposition, referrer_charset, suggested_name, mime_type, |
43 referrer_charset, | 45 default_file_name, &extension_option); |
44 suggested_name, | 46 base::FilePath path = base::FilePath::FromUTF8Unsafe(unsafe_filename); |
45 mime_type, | 47 ReplaceIllegalCharactersInPath(&path, kReplacement); |
46 default_file_name, | 48 EnsureSafeFilenameInternal(mime_type, extension_option, &path); |
47 base::Bind(&ReplaceIllegalCharactersInPath)); | 49 return path.Extension(); |
48 return filepath.Extension(); | |
49 } | 50 } |
50 | 51 |
51 } // namespace net | 52 } // namespace net |
OLD | NEW |