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 allowed when the pattern is parsed with the IGNORE_PORTS | |
25 // option. Unless the |ignore_ports| flag is set to false, the port will be | |
26 // ignored for |MatchesURL| and |OverlapsWith| regardless. | |
Sam Kerner (Chrome)
2011/06/28 18:12:39
Comment out of date?
Bernhard Bauer
2011/06/29 13:53:18
Done.
| |
23 // * The path can have embedded '*' characters which act as glob wildcards. | 27 // * 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 | 28 // * '<all_urls>' is a special pattern that matches any URL that contains a |
25 // valid scheme (as specified by valid_schemes_). | 29 // valid scheme (as specified by valid_schemes_). |
26 // * The '*' scheme pattern excludes file URLs. | 30 // * The '*' scheme pattern excludes file URLs. |
27 // | 31 // |
28 // Examples of valid patterns: | 32 // Examples of valid patterns: |
29 // - http://*/* | 33 // - http://*/* |
30 // - http://*/foo* | 34 // - http://*/foo* |
31 // - https://*.google.com/foo*bar | 35 // - https://*.google.com/foo*bar |
32 // - file://monkey* | 36 // - 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- | 91 // SCHEME_ALL will match every scheme, including chrome://, chrome- |
88 // extension://, about:, etc. Because this has lots of security | 92 // extension://, about:, etc. Because this has lots of security |
89 // implications, third-party extensions should never be able to get access | 93 // 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 | 94 // to URL patterns initialized this way. It should only be used for internal |
91 // Chrome code. | 95 // Chrome code. |
92 SCHEME_ALL = -1, | 96 SCHEME_ALL = -1, |
93 }; | 97 }; |
94 | 98 |
95 // Options for URLPattern::Parse(). | 99 // Options for URLPattern::Parse(). |
96 enum ParseOption { | 100 enum ParseOption { |
97 PARSE_LENIENT, | 101 ERROR_ON_PORTS, |
Sam Kerner (Chrome)
2011/06/28 18:12:39
Does IGNORE_PORTS mean http://foo.com:1234/* match
bauerb at google
2011/06/28 22:36:52
Yes, just like it did with PARSE_LENIENT.
| |
98 PARSE_STRICT | 102 IGNORE_PORTS, |
103 USE_PORTS, | |
99 }; | 104 }; |
100 | 105 |
101 // Error codes returned from Parse(). | 106 // Error codes returned from Parse(). |
102 enum ParseResult { | 107 enum ParseResult { |
103 PARSE_SUCCESS = 0, | 108 PARSE_SUCCESS = 0, |
104 PARSE_ERROR_MISSING_SCHEME_SEPARATOR, | 109 PARSE_ERROR_MISSING_SCHEME_SEPARATOR, |
105 PARSE_ERROR_INVALID_SCHEME, | 110 PARSE_ERROR_INVALID_SCHEME, |
106 PARSE_ERROR_WRONG_SCHEME_SEPARATOR, | 111 PARSE_ERROR_WRONG_SCHEME_SEPARATOR, |
107 PARSE_ERROR_EMPTY_HOST, | 112 PARSE_ERROR_EMPTY_HOST, |
108 PARSE_ERROR_INVALID_HOST_WILDCARD, | 113 PARSE_ERROR_INVALID_HOST_WILDCARD, |
109 PARSE_ERROR_EMPTY_PATH, | 114 PARSE_ERROR_EMPTY_PATH, |
110 PARSE_ERROR_HAS_COLON, // Only checked when strict checks are enabled. | 115 PARSE_ERROR_HAS_COLON, // Only checked when strict checks are enabled. |
Sam Kerner (Chrome)
2011/06/28 18:12:39
This comment needs an update: "strict checks" is n
Bernhard Bauer
2011/06/29 13:53:18
Done.
| |
116 PARSE_ERROR_INVALID_PORT, | |
111 NUM_PARSE_RESULTS | 117 NUM_PARSE_RESULTS |
112 }; | 118 }; |
113 | 119 |
114 // The <all_urls> string pattern. | 120 // The <all_urls> string pattern. |
115 static const char kAllUrlsPattern[]; | 121 static const char kAllUrlsPattern[]; |
116 | 122 |
117 // Construct an URLPattern with the given set of allowable schemes. See | 123 // Construct an URLPattern with the given set of allowable schemes. See |
118 // valid_schemes_ for more info. | 124 // valid_schemes_ for more info. |
119 explicit URLPattern(int valid_schemes); | 125 explicit URLPattern(int valid_schemes); |
120 | 126 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
187 // Returns true if |test| matches our scheme. | 193 // Returns true if |test| matches our scheme. |
188 bool MatchesScheme(const std::string& test) const; | 194 bool MatchesScheme(const std::string& test) const; |
189 | 195 |
190 // Returns true if |test| matches our host. | 196 // Returns true if |test| matches our host. |
191 bool MatchesHost(const std::string& test) const; | 197 bool MatchesHost(const std::string& test) const; |
192 bool MatchesHost(const GURL& test) const; | 198 bool MatchesHost(const GURL& test) const; |
193 | 199 |
194 // Returns true if |test| matches our path. | 200 // Returns true if |test| matches our path. |
195 bool MatchesPath(const std::string& test) const; | 201 bool MatchesPath(const std::string& test) const; |
196 | 202 |
203 // Returns true if |test| matches our port. | |
204 bool MatchesPort(const std::string& test) const; | |
205 | |
206 // Sets the port. Returns false if the port is invalid. | |
207 bool SetPort(const std::string& port); | |
208 const std::string& port() const { return port_; } | |
209 | |
197 // Returns a string representing this instance. | 210 // Returns a string representing this instance. |
198 std::string GetAsString() const; | 211 std::string GetAsString() const; |
199 | 212 |
200 // Determine whether there is a URL that would match this instance and another | 213 // Determine whether there is a URL that would match this instance and another |
201 // instance. This method is symmetrical: Calling other.OverlapsWith(this) | 214 // instance. This method is symmetrical: Calling other.OverlapsWith(this) |
202 // would result in the same answer. | 215 // would result in the same answer. |
203 bool OverlapsWith(const URLPattern& other) const; | 216 bool OverlapsWith(const URLPattern& other) const; |
204 | 217 |
205 // Convert this URLPattern into an equivalent set of URLPatterns that don't | 218 // 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 | 219 // 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. | 266 // The scheme for the pattern. |
254 std::string scheme_; | 267 std::string scheme_; |
255 | 268 |
256 // The host without any leading "*" components. | 269 // The host without any leading "*" components. |
257 std::string host_; | 270 std::string host_; |
258 | 271 |
259 // Whether we should match subdomains of the host. This is true if the first | 272 // Whether we should match subdomains of the host. This is true if the first |
260 // component of the pattern's host was "*". | 273 // component of the pattern's host was "*". |
261 bool match_subdomains_; | 274 bool match_subdomains_; |
262 | 275 |
276 // The port. URL patterns only support ports if they are parsed with the | |
277 // |USE_PORTS| option. | |
278 std::string port_; | |
279 | |
263 // The path to match. This is everything after the host of the URL, or | 280 // 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. | 281 // everything after the scheme in the case of file:// URLs. |
265 std::string path_; | 282 std::string path_; |
266 | 283 |
267 // The path with "?" and "\" characters escaped for use with the | 284 // The path with "?" and "\" characters escaped for use with the |
268 // MatchPattern() function. | 285 // MatchPattern() function. |
269 std::string path_escaped_; | 286 std::string path_escaped_; |
270 }; | 287 }; |
271 | 288 |
272 typedef std::vector<URLPattern> URLPatternList; | 289 typedef std::vector<URLPattern> URLPatternList; |
273 | 290 |
274 #endif // CHROME_COMMON_EXTENSIONS_URL_PATTERN_H_ | 291 #endif // CHROME_COMMON_EXTENSIONS_URL_PATTERN_H_ |
OLD | NEW |