| 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/mojo_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 "base/path_service.h" | |
| 11 #include "shell/filename_util.h" | |
| 12 #include "url/url_util.h" | 10 #include "url/url_util.h" |
| 13 | 11 |
| 14 namespace mojo { | 12 namespace mojo { |
| 15 namespace shell { | 13 namespace shell { |
| 16 namespace { | |
| 17 | |
| 18 GURL AddTrailingSlashIfNeeded(const GURL& url) { | |
| 19 if (!url.has_path() || *url.path().rbegin() == '/') | |
| 20 return url; | |
| 21 | |
| 22 std::string path(url.path() + '/'); | |
| 23 GURL::Replacements replacements; | |
| 24 replacements.SetPathStr(path); | |
| 25 return url.ReplaceComponents(replacements); | |
| 26 } | |
| 27 | |
| 28 } // namespace | |
| 29 | 14 |
| 30 MojoURLResolver::MojoURLResolver() { | 15 MojoURLResolver::MojoURLResolver() { |
| 31 // Needed to treat first component of mojo URLs as host, not path. | 16 // Needed to treat first component of mojo URLs as host, not path. |
| 32 url::AddStandardScheme("mojo"); | 17 url::AddStandardScheme("mojo"); |
| 33 // By default assume that the local apps reside alongside the shell. | |
| 34 base::FilePath path; | |
| 35 PathService::Get(base::DIR_MODULE, &path); | |
| 36 local_apps_url_ = AddTrailingSlashIfNeeded(FilePathToFileURL(path)); | |
| 37 } | 18 } |
| 38 | 19 |
| 39 MojoURLResolver::~MojoURLResolver() { | 20 MojoURLResolver::~MojoURLResolver() { |
| 40 } | 21 } |
| 41 | 22 |
| 42 void MojoURLResolver::SetBaseURL(const GURL& base_url) { | 23 void MojoURLResolver::SetBaseURL(const GURL& base_url) { |
| 43 DCHECK(base_url.is_valid()); | 24 DCHECK(base_url.is_valid()); |
| 44 // Force a trailing slash on the base_url to simplify resolving | 25 // Force a trailing slash on the base_url to simplify resolving |
| 45 // relative files and URLs below. | 26 // relative files and URLs below. |
| 46 base_url_ = AddTrailingSlashIfNeeded(base_url); | 27 DCHECK(!base_url.has_path() || *base_url.path().rbegin() == '/'); |
| 47 } | 28 base_url_ = base_url; |
| 48 | |
| 49 void MojoURLResolver::SetLocalAppsPath(const base::FilePath& local_apps_path) { | |
| 50 local_apps_url_ = | |
| 51 AddTrailingSlashIfNeeded(FilePathToFileURL(local_apps_path)); | |
| 52 DCHECK(local_apps_url_.is_valid()); | |
| 53 } | 29 } |
| 54 | 30 |
| 55 void MojoURLResolver::AddCustomMapping(const GURL& mojo_url, | 31 void MojoURLResolver::AddCustomMapping(const GURL& mojo_url, |
| 56 const GURL& resolved_url) { | 32 const GURL& resolved_url) { |
| 57 url_map_[mojo_url] = resolved_url; | 33 url_map_[mojo_url] = resolved_url; |
| 58 } | 34 } |
| 59 | 35 |
| 60 void MojoURLResolver::AddLocalFileMapping(const GURL& mojo_url) { | |
| 61 local_file_set_.insert(mojo_url); | |
| 62 } | |
| 63 | |
| 64 GURL MojoURLResolver::Resolve(const GURL& mojo_url) const { | 36 GURL MojoURLResolver::Resolve(const GURL& mojo_url) const { |
| 65 const GURL mapped_url(ApplyCustomMappings(mojo_url)); | 37 const GURL mapped_url(ApplyCustomMappings(mojo_url)); |
| 66 | 38 |
| 67 // Continue resolving if the mapped url is a mojo: url. | 39 if (mapped_url.scheme() != "mojo") { |
| 68 if (mapped_url.scheme() != "mojo") | 40 // The mapping has produced some sort of non-mojo: URL - file:, http:, etc. |
| 69 return mapped_url; | 41 return mapped_url; |
| 70 | 42 } else { |
| 71 std::string lib = mapped_url.host() + ".mojo"; | 43 // It's still a mojo: URL, use the default mapping scheme. |
| 72 | 44 std::string lib = mapped_url.host() + ".mojo"; |
| 73 if (!base_url_.is_valid() || | 45 return base_url_.Resolve(lib); |
| 74 local_file_set_.find(mapped_url) != local_file_set_.end()) { | |
| 75 // Resolve to a local file URL. | |
| 76 return local_apps_url_.Resolve(lib); | |
| 77 } | 46 } |
| 78 | |
| 79 // Otherwise, resolve to an URL relative to base_url_. | |
| 80 return base_url_.Resolve(lib); | |
| 81 } | 47 } |
| 82 | 48 |
| 83 GURL MojoURLResolver::ApplyCustomMappings(const GURL& url) const { | 49 GURL MojoURLResolver::ApplyCustomMappings(const GURL& url) const { |
| 84 GURL mapped_url(url); | 50 GURL mapped_url(url); |
| 85 for (;;) { | 51 for (;;) { |
| 86 std::map<GURL, GURL>::const_iterator it = url_map_.find(mapped_url); | 52 std::map<GURL, GURL>::const_iterator it = url_map_.find(mapped_url); |
| 87 if (it == url_map_.end()) | 53 if (it == url_map_.end()) |
| 88 break; | 54 break; |
| 89 mapped_url = it->second; | 55 mapped_url = it->second; |
| 90 } | 56 } |
| 91 return mapped_url; | 57 return mapped_url; |
| 92 } | 58 } |
| 93 | 59 |
| 94 } // namespace shell | 60 } // namespace shell |
| 95 } // namespace mojo | 61 } // namespace mojo |
| OLD | NEW |