1// Copyright (c) 2011 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_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_PATTERN_H_ 8#define CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_PATTERN_H_ 9#pragma once 10 11#include <ostream> 12#include <string> 13 14class GURL; 15 16// A pattern used in content setting rules. See |IsValid| for a description of 17// possible patterns. 18class ContentSettingsPattern { 19 public: 20 // Returns a pattern that matches the host of this URL and all subdomains. 21 static ContentSettingsPattern FromURL(const GURL& url); 22 23 // Returns a pattern that matches exactly this URL. 24 static ContentSettingsPattern FromURLNoWildcard(const GURL& url); 25 26 ContentSettingsPattern() {} 27 28 explicit ContentSettingsPattern(const std::string& pattern) 29 : pattern_(pattern), 30 scheme_("") {} 31 32 // True if this is a valid pattern. Valid patterns are 33 // - [*.]domain.tld (matches domain.tld and all sub-domains) 34 // - host (matches an exact hostname) 35 // - a.b.c.d (matches an exact IPv4 ip) 36 // - [a:b:c:d:e:f:g:h] (matches an exact IPv6 ip) 37 // TODO(jochen): should also return true for a complete URL without a host. 38 bool IsValid() const; 39 40 // True if |url| matches this pattern. 41 bool Matches(const GURL& url) const; 42 43 // Returns a std::string representation of this pattern. 44 const std::string& AsString() const { return pattern_; } 45 46 bool operator==(const ContentSettingsPattern& other) const { 47 return pattern_ == other.pattern_; 48 } 49 50 // Canonicalizes the pattern so that it's ASCII only, either 51 // in original (if it was already ASCII) or punycode form. 52 std::string CanonicalizePattern() const; 53 54 std::string scheme() const { 55 return scheme_; 56 } 57 58 // The version of the pattern format implemented. 59 static const int kContentSettingsPatternVersion; 60 61 // The format of a domain wildcard. 62 static const char* kDomainWildcard; 63 64 // The length of kDomainWildcard (without the trailing '\0'). 65 static const size_t kDomainWildcardLength; 66 67 private: 68 // TODO(markusheintz): This constructor is only here to fix bug 76693. Further 69 // refactoring pending to fully integrate scheme support in content settings 70 // patterns. 71 ContentSettingsPattern(const std::string& host, const std::string& scheme) 72 : pattern_(host), 73 scheme_(scheme) {} 74 75 std::string pattern_; 76 77 // TODO(markusheintz): This is only here to fix bug 76693. There is more work 78 // to do to add scheme support to content-settings patterns. 79 // TODO(markusheintz): canonicalize to lowercase; 80 std::string scheme_; 81}; 82 83// Stream operator so ContentSettingsPattern can be used in assertion 84// statements. 85inline std::ostream& operator<<( 86 std::ostream& out, const ContentSettingsPattern& pattern) { 87 return out << pattern.AsString(); 88} 89 90#endif // CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_PATTERN_H_ 91