xref: /aosp_15_r20/external/cronet/net/base/filename_util.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2014 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker #ifndef NET_BASE_FILENAME_UTIL_H_
6*6777b538SAndroid Build Coastguard Worker #define NET_BASE_FILENAME_UTIL_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #include <string>
9*6777b538SAndroid Build Coastguard Worker 
10*6777b538SAndroid Build Coastguard Worker #include "base/files/file_path.h"
11*6777b538SAndroid Build Coastguard Worker #include "net/base/net_export.h"
12*6777b538SAndroid Build Coastguard Worker 
13*6777b538SAndroid Build Coastguard Worker class GURL;
14*6777b538SAndroid Build Coastguard Worker 
15*6777b538SAndroid Build Coastguard Worker namespace base {
16*6777b538SAndroid Build Coastguard Worker class FilePath;
17*6777b538SAndroid Build Coastguard Worker }
18*6777b538SAndroid Build Coastguard Worker 
19*6777b538SAndroid Build Coastguard Worker namespace net {
20*6777b538SAndroid Build Coastguard Worker 
21*6777b538SAndroid Build Coastguard Worker // Given the full path to a file name, creates a file: URL. The returned URL
22*6777b538SAndroid Build Coastguard Worker // may not be valid if the input is malformed.
23*6777b538SAndroid Build Coastguard Worker NET_EXPORT GURL FilePathToFileURL(const base::FilePath& path);
24*6777b538SAndroid Build Coastguard Worker 
25*6777b538SAndroid Build Coastguard Worker // Converts a file: URL back to a filename that can be passed to the OS. The
26*6777b538SAndroid Build Coastguard Worker // file URL must be well-formed (GURL::is_valid() must return true); we don't
27*6777b538SAndroid Build Coastguard Worker // handle degenerate cases here. Returns true on success, false if |url| is
28*6777b538SAndroid Build Coastguard Worker // invalid or the file path cannot be extracted from |url|.
29*6777b538SAndroid Build Coastguard Worker // On failure, *file_path will be empty.
30*6777b538SAndroid Build Coastguard Worker //
31*6777b538SAndroid Build Coastguard Worker // Do not call this with a |url| that doesn't have a file:// scheme.
32*6777b538SAndroid Build Coastguard Worker // The implementation is specific to the platform filesystem, and not
33*6777b538SAndroid Build Coastguard Worker // applicable to other schemes.
34*6777b538SAndroid Build Coastguard Worker NET_EXPORT bool FileURLToFilePath(const GURL& url, base::FilePath* file_path);
35*6777b538SAndroid Build Coastguard Worker 
36*6777b538SAndroid Build Coastguard Worker // Generates a filename using the first successful method from the following (in
37*6777b538SAndroid Build Coastguard Worker // order):
38*6777b538SAndroid Build Coastguard Worker //
39*6777b538SAndroid Build Coastguard Worker // 1) The raw Content-Disposition header in |content_disposition| as read from
40*6777b538SAndroid Build Coastguard Worker //    the network.  |referrer_charset| is used to decode non-ASCII strings.
41*6777b538SAndroid Build Coastguard Worker // 2) |suggested_name| if specified.  |suggested_name| is assumed to be in
42*6777b538SAndroid Build Coastguard Worker //    UTF-8.
43*6777b538SAndroid Build Coastguard Worker // 3) The filename extracted from the |url|.  |referrer_charset| will be used to
44*6777b538SAndroid Build Coastguard Worker //    interpret the URL if there are non-ascii characters.The file extension for
45*6777b538SAndroid Build Coastguard Worker //    filenames extracted from the URL are considered unreliable if the URL
46*6777b538SAndroid Build Coastguard Worker //    contains a query string. If a MIME type is available (i.e. |mime_type| is
47*6777b538SAndroid Build Coastguard Worker //    not empty) and that MIME type has a preferred extension, then the
48*6777b538SAndroid Build Coastguard Worker //    resulting filename will have that preferred extension.
49*6777b538SAndroid Build Coastguard Worker // 4) |default_name|.  If non-empty, |default_name| is assumed to be a filename
50*6777b538SAndroid Build Coastguard Worker //    and shouldn't contain a path.  |default_name| is not subject to validation
51*6777b538SAndroid Build Coastguard Worker //    or sanitization, and therefore shouldn't be a user supplied string.
52*6777b538SAndroid Build Coastguard Worker // 5) The hostname portion from the |url|
53*6777b538SAndroid Build Coastguard Worker //
54*6777b538SAndroid Build Coastguard Worker // Then, leading and trailing '.'s will be removed.  On Windows, trailing spaces
55*6777b538SAndroid Build Coastguard Worker // are also removed.  The string "download" is the final fallback if no filename
56*6777b538SAndroid Build Coastguard Worker // is found or the filename is empty.
57*6777b538SAndroid Build Coastguard Worker //
58*6777b538SAndroid Build Coastguard Worker // Any illegal characters in the filename will be replaced by '-'.  If the
59*6777b538SAndroid Build Coastguard Worker // filename doesn't contain an extension, and a |mime_type| is specified, the
60*6777b538SAndroid Build Coastguard Worker // preferred extension for the |mime_type| will be appended to the filename.
61*6777b538SAndroid Build Coastguard Worker // The resulting filename is then checked against a list of reserved names on
62*6777b538SAndroid Build Coastguard Worker // Windows.  If the name is reserved, an underscore will be prepended to the
63*6777b538SAndroid Build Coastguard Worker // filename.
64*6777b538SAndroid Build Coastguard Worker //
65*6777b538SAndroid Build Coastguard Worker // Note: |mime_type| should only be specified if this function is called from a
66*6777b538SAndroid Build Coastguard Worker // thread that allows IO.
67*6777b538SAndroid Build Coastguard Worker NET_EXPORT std::u16string GetSuggestedFilename(
68*6777b538SAndroid Build Coastguard Worker     const GURL& url,
69*6777b538SAndroid Build Coastguard Worker     const std::string& content_disposition,
70*6777b538SAndroid Build Coastguard Worker     const std::string& referrer_charset,
71*6777b538SAndroid Build Coastguard Worker     const std::string& suggested_name,
72*6777b538SAndroid Build Coastguard Worker     const std::string& mime_type,
73*6777b538SAndroid Build Coastguard Worker     const std::string& default_name);
74*6777b538SAndroid Build Coastguard Worker 
75*6777b538SAndroid Build Coastguard Worker // Similar to GetSuggestedFilename(), but returns a FilePath.
76*6777b538SAndroid Build Coastguard Worker NET_EXPORT base::FilePath GenerateFileName(
77*6777b538SAndroid Build Coastguard Worker     const GURL& url,
78*6777b538SAndroid Build Coastguard Worker     const std::string& content_disposition,
79*6777b538SAndroid Build Coastguard Worker     const std::string& referrer_charset,
80*6777b538SAndroid Build Coastguard Worker     const std::string& suggested_name,
81*6777b538SAndroid Build Coastguard Worker     const std::string& mime_type,
82*6777b538SAndroid Build Coastguard Worker     const std::string& default_name);
83*6777b538SAndroid Build Coastguard Worker 
84*6777b538SAndroid Build Coastguard Worker // Similar to GetSuggestedFilename(). If |should_replace_extension| is true, the
85*6777b538SAndroid Build Coastguard Worker // file extension extracted from a URL will always be considered unreliable and
86*6777b538SAndroid Build Coastguard Worker // the file extension will be determined by |mime_type|.
87*6777b538SAndroid Build Coastguard Worker NET_EXPORT base::FilePath GenerateFileName(
88*6777b538SAndroid Build Coastguard Worker     const GURL& url,
89*6777b538SAndroid Build Coastguard Worker     const std::string& content_disposition,
90*6777b538SAndroid Build Coastguard Worker     const std::string& referrer_charset,
91*6777b538SAndroid Build Coastguard Worker     const std::string& suggested_name,
92*6777b538SAndroid Build Coastguard Worker     const std::string& mime_type,
93*6777b538SAndroid Build Coastguard Worker     const std::string& default_name,
94*6777b538SAndroid Build Coastguard Worker     bool should_replace_extension);
95*6777b538SAndroid Build Coastguard Worker 
96*6777b538SAndroid Build Coastguard Worker // Valid components:
97*6777b538SAndroid Build Coastguard Worker // * are not empty
98*6777b538SAndroid Build Coastguard Worker // * are not Windows reserved names (CON, NUL.zip, etc.)
99*6777b538SAndroid Build Coastguard Worker // * do not have trailing separators
100*6777b538SAndroid Build Coastguard Worker // * do not equal kCurrentDirectory
101*6777b538SAndroid Build Coastguard Worker // * do not reference the parent directory
102*6777b538SAndroid Build Coastguard Worker // * do not contain illegal characters
103*6777b538SAndroid Build Coastguard Worker // * do not end with Windows shell-integrated extensions (even on posix)
104*6777b538SAndroid Build Coastguard Worker // * do not begin with '.' (which would hide them in most file managers)
105*6777b538SAndroid Build Coastguard Worker // * do not end with ' ' or '.'
106*6777b538SAndroid Build Coastguard Worker NET_EXPORT bool IsSafePortablePathComponent(const base::FilePath& component);
107*6777b538SAndroid Build Coastguard Worker 
108*6777b538SAndroid Build Coastguard Worker // Basenames of valid relative paths are IsSafePortableBasename(), and internal
109*6777b538SAndroid Build Coastguard Worker // path components of valid relative paths are valid path components as
110*6777b538SAndroid Build Coastguard Worker // described above IsSafePortableBasename(). Valid relative paths are not
111*6777b538SAndroid Build Coastguard Worker // absolute paths.
112*6777b538SAndroid Build Coastguard Worker NET_EXPORT bool IsSafePortableRelativePath(const base::FilePath& path);
113*6777b538SAndroid Build Coastguard Worker 
114*6777b538SAndroid Build Coastguard Worker // Ensures that the filename and extension is safe to use in the filesystem.
115*6777b538SAndroid Build Coastguard Worker //
116*6777b538SAndroid Build Coastguard Worker // Assumes that |file_path| already contains a valid path or file name.  On
117*6777b538SAndroid Build Coastguard Worker // Windows if the extension causes the file to have an unsafe interaction with
118*6777b538SAndroid Build Coastguard Worker // the shell (see net_util::IsShellIntegratedExtension()), then it will be
119*6777b538SAndroid Build Coastguard Worker // replaced by the string 'download'.  If |file_path| doesn't contain an
120*6777b538SAndroid Build Coastguard Worker // extension or |ignore_extension| is true then the preferred extension, if one
121*6777b538SAndroid Build Coastguard Worker // exists, for |mime_type| will be used as the extension.
122*6777b538SAndroid Build Coastguard Worker //
123*6777b538SAndroid Build Coastguard Worker // On Windows, the filename will be checked against a set of reserved names, and
124*6777b538SAndroid Build Coastguard Worker // if so, an underscore will be prepended to the name.
125*6777b538SAndroid Build Coastguard Worker //
126*6777b538SAndroid Build Coastguard Worker // |file_name| can either be just the file name or it can be a full path to a
127*6777b538SAndroid Build Coastguard Worker // file.
128*6777b538SAndroid Build Coastguard Worker //
129*6777b538SAndroid Build Coastguard Worker // Note: |mime_type| should only be non-empty if this function is called from a
130*6777b538SAndroid Build Coastguard Worker // thread that allows IO.
131*6777b538SAndroid Build Coastguard Worker NET_EXPORT void GenerateSafeFileName(const std::string& mime_type,
132*6777b538SAndroid Build Coastguard Worker                                      bool ignore_extension,
133*6777b538SAndroid Build Coastguard Worker                                      base::FilePath* file_path);
134*6777b538SAndroid Build Coastguard Worker 
135*6777b538SAndroid Build Coastguard Worker // Returns whether the specified file name is a reserved name on Windows.
136*6777b538SAndroid Build Coastguard Worker // This includes names like "com2.zip" (which correspond to devices) and
137*6777b538SAndroid Build Coastguard Worker // desktop.ini and thumbs.db which have special meaning to the Windows shell.
138*6777b538SAndroid Build Coastguard Worker // Even on other platforms, this will return whether or not a file name is
139*6777b538SAndroid Build Coastguard Worker // reserved on Windows.
140*6777b538SAndroid Build Coastguard Worker NET_EXPORT bool IsReservedNameOnWindows(
141*6777b538SAndroid Build Coastguard Worker     const base::FilePath::StringType& filename);
142*6777b538SAndroid Build Coastguard Worker 
143*6777b538SAndroid Build Coastguard Worker }  // namespace net
144*6777b538SAndroid Build Coastguard Worker 
145*6777b538SAndroid Build Coastguard Worker #endif  // NET_BASE_FILENAME_UTIL_H_
146