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

Side by Side Diff: net/base/filename_util.h

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 | « content/child/web_url_loader_impl.cc ('k') | net/base/filename_util.cc » ('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 #ifndef NET_BASE_FILENAME_UTIL_H_ 5 #ifndef NET_BASE_FILENAME_UTIL_H_
6 #define NET_BASE_FILENAME_UTIL_H_ 6 #define NET_BASE_FILENAME_UTIL_H_
7 7
8 #include <string> 8 #include <string>
9 9
10 #include "base/strings/string16.h" 10 #include "base/strings/string16.h"
(...skipping 21 matching lines...) Expand all
32 // order): 32 // order):
33 // 33 //
34 // 1) The raw Content-Disposition header in |content_disposition| as read from 34 // 1) The raw Content-Disposition header in |content_disposition| as read from
35 // the network. |referrer_charset| is used to decode non-ASCII strings. 35 // the network. |referrer_charset| is used to decode non-ASCII strings.
36 // 2) |suggested_name| if specified. |suggested_name| is assumed to be in 36 // 2) |suggested_name| if specified. |suggested_name| is assumed to be in
37 // UTF-8. 37 // UTF-8.
38 // 3) The filename extracted from the |url|. |referrer_charset| will be used to 38 // 3) The filename extracted from the |url|. |referrer_charset| will be used to
39 // interpret the URL if there are non-ascii characters. 39 // interpret the URL if there are non-ascii characters.
40 // 4) |default_name|. If non-empty, |default_name| is assumed to be a filename 40 // 4) |default_name|. If non-empty, |default_name| is assumed to be a filename
41 // and shouldn't contain a path. |default_name| is not subject to validation 41 // and shouldn't contain a path. |default_name| is not subject to validation
42 // or sanitization, and therefore shouldn't be a user supplied string. 42 // or sanitization, and therefore shouldn't be a user supplied string. It is
43 // 5) The hostname portion from the |url| 43 // assumed to be in UTF-8.
44 // 5) The hostname portion from the |url|.
45 // 6) The string "download".
44 // 46 //
45 // Then, leading and trailing '.'s will be removed. On Windows, trailing spaces 47 // Any illegal characters in the filename will be replaced by '_'. This includes
46 // are also removed. The string "download" is the final fallback if no filename 48 // Ileading and trailing whitespace and periods. If the filename doesn't contain
47 // is found or the filename is empty. 49 // an extension, and a |mime_type| is specified, the preferred extension for the
48 // 50 // |mime_type| will be appended to the filename. The resulting filename is then
49 // Any illegal characters in the filename will be replaced by '-'. If the 51 // checked against a list of reserved names on Windows. If the name is
50 // filename doesn't contain an extension, and a |mime_type| is specified, the 52 // reserved, an underscore will be prepended to the filename.
51 // preferred extension for the |mime_type| will be appended to the filename.
52 // The resulting filename is then checked against a list of reserved names on
53 // Windows. If the name is reserved, an underscore will be prepended to the
54 // filename.
55 // 53 //
56 // Note: |mime_type| should only be specified if this function is called from a 54 // Note: |mime_type| should only be specified if this function is called from a
57 // thread that allows IO. 55 // thread that allows IO.
58 NET_EXPORT base::string16 GetSuggestedFilename(
59 const GURL& url,
60 const std::string& content_disposition,
61 const std::string& referrer_charset,
62 const std::string& suggested_name,
63 const std::string& mime_type,
64 const std::string& default_name);
65
66 // Similar to GetSuggestedFilename(), but returns a FilePath.
67 NET_EXPORT base::FilePath GenerateFileName( 56 NET_EXPORT base::FilePath GenerateFileName(
68 const GURL& url, 57 const GURL& url,
69 const std::string& content_disposition, 58 const std::string& content_disposition,
70 const std::string& referrer_charset, 59 const std::string& referrer_charset,
71 const std::string& suggested_name, 60 const std::string& suggested_name,
72 const std::string& mime_type, 61 const std::string& mime_type,
73 const std::string& default_name); 62 const std::string& default_name);
74 63
75 // Valid components: 64 // Valid components:
76 // * are not empty 65 // * are not empty
77 // * are not Windows reserved names (CON, NUL.zip, etc.) 66 // * are not Windows reserved names (CON, NUL.zip, etc.)
78 // * do not have trailing separators 67 // * do not have trailing separators
79 // * do not equal kCurrentDirectory 68 // * do not equal kCurrentDirectory
80 // * do not reference the parent directory 69 // * do not reference the parent directory
81 // * do not contain illegal characters 70 // * do not contain illegal characters
82 // * do not end with Windows shell-integrated extensions (even on posix) 71 // * do not end with Windows shell-integrated extensions (even on posix)
83 // * do not begin with '.' (which would hide them in most file managers) 72 // * do not begin with '.' (which would hide them in most file managers)
84 // * do not end with ' ' or '.' 73 // * do not end with ' ' or '.'
85 NET_EXPORT bool IsSafePortablePathComponent(const base::FilePath& component); 74 NET_EXPORT bool IsSafePortablePathComponent(const base::FilePath& component);
86 75
87 // Basenames of valid relative paths are IsSafePortableBasename(), and internal 76 // Basenames of valid relative paths are IsSafePortableBasename(), and internal
88 // path components of valid relative paths are valid path components as 77 // path components of valid relative paths are valid path components as
89 // described above IsSafePortableBasename(). Valid relative paths are not 78 // described above IsSafePortableBasename(). Valid relative paths are not
90 // absolute paths. 79 // absolute paths.
91 NET_EXPORT bool IsSafePortableRelativePath(const base::FilePath& path); 80 NET_EXPORT bool IsSafePortableRelativePath(const base::FilePath& path);
92 81
82 // Options controlling how filename extensions are handled in
83 // EnsureSafePortableFileName().
84 enum ExtensionGenerationOption {
85 EXTENSION_OPTION_KEEP_EXISTING, // Don't generate a new extension.
86 EXTENSION_OPTION_GENERATE_IF_MISSING, // Generate one if the filename doesn't
87 // already have an extension.
88 EXTENSION_OPTION_GENERATE, // Always generate a new extension and replace the
89 // existing extension.
90 EXTENSION_OPTION_GENERATE_AND_APPEND // Generate a new extension and append
91 // it to the filename.
92 };
93
93 // Ensures that the filename and extension is safe to use in the filesystem. 94 // Ensures that the filename and extension is safe to use in the filesystem.
94 // 95 //
95 // Assumes that |file_path| already contains a valid path or file name. On 96 // Note that this function only sanitizes the basename of |file_path|. I.e. it
96 // Windows if the extension causes the file to have an unsafe interaction with 97 // assumes that the path leading up to the parent directory of |file_path| is
97 // the shell (see net_util::IsShellIntegratedExtension()), then it will be 98 // valid.
98 // replaced by the string 'download'. If |file_path| doesn't contain an
99 // extension or |ignore_extension| is true then the preferred extension, if one
100 // exists, for |mime_type| will be used as the extension.
101 // 99 //
102 // On Windows, the filename will be checked against a set of reserved names, and 100 // This function:
103 // if so, an underscore will be prepended to the name.
104 // 101 //
105 // |file_name| can either be just the file name or it can be a full path to a 102 // - Updates |file_path| with a valid filename extension if |mime_type| is known
106 // file. 103 // and |extension_option| specifies that an extension should be generated.
104 //
105 // - Replaces the extension with the string 'download' if the filename extension
106 // is considered to be one which interacts with the Windows shell in an unsafe
107 // manner.
108 //
109 // - Prepends an underscore to the filename if the filename will be considered a
110 // device name or is otherwise reserved on Windows.
111 //
112 // - Replaces any illegal characters in the filename with underscores. This
113 // includes leading and trailing whitespace and periods.
107 // 114 //
108 // Note: |mime_type| should only be non-empty if this function is called from a 115 // Note: |mime_type| should only be non-empty if this function is called from a
109 // thread that allows IO. 116 // thread that allows IO.
110 NET_EXPORT void GenerateSafeFileName(const std::string& mime_type, 117 NET_EXPORT void EnsureSafePortableFileName(
111 bool ignore_extension, 118 const std::string& mime_type,
112 base::FilePath* file_path); 119 ExtensionGenerationOption extension_option,
120 base::FilePath* file_path);
113 121
114 } // namespace net 122 } // namespace net
115 123
116 #endif // NET_BASE_FILENAME_UTIL_H_ 124 #endif // NET_BASE_FILENAME_UTIL_H_
OLDNEW
« no previous file with comments | « content/child/web_url_loader_impl.cc ('k') | net/base/filename_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698