Mailbox support for texture layers.
[chromium-blink-merge.git] / extensions / common / url_pattern.h
blob42188a5038bae55cc9872fc1a5b5729720983fa8
1 // Copyright (c) 2012 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 #ifndef EXTENSIONS_COMMON_URL_PATTERN_H_
5 #define EXTENSIONS_COMMON_URL_PATTERN_H_
7 #include <functional>
8 #include <string>
9 #include <vector>
11 class GURL;
13 // A pattern that can be used to match URLs. A URLPattern is a very restricted
14 // subset of URL syntax:
16 // <url-pattern> := <scheme>://<host><port><path> | '<all_urls>'
17 // <scheme> := '*' | 'http' | 'https' | 'file' | 'ftp' | 'chrome' |
18 // 'chrome-extension' | 'filesystem'
19 // <host> := '*' | '*.' <anychar except '/' and '*'>+
20 // <port> := [':' ('*' | <port number between 0 and 65535>)]
21 // <path> := '/' <any chars>
23 // * Host is not used when the scheme is 'file'.
24 // * The path can have embedded '*' characters which act as glob wildcards.
25 // * '<all_urls>' is a special pattern that matches any URL that contains a
26 // valid scheme (as specified by valid_schemes_).
27 // * The '*' scheme pattern excludes file URLs.
29 // Examples of valid patterns:
30 // - http://*/*
31 // - http://*/foo*
32 // - https://*.google.com/foo*bar
33 // - file://monkey*
34 // - http://127.0.0.1/*
36 // Examples of invalid patterns:
37 // - http://* -- path not specified
38 // - http://*foo/bar -- * not allowed as substring of host component
39 // - http://foo.*.bar/baz -- * must be first component
40 // - http:/bar -- scheme separator not found
41 // - foo://* -- invalid scheme
42 // - chrome:// -- we don't support chrome internal URLs
43 class URLPattern {
44 public:
45 // A collection of scheme bitmasks for use with valid_schemes.
46 enum SchemeMasks {
47 SCHEME_NONE = 0,
48 SCHEME_HTTP = 1 << 0,
49 SCHEME_HTTPS = 1 << 1,
50 SCHEME_FILE = 1 << 2,
51 SCHEME_FTP = 1 << 3,
52 SCHEME_CHROMEUI = 1 << 4,
53 SCHEME_EXTENSION = 1 << 5,
54 SCHEME_FILESYSTEM = 1 << 6,
56 // IMPORTANT!
57 // SCHEME_ALL will match every scheme, including chrome://, chrome-
58 // extension://, about:, etc. Because this has lots of security
59 // implications, third-party extensions should usually not be able to get
60 // access to URL patterns initialized this way. If there is a reason
61 // for violating this general rule, document why this it safe.
62 SCHEME_ALL = -1,
65 // Error codes returned from Parse().
66 enum ParseResult {
67 PARSE_SUCCESS = 0,
68 PARSE_ERROR_MISSING_SCHEME_SEPARATOR,
69 PARSE_ERROR_INVALID_SCHEME,
70 PARSE_ERROR_WRONG_SCHEME_SEPARATOR,
71 PARSE_ERROR_EMPTY_HOST,
72 PARSE_ERROR_INVALID_HOST_WILDCARD,
73 PARSE_ERROR_EMPTY_PATH,
74 PARSE_ERROR_INVALID_PORT,
75 NUM_PARSE_RESULTS
78 // The <all_urls> string pattern.
79 static const char kAllUrlsPattern[];
81 explicit URLPattern(int valid_schemes);
83 // Convenience to construct a URLPattern from a string. If the string is not
84 // known ahead of time, use Parse() instead, which returns success or failure.
85 URLPattern(int valid_schemes, const std::string& pattern);
87 URLPattern();
88 ~URLPattern();
90 bool operator<(const URLPattern& other) const;
91 bool operator==(const URLPattern& other) const;
93 // Initializes this instance by parsing the provided string. Returns
94 // URLPattern::PARSE_SUCCESS on success, or an error code otherwise. On
95 // failure, this instance will have some intermediate values and is in an
96 // invalid state.
97 ParseResult Parse(const std::string& pattern_str);
99 // Gets the bitmask of valid schemes.
100 int valid_schemes() const { return valid_schemes_; }
101 void SetValidSchemes(int valid_schemes);
103 // Gets the host the pattern matches. This can be an empty string if the
104 // pattern matches all hosts (the input was <scheme>://*/<whatever>).
105 const std::string& host() const { return host_; }
106 void SetHost(const std::string& host);
108 // Gets whether to match subdomains of host().
109 bool match_subdomains() const { return match_subdomains_; }
110 void SetMatchSubdomains(bool val);
112 // Gets the path the pattern matches with the leading slash. This can have
113 // embedded asterisks which are interpreted using glob rules.
114 const std::string& path() const { return path_; }
115 void SetPath(const std::string& path);
117 // Returns true if this pattern matches all urls.
118 bool match_all_urls() const { return match_all_urls_; }
119 void SetMatchAllURLs(bool val);
121 // Sets the scheme for pattern matches. This can be a single '*' if the
122 // pattern matches all valid schemes (as defined by the valid_schemes_
123 // property). Returns false on failure (if the scheme is not valid).
124 bool SetScheme(const std::string& scheme);
125 // Note: You should use MatchesScheme() instead of this getter unless you
126 // absolutely need the exact scheme. This is exposed for testing.
127 const std::string& scheme() const { return scheme_; }
129 // Returns true if the specified scheme can be used in this URL pattern, and
130 // false otherwise. Uses valid_schemes_ to determine validity.
131 bool IsValidScheme(const std::string& scheme) const;
133 // Returns true if this instance matches the specified URL.
134 bool MatchesURL(const GURL& test) const;
136 // Returns true if this instance matches the specified security origin.
137 bool MatchesSecurityOrigin(const GURL& test) const;
139 // Returns true if |test| matches our scheme.
140 // Note that if test is "filesystem", this may fail whereas MatchesURL
141 // may succeed. MatchesURL is smart enough to look at the inner_url instead
142 // of the outer "filesystem:" part.
143 bool MatchesScheme(const std::string& test) const;
145 // Returns true if |test| matches our host.
146 bool MatchesHost(const std::string& test) const;
147 bool MatchesHost(const GURL& test) const;
149 // Returns true if |test| matches our path.
150 bool MatchesPath(const std::string& test) const;
152 // Returns true if |port| matches our port.
153 bool MatchesPort(int port) const;
155 // Sets the port. Returns false if the port is invalid.
156 bool SetPort(const std::string& port);
157 const std::string& port() const { return port_; }
159 // Returns a string representing this instance.
160 const std::string& GetAsString() const;
162 // Determine whether there is a URL that would match this instance and another
163 // instance. This method is symmetrical: Calling other.OverlapsWith(this)
164 // would result in the same answer.
165 bool OverlapsWith(const URLPattern& other) const;
167 // Convert this URLPattern into an equivalent set of URLPatterns that don't
168 // use a wildcard in the scheme component. If this URLPattern doesn't use a
169 // wildcard scheme, then the returned set will contain one element that is
170 // equivalent to this instance.
171 std::vector<URLPattern> ConvertToExplicitSchemes() const;
173 static bool EffectiveHostCompare(const URLPattern& a, const URLPattern& b) {
174 if (a.match_all_urls_ && b.match_all_urls_)
175 return false;
176 return a.host_.compare(b.host_) < 0;
179 // Used for origin comparisons in a std::set.
180 class EffectiveHostCompareFunctor {
181 public:
182 bool operator()(const URLPattern& a, const URLPattern& b) const {
183 return EffectiveHostCompare(a, b);
187 // Get an error string for a ParseResult.
188 static const char* GetParseResultString(URLPattern::ParseResult parse_result);
190 private:
191 // Returns true if any of the |schemes| items matches our scheme.
192 bool MatchesAnyScheme(const std::vector<std::string>& schemes) const;
194 bool MatchesSecurityOriginHelper(const GURL& test) const;
196 // If the URLPattern contains a wildcard scheme, returns a list of
197 // equivalent literal schemes, otherwise returns the current scheme.
198 std::vector<std::string> GetExplicitSchemes() const;
200 // A bitmask containing the schemes which are considered valid for this
201 // pattern. Parse() uses this to decide whether a pattern contains a valid
202 // scheme. MatchesScheme uses this to decide whether a wildcard scheme_
203 // matches a given test scheme.
204 int valid_schemes_;
206 // True if this is a special-case "<all_urls>" pattern.
207 bool match_all_urls_;
209 // The scheme for the pattern.
210 std::string scheme_;
212 // The host without any leading "*" components.
213 std::string host_;
215 // Whether we should match subdomains of the host. This is true if the first
216 // component of the pattern's host was "*".
217 bool match_subdomains_;
219 // The port.
220 std::string port_;
222 // The path to match. This is everything after the host of the URL, or
223 // everything after the scheme in the case of file:// URLs.
224 std::string path_;
226 // The path with "?" and "\" characters escaped for use with the
227 // MatchPattern() function.
228 std::string path_escaped_;
230 // A string representing this URLPattern.
231 mutable std::string spec_;
234 typedef std::vector<URLPattern> URLPatternList;
236 #endif // EXTENSIONS_COMMON_URL_PATTERN_H_