| 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 | 4 |
| 5 #include "chrome/common/extensions/url_pattern.h" | 5 #include "chrome/common/extensions/url_pattern.h" |
| 6 | 6 |
| 7 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
| 8 #include "base/string_piece.h" | 8 #include "base/string_piece.h" |
| 9 #include "base/string_split.h" | 9 #include "base/string_split.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 | 117 |
| 118 // Strict error checking is used, because this constructor is only | 118 // Strict error checking is used, because this constructor is only |
| 119 // appropriate when we know |pattern| is valid. | 119 // appropriate when we know |pattern| is valid. |
| 120 if (PARSE_SUCCESS != Parse(pattern, ERROR_ON_PORTS)) | 120 if (PARSE_SUCCESS != Parse(pattern, ERROR_ON_PORTS)) |
| 121 NOTREACHED() << "URLPattern is invalid: " << pattern; | 121 NOTREACHED() << "URLPattern is invalid: " << pattern; |
| 122 } | 122 } |
| 123 | 123 |
| 124 URLPattern::~URLPattern() { | 124 URLPattern::~URLPattern() { |
| 125 } | 125 } |
| 126 | 126 |
| 127 bool URLPattern::operator<(const URLPattern& other) const { |
| 128 return GetAsString() < other.GetAsString(); |
| 129 } |
| 130 |
| 131 bool URLPattern::operator==(const URLPattern& other) const { |
| 132 return GetAsString() == other.GetAsString(); |
| 133 } |
| 134 |
| 127 URLPattern::ParseResult URLPattern::Parse(const std::string& pattern, | 135 URLPattern::ParseResult URLPattern::Parse(const std::string& pattern, |
| 128 ParseOption strictness) { | 136 ParseOption strictness) { |
| 137 spec_.clear(); |
| 138 |
| 129 // Special case pattern to match every valid URL. | 139 // Special case pattern to match every valid URL. |
| 130 if (pattern == kAllUrlsPattern) { | 140 if (pattern == kAllUrlsPattern) { |
| 131 match_all_urls_ = true; | 141 match_all_urls_ = true; |
| 132 match_subdomains_ = true; | 142 match_subdomains_ = true; |
| 133 scheme_ = "*"; | 143 scheme_ = "*"; |
| 134 host_.clear(); | 144 host_.clear(); |
| 135 SetPath("/*"); | 145 SetPath("/*"); |
| 136 return PARSE_SUCCESS; | 146 return PARSE_SUCCESS; |
| 137 } | 147 } |
| 138 | 148 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 | 237 |
| 228 // No other '*' can occur in the host, though. This isn't necessary, but is | 238 // No other '*' can occur in the host, though. This isn't necessary, but is |
| 229 // done as a convenience to developers who might otherwise be confused and | 239 // done as a convenience to developers who might otherwise be confused and |
| 230 // think '*' works as a glob in the host. | 240 // think '*' works as a glob in the host. |
| 231 if (host_.find('*') != std::string::npos) | 241 if (host_.find('*') != std::string::npos) |
| 232 return PARSE_ERROR_INVALID_HOST_WILDCARD; | 242 return PARSE_ERROR_INVALID_HOST_WILDCARD; |
| 233 | 243 |
| 234 return PARSE_SUCCESS; | 244 return PARSE_SUCCESS; |
| 235 } | 245 } |
| 236 | 246 |
| 247 void URLPattern::SetValidSchemes(int valid_schemes) { |
| 248 spec_.clear(); |
| 249 valid_schemes_ = valid_schemes; |
| 250 } |
| 251 |
| 252 void URLPattern::SetHost(const std::string& host) { |
| 253 spec_.clear(); |
| 254 host_ = host; |
| 255 } |
| 256 |
| 257 void URLPattern::SetMatchAllURLs(bool val) { |
| 258 spec_.clear(); |
| 259 match_all_urls_ = val; |
| 260 } |
| 261 |
| 262 void URLPattern::SetMatchSubdomains(bool val) { |
| 263 spec_.clear(); |
| 264 match_subdomains_ = val; |
| 265 } |
| 266 |
| 237 bool URLPattern::SetScheme(const std::string& scheme) { | 267 bool URLPattern::SetScheme(const std::string& scheme) { |
| 268 spec_.clear(); |
| 238 scheme_ = scheme; | 269 scheme_ = scheme; |
| 239 if (scheme_ == "*") { | 270 if (scheme_ == "*") { |
| 240 valid_schemes_ &= (SCHEME_HTTP | SCHEME_HTTPS); | 271 valid_schemes_ &= (SCHEME_HTTP | SCHEME_HTTPS); |
| 241 } else if (!IsValidScheme(scheme_)) { | 272 } else if (!IsValidScheme(scheme_)) { |
| 242 return false; | 273 return false; |
| 243 } | 274 } |
| 244 return true; | 275 return true; |
| 245 } | 276 } |
| 246 | 277 |
| 247 bool URLPattern::IsValidScheme(const std::string& scheme) const { | 278 bool URLPattern::IsValidScheme(const std::string& scheme) const { |
| 248 if (valid_schemes_ == SCHEME_ALL) | 279 if (valid_schemes_ == SCHEME_ALL) |
| 249 return true; | 280 return true; |
| 250 | 281 |
| 251 for (size_t i = 0; i < arraysize(kValidSchemes); ++i) { | 282 for (size_t i = 0; i < arraysize(kValidSchemes); ++i) { |
| 252 if (scheme == kValidSchemes[i] && (valid_schemes_ & kValidSchemeMasks[i])) | 283 if (scheme == kValidSchemes[i] && (valid_schemes_ & kValidSchemeMasks[i])) |
| 253 return true; | 284 return true; |
| 254 } | 285 } |
| 255 | 286 |
| 256 return false; | 287 return false; |
| 257 } | 288 } |
| 258 | 289 |
| 259 void URLPattern::SetPath(const std::string& path) { | 290 void URLPattern::SetPath(const std::string& path) { |
| 291 spec_.clear(); |
| 260 path_ = path; | 292 path_ = path; |
| 261 path_escaped_ = path_; | 293 path_escaped_ = path_; |
| 262 ReplaceSubstringsAfterOffset(&path_escaped_, 0, "\\", "\\\\"); | 294 ReplaceSubstringsAfterOffset(&path_escaped_, 0, "\\", "\\\\"); |
| 263 ReplaceSubstringsAfterOffset(&path_escaped_, 0, "?", "\\?"); | 295 ReplaceSubstringsAfterOffset(&path_escaped_, 0, "?", "\\?"); |
| 264 } | 296 } |
| 265 | 297 |
| 266 bool URLPattern::SetPort(const std::string& port) { | 298 bool URLPattern::SetPort(const std::string& port) { |
| 299 spec_.clear(); |
| 267 if (IsValidPortForScheme(scheme_, port)) { | 300 if (IsValidPortForScheme(scheme_, port)) { |
| 268 port_ = port; | 301 port_ = port; |
| 269 return true; | 302 return true; |
| 270 } | 303 } |
| 271 return false; | 304 return false; |
| 272 } | 305 } |
| 273 | 306 |
| 274 bool URLPattern::MatchesURL(const GURL &test) const { | 307 bool URLPattern::MatchesURL(const GURL &test) const { |
| 275 if (!MatchesScheme(test.scheme())) | 308 if (!MatchesScheme(test.scheme())) |
| 276 return false; | 309 return false; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 return true; | 377 return true; |
| 345 } | 378 } |
| 346 | 379 |
| 347 bool URLPattern::MatchesPort(int port) const { | 380 bool URLPattern::MatchesPort(int port) const { |
| 348 if (port == url_parse::PORT_INVALID) | 381 if (port == url_parse::PORT_INVALID) |
| 349 return false; | 382 return false; |
| 350 | 383 |
| 351 return port_ == "*" || port_ == base::IntToString(port); | 384 return port_ == "*" || port_ == base::IntToString(port); |
| 352 } | 385 } |
| 353 | 386 |
| 354 std::string URLPattern::GetAsString() const { | 387 |
| 355 if (match_all_urls_) | 388 const std::string& URLPattern::GetAsString() const { |
| 356 return kAllUrlsPattern; | 389 if (!spec_.empty()) |
| 390 return spec_; |
| 391 |
| 392 if (match_all_urls_) { |
| 393 spec_ = kAllUrlsPattern; |
| 394 return spec_; |
| 395 } |
| 357 | 396 |
| 358 bool standard_scheme = IsStandardScheme(scheme_); | 397 bool standard_scheme = IsStandardScheme(scheme_); |
| 359 | 398 |
| 360 std::string spec = scheme_ + | 399 std::string spec = scheme_ + |
| 361 (standard_scheme ? chrome::kStandardSchemeSeparator : ":"); | 400 (standard_scheme ? chrome::kStandardSchemeSeparator : ":"); |
| 362 | 401 |
| 363 if (scheme_ != chrome::kFileScheme && standard_scheme) { | 402 if (scheme_ != chrome::kFileScheme && standard_scheme) { |
| 364 if (match_subdomains_) { | 403 if (match_subdomains_) { |
| 365 spec += "*"; | 404 spec += "*"; |
| 366 if (!host_.empty()) | 405 if (!host_.empty()) |
| 367 spec += "."; | 406 spec += "."; |
| 368 } | 407 } |
| 369 | 408 |
| 370 if (!host_.empty()) | 409 if (!host_.empty()) |
| 371 spec += host_; | 410 spec += host_; |
| 372 | 411 |
| 373 if (port_ != "*") { | 412 if (port_ != "*") { |
| 374 spec += ":"; | 413 spec += ":"; |
| 375 spec += port_; | 414 spec += port_; |
| 376 } | 415 } |
| 377 } | 416 } |
| 378 | 417 |
| 379 if (!path_.empty()) | 418 if (!path_.empty()) |
| 380 spec += path_; | 419 spec += path_; |
| 381 | 420 |
| 382 return spec; | 421 spec_ = spec; |
| 422 return spec_; |
| 383 } | 423 } |
| 384 | 424 |
| 385 bool URLPattern::OverlapsWith(const URLPattern& other) const { | 425 bool URLPattern::OverlapsWith(const URLPattern& other) const { |
| 386 if (!MatchesAnyScheme(other.GetExplicitSchemes()) && | 426 if (!MatchesAnyScheme(other.GetExplicitSchemes()) && |
| 387 !other.MatchesAnyScheme(GetExplicitSchemes())) { | 427 !other.MatchesAnyScheme(GetExplicitSchemes())) { |
| 388 return false; | 428 return false; |
| 389 } | 429 } |
| 390 | 430 |
| 391 if (!MatchesHost(other.host()) && !other.MatchesHost(host_)) | 431 if (!MatchesHost(other.host()) && !other.MatchesHost(host_)) |
| 392 return false; | 432 return false; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 } | 478 } |
| 439 | 479 |
| 440 std::vector<URLPattern> URLPattern::ConvertToExplicitSchemes() const { | 480 std::vector<URLPattern> URLPattern::ConvertToExplicitSchemes() const { |
| 441 std::vector<std::string> explicit_schemes = GetExplicitSchemes(); | 481 std::vector<std::string> explicit_schemes = GetExplicitSchemes(); |
| 442 std::vector<URLPattern> result; | 482 std::vector<URLPattern> result; |
| 443 | 483 |
| 444 for (std::vector<std::string>::const_iterator i = explicit_schemes.begin(); | 484 for (std::vector<std::string>::const_iterator i = explicit_schemes.begin(); |
| 445 i != explicit_schemes.end(); ++i) { | 485 i != explicit_schemes.end(); ++i) { |
| 446 URLPattern temp = *this; | 486 URLPattern temp = *this; |
| 447 temp.SetScheme(*i); | 487 temp.SetScheme(*i); |
| 448 temp.set_match_all_urls(false); | 488 temp.SetMatchAllURLs(false); |
| 449 result.push_back(temp); | 489 result.push_back(temp); |
| 450 } | 490 } |
| 451 | 491 |
| 452 return result; | 492 return result; |
| 453 } | 493 } |
| 454 | 494 |
| 455 // static | 495 // static |
| 456 const char* URLPattern::GetParseResultString( | 496 const char* URLPattern::GetParseResultString( |
| 457 URLPattern::ParseResult parse_result) { | 497 URLPattern::ParseResult parse_result) { |
| 458 return kParseResultMessages[parse_result]; | 498 return kParseResultMessages[parse_result]; |
| 459 } | 499 } |
| OLD | NEW |