| OLD | NEW |
| 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/application_manager/query_util.h" |
| 10 #include "shell/filename_util.h" | 11 #include "shell/filename_util.h" |
| 11 #include "shell/switches.h" | 12 #include "shell/switches.h" |
| 12 #include "url/url_util.h" | 13 #include "url/url_util.h" |
| 13 | 14 |
| 14 namespace mojo { | 15 namespace mojo { |
| 15 namespace shell { | 16 namespace shell { |
| 16 | 17 |
| 17 URLResolver::URLResolver() { | 18 URLResolver::URLResolver() { |
| 18 // Needed to treat first component of mojo URLs as host, not path. | 19 // Needed to treat first component of mojo URLs as host, not path. |
| 19 url::AddStandardScheme("mojo"); | 20 url::AddStandardScheme("mojo"); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 origin != origin.GetOrigin()) { | 58 origin != origin.GetOrigin()) { |
| 58 // Disallow invalid mappings. | 59 // Disallow invalid mappings. |
| 59 LOG(ERROR) << "Invalid origin for mapping: " << origin; | 60 LOG(ERROR) << "Invalid origin for mapping: " << origin; |
| 60 return; | 61 return; |
| 61 } | 62 } |
| 62 // Force both origin and base_url to have trailing slashes. | 63 // Force both origin and base_url to have trailing slashes. |
| 63 origin_map_[origin] = AddTrailingSlashIfNeeded(base_url); | 64 origin_map_[origin] = AddTrailingSlashIfNeeded(base_url); |
| 64 } | 65 } |
| 65 | 66 |
| 66 GURL URLResolver::ApplyMappings(const GURL& url) const { | 67 GURL URLResolver::ApplyMappings(const GURL& url) const { |
| 67 GURL mapped_url(url); | 68 auto url_and_suffix = GetBaseURLAndQuery(url); |
| 69 GURL mapped_url = url_and_suffix.first; |
| 68 for (;;) { | 70 for (;;) { |
| 69 const auto& url_it = url_map_.find(mapped_url); | 71 const auto& url_it = url_map_.find(mapped_url); |
| 70 if (url_it != url_map_.end()) { | 72 if (url_it != url_map_.end()) { |
| 71 mapped_url = url_it->second; | 73 mapped_url = url_it->second; |
| 72 continue; | 74 continue; |
| 73 } | 75 } |
| 74 | 76 |
| 75 GURL origin = mapped_url.GetOrigin(); | 77 GURL origin = mapped_url.GetOrigin(); |
| 76 const auto& origin_it = origin_map_.find(origin); | 78 const auto& origin_it = origin_map_.find(origin); |
| 77 if (origin_it == origin_map_.end()) | 79 if (origin_it == origin_map_.end()) |
| 78 break; | 80 break; |
| 79 mapped_url = GURL(origin_it->second.spec() + | 81 mapped_url = GURL(origin_it->second.spec() + |
| 80 mapped_url.spec().substr(origin.spec().length())); | 82 mapped_url.spec().substr(origin.spec().length())); |
| 81 } | 83 } |
| 82 | 84 |
| 85 if (url_and_suffix.second.length()) |
| 86 mapped_url = GURL(mapped_url.spec() + url_and_suffix.second); |
| 83 return mapped_url; | 87 return mapped_url; |
| 84 } | 88 } |
| 85 | 89 |
| 86 void URLResolver::SetMojoBaseURL(const GURL& mojo_base_url) { | 90 void URLResolver::SetMojoBaseURL(const GURL& mojo_base_url) { |
| 87 DCHECK(mojo_base_url.is_valid()); | 91 DCHECK(mojo_base_url.is_valid()); |
| 88 // Force a trailing slash on the base_url to simplify resolving | 92 // Force a trailing slash on the base_url to simplify resolving |
| 89 // relative files and URLs below. | 93 // relative files and URLs below. |
| 90 mojo_base_url_ = AddTrailingSlashIfNeeded(mojo_base_url); | 94 mojo_base_url_ = AddTrailingSlashIfNeeded(mojo_base_url); |
| 91 } | 95 } |
| 92 | 96 |
| 93 GURL URLResolver::ResolveMojoURL(const GURL& mojo_url) const { | 97 GURL URLResolver::ResolveMojoURL(const GURL& mojo_url) const { |
| 94 if (mojo_url.scheme() != "mojo") { | 98 if (mojo_url.scheme() != "mojo") { |
| 95 // The mapping has produced some sort of non-mojo: URL - file:, http:, etc. | 99 // The mapping has produced some sort of non-mojo: URL - file:, http:, etc. |
| 96 return mojo_url; | 100 return mojo_url; |
| 97 } else { | 101 } else { |
| 98 // It's still a mojo: URL, use the default mapping scheme. | 102 // It's still a mojo: URL, use the default mapping scheme. |
| 99 std::string suffix = ""; | 103 auto url_and_suffix = GetBaseURLAndQuery(mojo_url); |
| 100 if (mojo_url.has_query()) { | 104 std::string lib = |
| 101 suffix = "?" + mojo_url.query(); | 105 url_and_suffix.first.host() + ".mojo" + url_and_suffix.second; |
| 102 } | |
| 103 std::string lib = mojo_url.host() + ".mojo" + suffix; | |
| 104 return mojo_base_url_.Resolve(lib); | 106 return mojo_base_url_.Resolve(lib); |
| 105 } | 107 } |
| 106 } | 108 } |
| 107 | 109 |
| 108 } // namespace shell | 110 } // namespace shell |
| 109 } // namespace mojo | 111 } // namespace mojo |
| OLD | NEW |