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/shell/mojo_url_resolver.h" | 5 #include "mojo/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 10 matching lines...) Expand all Loading... |
21 #elif defined(OS_LINUX) || defined(OS_ANDROID) | 21 #elif defined(OS_LINUX) || defined(OS_ANDROID) |
22 return "lib" + host_name + ".so"; | 22 return "lib" + host_name + ".so"; |
23 #elif defined(OS_MACOSX) | 23 #elif defined(OS_MACOSX) |
24 return host_name + ".so"; | 24 return host_name + ".so"; |
25 #else | 25 #else |
26 NOTREACHED() << "dynamic loading of services not supported"; | 26 NOTREACHED() << "dynamic loading of services not supported"; |
27 return std::string(); | 27 return std::string(); |
28 #endif | 28 #endif |
29 } | 29 } |
30 | 30 |
| 31 GURL AddTrailingSlashIfNeeded(const GURL& url) { |
| 32 if (!url.has_path() || *url.path().rbegin() == '/') |
| 33 return url; |
| 34 |
| 35 std::string path(url.path() + '/'); |
| 36 GURL::Replacements replacements; |
| 37 replacements.SetPathStr(path); |
| 38 return url.ReplaceComponents(replacements); |
| 39 } |
| 40 |
31 } // namespace | 41 } // namespace |
32 | 42 |
33 MojoURLResolver::MojoURLResolver() { | 43 MojoURLResolver::MojoURLResolver() { |
34 // Needed to treat first component of mojo URLs as host, not path. | 44 // Needed to treat first component of mojo URLs as host, not path. |
35 url::AddStandardScheme("mojo"); | 45 url::AddStandardScheme("mojo"); |
| 46 |
| 47 // By default, resolve mojo URLs to files living alongside the shell. |
| 48 base::FilePath path; |
| 49 PathService::Get(base::DIR_MODULE, &path); |
| 50 default_base_url_ = AddTrailingSlashIfNeeded(net::FilePathToFileURL(path)); |
36 } | 51 } |
37 | 52 |
38 MojoURLResolver::~MojoURLResolver() { | 53 MojoURLResolver::~MojoURLResolver() { |
39 } | 54 } |
40 | 55 |
41 void MojoURLResolver::SetBaseURL(const GURL& base_url) { | 56 void MojoURLResolver::SetBaseURL(const GURL& base_url) { |
42 DCHECK(base_url.is_valid()); | 57 DCHECK(base_url.is_valid()); |
43 base_url_ = base_url; | |
44 // Force a trailing slash on the base_url to simplify resolving | 58 // Force a trailing slash on the base_url to simplify resolving |
45 // relative files and URLs below. | 59 // relative files and URLs below. |
46 if (base_url.has_path() && *base_url.path().rbegin() != '/') { | 60 base_url_ = AddTrailingSlashIfNeeded(base_url); |
47 std::string path(base_url.path() + '/'); | |
48 GURL::Replacements replacements; | |
49 replacements.SetPathStr(path); | |
50 base_url_ = base_url_.ReplaceComponents(replacements); | |
51 } | |
52 } | 61 } |
53 | 62 |
54 void MojoURLResolver::AddCustomMapping(const GURL& mojo_url, | 63 void MojoURLResolver::AddCustomMapping(const GURL& mojo_url, |
55 const GURL& resolved_url) { | 64 const GURL& resolved_url) { |
56 url_map_[mojo_url] = resolved_url; | 65 url_map_[mojo_url] = resolved_url; |
57 } | 66 } |
58 | 67 |
59 void MojoURLResolver::AddLocalFileMapping(const GURL& mojo_url) { | 68 void MojoURLResolver::AddLocalFileMapping(const GURL& mojo_url) { |
60 local_file_set_.insert(mojo_url); | 69 local_file_set_.insert(mojo_url); |
61 } | 70 } |
62 | 71 |
63 GURL MojoURLResolver::Resolve(const GURL& mojo_url) const { | 72 GURL MojoURLResolver::Resolve(const GURL& mojo_url) const { |
64 std::map<GURL, GURL>::const_iterator it = url_map_.find(mojo_url); | 73 std::map<GURL, GURL>::const_iterator it = url_map_.find(mojo_url); |
65 if (it != url_map_.end()) | 74 if (it != url_map_.end()) |
66 return it->second; | 75 return it->second; |
67 | 76 |
68 std::string lib = MakeSharedLibraryName(mojo_url.host()); | 77 std::string lib = MakeSharedLibraryName(mojo_url.host()); |
69 | 78 |
70 if (local_file_set_.find(mojo_url) != local_file_set_.end()) { | 79 if (!base_url_.is_valid() || |
| 80 local_file_set_.find(mojo_url) != local_file_set_.end()) { |
71 // Resolve to a local file URL. | 81 // Resolve to a local file URL. |
72 base::FilePath path; | 82 return default_base_url_.Resolve(lib); |
73 PathService::Get(base::DIR_MODULE, &path); | |
74 path = path.Append(base::FilePath::FromUTF8Unsafe(lib)); | |
75 return net::FilePathToFileURL(path); | |
76 } | 83 } |
77 | 84 |
78 // Otherwise, resolve to an URL relative to base_url_. | 85 // Otherwise, resolve to an URL relative to base_url_. |
79 return base_url_.Resolve(lib); | 86 return base_url_.Resolve(lib); |
80 } | 87 } |
81 | 88 |
82 } // namespace shell | 89 } // namespace shell |
83 } // namespace mojo | 90 } // namespace mojo |
OLD | NEW |