OLD | NEW |
| (Empty) |
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. | |
4 | |
5 // Patterns used in content setting rules. | |
6 | |
7 #ifndef CHROME_COMMON_CONTENT_SETTINGS_PATTERN_H_ | |
8 #define CHROME_COMMON_CONTENT_SETTINGS_PATTERN_H_ | |
9 | |
10 #include <string> | |
11 | |
12 #include "base/gtest_prod_util.h" | |
13 | |
14 class GURL; | |
15 | |
16 namespace content_settings { | |
17 class PatternParser; | |
18 } | |
19 | |
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. | |
30 // | |
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. | |
35 // | |
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. | |
40 // | |
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. | |
44 // | |
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, | |
54 }; | |
55 | |
56 struct PatternParts { | |
57 PatternParts(); | |
58 ~PatternParts(); | |
59 | |
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; | |
63 | |
64 // True if the scheme wildcard is set. | |
65 bool is_scheme_wildcard; | |
66 | |
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; | |
73 | |
74 // True if the domain wildcard is set. | |
75 bool has_domain_wildcard; | |
76 | |
77 // String with the port to match. This string is empty if the | |
78 // |is_port_wildcard| flag is set. | |
79 std::string port; | |
80 | |
81 // True if the port wildcard is set. | |
82 bool is_port_wildcard; | |
83 | |
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; | |
88 | |
89 // True if the path wildcard is set. | |
90 bool is_path_wildcard; | |
91 }; | |
92 | |
93 class BuilderInterface { | |
94 public: | |
95 virtual ~BuilderInterface() {} | |
96 | |
97 virtual BuilderInterface* WithPort(const std::string& port) = 0; | |
98 | |
99 virtual BuilderInterface* WithPortWildcard() = 0; | |
100 | |
101 virtual BuilderInterface* WithHost(const std::string& host) = 0; | |
102 | |
103 virtual BuilderInterface* WithDomainWildcard() = 0; | |
104 | |
105 virtual BuilderInterface* WithScheme(const std::string& scheme) = 0; | |
106 | |
107 virtual BuilderInterface* WithSchemeWildcard() = 0; | |
108 | |
109 virtual BuilderInterface* WithPath(const std::string& path) = 0; | |
110 | |
111 virtual BuilderInterface* WithPathWildcard() = 0; | |
112 | |
113 virtual BuilderInterface* Invalid() = 0; | |
114 | |
115 // Returns a content settings pattern according to the current configuration | |
116 // of the builder. | |
117 virtual ContentSettingsPattern Build() = 0; | |
118 }; | |
119 | |
120 static BuilderInterface* CreateBuilder(bool use_legacy_validate); | |
121 | |
122 // The version of the pattern format implemented. | |
123 static const int kContentSettingsPatternVersion; | |
124 | |
125 // Returns a wildcard content settings pattern that matches all possible valid | |
126 // origins. | |
127 static ContentSettingsPattern Wildcard(); | |
128 | |
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); | |
132 | |
133 // Returns a pattern that matches exactly this URL. | |
134 static ContentSettingsPattern FromURLNoWildcard(const GURL& url); | |
135 | |
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); | |
146 | |
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); | |
152 | |
153 // Compares |scheme| against the scheme set by the embedder. | |
154 static bool IsNonWildcardDomainNonPortScheme(const std::string& scheme); | |
155 | |
156 // Constructs an empty pattern. Empty patterns are invalid patterns. Invalid | |
157 // patterns match nothing. | |
158 ContentSettingsPattern(); | |
159 | |
160 // True if this is a valid pattern. | |
161 bool IsValid() const { return is_valid_; } | |
162 | |
163 // True if |url| matches this pattern. | |
164 bool Matches(const GURL& url) const; | |
165 | |
166 // True if this pattern matches all hosts (i.e. it has a host wildcard). | |
167 bool MatchesAllHosts() const; | |
168 | |
169 // Returns a std::string representation of this pattern. | |
170 std::string ToString() const; | |
171 | |
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; | |
175 | |
176 // Returns true if the pattern and the |other| pattern are identical. | |
177 bool operator==(const ContentSettingsPattern& other) const; | |
178 | |
179 // Returns true if the pattern and the |other| pattern are not identical. | |
180 bool operator!=(const ContentSettingsPattern& other) const; | |
181 | |
182 // Returns true if the pattern has a lower priority than the |other| pattern. | |
183 bool operator<(const ContentSettingsPattern& other) const; | |
184 | |
185 // Returns true if the pattern has a higher priority than the |other| pattern. | |
186 bool operator>(const ContentSettingsPattern& other) const; | |
187 | |
188 private: | |
189 friend class content_settings::PatternParser; | |
190 friend class ContentSettingsPatternSerializer; | |
191 FRIEND_TEST_ALL_PREFIXES(ContentSettingsPatternParserTest, SerializePatterns); | |
192 | |
193 class Builder; | |
194 | |
195 static Relation CompareScheme( | |
196 const ContentSettingsPattern::PatternParts& parts, | |
197 const ContentSettingsPattern::PatternParts& other_parts); | |
198 | |
199 static Relation CompareHost( | |
200 const ContentSettingsPattern::PatternParts& parts, | |
201 const ContentSettingsPattern::PatternParts& other_parts); | |
202 | |
203 static Relation ComparePort( | |
204 const ContentSettingsPattern::PatternParts& parts, | |
205 const ContentSettingsPattern::PatternParts& other_parts); | |
206 | |
207 ContentSettingsPattern(const PatternParts& parts, bool valid); | |
208 | |
209 PatternParts parts_; | |
210 | |
211 bool is_valid_; | |
212 }; | |
213 | |
214 #endif // CHROME_COMMON_CONTENT_SETTINGS_PATTERN_H_ | |
OLD | NEW |