OLD | NEW |
1 // Copyright (c) 2012 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 EXTENSIONS_COMMON_URL_PATTERN_H_ | 4 #ifndef EXTENSIONS_COMMON_URL_PATTERN_H_ |
5 #define EXTENSIONS_COMMON_URL_PATTERN_H_ | 5 #define EXTENSIONS_COMMON_URL_PATTERN_H_ |
6 | 6 |
7 #include <functional> | 7 #include <functional> |
8 #include <iosfwd> | 8 #include <iosfwd> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 PARSE_ERROR_INVALID_SCHEME, | 74 PARSE_ERROR_INVALID_SCHEME, |
75 PARSE_ERROR_WRONG_SCHEME_SEPARATOR, | 75 PARSE_ERROR_WRONG_SCHEME_SEPARATOR, |
76 PARSE_ERROR_EMPTY_HOST, | 76 PARSE_ERROR_EMPTY_HOST, |
77 PARSE_ERROR_INVALID_HOST_WILDCARD, | 77 PARSE_ERROR_INVALID_HOST_WILDCARD, |
78 PARSE_ERROR_EMPTY_PATH, | 78 PARSE_ERROR_EMPTY_PATH, |
79 PARSE_ERROR_INVALID_PORT, | 79 PARSE_ERROR_INVALID_PORT, |
80 PARSE_ERROR_INVALID_HOST, | 80 PARSE_ERROR_INVALID_HOST, |
81 NUM_PARSE_RESULTS | 81 NUM_PARSE_RESULTS |
82 }; | 82 }; |
83 | 83 |
| 84 // Types of URLPattern that Parse() considers valid. |
| 85 enum ParseOptions { |
| 86 DENY_WILDCARD_FOR_EFFECTIVE_TLD, |
| 87 ALLOW_WILDCARD_FOR_EFFECTIVE_TLD, |
| 88 }; |
| 89 |
84 // The <all_urls> string pattern. | 90 // The <all_urls> string pattern. |
85 static const char kAllUrlsPattern[]; | 91 static const char kAllUrlsPattern[]; |
86 | 92 |
87 // Returns true if the given |scheme| is considered valid for extensions. | 93 // Returns true if the given |scheme| is considered valid for extensions. |
88 static bool IsValidSchemeForExtensions(base::StringPiece scheme); | 94 static bool IsValidSchemeForExtensions(base::StringPiece scheme); |
89 | 95 |
90 // Returns the mask for all schemes considered valid for extensions. | 96 // Returns the mask for all schemes considered valid for extensions. |
91 static int GetValidSchemeMaskForExtensions(); | 97 static int GetValidSchemeMaskForExtensions(); |
92 | 98 |
93 explicit URLPattern(int valid_schemes); | 99 explicit URLPattern(int valid_schemes); |
94 | 100 |
95 // Convenience to construct a URLPattern from a string. If the string is not | 101 // Convenience to construct a URLPattern from a string. If the string is not |
96 // known ahead of time, use Parse() instead, which returns success or failure. | 102 // known ahead of time, use Parse() instead, which returns success or failure. |
97 URLPattern(int valid_schemes, base::StringPiece pattern); | 103 URLPattern(int valid_schemes, base::StringPiece pattern); |
98 | 104 |
99 URLPattern(); | 105 URLPattern(); |
100 URLPattern(const URLPattern& other); | 106 URLPattern(const URLPattern& other); |
101 ~URLPattern(); | 107 ~URLPattern(); |
102 | 108 |
103 bool operator<(const URLPattern& other) const; | 109 bool operator<(const URLPattern& other) const; |
104 bool operator>(const URLPattern& other) const; | 110 bool operator>(const URLPattern& other) const; |
105 bool operator==(const URLPattern& other) const; | 111 bool operator==(const URLPattern& other) const; |
106 | 112 |
107 // Initializes this instance by parsing the provided string. Returns | 113 // Initializes this instance by parsing the provided string. Returns |
108 // URLPattern::PARSE_SUCCESS on success, or an error code otherwise. On | 114 // URLPattern::PARSE_SUCCESS on success, or an error code otherwise. On |
109 // failure, this instance will have some intermediate values and is in an | 115 // failure, this instance will have some intermediate values and is in an |
110 // invalid state. | 116 // invalid state. If you want to allow the match pattern to specify a wildcard |
| 117 // for the effective TLD, specify in |parse_options|. |
111 ParseResult Parse(base::StringPiece pattern_str); | 118 ParseResult Parse(base::StringPiece pattern_str); |
| 119 ParseResult Parse(base::StringPiece pattern_str, ParseOptions parse_options); |
112 | 120 |
113 // Gets the bitmask of valid schemes. | 121 // Gets the bitmask of valid schemes. |
114 int valid_schemes() const { return valid_schemes_; } | 122 int valid_schemes() const { return valid_schemes_; } |
115 void SetValidSchemes(int valid_schemes); | 123 void SetValidSchemes(int valid_schemes); |
116 | 124 |
117 // Gets the host the pattern matches. This can be an empty string if the | 125 // Gets the host the pattern matches. This can be an empty string if the |
118 // pattern matches all hosts (the input was <scheme>://*/<whatever>). | 126 // pattern matches all hosts (the input was <scheme>://*/<whatever>). |
119 const std::string& host() const { return host_; } | 127 const std::string& host() const { return host_; } |
120 void SetHost(base::StringPiece host); | 128 void SetHost(base::StringPiece host); |
121 | 129 |
122 // Gets whether to match subdomains of host(). | 130 // Gets whether to match subdomains of host(). |
123 bool match_subdomains() const { return match_subdomains_; } | 131 bool match_subdomains() const { return match_subdomains_; } |
124 void SetMatchSubdomains(bool val); | 132 void SetMatchSubdomains(bool val); |
125 | 133 |
| 134 // Gets whether host() contains an effective TLD. If false, during |
| 135 // a match, the URL you're comparing must have its TLD removed |
| 136 // prior to comparison. |
| 137 // e.g. For the match pattern https://google.com/* |
| 138 // If this is true: host() would be google.com |
| 139 // If this is false: host() would be google |
| 140 bool match_effective_tld() const { return match_effective_tld_; } |
| 141 void SetMatchEffectiveTld(bool val); |
| 142 |
126 // Gets the path the pattern matches with the leading slash. This can have | 143 // Gets the path the pattern matches with the leading slash. This can have |
127 // embedded asterisks which are interpreted using glob rules. | 144 // embedded asterisks which are interpreted using glob rules. |
128 const std::string& path() const { return path_; } | 145 const std::string& path() const { return path_; } |
129 void SetPath(base::StringPiece path); | 146 void SetPath(base::StringPiece path); |
130 | 147 |
131 // Returns true if this pattern matches all urls. | 148 // Returns true if this pattern matches all urls. |
132 bool match_all_urls() const { return match_all_urls_; } | 149 bool match_all_urls() const { return match_all_urls_; } |
133 void SetMatchAllURLs(bool val); | 150 void SetMatchAllURLs(bool val); |
134 | 151 |
135 // Sets the scheme for pattern matches. This can be a single '*' if the | 152 // Sets the scheme for pattern matches. This can be a single '*' if the |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 // The scheme for the pattern. | 257 // The scheme for the pattern. |
241 std::string scheme_; | 258 std::string scheme_; |
242 | 259 |
243 // The host without any leading "*" components. | 260 // The host without any leading "*" components. |
244 std::string host_; | 261 std::string host_; |
245 | 262 |
246 // Whether we should match subdomains of the host. This is true if the first | 263 // Whether we should match subdomains of the host. This is true if the first |
247 // component of the pattern's host was "*". | 264 // component of the pattern's host was "*". |
248 bool match_subdomains_; | 265 bool match_subdomains_; |
249 | 266 |
| 267 // Whether we should match the effective TLD of the host. This is true by |
| 268 // default and only false if ParseOptions is ALLOW_WILDCARD_FOR_EFFECTIVE_TLD |
| 269 // and is only applicable when the the pattern's host ends with ".*" |
| 270 // (e.g. https://example.*/*). |
| 271 bool match_effective_tld_; |
| 272 |
250 // The port. | 273 // The port. |
251 std::string port_; | 274 std::string port_; |
252 | 275 |
253 // The path to match. This is everything after the host of the URL, or | 276 // The path to match. This is everything after the host of the URL, or |
254 // everything after the scheme in the case of file:// URLs. | 277 // everything after the scheme in the case of file:// URLs. |
255 std::string path_; | 278 std::string path_; |
256 | 279 |
257 // The path with "?" and "\" characters escaped for use with the | 280 // The path with "?" and "\" characters escaped for use with the |
258 // MatchPattern() function. | 281 // MatchPattern() function. |
259 std::string path_escaped_; | 282 std::string path_escaped_; |
260 | 283 |
261 // A string representing this URLPattern. | 284 // A string representing this URLPattern. |
262 mutable std::string spec_; | 285 mutable std::string spec_; |
263 }; | 286 }; |
264 | 287 |
265 std::ostream& operator<<(std::ostream& out, const URLPattern& url_pattern); | 288 std::ostream& operator<<(std::ostream& out, const URLPattern& url_pattern); |
266 | 289 |
267 typedef std::vector<URLPattern> URLPatternList; | 290 typedef std::vector<URLPattern> URLPatternList; |
268 | 291 |
269 #endif // EXTENSIONS_COMMON_URL_PATTERN_H_ | 292 #endif // EXTENSIONS_COMMON_URL_PATTERN_H_ |
OLD | NEW |