| 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/mojo_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 "url/url_util.h" | 11 #include "url/url_util.h" |
| 12 | 12 |
| 13 namespace mojo { | 13 namespace mojo { |
| 14 namespace shell { | 14 namespace shell { |
| 15 | 15 |
| 16 MojoURLResolver::MojoURLResolver() { | 16 URLResolver::URLResolver() { |
| 17 // Needed to treat first component of mojo URLs as host, not path. | 17 // Needed to treat first component of mojo URLs as host, not path. |
| 18 url::AddStandardScheme("mojo"); | 18 url::AddStandardScheme("mojo"); |
| 19 } | 19 } |
| 20 | 20 |
| 21 MojoURLResolver::~MojoURLResolver() { | 21 URLResolver::~URLResolver() { |
| 22 } | 22 } |
| 23 | 23 |
| 24 void MojoURLResolver::SetBaseURL(const GURL& base_url) { | 24 void URLResolver::AddURLMapping(const GURL& url, const GURL& resolved_url) { |
| 25 DCHECK(base_url.is_valid()); | 25 url_map_[url] = resolved_url; |
| 26 // Force a trailing slash on the base_url to simplify resolving | |
| 27 // relative files and URLs below. | |
| 28 base_url_ = AddTrailingSlashIfNeeded(base_url); | |
| 29 } | 26 } |
| 30 | 27 |
| 31 void MojoURLResolver::AddCustomMapping(const GURL& mojo_url, | 28 GURL URLResolver::ApplyURLMappings(const GURL& url) const { |
| 32 const GURL& resolved_url) { | |
| 33 url_map_[mojo_url] = resolved_url; | |
| 34 } | |
| 35 | |
| 36 GURL MojoURLResolver::Resolve(const GURL& mojo_url) const { | |
| 37 const GURL mapped_url(ApplyCustomMappings(mojo_url)); | |
| 38 | |
| 39 if (mapped_url.scheme() != "mojo") { | |
| 40 // The mapping has produced some sort of non-mojo: URL - file:, http:, etc. | |
| 41 return mapped_url; | |
| 42 } else { | |
| 43 // It's still a mojo: URL, use the default mapping scheme. | |
| 44 std::string lib = mapped_url.host() + ".mojo"; | |
| 45 return base_url_.Resolve(lib); | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 GURL MojoURLResolver::ApplyCustomMappings(const GURL& url) const { | |
| 50 GURL mapped_url(url); | 29 GURL mapped_url(url); |
| 51 for (;;) { | 30 for (;;) { |
| 52 std::map<GURL, GURL>::const_iterator it = url_map_.find(mapped_url); | 31 const auto& it = url_map_.find(mapped_url); |
| 53 if (it == url_map_.end()) | 32 if (it == url_map_.end()) |
| 54 break; | 33 break; |
| 55 mapped_url = it->second; | 34 mapped_url = it->second; |
| 56 } | 35 } |
| 57 return mapped_url; | 36 return mapped_url; |
| 58 } | 37 } |
| 59 | 38 |
| 39 void URLResolver::SetMojoBaseURL(const GURL& mojo_base_url) { |
| 40 DCHECK(mojo_base_url.is_valid()); |
| 41 // Force a trailing slash on the base_url to simplify resolving |
| 42 // relative files and URLs below. |
| 43 mojo_base_url_ = AddTrailingSlashIfNeeded(mojo_base_url); |
| 44 } |
| 45 |
| 46 GURL URLResolver::ResolveMojoURL(const GURL& mojo_url) const { |
| 47 if (mojo_url.scheme() != "mojo") { |
| 48 // The mapping has produced some sort of non-mojo: URL - file:, http:, etc. |
| 49 return mojo_url; |
| 50 } else { |
| 51 // It's still a mojo: URL, use the default mapping scheme. |
| 52 std::string lib = mojo_url.host() + ".mojo"; |
| 53 return mojo_base_url_.Resolve(lib); |
| 54 } |
| 55 } |
| 56 |
| 60 } // namespace shell | 57 } // namespace shell |
| 61 } // namespace mojo | 58 } // namespace mojo |
| OLD | NEW |