OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/shell/mojo_url_resolver.h" | |
6 | |
7 #include "base/base_paths.h" | |
8 #include "base/files/file_path.h" | |
9 #include "base/logging.h" | |
10 #include "base/path_service.h" | |
11 #include "base/strings/string_util.h" | |
12 #include "mojo/shell/filename_util.h" | |
13 #include "url/url_util.h" | |
14 | |
15 namespace mojo { | |
16 namespace shell { | |
17 namespace { | |
18 | |
19 std::string MakeSharedLibraryName(std::string host_name) { | |
20 // TODO(aa): This should go away soon. In the Chromium repo, all the app | |
21 // target names start with "mojo_" by convention. But when we have an SDK, | |
22 // one would assume the libraries would have names that don't have this bit. | |
23 std::string prefix = "mojo_"; | |
24 if (!StartsWithASCII(host_name, prefix, true)) | |
25 host_name = prefix + host_name; | |
26 | |
27 #if defined(OS_WIN) | |
28 return host_name + ".dll"; | |
29 #elif defined(OS_LINUX) || defined(OS_ANDROID) | |
30 return "lib" + host_name + ".so"; | |
31 #elif defined(OS_MACOSX) | |
32 return host_name + ".so"; | |
33 #else | |
34 NOTREACHED() << "dynamic loading of services not supported"; | |
35 return std::string(); | |
36 #endif | |
37 } | |
38 | |
39 GURL AddTrailingSlashIfNeeded(const GURL& url) { | |
40 if (!url.has_path() || *url.path().rbegin() == '/') | |
41 return url; | |
42 | |
43 std::string path(url.path() + '/'); | |
44 GURL::Replacements replacements; | |
45 replacements.SetPathStr(path); | |
46 return url.ReplaceComponents(replacements); | |
47 } | |
48 | |
49 } // namespace | |
50 | |
51 MojoURLResolver::MojoURLResolver() { | |
52 // Needed to treat first component of mojo URLs as host, not path. | |
53 url::AddStandardScheme("mojo"); | |
54 | |
55 // By default, resolve mojo URLs to files living alongside the shell. | |
56 base::FilePath path; | |
57 PathService::Get(base::DIR_MODULE, &path); | |
58 default_base_url_ = AddTrailingSlashIfNeeded(FilePathToFileURL(path)); | |
59 } | |
60 | |
61 MojoURLResolver::~MojoURLResolver() { | |
62 } | |
63 | |
64 void MojoURLResolver::SetBaseURL(const GURL& base_url) { | |
65 DCHECK(base_url.is_valid()); | |
66 // Force a trailing slash on the base_url to simplify resolving | |
67 // relative files and URLs below. | |
68 base_url_ = AddTrailingSlashIfNeeded(base_url); | |
69 } | |
70 | |
71 void MojoURLResolver::AddCustomMapping(const GURL& mojo_url, | |
72 const GURL& resolved_url) { | |
73 url_map_[mojo_url] = resolved_url; | |
74 } | |
75 | |
76 void MojoURLResolver::AddLocalFileMapping(const GURL& mojo_url) { | |
77 local_file_set_.insert(mojo_url); | |
78 } | |
79 | |
80 GURL MojoURLResolver::Resolve(const GURL& mojo_url) const { | |
81 const GURL mapped_url(ApplyCustomMappings(mojo_url)); | |
82 | |
83 // Continue resolving if the mapped url is a mojo: url. | |
84 if (mapped_url.scheme() != "mojo") | |
85 return mapped_url; | |
86 | |
87 std::string lib = MakeSharedLibraryName(mapped_url.host()); | |
88 | |
89 if (!base_url_.is_valid() || | |
90 local_file_set_.find(mapped_url) != local_file_set_.end()) { | |
91 // Resolve to a local file URL. | |
92 return default_base_url_.Resolve(lib); | |
93 } | |
94 | |
95 // Otherwise, resolve to an URL relative to base_url_. | |
96 return base_url_.Resolve(lib); | |
97 } | |
98 | |
99 GURL MojoURLResolver::ApplyCustomMappings(const GURL& url) const { | |
100 GURL mapped_url(url); | |
101 for (;;) { | |
102 std::map<GURL, GURL>::const_iterator it = url_map_.find(mapped_url); | |
103 if (it == url_map_.end()) | |
104 break; | |
105 mapped_url = it->second; | |
106 } | |
107 return mapped_url; | |
108 } | |
109 | |
110 } // namespace shell | |
111 } // namespace mojo | |
OLD | NEW |