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