| 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 // Patterns used in content setting rules. | 5 // Patterns used in content setting rules. |
| 6 | 6 |
| 7 #ifndef CHROME_COMMON_CONTENT_SETTINGS_PATTERN_H_ | 7 #ifndef CHROME_COMMON_CONTENT_SETTINGS_PATTERN_H_ |
| 8 #define CHROME_COMMON_CONTENT_SETTINGS_PATTERN_H_ | 8 #define CHROME_COMMON_CONTENT_SETTINGS_PATTERN_H_ |
| 9 | 9 |
| 10 #include <ostream> | |
| 11 #include <string> | 10 #include <string> |
| 12 | 11 |
| 13 #include "base/basictypes.h" | |
| 14 #include "base/compiler_specific.h" | |
| 15 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 16 | 13 |
| 17 class GURL; | 14 class GURL; |
| 18 class Pickle; | |
| 19 class PickleIterator; | |
| 20 | 15 |
| 21 namespace content_settings { | 16 namespace content_settings { |
| 22 class PatternParser; | 17 class PatternParser; |
| 23 } | 18 } |
| 24 | 19 |
| 25 namespace IPC { | |
| 26 class Message; | |
| 27 } | |
| 28 | |
| 29 // A pattern used in content setting rules. See |IsValid| for a description of | 20 // A pattern used in content setting rules. See |IsValid| for a description of |
| 30 // possible patterns. | 21 // possible patterns. |
| 31 class ContentSettingsPattern { | 22 class ContentSettingsPattern { |
| 32 public: | 23 public: |
| 33 // Each content settings pattern describes a set of origins. Patterns, and the | 24 // Each content settings pattern describes a set of origins. Patterns, and the |
| 34 // sets they describe, have specific relations. |Relation| describes the | 25 // sets they describe, have specific relations. |Relation| describes the |
| 35 // relation of two patterns A and B. When pattern A is compared with pattern B | 26 // relation of two patterns A and B. When pattern A is compared with pattern B |
| 36 // (A compare B) interesting relations are: | 27 // (A compare B) interesting relations are: |
| 37 // - IDENTITY: | 28 // - IDENTITY: |
| 38 // Pattern A and B are identical. The patterns are equal. | 29 // Pattern A and B are identical. The patterns are equal. |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 // Returns a content settings pattern according to the current configuration | 115 // Returns a content settings pattern according to the current configuration |
| 125 // of the builder. | 116 // of the builder. |
| 126 virtual ContentSettingsPattern Build() = 0; | 117 virtual ContentSettingsPattern Build() = 0; |
| 127 }; | 118 }; |
| 128 | 119 |
| 129 static BuilderInterface* CreateBuilder(bool use_legacy_validate); | 120 static BuilderInterface* CreateBuilder(bool use_legacy_validate); |
| 130 | 121 |
| 131 // The version of the pattern format implemented. | 122 // The version of the pattern format implemented. |
| 132 static const int kContentSettingsPatternVersion; | 123 static const int kContentSettingsPatternVersion; |
| 133 | 124 |
| 134 // The format of a domain wildcard. | |
| 135 static const char* kDomainWildcard; | |
| 136 | |
| 137 // The length of kDomainWildcard (without the trailing '\0'). | |
| 138 static const size_t kDomainWildcardLength; | |
| 139 | |
| 140 // Returns a wildcard content settings pattern that matches all possible valid | 125 // Returns a wildcard content settings pattern that matches all possible valid |
| 141 // origins. | 126 // origins. |
| 142 static ContentSettingsPattern Wildcard(); | 127 static ContentSettingsPattern Wildcard(); |
| 143 | 128 |
| 144 // Returns a pattern that matches the scheme and host of this URL, as well as | 129 // Returns a pattern that matches the scheme and host of this URL, as well as |
| 145 // all subdomains and ports. | 130 // all subdomains and ports. |
| 146 static ContentSettingsPattern FromURL(const GURL& url); | 131 static ContentSettingsPattern FromURL(const GURL& url); |
| 147 | 132 |
| 148 // Returns a pattern that matches exactly this URL. | 133 // Returns a pattern that matches exactly this URL. |
| 149 static ContentSettingsPattern FromURLNoWildcard(const GURL& url); | 134 static ContentSettingsPattern FromURLNoWildcard(const GURL& url); |
| 150 | 135 |
| 151 // Returns a pattern that matches the given pattern specification. | 136 // Returns a pattern that matches the given pattern specification. |
| 152 // Valid patterns specifications are: | 137 // Valid patterns specifications are: |
| 153 // - [*.]domain.tld (matches domain.tld and all sub-domains) | 138 // - [*.]domain.tld (matches domain.tld and all sub-domains) |
| 154 // - host (matches an exact hostname) | 139 // - host (matches an exact hostname) |
| 155 // - scheme://host:port (supported schemes: http,https) | 140 // - scheme://host:port (supported schemes: http,https) |
| 156 // - scheme://[*.]domain.tld:port (supported schemes: http,https) | 141 // - scheme://[*.]domain.tld:port (supported schemes: http,https) |
| 157 // - file://path (The path has to be an absolute path and start with a '/') | 142 // - file://path (The path has to be an absolute path and start with a '/') |
| 158 // - a.b.c.d (matches an exact IPv4 ip) | 143 // - a.b.c.d (matches an exact IPv4 ip) |
| 159 // - [a:b:c:d:e:f:g:h] (matches an exact IPv6 ip) | 144 // - [a:b:c:d:e:f:g:h] (matches an exact IPv6 ip) |
| 160 static ContentSettingsPattern FromString(const std::string& pattern_spec); | 145 static ContentSettingsPattern FromString(const std::string& pattern_spec); |
| 161 | 146 |
| 162 static ContentSettingsPattern LegacyFromString( | |
| 163 const std::string& pattern_spec); | |
| 164 | |
| 165 // Constructs an empty pattern. Empty patterns are invalid patterns. Invalid | 147 // Constructs an empty pattern. Empty patterns are invalid patterns. Invalid |
| 166 // patterns match nothing. | 148 // patterns match nothing. |
| 167 ContentSettingsPattern(); | 149 ContentSettingsPattern(); |
| 168 | 150 |
| 169 // Serializes the pattern to an IPC message or deserializes it. | |
| 170 void WriteToMessage(IPC::Message* m) const; | |
| 171 bool ReadFromMessage(const IPC::Message* m, PickleIterator* iter); | |
| 172 | |
| 173 // True if this is a valid pattern. | 151 // True if this is a valid pattern. |
| 174 bool IsValid() const { return is_valid_; } | 152 bool IsValid() const { return is_valid_; } |
| 175 | 153 |
| 176 // True if |url| matches this pattern. | 154 // True if |url| matches this pattern. |
| 177 bool Matches(const GURL& url) const; | 155 bool Matches(const GURL& url) const; |
| 178 | 156 |
| 179 // True if this pattern matches all hosts (i.e. it has a host wildcard). | 157 // True if this pattern matches all hosts (i.e. it has a host wildcard). |
| 180 bool MatchesAllHosts() const; | 158 bool MatchesAllHosts() const; |
| 181 | 159 |
| 182 // Returns a std::string representation of this pattern. | 160 // Returns a std::string representation of this pattern. |
| 183 const std::string ToString() const; | 161 std::string ToString() const; |
| 184 | 162 |
| 185 // Compares the pattern with a given |other| pattern and returns the | 163 // Compares the pattern with a given |other| pattern and returns the |
| 186 // |Relation| of the two patterns. | 164 // |Relation| of the two patterns. |
| 187 Relation Compare(const ContentSettingsPattern& other) const; | 165 Relation Compare(const ContentSettingsPattern& other) const; |
| 188 | 166 |
| 189 // Returns true if the pattern and the |other| pattern are identical. | 167 // Returns true if the pattern and the |other| pattern are identical. |
| 190 bool operator==(const ContentSettingsPattern& other) const; | 168 bool operator==(const ContentSettingsPattern& other) const; |
| 191 | 169 |
| 192 // Returns true if the pattern and the |other| pattern are not identical. | 170 // Returns true if the pattern and the |other| pattern are not identical. |
| 193 bool operator!=(const ContentSettingsPattern& other) const; | 171 bool operator!=(const ContentSettingsPattern& other) const; |
| 194 | 172 |
| 195 // Returns true if the pattern has a lower priority than the |other| pattern. | 173 // Returns true if the pattern has a lower priority than the |other| pattern. |
| 196 bool operator<(const ContentSettingsPattern& other) const; | 174 bool operator<(const ContentSettingsPattern& other) const; |
| 197 | 175 |
| 198 // Returns true if the pattern has a higher priority than the |other| pattern. | 176 // Returns true if the pattern has a higher priority than the |other| pattern. |
| 199 bool operator>(const ContentSettingsPattern& other) const; | 177 bool operator>(const ContentSettingsPattern& other) const; |
| 200 | 178 |
| 201 private: | 179 private: |
| 202 friend class content_settings::PatternParser; | 180 friend class content_settings::PatternParser; |
| 203 friend class Builder; | 181 friend class ContentSettingsPatternSerializer; |
| 204 FRIEND_TEST_ALL_PREFIXES(ContentSettingsPatternParserTest, SerializePatterns); | 182 FRIEND_TEST_ALL_PREFIXES(ContentSettingsPatternParserTest, SerializePatterns); |
| 205 | 183 |
| 206 class Builder : public BuilderInterface { | 184 class Builder; |
| 207 public: | |
| 208 explicit Builder(bool use_legacy_validate); | |
| 209 virtual ~Builder(); | |
| 210 | |
| 211 // Overrides BuilderInterface | |
| 212 virtual BuilderInterface* WithPort(const std::string& port) OVERRIDE; | |
| 213 | |
| 214 virtual BuilderInterface* WithPortWildcard() OVERRIDE; | |
| 215 | |
| 216 virtual BuilderInterface* WithHost(const std::string& host) OVERRIDE; | |
| 217 | |
| 218 virtual BuilderInterface* WithDomainWildcard() OVERRIDE; | |
| 219 | |
| 220 virtual BuilderInterface* WithScheme(const std::string& scheme) OVERRIDE; | |
| 221 | |
| 222 virtual BuilderInterface* WithSchemeWildcard() OVERRIDE; | |
| 223 | |
| 224 virtual BuilderInterface* WithPath(const std::string& path) OVERRIDE; | |
| 225 | |
| 226 virtual BuilderInterface* WithPathWildcard() OVERRIDE; | |
| 227 | |
| 228 virtual BuilderInterface* Invalid() OVERRIDE; | |
| 229 | |
| 230 virtual ContentSettingsPattern Build() OVERRIDE; | |
| 231 | |
| 232 private: | |
| 233 // Canonicalizes the pattern parts so that they are ASCII only, either | |
| 234 // in original (if it was already ASCII) or punycode form. Returns true if | |
| 235 // the canonicalization was successful. | |
| 236 static bool Canonicalize(PatternParts* parts); | |
| 237 | |
| 238 // Returns true when the pattern |parts| represent a valid pattern. | |
| 239 static bool Validate(const PatternParts& parts); | |
| 240 | |
| 241 static bool LegacyValidate(const PatternParts& parts); | |
| 242 | |
| 243 bool is_valid_; | |
| 244 | |
| 245 bool use_legacy_validate_; | |
| 246 | |
| 247 PatternParts parts_; | |
| 248 | |
| 249 DISALLOW_COPY_AND_ASSIGN(Builder); | |
| 250 }; | |
| 251 | 185 |
| 252 static Relation CompareScheme( | 186 static Relation CompareScheme( |
| 253 const ContentSettingsPattern::PatternParts& parts, | 187 const ContentSettingsPattern::PatternParts& parts, |
| 254 const ContentSettingsPattern::PatternParts& other_parts); | 188 const ContentSettingsPattern::PatternParts& other_parts); |
| 255 | 189 |
| 256 static Relation CompareHost( | 190 static Relation CompareHost( |
| 257 const ContentSettingsPattern::PatternParts& parts, | 191 const ContentSettingsPattern::PatternParts& parts, |
| 258 const ContentSettingsPattern::PatternParts& other_parts); | 192 const ContentSettingsPattern::PatternParts& other_parts); |
| 259 | 193 |
| 260 static Relation ComparePort( | 194 static Relation ComparePort( |
| 261 const ContentSettingsPattern::PatternParts& parts, | 195 const ContentSettingsPattern::PatternParts& parts, |
| 262 const ContentSettingsPattern::PatternParts& other_parts); | 196 const ContentSettingsPattern::PatternParts& other_parts); |
| 263 | 197 |
| 264 static bool Validate(const PatternParts& parts); | 198 static bool Validate(const PatternParts& parts); |
| 265 | 199 |
| 266 ContentSettingsPattern(const PatternParts& parts, bool valid); | 200 ContentSettingsPattern(const PatternParts& parts, bool valid); |
| 267 | 201 |
| 268 PatternParts parts_; | 202 PatternParts parts_; |
| 269 | 203 |
| 270 bool is_valid_; | 204 bool is_valid_; |
| 271 }; | 205 }; |
| 272 | 206 |
| 273 // Stream operator so ContentSettingsPattern can be used in assertion | |
| 274 // statements. | |
| 275 inline std::ostream& operator<<( | |
| 276 std::ostream& out, const ContentSettingsPattern& pattern) { | |
| 277 return out << pattern.ToString(); | |
| 278 } | |
| 279 | |
| 280 #endif // CHROME_COMMON_CONTENT_SETTINGS_PATTERN_H_ | 207 #endif // CHROME_COMMON_CONTENT_SETTINGS_PATTERN_H_ |
| OLD | NEW |