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 #ifndef MOJO_RUNNER_URL_RESOLVER_H_ | 5 #ifndef MOJO_RUNNER_URL_RESOLVER_H_ |
6 #define MOJO_RUNNER_URL_RESOLVER_H_ | 6 #define MOJO_RUNNER_URL_RESOLVER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
14 #include "url/gurl.h" | 14 #include "url/gurl.h" |
15 | 15 |
16 namespace mojo { | 16 namespace mojo { |
17 namespace runner { | 17 namespace runner { |
18 | 18 |
19 // This class supports the mapping of URLs to other URLs. | 19 // This class supports the mapping of URLs to other URLs. |
20 // It's commonly used with mojo: URL, to provide a physical location (i.e. | 20 // It's commonly used with mojo: URL, to provide a physical location URL |
21 // file: or https:) but works with any URL. | 21 // (i.e. file: or https:) but works with any URL. |
22 // By default, "mojo:" URLs resolve to a file location, with ".mojo" appended, | 22 // By default, "mojo:" URLs resolve to a file location, with ".mojo" appended, |
23 // but that resolution can be customized via the AddCustomMapping method. | 23 // but that resolution can be customized via the AddURLMapping and |
| 24 // AddOriginMapping methods. |
24 class URLResolver { | 25 class URLResolver { |
25 public: | 26 public: |
26 URLResolver(); | 27 URLResolver(); |
27 ~URLResolver(); | 28 ~URLResolver(); |
28 | 29 |
29 // Used for the return value of GetOriginMappings(). | 30 // Used for the return value of GetOriginMappings(). |
30 struct OriginMapping { | 31 struct OriginMapping { |
31 OriginMapping(const std::string& origin, const std::string& base_url) | 32 OriginMapping(const std::string& origin, const std::string& base_url) |
32 : origin(origin), base_url(base_url) {} | 33 : origin(origin), base_url(base_url) {} |
33 | 34 |
34 std::string origin; | 35 std::string origin; |
35 std::string base_url; | 36 std::string base_url; |
36 }; | 37 }; |
37 | 38 |
38 // Returns a list of origin mappings based on command line args. | 39 // Returns a list of origin mappings based on command line args. |
39 // The switch --map-origin can be specified multiple times. Each occurance | 40 // The switch --map-origin can be specified multiple times. Each occurrence |
40 // has the format of --map-origin={origin}={base_url} | 41 // has the format of --map-origin={origin}={base_url} |
41 // For example: | 42 // For example: |
42 // --map-origin=http://domokit.org=file:///source/out | 43 // --map-origin=http://domokit.org=file:///source/out |
43 static std::vector<OriginMapping> GetOriginMappings( | 44 static std::vector<OriginMapping> GetOriginMappings( |
44 const base::CommandLine::StringVector& argv); | 45 const base::CommandLine::StringVector& argv); |
45 | 46 |
46 // Add a custom mapping for a particular URL. If |mapped_url| is | 47 // Add a custom mapping for a particular URL. If |mapped_url| is |
47 // itself a mojo url normal resolution rules apply. | 48 // itself a mojo URL, then normal resolution rules apply. |
48 void AddURLMapping(const GURL& url, const GURL& mapped_url); | 49 void AddURLMapping(const GURL& url, const GURL& mapped_url); |
49 | 50 |
50 // Add a custom mapping for all urls rooted at |origin|. | 51 // Add a custom mapping for all URLs rooted at |origin|. An origin is a |
| 52 // combination of URL scheme, host and port. |
51 void AddOriginMapping(const GURL& origin, const GURL& base_url); | 53 void AddOriginMapping(const GURL& origin, const GURL& base_url); |
52 | 54 |
53 // Applies all custom mappings for |url|, returning the last non-mapped url. | 55 // Applies all custom mappings for |url|, returning the last non-mapped url. |
54 // For example, if 'a' maps to 'b' and 'b' maps to 'c' calling this with 'a' | 56 // For example, if 'a' maps to 'b' and 'b' maps to 'c', calling this with 'a' |
55 // returns 'c'. | 57 // returns 'c'. |
56 GURL ApplyMappings(const GURL& url) const; | 58 GURL ApplyMappings(const GURL& url) const; |
57 | 59 |
58 // If specified, then "mojo:" URLs will be resolved relative to this | 60 // If specified, then "mojo:" URLs will be resolved relative to this |
59 // URL. That is, the portion after the colon will be appeneded to | 61 // URL. That is, the portion after the colon will be appended to |
60 // |mojo_base_url| with .mojo appended. | 62 // |mojo_base_url| with .mojo appended. |
61 void SetMojoBaseURL(const GURL& mojo_base_url); | 63 void SetMojoBaseURL(const GURL& mojo_base_url); |
62 | 64 |
63 // Resolve the given "mojo:" URL to the URL that should be used to fetch the | 65 // Resolve the given "mojo:" URL to the URL that should be used to fetch the |
64 // code for the corresponding Mojo App. | 66 // code for the corresponding Mojo App. |
65 GURL ResolveMojoURL(const GURL& mojo_url) const; | 67 GURL ResolveMojoURL(const GURL& mojo_url) const; |
66 | 68 |
67 private: | 69 private: |
68 using GURLToGURLMap = std::map<GURL, GURL>; | 70 using GURLToGURLMap = std::map<GURL, GURL>; |
69 GURLToGURLMap url_map_; | 71 GURLToGURLMap url_map_; |
70 GURLToGURLMap origin_map_; | 72 GURLToGURLMap origin_map_; |
71 GURL mojo_base_url_; | 73 GURL mojo_base_url_; |
72 | 74 |
73 DISALLOW_COPY_AND_ASSIGN(URLResolver); | 75 DISALLOW_COPY_AND_ASSIGN(URLResolver); |
74 }; | 76 }; |
75 | 77 |
76 } // namespace runner | 78 } // namespace runner |
77 } // namespace mojo | 79 } // namespace mojo |
78 | 80 |
79 #endif // MOJO_RUNNER_URL_RESOLVER_H_ | 81 #endif // MOJO_RUNNER_URL_RESOLVER_H_ |
OLD | NEW |