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