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 PARSE_LENIENT | |
Sam Kerner (Chrome)
2011/06/27 17:58:25
The idea of lenient vs. strict parsing made sense
| |
25 // option. If the |port_allowed| flag is not set, the port will be ignored | |
26 // regardless. | |
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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
101 // Error codes returned from Parse(). | 105 // Error codes returned from Parse(). |
102 enum ParseResult { | 106 enum ParseResult { |
103 PARSE_SUCCESS = 0, | 107 PARSE_SUCCESS = 0, |
104 PARSE_ERROR_MISSING_SCHEME_SEPARATOR, | 108 PARSE_ERROR_MISSING_SCHEME_SEPARATOR, |
105 PARSE_ERROR_INVALID_SCHEME, | 109 PARSE_ERROR_INVALID_SCHEME, |
106 PARSE_ERROR_WRONG_SCHEME_SEPARATOR, | 110 PARSE_ERROR_WRONG_SCHEME_SEPARATOR, |
107 PARSE_ERROR_EMPTY_HOST, | 111 PARSE_ERROR_EMPTY_HOST, |
108 PARSE_ERROR_INVALID_HOST_WILDCARD, | 112 PARSE_ERROR_INVALID_HOST_WILDCARD, |
109 PARSE_ERROR_EMPTY_PATH, | 113 PARSE_ERROR_EMPTY_PATH, |
110 PARSE_ERROR_HAS_COLON, // Only checked when strict checks are enabled. | 114 PARSE_ERROR_HAS_COLON, // Only checked when strict checks are enabled. |
115 PARSE_ERROR_INVALID_PORT, | |
111 NUM_PARSE_RESULTS | 116 NUM_PARSE_RESULTS |
112 }; | 117 }; |
113 | 118 |
114 // The <all_urls> string pattern. | 119 // The <all_urls> string pattern. |
115 static const char kAllUrlsPattern[]; | 120 static const char kAllUrlsPattern[]; |
116 | 121 |
117 // Construct an URLPattern with the given set of allowable schemes. See | 122 // Construct an URLPattern with the given set of allowable schemes. See |
118 // valid_schemes_ for more info. | 123 // valid_schemes_ for more info. |
119 explicit URLPattern(int valid_schemes); | 124 explicit URLPattern(int valid_schemes); |
120 | 125 |
(...skipping 27 matching lines...) Expand all Loading... | |
148 | 153 |
149 // Gets the path the pattern matches with the leading slash. This can have | 154 // Gets the path the pattern matches with the leading slash. This can have |
150 // embedded asterisks which are interpreted using glob rules. | 155 // embedded asterisks which are interpreted using glob rules. |
151 const std::string& path() const { return path_; } | 156 const std::string& path() const { return path_; } |
152 void SetPath(const std::string& path); | 157 void SetPath(const std::string& path); |
153 | 158 |
154 // Returns true if this pattern matches all urls. | 159 // Returns true if this pattern matches all urls. |
155 bool match_all_urls() const { return match_all_urls_; } | 160 bool match_all_urls() const { return match_all_urls_; } |
156 void set_match_all_urls(bool val) { match_all_urls_ = val; } | 161 void set_match_all_urls(bool val) { match_all_urls_ = val; } |
157 | 162 |
163 bool ignore_ports() const { return ignore_ports_; } | |
164 void set_ignore_ports(bool val) { ignore_ports_ = val; } | |
165 | |
158 // Initializes this instance by parsing the provided string. Returns | 166 // Initializes this instance by parsing the provided string. Returns |
159 // URLPattern::PARSE_SUCCESS on success, or an error code otherwise. On | 167 // URLPattern::PARSE_SUCCESS on success, or an error code otherwise. On |
160 // failure, this instance will have some intermediate values and is in an | 168 // failure, this instance will have some intermediate values and is in an |
161 // invalid state. Adding error checks to URLPattern::Parse() can cause | 169 // invalid state. Adding error checks to URLPattern::Parse() can cause |
162 // patterns in installed extensions to fail. If an installed extension | 170 // patterns in installed extensions to fail. If an installed extension |
163 // uses a pattern that was valid but fails a new error check, the | 171 // uses a pattern that was valid but fails a new error check, the |
164 // extension will fail to load when chrome is auto-updated. To avoid | 172 // extension will fail to load when chrome is auto-updated. To avoid |
165 // this, new parse checks are enabled only when |strictness| is | 173 // this, new parse checks are enabled only when |strictness| is |
166 // OPTION_STRICT. OPTION_STRICT should be used when loading in developer | 174 // OPTION_STRICT. OPTION_STRICT should be used when loading in developer |
167 // mode, or when an extension's patterns are controlled by chrome (such | 175 // mode, or when an extension's patterns are controlled by chrome (such |
(...skipping 19 matching lines...) Expand all 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 |test| matches our port. | |
206 bool MatchesPort(const std::string& test) 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 // Whether we silently ignore ports when testing for matches or overlaps. | |
279 bool ignore_ports_; | |
280 | |
281 // The port. URL patterns only support ports if they are parsed with the | |
282 // |PARSE_LENIENT| option. | |
283 std::string port_; | |
284 | |
263 // The path to match. This is everything after the host of the URL, or | 285 // 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. | 286 // everything after the scheme in the case of file:// URLs. |
265 std::string path_; | 287 std::string path_; |
266 | 288 |
267 // The path with "?" and "\" characters escaped for use with the | 289 // The path with "?" and "\" characters escaped for use with the |
268 // MatchPattern() function. | 290 // MatchPattern() function. |
269 std::string path_escaped_; | 291 std::string path_escaped_; |
270 }; | 292 }; |
271 | 293 |
272 typedef std::vector<URLPattern> URLPatternList; | 294 typedef std::vector<URLPattern> URLPatternList; |
273 | 295 |
274 #endif // CHROME_COMMON_EXTENSIONS_URL_PATTERN_H_ | 296 #endif // CHROME_COMMON_EXTENSIONS_URL_PATTERN_H_ |
OLD | NEW |