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