Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
|
markusheintz_
2012/02/16 10:02:57
nit: s/2011/2012
ericu
2012/02/22 00:00:51
Done.
| |
| 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 // Sets the bitmask of valid schemes and inner_schemes. | |
| 102 void SetValidSchemes(int valid_schemes); | 106 void SetValidSchemes(int valid_schemes); |
| 107 // Gets the bitmask of valid inner schemes. | |
| 108 int valid_inner_schemes() const { return valid_inner_schemes_; } | |
| 109 // Sets the bitmask of valid inner_schemes. | |
| 110 void SetValidInnerSchemes(int valid_schemes); | |
| 103 | 111 |
| 104 // Gets the host the pattern matches. This can be an empty string if the | 112 // Gets the host the pattern matches. This can be an empty string if the |
| 105 // pattern matches all hosts (the input was <scheme>://*/<whatever>). | 113 // pattern matches all hosts (the input was <scheme>://*/<whatever>). |
| 106 const std::string& host() const { return host_; } | 114 const std::string& host() const { return host_; } |
| 107 void SetHost(const std::string& host); | 115 void SetHost(const std::string& host); |
| 108 | 116 |
| 109 // Gets whether to match subdomains of host(). | 117 // Gets whether to match subdomains of host(). |
| 110 bool match_subdomains() const { return match_subdomains_; } | 118 bool match_subdomains() const { return match_subdomains_; } |
| 111 void SetMatchSubdomains(bool val); | 119 void SetMatchSubdomains(bool val); |
| 112 | 120 |
| 113 // Gets the path the pattern matches with the leading slash. This can have | 121 // Gets the path the pattern matches with the leading slash. This can have |
| 114 // embedded asterisks which are interpreted using glob rules. | 122 // embedded asterisks which are interpreted using glob rules. |
| 115 const std::string& path() const { return path_; } | 123 const std::string& path() const { return path_; } |
| 116 void SetPath(const std::string& path); | 124 void SetPath(const std::string& path); |
| 117 | 125 |
| 118 // Returns true if this pattern matches all urls. | 126 // Returns true if this pattern matches all urls. |
| 119 bool match_all_urls() const { return match_all_urls_; } | 127 bool match_all_urls() const { return match_all_urls_; } |
| 120 void SetMatchAllURLs(bool val); | 128 void SetMatchAllURLs(bool val); |
| 121 | 129 |
| 122 // Sets the scheme for pattern matches. This can be a single '*' if the | 130 // 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_ | 131 // pattern matches all valid schemes (as defined by the valid_schemes_ |
| 124 // property). Returns false on failure (if the scheme is not valid). | 132 // property). Returns false on failure (if the scheme is not valid). |
| 125 bool SetScheme(const std::string& scheme); | 133 bool SetScheme(const std::string& scheme); |
| 134 // Sets the inner scheme for pattern matches; this can only be used for | |
| 135 // patterns whose scheme has already been set to "filesystem". This can be a | |
| 136 // single '*' if the pattern matches all valid schemes (as defined by the | |
| 137 // valid_inner_schemes_ property). Returns false on failure (if the scheme is | |
| 138 // not valid). | |
| 139 bool SetInnerScheme(const std::string& scheme); | |
| 126 // Note: You should use MatchesScheme() instead of this getter unless you | 140 // Note: You should use MatchesScheme() instead of this getter unless you |
| 127 // absolutely need the exact scheme. This is exposed for testing. | 141 // absolutely need the exact scheme. This is exposed for testing. |
| 128 const std::string& scheme() const { return scheme_; } | 142 const std::string& scheme() const { return scheme_; } |
| 143 // Note: You should use MatchesInnerScheme() instead of this getter unless you | |
| 144 // absolutely need the exact scheme. This is exposed for testing. | |
| 145 const std::string& inner_scheme() const { return inner_scheme_; } | |
| 129 | 146 |
| 130 // Returns true if the specified scheme can be used in this URL pattern, and | 147 // Returns true if the specified scheme can be used in this URL pattern, and |
| 131 // false otherwise. Uses valid_schemes_ to determine validity. | 148 // false otherwise. Uses valid_schemes_ to determine validity. |
| 132 bool IsValidScheme(const std::string& scheme) const; | 149 bool IsValidScheme(const std::string& scheme) const; |
| 133 | 150 |
| 151 // Returns true if the specified scheme can be used in this URL pattern, and | |
| 152 // false otherwise. Uses valid_inner_schemes_ to determine validity. | |
| 153 bool IsValidInnerScheme(const std::string& scheme) const; | |
| 154 | |
| 134 // Returns true if this instance matches the specified URL. | 155 // Returns true if this instance matches the specified URL. |
| 135 bool MatchesURL(const GURL& test) const; | 156 bool MatchesURL(const GURL& test) const; |
| 136 | 157 |
| 137 // Returns true if this instance matches the specified security origin. | 158 // Returns true if this instance matches the specified security origin. |
| 138 bool MatchesSecurityOrigin(const GURL& test) const; | 159 bool MatchesSecurityOrigin(const GURL& test) const; |
| 139 | 160 |
| 140 // Returns true if |test| matches our scheme. | 161 // Returns true if |test| matches our scheme. |
| 141 bool MatchesScheme(const std::string& test) const; | 162 bool MatchesScheme(const std::string& test) const; |
| 142 | 163 |
| 164 // Returns true if |test| matches our inner scheme. | |
| 165 // Only use this if scheme is "filesystem". | |
| 166 bool MatchesInnerScheme(const std::string& test) const; | |
| 167 | |
| 143 // Returns true if |test| matches our host. | 168 // Returns true if |test| matches our host. |
| 144 bool MatchesHost(const std::string& test) const; | 169 bool MatchesHost(const std::string& test) const; |
| 145 bool MatchesHost(const GURL& test) const; | 170 bool MatchesHost(const GURL& test) const; |
| 146 | 171 |
| 147 // Returns true if |test| matches our path. | 172 // Returns true if |test| matches our path. |
| 148 bool MatchesPath(const std::string& test) const; | 173 bool MatchesPath(const std::string& test) const; |
| 149 | 174 |
| 150 // Returns true if |port| matches our port. | 175 // Returns true if |port| matches our port. |
| 151 bool MatchesPort(int port) const; | 176 bool MatchesPort(int port) const; |
| 152 | 177 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 193 | 218 |
| 194 // If the URLPattern contains a wildcard scheme, returns a list of | 219 // If the URLPattern contains a wildcard scheme, returns a list of |
| 195 // equivalent literal schemes, otherwise returns the current scheme. | 220 // equivalent literal schemes, otherwise returns the current scheme. |
| 196 std::vector<std::string> GetExplicitSchemes() const; | 221 std::vector<std::string> GetExplicitSchemes() const; |
| 197 | 222 |
| 198 // A bitmask containing the schemes which are considered valid for this | 223 // A bitmask containing the schemes which are considered valid for this |
| 199 // pattern. Parse() uses this to decide whether a pattern contains a valid | 224 // pattern. Parse() uses this to decide whether a pattern contains a valid |
| 200 // scheme. MatchesScheme uses this to decide whether a wildcard scheme_ | 225 // scheme. MatchesScheme uses this to decide whether a wildcard scheme_ |
| 201 // matches a given test scheme. | 226 // matches a given test scheme. |
| 202 int valid_schemes_; | 227 int valid_schemes_; |
| 228 // A bitmask containing the inner schemes which are considered valid for this | |
| 229 // pattern. Parse() uses this to decide whether a pattern contains a valid | |
| 230 // scheme. MatchesInnerScheme uses this to decide whether a wildcard | |
| 231 // inner_scheme_ matches a given test scheme. | |
| 232 int valid_inner_schemes_; | |
| 203 | 233 |
| 204 // True if this is a special-case "<all_urls>" pattern. | 234 // True if this is a special-case "<all_urls>" pattern. |
| 205 bool match_all_urls_; | 235 bool match_all_urls_; |
| 206 | 236 |
| 207 // The scheme for the pattern. | 237 // The scheme for the pattern. |
| 208 std::string scheme_; | 238 std::string scheme_; |
| 209 | 239 |
| 240 // The inner scheme for the pattern, used only when scheme_ is "filesystem". | |
| 241 std::string inner_scheme_; | |
| 242 | |
| 210 // The host without any leading "*" components. | 243 // The host without any leading "*" components. |
| 211 std::string host_; | 244 std::string host_; |
| 212 | 245 |
| 213 // 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 |
| 214 // component of the pattern's host was "*". | 247 // component of the pattern's host was "*". |
| 215 bool match_subdomains_; | 248 bool match_subdomains_; |
| 216 | 249 |
| 217 // The port. | 250 // The port. |
| 218 std::string port_; | 251 std::string port_; |
| 219 | 252 |
| 220 // 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 |
| 221 // everything after the scheme in the case of file:// URLs. | 254 // everything after the scheme in the case of file:// URLs. |
| 222 std::string path_; | 255 std::string path_; |
| 223 | 256 |
| 224 // The path with "?" and "\" characters escaped for use with the | 257 // The path with "?" and "\" characters escaped for use with the |
| 225 // MatchPattern() function. | 258 // MatchPattern() function. |
| 226 std::string path_escaped_; | 259 std::string path_escaped_; |
| 227 | 260 |
| 228 // A string representing this URLPattern. | 261 // A string representing this URLPattern. |
| 229 mutable std::string spec_; | 262 mutable std::string spec_; |
| 230 }; | 263 }; |
| 231 | 264 |
| 232 typedef std::vector<URLPattern> URLPatternList; | 265 typedef std::vector<URLPattern> URLPatternList; |
| 233 | 266 |
| 234 #endif // CHROME_COMMON_EXTENSIONS_URL_PATTERN_H_ | 267 #endif // CHROME_COMMON_EXTENSIONS_URL_PATTERN_H_ |
| OLD | NEW |