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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_CONTENT_SETTINGS_PREF_PROVIDER_H_
6 #define COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_CONTENT_SETTINGS_PREF_PROVIDER_H_
8 // A content settings provider that takes its settings out of the pref service.
12 #include "base/basictypes.h"
13 #include "base/prefs/pref_change_registrar.h"
14 #include "base/synchronization/lock.h"
15 #include "components/content_settings/core/browser/content_settings_observable_provider.h"
16 #include "components/content_settings/core/browser/content_settings_origin_identifier_value_map.h"
17 #include "components/content_settings/core/browser/content_settings_utils.h"
23 class DictionaryValue;
26 namespace user_prefs {
27 class PrefRegistrySyncable;
30 namespace content_settings {
32 // Content settings provider that provides content settings from the user
34 class PrefProvider : public ObservableProvider {
36 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
38 PrefProvider(PrefService* prefs, bool incognito);
39 ~PrefProvider() override;
41 // ProviderInterface implementations.
42 RuleIterator* GetRuleIterator(ContentSettingsType content_type,
43 const ResourceIdentifier& resource_identifier,
44 bool incognito) const override;
46 bool SetWebsiteSetting(const ContentSettingsPattern& primary_pattern,
47 const ContentSettingsPattern& secondary_pattern,
48 ContentSettingsType content_type,
49 const ResourceIdentifier& resource_identifier,
50 base::Value* value) override;
52 void ClearAllContentSettingsRules(ContentSettingsType content_type) override;
54 void ShutdownOnUIThread() override;
56 // Records the last time the given pattern has used a certain content setting.
57 void UpdateLastUsage(const ContentSettingsPattern& primary_pattern,
58 const ContentSettingsPattern& secondary_pattern,
59 ContentSettingsType content_type);
61 base::Time GetLastUsage(const ContentSettingsPattern& primary_pattern,
62 const ContentSettingsPattern& secondary_pattern,
63 ContentSettingsType content_type);
65 // Gains ownership of |clock|.
66 void SetClockForTesting(scoped_ptr<base::Clock> clock);
69 friend class DeadlockCheckerThread; // For testing.
70 // Reads all content settings exceptions from the preference and load them
71 // into the |value_map_|. The |value_map_| is cleared first if |overwrite| is
73 void ReadContentSettingsFromPref(bool overwrite);
75 // Callback for changes in the pref with the same name.
76 void OnContentSettingsPatternPairsChanged();
78 // Update the preference that stores content settings exceptions and syncs the
79 // value to the obsolete preference. When calling this function, |lock_|
80 // should not be held, since this function will send out notifications of
81 // preference changes.
83 const ContentSettingsPattern& primary_pattern,
84 const ContentSettingsPattern& secondary_pattern,
85 ContentSettingsType content_type,
86 const ResourceIdentifier& resource_identifier,
87 const base::Value* value);
89 // Migrate the old media setting into new mic/camera content settings.
90 void MigrateObsoleteMediaContentSetting();
92 static void CanonicalizeContentSettingsExceptions(
93 base::DictionaryValue* all_settings_dictionary);
95 // In the debug mode, asserts that |lock_| is not held by this thread. It's
96 // ok if some other thread holds |lock_|, as long as it will eventually
98 void AssertLockNotHeld() const;
100 // Weak; owned by the Profile and reset in ShutdownOnUIThread.
103 // Can be set for testing.
104 scoped_ptr<base::Clock> clock_;
108 PrefChangeRegistrar pref_change_registrar_;
110 // Whether we are currently updating preferences, this is used to ignore
111 // notifications from the preferences service that we triggered ourself.
112 bool updating_preferences_;
114 OriginIdentifierValueMap value_map_;
116 OriginIdentifierValueMap incognito_value_map_;
118 // Used around accesses to the value map objects to guarantee thread safety.
119 mutable base::Lock lock_;
121 DISALLOW_COPY_AND_ASSIGN(PrefProvider);
124 } // namespace content_settings
126 #endif // COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_CONTENT_SETTINGS_PREF_PROVIDER_H_