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.
5 #include "mojo/shell/mojo_url_resolver.h"
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"
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
;
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";
34 NOTREACHED() << "dynamic loading of services not supported";
39 GURL
AddTrailingSlashIfNeeded(const GURL
& url
) {
40 if (!url
.has_path() || *url
.path().rbegin() == '/')
43 std::string
path(url
.path() + '/');
44 GURL::Replacements replacements
;
45 replacements
.SetPathStr(path
);
46 return url
.ReplaceComponents(replacements
);
51 MojoURLResolver::MojoURLResolver() {
52 // Needed to treat first component of mojo URLs as host, not path.
53 url::AddStandardScheme("mojo");
55 // By default, resolve mojo URLs to files living alongside the shell.
57 PathService::Get(base::DIR_MODULE
, &path
);
58 default_base_url_
= AddTrailingSlashIfNeeded(FilePathToFileURL(path
));
61 MojoURLResolver::~MojoURLResolver() {
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
);
71 void MojoURLResolver::AddCustomMapping(const GURL
& mojo_url
,
72 const GURL
& resolved_url
) {
73 url_map_
[mojo_url
] = resolved_url
;
76 void MojoURLResolver::AddLocalFileMapping(const GURL
& mojo_url
) {
77 local_file_set_
.insert(mojo_url
);
80 GURL
MojoURLResolver::Resolve(const GURL
& mojo_url
) const {
81 const GURL
mapped_url(ApplyCustomMappings(mojo_url
));
83 // Continue resolving if the mapped url is a mojo: url.
84 if (mapped_url
.scheme() != "mojo")
87 std::string lib
= MakeSharedLibraryName(mapped_url
.host());
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
);
95 // Otherwise, resolve to an URL relative to base_url_.
96 return base_url_
.Resolve(lib
);
99 GURL
MojoURLResolver::ApplyCustomMappings(const GURL
& url
) const {
100 GURL
mapped_url(url
);
102 std::map
<GURL
, GURL
>::const_iterator it
= url_map_
.find(mapped_url
);
103 if (it
== url_map_
.end())
105 mapped_url
= it
->second
;