blob: 936acb06ad2313e03a58f635b1a8b12199c4225a [file] [log] [blame]
James Robinson24218d72014-10-20 16:18:41 -07001// 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 Robinsonb4b7af22014-12-05 11:21:01 -08005#include "shell/filename_util.h"
James Robinson24218d72014-10-20 16:18:41 -07006
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 Luu36faa4d2015-03-04 18:08:18 -080014namespace shell {
James Robinson24218d72014-10-20 16:18:41 -070015
16// Prefix to prepend to get a file URL.
17static const base::FilePath::CharType kFileURLPrefix[] =
18 FILE_PATH_LITERAL("file://");
19
20GURL 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, &current_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 Robinsonc4d0fb22016-01-28 14:31:21 -080039 base::ReplaceSubstringsAfterOffset(&url_string, 0, FILE_PATH_LITERAL("%"),
40 FILE_PATH_LITERAL("%25"));
James Robinson24218d72014-10-20 16:18:41 -070041
42 // A semicolon is supposed to be some kind of separator according to RFC 2396.
James Robinsonc4d0fb22016-01-28 14:31:21 -080043 base::ReplaceSubstringsAfterOffset(&url_string, 0, FILE_PATH_LITERAL(";"),
44 FILE_PATH_LITERAL("%3B"));
James Robinson24218d72014-10-20 16:18:41 -070045
James Robinsonc4d0fb22016-01-28 14:31:21 -080046 base::ReplaceSubstringsAfterOffset(&url_string, 0, FILE_PATH_LITERAL("#"),
47 FILE_PATH_LITERAL("%23"));
James Robinson24218d72014-10-20 16:18:41 -070048
James Robinsonc4d0fb22016-01-28 14:31:21 -080049 base::ReplaceSubstringsAfterOffset(&url_string, 0, FILE_PATH_LITERAL("?"),
50 FILE_PATH_LITERAL("%3F"));
James Robinson24218d72014-10-20 16:18:41 -070051
James Robinsonc4d0fb22016-01-28 14:31:21 -080052 base::ReplaceSubstringsAfterOffset(&url_string, 0, FILE_PATH_LITERAL("\\"),
53 FILE_PATH_LITERAL("%5C"));
James Robinson24218d72014-10-20 16:18:41 -070054
55 return GURL(url_string);
56}
57
Nick Bray29e5fc62015-01-27 12:21:30 -080058GURL 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 Luu36faa4d2015-03-04 18:08:18 -080068} // namespace shell