OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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><port><path> | '<all_urls>' | 17 // <url-pattern> := [filesystem:]<scheme>://<host><port><path> | '<all_urls>' | |
18 // <nonstandard-scheme>:<any chars> | |
18 // <scheme> := '*' | 'http' | 'https' | 'file' | 'ftp' | 'chrome' | | 19 // <scheme> := '*' | 'http' | 'https' | 'file' | 'ftp' | 'chrome' | |
19 // 'chrome-extension' | 'filesystem' | 20 // 'chrome-extension' |
20 // <host> := '*' | '*.' <anychar except '/' and '*'>+ | 21 // <host> := '*' | '*.' <anychar except '/' and '*'>+ |
21 // <port> := [':' ('*' | <port number between 0 and 65535>)] | 22 // <port> := [':' ('*' | <port number between 0 and 65535>)] |
22 // <path> := '/' <any chars> | 23 // <path> := '/' <any chars> |
24 // <nonstandard-scheme> := <any string except for "//" or those in scheme> | |
23 // | 25 // |
24 // * Host is not used when the scheme is 'file'. | 26 // * Host is not used when the scheme is 'file'. |
25 // * The path can have embedded '*' characters which act as glob wildcards. | 27 // * The path can have embedded '*' characters which act as glob wildcards. |
26 // * '<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 |
27 // valid scheme (as specified by valid_schemes_). | 29 // valid scheme (as specified by valid_schemes_). |
28 // * The '*' scheme pattern excludes file URLs. | 30 // * The '*' scheme pattern excludes file URLs. |
29 // | 31 // |
30 // Examples of valid patterns: | 32 // Examples of valid patterns: |
31 // - http://*/* | 33 // - http://*/* |
32 // - http://*/foo* | 34 // - http://*/foo* |
33 // - https://*.google.com/foo*bar | 35 // - https://*.google.com/foo*bar |
34 // - file://monkey* | 36 // - file://monkey* |
35 // - http://127.0.0.1/* | 37 // - http://127.0.0.1/* |
38 // - filesystem:*://*/* | |
36 // | 39 // |
37 // Examples of invalid patterns: | 40 // Examples of invalid patterns: |
38 // - http://* -- path not specified | 41 // - http://* -- path not specified |
39 // - http://*foo/bar -- * not allowed as substring of host component | 42 // - http://*foo/bar -- * not allowed as substring of host component |
40 // - http://foo.*.bar/baz -- * must be first component | 43 // - http://foo.*.bar/baz -- * must be first component |
41 // - http:/bar -- scheme separator not found | 44 // - http:/bar -- scheme separator not found |
42 // - foo://* -- invalid scheme | 45 // - foo://* -- invalid scheme |
43 // - chrome:// -- we don't support chrome internal URLs | 46 // - chrome:// -- we don't support chrome internal URLs |
44 class URLPattern { | 47 class URLPattern { |
45 public: | 48 public: |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 bool operator==(const URLPattern& other) const; | 95 bool operator==(const URLPattern& other) const; |
93 | 96 |
94 // Initializes this instance by parsing the provided string. Returns | 97 // Initializes this instance by parsing the provided string. Returns |
95 // URLPattern::PARSE_SUCCESS on success, or an error code otherwise. On | 98 // URLPattern::PARSE_SUCCESS on success, or an error code otherwise. On |
96 // failure, this instance will have some intermediate values and is in an | 99 // failure, this instance will have some intermediate values and is in an |
97 // invalid state. | 100 // invalid state. |
98 ParseResult Parse(const std::string& pattern_str); | 101 ParseResult Parse(const std::string& pattern_str); |
99 | 102 |
100 // Gets the bitmask of valid schemes. | 103 // Gets the bitmask of valid schemes. |
101 int valid_schemes() const { return valid_schemes_; } | 104 int valid_schemes() const { return valid_schemes_; } |
105 | |
106 // Sets the bitmask of valid schemes and inner_schemes. | |
102 void SetValidSchemes(int valid_schemes); | 107 void SetValidSchemes(int valid_schemes); |
Aaron Boodman
2012/03/12 23:17:13
It's surprising that this would also set the inner
ericu
2012/03/13 21:58:47
Generally if you want to match http://google.com,
| |
103 | 108 |
109 // Gets the bitmask of valid inner schemes. | |
110 int valid_inner_schemes() const { return valid_inner_schemes_; } | |
111 | |
112 // Sets the bitmask of valid inner_schemes. | |
113 void SetValidInnerSchemes(int valid_schemes); | |
114 | |
104 // Gets the host the pattern matches. This can be an empty string if the | 115 // Gets the host the pattern matches. This can be an empty string if the |
105 // pattern matches all hosts (the input was <scheme>://*/<whatever>). | 116 // pattern matches all hosts (the input was <scheme>://*/<whatever>). |
106 const std::string& host() const { return host_; } | 117 const std::string& host() const { return host_; } |
107 void SetHost(const std::string& host); | 118 void SetHost(const std::string& host); |
108 | 119 |
109 // Gets whether to match subdomains of host(). | 120 // Gets whether to match subdomains of host(). |
110 bool match_subdomains() const { return match_subdomains_; } | 121 bool match_subdomains() const { return match_subdomains_; } |
111 void SetMatchSubdomains(bool val); | 122 void SetMatchSubdomains(bool val); |
112 | 123 |
113 // Gets the path the pattern matches with the leading slash. This can have | 124 // Gets the path the pattern matches with the leading slash. This can have |
114 // embedded asterisks which are interpreted using glob rules. | 125 // embedded asterisks which are interpreted using glob rules. |
115 const std::string& path() const { return path_; } | 126 const std::string& path() const { return path_; } |
116 void SetPath(const std::string& path); | 127 void SetPath(const std::string& path); |
117 | 128 |
118 // Returns true if this pattern matches all urls. | 129 // Returns true if this pattern matches all urls. |
119 bool match_all_urls() const { return match_all_urls_; } | 130 bool match_all_urls() const { return match_all_urls_; } |
120 void SetMatchAllURLs(bool val); | 131 void SetMatchAllURLs(bool val); |
121 | 132 |
122 // Sets the scheme for pattern matches. This can be a single '*' if the | 133 // Sets the scheme for pattern matches. This can be a single '*' if the |
123 // pattern matches all valid schemes (as defined by the valid_schemes_ | 134 // pattern matches all valid schemes (as defined by the valid_schemes_ |
124 // property). Returns false on failure (if the scheme is not valid). | 135 // property). Returns false on failure (if the scheme is not valid). |
125 bool SetScheme(const std::string& scheme); | 136 bool SetScheme(const std::string& scheme); |
126 // Note: You should use MatchesScheme() instead of this getter unless you | 137 // Sets the inner scheme for pattern matches; this can only be used for |
Aaron Boodman
2012/03/12 23:17:13
Add blank line before this comment block.
ericu
2012/03/13 21:58:47
Done.
| |
127 // absolutely need the exact scheme. This is exposed for testing. | 138 // patterns whose scheme has already been set to "filesystem". This can be a |
Aaron Boodman
2012/03/12 23:17:13
"with a scheme that supports inner schemes."
ericu
2012/03/13 21:58:47
Done.
| |
139 // single '*' if the pattern matches all valid schemes (as defined by the | |
140 // valid_inner_schemes_ property). Returns false on failure (if the scheme is | |
141 // not valid). | |
142 bool SetInnerScheme(const std::string& scheme); | |
143 | |
144 // Returns the scheme component of the pattern. Note that it is usually | |
145 // better to call MatchesScheme(), since that will also handle the case where | |
146 // the scheme is a wildcard. | |
128 const std::string& scheme() const { return scheme_; } | 147 const std::string& scheme() const { return scheme_; } |
129 | 148 |
149 // Returns the inner scheme component of the pattern. Note that it is usually | |
150 // better to call MatchesInnerScheme(), since that will also handle the case | |
151 // where the inner scheme is a wildcard. | |
152 const std::string& inner_scheme() const { return inner_scheme_; } | |
153 | |
130 // Returns true if the specified scheme can be used in this URL pattern, and | 154 // Returns true if the specified scheme can be used in this URL pattern, and |
131 // false otherwise. Uses valid_schemes_ to determine validity. | 155 // false otherwise. Uses valid_schemes_ to determine validity. |
132 bool IsValidScheme(const std::string& scheme) const; | 156 bool IsValidScheme(const std::string& scheme) const; |
133 | 157 |
158 // Returns true if the specified inner scheme can be used in this URL pattern, | |
159 // and false otherwise. Uses valid_inner_schemes_ to determine validity. | |
160 bool IsValidInnerScheme(const std::string& inner_scheme) const; | |
161 | |
134 // Returns true if this instance matches the specified URL. | 162 // Returns true if this instance matches the specified URL. |
135 bool MatchesURL(const GURL& test) const; | 163 bool MatchesURL(const GURL& test) const; |
136 | 164 |
137 // Returns true if this instance matches the specified security origin. | 165 // Returns true if this instance matches the specified security origin. |
138 bool MatchesSecurityOrigin(const GURL& test) const; | 166 bool MatchesSecurityOrigin(const GURL& test) const; |
139 | 167 |
140 // Returns true if |test| matches our scheme. | 168 // Returns true if |test| matches our scheme. |
141 bool MatchesScheme(const std::string& test) const; | 169 bool MatchesScheme(const std::string& test) const; |
142 | 170 |
171 // Returns true if |test| matches our inner scheme. | |
172 // Only use this if scheme is "filesystem". | |
173 bool MatchesInnerScheme(const std::string& test) const; | |
174 | |
143 // Returns true if |test| matches our host. | 175 // Returns true if |test| matches our host. |
144 bool MatchesHost(const std::string& test) const; | 176 bool MatchesHost(const std::string& test) const; |
145 bool MatchesHost(const GURL& test) const; | 177 bool MatchesHost(const GURL& test) const; |
146 | 178 |
147 // Returns true if |test| matches our path. | 179 // Returns true if |test| matches our path. |
148 bool MatchesPath(const std::string& test) const; | 180 bool MatchesPath(const std::string& test) const; |
149 | 181 |
150 // Returns true if |port| matches our port. | 182 // Returns true if |port| matches our port. |
151 bool MatchesPort(int port) const; | 183 bool MatchesPort(int port) const; |
152 | 184 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
194 // If the URLPattern contains a wildcard scheme, returns a list of | 226 // If the URLPattern contains a wildcard scheme, returns a list of |
195 // equivalent literal schemes, otherwise returns the current scheme. | 227 // equivalent literal schemes, otherwise returns the current scheme. |
196 std::vector<std::string> GetExplicitSchemes() const; | 228 std::vector<std::string> GetExplicitSchemes() const; |
197 | 229 |
198 // A bitmask containing the schemes which are considered valid for this | 230 // A bitmask containing the schemes which are considered valid for this |
199 // pattern. Parse() uses this to decide whether a pattern contains a valid | 231 // pattern. Parse() uses this to decide whether a pattern contains a valid |
200 // scheme. MatchesScheme uses this to decide whether a wildcard scheme_ | 232 // scheme. MatchesScheme uses this to decide whether a wildcard scheme_ |
201 // matches a given test scheme. | 233 // matches a given test scheme. |
202 int valid_schemes_; | 234 int valid_schemes_; |
203 | 235 |
236 // A bitmask containing the inner schemes which are considered valid for this | |
237 // pattern. Parse() uses this to decide whether a pattern contains a valid | |
238 // scheme. MatchesInnerScheme uses this to decide whether a wildcard | |
239 // inner_scheme_ matches a given test scheme. | |
240 int valid_inner_schemes_; | |
241 | |
204 // True if this is a special-case "<all_urls>" pattern. | 242 // True if this is a special-case "<all_urls>" pattern. |
205 bool match_all_urls_; | 243 bool match_all_urls_; |
206 | 244 |
207 // The scheme for the pattern. | 245 // The scheme for the pattern. |
208 std::string scheme_; | 246 std::string scheme_; |
209 | 247 |
248 // The inner scheme for the pattern, used only when scheme_ is "filesystem". | |
Aaron Boodman
2012/03/12 23:17:13
It would be better to create an array of schemes t
ericu
2012/03/13 21:58:47
I made a trivial inline function instead of an arr
| |
249 std::string inner_scheme_; | |
250 | |
210 // The host without any leading "*" components. | 251 // The host without any leading "*" components. |
211 std::string host_; | 252 std::string host_; |
212 | 253 |
213 // Whether we should match subdomains of the host. This is true if the first | 254 // Whether we should match subdomains of the host. This is true if the first |
214 // component of the pattern's host was "*". | 255 // component of the pattern's host was "*". |
215 bool match_subdomains_; | 256 bool match_subdomains_; |
216 | 257 |
217 // The port. | 258 // The port. |
218 std::string port_; | 259 std::string port_; |
219 | 260 |
220 // The path to match. This is everything after the host of the URL, or | 261 // The path to match. This is everything after the host of the URL, or |
221 // everything after the scheme in the case of file:// URLs. | 262 // everything after the scheme in the case of file:// URLs. |
222 std::string path_; | 263 std::string path_; |
223 | 264 |
224 // The path with "?" and "\" characters escaped for use with the | 265 // The path with "?" and "\" characters escaped for use with the |
225 // MatchPattern() function. | 266 // MatchPattern() function. |
226 std::string path_escaped_; | 267 std::string path_escaped_; |
227 | 268 |
228 // A string representing this URLPattern. | 269 // A string representing this URLPattern. |
229 mutable std::string spec_; | 270 mutable std::string spec_; |
230 }; | 271 }; |
231 | 272 |
232 typedef std::vector<URLPattern> URLPatternList; | 273 typedef std::vector<URLPattern> URLPatternList; |
233 | 274 |
234 #endif // CHROME_COMMON_EXTENSIONS_URL_PATTERN_H_ | 275 #endif // CHROME_COMMON_EXTENSIONS_URL_PATTERN_H_ |
OLD | NEW |