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.
5 #include "mojo/util/filename_util.h"
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"
12 #include "url/url_canon_internal.h"
13 #include "url/url_util.h"
18 // Prefix to prepend to get a file URL.
19 static const base::FilePath::CharType kFileURLPrefix
[] =
20 FILE_PATH_LITERAL("file://");
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]);
33 url_string
.append(path
.value());
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
39 // This must be the first substitution since others will introduce percents as
40 // the escape character
41 ReplaceSubstringsAfterOffset(&url_string
, 0, FILE_PATH_LITERAL("%"),
42 FILE_PATH_LITERAL("%25"));
44 // A semicolon is supposed to be some kind of separator according to RFC 2396.
45 ReplaceSubstringsAfterOffset(&url_string
, 0, FILE_PATH_LITERAL(";"),
46 FILE_PATH_LITERAL("%3B"));
48 ReplaceSubstringsAfterOffset(&url_string
, 0, FILE_PATH_LITERAL("#"),
49 FILE_PATH_LITERAL("%23"));
51 ReplaceSubstringsAfterOffset(&url_string
, 0, FILE_PATH_LITERAL("?"),
52 FILE_PATH_LITERAL("%3F"));
55 ReplaceSubstringsAfterOffset(&url_string
, 0, FILE_PATH_LITERAL("\\"),
56 FILE_PATH_LITERAL("%5C"));
59 return GURL(url_string
);
62 GURL
AddTrailingSlashIfNeeded(const GURL
& url
) {
63 if (!url
.has_path() || *url
.path().rbegin() == '/')
66 std::string
path(url
.path() + '/');
67 GURL::Replacements replacements
;
68 replacements
.SetPathStr(path
);
69 return url
.ReplaceComponents(replacements
);
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());
79 base::TrimString(decoded_path
, L
"/", &decoded_path
);
80 base::FilePath
path(decoded_path
);
82 base::FilePath
path(base::UTF16ToUTF8(decoded_path
));