PageRenderTime 21ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/components/content_settings/core/common/content_settings_pattern.h

http://github.com/chromium/chromium
C Header | 265 lines | 111 code | 61 blank | 93 comment | 0 complexity | 9e323c9c1f32c409900084408fe024a4 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.0, BSD-2-Clause, LGPL-2.1, MPL-2.0, 0BSD, EPL-1.0, MPL-2.0-no-copyleft-exception, GPL-2.0, BitTorrent-1.0, CPL-1.0, LGPL-3.0, Unlicense, BSD-3-Clause, CC0-1.0, JSON, MIT, GPL-3.0, CC-BY-SA-3.0, AGPL-1.0
  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. // Patterns used in content setting rules.
  5. #ifndef COMPONENTS_CONTENT_SETTINGS_CORE_COMMON_CONTENT_SETTINGS_PATTERN_H_
  6. #define COMPONENTS_CONTENT_SETTINGS_CORE_COMMON_CONTENT_SETTINGS_PATTERN_H_
  7. #include <memory>
  8. #include <string>
  9. #include "base/gtest_prod_util.h"
  10. #include "base/strings/string_piece_forward.h"
  11. #include "mojo/public/cpp/bindings/struct_traits.h"
  12. class GURL;
  13. namespace content_settings {
  14. class PatternParser;
  15. namespace mojom {
  16. class ContentSettingsPatternDataView;
  17. }
  18. }
  19. // A pattern used in content setting rules. See |IsValid| for a description of
  20. // possible patterns.
  21. class ContentSettingsPattern {
  22. public:
  23. // Each content settings pattern describes a set of origins. Patterns, and the
  24. // sets they describe, have specific relations. |Relation| describes the
  25. // relation of two patterns A and B. When pattern A is compared with pattern B
  26. // (A compare B) interesting relations are:
  27. // - IDENTITY:
  28. // Pattern A and B are identical. The patterns are equal.
  29. //
  30. // - DISJOINT_ORDER_PRE:
  31. // Pattern A and B have no intersection. A and B never match the origin of
  32. // a URL at the same time. But pattern A has a higher precedence than
  33. // pattern B when patterns are sorted.
  34. //
  35. // - DISJOINT_ORDER_POST:
  36. // Pattern A and B have no intersection. A and B never match the origin of
  37. // a URL at the same time. But pattern A has a lower precedence than
  38. // pattern B when patterns are sorted.
  39. //
  40. // - SUCCESSOR:
  41. // Pattern A and B have an intersection. But pattern B has a higher
  42. // precedence than pattern A for URLs that are matched by both pattern.
  43. //
  44. // - PREDECESSOR:
  45. // Pattern A and B have an intersection. But pattern A has a higher
  46. // precedence than pattern B for URLs that are matched by both pattern.
  47. enum Relation {
  48. DISJOINT_ORDER_POST = -2,
  49. SUCCESSOR = -1,
  50. IDENTITY = 0,
  51. PREDECESSOR = 1,
  52. DISJOINT_ORDER_PRE = 2,
  53. };
  54. // This enum is used to back an UMA histogram, the order of existing values
  55. // should not be changed.
  56. // New values should only be appended before SCHEME_MAX.
  57. // Also keep it consistent with kSchemeNames in content_settings_pattern.cc,
  58. // and the ContentSettingScheme enum in histograms/enums.xml.
  59. enum SchemeType {
  60. SCHEME_WILDCARD,
  61. SCHEME_OTHER,
  62. SCHEME_HTTP,
  63. SCHEME_HTTPS,
  64. SCHEME_FILE,
  65. SCHEME_CHROMEEXTENSION,
  66. SCHEME_CHROMESEARCH,
  67. SCHEME_CHROME,
  68. SCHEME_CHROMEUNTRUSTED,
  69. SCHEME_MAX,
  70. };
  71. struct PatternParts {
  72. PatternParts();
  73. PatternParts(const PatternParts& other);
  74. PatternParts(PatternParts&& other);
  75. ~PatternParts();
  76. PatternParts& operator=(const PatternParts& other);
  77. PatternParts& operator=(PatternParts&& other);
  78. // Lowercase string of the URL scheme to match. This string is empty if the
  79. // |is_scheme_wildcard| flag is set.
  80. std::string scheme;
  81. // True if the scheme wildcard is set.
  82. bool is_scheme_wildcard;
  83. // Normalized string that is either of the following:
  84. // - IPv4 or IPv6
  85. // - hostname
  86. // - domain
  87. // - empty string if the |is_host_wildcard| flag is set.
  88. std::string host;
  89. // True if the domain wildcard is set.
  90. bool has_domain_wildcard;
  91. // String with the port to match. This string is empty if the
  92. // |is_port_wildcard| flag is set.
  93. std::string port;
  94. // True if the port wildcard is set.
  95. bool is_port_wildcard;
  96. // TODO(markusheintz): Needed for legacy reasons. Remove. Path
  97. // specification. Only used for content settings pattern with a "file"
  98. // scheme part.
  99. std::string path;
  100. // True if the path wildcard is set.
  101. bool is_path_wildcard;
  102. };
  103. class BuilderInterface {
  104. public:
  105. virtual ~BuilderInterface() {}
  106. virtual BuilderInterface* WithPort(const std::string& port) = 0;
  107. virtual BuilderInterface* WithPortWildcard() = 0;
  108. virtual BuilderInterface* WithHost(const std::string& host) = 0;
  109. virtual BuilderInterface* WithDomainWildcard() = 0;
  110. virtual BuilderInterface* WithScheme(const std::string& scheme) = 0;
  111. virtual BuilderInterface* WithSchemeWildcard() = 0;
  112. virtual BuilderInterface* WithPath(const std::string& path) = 0;
  113. virtual BuilderInterface* WithPathWildcard() = 0;
  114. virtual BuilderInterface* Invalid() = 0;
  115. // Returns a content settings pattern according to the current configuration
  116. // of the builder.
  117. virtual ContentSettingsPattern Build() = 0;
  118. };
  119. static std::unique_ptr<BuilderInterface> CreateBuilder();
  120. // The version of the pattern format implemented.
  121. static const int kContentSettingsPatternVersion;
  122. // Returns a wildcard content settings pattern that matches all possible valid
  123. // origins.
  124. static ContentSettingsPattern Wildcard();
  125. // Returns a pattern that matches the scheme and host of this URL, as well as
  126. // all subdomains and ports.
  127. static ContentSettingsPattern FromURL(const GURL& url);
  128. // Returns a pattern that matches exactly this URL.
  129. static ContentSettingsPattern FromURLNoWildcard(const GURL& url);
  130. // Returns a pattern that matches the given pattern specification.
  131. // Valid patterns specifications are:
  132. // - [*.]domain.tld (matches domain.tld and all sub-domains)
  133. // - host (matches an exact hostname)
  134. // - scheme://host:port (supported schemes: http,https)
  135. // - scheme://[*.]domain.tld:port (supported schemes: http,https)
  136. // - file://path (The path has to be an absolute path and start with a '/')
  137. // - a.b.c.d (matches an exact IPv4 ip)
  138. // - [a:b:c:d:e:f:g:h] (matches an exact IPv6 ip)
  139. static ContentSettingsPattern FromString(base::StringPiece pattern_spec);
  140. // Sets schemes that do not support domain wildcards and ports.
  141. // Needs to be called by the embedder before using ContentSettingsPattern.
  142. // |schemes| can't be NULL, and the pointed to strings must remain alive
  143. // until the app terminates.
  144. // The method should only be called once. If called again, the parameters
  145. // must have values equal to the parameter values of the first call.
  146. // The |count| parameter represents the number of strings that
  147. // |schemes| points to.
  148. static void SetNonWildcardDomainNonPortSchemes(const char* const* schemes,
  149. size_t count);
  150. // Compares |scheme| against the schemes set by the embedder.
  151. static bool IsNonWildcardDomainNonPortScheme(base::StringPiece scheme);
  152. // Constructs an empty pattern. Empty patterns are invalid patterns. Invalid
  153. // patterns match nothing.
  154. ContentSettingsPattern();
  155. // True if this is a valid pattern.
  156. bool IsValid() const { return is_valid_; }
  157. // True if |url| matches this pattern.
  158. bool Matches(const GURL& url) const;
  159. // True if this pattern matches all hosts (i.e. it has a host wildcard).
  160. bool MatchesAllHosts() const;
  161. // Returns a std::string representation of this pattern.
  162. std::string ToString() const;
  163. // Returns scheme type of pattern.
  164. ContentSettingsPattern::SchemeType GetScheme() const;
  165. // Returns the host of a pattern.
  166. const std::string& GetHost() const;
  167. // True if this pattern has a non-empty path. Can only be used for patterns
  168. // with file: schemes.
  169. bool HasPath() const;
  170. // Compares the pattern with a given |other| pattern and returns the
  171. // |Relation| of the two patterns.
  172. Relation Compare(const ContentSettingsPattern& other) const;
  173. // Returns true if the pattern and the |other| pattern are identical.
  174. bool operator==(const ContentSettingsPattern& other) const;
  175. // Returns true if the pattern and the |other| pattern are not identical.
  176. bool operator!=(const ContentSettingsPattern& other) const;
  177. // Returns true if the pattern has a lower priority than the |other| pattern.
  178. bool operator<(const ContentSettingsPattern& other) const;
  179. // Returns true if the pattern has a higher priority than the |other| pattern.
  180. bool operator>(const ContentSettingsPattern& other) const;
  181. private:
  182. friend class content_settings::PatternParser;
  183. friend struct mojo::StructTraits<
  184. content_settings::mojom::ContentSettingsPatternDataView,
  185. ContentSettingsPattern>;
  186. FRIEND_TEST_ALL_PREFIXES(ContentSettingsPatternParserTest, SerializePatterns);
  187. class Builder;
  188. static Relation CompareScheme(
  189. const ContentSettingsPattern::PatternParts& parts,
  190. const ContentSettingsPattern::PatternParts& other_parts);
  191. static Relation CompareHost(
  192. const ContentSettingsPattern::PatternParts& parts,
  193. const ContentSettingsPattern::PatternParts& other_parts);
  194. static Relation ComparePort(
  195. const ContentSettingsPattern::PatternParts& parts,
  196. const ContentSettingsPattern::PatternParts& other_parts);
  197. static Relation ComparePath(
  198. const ContentSettingsPattern::PatternParts& parts,
  199. const ContentSettingsPattern::PatternParts& other_parts);
  200. ContentSettingsPattern(PatternParts parts, bool valid);
  201. PatternParts parts_;
  202. bool is_valid_;
  203. };
  204. #endif // COMPONENTS_CONTENT_SETTINGS_CORE_COMMON_CONTENT_SETTINGS_PATTERN_H_