| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Maps hostnames to custom content settings. Written on the UI thread and read | 5 // Maps hostnames to custom content settings. Written on the UI thread and read |
| 6 // on any thread. One instance per profile. | 6 // on any thread. One instance per profile. |
| 7 | 7 |
| 8 #ifndef CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_ | 8 #ifndef CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_ |
| 9 #define CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_ | 9 #define CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_ |
| 10 #pragma once | 10 #pragma once |
| 11 | 11 |
| 12 #include <map> | 12 #include <map> |
| 13 #include <string> | 13 #include <string> |
| 14 #include <utility> | 14 #include <utility> |
| 15 #include <vector> | 15 #include <vector> |
| 16 | 16 |
| 17 #include "base/basictypes.h" | 17 #include "base/basictypes.h" |
| 18 #include "base/lock.h" | 18 #include "base/lock.h" |
| 19 #include "base/ref_counted.h" | 19 #include "base/ref_counted.h" |
| 20 #include "chrome/browser/browser_thread.h" | 20 #include "chrome/browser/browser_thread.h" |
| 21 #include "chrome/browser/content_settings/content_settings_pattern.h" |
| 21 #include "chrome/browser/prefs/pref_change_registrar.h" | 22 #include "chrome/browser/prefs/pref_change_registrar.h" |
| 22 #include "chrome/common/content_settings.h" | 23 #include "chrome/common/content_settings.h" |
| 23 #include "chrome/common/notification_observer.h" | 24 #include "chrome/common/notification_observer.h" |
| 24 #include "chrome/common/notification_registrar.h" | 25 #include "chrome/common/notification_registrar.h" |
| 25 | 26 |
| 27 class ContentSettingsDetails; |
| 26 class DictionaryValue; | 28 class DictionaryValue; |
| 27 class GURL; | 29 class GURL; |
| 28 class PrefService; | 30 class PrefService; |
| 29 class Profile; | 31 class Profile; |
| 30 | 32 |
| 31 class HostContentSettingsMap | 33 class HostContentSettingsMap |
| 32 : public NotificationObserver, | 34 : public NotificationObserver, |
| 33 public base::RefCountedThreadSafe<HostContentSettingsMap, | 35 public base::RefCountedThreadSafe<HostContentSettingsMap, |
| 34 BrowserThread::DeleteOnUIThread> { | 36 BrowserThread::DeleteOnUIThread> { |
| 35 public: | 37 public: |
| 36 // A hostname pattern. See |IsValid| for a description of possible patterns. | 38 typedef std::pair<ContentSettingsPattern, ContentSetting> PatternSettingPair; |
| 37 class Pattern { | |
| 38 public: | |
| 39 // Returns a pattern that matches the host of this URL and all subdomains. | |
| 40 static Pattern FromURL(const GURL& url); | |
| 41 | |
| 42 // Returns a pattern that matches exactly this URL. | |
| 43 static Pattern FromURLNoWildcard(const GURL& url); | |
| 44 | |
| 45 Pattern() {} | |
| 46 | |
| 47 explicit Pattern(const std::string& pattern) : pattern_(pattern) {} | |
| 48 | |
| 49 // True if this is a valid pattern. Valid patterns are | |
| 50 // - [*.]domain.tld (matches domain.tld and all sub-domains) | |
| 51 // - host (matches an exact hostname) | |
| 52 // - a.b.c.d (matches an exact IPv4 ip) | |
| 53 // - [a:b:c:d:e:f:g:h] (matches an exact IPv6 ip) | |
| 54 bool IsValid() const; | |
| 55 | |
| 56 // True if |url| matches this pattern. | |
| 57 bool Matches(const GURL& url) const; | |
| 58 | |
| 59 // Returns a std::string representation of this pattern. | |
| 60 const std::string& AsString() const { return pattern_; } | |
| 61 | |
| 62 bool operator==(const Pattern& other) const { | |
| 63 return pattern_ == other.pattern_; | |
| 64 } | |
| 65 | |
| 66 // Canonicalizes the pattern so that it's ASCII only, either | |
| 67 // in original or punycode form. | |
| 68 std::string CanonicalizePattern() const; | |
| 69 | |
| 70 private: | |
| 71 std::string pattern_; | |
| 72 }; | |
| 73 | |
| 74 // Details for the CONTENT_SETTINGS_CHANGED notification. This is sent when | |
| 75 // content settings change for at least one host. If settings change for more | |
| 76 // than one pattern in one user interaction, this will usually send a single | |
| 77 // notification with update_all() returning true instead of one notification | |
| 78 // for each pattern. | |
| 79 class ContentSettingsDetails { | |
| 80 public: | |
| 81 // Update the setting that matches this pattern/content type/resource. | |
| 82 ContentSettingsDetails(const Pattern& pattern, | |
| 83 ContentSettingsType type, | |
| 84 const std::string& resource_identifier) | |
| 85 : pattern_(pattern), | |
| 86 type_(type), | |
| 87 resource_identifier_(resource_identifier) {} | |
| 88 | |
| 89 // The pattern whose settings have changed. | |
| 90 const Pattern& pattern() const { return pattern_; } | |
| 91 | |
| 92 // True if all settings should be updated for the given type. | |
| 93 bool update_all() const { return pattern_.AsString().empty(); } | |
| 94 | |
| 95 // The type of the pattern whose settings have changed. | |
| 96 ContentSettingsType type() const { return type_; } | |
| 97 | |
| 98 // The resource identifier for the settings type that has changed. | |
| 99 const std::string& resource_identifier() const { | |
| 100 return resource_identifier_; | |
| 101 } | |
| 102 | |
| 103 // True if all types should be updated. If update_all() is false, this will | |
| 104 // be false as well (although the reverse does not hold true). | |
| 105 bool update_all_types() const { | |
| 106 return CONTENT_SETTINGS_TYPE_DEFAULT == type_; | |
| 107 } | |
| 108 | |
| 109 private: | |
| 110 Pattern pattern_; | |
| 111 ContentSettingsType type_; | |
| 112 std::string resource_identifier_; | |
| 113 }; | |
| 114 | |
| 115 typedef std::pair<Pattern, ContentSetting> PatternSettingPair; | |
| 116 typedef std::vector<PatternSettingPair> SettingsForOneType; | 39 typedef std::vector<PatternSettingPair> SettingsForOneType; |
| 117 | 40 |
| 118 explicit HostContentSettingsMap(Profile* profile); | 41 explicit HostContentSettingsMap(Profile* profile); |
| 119 ~HostContentSettingsMap(); | 42 ~HostContentSettingsMap(); |
| 120 | 43 |
| 121 static void RegisterUserPrefs(PrefService* prefs); | 44 static void RegisterUserPrefs(PrefService* prefs); |
| 122 | 45 |
| 123 // Returns the default setting for a particular content type. | 46 // Returns the default setting for a particular content type. |
| 124 // | 47 // |
| 125 // This may be called on any thread. | 48 // This may be called on any thread. |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 void SetDefaultContentSetting(ContentSettingsType content_type, | 105 void SetDefaultContentSetting(ContentSettingsType content_type, |
| 183 ContentSetting setting); | 106 ContentSetting setting); |
| 184 | 107 |
| 185 // Sets the blocking setting for a particular pattern and content type. | 108 // Sets the blocking setting for a particular pattern and content type. |
| 186 // Setting the value to CONTENT_SETTING_DEFAULT causes the default setting | 109 // Setting the value to CONTENT_SETTING_DEFAULT causes the default setting |
| 187 // for that type to be used when loading pages matching this pattern. For | 110 // for that type to be used when loading pages matching this pattern. For |
| 188 // ContentSettingsTypes that require an resource identifier to be specified, | 111 // ContentSettingsTypes that require an resource identifier to be specified, |
| 189 // the |resource_identifier| must be non-empty. | 112 // the |resource_identifier| must be non-empty. |
| 190 // | 113 // |
| 191 // This should only be called on the UI thread. | 114 // This should only be called on the UI thread. |
| 192 void SetContentSetting(const Pattern& pattern, | 115 void SetContentSetting(const ContentSettingsPattern& pattern, |
| 193 ContentSettingsType content_type, | 116 ContentSettingsType content_type, |
| 194 const std::string& resource_identifier, | 117 const std::string& resource_identifier, |
| 195 ContentSetting setting); | 118 ContentSetting setting); |
| 196 | 119 |
| 197 // Convenience method to add a content setting for a given URL, making sure | 120 // Convenience method to add a content setting for a given URL, making sure |
| 198 // that there is no setting overriding it. For ContentSettingsTypes that | 121 // that there is no setting overriding it. For ContentSettingsTypes that |
| 199 // require an resource identifier to be specified, the |resource_identifier| | 122 // require an resource identifier to be specified, the |resource_identifier| |
| 200 // must be non-empty. | 123 // must be non-empty. |
| 201 // | 124 // |
| 202 // This should only be called on the UI thread. | 125 // This should only be called on the UI thread. |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 // Whether this settings map is for an OTR session. | 260 // Whether this settings map is for an OTR session. |
| 338 bool is_off_the_record_; | 261 bool is_off_the_record_; |
| 339 | 262 |
| 340 // Whether we are currently updating preferences, this is used to ignore | 263 // Whether we are currently updating preferences, this is used to ignore |
| 341 // notifications from the preferences service that we triggered ourself. | 264 // notifications from the preferences service that we triggered ourself. |
| 342 bool updating_preferences_; | 265 bool updating_preferences_; |
| 343 | 266 |
| 344 DISALLOW_COPY_AND_ASSIGN(HostContentSettingsMap); | 267 DISALLOW_COPY_AND_ASSIGN(HostContentSettingsMap); |
| 345 }; | 268 }; |
| 346 | 269 |
| 347 // Stream operator so HostContentSettingsMap::Pattern can be used in | |
| 348 // assertion statements. | |
| 349 inline std::ostream& operator<<( | |
| 350 std::ostream& out, const HostContentSettingsMap::Pattern& pattern) { | |
| 351 return out << pattern.AsString(); | |
| 352 } | |
| 353 | |
| 354 #endif // CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_ | 270 #endif // CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_ |
| OLD | NEW |