Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(469)

Unified Diff: net/base/filename_util_icu.cc

Issue 869233006: [net] Cleanup filename_util and make it portable. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@clang-format-filename-tests
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/base/filename_util.cc ('k') | net/base/filename_util_internal.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
« no previous file with comments | « net/base/filename_util.cc ('k') | net/base/filename_util_internal.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698