| 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 | |
| 90 // The <all_urls> string pattern. | 84 // The <all_urls> string pattern. |
| 91 static const char kAllUrlsPattern[]; | 85 static const char kAllUrlsPattern[]; |
| 92 | 86 |
| 93 // Returns true if the given |scheme| is considered valid for extensions. | 87 // Returns true if the given |scheme| is considered valid for extensions. |
| 94 static bool IsValidSchemeForExtensions(base::StringPiece scheme); | 88 static bool IsValidSchemeForExtensions(base::StringPiece scheme); |
| 95 | 89 |
| 96 // Returns the mask for all schemes considered valid for extensions. | 90 // Returns the mask for all schemes considered valid for extensions. |
| 97 static int GetValidSchemeMaskForExtensions(); | 91 static int GetValidSchemeMaskForExtensions(); |
| 98 | 92 |
| 99 explicit URLPattern(int valid_schemes); | 93 explicit URLPattern(int valid_schemes); |
| 100 | 94 |
| 101 // Convenience to construct a URLPattern from a string. If the string is not | 95 // Convenience to construct a URLPattern from a string. If the string is not |
| 102 // known ahead of time, use Parse() instead, which returns success or failure. | 96 // known ahead of time, use Parse() instead, which returns success or failure. |
| 103 URLPattern(int valid_schemes, base::StringPiece pattern); | 97 URLPattern(int valid_schemes, base::StringPiece pattern); |
| 104 | 98 |
| 105 URLPattern(); | 99 URLPattern(); |
| 106 URLPattern(const URLPattern& other); | 100 URLPattern(const URLPattern& other); |
| 107 ~URLPattern(); | 101 ~URLPattern(); |
| 108 | 102 |
| 109 bool operator<(const URLPattern& other) const; | 103 bool operator<(const URLPattern& other) const; |
| 110 bool operator>(const URLPattern& other) const; | 104 bool operator>(const URLPattern& other) const; |
| 111 bool operator==(const URLPattern& other) const; | 105 bool operator==(const URLPattern& other) const; |
| 112 | 106 |
| 113 // Initializes this instance by parsing the provided string. Returns | 107 // Initializes this instance by parsing the provided string. Returns |
| 114 // URLPattern::PARSE_SUCCESS on success, or an error code otherwise. On | 108 // URLPattern::PARSE_SUCCESS on success, or an error code otherwise. On |
| 115 // failure, this instance will have some intermediate values and is in an | 109 // failure, this instance will have some intermediate values and is in an |
| 116 // invalid state. If you want to allow the match pattern to specify a wildcard | 110 // invalid state. |
| 117 // for the effective TLD, specify in |parse_options|. | |
| 118 ParseResult Parse(base::StringPiece pattern_str); | 111 ParseResult Parse(base::StringPiece pattern_str); |
| 119 ParseResult Parse(base::StringPiece pattern_str, ParseOptions parse_options); | |
| 120 | 112 |
| 121 // Gets the bitmask of valid schemes. | 113 // Gets the bitmask of valid schemes. |
| 122 int valid_schemes() const { return valid_schemes_; } | 114 int valid_schemes() const { return valid_schemes_; } |
| 123 void SetValidSchemes(int valid_schemes); | 115 void SetValidSchemes(int valid_schemes); |
| 124 | 116 |
| 125 // Gets the host the pattern matches. This can be an empty string if the | 117 // Gets the host the pattern matches. This can be an empty string if the |
| 126 // pattern matches all hosts (the input was <scheme>://*/<whatever>). | 118 // pattern matches all hosts (the input was <scheme>://*/<whatever>). |
| 127 const std::string& host() const { return host_; } | 119 const std::string& host() const { return host_; } |
| 128 void SetHost(base::StringPiece host); | 120 void SetHost(base::StringPiece host); |
| 129 | 121 |
| 130 // Gets whether to match subdomains of host(). | 122 // Gets whether to match subdomains of host(). |
| 131 bool match_subdomains() const { return match_subdomains_; } | 123 bool match_subdomains() const { return match_subdomains_; } |
| 132 void SetMatchSubdomains(bool val); | 124 void SetMatchSubdomains(bool val); |
| 133 | 125 |
| 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 | |
| 143 // Gets the path the pattern matches with the leading slash. This can have | 126 // Gets the path the pattern matches with the leading slash. This can have |
| 144 // embedded asterisks which are interpreted using glob rules. | 127 // embedded asterisks which are interpreted using glob rules. |
| 145 const std::string& path() const { return path_; } | 128 const std::string& path() const { return path_; } |
| 146 void SetPath(base::StringPiece path); | 129 void SetPath(base::StringPiece path); |
| 147 | 130 |
| 148 // Returns true if this pattern matches all urls. | 131 // Returns true if this pattern matches all urls. |
| 149 bool match_all_urls() const { return match_all_urls_; } | 132 bool match_all_urls() const { return match_all_urls_; } |
| 150 void SetMatchAllURLs(bool val); | 133 void SetMatchAllURLs(bool val); |
| 151 | 134 |
| 152 // Sets the scheme for pattern matches. This can be a single '*' if the | 135 // 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... |
| 257 // The scheme for the pattern. | 240 // The scheme for the pattern. |
| 258 std::string scheme_; | 241 std::string scheme_; |
| 259 | 242 |
| 260 // The host without any leading "*" components. | 243 // The host without any leading "*" components. |
| 261 std::string host_; | 244 std::string host_; |
| 262 | 245 |
| 263 // Whether we should match subdomains of the host. This is true if the first | 246 // Whether we should match subdomains of the host. This is true if the first |
| 264 // component of the pattern's host was "*". | 247 // component of the pattern's host was "*". |
| 265 bool match_subdomains_; | 248 bool match_subdomains_; |
| 266 | 249 |
| 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 | |
| 273 // The port. | 250 // The port. |
| 274 std::string port_; | 251 std::string port_; |
| 275 | 252 |
| 276 // The path to match. This is everything after the host of the URL, or | 253 // The path to match. This is everything after the host of the URL, or |
| 277 // everything after the scheme in the case of file:// URLs. | 254 // everything after the scheme in the case of file:// URLs. |
| 278 std::string path_; | 255 std::string path_; |
| 279 | 256 |
| 280 // The path with "?" and "\" characters escaped for use with the | 257 // The path with "?" and "\" characters escaped for use with the |
| 281 // MatchPattern() function. | 258 // MatchPattern() function. |
| 282 std::string path_escaped_; | 259 std::string path_escaped_; |
| 283 | 260 |
| 284 // A string representing this URLPattern. | 261 // A string representing this URLPattern. |
| 285 mutable std::string spec_; | 262 mutable std::string spec_; |
| 286 }; | 263 }; |
| 287 | 264 |
| 288 std::ostream& operator<<(std::ostream& out, const URLPattern& url_pattern); | 265 std::ostream& operator<<(std::ostream& out, const URLPattern& url_pattern); |
| 289 | 266 |
| 290 typedef std::vector<URLPattern> URLPatternList; | 267 typedef std::vector<URLPattern> URLPatternList; |
| 291 | 268 |
| 292 #endif // EXTENSIONS_COMMON_URL_PATTERN_H_ | 269 #endif // EXTENSIONS_COMMON_URL_PATTERN_H_ |
| OLD | NEW |