| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Patterns used in content setting rules. | |
| 6 | |
| 7 #ifndef CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_PATTERN_H_ | |
| 8 #define CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_PATTERN_H_ | |
| 9 #pragma once | |
| 10 | |
| 11 #include <ostream> | |
| 12 #include <string> | |
| 13 | |
| 14 #include "base/basictypes.h" | |
| 15 | |
| 16 class GURL; | |
| 17 | |
| 18 namespace content_settings { | |
| 19 class PatternParser; | |
| 20 } | |
| 21 | |
| 22 // A pattern used in content setting rules. See |IsValid| for a description of | |
| 23 // possible patterns. | |
| 24 class ContentSettingsPattern { | |
| 25 public: | |
| 26 // Each content settings pattern describes a set of origins. Patterns, and the | |
| 27 // sets they describe, have specific relations. |Relation| describes the | |
| 28 // relation of two patterns A and B. When pattern A is compared with pattern B | |
| 29 // (A compare B) interesting relations are: | |
| 30 // - IDENTITY: | |
| 31 // Pattern A and B are identical. The patterns are equal. | |
| 32 // | |
| 33 // - DISJOINT_ORDER_PRE: | |
| 34 // Pattern A and B have no intersection. A and B never match the origin of | |
| 35 // a URL at the same time. But pattern A has a higher precedence than | |
| 36 // pattern B when patterns are sorted. | |
| 37 // | |
| 38 // - DISJOINT_ORDER_POST: | |
| 39 // Pattern A and B have no intersection. A and B never match the origin of | |
| 40 // a URL at the same time. But pattern A has a lower precedence than | |
| 41 // pattern B when patterns are sorted. | |
| 42 // | |
| 43 // - SUCCESSOR: | |
| 44 // Pattern A and B have an intersection. But pattern B has a higher | |
| 45 // precedence than pattern A for URLs that are matched by both pattern. | |
| 46 // | |
| 47 // - PREDECESSOR: | |
| 48 // Pattern A and B have an intersection. But pattern A has a higher | |
| 49 // precedence than pattern B for URLs that are matched by both pattern. | |
| 50 enum Relation { | |
| 51 DISJOINT_ORDER_POST = -2, | |
| 52 SUCCESSOR = -1, | |
| 53 IDENTITY = 0, | |
| 54 PREDECESSOR = 1, | |
| 55 DISJOINT_ORDER_PRE = 2, | |
| 56 }; | |
| 57 | |
| 58 class BuilderInterface { | |
| 59 public: | |
| 60 virtual ~BuilderInterface() {} | |
| 61 | |
| 62 virtual BuilderInterface* WithPort(const std::string& port) = 0; | |
| 63 | |
| 64 virtual BuilderInterface* WithPortWildcard() = 0; | |
| 65 | |
| 66 virtual BuilderInterface* WithHost(const std::string& host) = 0; | |
| 67 | |
| 68 virtual BuilderInterface* WithDomainWildcard() = 0; | |
| 69 | |
| 70 virtual BuilderInterface* WithScheme(const std::string& scheme) = 0; | |
| 71 | |
| 72 virtual BuilderInterface* WithSchemeWildcard() = 0; | |
| 73 | |
| 74 virtual BuilderInterface* WithPath(const std::string& path) = 0; | |
| 75 | |
| 76 virtual BuilderInterface* Invalid() = 0; | |
| 77 | |
| 78 // Returns a content settings pattern according to the current configuration | |
| 79 // of the builder. | |
| 80 virtual ContentSettingsPattern Build() = 0; | |
| 81 }; | |
| 82 | |
| 83 static BuilderInterface* CreateBuilder(bool use_legacy_validate); | |
| 84 | |
| 85 // The version of the pattern format implemented. | |
| 86 static const int kContentSettingsPatternVersion; | |
| 87 | |
| 88 // The format of a domain wildcard. | |
| 89 static const char* kDomainWildcard; | |
| 90 | |
| 91 // The length of kDomainWildcard (without the trailing '\0'). | |
| 92 static const size_t kDomainWildcardLength; | |
| 93 | |
| 94 // Returns a wildcard content settings pattern that matches all possible valid | |
| 95 // origins. | |
| 96 static ContentSettingsPattern Wildcard(); | |
| 97 | |
| 98 // Returns a pattern that matches the scheme and host of this URL, as well as | |
| 99 // all subdomains and ports. | |
| 100 static ContentSettingsPattern FromURL(const GURL& url); | |
| 101 | |
| 102 // Returns a pattern that matches exactly this URL. | |
| 103 static ContentSettingsPattern FromURLNoWildcard(const GURL& url); | |
| 104 | |
| 105 // Returns a pattern that matches the given pattern specification. | |
| 106 // Valid patterns specifications are: | |
| 107 // - [*.]domain.tld (matches domain.tld and all sub-domains) | |
| 108 // - host (matches an exact hostname) | |
| 109 // - scheme://host:port (supported schemes: http,https) | |
| 110 // - scheme://[*.]domain.tld:port (supported schemes: http,https) | |
| 111 // - file://path (The path has to be an absolute path and start with a '/') | |
| 112 // - a.b.c.d (matches an exact IPv4 ip) | |
| 113 // - [a:b:c:d:e:f:g:h] (matches an exact IPv6 ip) | |
| 114 static ContentSettingsPattern FromString(const std::string& pattern_spec); | |
| 115 | |
| 116 static ContentSettingsPattern LegacyFromString( | |
| 117 const std::string& pattern_spec); | |
| 118 | |
| 119 // Constructs an empty pattern. Empty patterns are invalid patterns. Invalid | |
| 120 // patterns match nothing. | |
| 121 ContentSettingsPattern(); | |
| 122 | |
| 123 // True if this is a valid pattern. | |
| 124 bool IsValid() const { return is_valid_; } | |
| 125 | |
| 126 // True if |url| matches this pattern. | |
| 127 bool Matches(const GURL& url) const; | |
| 128 | |
| 129 // Returns a std::string representation of this pattern. | |
| 130 const std::string ToString() const; | |
| 131 | |
| 132 // Compares the pattern with a given |other| pattern and returns the | |
| 133 // |Relation| of the two patterns. | |
| 134 Relation Compare(const ContentSettingsPattern& other) const; | |
| 135 | |
| 136 // Returns true if the pattern and the |other| pattern are identical. | |
| 137 bool operator==(const ContentSettingsPattern& other) const; | |
| 138 | |
| 139 // Returns true if the pattern and the |other| pattern are not identical. | |
| 140 bool operator!=(const ContentSettingsPattern& other) const; | |
| 141 | |
| 142 // Returns true if the pattern has a lower priority than the |other| pattern. | |
| 143 bool operator<(const ContentSettingsPattern& other) const; | |
| 144 | |
| 145 // Returns true if the pattern has a higher priority than the |other| pattern. | |
| 146 bool operator>(const ContentSettingsPattern& other) const; | |
| 147 | |
| 148 private: | |
| 149 friend class content_settings::PatternParser; | |
| 150 friend class ContentSettingsPatternParserTest_SerializePatterns_Test; | |
| 151 friend class Builder; | |
| 152 | |
| 153 struct PatternParts { | |
| 154 PatternParts(); | |
| 155 ~PatternParts(); | |
| 156 | |
| 157 // Lowercase string of the URL scheme to match. This string is empty if the | |
| 158 // |is_scheme_wildcard| flag is set. | |
| 159 std::string scheme; | |
| 160 | |
| 161 // True if the scheme wildcard is set. | |
| 162 bool is_scheme_wildcard; | |
| 163 | |
| 164 // Normalized string that is either of the following: | |
| 165 // - IPv4 or IPv6 | |
| 166 // - hostname | |
| 167 // - domain | |
| 168 // - empty string if the |is_host_wildcard flag is set. | |
| 169 std::string host; | |
| 170 | |
| 171 // True if the domain wildcard is set. | |
| 172 bool has_domain_wildcard; | |
| 173 | |
| 174 // String with the port to match. This string is empty if the | |
| 175 // |is_port_wildcard| flag is set. | |
| 176 std::string port; | |
| 177 | |
| 178 // True if the port wildcard is set. | |
| 179 bool is_port_wildcard; | |
| 180 | |
| 181 // TODO(markusheintz): Needed for legacy reasons. Remove. Path | |
| 182 // specification. Only used for content settings pattern with a "file" | |
| 183 // scheme part. | |
| 184 std::string path; | |
| 185 }; | |
| 186 | |
| 187 class Builder : public BuilderInterface { | |
| 188 public: | |
| 189 explicit Builder(bool use_legacy_validate); | |
| 190 virtual ~Builder(); | |
| 191 | |
| 192 // Overrides BuilderInterface | |
| 193 virtual BuilderInterface* WithPort(const std::string& port); | |
| 194 | |
| 195 virtual BuilderInterface* WithPortWildcard(); | |
| 196 | |
| 197 virtual BuilderInterface* WithHost(const std::string& host); | |
| 198 | |
| 199 virtual BuilderInterface* WithDomainWildcard(); | |
| 200 | |
| 201 virtual BuilderInterface* WithScheme(const std::string& scheme); | |
| 202 | |
| 203 virtual BuilderInterface* WithSchemeWildcard(); | |
| 204 | |
| 205 virtual BuilderInterface* WithPath(const std::string& path); | |
| 206 | |
| 207 virtual BuilderInterface* Invalid(); | |
| 208 | |
| 209 virtual ContentSettingsPattern Build(); | |
| 210 private: | |
| 211 // Canonicalizes the pattern parts so that they are ASCII only, either | |
| 212 // in original (if it was already ASCII) or punycode form. | |
| 213 static void Canonicalize(PatternParts* parts); | |
| 214 | |
| 215 // Returns true when the pattern |parts| represent a valid pattern. | |
| 216 static bool Validate(const PatternParts& parts); | |
| 217 | |
| 218 static bool LegacyValidate(const PatternParts& parts); | |
| 219 | |
| 220 bool is_valid_; | |
| 221 | |
| 222 bool use_legacy_validate_; | |
| 223 | |
| 224 PatternParts parts_; | |
| 225 | |
| 226 DISALLOW_COPY_AND_ASSIGN(Builder); | |
| 227 }; | |
| 228 | |
| 229 static Relation CompareScheme( | |
| 230 const ContentSettingsPattern::PatternParts& parts, | |
| 231 const ContentSettingsPattern::PatternParts& other_parts); | |
| 232 | |
| 233 static Relation CompareHost( | |
| 234 const ContentSettingsPattern::PatternParts& parts, | |
| 235 const ContentSettingsPattern::PatternParts& other_parts); | |
| 236 | |
| 237 static Relation ComparePort( | |
| 238 const ContentSettingsPattern::PatternParts& parts, | |
| 239 const ContentSettingsPattern::PatternParts& other_parts); | |
| 240 | |
| 241 static bool Validate(const PatternParts& parts); | |
| 242 | |
| 243 ContentSettingsPattern(const PatternParts& parts, bool valid); | |
| 244 | |
| 245 PatternParts parts_; | |
| 246 | |
| 247 bool is_valid_; | |
| 248 }; | |
| 249 | |
| 250 // Stream operator so ContentSettingsPattern can be used in assertion | |
| 251 // statements. | |
| 252 inline std::ostream& operator<<( | |
| 253 std::ostream& out, const ContentSettingsPattern& pattern) { | |
| 254 return out << pattern.ToString(); | |
| 255 } | |
| 256 | |
| 257 #endif // CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_PATTERN_H_ | |
| OLD | NEW |