| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
| 17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 18 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
| 19 #include "base/tuple.h" | 19 #include "base/tuple.h" |
| 20 #include "chrome/browser/content_settings/content_settings_observer.h" | 20 #include "chrome/browser/content_settings/content_settings_observer.h" |
| 21 #include "chrome/browser/prefs/pref_change_registrar.h" | 21 #include "chrome/browser/prefs/pref_change_registrar.h" |
| 22 #include "chrome/common/content_settings.h" | 22 #include "chrome/common/content_settings.h" |
| 23 #include "chrome/common/content_settings_pattern.h" | 23 #include "chrome/common/content_settings_pattern.h" |
| 24 #include "content/public/browser/notification_observer.h" | |
| 25 #include "content/public/browser/notification_registrar.h" | |
| 26 | 24 |
| 27 namespace base { | 25 namespace base { |
| 28 class Value; | 26 class Value; |
| 29 } // namespace base | 27 } // namespace base |
| 30 | 28 |
| 31 namespace content_settings { | 29 namespace content_settings { |
| 32 class ProviderInterface; | 30 class ProviderInterface; |
| 33 } // namespace content_settings | 31 } // namespace content_settings |
| 34 | 32 |
| 35 class ExtensionService; | 33 class ExtensionService; |
| 36 class GURL; | 34 class GURL; |
| 37 class PrefService; | 35 class PrefService; |
| 38 | 36 |
| 39 class HostContentSettingsMap | 37 class HostContentSettingsMap |
| 40 : public content_settings::Observer, | 38 : public content_settings::Observer, |
| 41 public content::NotificationObserver, | |
| 42 public base::RefCountedThreadSafe<HostContentSettingsMap> { | 39 public base::RefCountedThreadSafe<HostContentSettingsMap> { |
| 43 public: | 40 public: |
| 44 enum ProviderType { | 41 enum ProviderType { |
| 45 POLICY_PROVIDER = 0, | 42 POLICY_PROVIDER = 0, |
| 46 EXTENSION_PROVIDER = 1, | 43 EXTENSION_PROVIDER = 1, |
| 47 PREF_PROVIDER, | 44 PREF_PROVIDER, |
| 48 DEFAULT_PROVIDER, | 45 DEFAULT_PROVIDER, |
| 49 NUM_PROVIDER_TYPES, | 46 NUM_PROVIDER_TYPES, |
| 50 }; | 47 }; |
| 51 | 48 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 70 // | 67 // |
| 71 // This may be called on any thread. | 68 // This may be called on any thread. |
| 72 ContentSetting GetDefaultContentSetting(ContentSettingsType content_type, | 69 ContentSetting GetDefaultContentSetting(ContentSettingsType content_type, |
| 73 std::string* provider_id) const; | 70 std::string* provider_id) const; |
| 74 | 71 |
| 75 // Returns the default settings for all content types. | 72 // Returns the default settings for all content types. |
| 76 // | 73 // |
| 77 // This may be called on any thread. | 74 // This may be called on any thread. |
| 78 ContentSettings GetDefaultContentSettings() const; | 75 ContentSettings GetDefaultContentSettings() const; |
| 79 | 76 |
| 80 // Returns a single |ContentSetting| which applies to the given URLs. | 77 // Returns a single |ContentSetting| which applies to the given URLs. Note |
| 81 // Note that certain internal schemes are whitelisted. | 78 // that certain internal schemes are whitelisted. For |CONTENT_TYPE_COOKIES|, |
| 82 // For |CONTENT_TYPE_COOKIES|, |GetCookieContentSetting| should be called, | 79 // |CookieSettings| should be used instead. For content types that can't be |
| 83 // and for content types that can't be converted to a ContentSetting, | 80 // converted to a |ContentSetting|, |GetContentSettingValue| should be called. |
| 84 // |GetContentSettingValue| should be called. | |
| 85 // If there is no content setting, returns CONTENT_SETTING_DEFAULT. | 81 // If there is no content setting, returns CONTENT_SETTING_DEFAULT. |
| 82 // |
| 86 // May be called on any thread. | 83 // May be called on any thread. |
| 87 ContentSetting GetContentSetting( | 84 ContentSetting GetContentSetting( |
| 88 const GURL& primary_url, | 85 const GURL& primary_url, |
| 89 const GURL& secondary_url, | 86 const GURL& secondary_url, |
| 90 ContentSettingsType content_type, | 87 ContentSettingsType content_type, |
| 91 const std::string& resource_identifier) const; | 88 const std::string& resource_identifier) const; |
| 92 | 89 |
| 93 // Returns a single content setting |Value| which applies to the given URLs. | 90 // Returns a single content setting |Value| which applies to the given URLs. |
| 94 // If |primary_pattern| and |secondary_pattern| are not NULL, they are set to | 91 // If |primary_pattern| and |secondary_pattern| are not NULL, they are set to |
| 95 // the patterns of the applying rule. | 92 // the patterns of the applying rule. |
| 96 // Note that certain internal schemes are whitelisted. | 93 // Note that certain internal schemes are whitelisted. |
| 97 // If there is no content setting, returns NULL and leaves |primary_pattern| | 94 // If there is no content setting, returns NULL and leaves |primary_pattern| |
| 98 // and |secondary_pattern| unchanged. | 95 // and |secondary_pattern| unchanged. |
| 99 // Otherwise transfers ownership of the resulting |Value| to the caller. | 96 // Otherwise transfers ownership of the resulting |Value| to the caller. |
| 100 // May be called on any thread. | 97 // May be called on any thread. |
| 101 base::Value* GetContentSettingValue( | 98 base::Value* GetContentSettingValue( |
| 102 const GURL& primary_url, | 99 const GURL& primary_url, |
| 103 const GURL& secondary_url, | 100 const GURL& secondary_url, |
| 104 ContentSettingsType content_type, | 101 ContentSettingsType content_type, |
| 105 const std::string& resource_identifier, | 102 const std::string& resource_identifier, |
| 106 ContentSettingsPattern* primary_pattern, | 103 ContentSettingsPattern* primary_pattern, |
| 107 ContentSettingsPattern* secondary_pattern) const; | 104 ContentSettingsPattern* secondary_pattern) const; |
| 108 | 105 |
| 109 // Gets the content setting for cookies. This takes the third party cookie | 106 // Returns all ContentSettings which apply to the given |primary_url|. For |
| 110 // flag into account, and therefore needs to know whether we read or write a | 107 // content setting types that require an additional resource identifier, the |
| 111 // cookie. | 108 // default content setting is returned. |
| 112 // | |
| 113 // This may be called on any thread. | |
| 114 ContentSetting GetCookieContentSetting( | |
| 115 const GURL& url, | |
| 116 const GURL& first_party_url, | |
| 117 bool setting_cookie) const; | |
| 118 | |
| 119 // Returns all ContentSettings which apply to the given URLs. For content | |
| 120 // setting types that require an additional resource identifier, the default | |
| 121 // content setting is returned. | |
| 122 // | 109 // |
| 123 // This may be called on any thread. | 110 // This may be called on any thread. |
| 124 ContentSettings GetContentSettings( | 111 ContentSettings GetContentSettings( |
| 125 const GURL& primary_url, | 112 const GURL& primary_url) const; |
| 126 const GURL& secondary_url) const; | |
| 127 | 113 |
| 128 // For a given content type, returns all patterns with a non-default setting, | 114 // For a given content type, returns all patterns with a non-default setting, |
| 129 // mapped to their actual settings, in lexicographical order. |settings| | 115 // mapped to their actual settings, in lexicographical order. |settings| |
| 130 // must be a non-NULL outparam. If this map was created for the | 116 // must be a non-NULL outparam. If this map was created for the |
| 131 // incognito profile, it will only return those settings differing from | 117 // incognito profile, it will only return those settings differing from |
| 132 // the main map. For ContentSettingsTypes that require an resource identifier | 118 // the main map. For ContentSettingsTypes that require an resource identifier |
| 133 // to be specified, the |resource_identifier| must be non-empty. | 119 // to be specified, the |resource_identifier| must be non-empty. |
| 134 // | 120 // |
| 135 // This may be called on any thread. | 121 // This may be called on any thread. |
| 136 void GetSettingsForOneType(ContentSettingsType content_type, | 122 void GetSettingsForOneType(ContentSettingsType content_type, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 ContentSetting setting); | 156 ContentSetting setting); |
| 171 | 157 |
| 172 // Clears all host-specific settings for one content type. | 158 // Clears all host-specific settings for one content type. |
| 173 // | 159 // |
| 174 // This should only be called on the UI thread. | 160 // This should only be called on the UI thread. |
| 175 void ClearSettingsForOneType(ContentSettingsType content_type); | 161 void ClearSettingsForOneType(ContentSettingsType content_type); |
| 176 | 162 |
| 177 static bool IsSettingAllowedForType(ContentSetting setting, | 163 static bool IsSettingAllowedForType(ContentSetting setting, |
| 178 ContentSettingsType content_type); | 164 ContentSettingsType content_type); |
| 179 | 165 |
| 180 // This setting trumps any host-specific settings. | |
| 181 bool BlockThirdPartyCookies() const { return block_third_party_cookies_; } | |
| 182 bool IsBlockThirdPartyCookiesManaged() const { | |
| 183 return is_block_third_party_cookies_managed_; | |
| 184 } | |
| 185 | |
| 186 // Sets whether we block all third-party cookies. This method must not be | |
| 187 // invoked on an incognito map. | |
| 188 // | |
| 189 // This should only be called on the UI thread. | |
| 190 void SetBlockThirdPartyCookies(bool block); | |
| 191 | |
| 192 // Detaches the HostContentSettingsMap from all Profile-related objects like | 166 // Detaches the HostContentSettingsMap from all Profile-related objects like |
| 193 // PrefService. This methods needs to be called before destroying the Profile. | 167 // PrefService. This methods needs to be called before destroying the Profile. |
| 194 // Afterwards, none of the methods above that should only be called on the UI | 168 // Afterwards, none of the methods above that should only be called on the UI |
| 195 // thread should be called anymore. | 169 // thread should be called anymore. |
| 196 void ShutdownOnUIThread(); | 170 void ShutdownOnUIThread(); |
| 197 | 171 |
| 198 // content_settings::Observer implementation. | 172 // content_settings::Observer implementation. |
| 199 virtual void OnContentSettingChanged( | 173 virtual void OnContentSettingChanged( |
| 200 ContentSettingsPattern primary_pattern, | 174 ContentSettingsPattern primary_pattern, |
| 201 ContentSettingsPattern secondary_pattern, | 175 ContentSettingsPattern secondary_pattern, |
| 202 ContentSettingsType content_type, | 176 ContentSettingsType content_type, |
| 203 std::string resource_identifier); | 177 std::string resource_identifier); |
| 204 | 178 |
| 205 // content::NotificationObserver implementation. | 179 // Returns true if we should allow all content types for this URL. This is |
| 206 virtual void Observe(int type, | 180 // true for various internal objects like chrome:// URLs, so UI and other |
| 207 const content::NotificationSource& source, | 181 // things users think of as "not webpages" don't break. |
| 208 const content::NotificationDetails& details); | 182 static bool ShouldAllowAllContent(const GURL& url, |
| 183 ContentSettingsType content_type); |
| 209 | 184 |
| 210 private: | 185 private: |
| 211 friend class base::RefCountedThreadSafe<HostContentSettingsMap>; | 186 friend class base::RefCountedThreadSafe<HostContentSettingsMap>; |
| 212 friend class HostContentSettingsMapTest_NonDefaultSettings_Test; | 187 friend class HostContentSettingsMapTest_NonDefaultSettings_Test; |
| 213 | 188 |
| 214 typedef std::map<ProviderType, content_settings::ProviderInterface*> | 189 typedef std::map<ProviderType, content_settings::ProviderInterface*> |
| 215 ProviderMap; | 190 ProviderMap; |
| 216 typedef ProviderMap::iterator ProviderIterator; | 191 typedef ProviderMap::iterator ProviderIterator; |
| 217 typedef ProviderMap::const_iterator ConstProviderIterator; | 192 typedef ProviderMap::const_iterator ConstProviderIterator; |
| 218 | 193 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 235 const content_settings::ProviderInterface* provider, | 210 const content_settings::ProviderInterface* provider, |
| 236 ProviderType provider_type, | 211 ProviderType provider_type, |
| 237 ContentSettingsType content_type, | 212 ContentSettingsType content_type, |
| 238 const std::string& resource_identifier, | 213 const std::string& resource_identifier, |
| 239 SettingsForOneType* settings, | 214 SettingsForOneType* settings, |
| 240 bool incognito) const; | 215 bool incognito) const; |
| 241 | 216 |
| 242 // Weak; owned by the Profile. | 217 // Weak; owned by the Profile. |
| 243 PrefService* prefs_; | 218 PrefService* prefs_; |
| 244 | 219 |
| 245 PrefChangeRegistrar pref_change_registrar_; | |
| 246 | |
| 247 // Whether this settings map is for an OTR session. | 220 // Whether this settings map is for an OTR session. |
| 248 bool is_off_the_record_; | 221 bool is_off_the_record_; |
| 249 | 222 |
| 250 // Whether we are currently updating preferences, this is used to ignore | 223 // Whether we are currently updating preferences, this is used to ignore |
| 251 // notifications from the preferences service that we triggered ourself. | 224 // notifications from the preferences service that we triggered ourself. |
| 252 bool updating_preferences_; | 225 bool updating_preferences_; |
| 253 | 226 |
| 254 // Content setting providers. | 227 // Content setting providers. |
| 255 ProviderMap content_settings_providers_; | 228 ProviderMap content_settings_providers_; |
| 256 | 229 |
| 257 // Used around accesses to the following objects to guarantee thread safety. | 230 // Used around accesses to the following objects to guarantee thread safety. |
| 258 mutable base::Lock lock_; | 231 mutable base::Lock lock_; |
| 259 | 232 |
| 260 // Misc global settings. | |
| 261 bool block_third_party_cookies_; | |
| 262 bool is_block_third_party_cookies_managed_; | |
| 263 | |
| 264 DISALLOW_COPY_AND_ASSIGN(HostContentSettingsMap); | 233 DISALLOW_COPY_AND_ASSIGN(HostContentSettingsMap); |
| 265 }; | 234 }; |
| 266 | 235 |
| 267 #endif // CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_ | 236 #endif // CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_ |
| OLD | NEW |