Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(309)

Side by Side Diff: shell/mojo_url_resolver.cc

Issue 868963002: Simplify resolution of mojo: URLs. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Comments Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « shell/mojo_url_resolver.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "shell/mojo_url_resolver.h" 5 #include "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"
11 #include "shell/filename_util.h" 10 #include "shell/filename_util.h"
12 #include "url/url_util.h" 11 #include "url/url_util.h"
13 12
14 namespace mojo { 13 namespace mojo {
15 namespace shell { 14 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 15
30 MojoURLResolver::MojoURLResolver() { 16 MojoURLResolver::MojoURLResolver() {
31 // Needed to treat first component of mojo URLs as host, not path. 17 // Needed to treat first component of mojo URLs as host, not path.
32 url::AddStandardScheme("mojo"); 18 url::AddStandardScheme("mojo");
33 // By default assume that the local apps reside alongside the shell.
34 base::FilePath path;
35 PathService::Get(base::DIR_MODULE, &path);
36 local_apps_url_ = AddTrailingSlashIfNeeded(FilePathToFileURL(path));
37 } 19 }
38 20
39 MojoURLResolver::~MojoURLResolver() { 21 MojoURLResolver::~MojoURLResolver() {
40 } 22 }
41 23
42 void MojoURLResolver::SetBaseURL(const GURL& base_url) { 24 void MojoURLResolver::SetBaseURL(const GURL& base_url) {
43 DCHECK(base_url.is_valid()); 25 DCHECK(base_url.is_valid());
44 // Force a trailing slash on the base_url to simplify resolving 26 // Force a trailing slash on the base_url to simplify resolving
45 // relative files and URLs below. 27 // relative files and URLs below.
46 base_url_ = AddTrailingSlashIfNeeded(base_url); 28 base_url_ = AddTrailingSlashIfNeeded(base_url);
47 } 29 }
48 30
49 void MojoURLResolver::SetLocalAppsPath(const base::FilePath& local_apps_path) {
50 local_apps_url_ =
51 AddTrailingSlashIfNeeded(FilePathToFileURL(local_apps_path));
52 DCHECK(local_apps_url_.is_valid());
53 }
54
55 void MojoURLResolver::AddCustomMapping(const GURL& mojo_url, 31 void MojoURLResolver::AddCustomMapping(const GURL& mojo_url,
56 const GURL& resolved_url) { 32 const GURL& resolved_url) {
57 url_map_[mojo_url] = resolved_url; 33 url_map_[mojo_url] = resolved_url;
58 } 34 }
59 35
60 void MojoURLResolver::AddLocalFileMapping(const GURL& mojo_url) {
61 local_file_set_.insert(mojo_url);
62 }
63
64 GURL MojoURLResolver::Resolve(const GURL& mojo_url) const { 36 GURL MojoURLResolver::Resolve(const GURL& mojo_url) const {
65 const GURL mapped_url(ApplyCustomMappings(mojo_url)); 37 const GURL mapped_url(ApplyCustomMappings(mojo_url));
66 38
67 // Continue resolving if the mapped url is a mojo: url. 39 if (mapped_url.scheme() != "mojo") {
68 if (mapped_url.scheme() != "mojo") 40 // The mapping has produced some sort of non-mojo: URL - file:, http:, etc.
69 return mapped_url; 41 return mapped_url;
70 42 } else {
71 std::string lib = mapped_url.host() + ".mojo"; 43 // It's still a mojo: URL, use the default mapping scheme.
72 44 std::string lib = mapped_url.host() + ".mojo";
73 if (!base_url_.is_valid() || 45 return base_url_.Resolve(lib);
74 local_file_set_.find(mapped_url) != local_file_set_.end()) {
75 // Resolve to a local file URL.
76 return local_apps_url_.Resolve(lib);
77 } 46 }
78
79 // Otherwise, resolve to an URL relative to base_url_.
80 return base_url_.Resolve(lib);
81 } 47 }
82 48
83 GURL MojoURLResolver::ApplyCustomMappings(const GURL& url) const { 49 GURL MojoURLResolver::ApplyCustomMappings(const GURL& url) const {
84 GURL mapped_url(url); 50 GURL mapped_url(url);
85 for (;;) { 51 for (;;) {
86 std::map<GURL, GURL>::const_iterator it = url_map_.find(mapped_url); 52 std::map<GURL, GURL>::const_iterator it = url_map_.find(mapped_url);
87 if (it == url_map_.end()) 53 if (it == url_map_.end())
88 break; 54 break;
89 mapped_url = it->second; 55 mapped_url = it->second;
90 } 56 }
91 return mapped_url; 57 return mapped_url;
92 } 58 }
93 59
94 } // namespace shell 60 } // namespace shell
95 } // namespace mojo 61 } // namespace mojo
OLDNEW
« no previous file with comments | « shell/mojo_url_resolver.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698