| 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/runner/url_resolver.h" | |
| 6 | |
| 7 #include "base/base_paths.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/strings/utf_string_conversions.h" | |
| 10 #include "mojo/runner/switches.h" | |
| 11 #include "mojo/shell/query_util.h" | |
| 12 #include "mojo/util/filename_util.h" | |
| 13 #include "url/url_util.h" | |
| 14 | |
| 15 namespace mojo { | |
| 16 namespace runner { | |
| 17 | |
| 18 URLResolver::URLResolver() { | |
| 19 // Needed to treat first component of mojo URLs as host, not path. | |
| 20 url::AddStandardScheme("mojo", url::SCHEME_WITHOUT_AUTHORITY); | |
| 21 } | |
| 22 | |
| 23 URLResolver::~URLResolver() { | |
| 24 } | |
| 25 | |
| 26 // static | |
| 27 std::vector<URLResolver::OriginMapping> URLResolver::GetOriginMappings( | |
| 28 const base::CommandLine::StringVector& args) { | |
| 29 std::vector<OriginMapping> origin_mappings; | |
| 30 const std::string kArgsForSwitches[] = { | |
| 31 "-" + std::string(switches::kMapOrigin) + "=", | |
| 32 "--" + std::string(switches::kMapOrigin) + "=", | |
| 33 }; | |
| 34 for (auto& arg : args) { | |
| 35 for (size_t i = 0; i < arraysize(kArgsForSwitches); i++) { | |
| 36 const std::string& argsfor_switch = kArgsForSwitches[i]; | |
| 37 std::string arg_string; | |
| 38 #if defined(OS_WIN) | |
| 39 arg_string = base::UTF16ToUTF8(arg); | |
| 40 #else | |
| 41 arg_string = arg; | |
| 42 #endif | |
| 43 if (arg_string.compare(0, argsfor_switch.size(), argsfor_switch) == 0) { | |
| 44 std::string value = | |
| 45 arg_string.substr(argsfor_switch.size(), std::string::npos); | |
| 46 size_t delim = value.find('='); | |
| 47 if (delim <= 0 || delim >= value.size()) | |
| 48 continue; | |
| 49 origin_mappings.push_back( | |
| 50 OriginMapping(value.substr(0, delim), | |
| 51 value.substr(delim + 1, std::string::npos))); | |
| 52 } | |
| 53 } | |
| 54 } | |
| 55 return origin_mappings; | |
| 56 } | |
| 57 | |
| 58 void URLResolver::AddURLMapping(const GURL& url, const GURL& mapped_url) { | |
| 59 url_map_[url] = mapped_url; | |
| 60 } | |
| 61 | |
| 62 void URLResolver::AddOriginMapping(const GURL& origin, const GURL& base_url) { | |
| 63 if (!origin.is_valid() || !base_url.is_valid() || | |
| 64 origin != origin.GetOrigin()) { | |
| 65 // Disallow invalid mappings. | |
| 66 LOG(ERROR) << "Invalid origin for mapping: " << origin; | |
| 67 return; | |
| 68 } | |
| 69 // Force both origin and base_url to have trailing slashes. | |
| 70 origin_map_[origin] = util::AddTrailingSlashIfNeeded(base_url); | |
| 71 } | |
| 72 | |
| 73 GURL URLResolver::ApplyMappings(const GURL& url) const { | |
| 74 std::string query; | |
| 75 GURL mapped_url = shell::GetBaseURLAndQuery(url, &query); | |
| 76 for (;;) { | |
| 77 const auto& url_it = url_map_.find(mapped_url); | |
| 78 if (url_it != url_map_.end()) { | |
| 79 mapped_url = url_it->second; | |
| 80 continue; | |
| 81 } | |
| 82 | |
| 83 GURL origin = mapped_url.GetOrigin(); | |
| 84 const auto& origin_it = origin_map_.find(origin); | |
| 85 if (origin_it == origin_map_.end()) | |
| 86 break; | |
| 87 mapped_url = GURL(origin_it->second.spec() + | |
| 88 mapped_url.spec().substr(origin.spec().length())); | |
| 89 } | |
| 90 | |
| 91 if (query.length()) | |
| 92 mapped_url = GURL(mapped_url.spec() + query); | |
| 93 return mapped_url; | |
| 94 } | |
| 95 | |
| 96 void URLResolver::SetMojoBaseURL(const GURL& mojo_base_url) { | |
| 97 DCHECK(mojo_base_url.is_valid()); | |
| 98 // Force a trailing slash on the base_url to simplify resolving | |
| 99 // relative files and URLs below. | |
| 100 mojo_base_url_ = util::AddTrailingSlashIfNeeded(mojo_base_url); | |
| 101 } | |
| 102 | |
| 103 GURL URLResolver::ResolveMojoURL(const GURL& mojo_url) const { | |
| 104 if (mojo_url.scheme() != "mojo") { | |
| 105 // The mapping has produced some sort of non-mojo: URL - file:, http:, etc. | |
| 106 return mojo_url; | |
| 107 } | |
| 108 | |
| 109 // It's still a mojo: URL, use the default mapping scheme. | |
| 110 std::string query; | |
| 111 GURL base_url = shell::GetBaseURLAndQuery(mojo_url, &query); | |
| 112 const std::string host = base_url.host(); | |
| 113 return mojo_base_url_.Resolve(host + "/" + host + ".mojo" + query); | |
| 114 } | |
| 115 | |
| 116 } // namespace runner | |
| 117 } // namespace mojo | |
| OLD | NEW |