| OLD | NEW |
| 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/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 // latter case to be the canonical UNC form: "file://server/path" | 29 // latter case to be the canonical UNC form: "file://server/path" |
| 30 base::FilePath::StringType url_string(kFileURLPrefix); | 30 base::FilePath::StringType url_string(kFileURLPrefix); |
| 31 url_string.append(path.value()); | 31 url_string.append(path.value()); |
| 32 | 32 |
| 33 // Now do replacement of some characters. Since we assume the input is a | 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 | 34 // literal filename, anything the URL parser might consider special should |
| 35 // be escaped here. | 35 // be escaped here. |
| 36 | 36 |
| 37 // must be the first substitution since others will introduce percents as the | 37 // must be the first substitution since others will introduce percents as the |
| 38 // escape character | 38 // escape character |
| 39 ReplaceSubstringsAfterOffset( | 39 base::ReplaceSubstringsAfterOffset(&url_string, 0, FILE_PATH_LITERAL("%"), |
| 40 &url_string, 0, FILE_PATH_LITERAL("%"), FILE_PATH_LITERAL("%25")); | 40 FILE_PATH_LITERAL("%25")); |
| 41 | 41 |
| 42 // semicolon is supposed to be some kind of separator according to RFC 2396 | 42 // semicolon is supposed to be some kind of separator according to RFC 2396 |
| 43 ReplaceSubstringsAfterOffset( | 43 base::ReplaceSubstringsAfterOffset(&url_string, 0, FILE_PATH_LITERAL(";"), |
| 44 &url_string, 0, FILE_PATH_LITERAL(";"), FILE_PATH_LITERAL("%3B")); | 44 FILE_PATH_LITERAL("%3B")); |
| 45 | 45 |
| 46 ReplaceSubstringsAfterOffset( | 46 base::ReplaceSubstringsAfterOffset(&url_string, 0, FILE_PATH_LITERAL("#"), |
| 47 &url_string, 0, FILE_PATH_LITERAL("#"), FILE_PATH_LITERAL("%23")); | 47 FILE_PATH_LITERAL("%23")); |
| 48 | 48 |
| 49 ReplaceSubstringsAfterOffset( | 49 base::ReplaceSubstringsAfterOffset(&url_string, 0, FILE_PATH_LITERAL("?"), |
| 50 &url_string, 0, FILE_PATH_LITERAL("?"), FILE_PATH_LITERAL("%3F")); | 50 FILE_PATH_LITERAL("%3F")); |
| 51 | 51 |
| 52 #if defined(OS_POSIX) | 52 #if defined(OS_POSIX) |
| 53 ReplaceSubstringsAfterOffset( | 53 base::ReplaceSubstringsAfterOffset(&url_string, 0, FILE_PATH_LITERAL("\\"), |
| 54 &url_string, 0, FILE_PATH_LITERAL("\\"), FILE_PATH_LITERAL("%5C")); | 54 FILE_PATH_LITERAL("%5C")); |
| 55 #endif | 55 #endif |
| 56 | 56 |
| 57 return GURL(url_string); | 57 return GURL(url_string); |
| 58 } | 58 } |
| 59 | 59 |
| 60 bool FileURLToFilePath(const GURL& url, base::FilePath* file_path) { | 60 bool FileURLToFilePath(const GURL& url, base::FilePath* file_path) { |
| 61 *file_path = base::FilePath(); | 61 *file_path = base::FilePath(); |
| 62 base::FilePath::StringType& file_path_str = | 62 base::FilePath::StringType& file_path_str = |
| 63 const_cast<base::FilePath::StringType&>(file_path->value()); | 63 const_cast<base::FilePath::StringType&>(file_path->value()); |
| 64 file_path_str.clear(); | 64 file_path_str.clear(); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 // are giving the conversion function a nonempty string, and it may fail if | 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 | 115 // the given string is not in the current encoding and give us an empty |
| 116 // string back. We detect this and report failure. | 116 // string back. We detect this and report failure. |
| 117 file_path_str = base::SysNativeMBToWide(path); | 117 file_path_str = base::SysNativeMBToWide(path); |
| 118 } | 118 } |
| 119 #else // defined(OS_WIN) | 119 #else // defined(OS_WIN) |
| 120 // Collapse multiple path slashes into a single path slash. | 120 // Collapse multiple path slashes into a single path slash. |
| 121 std::string new_path; | 121 std::string new_path; |
| 122 do { | 122 do { |
| 123 new_path = path; | 123 new_path = path; |
| 124 ReplaceSubstringsAfterOffset(&new_path, 0, "//", "/"); | 124 base::ReplaceSubstringsAfterOffset(&new_path, 0, "//", "/"); |
| 125 path.swap(new_path); | 125 path.swap(new_path); |
| 126 } while (new_path != path); | 126 } while (new_path != path); |
| 127 | 127 |
| 128 file_path_str.assign(path); | 128 file_path_str.assign(path); |
| 129 #endif // !defined(OS_WIN) | 129 #endif // !defined(OS_WIN) |
| 130 | 130 |
| 131 return !file_path_str.empty(); | 131 return !file_path_str.empty(); |
| 132 } | 132 } |
| 133 | 133 |
| 134 void GenerateSafeFileName(const std::string& mime_type, | 134 void GenerateSafeFileName(const std::string& mime_type, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 147 if (file_path->value() == base::FilePath::kCurrentDirectory) { | 147 if (file_path->value() == base::FilePath::kCurrentDirectory) { |
| 148 *file_path = base::FilePath(leaf_name); | 148 *file_path = base::FilePath(leaf_name); |
| 149 } else { | 149 } else { |
| 150 *file_path = file_path->Append(leaf_name); | 150 *file_path = file_path->Append(leaf_name); |
| 151 } | 151 } |
| 152 } | 152 } |
| 153 #endif | 153 #endif |
| 154 } | 154 } |
| 155 | 155 |
| 156 } // namespace net | 156 } // namespace net |
| OLD | NEW |