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

Side by Side Diff: shell/url_resolver.cc

Issue 926093003: AddOriginMapping() (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Comments Created 5 years, 10 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "shell/url_resolver.h" 5 #include "shell/url_resolver.h"
6 6
7 #include "base/base_paths.h" 7 #include "base/base_paths.h"
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "shell/filename_util.h" 10 #include "shell/filename_util.h"
11 #include "shell/switches.h"
11 #include "url/url_util.h" 12 #include "url/url_util.h"
12 13
13 namespace mojo { 14 namespace mojo {
14 namespace shell { 15 namespace shell {
15 16
16 URLResolver::URLResolver() { 17 URLResolver::URLResolver() {
17 // Needed to treat first component of mojo URLs as host, not path. 18 // Needed to treat first component of mojo URLs as host, not path.
18 url::AddStandardScheme("mojo"); 19 url::AddStandardScheme("mojo");
19 } 20 }
20 21
21 URLResolver::~URLResolver() { 22 URLResolver::~URLResolver() {
22 } 23 }
23 24
24 void URLResolver::AddURLMapping(const GURL& url, const GURL& resolved_url) { 25 // static
25 url_map_[url] = resolved_url; 26 std::vector<URLResolver::OriginMapping> URLResolver::GetOriginMappings(
27 const std::vector<std::string> args) {
28 std::vector<OriginMapping> origin_mappings;
29 const std::string kArgsForSwitches[] = {
30 "-" + std::string(switches::kMapOrigin) + "=",
Aaron Boodman 2015/02/13 22:44:15 Can't we just pick one? Why have choices when we c
DaveMoore 2015/02/13 23:07:10 It's consistent with the rest of the switches. bas
blundell 2015/02/16 08:32:34 nit: is it not possible to allow a space instead o
31 "--" + std::string(switches::kMapOrigin) + "=",
32 };
33 for (auto& arg : args) {
34 for (size_t i = 0; i < arraysize(kArgsForSwitches); i++) {
35 const std::string& argsfor_switch = kArgsForSwitches[i];
36 if (arg.compare(0, argsfor_switch.size(), argsfor_switch) == 0) {
37 std::string value =
38 arg.substr(argsfor_switch.size(), std::string::npos);
39 size_t delim = value.find('=');
40 if (delim <= 0 || delim >= value.size())
41 continue;
42 origin_mappings.push_back(
43 OriginMapping(value.substr(0, delim),
44 value.substr(delim + 1, std::string::npos)));
45 }
46 }
47 }
48 return origin_mappings;
26 } 49 }
27 50
28 GURL URLResolver::ApplyURLMappings(const GURL& url) const { 51 void URLResolver::AddURLMapping(const GURL& url, const GURL& mapped_url) {
52 url_map_[url] = mapped_url;
53 }
54
55 void URLResolver::AddOriginMapping(const GURL& origin, const GURL& base_url) {
56 if (!origin.is_valid() || !base_url.is_valid() ||
57 origin != origin.GetOrigin()) {
58 // Disallow invalid mappings.
59 LOG(ERROR) << "Invalid origin for mapping: " << origin;
60 return;
61 }
62 // Force both origin and base_url to have trailing slashes.
63 origin_map_[origin] = AddTrailingSlashIfNeeded(base_url);
64 }
65
66 GURL URLResolver::ApplyMappings(const GURL& url) const {
29 GURL mapped_url(url); 67 GURL mapped_url(url);
30 for (;;) { 68 for (;;) {
31 const auto& it = url_map_.find(mapped_url); 69 const auto& url_it = url_map_.find(mapped_url);
32 if (it == url_map_.end()) 70 if (url_it != url_map_.end()) {
71 mapped_url = url_it->second;
72 continue;
73 }
74
75 GURL origin = mapped_url.GetOrigin();
76 const auto& origin_it = origin_map_.find(origin);
77 if (origin_it == origin_map_.end())
33 break; 78 break;
34 mapped_url = it->second; 79 mapped_url = GURL(origin_it->second.spec() +
80 mapped_url.spec().substr(origin.spec().length()));
35 } 81 }
82
36 return mapped_url; 83 return mapped_url;
37 } 84 }
38 85
39 void URLResolver::SetMojoBaseURL(const GURL& mojo_base_url) { 86 void URLResolver::SetMojoBaseURL(const GURL& mojo_base_url) {
40 DCHECK(mojo_base_url.is_valid()); 87 DCHECK(mojo_base_url.is_valid());
41 // Force a trailing slash on the base_url to simplify resolving 88 // Force a trailing slash on the base_url to simplify resolving
42 // relative files and URLs below. 89 // relative files and URLs below.
43 mojo_base_url_ = AddTrailingSlashIfNeeded(mojo_base_url); 90 mojo_base_url_ = AddTrailingSlashIfNeeded(mojo_base_url);
44 } 91 }
45 92
46 GURL URLResolver::ResolveMojoURL(const GURL& mojo_url) const { 93 GURL URLResolver::ResolveMojoURL(const GURL& mojo_url) const {
47 if (mojo_url.scheme() != "mojo") { 94 if (mojo_url.scheme() != "mojo") {
48 // The mapping has produced some sort of non-mojo: URL - file:, http:, etc. 95 // The mapping has produced some sort of non-mojo: URL - file:, http:, etc.
49 return mojo_url; 96 return mojo_url;
50 } else { 97 } else {
51 // It's still a mojo: URL, use the default mapping scheme. 98 // It's still a mojo: URL, use the default mapping scheme.
52 std::string suffix = ""; 99 std::string suffix = "";
53 if (mojo_url.has_query()) { 100 if (mojo_url.has_query()) {
54 suffix = "?" + mojo_url.query(); 101 suffix = "?" + mojo_url.query();
55 } 102 }
56 std::string lib = mojo_url.host() + ".mojo" + suffix; 103 std::string lib = mojo_url.host() + ".mojo" + suffix;
57 return mojo_base_url_.Resolve(lib); 104 return mojo_base_url_.Resolve(lib);
58 } 105 }
59 } 106 }
60 107
61 } // namespace shell 108 } // namespace shell
62 } // namespace mojo 109 } // namespace mojo
OLDNEW
« shell/switches.cc ('K') | « shell/url_resolver.h ('k') | shell/url_resolver_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698