| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 | 10 |
| 11 #include <map> | |
| 12 #include <string> | 11 #include <string> |
| 13 #include <vector> | 12 #include <vector> |
| 14 | 13 |
| 15 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 16 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/scoped_vector.h" |
| 17 #include "base/observer_list.h" | 17 #include "base/observer_list.h" |
| 18 #include "base/prefs/pref_change_registrar.h" | 18 #include "base/prefs/pref_change_registrar.h" |
| 19 #include "base/threading/platform_thread.h" | 19 #include "base/threading/platform_thread.h" |
| 20 #include "base/tuple.h" | 20 #include "base/tuple.h" |
| 21 #include "chrome/browser/content_settings/content_settings_observer.h" | 21 #include "chrome/browser/content_settings/content_settings_observer.h" |
| 22 #include "components/content_settings/core/common/content_settings.h" | 22 #include "components/content_settings/core/common/content_settings.h" |
| 23 #include "components/content_settings/core/common/content_settings_pattern.h" | 23 #include "components/content_settings/core/common/content_settings_pattern.h" |
| 24 #include "components/content_settings/core/common/content_settings_types.h" | 24 #include "components/content_settings/core/common/content_settings_types.h" |
| 25 | 25 |
| 26 class ExtensionService; | 26 class ExtensionService; |
| 27 class GURL; | 27 class GURL; |
| 28 class PrefService; | 28 class PrefService; |
| 29 | 29 |
| 30 namespace base { | 30 namespace base { |
| 31 class Clock; | 31 class Clock; |
| 32 class Value; | 32 class Value; |
| 33 } | 33 } |
| 34 | 34 |
| 35 namespace content_settings { | 35 namespace content_settings { |
| 36 class DefaultProvider; |
| 37 class ObservableProvider; |
| 36 class ProviderInterface; | 38 class ProviderInterface; |
| 37 class PrefProvider; | 39 class PrefProvider; |
| 38 } | 40 } |
| 39 | 41 |
| 40 namespace user_prefs { | 42 namespace user_prefs { |
| 41 class PrefRegistrySyncable; | 43 class PrefRegistrySyncable; |
| 42 } | 44 } |
| 43 | 45 |
| 44 class HostContentSettingsMap | 46 class HostContentSettingsMap |
| 45 : public content_settings::Observer, | 47 : public content_settings::Observer, |
| 46 public base::RefCountedThreadSafe<HostContentSettingsMap> { | 48 public base::RefCountedThreadSafe<HostContentSettingsMap> { |
| 47 public: | 49 public: |
| 48 enum ProviderType { | |
| 49 INTERNAL_EXTENSION_PROVIDER = 0, | |
| 50 POLICY_PROVIDER, | |
| 51 CUSTOM_EXTENSION_PROVIDER, | |
| 52 PREF_PROVIDER, | |
| 53 DEFAULT_PROVIDER, | |
| 54 NUM_PROVIDER_TYPES, | |
| 55 }; | |
| 56 | |
| 57 HostContentSettingsMap(PrefService* prefs, bool incognito); | 50 HostContentSettingsMap(PrefService* prefs, bool incognito); |
| 58 | 51 |
| 59 #if defined(ENABLE_EXTENSIONS) | 52 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); |
| 60 // In some cases, the ExtensionService is not available at the time the | |
| 61 // HostContentSettingsMap is constructed. In these cases, we register the | |
| 62 // service once it's available. | |
| 63 void RegisterExtensionService(ExtensionService* extension_service); | |
| 64 #endif | |
| 65 | 53 |
| 66 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | 54 // Adds a new provider for |type|. |
| 55 void RegisterProvider( |
| 56 scoped_ptr<content_settings::ObservableProvider> provider); |
| 57 |
| 58 // Gets the number of providers it has. |
| 59 size_t GetNumProviders() const; |
| 60 |
| 61 // Gets the i-th provider. |
| 62 const content_settings::ProviderInterface* GetProviderAt(size_t i) const; |
| 67 | 63 |
| 68 // Returns the default setting for a particular content type. If |provider_id| | 64 // Returns the default setting for a particular content type. If |provider_id| |
| 69 // is not NULL, the id of the provider which provided the default setting is | 65 // is not NULL, the id of the provider which provided the default setting is |
| 70 // assigned to it. | 66 // assigned to it. |
| 71 // | 67 // |
| 72 // This may be called on any thread. | 68 // This may be called on any thread. |
| 73 ContentSetting GetDefaultContentSetting(ContentSettingsType content_type, | 69 ContentSetting GetDefaultContentSetting(ContentSettingsType content_type, |
| 74 std::string* provider_id) const; | 70 std::string* provider_id) const; |
| 75 | 71 |
| 76 // Returns a single |ContentSetting| which applies to the given URLs. Note | 72 // Returns a single |ContentSetting| which applies to the given URLs. Note |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 ContentSettingsType content_type, | 191 ContentSettingsType content_type, |
| 196 std::string resource_identifier) OVERRIDE; | 192 std::string resource_identifier) OVERRIDE; |
| 197 | 193 |
| 198 // Returns true if we should allow all content types for this URL. This is | 194 // Returns true if we should allow all content types for this URL. This is |
| 199 // true for various internal objects like chrome:// URLs, so UI and other | 195 // true for various internal objects like chrome:// URLs, so UI and other |
| 200 // things users think of as "not webpages" don't break. | 196 // things users think of as "not webpages" don't break. |
| 201 static bool ShouldAllowAllContent(const GURL& primary_url, | 197 static bool ShouldAllowAllContent(const GURL& primary_url, |
| 202 const GURL& secondary_url, | 198 const GURL& secondary_url, |
| 203 ContentSettingsType content_type); | 199 ContentSettingsType content_type); |
| 204 | 200 |
| 205 // Returns the ProviderType associated with the given source string. | |
| 206 // TODO(estade): I regret adding this. At the moment there are no legitimate | |
| 207 // uses. We should stick to ProviderType rather than string so we don't have | |
| 208 // to convert backwards. | |
| 209 static ProviderType GetProviderTypeFromSource(const std::string& source); | |
| 210 | |
| 211 bool is_off_the_record() const { | 201 bool is_off_the_record() const { |
| 212 return is_off_the_record_; | 202 return is_off_the_record_; |
| 213 } | 203 } |
| 214 | 204 |
| 215 // Returns a single |ContentSetting| which applies to the given URLs, just as | 205 // Returns a single |ContentSetting| which applies to the given URLs, just as |
| 216 // |GetContentSetting| does. If the setting is allowed, it also records the | 206 // |GetContentSetting| does. If the setting is allowed, it also records the |
| 217 // last usage to preferences. | 207 // last usage to preferences. |
| 218 // | 208 // |
| 219 // This should only be called on the UI thread, unlike |GetContentSetting|. | 209 // This should only be called on the UI thread, unlike |GetContentSetting|. |
| 220 ContentSetting GetContentSettingAndMaybeUpdateLastUsage( | 210 ContentSetting GetContentSettingAndMaybeUpdateLastUsage( |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 void AddObserver(content_settings::Observer* observer); | 242 void AddObserver(content_settings::Observer* observer); |
| 253 void RemoveObserver(content_settings::Observer* observer); | 243 void RemoveObserver(content_settings::Observer* observer); |
| 254 | 244 |
| 255 // Passes ownership of |clock|. | 245 // Passes ownership of |clock|. |
| 256 void SetPrefClockForTesting(scoped_ptr<base::Clock> clock); | 246 void SetPrefClockForTesting(scoped_ptr<base::Clock> clock); |
| 257 | 247 |
| 258 private: | 248 private: |
| 259 friend class base::RefCountedThreadSafe<HostContentSettingsMap>; | 249 friend class base::RefCountedThreadSafe<HostContentSettingsMap>; |
| 260 friend class HostContentSettingsMapTest_NonDefaultSettings_Test; | 250 friend class HostContentSettingsMapTest_NonDefaultSettings_Test; |
| 261 | 251 |
| 262 typedef std::map<ProviderType, content_settings::ProviderInterface*> | 252 typedef ScopedVector<content_settings::ProviderInterface> ProviderList; |
| 263 ProviderMap; | 253 typedef ProviderList::iterator ProviderIterator; |
| 264 typedef ProviderMap::iterator ProviderIterator; | 254 typedef ProviderList::const_iterator ConstProviderIterator; |
| 265 typedef ProviderMap::const_iterator ConstProviderIterator; | |
| 266 | 255 |
| 267 virtual ~HostContentSettingsMap(); | 256 virtual ~HostContentSettingsMap(); |
| 268 | 257 |
| 269 ContentSetting GetDefaultContentSettingFromProvider( | 258 ContentSetting GetDefaultContentSettingFromProvider( |
| 270 ContentSettingsType content_type, | 259 ContentSettingsType content_type, |
| 271 content_settings::ProviderInterface* provider) const; | 260 content_settings::ProviderInterface* provider) const; |
| 272 | 261 |
| 273 // Migrate the Clear on exit pref into equivalent content settings. | 262 // Migrate the Clear on exit pref into equivalent content settings. |
| 274 void MigrateObsoleteClearOnExitPref(); | 263 void MigrateObsoleteClearOnExitPref(); |
| 275 | 264 |
| 276 // Adds content settings for |content_type| and |resource_identifier|, | 265 // Adds content settings for |content_type| and |resource_identifier|, |
| 277 // provided by |provider|, into |settings|. If |incognito| is true, adds only | 266 // provided by |provider|, into |settings|. If |incognito| is true, adds only |
| 278 // the content settings which are applicable to the incognito mode and differ | 267 // the content settings which are applicable to the incognito mode and differ |
| 279 // from the normal mode. Otherwise, adds the content settings for the normal | 268 // from the normal mode. Otherwise, adds the content settings for the normal |
| 280 // mode. | 269 // mode. |
| 281 void AddSettingsForOneType( | 270 void AddSettingsForOneType( |
| 282 const content_settings::ProviderInterface* provider, | 271 const content_settings::ProviderInterface* provider, |
| 283 ProviderType provider_type, | |
| 284 ContentSettingsType content_type, | 272 ContentSettingsType content_type, |
| 285 const std::string& resource_identifier, | 273 const std::string& resource_identifier, |
| 286 ContentSettingsForOneType* settings, | 274 ContentSettingsForOneType* settings, |
| 287 bool incognito) const; | 275 bool incognito) const; |
| 288 | 276 |
| 289 // Call UsedContentSettingsProviders() whenever you access | 277 // Call UsedContentSettingsProviders() whenever you access |
| 290 // content_settings_providers_ (apart from initialization and | 278 // content_settings_providers_ (apart from initialization and |
| 291 // teardown), so that we can DCHECK in RegisterExtensionService that | 279 // teardown), so that we can DCHECK in RegisterExtensionService that |
| 292 // it is not being called too late. | 280 // it is not being called too late. |
| 293 void UsedContentSettingsProviders() const; | 281 void UsedContentSettingsProviders() const; |
| 294 | 282 |
| 295 content_settings::PrefProvider* GetPrefProvider(); | |
| 296 | |
| 297 #ifndef NDEBUG | 283 #ifndef NDEBUG |
| 298 // This starts as the thread ID of the thread that constructs this | 284 // This starts as the thread ID of the thread that constructs this |
| 299 // object, and remains until used by a different thread, at which | 285 // object, and remains until used by a different thread, at which |
| 300 // point it is set to base::kInvalidThreadId. This allows us to | 286 // point it is set to base::kInvalidThreadId. This allows us to |
| 301 // DCHECK on unsafe usage of content_settings_providers_ (they | 287 // DCHECK on unsafe usage of content_settings_providers_ (they |
| 302 // should be set up on a single thread, after which they are | 288 // should be set up on a single thread, after which they are |
| 303 // immutable). | 289 // immutable). |
| 304 mutable base::PlatformThreadId used_from_thread_id_; | 290 mutable base::PlatformThreadId used_from_thread_id_; |
| 305 #endif | 291 #endif |
| 306 | 292 |
| 307 // Weak; owned by the Profile. | 293 // Weak; owned by the Profile. |
| 308 PrefService* prefs_; | 294 PrefService* prefs_; |
| 309 | 295 |
| 310 // Whether this settings map is for an OTR session. | 296 // Whether this settings map is for an OTR session. |
| 311 bool is_off_the_record_; | 297 bool is_off_the_record_; |
| 312 | 298 |
| 313 // Content setting providers. This is only modified at construction | 299 // Content setting providers. This is only modified at construction |
| 314 // time and by RegisterExtensionService, both of which should happen | 300 // time and by RegisterExtensionService, both of which should happen |
| 315 // before any other uses of it. | 301 // before any other uses of it. |
| 316 ProviderMap content_settings_providers_; | 302 ProviderList content_settings_providers_; |
| 303 |
| 304 // Keeping the pointers to specific type of the provider. |
| 305 content_settings::DefaultProvider* default_provider_; |
| 306 content_settings::PrefProvider* pref_provider_; |
| 317 | 307 |
| 318 ObserverList<content_settings::Observer> observers_; | 308 ObserverList<content_settings::Observer> observers_; |
| 319 | 309 |
| 320 DISALLOW_COPY_AND_ASSIGN(HostContentSettingsMap); | 310 DISALLOW_COPY_AND_ASSIGN(HostContentSettingsMap); |
| 321 }; | 311 }; |
| 322 | 312 |
| 323 #endif // CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_ | 313 #endif // CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_ |
| OLD | NEW |