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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_CONTENT_SETTINGS_PREF_H_
6 #define COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_CONTENT_SETTINGS_PREF_H_
10 #include "base/callback.h"
11 #include "base/synchronization/lock.h"
12 #include "base/threading/thread_checker.h"
13 #include "base/values.h"
14 #include "components/content_settings/core/browser/content_settings_origin_identifier_value_map.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"
20 class PrefChangeRegistrar
;
24 class DictionaryValue
;
27 namespace user_prefs
{
28 class PrefRegistrySyncable
;
31 namespace content_settings
{
35 // Represents a single pref for reading/writing content settings of one type.
36 class ContentSettingsPref
{
38 typedef base::Callback
<void(const ContentSettingsPattern
&,
39 const ContentSettingsPattern
&,
41 const std::string
&)> NotifyObserversCallback
;
43 ContentSettingsPref(ContentSettingsType content_type
,
45 PrefChangeRegistrar
* registrar
,
46 const std::string
& pref_name
,
48 NotifyObserversCallback notify_callback
);
49 ~ContentSettingsPref();
51 RuleIterator
* GetRuleIterator(const ResourceIdentifier
& resource_identifier
,
52 bool incognito
) const;
54 bool SetWebsiteSetting(const ContentSettingsPattern
& primary_pattern
,
55 const ContentSettingsPattern
& secondary_pattern
,
56 const ResourceIdentifier
& resource_identifier
,
59 void ClearAllContentSettingsRules();
61 void UpdateLastUsage(const ContentSettingsPattern
& primary_pattern
,
62 const ContentSettingsPattern
& secondary_pattern
,
65 base::Time
GetLastUsage(const ContentSettingsPattern
& primary_pattern
,
66 const ContentSettingsPattern
& secondary_pattern
);
68 size_t GetNumExceptions();
70 // Tries to lock |lock_|. If successful, returns true and releases the lock.
71 bool TryLockForTesting() const;
74 // TODO(msramek): Currently only needed in the unittest to get the
75 // corresponding pref name. Remove once pref names are in WebsiteSettingsInfo.
76 friend class DeadlockCheckerObserver
;
78 // Reads all content settings exceptions from the preference and loads them
79 // into the |value_map_|. The |value_map_| is cleared first.
80 void ReadContentSettingsFromPref();
82 // Callback for changes in the pref with the same name.
85 // Update the preference that stores content settings exceptions and syncs the
86 // value to the obsolete preference. When calling this function, |lock_|
87 // should not be held, since this function will send out notifications of
88 // preference changes.
90 const ContentSettingsPattern
& primary_pattern
,
91 const ContentSettingsPattern
& secondary_pattern
,
92 const ResourceIdentifier
& resource_identifier
,
93 const base::Value
* value
);
95 static void CanonicalizeContentSettingsExceptions(
96 base::DictionaryValue
* all_settings_dictionary
);
98 // In the debug mode, asserts that |lock_| is not held by this thread. It's
99 // ok if some other thread holds |lock_|, as long as it will eventually
101 void AssertLockNotHeld() const;
103 // The type of content settings stored in this pref.
104 ContentSettingsType content_type_
;
106 // Weak; owned by the Profile and reset in ShutdownOnUIThread.
109 // Owned by the PrefProvider.
110 PrefChangeRegistrar
* registrar_
;
112 // Name of the dictionary preference managed by this class.
113 const std::string
& pref_name_
;
117 // Whether we are currently updating preferences, this is used to ignore
118 // notifications from the preferences service that we triggered ourself.
119 bool updating_preferences_
;
121 OriginIdentifierValueMap value_map_
;
123 OriginIdentifierValueMap incognito_value_map_
;
125 NotifyObserversCallback notify_callback_
;
127 // Used around accesses to the value map objects to guarantee thread safety.
128 mutable base::Lock lock_
;
130 base::ThreadChecker thread_checker_
;
132 DISALLOW_COPY_AND_ASSIGN(ContentSettingsPref
);
135 } // namespace content_settings
137 #endif // COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_CONTENT_SETTINGS_PREF_H_