OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #ifndef CHROME_COMMON_EXTENSIONS_URL_PATTERN_H_ | 4 #ifndef CHROME_COMMON_EXTENSIONS_URL_PATTERN_H_ |
5 #define CHROME_COMMON_EXTENSIONS_URL_PATTERN_H_ | 5 #define CHROME_COMMON_EXTENSIONS_URL_PATTERN_H_ |
6 #pragma once | 6 #pragma once |
7 | 7 |
8 #include <functional> | 8 #include <functional> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 class GURL; | 12 class GURL; |
13 | 13 |
14 // A pattern that can be used to match URLs. A URLPattern is a very restricted | 14 // A pattern that can be used to match URLs. A URLPattern is a very restricted |
15 // subset of URL syntax: | 15 // subset of URL syntax: |
16 // | 16 // |
17 // <url-pattern> := <scheme>://<host><path> | '<all_urls>' | 17 // <url-pattern> := <scheme>://<host><port><path> | '<all_urls>' |
18 // <scheme> := '*' | 'http' | 'https' | 'file' | 'ftp' | 'chrome' | 18 // <scheme> := '*' | 'http' | 'https' | 'file' | 'ftp' | 'chrome' |
19 // <host> := '*' | '*.' <anychar except '/' and '*'>+ | 19 // <host> := '*' | '*.' <anychar except '/' and '*'>+ |
20 // <port> := [':' ('*' | <port number between 0 and 65535>)] | |
20 // <path> := '/' <any chars> | 21 // <path> := '/' <any chars> |
21 // | 22 // |
22 // * Host is not used when the scheme is 'file'. | 23 // * Host is not used when the scheme is 'file'. |
24 // * The port is only used if the pattern is parsed with the USE_PORTS option. | |
25 // If the patterns is parsed with the ERROR_ON_PORTS option, the port is not | |
26 // allowed, and the resulting pattern matches any port. If it is parsed with | |
27 // the IGNORE_PORTS option, the port (including colon) is kept as part of the | |
28 // host, which makes the pattern effectively never match any URL. | |
Sam Kerner (Chrome)
2011/06/30 02:36:52
Please point out that this seemingly silly behavio
| |
23 // * The path can have embedded '*' characters which act as glob wildcards. | 29 // * The path can have embedded '*' characters which act as glob wildcards. |
24 // * '<all_urls>' is a special pattern that matches any URL that contains a | 30 // * '<all_urls>' is a special pattern that matches any URL that contains a |
25 // valid scheme (as specified by valid_schemes_). | 31 // valid scheme (as specified by valid_schemes_). |
26 // * The '*' scheme pattern excludes file URLs. | 32 // * The '*' scheme pattern excludes file URLs. |
27 // | 33 // |
28 // Examples of valid patterns: | 34 // Examples of valid patterns: |
29 // - http://*/* | 35 // - http://*/* |
30 // - http://*/foo* | 36 // - http://*/foo* |
31 // - https://*.google.com/foo*bar | 37 // - https://*.google.com/foo*bar |
32 // - file://monkey* | 38 // - file://monkey* |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
87 // SCHEME_ALL will match every scheme, including chrome://, chrome- | 93 // SCHEME_ALL will match every scheme, including chrome://, chrome- |
88 // extension://, about:, etc. Because this has lots of security | 94 // extension://, about:, etc. Because this has lots of security |
89 // implications, third-party extensions should never be able to get access | 95 // implications, third-party extensions should never be able to get access |
90 // to URL patterns initialized this way. It should only be used for internal | 96 // to URL patterns initialized this way. It should only be used for internal |
91 // Chrome code. | 97 // Chrome code. |
92 SCHEME_ALL = -1, | 98 SCHEME_ALL = -1, |
93 }; | 99 }; |
94 | 100 |
95 // Options for URLPattern::Parse(). | 101 // Options for URLPattern::Parse(). |
96 enum ParseOption { | 102 enum ParseOption { |
97 PARSE_LENIENT, | 103 ERROR_ON_PORTS, |
98 PARSE_STRICT | 104 IGNORE_PORTS, |
105 USE_PORTS, | |
99 }; | 106 }; |
100 | 107 |
101 // Error codes returned from Parse(). | 108 // Error codes returned from Parse(). |
102 enum ParseResult { | 109 enum ParseResult { |
103 PARSE_SUCCESS = 0, | 110 PARSE_SUCCESS = 0, |
104 PARSE_ERROR_MISSING_SCHEME_SEPARATOR, | 111 PARSE_ERROR_MISSING_SCHEME_SEPARATOR, |
105 PARSE_ERROR_INVALID_SCHEME, | 112 PARSE_ERROR_INVALID_SCHEME, |
106 PARSE_ERROR_WRONG_SCHEME_SEPARATOR, | 113 PARSE_ERROR_WRONG_SCHEME_SEPARATOR, |
107 PARSE_ERROR_EMPTY_HOST, | 114 PARSE_ERROR_EMPTY_HOST, |
108 PARSE_ERROR_INVALID_HOST_WILDCARD, | 115 PARSE_ERROR_INVALID_HOST_WILDCARD, |
109 PARSE_ERROR_EMPTY_PATH, | 116 PARSE_ERROR_EMPTY_PATH, |
110 PARSE_ERROR_HAS_COLON, // Only checked when strict checks are enabled. | 117 PARSE_ERROR_HAS_COLON, // Only checked when parsing with ERROR_ON_PORTS. |
118 PARSE_ERROR_INVALID_PORT, // Only checked when parsing with USE_PORTS. | |
111 NUM_PARSE_RESULTS | 119 NUM_PARSE_RESULTS |
112 }; | 120 }; |
113 | 121 |
114 // The <all_urls> string pattern. | 122 // The <all_urls> string pattern. |
115 static const char kAllUrlsPattern[]; | 123 static const char kAllUrlsPattern[]; |
116 | 124 |
117 // Construct an URLPattern with the given set of allowable schemes. See | 125 // Construct an URLPattern with the given set of allowable schemes. See |
118 // valid_schemes_ for more info. | 126 // valid_schemes_ for more info. |
119 explicit URLPattern(int valid_schemes); | 127 explicit URLPattern(int valid_schemes); |
120 | 128 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
187 // Returns true if |test| matches our scheme. | 195 // Returns true if |test| matches our scheme. |
188 bool MatchesScheme(const std::string& test) const; | 196 bool MatchesScheme(const std::string& test) const; |
189 | 197 |
190 // Returns true if |test| matches our host. | 198 // Returns true if |test| matches our host. |
191 bool MatchesHost(const std::string& test) const; | 199 bool MatchesHost(const std::string& test) const; |
192 bool MatchesHost(const GURL& test) const; | 200 bool MatchesHost(const GURL& test) const; |
193 | 201 |
194 // Returns true if |test| matches our path. | 202 // Returns true if |test| matches our path. |
195 bool MatchesPath(const std::string& test) const; | 203 bool MatchesPath(const std::string& test) const; |
196 | 204 |
205 // Returns true if |port| matches our port. | |
206 bool MatchesPort(int port) const; | |
207 | |
208 // Sets the port. Returns false if the port is invalid. | |
209 bool SetPort(const std::string& port); | |
210 const std::string& port() const { return port_; } | |
211 | |
197 // Returns a string representing this instance. | 212 // Returns a string representing this instance. |
198 std::string GetAsString() const; | 213 std::string GetAsString() const; |
199 | 214 |
200 // Determine whether there is a URL that would match this instance and another | 215 // Determine whether there is a URL that would match this instance and another |
201 // instance. This method is symmetrical: Calling other.OverlapsWith(this) | 216 // instance. This method is symmetrical: Calling other.OverlapsWith(this) |
202 // would result in the same answer. | 217 // would result in the same answer. |
203 bool OverlapsWith(const URLPattern& other) const; | 218 bool OverlapsWith(const URLPattern& other) const; |
204 | 219 |
205 // Convert this URLPattern into an equivalent set of URLPatterns that don't | 220 // Convert this URLPattern into an equivalent set of URLPatterns that don't |
206 // use a wildcard in the scheme component. If this URLPattern doesn't use a | 221 // use a wildcard in the scheme component. If this URLPattern doesn't use a |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
253 // The scheme for the pattern. | 268 // The scheme for the pattern. |
254 std::string scheme_; | 269 std::string scheme_; |
255 | 270 |
256 // The host without any leading "*" components. | 271 // The host without any leading "*" components. |
257 std::string host_; | 272 std::string host_; |
258 | 273 |
259 // Whether we should match subdomains of the host. This is true if the first | 274 // Whether we should match subdomains of the host. This is true if the first |
260 // component of the pattern's host was "*". | 275 // component of the pattern's host was "*". |
261 bool match_subdomains_; | 276 bool match_subdomains_; |
262 | 277 |
278 // The port. URL patterns only support specific ports if they are parsed with | |
279 // the |USE_PORTS| option. | |
280 std::string port_; | |
281 | |
263 // The path to match. This is everything after the host of the URL, or | 282 // The path to match. This is everything after the host of the URL, or |
264 // everything after the scheme in the case of file:// URLs. | 283 // everything after the scheme in the case of file:// URLs. |
265 std::string path_; | 284 std::string path_; |
266 | 285 |
267 // The path with "?" and "\" characters escaped for use with the | 286 // The path with "?" and "\" characters escaped for use with the |
268 // MatchPattern() function. | 287 // MatchPattern() function. |
269 std::string path_escaped_; | 288 std::string path_escaped_; |
270 }; | 289 }; |
271 | 290 |
272 typedef std::vector<URLPattern> URLPatternList; | 291 typedef std::vector<URLPattern> URLPatternList; |
273 | 292 |
274 #endif // CHROME_COMMON_EXTENSIONS_URL_PATTERN_H_ | 293 #endif // CHROME_COMMON_EXTENSIONS_URL_PATTERN_H_ |
OLD | NEW |