| 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> |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 | 125 |
| 126 // Construct an URLPattern with the given set of allowable schemes. See | 126 // Construct an URLPattern with the given set of allowable schemes. See |
| 127 // valid_schemes_ for more info. | 127 // valid_schemes_ for more info. |
| 128 explicit URLPattern(int valid_schemes); | 128 explicit URLPattern(int valid_schemes); |
| 129 | 129 |
| 130 // Convenience to construct a URLPattern from a string. The string is expected | 130 // Convenience to construct a URLPattern from a string. The string is expected |
| 131 // to be a valid pattern. If the string is not known ahead of time, use | 131 // to be a valid pattern. If the string is not known ahead of time, use |
| 132 // Parse() instead, which returns success or failure. | 132 // Parse() instead, which returns success or failure. |
| 133 URLPattern(int valid_schemes, const std::string& pattern); | 133 URLPattern(int valid_schemes, const std::string& pattern); |
| 134 | 134 |
| 135 #if defined(_MSC_VER) && _MSC_VER >= 1600 | |
| 136 // Note: don't use this directly. This exists so URLPattern can be used | 135 // Note: don't use this directly. This exists so URLPattern can be used |
| 137 // with STL containers. Starting with Visual Studio 2010, we can't have this | 136 // with STL containers. |
| 138 // method private and use "friend class std::vector<URLPattern>;" as we used | |
| 139 // to do. | |
| 140 URLPattern(); | 137 URLPattern(); |
| 141 #endif | 138 ~URLPattern(); |
| 142 | 139 |
| 143 ~URLPattern(); | 140 bool operator<(const URLPattern& other) const; |
| 141 bool operator==(const URLPattern& other) const; |
| 142 |
| 143 // Initializes this instance by parsing the provided string. Returns |
| 144 // URLPattern::PARSE_SUCCESS on success, or an error code otherwise. On |
| 145 // failure, this instance will have some intermediate values and is in an |
| 146 // invalid state. Adding error checks to URLPattern::Parse() can cause |
| 147 // patterns in installed extensions to fail. If an installed extension |
| 148 // uses a pattern that was valid but fails a new error check, the |
| 149 // extension will fail to load when chrome is auto-updated. To avoid |
| 150 // this, new parse checks are enabled only when |strictness| is |
| 151 // OPTION_STRICT. OPTION_STRICT should be used when loading in developer |
| 152 // mode, or when an extension's patterns are controlled by chrome (such |
| 153 // as component extensions). |
| 154 ParseResult Parse(const std::string& pattern_str, |
| 155 ParseOption strictness); |
| 144 | 156 |
| 145 // Gets the bitmask of valid schemes. | 157 // Gets the bitmask of valid schemes. |
| 146 int valid_schemes() const { return valid_schemes_; } | 158 int valid_schemes() const { return valid_schemes_; } |
| 147 void set_valid_schemes(int valid_schemes) { valid_schemes_ = valid_schemes; } | 159 void SetValidSchemes(int valid_schemes); |
| 148 | 160 |
| 149 // Gets the host the pattern matches. This can be an empty string if the | 161 // Gets the host the pattern matches. This can be an empty string if the |
| 150 // pattern matches all hosts (the input was <scheme>://*/<whatever>). | 162 // pattern matches all hosts (the input was <scheme>://*/<whatever>). |
| 151 const std::string& host() const { return host_; } | 163 const std::string& host() const { return host_; } |
| 152 void set_host(const std::string& host) { host_ = host; } | 164 void SetHost(const std::string& host); |
| 153 | 165 |
| 154 // Gets whether to match subdomains of host(). | 166 // Gets whether to match subdomains of host(). |
| 155 bool match_subdomains() const { return match_subdomains_; } | 167 bool match_subdomains() const { return match_subdomains_; } |
| 156 void set_match_subdomains(bool val) { match_subdomains_ = val; } | 168 void SetMatchSubdomains(bool val); |
| 157 | 169 |
| 158 // Gets the path the pattern matches with the leading slash. This can have | 170 // Gets the path the pattern matches with the leading slash. This can have |
| 159 // embedded asterisks which are interpreted using glob rules. | 171 // embedded asterisks which are interpreted using glob rules. |
| 160 const std::string& path() const { return path_; } | 172 const std::string& path() const { return path_; } |
| 161 void SetPath(const std::string& path); | 173 void SetPath(const std::string& path); |
| 162 | 174 |
| 163 // Returns true if this pattern matches all urls. | 175 // Returns true if this pattern matches all urls. |
| 164 bool match_all_urls() const { return match_all_urls_; } | 176 bool match_all_urls() const { return match_all_urls_; } |
| 165 void set_match_all_urls(bool val) { match_all_urls_ = val; } | 177 void SetMatchAllURLs(bool val); |
| 166 | |
| 167 // Initializes this instance by parsing the provided string. Returns | |
| 168 // URLPattern::PARSE_SUCCESS on success, or an error code otherwise. On | |
| 169 // failure, this instance will have some intermediate values and is in an | |
| 170 // invalid state. Adding error checks to URLPattern::Parse() can cause | |
| 171 // patterns in installed extensions to fail. If an installed extension | |
| 172 // uses a pattern that was valid but fails a new error check, the | |
| 173 // extension will fail to load when chrome is auto-updated. To avoid | |
| 174 // this, new parse checks are enabled only when |strictness| is | |
| 175 // OPTION_STRICT. OPTION_STRICT should be used when loading in developer | |
| 176 // mode, or when an extension's patterns are controlled by chrome (such | |
| 177 // as component extensions). | |
| 178 ParseResult Parse(const std::string& pattern_str, | |
| 179 ParseOption strictness); | |
| 180 | 178 |
| 181 // Sets the scheme for pattern matches. This can be a single '*' if the | 179 // Sets the scheme for pattern matches. This can be a single '*' if the |
| 182 // pattern matches all valid schemes (as defined by the valid_schemes_ | 180 // pattern matches all valid schemes (as defined by the valid_schemes_ |
| 183 // property). Returns false on failure (if the scheme is not valid). | 181 // property). Returns false on failure (if the scheme is not valid). |
| 184 bool SetScheme(const std::string& scheme); | 182 bool SetScheme(const std::string& scheme); |
| 185 // Note: You should use MatchesScheme() instead of this getter unless you | 183 // Note: You should use MatchesScheme() instead of this getter unless you |
| 186 // absolutely need the exact scheme. This is exposed for testing. | 184 // absolutely need the exact scheme. This is exposed for testing. |
| 187 const std::string& scheme() const { return scheme_; } | 185 const std::string& scheme() const { return scheme_; } |
| 188 | 186 |
| 189 // Returns true if the specified scheme can be used in this URL pattern, and | 187 // Returns true if the specified scheme can be used in this URL pattern, and |
| (...skipping 14 matching lines...) Expand all Loading... |
| 204 bool MatchesPath(const std::string& test) const; | 202 bool MatchesPath(const std::string& test) const; |
| 205 | 203 |
| 206 // Returns true if |port| matches our port. | 204 // Returns true if |port| matches our port. |
| 207 bool MatchesPort(int port) const; | 205 bool MatchesPort(int port) const; |
| 208 | 206 |
| 209 // Sets the port. Returns false if the port is invalid. | 207 // Sets the port. Returns false if the port is invalid. |
| 210 bool SetPort(const std::string& port); | 208 bool SetPort(const std::string& port); |
| 211 const std::string& port() const { return port_; } | 209 const std::string& port() const { return port_; } |
| 212 | 210 |
| 213 // Returns a string representing this instance. | 211 // Returns a string representing this instance. |
| 214 std::string GetAsString() const; | 212 const std::string& GetAsString() const; |
| 215 | 213 |
| 216 // Determine whether there is a URL that would match this instance and another | 214 // Determine whether there is a URL that would match this instance and another |
| 217 // instance. This method is symmetrical: Calling other.OverlapsWith(this) | 215 // instance. This method is symmetrical: Calling other.OverlapsWith(this) |
| 218 // would result in the same answer. | 216 // would result in the same answer. |
| 219 bool OverlapsWith(const URLPattern& other) const; | 217 bool OverlapsWith(const URLPattern& other) const; |
| 220 | 218 |
| 221 // Convert this URLPattern into an equivalent set of URLPatterns that don't | 219 // Convert this URLPattern into an equivalent set of URLPatterns that don't |
| 222 // use a wildcard in the scheme component. If this URLPattern doesn't use a | 220 // use a wildcard in the scheme component. If this URLPattern doesn't use a |
| 223 // wildcard scheme, then the returned set will contain one element that is | 221 // wildcard scheme, then the returned set will contain one element that is |
| 224 // equivalent to this instance. | 222 // equivalent to this instance. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 235 public: | 233 public: |
| 236 bool operator()(const URLPattern& a, const URLPattern& b) const { | 234 bool operator()(const URLPattern& a, const URLPattern& b) const { |
| 237 return EffectiveHostCompare(a, b); | 235 return EffectiveHostCompare(a, b); |
| 238 }; | 236 }; |
| 239 }; | 237 }; |
| 240 | 238 |
| 241 // Get an error string for a ParseResult. | 239 // Get an error string for a ParseResult. |
| 242 static const char* GetParseResultString(URLPattern::ParseResult parse_result); | 240 static const char* GetParseResultString(URLPattern::ParseResult parse_result); |
| 243 | 241 |
| 244 private: | 242 private: |
| 245 #if !(defined(_MSC_VER) && _MSC_VER >= 1600) | |
| 246 friend class std::vector<URLPattern>; | |
| 247 | |
| 248 // Note: don't use this directly. This exists so URLPattern can be used | |
| 249 // with STL containers. | |
| 250 URLPattern(); | |
| 251 #endif | |
| 252 | |
| 253 // Returns true if any of the |schemes| items matches our scheme. | 243 // Returns true if any of the |schemes| items matches our scheme. |
| 254 bool MatchesAnyScheme(const std::vector<std::string>& schemes) const; | 244 bool MatchesAnyScheme(const std::vector<std::string>& schemes) const; |
| 255 | 245 |
| 256 // If the URLPattern contains a wildcard scheme, returns a list of | 246 // If the URLPattern contains a wildcard scheme, returns a list of |
| 257 // equivalent literal schemes, otherwise returns the current scheme. | 247 // equivalent literal schemes, otherwise returns the current scheme. |
| 258 std::vector<std::string> GetExplicitSchemes() const; | 248 std::vector<std::string> GetExplicitSchemes() const; |
| 259 | 249 |
| 260 // A bitmask containing the schemes which are considered valid for this | 250 // A bitmask containing the schemes which are considered valid for this |
| 261 // pattern. Parse() uses this to decide whether a pattern contains a valid | 251 // pattern. Parse() uses this to decide whether a pattern contains a valid |
| 262 // scheme. MatchesScheme uses this to decide whether a wildcard scheme_ | 252 // scheme. MatchesScheme uses this to decide whether a wildcard scheme_ |
| (...skipping 17 matching lines...) Expand all Loading... |
| 280 // the |USE_PORTS| option. | 270 // the |USE_PORTS| option. |
| 281 std::string port_; | 271 std::string port_; |
| 282 | 272 |
| 283 // The path to match. This is everything after the host of the URL, or | 273 // The path to match. This is everything after the host of the URL, or |
| 284 // everything after the scheme in the case of file:// URLs. | 274 // everything after the scheme in the case of file:// URLs. |
| 285 std::string path_; | 275 std::string path_; |
| 286 | 276 |
| 287 // The path with "?" and "\" characters escaped for use with the | 277 // The path with "?" and "\" characters escaped for use with the |
| 288 // MatchPattern() function. | 278 // MatchPattern() function. |
| 289 std::string path_escaped_; | 279 std::string path_escaped_; |
| 280 |
| 281 // A string representing this URLPattern. |
| 282 mutable std::string spec_; |
| 290 }; | 283 }; |
| 291 | 284 |
| 292 typedef std::vector<URLPattern> URLPatternList; | 285 typedef std::vector<URLPattern> URLPatternList; |
| 293 | 286 |
| 294 #endif // CHROME_COMMON_EXTENSIONS_URL_PATTERN_H_ | 287 #endif // CHROME_COMMON_EXTENSIONS_URL_PATTERN_H_ |
| OLD | NEW |