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_
12 #include "base/gtest_prod_util.h"
16 namespace content_settings
{
20 // A pattern used in content setting rules. See |IsValid| for a description of
22 class ContentSettingsPattern
{
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:
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.
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.
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.
49 DISJOINT_ORDER_POST
= -2,
53 DISJOINT_ORDER_PRE
= 2,
60 // Lowercase string of the URL scheme to match. This string is empty if the
61 // |is_scheme_wildcard| flag is set.
64 // True if the scheme wildcard is set.
65 bool is_scheme_wildcard
;
67 // Normalized string that is either of the following:
71 // - empty string if the |is_host_wildcard flag is set.
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.
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"
89 // True if the path wildcard is set.
90 bool is_path_wildcard
;
93 class BuilderInterface
{
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
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
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
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;
189 friend class content_settings::PatternParser
;
190 friend class ContentSettingsPatternSerializer
;
191 FRIEND_TEST_ALL_PREFIXES(ContentSettingsPatternParserTest
, SerializePatterns
);
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
);
214 #endif // COMPONENTS_CONTENT_SETTINGS_CORE_COMMON_CONTENT_SETTINGS_PATTERN_H_