OLD | NEW |
| (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 | |
OLD | NEW |