| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "googleurl/src/gurl.h" | 9 #include "googleurl/src/gurl.h" |
| 10 | 10 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 public: | 71 public: |
| 72 // Returns true if the specified scheme can be used in URL patterns, and false | 72 // Returns true if the specified scheme can be used in URL patterns, and false |
| 73 // otherwise. | 73 // otherwise. |
| 74 static bool IsValidScheme(const std::string& scheme); | 74 static bool IsValidScheme(const std::string& scheme); |
| 75 | 75 |
| 76 // Convenience to create a pattern from a string. | 76 // Convenience to create a pattern from a string. |
| 77 static URLPattern* CreateFromString(const std::string& pattern); | 77 static URLPattern* CreateFromString(const std::string& pattern); |
| 78 | 78 |
| 79 URLPattern() : match_subdomains_(false) {} | 79 URLPattern() : match_subdomains_(false) {} |
| 80 | 80 |
| 81 // Initializes this instance by parsing the provided string. On failure, the | |
| 82 // instance will have some intermediate values and is in an invalid state. | |
| 83 bool Parse(const std::string& pattern_str); | |
| 84 | |
| 85 // Returns true if this instance matches the specified URL. | |
| 86 bool MatchesUrl(const GURL& url) const; | |
| 87 | |
| 88 std::string GetAsString() const; | |
| 89 | |
| 90 // Get the scheme the pattern matches. This will always return a valid scheme | 81 // Get the scheme the pattern matches. This will always return a valid scheme |
| 91 // if is_valid() returns true. | 82 // if is_valid() returns true. |
| 92 std::string scheme() const { return scheme_; } | 83 std::string scheme() const { return scheme_; } |
| 93 void set_scheme(const std::string& scheme) { scheme_ = scheme; } | 84 void set_scheme(const std::string& scheme) { scheme_ = scheme; } |
| 94 | 85 |
| 95 // Gets the host the pattern matches. This can be an empty string if the | 86 // Gets the host the pattern matches. This can be an empty string if the |
| 96 // pattern matches all hosts (the input was <scheme>://*/<whatever>). | 87 // pattern matches all hosts (the input was <scheme>://*/<whatever>). |
| 97 std::string host() const { return host_; } | 88 std::string host() const { return host_; } |
| 98 void set_host(const std::string& host) { host_ = host; } | 89 void set_host(const std::string& host) { host_ = host; } |
| 99 | 90 |
| 100 // Gets whether to match subdomains of host(). | 91 // Gets whether to match subdomains of host(). |
| 101 bool match_subdomains() const { return match_subdomains_; } | 92 bool match_subdomains() const { return match_subdomains_; } |
| 102 void set_match_subdomains(bool val) { match_subdomains_ = val; } | 93 void set_match_subdomains(bool val) { match_subdomains_ = val; } |
| 103 | 94 |
| 104 // Gets the path the pattern matches with the leading slash. This can have | 95 // Gets the path the pattern matches with the leading slash. This can have |
| 105 // embedded asterisks which are interpreted using glob rules. | 96 // embedded asterisks which are interpreted using glob rules. |
| 106 std::string path() const { return path_; } | 97 std::string path() const { return path_; } |
| 107 void set_path(const std::string& path) { | 98 void set_path(const std::string& path) { |
| 108 path_ = path; | 99 path_ = path; |
| 109 path_escaped_ = ""; | 100 path_escaped_ = ""; |
| 110 } | 101 } |
| 111 | 102 |
| 103 // Initializes this instance by parsing the provided string. On failure, the |
| 104 // instance will have some intermediate values and is in an invalid state. |
| 105 bool Parse(const std::string& pattern_str); |
| 106 |
| 107 // Returns true if this instance matches the specified URL. |
| 108 bool MatchesUrl(const GURL& url) const; |
| 109 |
| 110 // Returns a string representing this instance. |
| 111 std::string GetAsString() const; |
| 112 |
| 113 // Determine whether there is a URL that would match this instance and another |
| 114 // instance. This method is symmetrical: Calling other.OverlapsWith(this) |
| 115 // would result in the same answer. |
| 116 bool OverlapsWith(const URLPattern& other) const; |
| 117 |
| 112 private: | 118 private: |
| 113 // Returns true if |test| matches our host. | 119 // Returns true if |test| matches our host. |
| 120 bool MatchesHost(const std::string& host) const; |
| 114 bool MatchesHost(const GURL& test) const; | 121 bool MatchesHost(const GURL& test) const; |
| 115 | 122 |
| 116 // Returns true if |test| matches our path. | 123 // Returns true if |test| matches our path. |
| 117 bool MatchesPath(const GURL& test) const; | 124 bool MatchesPath(const std::string& test) const; |
| 118 | 125 |
| 119 // The scheme for the pattern. | 126 // The scheme for the pattern. |
| 120 std::string scheme_; | 127 std::string scheme_; |
| 121 | 128 |
| 122 // The host without any leading "*" components. | 129 // The host without any leading "*" components. |
| 123 std::string host_; | 130 std::string host_; |
| 124 | 131 |
| 125 // Whether we should match subdomains of the host. This is true if the first | 132 // Whether we should match subdomains of the host. This is true if the first |
| 126 // component of the pattern's host was "*". | 133 // component of the pattern's host was "*". |
| 127 bool match_subdomains_; | 134 bool match_subdomains_; |
| 128 | 135 |
| 129 // The path to match. This is everything after the host of the URL, or | 136 // The path to match. This is everything after the host of the URL, or |
| 130 // everything after the scheme in the case of file:// URLs. | 137 // everything after the scheme in the case of file:// URLs. |
| 131 std::string path_; | 138 std::string path_; |
| 132 | 139 |
| 133 // The path with "?" and "\" characters escaped for use with the | 140 // The path with "?" and "\" characters escaped for use with the |
| 134 // MatchPatternASCII() function. This is populated lazily, the first time it | 141 // MatchPatternASCII() function. This is populated lazily, the first time it |
| 135 // is needed. | 142 // is needed. |
| 136 mutable std::string path_escaped_; | 143 mutable std::string path_escaped_; |
| 137 }; | 144 }; |
| 138 | 145 |
| 139 #endif // CHROME_COMMON_EXTENSIONS_URL_PATTERN_H_ | 146 #endif // CHROME_COMMON_EXTENSIONS_URL_PATTERN_H_ |
| OLD | NEW |