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