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.
5 #ifndef MOJO_RUNNER_URL_RESOLVER_H_
6 #define MOJO_RUNNER_URL_RESOLVER_H_
11 #include "base/basictypes.h"
12 #include "base/command_line.h"
13 #include "base/files/file_path.h"
19 // This class supports the mapping of URLs to other URLs.
20 // It's commonly used with mojo: URL, to provide a physical location 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,
23 // but that resolution can be customized via the AddURLMapping and
24 // AddOriginMapping methods.
30 // Used for the return value of GetOriginMappings().
31 struct OriginMapping
{
32 OriginMapping(const std::string
& origin
, const std::string
& base_url
)
33 : origin(origin
), base_url(base_url
) {}
39 // Returns a list of origin mappings based on command line args.
40 // The switch --map-origin can be specified multiple times. Each occurrence
41 // has the format of --map-origin={origin}={base_url}
43 // --map-origin=http://domokit.org=file:///source/out
44 static std::vector
<OriginMapping
> GetOriginMappings(
45 const base::CommandLine::StringVector
& argv
);
47 // Add a custom mapping for a particular URL. If |mapped_url| is
48 // itself a mojo URL, then normal resolution rules apply.
49 void AddURLMapping(const GURL
& url
, const GURL
& mapped_url
);
51 // Add a custom mapping for all URLs rooted at |origin|. An origin is a
52 // combination of URL scheme, host and port.
53 void AddOriginMapping(const GURL
& origin
, const GURL
& base_url
);
55 // Applies all custom mappings for |url|, returning the last non-mapped url.
56 // For example, if 'a' maps to 'b' and 'b' maps to 'c', calling this with 'a'
58 GURL
ApplyMappings(const GURL
& url
) const;
60 // If specified, then "mojo:" URLs will be resolved relative to this
61 // URL. That is, the portion after the colon will be appended to
62 // |mojo_base_url| with .mojo appended.
63 void SetMojoBaseURL(const GURL
& mojo_base_url
);
65 // Resolve the given "mojo:" URL to the URL that should be used to fetch the
66 // code for the corresponding Mojo App.
67 GURL
ResolveMojoURL(const GURL
& mojo_url
) const;
70 using GURLToGURLMap
= std::map
<GURL
, GURL
>;
71 GURLToGURLMap url_map_
;
72 GURLToGURLMap origin_map_
;
75 DISALLOW_COPY_AND_ASSIGN(URLResolver
);
81 #endif // MOJO_RUNNER_URL_RESOLVER_H_