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 // Keep it consistent with enum SchemeType in content_settings_pattern.h. |
| 25 const char* const kSchemeNames[] = { |
| 26 "wildcard", |
| 27 "other", |
| 28 url::kHttpScheme, |
| 29 url::kHttpsScheme, |
| 30 url::kFileScheme, |
| 31 "chrome-extension", |
| 32 }; |
| 33 |
| 34 static_assert(arraysize(kSchemeNames) == ContentSettingsPattern::SCHEME_MAX, |
| 35 "kSchemeNames should have SCHEME_MAX elements"); |
| 36 |
24 std::string GetDefaultPort(const std::string& scheme) { | 37 std::string GetDefaultPort(const std::string& scheme) { |
25 if (scheme == url::kHttpScheme) | 38 if (scheme == url::kHttpScheme) |
26 return "80"; | 39 return "80"; |
27 if (scheme == url::kHttpsScheme) | 40 if (scheme == url::kHttpsScheme) |
28 return "443"; | 41 return "443"; |
29 return std::string(); | 42 return std::string(); |
30 } | 43 } |
31 | 44 |
32 // Returns true if |sub_domain| is a sub domain or equals |domain|. E.g. | 45 // 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 | 46 // "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(); | 596 return parts_.has_domain_wildcard && parts_.host.empty(); |
584 } | 597 } |
585 | 598 |
586 std::string ContentSettingsPattern::ToString() const { | 599 std::string ContentSettingsPattern::ToString() const { |
587 if (IsValid()) | 600 if (IsValid()) |
588 return content_settings::PatternParser::ToString(parts_); | 601 return content_settings::PatternParser::ToString(parts_); |
589 else | 602 else |
590 return std::string(); | 603 return std::string(); |
591 } | 604 } |
592 | 605 |
| 606 ContentSettingsPattern::SchemeType ContentSettingsPattern::GetScheme() const { |
| 607 if (parts_.is_scheme_wildcard) |
| 608 return SCHEME_WILDCARD; |
| 609 |
| 610 for (size_t i = 2; i < arraysize(kSchemeNames); ++i) { |
| 611 if (parts_.scheme == kSchemeNames[i]) |
| 612 return static_cast<SchemeType>(i); |
| 613 } |
| 614 return SCHEME_OTHER; |
| 615 } |
| 616 |
593 ContentSettingsPattern::Relation ContentSettingsPattern::Compare( | 617 ContentSettingsPattern::Relation ContentSettingsPattern::Compare( |
594 const ContentSettingsPattern& other) const { | 618 const ContentSettingsPattern& other) const { |
595 // Two invalid patterns are identical in the way they behave. They don't match | 619 // 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 | 620 // anything and are represented as an empty string. So it's fair to treat them |
597 // as identical. | 621 // as identical. |
598 if ((this == &other) || | 622 if ((this == &other) || |
599 (!is_valid_ && !other.is_valid_)) | 623 (!is_valid_ && !other.is_valid_)) |
600 return IDENTITY; | 624 return IDENTITY; |
601 | 625 |
602 if (!is_valid_ && other.is_valid_) | 626 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) | 815 if (!parts.is_path_wildcard && other_parts.is_path_wildcard) |
792 return ContentSettingsPattern::PREDECESSOR; | 816 return ContentSettingsPattern::PREDECESSOR; |
793 | 817 |
794 int result = parts.path.compare(other_parts.path); | 818 int result = parts.path.compare(other_parts.path); |
795 if (result == 0) | 819 if (result == 0) |
796 return ContentSettingsPattern::IDENTITY; | 820 return ContentSettingsPattern::IDENTITY; |
797 if (result > 0) | 821 if (result > 0) |
798 return ContentSettingsPattern::DISJOINT_ORDER_PRE; | 822 return ContentSettingsPattern::DISJOINT_ORDER_PRE; |
799 return ContentSettingsPattern::DISJOINT_ORDER_POST; | 823 return ContentSettingsPattern::DISJOINT_ORDER_POST; |
800 } | 824 } |
OLD | NEW |