| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #ifndef COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_CONTENT_SETTINGS_PREF_PROVIDER_
H_ | 5 #ifndef COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_CONTENT_SETTINGS_PREF_H_ |
| 6 #define COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_CONTENT_SETTINGS_PREF_PROVIDER_
H_ | 6 #define COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_CONTENT_SETTINGS_PREF_H_ |
| 7 | 7 |
| 8 // A content settings provider that takes its settings out of the pref service. | 8 #include <string> |
| 9 | 9 |
| 10 #include <vector> | 10 #include "base/callback.h" |
| 11 | |
| 12 #include "base/basictypes.h" | |
| 13 #include "base/prefs/pref_change_registrar.h" | |
| 14 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
| 15 #include "components/content_settings/core/browser/content_settings_observable_p
rovider.h" | 12 #include "base/threading/thread_checker.h" |
| 13 #include "base/values.h" |
| 16 #include "components/content_settings/core/browser/content_settings_origin_ident
ifier_value_map.h" | 14 #include "components/content_settings/core/browser/content_settings_origin_ident
ifier_value_map.h" |
| 17 #include "components/content_settings/core/browser/content_settings_utils.h" | 15 #include "components/content_settings/core/browser/content_settings_provider.h" |
| 16 #include "components/content_settings/core/common/content_settings_pattern.h" |
| 17 #include "components/content_settings/core/common/content_settings_types.h" |
| 18 | 18 |
| 19 class PrefService; | 19 class PrefService; |
| 20 class PrefChangeRegistrar; |
| 20 | 21 |
| 21 namespace base { | 22 namespace base { |
| 22 class Clock; | 23 class Clock; |
| 23 class DictionaryValue; | 24 class DictionaryValue; |
| 24 } | 25 } |
| 25 | 26 |
| 26 namespace user_prefs { | 27 namespace user_prefs { |
| 27 class PrefRegistrySyncable; | 28 class PrefRegistrySyncable; |
| 28 } | 29 } |
| 29 | 30 |
| 30 namespace content_settings { | 31 namespace content_settings { |
| 31 | 32 |
| 32 // Content settings provider that provides content settings from the user | 33 class RuleIterator; |
| 33 // preference. | 34 |
| 34 class PrefProvider : public ObservableProvider { | 35 // Represents a single pref for reading/writing content settings. |
| 36 // TODO(raymes): Currently all content settings types are stored in a single |
| 37 // instance of one of these. But the intention is that there will be one |
| 38 // instance of this class per content settings type. |
| 39 class ContentSettingsPref { |
| 35 public: | 40 public: |
| 36 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | 41 typedef base::Callback<void(const ContentSettingsPattern&, |
| 42 const ContentSettingsPattern&, |
| 43 ContentSettingsType, |
| 44 const std::string&)> NotifyObserversCallback; |
| 37 | 45 |
| 38 PrefProvider(PrefService* prefs, bool incognito); | 46 ContentSettingsPref(PrefService* prefs, |
| 39 ~PrefProvider() override; | 47 PrefChangeRegistrar* registrar, |
| 48 base::Clock* clock, |
| 49 bool incognito, |
| 50 NotifyObserversCallback notify_callback); |
| 51 ~ContentSettingsPref(); |
| 40 | 52 |
| 41 // ProviderInterface implementations. | |
| 42 RuleIterator* GetRuleIterator(ContentSettingsType content_type, | 53 RuleIterator* GetRuleIterator(ContentSettingsType content_type, |
| 43 const ResourceIdentifier& resource_identifier, | 54 const ResourceIdentifier& resource_identifier, |
| 44 bool incognito) const override; | 55 bool incognito) const; |
| 45 | 56 |
| 46 bool SetWebsiteSetting(const ContentSettingsPattern& primary_pattern, | 57 bool SetWebsiteSetting(const ContentSettingsPattern& primary_pattern, |
| 47 const ContentSettingsPattern& secondary_pattern, | 58 const ContentSettingsPattern& secondary_pattern, |
| 48 ContentSettingsType content_type, | 59 ContentSettingsType content_type, |
| 49 const ResourceIdentifier& resource_identifier, | 60 const ResourceIdentifier& resource_identifier, |
| 50 base::Value* value) override; | 61 base::Value* value); |
| 51 | 62 |
| 52 void ClearAllContentSettingsRules(ContentSettingsType content_type) override; | 63 void ClearAllContentSettingsRules(ContentSettingsType content_type); |
| 53 | 64 |
| 54 void ShutdownOnUIThread() override; | |
| 55 | |
| 56 // Records the last time the given pattern has used a certain content setting. | |
| 57 void UpdateLastUsage(const ContentSettingsPattern& primary_pattern, | 65 void UpdateLastUsage(const ContentSettingsPattern& primary_pattern, |
| 58 const ContentSettingsPattern& secondary_pattern, | 66 const ContentSettingsPattern& secondary_pattern, |
| 59 ContentSettingsType content_type); | 67 ContentSettingsType content_type); |
| 60 | 68 |
| 61 base::Time GetLastUsage(const ContentSettingsPattern& primary_pattern, | 69 base::Time GetLastUsage(const ContentSettingsPattern& primary_pattern, |
| 62 const ContentSettingsPattern& secondary_pattern, | 70 const ContentSettingsPattern& secondary_pattern, |
| 63 ContentSettingsType content_type); | 71 ContentSettingsType content_type); |
| 64 | 72 |
| 65 // Gains ownership of |clock|. | 73 size_t GetNumExceptions(); |
| 66 void SetClockForTesting(scoped_ptr<base::Clock> clock); | |
| 67 | 74 |
| 75 void SetClockForTesting(base::Clock* clock); |
| 68 private: | 76 private: |
| 69 friend class DeadlockCheckerThread; // For testing. | |
| 70 // Reads all content settings exceptions from the preference and load them | 77 // Reads all content settings exceptions from the preference and load them |
| 71 // into the |value_map_|. The |value_map_| is cleared first. | 78 // into the |value_map_|. The |value_map_| is cleared first. |
| 72 void ReadContentSettingsFromPref(); | 79 void ReadContentSettingsFromPref(); |
| 73 | 80 |
| 74 // Callback for changes in the pref with the same name. | 81 // Callback for changes in the pref with the same name. |
| 75 void OnContentSettingsPatternPairsChanged(); | 82 void OnContentSettingsPatternPairsChanged(); |
| 76 | 83 |
| 77 // Update the preference that stores content settings exceptions and syncs the | 84 // Update the preference that stores content settings exceptions and syncs the |
| 78 // value to the obsolete preference. When calling this function, |lock_| | 85 // value to the obsolete preference. When calling this function, |lock_| |
| 79 // should not be held, since this function will send out notifications of | 86 // should not be held, since this function will send out notifications of |
| 80 // preference changes. | 87 // preference changes. |
| 81 void UpdatePref( | 88 void UpdatePref( |
| 82 const ContentSettingsPattern& primary_pattern, | 89 const ContentSettingsPattern& primary_pattern, |
| 83 const ContentSettingsPattern& secondary_pattern, | 90 const ContentSettingsPattern& secondary_pattern, |
| 84 ContentSettingsType content_type, | 91 ContentSettingsType content_type, |
| 85 const ResourceIdentifier& resource_identifier, | 92 const ResourceIdentifier& resource_identifier, |
| 86 const base::Value* value); | 93 const base::Value* value); |
| 87 | 94 |
| 88 // Migrate the old media setting into new mic/camera content settings. | |
| 89 void MigrateObsoleteMediaContentSetting(); | |
| 90 | |
| 91 static void CanonicalizeContentSettingsExceptions( | 95 static void CanonicalizeContentSettingsExceptions( |
| 92 base::DictionaryValue* all_settings_dictionary); | 96 base::DictionaryValue* all_settings_dictionary); |
| 93 | 97 |
| 94 // In the debug mode, asserts that |lock_| is not held by this thread. It's | 98 // In the debug mode, asserts that |lock_| is not held by this thread. It's |
| 95 // ok if some other thread holds |lock_|, as long as it will eventually | 99 // ok if some other thread holds |lock_|, as long as it will eventually |
| 96 // release it. | 100 // release it. |
| 97 void AssertLockNotHeld() const; | 101 void AssertLockNotHeld() const; |
| 98 | 102 |
| 99 // Weak; owned by the Profile and reset in ShutdownOnUIThread. | 103 // Weak; owned by the Profile and reset in ShutdownOnUIThread. |
| 100 PrefService* prefs_; | 104 PrefService* prefs_; |
| 101 | 105 |
| 102 // Can be set for testing. | 106 // Owned by the PrefProvider. |
| 103 scoped_ptr<base::Clock> clock_; | 107 base::Clock* clock_; |
| 108 |
| 109 // Owned by the PrefProvider. |
| 110 PrefChangeRegistrar* registrar_; |
| 104 | 111 |
| 105 bool is_incognito_; | 112 bool is_incognito_; |
| 106 | 113 |
| 107 PrefChangeRegistrar pref_change_registrar_; | |
| 108 | |
| 109 // Whether we are currently updating preferences, this is used to ignore | 114 // Whether we are currently updating preferences, this is used to ignore |
| 110 // notifications from the preferences service that we triggered ourself. | 115 // notifications from the preferences service that we triggered ourself. |
| 111 bool updating_preferences_; | 116 bool updating_preferences_; |
| 112 | 117 |
| 113 OriginIdentifierValueMap value_map_; | 118 OriginIdentifierValueMap value_map_; |
| 114 | 119 |
| 115 OriginIdentifierValueMap incognito_value_map_; | 120 OriginIdentifierValueMap incognito_value_map_; |
| 116 | 121 |
| 122 NotifyObserversCallback notify_callback_; |
| 123 |
| 117 // Used around accesses to the value map objects to guarantee thread safety. | 124 // Used around accesses to the value map objects to guarantee thread safety. |
| 118 mutable base::Lock lock_; | 125 mutable base::Lock lock_; |
| 119 | 126 |
| 120 DISALLOW_COPY_AND_ASSIGN(PrefProvider); | 127 base::ThreadChecker thread_checker_; |
| 128 |
| 129 DISALLOW_COPY_AND_ASSIGN(ContentSettingsPref); |
| 121 }; | 130 }; |
| 122 | 131 |
| 123 } // namespace content_settings | 132 } // namespace content_settings |
| 124 | 133 |
| 125 #endif // COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_CONTENT_SETTINGS_PREF_PROVID
ER_H_ | 134 #endif // COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_CONTENT_SETTINGS_PREF_H_ |
| OLD | NEW |