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