| 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 | 4 |
| 5 #include "chrome/common/extensions/url_pattern.h" | 5 #include "chrome/common/extensions/url_pattern.h" |
| 6 | 6 |
| 7 #include "base/scoped_ptr.h" | 7 #include "base/scoped_ptr.h" |
| 8 #include "base/string_piece.h" | 8 #include "base/string_piece.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "chrome/common/url_constants.h" | 10 #include "chrome/common/url_constants.h" |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 return true; | 92 return true; |
| 93 } | 93 } |
| 94 | 94 |
| 95 bool URLPattern::MatchesUrl(const GURL &test) const { | 95 bool URLPattern::MatchesUrl(const GURL &test) const { |
| 96 if (test.scheme() != scheme_) | 96 if (test.scheme() != scheme_) |
| 97 return false; | 97 return false; |
| 98 | 98 |
| 99 if (!MatchesHost(test)) | 99 if (!MatchesHost(test)) |
| 100 return false; | 100 return false; |
| 101 | 101 |
| 102 if (!MatchesPath(test)) | 102 if (!MatchesPath(test.PathForRequest())) |
| 103 return false; | 103 return false; |
| 104 | 104 |
| 105 return true; | 105 return true; |
| 106 } | 106 } |
| 107 | 107 |
| 108 bool URLPattern::MatchesHost(const std::string& host) const { |
| 109 std::string url(chrome::kHttpScheme); |
| 110 url += chrome::kStandardSchemeSeparator; |
| 111 url += host; |
| 112 url += "/"; |
| 113 |
| 114 return MatchesHost(GURL(url)); |
| 115 } |
| 116 |
| 108 bool URLPattern::MatchesHost(const GURL& test) const { | 117 bool URLPattern::MatchesHost(const GURL& test) const { |
| 109 // If the hosts are exactly equal, we have a match. | 118 // If the hosts are exactly equal, we have a match. |
| 110 if (test.host() == host_) | 119 if (test.host() == host_) |
| 111 return true; | 120 return true; |
| 112 | 121 |
| 113 // If we're matching subdomains, and we have no host in the match pattern, | 122 // If we're matching subdomains, and we have no host in the match pattern, |
| 114 // that means that we're matching all hosts, which means we have a match no | 123 // that means that we're matching all hosts, which means we have a match no |
| 115 // matter what the test host is. | 124 // matter what the test host is. |
| 116 if (match_subdomains_ && host_.empty()) | 125 if (match_subdomains_ && host_.empty()) |
| 117 return true; | 126 return true; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 129 if (test.host().length() <= (host_.length() + 1)) | 138 if (test.host().length() <= (host_.length() + 1)) |
| 130 return false; | 139 return false; |
| 131 | 140 |
| 132 if (test.host().compare(test.host().length() - host_.length(), | 141 if (test.host().compare(test.host().length() - host_.length(), |
| 133 host_.length(), host_) != 0) | 142 host_.length(), host_) != 0) |
| 134 return false; | 143 return false; |
| 135 | 144 |
| 136 return test.host()[test.host().length() - host_.length() - 1] == '.'; | 145 return test.host()[test.host().length() - host_.length() - 1] == '.'; |
| 137 } | 146 } |
| 138 | 147 |
| 139 bool URLPattern::MatchesPath(const GURL& test) const { | 148 bool URLPattern::MatchesPath(const std::string& test) const { |
| 140 if (path_escaped_.empty()) { | 149 if (path_escaped_.empty()) { |
| 141 path_escaped_ = path_; | 150 path_escaped_ = path_; |
| 142 ReplaceSubstringsAfterOffset(&path_escaped_, 0, "\\", "\\\\"); | 151 ReplaceSubstringsAfterOffset(&path_escaped_, 0, "\\", "\\\\"); |
| 143 ReplaceSubstringsAfterOffset(&path_escaped_, 0, "?", "\\?"); | 152 ReplaceSubstringsAfterOffset(&path_escaped_, 0, "?", "\\?"); |
| 144 } | 153 } |
| 145 | 154 |
| 146 if (!MatchPatternASCII(test.PathForRequest(), path_escaped_)) | 155 if (!MatchPatternASCII(test, path_escaped_)) |
| 147 return false; | 156 return false; |
| 148 | 157 |
| 149 return true; | 158 return true; |
| 150 } | 159 } |
| 151 | 160 |
| 152 std::string URLPattern::GetAsString() const { | 161 std::string URLPattern::GetAsString() const { |
| 153 std::string spec = scheme_ + chrome::kStandardSchemeSeparator; | 162 std::string spec = scheme_ + chrome::kStandardSchemeSeparator; |
| 154 | 163 |
| 155 if (match_subdomains_) { | 164 if (match_subdomains_) { |
| 156 spec += "*"; | 165 spec += "*"; |
| 157 if (!host_.empty()) | 166 if (!host_.empty()) |
| 158 spec += "."; | 167 spec += "."; |
| 159 } | 168 } |
| 160 | 169 |
| 161 if (!host_.empty()) | 170 if (!host_.empty()) |
| 162 spec += host_; | 171 spec += host_; |
| 163 | 172 |
| 164 if (!path_.empty()) | 173 if (!path_.empty()) |
| 165 spec += path_; | 174 spec += path_; |
| 166 | 175 |
| 167 return spec; | 176 return spec; |
| 168 } | 177 } |
| 178 |
| 179 bool URLPattern::OverlapsWith(const URLPattern& other) const { |
| 180 if (scheme_ != other.scheme()) |
| 181 return false; |
| 182 |
| 183 if (!MatchesHost(other.host()) && !other.MatchesHost(host_)) |
| 184 return false; |
| 185 |
| 186 // We currently only use OverlapsWith() for the patterns inside |
| 187 // ExtensionExtent. In those cases, we know that the path will have only a |
| 188 // single wildcard at the end. This makes figuring out overlap much easier. It |
| 189 // seems like there is probably a computer-sciency way to solve the general |
| 190 // case, but we don't need that yet. |
| 191 DCHECK(path_.find('*') == path_.size() - 1); |
| 192 DCHECK(other.path().find('*') == other.path().size() - 1); |
| 193 |
| 194 if (!MatchesPath(other.path().substr(0, other.path().size() - 1)) && |
| 195 !other.MatchesPath(path_.substr(0, path_.size() - 1))) |
| 196 return false; |
| 197 |
| 198 return true; |
| 199 } |
| OLD | NEW |