Chromium Code Reviews| 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 "chrome/common/content_settings_pattern.h" | 5 #include "chrome/common/content_settings_pattern.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "chrome/common/content_settings_pattern_parser.h" | 12 #include "chrome/common/content_settings_pattern_parser.h" |
| 13 #include "chrome/common/render_messages.h" | 13 #include "chrome/common/render_messages.h" |
| 14 #include "chrome/common/url_constants.h" | |
| 15 #include "extensions/common/constants.h" | 14 #include "extensions/common/constants.h" |
| 16 #include "ipc/ipc_message_utils.h" | 15 #include "ipc/ipc_message_utils.h" |
| 17 #include "net/base/dns_util.h" | 16 #include "net/base/dns_util.h" |
| 18 #include "net/base/net_util.h" | 17 #include "net/base/net_util.h" |
| 19 #include "url/gurl.h" | |
| 20 #include "url/url_canon.h" | |
| 21 | 18 |
| 22 namespace { | 19 namespace { |
| 23 | 20 |
| 24 std::string GetDefaultPort(const std::string& scheme) { | 21 std::string GetDefaultPort(const std::string& scheme) { |
| 25 if (scheme == url::kHttpScheme) | 22 if (scheme == url::kHttpScheme) |
| 26 return "80"; | 23 return "80"; |
| 27 if (scheme == url::kHttpsScheme) | 24 if (scheme == url::kHttpsScheme) |
| 28 return "443"; | 25 return "443"; |
| 29 return std::string(); | 26 return std::string(); |
| 30 } | 27 } |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 78 return 0; | 75 return 0; |
| 79 } | 76 } |
| 80 | 77 |
| 81 typedef ContentSettingsPattern::BuilderInterface BuilderInterface; | 78 typedef ContentSettingsPattern::BuilderInterface BuilderInterface; |
| 82 | 79 |
| 83 } // namespace | 80 } // namespace |
| 84 | 81 |
| 85 // //////////////////////////////////////////////////////////////////////////// | 82 // //////////////////////////////////////////////////////////////////////////// |
| 86 // ContentSettingsPattern::Builder | 83 // ContentSettingsPattern::Builder |
| 87 // | 84 // |
| 85 class ContentSettingsPattern::Builder : | |
| 86 public ContentSettingsPattern::BuilderInterface { | |
| 87 public: | |
| 88 explicit Builder(bool use_legacy_validate); | |
| 89 virtual ~Builder(); | |
| 90 | |
| 91 // Overrides BuilderInterface | |
|
vabr (Chromium)
2014/08/06 15:14:00
nit: Overrides -> Implements ?
vasilii
2014/08/11 13:57:18
Done.
| |
| 92 virtual BuilderInterface* WithPort(const std::string& port) OVERRIDE; | |
| 93 virtual BuilderInterface* WithPortWildcard() OVERRIDE; | |
| 94 virtual BuilderInterface* WithHost(const std::string& host) OVERRIDE; | |
| 95 virtual BuilderInterface* WithDomainWildcard() OVERRIDE; | |
| 96 virtual BuilderInterface* WithScheme(const std::string& scheme) OVERRIDE; | |
| 97 virtual BuilderInterface* WithSchemeWildcard() OVERRIDE; | |
| 98 virtual BuilderInterface* WithPath(const std::string& path) OVERRIDE; | |
| 99 virtual BuilderInterface* WithPathWildcard() OVERRIDE; | |
| 100 virtual BuilderInterface* Invalid() OVERRIDE; | |
| 101 virtual ContentSettingsPattern Build() OVERRIDE; | |
| 102 | |
| 103 private: | |
| 104 // Canonicalizes the pattern parts so that they are ASCII only, either | |
| 105 // in original (if it was already ASCII) or punycode form. Returns true if | |
| 106 // the canonicalization was successful. | |
| 107 static bool Canonicalize(PatternParts* parts); | |
| 108 | |
| 109 // Returns true when the pattern |parts| represent a valid pattern. | |
| 110 static bool Validate(const PatternParts& parts); | |
| 111 | |
| 112 static bool LegacyValidate(const PatternParts& parts); | |
| 113 | |
| 114 bool is_valid_; | |
| 115 | |
| 116 bool use_legacy_validate_; | |
| 117 | |
| 118 PatternParts parts_; | |
| 119 | |
| 120 DISALLOW_COPY_AND_ASSIGN(Builder); | |
| 121 }; | |
| 122 | |
| 88 ContentSettingsPattern::Builder::Builder(bool use_legacy_validate) | 123 ContentSettingsPattern::Builder::Builder(bool use_legacy_validate) |
| 89 : is_valid_(true), | 124 : is_valid_(true), |
| 90 use_legacy_validate_(use_legacy_validate) {} | 125 use_legacy_validate_(use_legacy_validate) {} |
| 91 | 126 |
| 92 ContentSettingsPattern::Builder::~Builder() {} | 127 ContentSettingsPattern::Builder::~Builder() {} |
| 93 | 128 |
| 94 BuilderInterface* ContentSettingsPattern::Builder::WithPort( | 129 BuilderInterface* ContentSettingsPattern::Builder::WithPort( |
| 95 const std::string& port) { | 130 const std::string& port) { |
| 96 parts_.port = port; | 131 parts_.port = port; |
| 97 parts_.is_port_wildcard = false; | 132 parts_.is_port_wildcard = false; |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 304 // following patterns: | 339 // following patterns: |
| 305 // - [*.]domain.tld (matches domain.tld and all sub-domains) | 340 // - [*.]domain.tld (matches domain.tld and all sub-domains) |
| 306 // - host (matches an exact hostname) | 341 // - host (matches an exact hostname) |
| 307 // - a.b.c.d (matches an exact IPv4 ip) | 342 // - a.b.c.d (matches an exact IPv4 ip) |
| 308 // - [a:b:c:d:e:f:g:h] (matches an exact IPv6 ip) | 343 // - [a:b:c:d:e:f:g:h] (matches an exact IPv6 ip) |
| 309 // - file:///tmp/test.html (a complete URL without a host) | 344 // - file:///tmp/test.html (a complete URL without a host) |
| 310 // Version 2 adds a resource identifier for plugins. | 345 // Version 2 adds a resource identifier for plugins. |
| 311 // TODO(jochen): update once this feature is no longer behind a flag. | 346 // TODO(jochen): update once this feature is no longer behind a flag. |
| 312 const int ContentSettingsPattern::kContentSettingsPatternVersion = 1; | 347 const int ContentSettingsPattern::kContentSettingsPatternVersion = 1; |
| 313 | 348 |
| 314 // TODO(markusheintz): These two constants were moved to the Pattern Parser. | |
| 315 // Remove once the dependency of the ContentSettingsBaseProvider is removed. | |
| 316 const char* ContentSettingsPattern::kDomainWildcard = "[*.]"; | |
| 317 const size_t ContentSettingsPattern::kDomainWildcardLength = 4; | |
| 318 | |
| 319 // static | 349 // static |
| 320 BuilderInterface* ContentSettingsPattern::CreateBuilder( | 350 BuilderInterface* ContentSettingsPattern::CreateBuilder( |
| 321 bool validate) { | 351 bool validate) { |
| 322 return new Builder(validate); | 352 return new Builder(validate); |
| 323 } | 353 } |
| 324 | 354 |
| 325 // static | 355 // static |
| 326 ContentSettingsPattern ContentSettingsPattern::FromURL( | 356 ContentSettingsPattern ContentSettingsPattern::FromURL( |
| 327 const GURL& url) { | 357 const GURL& url) { |
| 328 scoped_ptr<ContentSettingsPattern::BuilderInterface> builder( | 358 scoped_ptr<ContentSettingsPattern::BuilderInterface> builder( |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 378 builder->WithPort(GetDefaultPort(local_url->scheme())); | 408 builder->WithPort(GetDefaultPort(local_url->scheme())); |
| 379 } else { | 409 } else { |
| 380 builder->WithPort(local_url->port()); | 410 builder->WithPort(local_url->port()); |
| 381 } | 411 } |
| 382 } | 412 } |
| 383 return builder->Build(); | 413 return builder->Build(); |
| 384 } | 414 } |
| 385 | 415 |
| 386 // static | 416 // static |
| 387 ContentSettingsPattern ContentSettingsPattern::FromString( | 417 ContentSettingsPattern ContentSettingsPattern::FromString( |
| 418 content_settings::ContentSettingsClient* client, | |
| 388 const std::string& pattern_spec) { | 419 const std::string& pattern_spec) { |
| 389 scoped_ptr<ContentSettingsPattern::BuilderInterface> builder( | 420 scoped_ptr<ContentSettingsPattern::BuilderInterface> builder( |
| 390 ContentSettingsPattern::CreateBuilder(false)); | 421 ContentSettingsPattern::CreateBuilder(false)); |
| 391 content_settings::PatternParser::Parse(pattern_spec, builder.get()); | 422 content_settings::PatternParser::Parse(client, pattern_spec, builder.get()); |
| 392 return builder->Build(); | 423 return builder->Build(); |
| 393 } | 424 } |
| 394 | 425 |
| 395 // static | |
| 396 ContentSettingsPattern ContentSettingsPattern::LegacyFromString( | |
| 397 const std::string& pattern_spec) { | |
| 398 scoped_ptr<ContentSettingsPattern::BuilderInterface> builder( | |
| 399 ContentSettingsPattern::CreateBuilder(true)); | |
| 400 content_settings::PatternParser::Parse(pattern_spec, builder.get()); | |
| 401 return builder->Build(); | |
| 402 } | |
| 403 | |
| 404 // static | 426 // static |
| 405 ContentSettingsPattern ContentSettingsPattern::Wildcard() { | 427 ContentSettingsPattern ContentSettingsPattern::Wildcard() { |
| 406 scoped_ptr<ContentSettingsPattern::BuilderInterface> builder( | 428 scoped_ptr<ContentSettingsPattern::BuilderInterface> builder( |
| 407 ContentSettingsPattern::CreateBuilder(true)); | 429 ContentSettingsPattern::CreateBuilder(true)); |
| 408 builder->WithSchemeWildcard()->WithDomainWildcard()->WithPortWildcard()-> | 430 builder->WithSchemeWildcard()->WithDomainWildcard()->WithPortWildcard()-> |
| 409 WithPathWildcard(); | 431 WithPathWildcard(); |
| 410 return builder->Build(); | 432 return builder->Build(); |
| 411 } | 433 } |
| 412 | 434 |
| 413 ContentSettingsPattern::ContentSettingsPattern() | 435 ContentSettingsPattern::ContentSettingsPattern() |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 490 return false; | 512 return false; |
| 491 } | 513 } |
| 492 | 514 |
| 493 return true; | 515 return true; |
| 494 } | 516 } |
| 495 | 517 |
| 496 bool ContentSettingsPattern::MatchesAllHosts() const { | 518 bool ContentSettingsPattern::MatchesAllHosts() const { |
| 497 return parts_.has_domain_wildcard && parts_.host.empty(); | 519 return parts_.has_domain_wildcard && parts_.host.empty(); |
| 498 } | 520 } |
| 499 | 521 |
| 500 const std::string ContentSettingsPattern::ToString() const { | 522 std::string ContentSettingsPattern::ToString( |
| 523 content_settings::ContentSettingsClient* client) const { | |
| 501 if (IsValid()) | 524 if (IsValid()) |
| 502 return content_settings::PatternParser::ToString(parts_); | 525 return content_settings::PatternParser::ToString(client, parts_); |
| 503 else | 526 else |
| 504 return std::string(); | 527 return std::string(); |
| 505 } | 528 } |
| 506 | 529 |
| 507 ContentSettingsPattern::Relation ContentSettingsPattern::Compare( | 530 ContentSettingsPattern::Relation ContentSettingsPattern::Compare( |
| 508 const ContentSettingsPattern& other) const { | 531 const ContentSettingsPattern& other) const { |
| 509 // Two invalid patterns are identical in the way they behave. They don't match | 532 // Two invalid patterns are identical in the way they behave. They don't match |
| 510 // anything and are represented as an empty string. So it's fair to treat them | 533 // anything and are represented as an empty string. So it's fair to treat them |
| 511 // as identical. | 534 // as identical. |
| 512 if ((this == &other) || | 535 if ((this == &other) || |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 674 if (!parts.is_port_wildcard && other_parts.is_port_wildcard) | 697 if (!parts.is_port_wildcard && other_parts.is_port_wildcard) |
| 675 return ContentSettingsPattern::PREDECESSOR; | 698 return ContentSettingsPattern::PREDECESSOR; |
| 676 | 699 |
| 677 int result = parts.port.compare(other_parts.port); | 700 int result = parts.port.compare(other_parts.port); |
| 678 if (result == 0) | 701 if (result == 0) |
| 679 return ContentSettingsPattern::IDENTITY; | 702 return ContentSettingsPattern::IDENTITY; |
| 680 if (result > 0) | 703 if (result > 0) |
| 681 return ContentSettingsPattern::DISJOINT_ORDER_PRE; | 704 return ContentSettingsPattern::DISJOINT_ORDER_PRE; |
| 682 return ContentSettingsPattern::DISJOINT_ORDER_POST; | 705 return ContentSettingsPattern::DISJOINT_ORDER_POST; |
| 683 } | 706 } |
| OLD | NEW |