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