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 "components/content_settings/core/common/content_settings_pattern.h" | 5 #include "components/content_settings/core/common/content_settings_pattern.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
15 #include "components/content_settings/core/common/content_settings_pattern_parse
r.h" | 15 #include "components/content_settings/core/common/content_settings_pattern_parse
r.h" |
16 #include "net/base/url_util.h" | 16 #include "net/base/url_util.h" |
17 #include "url/gurl.h" | 17 #include "url/gurl.h" |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 // The component supports only one scheme for simplicity. | 21 // The component supports only one scheme for simplicity. |
22 const char* non_port_non_domain_wildcard_scheme = NULL; | 22 const char* non_port_non_domain_wildcard_scheme = NULL; |
23 | 23 |
| 24 const char* const kSchemeNames[] = { |
| 25 "wildcard", |
| 26 url::kHttpScheme, |
| 27 url::kHttpsScheme, |
| 28 url::kFileScheme, |
| 29 "chrome-extension", |
| 30 }; |
| 31 |
| 32 static_assert(arraysize(kSchemeNames) == ContentSettingsPattern::SCHEME_MAX - 1, |
| 33 "kSchemeNames should have SCHEME_MAX - 1 elements"); |
| 34 |
24 std::string GetDefaultPort(const std::string& scheme) { | 35 std::string GetDefaultPort(const std::string& scheme) { |
25 if (scheme == url::kHttpScheme) | 36 if (scheme == url::kHttpScheme) |
26 return "80"; | 37 return "80"; |
27 if (scheme == url::kHttpsScheme) | 38 if (scheme == url::kHttpsScheme) |
28 return "443"; | 39 return "443"; |
29 return std::string(); | 40 return std::string(); |
30 } | 41 } |
31 | 42 |
32 // Returns true if |sub_domain| is a sub domain or equals |domain|. E.g. | 43 // Returns true if |sub_domain| is a sub domain or equals |domain|. E.g. |
33 // "mail.google.com" is a sub domain of "google.com" but "evilhost.com" is not a | 44 // "mail.google.com" is a sub domain of "google.com" but "evilhost.com" is not a |
(...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
583 return parts_.has_domain_wildcard && parts_.host.empty(); | 594 return parts_.has_domain_wildcard && parts_.host.empty(); |
584 } | 595 } |
585 | 596 |
586 std::string ContentSettingsPattern::ToString() const { | 597 std::string ContentSettingsPattern::ToString() const { |
587 if (IsValid()) | 598 if (IsValid()) |
588 return content_settings::PatternParser::ToString(parts_); | 599 return content_settings::PatternParser::ToString(parts_); |
589 else | 600 else |
590 return std::string(); | 601 return std::string(); |
591 } | 602 } |
592 | 603 |
| 604 ContentSettingsPattern::SchemeType ContentSettingsPattern::GetScheme() const { |
| 605 if (parts_.is_scheme_wildcard) |
| 606 return SCHEME_WILDCARD; |
| 607 |
| 608 for (size_t i = 1; i < arraysize(kSchemeNames); ++i) { |
| 609 if (parts_.scheme == kSchemeNames[i]) |
| 610 return static_cast<SchemeType>(i); |
| 611 } |
| 612 return SCHEME_OTHER; |
| 613 } |
| 614 |
593 ContentSettingsPattern::Relation ContentSettingsPattern::Compare( | 615 ContentSettingsPattern::Relation ContentSettingsPattern::Compare( |
594 const ContentSettingsPattern& other) const { | 616 const ContentSettingsPattern& other) const { |
595 // Two invalid patterns are identical in the way they behave. They don't match | 617 // Two invalid patterns are identical in the way they behave. They don't match |
596 // anything and are represented as an empty string. So it's fair to treat them | 618 // anything and are represented as an empty string. So it's fair to treat them |
597 // as identical. | 619 // as identical. |
598 if ((this == &other) || | 620 if ((this == &other) || |
599 (!is_valid_ && !other.is_valid_)) | 621 (!is_valid_ && !other.is_valid_)) |
600 return IDENTITY; | 622 return IDENTITY; |
601 | 623 |
602 if (!is_valid_ && other.is_valid_) | 624 if (!is_valid_ && other.is_valid_) |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
791 if (!parts.is_path_wildcard && other_parts.is_path_wildcard) | 813 if (!parts.is_path_wildcard && other_parts.is_path_wildcard) |
792 return ContentSettingsPattern::PREDECESSOR; | 814 return ContentSettingsPattern::PREDECESSOR; |
793 | 815 |
794 int result = parts.path.compare(other_parts.path); | 816 int result = parts.path.compare(other_parts.path); |
795 if (result == 0) | 817 if (result == 0) |
796 return ContentSettingsPattern::IDENTITY; | 818 return ContentSettingsPattern::IDENTITY; |
797 if (result > 0) | 819 if (result > 0) |
798 return ContentSettingsPattern::DISJOINT_ORDER_PRE; | 820 return ContentSettingsPattern::DISJOINT_ORDER_PRE; |
799 return ContentSettingsPattern::DISJOINT_ORDER_POST; | 821 return ContentSettingsPattern::DISJOINT_ORDER_POST; |
800 } | 822 } |
OLD | NEW |