OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "extensions/common/url_pattern.h" | 5 #include "extensions/common/url_pattern.h" |
6 | 6 |
7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
8 #include "base/strings/string_piece.h" | 8 #include "base/strings/string_piece.h" |
9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "base/strings/stringprintf.h" |
11 #include "content/public/common/url_constants.h" | 12 #include "content/public/common/url_constants.h" |
12 #include "extensions/common/constants.h" | 13 #include "extensions/common/constants.h" |
| 14 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
13 #include "url/gurl.h" | 15 #include "url/gurl.h" |
14 #include "url/url_util.h" | 16 #include "url/url_util.h" |
15 | 17 |
16 const char URLPattern::kAllUrlsPattern[] = "<all_urls>"; | 18 const char URLPattern::kAllUrlsPattern[] = "<all_urls>"; |
17 | 19 |
18 namespace { | 20 namespace { |
19 | 21 |
20 // TODO(aa): What about more obscure schemes like data: and javascript: ? | 22 // TODO(aa): What about more obscure schemes like data: and javascript: ? |
21 // Note: keep this array in sync with kValidSchemeMasks. | 23 // Note: keep this array in sync with kValidSchemeMasks. |
22 const char* kValidSchemes[] = { | 24 const char* kValidSchemes[] = { |
(...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 if (test.host().length() <= (host_.length() + 1)) | 407 if (test.host().length() <= (host_.length() + 1)) |
406 return false; | 408 return false; |
407 | 409 |
408 if (test.host().compare(test.host().length() - host_.length(), | 410 if (test.host().compare(test.host().length() - host_.length(), |
409 host_.length(), host_) != 0) | 411 host_.length(), host_) != 0) |
410 return false; | 412 return false; |
411 | 413 |
412 return test.host()[test.host().length() - host_.length() - 1] == '.'; | 414 return test.host()[test.host().length() - host_.length() - 1] == '.'; |
413 } | 415 } |
414 | 416 |
| 417 bool URLPattern::ImpliesAllHosts() const { |
| 418 // Check if it matches all urls or is a pattern like http://*/*. |
| 419 if (match_all_urls_ || |
| 420 (match_subdomains_ && host_.empty() && port_ == "*" && path_ == "/*")) { |
| 421 return true; |
| 422 } |
| 423 |
| 424 // If this doesn't even match subdomains, it can't possibly imply all hosts. |
| 425 if (!match_subdomains_) |
| 426 return false; |
| 427 |
| 428 // If |host_| is a recognized TLD, this will be 0. We don't include private |
| 429 // TLDs, so that, e.g., *.appspot.com does not imply all hosts. |
| 430 size_t registry_length = net::registry_controlled_domains::GetRegistryLength( |
| 431 host_, |
| 432 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, |
| 433 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); |
| 434 // If there was more than just a TLD in the host (e.g., *.foobar.com), it |
| 435 // doesn't imply all hosts. |
| 436 if (registry_length > 0) |
| 437 return false; |
| 438 |
| 439 // At this point the host could either be just a TLD ("com") or some unknown |
| 440 // TLD-like string ("notatld"). To disambiguate between them construct a |
| 441 // fake URL, and check the registry. This returns 0 if the TLD is |
| 442 // unrecognized, or the length of the recognized TLD. |
| 443 registry_length = net::registry_controlled_domains::GetRegistryLength( |
| 444 base::StringPrintf("foo.%s", host_.c_str()), |
| 445 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, |
| 446 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); |
| 447 // If we recognized this TLD, then this is a pattern like *.com, and it |
| 448 // should imply all hosts. Otherwise, this doesn't imply all hosts. |
| 449 return registry_length > 0; |
| 450 } |
| 451 |
415 bool URLPattern::MatchesPath(const std::string& test) const { | 452 bool URLPattern::MatchesPath(const std::string& test) const { |
416 // Make the behaviour of OverlapsWith consistent with MatchesURL, which is | 453 // Make the behaviour of OverlapsWith consistent with MatchesURL, which is |
417 // need to match hosted apps on e.g. 'google.com' also run on 'google.com/'. | 454 // need to match hosted apps on e.g. 'google.com' also run on 'google.com/'. |
418 if (test + "/*" == path_escaped_) | 455 if (test + "/*" == path_escaped_) |
419 return true; | 456 return true; |
420 | 457 |
421 return MatchPattern(test, path_escaped_); | 458 return MatchPattern(test, path_escaped_); |
422 } | 459 } |
423 | 460 |
424 const std::string& URLPattern::GetAsString() const { | 461 const std::string& URLPattern::GetAsString() const { |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
545 } | 582 } |
546 | 583 |
547 return result; | 584 return result; |
548 } | 585 } |
549 | 586 |
550 // static | 587 // static |
551 const char* URLPattern::GetParseResultString( | 588 const char* URLPattern::GetParseResultString( |
552 URLPattern::ParseResult parse_result) { | 589 URLPattern::ParseResult parse_result) { |
553 return kParseResultMessages[parse_result]; | 590 return kParseResultMessages[parse_result]; |
554 } | 591 } |
OLD | NEW |