OLD | NEW |
---|---|
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 "mojo/shell/mojo_url_resolver.h" | 5 #include "mojo/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" | 10 #include "base/path_service.h" |
(...skipping 20 matching lines...) Expand all Loading... | |
31 } // namespace | 31 } // namespace |
32 | 32 |
33 MojoURLResolver::MojoURLResolver() { | 33 MojoURLResolver::MojoURLResolver() { |
34 // Needed to treat first component of mojo URLs as host, not path. | 34 // Needed to treat first component of mojo URLs as host, not path. |
35 url::AddStandardScheme("mojo"); | 35 url::AddStandardScheme("mojo"); |
36 } | 36 } |
37 | 37 |
38 MojoURLResolver::~MojoURLResolver() { | 38 MojoURLResolver::~MojoURLResolver() { |
39 } | 39 } |
40 | 40 |
41 void MojoURLResolver::SetOrigin(const std::string& origin) { | |
viettrungluu
2014/07/15 22:44:53
I wonder if "origin" is not the appropriate term.
tim (not reviewing)
2014/07/19 02:11:37
Done.
| |
42 DCHECK(!origin.empty()); | |
43 // Force a trailing slash on the origin to simplify resolving | |
44 // relative files and URLs below. | |
45 if (*origin.rbegin() != '/') | |
46 origin_ = GURL(origin + '/'); | |
47 else | |
48 origin_ = GURL(origin); | |
49 } | |
50 | |
41 void MojoURLResolver::AddCustomMapping(const GURL& mojo_url, | 51 void MojoURLResolver::AddCustomMapping(const GURL& mojo_url, |
42 const GURL& resolved_url) { | 52 const GURL& resolved_url) { |
43 url_map_[mojo_url] = resolved_url; | 53 url_map_[mojo_url] = resolved_url; |
44 } | 54 } |
45 | 55 |
46 void MojoURLResolver::AddLocalFileMapping(const GURL& mojo_url) { | 56 void MojoURLResolver::AddLocalFileMapping(const GURL& mojo_url) { |
47 local_file_set_.insert(mojo_url); | 57 local_file_set_.insert(mojo_url); |
48 } | 58 } |
49 | 59 |
50 GURL MojoURLResolver::Resolve(const GURL& mojo_url) const { | 60 GURL MojoURLResolver::Resolve(const GURL& mojo_url) const { |
51 std::map<GURL, GURL>::const_iterator it = url_map_.find(mojo_url); | 61 std::map<GURL, GURL>::const_iterator it = url_map_.find(mojo_url); |
52 if (it != url_map_.end()) | 62 if (it != url_map_.end()) |
53 return it->second; | 63 return it->second; |
54 | 64 |
55 std::string lib = MakeSharedLibraryName(mojo_url.host()); | 65 std::string lib = MakeSharedLibraryName(mojo_url.host()); |
56 | 66 |
57 if (local_file_set_.find(mojo_url) != local_file_set_.end()) { | 67 if (local_file_set_.find(mojo_url) != local_file_set_.end()) { |
58 // Resolve to a local file URL. | 68 // Resolve to a local file URL. |
59 base::FilePath path; | 69 base::FilePath path; |
60 PathService::Get(base::DIR_MODULE, &path); | 70 PathService::Get(base::DIR_MODULE, &path); |
61 path = path.Append(base::FilePath::FromUTF8Unsafe(lib)); | 71 path = path.Append(base::FilePath::FromUTF8Unsafe(lib)); |
62 return net::FilePathToFileURL(path); | 72 return net::FilePathToFileURL(path); |
63 } | 73 } |
64 | 74 |
65 // Otherwise, resolve to an URL relative to origin_. | 75 // Otherwise, resolve to an URL relative to origin_. |
66 return GURL(origin_ + "/" + lib); | 76 return origin_.Resolve(lib); |
67 } | 77 } |
68 | 78 |
69 } // namespace shell | 79 } // namespace shell |
70 } // namespace mojo | 80 } // namespace mojo |
OLD | NEW |