[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / net / base / filename_util.cc
blob0969f7155215884875a69992130e37098ef1db07
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
3 // found in the LICENSE file.
5 #include "net/base/filename_util.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/path_service.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/sys_string_conversions.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/threading/thread_restrictions.h"
14 #include "net/base/escape.h"
15 #include "net/base/filename_util_internal.h"
16 #include "net/base/net_string_util.h"
17 #include "net/http/http_content_disposition.h"
18 #include "url/gurl.h"
20 namespace net {
22 // Prefix to prepend to get a file URL.
23 static const base::FilePath::CharType kFileURLPrefix[] =
24 FILE_PATH_LITERAL("file:///");
26 GURL FilePathToFileURL(const base::FilePath& path) {
27 // Produce a URL like "file:///C:/foo" for a regular file, or
28 // "file://///server/path" for UNC. The URL canonicalizer will fix up the
29 // latter case to be the canonical UNC form: "file://server/path"
30 base::FilePath::StringType url_string(kFileURLPrefix);
31 url_string.append(path.value());
33 // Now do replacement of some characters. Since we assume the input is a
34 // literal filename, anything the URL parser might consider special should
35 // be escaped here.
37 // must be the first substitution since others will introduce percents as the
38 // escape character
39 base::ReplaceSubstringsAfterOffset(
40 &url_string, 0, FILE_PATH_LITERAL("%"), FILE_PATH_LITERAL("%25"));
42 // semicolon is supposed to be some kind of separator according to RFC 2396
43 base::ReplaceSubstringsAfterOffset(
44 &url_string, 0, FILE_PATH_LITERAL(";"), FILE_PATH_LITERAL("%3B"));
46 base::ReplaceSubstringsAfterOffset(
47 &url_string, 0, FILE_PATH_LITERAL("#"), FILE_PATH_LITERAL("%23"));
49 base::ReplaceSubstringsAfterOffset(
50 &url_string, 0, FILE_PATH_LITERAL("?"), FILE_PATH_LITERAL("%3F"));
52 #if defined(OS_POSIX)
53 base::ReplaceSubstringsAfterOffset(
54 &url_string, 0, FILE_PATH_LITERAL("\\"), FILE_PATH_LITERAL("%5C"));
55 #endif
57 return GURL(url_string);
60 bool FileURLToFilePath(const GURL& url, base::FilePath* file_path) {
61 *file_path = base::FilePath();
62 base::FilePath::StringType& file_path_str =
63 const_cast<base::FilePath::StringType&>(file_path->value());
64 file_path_str.clear();
66 if (!url.is_valid())
67 return false;
69 #if defined(OS_WIN)
70 std::string path;
71 std::string host = url.host();
72 if (host.empty()) {
73 // URL contains no host, the path is the filename. In this case, the path
74 // will probably be preceeded with a slash, as in "/C:/foo.txt", so we
75 // trim out that here.
76 path = url.path();
77 size_t first_non_slash = path.find_first_not_of("/\\");
78 if (first_non_slash != std::string::npos && first_non_slash > 0)
79 path.erase(0, first_non_slash);
80 } else {
81 // URL contains a host: this means it's UNC. We keep the preceeding slash
82 // on the path.
83 path = "\\\\";
84 path.append(host);
85 path.append(url.path());
87 std::replace(path.begin(), path.end(), '/', '\\');
88 #else // defined(OS_WIN)
89 // Firefox seems to ignore the "host" of a file url if there is one. That is,
90 // file://foo/bar.txt maps to /bar.txt.
91 // TODO(dhg): This should probably take into account UNCs which could
92 // include a hostname other than localhost or blank
93 std::string path = url.path();
94 #endif // !defined(OS_WIN)
96 if (path.empty())
97 return false;
99 // GURL stores strings as percent-encoded 8-bit, this will undo if possible.
100 path = UnescapeURLComponent(
101 path, UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS);
103 #if defined(OS_WIN)
104 if (base::IsStringUTF8(path)) {
105 file_path_str.assign(base::UTF8ToWide(path));
106 // We used to try too hard and see if |path| made up entirely of
107 // the 1st 256 characters in the Unicode was a zero-extended UTF-16.
108 // If so, we converted it to 'Latin-1' and checked if the result was UTF-8.
109 // If the check passed, we converted the result to UTF-8.
110 // Otherwise, we treated the result as the native OS encoding.
111 // However, that led to http://crbug.com/4619 and http://crbug.com/14153
112 } else {
113 // Not UTF-8, assume encoding is native codepage and we're done. We know we
114 // are giving the conversion function a nonempty string, and it may fail if
115 // the given string is not in the current encoding and give us an empty
116 // string back. We detect this and report failure.
117 file_path_str = base::SysNativeMBToWide(path);
119 #else // defined(OS_WIN)
120 // Collapse multiple path slashes into a single path slash.
121 std::string new_path;
122 do {
123 new_path = path;
124 base::ReplaceSubstringsAfterOffset(&new_path, 0, "//", "/");
125 path.swap(new_path);
126 } while (new_path != path);
128 file_path_str.assign(path);
129 #endif // !defined(OS_WIN)
131 return !file_path_str.empty();
134 void GenerateSafeFileName(const std::string& mime_type,
135 bool ignore_extension,
136 base::FilePath* file_path) {
137 // Make sure we get the right file extension
138 EnsureSafeExtension(mime_type, ignore_extension, file_path);
140 #if defined(OS_WIN)
141 // Prepend "_" to the file name if it's a reserved name
142 base::FilePath::StringType leaf_name = file_path->BaseName().value();
143 DCHECK(!leaf_name.empty());
144 if (IsReservedNameOnWindows(leaf_name)) {
145 leaf_name = base::FilePath::StringType(FILE_PATH_LITERAL("_")) + leaf_name;
146 *file_path = file_path->DirName();
147 if (file_path->value() == base::FilePath::kCurrentDirectory) {
148 *file_path = base::FilePath(leaf_name);
149 } else {
150 *file_path = file_path->Append(leaf_name);
153 #endif
156 bool IsReservedNameOnWindows(const base::FilePath::StringType& filename) {
157 // This list is taken from the MSDN article "Naming a file"
158 // http://msdn2.microsoft.com/en-us/library/aa365247(VS.85).aspx
159 // I also added clock$ because GetSaveFileName seems to consider it as a
160 // reserved name too.
161 static const char* const known_devices[] = {
162 "con", "prn", "aux", "nul", "com1", "com2", "com3", "com4",
163 "com5", "com6", "com7", "com8", "com9", "lpt1", "lpt2", "lpt3",
164 "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9", "clock$"};
165 #if defined(OS_WIN)
166 std::string filename_lower = base::ToLowerASCII(base::WideToUTF8(filename));
167 #elif defined(OS_POSIX)
168 std::string filename_lower = base::ToLowerASCII(filename);
169 #endif
171 for (size_t i = 0; i < arraysize(known_devices); ++i) {
172 // Exact match.
173 if (filename_lower == known_devices[i])
174 return true;
175 // Starts with "DEVICE.".
176 if (filename_lower.find(std::string(known_devices[i]) + ".") == 0)
177 return true;
180 static const char* const magic_names[] = {
181 // These file names are used by the "Customize folder" feature of the
182 // shell.
183 "desktop.ini",
184 "thumbs.db",
187 for (size_t i = 0; i < arraysize(magic_names); ++i) {
188 if (filename_lower == magic_names[i])
189 return true;
192 return false;
195 } // namespace net