diff options
author | John Abd-El-Malek <jam@chromium.org> | 2015-04-02 10:29:35 -0700 |
---|---|---|
committer | John Abd-El-Malek <jam@chromium.org> | 2015-04-02 17:31:11 +0000 |
commit | 537a670451020f4764d511cbdf8e30ec91ef897c (patch) | |
tree | d2868da2b0d33dc7ed8c8e709ae4a7f5bd5aefd8 /mojo/shell/filename_util.cc | |
parent | 83653dd1da59dfa7ddd9e48d4cd507a11cefd968 (diff) | |
download | chromium_src-537a670451020f4764d511cbdf8e30ec91ef897c.zip chromium_src-537a670451020f4764d511cbdf8e30ec91ef897c.tar.gz chromium_src-537a670451020f4764d511cbdf8e30ec91ef897c.tar.bz2 |
Get mojo_shell building inside chromium checkout.
This brings in mojo_shell and the necessary services to make html_viewer work.
This is copied from the Mojo repo at 272fbba5887d66fc0111e2ab44c1edf67b7f23e0.
R=scottmg@chromium.org
Review URL: https://codereview.chromium.org/1049993002
Cr-Commit-Position: refs/heads/master@{#323528}
Diffstat (limited to 'mojo/shell/filename_util.cc')
-rw-r--r-- | mojo/shell/filename_util.cc | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/mojo/shell/filename_util.cc b/mojo/shell/filename_util.cc new file mode 100644 index 0000000..073bdda --- /dev/null +++ b/mojo/shell/filename_util.cc @@ -0,0 +1,72 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "mojo/shell/filename_util.h" + +#include "base/files/file_path.h" +#include "base/path_service.h" +#include "base/strings/string_util.h" +#include "url/gurl.h" +#include "url/url_canon_internal.h" +#include "url/url_util.h" + +namespace mojo { +namespace shell { + +// Prefix to prepend to get a file URL. +static const base::FilePath::CharType kFileURLPrefix[] = + FILE_PATH_LITERAL("file://"); + +GURL FilePathToFileURL(const base::FilePath& path) { + // Produce a URL like "file:///C:/foo" for a regular file, or + // "file://///server/path" for UNC. The URL canonicalizer will fix up the + // latter case to be the canonical UNC form: "file://server/path" + base::FilePath::StringType url_string(kFileURLPrefix); + if (!path.IsAbsolute()) { + base::FilePath current_dir; + PathService::Get(base::DIR_CURRENT, ¤t_dir); + url_string.append(current_dir.value()); + url_string.push_back(base::FilePath::kSeparators[0]); + } + url_string.append(path.value()); + + // Now do replacement of some characters. Since we assume the input is a + // literal filename, anything the URL parser might consider special should + // be escaped here. + + // This must be the first substitution since others will introduce percents as + // the escape character + ReplaceSubstringsAfterOffset(&url_string, 0, FILE_PATH_LITERAL("%"), + FILE_PATH_LITERAL("%25")); + + // A semicolon is supposed to be some kind of separator according to RFC 2396. + ReplaceSubstringsAfterOffset(&url_string, 0, FILE_PATH_LITERAL(";"), + FILE_PATH_LITERAL("%3B")); + + ReplaceSubstringsAfterOffset(&url_string, 0, FILE_PATH_LITERAL("#"), + FILE_PATH_LITERAL("%23")); + + ReplaceSubstringsAfterOffset(&url_string, 0, FILE_PATH_LITERAL("?"), + FILE_PATH_LITERAL("%3F")); + +#if defined(OS_POSIX) + ReplaceSubstringsAfterOffset(&url_string, 0, FILE_PATH_LITERAL("\\"), + FILE_PATH_LITERAL("%5C")); +#endif + + return GURL(url_string); +} + +GURL AddTrailingSlashIfNeeded(const GURL& url) { + if (!url.has_path() || *url.path().rbegin() == '/') + return url; + + std::string path(url.path() + '/'); + GURL::Replacements replacements; + replacements.SetPathStr(path); + return url.ReplaceComponents(replacements); +} + +} // namespace shell +} // namespace mojo |