OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "mojo/util/filename_util.h" | |
6 | |
7 #include "base/files/file_path.h" | |
8 #include "base/path_service.h" | |
9 #include "base/strings/string_util.h" | |
10 #include "base/strings/utf_string_conversions.h" | |
11 #include "url/gurl.h" | |
12 #include "url/url_canon_internal.h" | |
13 #include "url/url_util.h" | |
14 | |
15 namespace mojo { | |
16 namespace util { | |
17 | |
18 // Prefix to prepend to get a file URL. | |
19 static const base::FilePath::CharType kFileURLPrefix[] = | |
20 FILE_PATH_LITERAL("file://"); | |
21 | |
22 GURL FilePathToFileURL(const base::FilePath& path) { | |
23 // Produce a URL like "file:///C:/foo" for a regular file, or | |
24 // "file://///server/path" for UNC. The URL canonicalizer will fix up the | |
25 // latter case to be the canonical UNC form: "file://server/path" | |
26 base::FilePath::StringType url_string(kFileURLPrefix); | |
27 if (!path.IsAbsolute()) { | |
28 base::FilePath current_dir; | |
29 PathService::Get(base::DIR_CURRENT, ¤t_dir); | |
30 url_string.append(current_dir.value()); | |
31 url_string.push_back(base::FilePath::kSeparators[0]); | |
32 } | |
33 url_string.append(path.value()); | |
34 | |
35 // Now do replacement of some characters. Since we assume the input is a | |
36 // literal filename, anything the URL parser might consider special should | |
37 // be escaped here. | |
38 | |
39 // This must be the first substitution since others will introduce percents as | |
40 // the escape character | |
41 base::ReplaceSubstringsAfterOffset(&url_string, 0, FILE_PATH_LITERAL("%"), | |
42 FILE_PATH_LITERAL("%25")); | |
43 | |
44 // A semicolon is supposed to be some kind of separator according to RFC 2396. | |
45 base::ReplaceSubstringsAfterOffset(&url_string, 0, FILE_PATH_LITERAL(";"), | |
46 FILE_PATH_LITERAL("%3B")); | |
47 | |
48 base::ReplaceSubstringsAfterOffset(&url_string, 0, FILE_PATH_LITERAL("#"), | |
49 FILE_PATH_LITERAL("%23")); | |
50 | |
51 base::ReplaceSubstringsAfterOffset(&url_string, 0, FILE_PATH_LITERAL("?"), | |
52 FILE_PATH_LITERAL("%3F")); | |
53 | |
54 #if defined(OS_POSIX) | |
55 base::ReplaceSubstringsAfterOffset(&url_string, 0, FILE_PATH_LITERAL("\\"), | |
56 FILE_PATH_LITERAL("%5C")); | |
57 #endif | |
58 | |
59 return GURL(url_string); | |
60 } | |
61 | |
62 GURL AddTrailingSlashIfNeeded(const GURL& url) { | |
63 if (!url.has_path() || *url.path().rbegin() == '/') | |
64 return url; | |
65 | |
66 std::string path(url.path() + '/'); | |
67 GURL::Replacements replacements; | |
68 replacements.SetPathStr(path); | |
69 return url.ReplaceComponents(replacements); | |
70 } | |
71 | |
72 base::FilePath UrlToFilePath(const GURL& url) { | |
73 DCHECK(url.SchemeIsFile()); | |
74 url::RawCanonOutputW<1024> output; | |
75 url::DecodeURLEscapeSequences(url.path().data(), | |
76 static_cast<int>(url.path().length()), &output); | |
77 base::string16 decoded_path = base::string16(output.data(), output.length()); | |
78 #if defined(OS_WIN) | |
79 base::TrimString(decoded_path, L"/", &decoded_path); | |
80 base::FilePath path(decoded_path); | |
81 #else | |
82 base::FilePath path(base::UTF16ToUTF8(decoded_path)); | |
83 #endif | |
84 return path; | |
85 } | |
86 | |
87 } // namespace util | |
88 } // namespace mojo | |
OLD | NEW |