James Robinson | 24218d7 | 2014-10-20 16:18:41 -0700 | [diff] [blame] | 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 | |
James Robinson | b4b7af2 | 2014-12-05 11:21:01 -0800 | [diff] [blame] | 5 | #include "shell/filename_util.h" |
James Robinson | 24218d7 | 2014-10-20 16:18:41 -0700 | [diff] [blame] | 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 | |
Viet-Trung Luu | 36faa4d | 2015-03-04 18:08:18 -0800 | [diff] [blame] | 14 | namespace shell { |
James Robinson | 24218d7 | 2014-10-20 16:18:41 -0700 | [diff] [blame] | 15 | |
| 16 | // Prefix to prepend to get a file URL. |
| 17 | static const base::FilePath::CharType kFileURLPrefix[] = |
| 18 | FILE_PATH_LITERAL("file://"); |
| 19 | |
| 20 | GURL FilePathToFileURL(const base::FilePath& path) { |
| 21 | // Produce a URL like "file:///C:/foo" for a regular file, or |
| 22 | // "file://///server/path" for UNC. The URL canonicalizer will fix up the |
| 23 | // latter case to be the canonical UNC form: "file://server/path" |
| 24 | base::FilePath::StringType url_string(kFileURLPrefix); |
| 25 | if (!path.IsAbsolute()) { |
| 26 | base::FilePath current_dir; |
| 27 | PathService::Get(base::DIR_CURRENT, ¤t_dir); |
| 28 | url_string.append(current_dir.value()); |
| 29 | url_string.push_back(base::FilePath::kSeparators[0]); |
| 30 | } |
| 31 | url_string.append(path.value()); |
| 32 | |
| 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. |
| 36 | |
| 37 | // This must be the first substitution since others will introduce percents as |
| 38 | // the escape character |
James Robinson | c4d0fb2 | 2016-01-28 14:31:21 -0800 | [diff] [blame] | 39 | base::ReplaceSubstringsAfterOffset(&url_string, 0, FILE_PATH_LITERAL("%"), |
| 40 | FILE_PATH_LITERAL("%25")); |
James Robinson | 24218d7 | 2014-10-20 16:18:41 -0700 | [diff] [blame] | 41 | |
| 42 | // A semicolon is supposed to be some kind of separator according to RFC 2396. |
James Robinson | c4d0fb2 | 2016-01-28 14:31:21 -0800 | [diff] [blame] | 43 | base::ReplaceSubstringsAfterOffset(&url_string, 0, FILE_PATH_LITERAL(";"), |
| 44 | FILE_PATH_LITERAL("%3B")); |
James Robinson | 24218d7 | 2014-10-20 16:18:41 -0700 | [diff] [blame] | 45 | |
James Robinson | c4d0fb2 | 2016-01-28 14:31:21 -0800 | [diff] [blame] | 46 | base::ReplaceSubstringsAfterOffset(&url_string, 0, FILE_PATH_LITERAL("#"), |
| 47 | FILE_PATH_LITERAL("%23")); |
James Robinson | 24218d7 | 2014-10-20 16:18:41 -0700 | [diff] [blame] | 48 | |
James Robinson | c4d0fb2 | 2016-01-28 14:31:21 -0800 | [diff] [blame] | 49 | base::ReplaceSubstringsAfterOffset(&url_string, 0, FILE_PATH_LITERAL("?"), |
| 50 | FILE_PATH_LITERAL("%3F")); |
James Robinson | 24218d7 | 2014-10-20 16:18:41 -0700 | [diff] [blame] | 51 | |
James Robinson | c4d0fb2 | 2016-01-28 14:31:21 -0800 | [diff] [blame] | 52 | base::ReplaceSubstringsAfterOffset(&url_string, 0, FILE_PATH_LITERAL("\\"), |
| 53 | FILE_PATH_LITERAL("%5C")); |
James Robinson | 24218d7 | 2014-10-20 16:18:41 -0700 | [diff] [blame] | 54 | |
| 55 | return GURL(url_string); |
| 56 | } |
| 57 | |
Nick Bray | 29e5fc6 | 2015-01-27 12:21:30 -0800 | [diff] [blame] | 58 | GURL AddTrailingSlashIfNeeded(const GURL& url) { |
| 59 | if (!url.has_path() || *url.path().rbegin() == '/') |
| 60 | return url; |
| 61 | |
| 62 | std::string path(url.path() + '/'); |
| 63 | GURL::Replacements replacements; |
| 64 | replacements.SetPathStr(path); |
| 65 | return url.ReplaceComponents(replacements); |
| 66 | } |
| 67 | |
Viet-Trung Luu | 36faa4d | 2015-03-04 18:08:18 -0800 | [diff] [blame] | 68 | } // namespace shell |