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

Side by Side 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, 10 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/bind.h" 7 #include "base/bind.h"
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/i18n/file_util_icu.h" 9 #include "base/i18n/file_util_icu.h"
10 #include "base/strings/string16.h" 10 #include "base/strings/string16.h"
11 #include "net/base/filename_util_internal.h" 11 #include "net/base/filename_util_internal.h"
12 12
13 class GURL; 13 class GURL;
14 14
15 namespace net { 15 namespace net {
16 16
17 namespace {
18
19 void ReplaceIllegalCharactersInPath(base::FilePath* path) {
20 base::FilePath::StringType path_string = path->value();
21 base::i18n::ReplaceIllegalCharactersInPath(&path_string, kReplacement);
22 *path = base::FilePath(path_string);
23 }
24 }
25
26 void EnsureSafePortableFileName(const std::string& mime_type,
27 ExtensionGenerationOption extension_option,
28 base::FilePath* file_path) {
29 base::FilePath basename = file_path->BaseName();
30 ReplaceIllegalCharactersInPath(&basename);
31 EnsureSafeFilenameInternal(mime_type, extension_option, &basename);
32
33 base::FilePath directory = file_path->DirName();
34 if (directory.value() == base::FilePath::kCurrentDirectory)
35 *file_path = basename;
36 else
37 *file_path = directory.Append(basename.value());
38
39 // When doing file manager operations on ChromeOS, the file paths get
40 // normalized in WebKit layer, so let's ensure downloaded files have
41 // normalized names. Otherwise, we won't be able to handle files with NFD
42 // utf8 encoded characters in name.
43 base::i18n::NormalizeFileNameEncoding(file_path);
44 }
45
46 base::FilePath GenerateFileName(const GURL& url,
47 const std::string& content_disposition,
48 const std::string& referrer_charset,
49 const std::string& suggested_name,
50 const std::string& mime_type,
51 const std::string& default_name) {
52 static const base::FilePath::CharType kFinalFallbackName[] =
53 FILE_PATH_LITERAL("download");
54 ExtensionGenerationOption extension_option = EXTENSION_OPTION_KEEP_EXISTING;
55
56 std::string filename = SelectUnsafeDownloadFilename(
57 url, content_disposition, referrer_charset, suggested_name, mime_type,
58 default_name, &extension_option);
59
60 base::FilePath result(base::FilePath::FromUTF8Unsafe(filename));
61 ReplaceIllegalCharactersInPath(&result);
62 EnsureSafeFilenameInternal(mime_type, extension_option, &result);
63 base::i18n::NormalizeFileNameEncoding(&result);
64
65 if (result.empty())
66 result = base::FilePath(kFinalFallbackName);
67
68 return result;
69 }
70
17 bool IsSafePortablePathComponent(const base::FilePath& component) { 71 bool IsSafePortablePathComponent(const base::FilePath& component) {
18 base::string16 component16; 72 base::FilePath sanitized = component;
19 base::FilePath::StringType sanitized = component.value(); 73 EnsureSafePortableFileName(std::string(), EXTENSION_OPTION_KEEP_EXISTING,
20 SanitizeGeneratedFileName(&sanitized, true); 74 &sanitized);
21 base::FilePath::StringType extension = component.Extension();
22 if (!extension.empty())
23 extension.erase(extension.begin()); // Erase preceding '.'.
24 return !component.empty() && (component == component.BaseName()) && 75 return !component.empty() && (component == component.BaseName()) &&
25 (component == component.StripTrailingSeparators()) && 76 (component == component.StripTrailingSeparators()) &&
26 FilePathToString16(component, &component16) && 77 sanitized == component;
27 base::i18n::IsFilenameLegal(component16) &&
28 !IsShellIntegratedExtension(extension) &&
29 (sanitized == component.value()) && !IsReservedName(component.value());
30 } 78 }
31 79
32 bool IsSafePortableRelativePath(const base::FilePath& path) { 80 bool IsSafePortableRelativePath(const base::FilePath& path) {
33 if (path.empty() || path.IsAbsolute() || path.EndsWithSeparator()) 81 if (path.empty() || path.IsAbsolute() || path.EndsWithSeparator())
34 return false; 82 return false;
35 std::vector<base::FilePath::StringType> components; 83 std::vector<base::FilePath::StringType> components;
36 path.GetComponents(&components); 84 path.GetComponents(&components);
37 if (components.empty()) 85 if (components.empty())
38 return false; 86 return false;
39 for (size_t i = 0; i < components.size() - 1; ++i) { 87 for (const auto& component : components) {
40 if (!IsSafePortablePathComponent(base::FilePath(components[i]))) 88 if (!IsSafePortablePathComponent(base::FilePath(component)))
41 return false; 89 return false;
42 } 90 }
43 return IsSafePortablePathComponent(path.BaseName()); 91 return true;
44 }
45
46 base::string16 GetSuggestedFilename(const GURL& url,
47 const std::string& content_disposition,
48 const std::string& referrer_charset,
49 const std::string& suggested_name,
50 const std::string& mime_type,
51 const std::string& default_name) {
52 return GetSuggestedFilenameImpl(
53 url,
54 content_disposition,
55 referrer_charset,
56 suggested_name,
57 mime_type,
58 default_name,
59 base::Bind(&base::i18n::ReplaceIllegalCharactersInPath));
60 }
61
62 base::FilePath GenerateFileName(const GURL& url,
63 const std::string& content_disposition,
64 const std::string& referrer_charset,
65 const std::string& suggested_name,
66 const std::string& mime_type,
67 const std::string& default_file_name) {
68 base::FilePath generated_name(GenerateFileNameImpl(
69 url,
70 content_disposition,
71 referrer_charset,
72 suggested_name,
73 mime_type,
74 default_file_name,
75 base::Bind(&base::i18n::ReplaceIllegalCharactersInPath)));
76
77 #if defined(OS_CHROMEOS)
78 // When doing file manager operations on ChromeOS, the file paths get
79 // normalized in WebKit layer, so let's ensure downloaded files have
80 // normalized names. Otherwise, we won't be able to handle files with NFD
81 // utf8 encoded characters in name.
82 base::i18n::NormalizeFileNameEncoding(&generated_name);
83 #endif
84
85 DCHECK(!generated_name.empty());
86
87 return generated_name;
88 } 92 }
89 93
90 } // namespace net 94 } // namespace net
OLDNEW
« 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