Index: net/base/filename_util_icu.cc |
diff --git a/net/base/filename_util_icu.cc b/net/base/filename_util_icu.cc |
index 8c22eec9520b84de32fcab7a76481e168a1ce2b4..2876517d3c0fd67398bfd61ae3d157a118e9d3a2 100644 |
--- a/net/base/filename_util_icu.cc |
+++ b/net/base/filename_util_icu.cc |
@@ -14,19 +14,67 @@ class GURL; |
namespace net { |
+namespace { |
+ |
+void ReplaceIllegalCharactersInPath(base::FilePath* path) { |
+ base::FilePath::StringType path_string = path->value(); |
+ base::i18n::ReplaceIllegalCharactersInPath(&path_string, kReplacement); |
+ *path = base::FilePath(path_string); |
+} |
+} |
+ |
+void EnsureSafePortableFileName(const std::string& mime_type, |
+ ExtensionGenerationOption extension_option, |
+ base::FilePath* file_path) { |
+ base::FilePath basename = file_path->BaseName(); |
+ ReplaceIllegalCharactersInPath(&basename); |
+ EnsureSafeFilenameInternal(mime_type, extension_option, &basename); |
+ |
+ base::FilePath directory = file_path->DirName(); |
+ if (directory.value() == base::FilePath::kCurrentDirectory) |
+ *file_path = basename; |
+ else |
+ *file_path = directory.Append(basename.value()); |
+ |
+ // When doing file manager operations on ChromeOS, the file paths get |
+ // normalized in WebKit layer, so let's ensure downloaded files have |
+ // normalized names. Otherwise, we won't be able to handle files with NFD |
+ // utf8 encoded characters in name. |
+ base::i18n::NormalizeFileNameEncoding(file_path); |
+} |
+ |
+base::FilePath GenerateFileName(const GURL& url, |
+ const std::string& content_disposition, |
+ const std::string& referrer_charset, |
+ const std::string& suggested_name, |
+ const std::string& mime_type, |
+ const std::string& default_name) { |
+ static const base::FilePath::CharType kFinalFallbackName[] = |
+ FILE_PATH_LITERAL("download"); |
+ ExtensionGenerationOption extension_option = EXTENSION_OPTION_KEEP_EXISTING; |
+ |
+ std::string filename = SelectUnsafeDownloadFilename( |
+ url, content_disposition, referrer_charset, suggested_name, mime_type, |
+ default_name, &extension_option); |
+ |
+ base::FilePath result(base::FilePath::FromUTF8Unsafe(filename)); |
+ ReplaceIllegalCharactersInPath(&result); |
+ EnsureSafeFilenameInternal(mime_type, extension_option, &result); |
+ base::i18n::NormalizeFileNameEncoding(&result); |
+ |
+ if (result.empty()) |
+ result = base::FilePath(kFinalFallbackName); |
+ |
+ return result; |
+} |
+ |
bool IsSafePortablePathComponent(const base::FilePath& component) { |
- base::string16 component16; |
- base::FilePath::StringType sanitized = component.value(); |
- SanitizeGeneratedFileName(&sanitized, true); |
- base::FilePath::StringType extension = component.Extension(); |
- if (!extension.empty()) |
- extension.erase(extension.begin()); // Erase preceding '.'. |
+ base::FilePath sanitized = component; |
+ EnsureSafePortableFileName(std::string(), EXTENSION_OPTION_KEEP_EXISTING, |
+ &sanitized); |
return !component.empty() && (component == component.BaseName()) && |
(component == component.StripTrailingSeparators()) && |
- FilePathToString16(component, &component16) && |
- base::i18n::IsFilenameLegal(component16) && |
- !IsShellIntegratedExtension(extension) && |
- (sanitized == component.value()) && !IsReservedName(component.value()); |
+ sanitized == component; |
} |
bool IsSafePortableRelativePath(const base::FilePath& path) { |
@@ -36,55 +84,11 @@ bool IsSafePortableRelativePath(const base::FilePath& path) { |
path.GetComponents(&components); |
if (components.empty()) |
return false; |
- for (size_t i = 0; i < components.size() - 1; ++i) { |
- if (!IsSafePortablePathComponent(base::FilePath(components[i]))) |
+ for (const auto& component : components) { |
+ if (!IsSafePortablePathComponent(base::FilePath(component))) |
return false; |
} |
- return IsSafePortablePathComponent(path.BaseName()); |
-} |
- |
-base::string16 GetSuggestedFilename(const GURL& url, |
- const std::string& content_disposition, |
- const std::string& referrer_charset, |
- const std::string& suggested_name, |
- const std::string& mime_type, |
- const std::string& default_name) { |
- return GetSuggestedFilenameImpl( |
- url, |
- content_disposition, |
- referrer_charset, |
- suggested_name, |
- mime_type, |
- default_name, |
- base::Bind(&base::i18n::ReplaceIllegalCharactersInPath)); |
-} |
- |
-base::FilePath GenerateFileName(const GURL& url, |
- const std::string& content_disposition, |
- const std::string& referrer_charset, |
- const std::string& suggested_name, |
- const std::string& mime_type, |
- const std::string& default_file_name) { |
- base::FilePath generated_name(GenerateFileNameImpl( |
- url, |
- content_disposition, |
- referrer_charset, |
- suggested_name, |
- mime_type, |
- default_file_name, |
- base::Bind(&base::i18n::ReplaceIllegalCharactersInPath))); |
- |
-#if defined(OS_CHROMEOS) |
- // When doing file manager operations on ChromeOS, the file paths get |
- // normalized in WebKit layer, so let's ensure downloaded files have |
- // normalized names. Otherwise, we won't be able to handle files with NFD |
- // utf8 encoded characters in name. |
- base::i18n::NormalizeFileNameEncoding(&generated_name); |
-#endif |
- |
- DCHECK(!generated_name.empty()); |
- |
- return generated_name; |
+ return true; |
} |
} // namespace net |