Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(414)

Side by Side Diff: mojo/shell/url_resolver.cc

Issue 1049993002: Get mojo_shell building inside chromium checkout. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix presubmit Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/shell/url_resolver.h ('k') | mojo/shell/url_resolver_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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/url_resolver.h"
6
7 #include "base/base_paths.h"
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "mojo/shell/application_manager/query_util.h"
12 #include "mojo/shell/filename_util.h"
13 #include "mojo/shell/switches.h"
14 #include "url/url_util.h"
15
16 namespace mojo {
17 namespace shell {
18
19 URLResolver::URLResolver() {
20 // Needed to treat first component of mojo URLs as host, not path.
21 url::AddStandardScheme("mojo");
22 }
23
24 URLResolver::~URLResolver() {
25 }
26
27 // static
28 std::vector<URLResolver::OriginMapping> URLResolver::GetOriginMappings(
29 const base::CommandLine::StringVector& args) {
30 std::vector<OriginMapping> origin_mappings;
31 const std::string kArgsForSwitches[] = {
32 "-" + std::string(switches::kMapOrigin) + "=",
33 "--" + std::string(switches::kMapOrigin) + "=",
34 };
35 for (auto& arg : args) {
36 for (size_t i = 0; i < arraysize(kArgsForSwitches); i++) {
37 const std::string& argsfor_switch = kArgsForSwitches[i];
38 std::string arg_string;
39 #if defined(OS_WIN)
40 arg_string = base::UTF16ToUTF8(arg);
41 #else
42 arg_string = arg;
43 #endif
44 if (arg_string.compare(0, argsfor_switch.size(), argsfor_switch) == 0) {
45 std::string value =
46 arg_string.substr(argsfor_switch.size(), std::string::npos);
47 size_t delim = value.find('=');
48 if (delim <= 0 || delim >= value.size())
49 continue;
50 origin_mappings.push_back(
51 OriginMapping(value.substr(0, delim),
52 value.substr(delim + 1, std::string::npos)));
53 }
54 }
55 }
56 return origin_mappings;
57 }
58
59 void URLResolver::AddURLMapping(const GURL& url, const GURL& mapped_url) {
60 url_map_[url] = mapped_url;
61 }
62
63 void URLResolver::AddOriginMapping(const GURL& origin, const GURL& base_url) {
64 if (!origin.is_valid() || !base_url.is_valid() ||
65 origin != origin.GetOrigin()) {
66 // Disallow invalid mappings.
67 LOG(ERROR) << "Invalid origin for mapping: " << origin;
68 return;
69 }
70 // Force both origin and base_url to have trailing slashes.
71 origin_map_[origin] = AddTrailingSlashIfNeeded(base_url);
72 }
73
74 GURL URLResolver::ApplyMappings(const GURL& url) const {
75 std::string query;
76 GURL mapped_url = GetBaseURLAndQuery(url, &query);
77 for (;;) {
78 const auto& url_it = url_map_.find(mapped_url);
79 if (url_it != url_map_.end()) {
80 mapped_url = url_it->second;
81 continue;
82 }
83
84 GURL origin = mapped_url.GetOrigin();
85 const auto& origin_it = origin_map_.find(origin);
86 if (origin_it == origin_map_.end())
87 break;
88 mapped_url = GURL(origin_it->second.spec() +
89 mapped_url.spec().substr(origin.spec().length()));
90 }
91
92 if (query.length())
93 mapped_url = GURL(mapped_url.spec() + query);
94 return mapped_url;
95 }
96
97 void URLResolver::SetMojoBaseURL(const GURL& mojo_base_url) {
98 DCHECK(mojo_base_url.is_valid());
99 // Force a trailing slash on the base_url to simplify resolving
100 // relative files and URLs below.
101 mojo_base_url_ = AddTrailingSlashIfNeeded(mojo_base_url);
102 }
103
104 GURL URLResolver::ResolveMojoURL(const GURL& mojo_url) const {
105 if (mojo_url.scheme() != "mojo") {
106 // The mapping has produced some sort of non-mojo: URL - file:, http:, etc.
107 return mojo_url;
108 } else {
109 // It's still a mojo: URL, use the default mapping scheme.
110 std::string query;
111 GURL base_url = GetBaseURLAndQuery(mojo_url, &query);
112 std::string lib = base_url.host() + ".mojo" + query;
113 return mojo_base_url_.Resolve(lib);
114 }
115 }
116
117 } // namespace shell
118 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/shell/url_resolver.h ('k') | mojo/shell/url_resolver_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698