Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / content_settings / core / common / content_settings_pattern.h
blob70b17a501a87f2ed021fee62e5520f9e0c2cd860
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
3 // found in the LICENSE file.
5 // Patterns used in content setting rules.
7 #ifndef COMPONENTS_CONTENT_SETTINGS_CORE_COMMON_CONTENT_SETTINGS_PATTERN_H_
8 #define COMPONENTS_CONTENT_SETTINGS_CORE_COMMON_CONTENT_SETTINGS_PATTERN_H_
10 #include <string>
12 #include "base/gtest_prod_util.h"
14 class GURL;
16 namespace content_settings {
17 class PatternParser;
20 // A pattern used in content setting rules. See |IsValid| for a description of
21 // possible patterns.
22 class ContentSettingsPattern {
23 public:
24 // Each content settings pattern describes a set of origins. Patterns, and the
25 // sets they describe, have specific relations. |Relation| describes the
26 // relation of two patterns A and B. When pattern A is compared with pattern B
27 // (A compare B) interesting relations are:
28 // - IDENTITY:
29 // Pattern A and B are identical. The patterns are equal.
31 // - DISJOINT_ORDER_PRE:
32 // Pattern A and B have no intersection. A and B never match the origin of
33 // a URL at the same time. But pattern A has a higher precedence than
34 // pattern B when patterns are sorted.
36 // - DISJOINT_ORDER_POST:
37 // Pattern A and B have no intersection. A and B never match the origin of
38 // a URL at the same time. But pattern A has a lower precedence than
39 // pattern B when patterns are sorted.
41 // - SUCCESSOR:
42 // Pattern A and B have an intersection. But pattern B has a higher
43 // precedence than pattern A for URLs that are matched by both pattern.
45 // - PREDECESSOR:
46 // Pattern A and B have an intersection. But pattern A has a higher
47 // precedence than pattern B for URLs that are matched by both pattern.
48 enum Relation {
49 DISJOINT_ORDER_POST = -2,
50 SUCCESSOR = -1,
51 IDENTITY = 0,
52 PREDECESSOR = 1,
53 DISJOINT_ORDER_PRE = 2,
56 struct PatternParts {
57 PatternParts();
58 ~PatternParts();
60 // Lowercase string of the URL scheme to match. This string is empty if the
61 // |is_scheme_wildcard| flag is set.
62 std::string scheme;
64 // True if the scheme wildcard is set.
65 bool is_scheme_wildcard;
67 // Normalized string that is either of the following:
68 // - IPv4 or IPv6
69 // - hostname
70 // - domain
71 // - empty string if the |is_host_wildcard flag is set.
72 std::string host;
74 // True if the domain wildcard is set.
75 bool has_domain_wildcard;
77 // String with the port to match. This string is empty if the
78 // |is_port_wildcard| flag is set.
79 std::string port;
81 // True if the port wildcard is set.
82 bool is_port_wildcard;
84 // TODO(markusheintz): Needed for legacy reasons. Remove. Path
85 // specification. Only used for content settings pattern with a "file"
86 // scheme part.
87 std::string path;
89 // True if the path wildcard is set.
90 bool is_path_wildcard;
93 class BuilderInterface {
94 public:
95 virtual ~BuilderInterface() {}
97 virtual BuilderInterface* WithPort(const std::string& port) = 0;
99 virtual BuilderInterface* WithPortWildcard() = 0;
101 virtual BuilderInterface* WithHost(const std::string& host) = 0;
103 virtual BuilderInterface* WithDomainWildcard() = 0;
105 virtual BuilderInterface* WithScheme(const std::string& scheme) = 0;
107 virtual BuilderInterface* WithSchemeWildcard() = 0;
109 virtual BuilderInterface* WithPath(const std::string& path) = 0;
111 virtual BuilderInterface* WithPathWildcard() = 0;
113 virtual BuilderInterface* Invalid() = 0;
115 // Returns a content settings pattern according to the current configuration
116 // of the builder.
117 virtual ContentSettingsPattern Build() = 0;
120 static BuilderInterface* CreateBuilder(bool use_legacy_validate);
122 // The version of the pattern format implemented.
123 static const int kContentSettingsPatternVersion;
125 // Returns a wildcard content settings pattern that matches all possible valid
126 // origins.
127 static ContentSettingsPattern Wildcard();
129 // Returns a pattern that matches the scheme and host of this URL, as well as
130 // all subdomains and ports.
131 static ContentSettingsPattern FromURL(const GURL& url);
133 // Returns a pattern that matches exactly this URL.
134 static ContentSettingsPattern FromURLNoWildcard(const GURL& url);
136 // Returns a pattern that matches the given pattern specification.
137 // Valid patterns specifications are:
138 // - [*.]domain.tld (matches domain.tld and all sub-domains)
139 // - host (matches an exact hostname)
140 // - scheme://host:port (supported schemes: http,https)
141 // - scheme://[*.]domain.tld:port (supported schemes: http,https)
142 // - file://path (The path has to be an absolute path and start with a '/')
143 // - a.b.c.d (matches an exact IPv4 ip)
144 // - [a:b:c:d:e:f:g:h] (matches an exact IPv6 ip)
145 static ContentSettingsPattern FromString(const std::string& pattern_spec);
147 // Sets the scheme that doesn't support domain wildcard and port.
148 // Needs to be called by the embedder before using ContentSettingsPattern.
149 // |scheme| can't be NULL, and the pointed string must remain alive until the
150 // app terminates.
151 static void SetNonWildcardDomainNonPortScheme(const char* scheme);
153 // Compares |scheme| against the scheme set by the embedder.
154 static bool IsNonWildcardDomainNonPortScheme(const std::string& scheme);
156 // Constructs an empty pattern. Empty patterns are invalid patterns. Invalid
157 // patterns match nothing.
158 ContentSettingsPattern();
160 // True if this is a valid pattern.
161 bool IsValid() const { return is_valid_; }
163 // True if |url| matches this pattern.
164 bool Matches(const GURL& url) const;
166 // True if this pattern matches all hosts (i.e. it has a host wildcard).
167 bool MatchesAllHosts() const;
169 // Returns a std::string representation of this pattern.
170 std::string ToString() const;
172 // Compares the pattern with a given |other| pattern and returns the
173 // |Relation| of the two patterns.
174 Relation Compare(const ContentSettingsPattern& other) const;
176 // Returns true if the pattern and the |other| pattern are identical.
177 bool operator==(const ContentSettingsPattern& other) const;
179 // Returns true if the pattern and the |other| pattern are not identical.
180 bool operator!=(const ContentSettingsPattern& other) const;
182 // Returns true if the pattern has a lower priority than the |other| pattern.
183 bool operator<(const ContentSettingsPattern& other) const;
185 // Returns true if the pattern has a higher priority than the |other| pattern.
186 bool operator>(const ContentSettingsPattern& other) const;
188 private:
189 friend class content_settings::PatternParser;
190 friend class ContentSettingsPatternSerializer;
191 FRIEND_TEST_ALL_PREFIXES(ContentSettingsPatternParserTest, SerializePatterns);
193 class Builder;
195 static Relation CompareScheme(
196 const ContentSettingsPattern::PatternParts& parts,
197 const ContentSettingsPattern::PatternParts& other_parts);
199 static Relation CompareHost(
200 const ContentSettingsPattern::PatternParts& parts,
201 const ContentSettingsPattern::PatternParts& other_parts);
203 static Relation ComparePort(
204 const ContentSettingsPattern::PatternParts& parts,
205 const ContentSettingsPattern::PatternParts& other_parts);
207 ContentSettingsPattern(const PatternParts& parts, bool valid);
209 PatternParts parts_;
211 bool is_valid_;
214 #endif // COMPONENTS_CONTENT_SETTINGS_CORE_COMMON_CONTENT_SETTINGS_PATTERN_H_