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 "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 "shell/filename_util.h" | |
11 #include "url/url_util.h" | |
12 | |
13 namespace mojo { | |
14 namespace shell { | |
15 | |
16 MojoURLResolver::MojoURLResolver() { | |
17 // Needed to treat first component of mojo URLs as host, not path. | |
18 url::AddStandardScheme("mojo"); | |
19 } | |
20 | |
21 MojoURLResolver::~MojoURLResolver() { | |
22 } | |
23 | |
24 void MojoURLResolver::SetBaseURL(const GURL& base_url) { | |
25 DCHECK(base_url.is_valid()); | |
26 // Force a trailing slash on the base_url to simplify resolving | |
27 // relative files and URLs below. | |
28 base_url_ = AddTrailingSlashIfNeeded(base_url); | |
29 } | |
30 | |
31 void MojoURLResolver::AddCustomMapping(const GURL& mojo_url, | |
32 const GURL& resolved_url) { | |
33 url_map_[mojo_url] = resolved_url; | |
34 } | |
35 | |
36 GURL MojoURLResolver::Resolve(const GURL& mojo_url) const { | |
37 const GURL mapped_url(ApplyCustomMappings(mojo_url)); | |
38 | |
39 if (mapped_url.scheme() != "mojo") { | |
40 // The mapping has produced some sort of non-mojo: URL - file:, http:, etc. | |
41 return mapped_url; | |
42 } else { | |
43 // It's still a mojo: URL, use the default mapping scheme. | |
44 std::string lib = mapped_url.host() + ".mojo"; | |
45 return base_url_.Resolve(lib); | |
46 } | |
47 } | |
48 | |
49 GURL MojoURLResolver::ApplyCustomMappings(const GURL& url) const { | |
50 GURL mapped_url(url); | |
51 for (;;) { | |
52 std::map<GURL, GURL>::const_iterator it = url_map_.find(mapped_url); | |
53 if (it == url_map_.end()) | |
54 break; | |
55 mapped_url = it->second; | |
56 } | |
57 return mapped_url; | |
58 } | |
59 | |
60 } // namespace shell | |
61 } // namespace mojo | |
OLD | NEW |