| 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/string_piece.h" | 7 #include "base/string_piece.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "chrome/common/url_constants.h" | 9 #include "chrome/common/url_constants.h" |
| 10 | 10 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 size_t host_start_pos = scheme_end_pos + | 69 size_t host_start_pos = scheme_end_pos + |
| 70 strlen(chrome::kStandardSchemeSeparator); | 70 strlen(chrome::kStandardSchemeSeparator); |
| 71 if (host_start_pos >= pattern.length()) | 71 if (host_start_pos >= pattern.length()) |
| 72 return false; | 72 return false; |
| 73 | 73 |
| 74 // Parse out the host and path. | 74 // Parse out the host and path. |
| 75 size_t path_start_pos = 0; | 75 size_t path_start_pos = 0; |
| 76 | 76 |
| 77 // File URLs are special because they have no host. There are other schemes | 77 // File URLs are special because they have no host. There are other schemes |
| 78 // with the same structure, but we don't support them (yet). | 78 // with the same structure, but we don't support them (yet). |
| 79 if (scheme_ == "file") { | 79 if (scheme_ == chrome::kFileScheme) { |
| 80 path_start_pos = host_start_pos; | 80 path_start_pos = host_start_pos; |
| 81 } else { | 81 } else { |
| 82 size_t host_end_pos = pattern.find(kPathSeparator, host_start_pos); | 82 size_t host_end_pos = pattern.find(kPathSeparator, host_start_pos); |
| 83 if (host_end_pos == std::string::npos) | 83 if (host_end_pos == std::string::npos) |
| 84 return false; | 84 return false; |
| 85 | 85 |
| 86 host_ = pattern.substr(host_start_pos, host_end_pos - host_start_pos); | 86 host_ = pattern.substr(host_start_pos, host_end_pos - host_start_pos); |
| 87 | 87 |
| 88 // The first component can optionally be '*' to match all subdomains. | 88 // The first component can optionally be '*' to match all subdomains. |
| 89 std::vector<std::string> host_components; | 89 std::vector<std::string> host_components; |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 | 199 |
| 200 return true; | 200 return true; |
| 201 } | 201 } |
| 202 | 202 |
| 203 std::string URLPattern::GetAsString() const { | 203 std::string URLPattern::GetAsString() const { |
| 204 if (match_all_urls_) | 204 if (match_all_urls_) |
| 205 return kAllUrlsPattern; | 205 return kAllUrlsPattern; |
| 206 | 206 |
| 207 std::string spec = scheme_ + chrome::kStandardSchemeSeparator; | 207 std::string spec = scheme_ + chrome::kStandardSchemeSeparator; |
| 208 | 208 |
| 209 if (match_subdomains_) { | 209 if (scheme_ != chrome::kFileScheme) { |
| 210 spec += "*"; | 210 if (match_subdomains_) { |
| 211 spec += "*"; |
| 212 if (!host_.empty()) |
| 213 spec += "."; |
| 214 } |
| 215 |
| 211 if (!host_.empty()) | 216 if (!host_.empty()) |
| 212 spec += "."; | 217 spec += host_; |
| 213 } | 218 } |
| 214 | 219 |
| 215 if (!host_.empty()) | |
| 216 spec += host_; | |
| 217 | |
| 218 if (!path_.empty()) | 220 if (!path_.empty()) |
| 219 spec += path_; | 221 spec += path_; |
| 220 | 222 |
| 221 return spec; | 223 return spec; |
| 222 } | 224 } |
| 223 | 225 |
| 224 bool URLPattern::OverlapsWith(const URLPattern& other) const { | 226 bool URLPattern::OverlapsWith(const URLPattern& other) const { |
| 225 if (!MatchesScheme(other.scheme_) && !other.MatchesScheme(scheme_)) | 227 if (!MatchesScheme(other.scheme_) && !other.MatchesScheme(scheme_)) |
| 226 return false; | 228 return false; |
| 227 | 229 |
| 228 if (!MatchesHost(other.host()) && !other.MatchesHost(host_)) | 230 if (!MatchesHost(other.host()) && !other.MatchesHost(host_)) |
| 229 return false; | 231 return false; |
| 230 | 232 |
| 231 // We currently only use OverlapsWith() for the patterns inside | 233 // We currently only use OverlapsWith() for the patterns inside |
| 232 // ExtensionExtent. In those cases, we know that the path will have only a | 234 // ExtensionExtent. In those cases, we know that the path will have only a |
| 233 // single wildcard at the end. This makes figuring out overlap much easier. It | 235 // single wildcard at the end. This makes figuring out overlap much easier. It |
| 234 // seems like there is probably a computer-sciency way to solve the general | 236 // seems like there is probably a computer-sciency way to solve the general |
| 235 // case, but we don't need that yet. | 237 // case, but we don't need that yet. |
| 236 DCHECK(path_.find('*') == path_.size() - 1); | 238 DCHECK(path_.find('*') == path_.size() - 1); |
| 237 DCHECK(other.path().find('*') == other.path().size() - 1); | 239 DCHECK(other.path().find('*') == other.path().size() - 1); |
| 238 | 240 |
| 239 if (!MatchesPath(other.path().substr(0, other.path().size() - 1)) && | 241 if (!MatchesPath(other.path().substr(0, other.path().size() - 1)) && |
| 240 !other.MatchesPath(path_.substr(0, path_.size() - 1))) | 242 !other.MatchesPath(path_.substr(0, path_.size() - 1))) |
| 241 return false; | 243 return false; |
| 242 | 244 |
| 243 return true; | 245 return true; |
| 244 } | 246 } |
| 247 |
| 248 std::vector<URLPattern> URLPattern::ConvertToExplicitSchemes() const { |
| 249 std::vector<URLPattern> result; |
| 250 |
| 251 if (scheme_ != "*" && !match_all_urls_) { |
| 252 result.push_back(*this); |
| 253 return result; |
| 254 } |
| 255 |
| 256 for (size_t i = 0; i < arraysize(kValidSchemes); ++i) { |
| 257 if (MatchesScheme(kValidSchemes[i])) { |
| 258 URLPattern temp = *this; |
| 259 temp.SetScheme(kValidSchemes[i]); |
| 260 temp.set_match_all_urls(false); |
| 261 result.push_back(temp); |
| 262 } |
| 263 } |
| 264 |
| 265 return result; |
| 266 } |
| OLD | NEW |