1 // Copyright 2014 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 EXTENSIONS_BROWSER_EXTENSION_PREF_VALUE_MAP_H_
6 #define EXTENSIONS_BROWSER_EXTENSION_PREF_VALUE_MAP_H_
12 #include "base/observer_list.h"
13 #include "base/prefs/pref_value_map.h"
14 #include "base/time/time.h"
15 #include "base/values.h"
16 #include "components/keyed_service/core/keyed_service.h"
17 #include "extensions/browser/extension_prefs_scope.h"
19 // Non-persistent data container that is shared by ExtensionPrefStores. All
20 // extension pref values (incognito and regular) are stored herein and
21 // provided to ExtensionPrefStores.
23 // The semantics of the ExtensionPrefValueMap are:
24 // - A regular setting applies to regular browsing sessions as well as incognito
26 // - An incognito setting applies only to incognito browsing sessions, not to
27 // regular ones. It takes precedence over a regular setting set by the same
29 // - A regular-only setting applies only to regular browsing sessions, not to
30 // incognito ones. It takes precedence over a regular setting set by the same
32 // - If two different extensions set a value for the same preference (and both
33 // values apply to the regular/incognito browsing session), the extension that
34 // was installed later takes precedence, regardless of whether the settings
35 // are regular, incognito or regular-only.
37 // The following table illustrates the behavior:
38 // A.reg | A.reg_only | A.inc | B.reg | B.reg_only | B.inc | E.reg | E.inc
39 // 1 | - | - | - | - | - | 1 | 1
40 // 1 | 2 | - | - | - | - | 2 | 1
41 // 1 | - | 3 | - | - | - | 1 | 3
42 // 1 | 2 | 3 | - | - | - | 2 | 3
43 // 1 | - | - | 4 | - | - | 4 | 4
44 // 1 | 2 | 3 | 4 | - | - | 4 | 4
45 // 1 | - | - | - | 5 | - | 5 | 1
46 // 1 | - | 3 | 4 | 5 | - | 5 | 4
47 // 1 | - | - | - | - | 6 | 1 | 6
48 // 1 | 2 | - | 4 | - | 6 | 4 | 6
49 // 1 | 2 | 3 | - | 5 | 6 | 5 | 6
51 // A = extension A, B = extension B, E = effective value
52 // .reg = regular value
53 // .reg_only = regular-only value
54 // .inc = incognito value
55 // Extension B has higher precedence than A.
56 class ExtensionPrefValueMap
: public KeyedService
{
58 // Observer interface for monitoring ExtensionPrefValueMap.
61 // Called when the value for the given |key| set by one of the extensions
62 // changes. This does not necessarily mean that the effective value has
64 virtual void OnPrefValueChanged(const std::string
& key
) = 0;
65 // Notification about the ExtensionPrefValueMap being fully initialized.
66 virtual void OnInitializationCompleted() = 0;
67 // Called when the ExtensionPrefValueMap is being destroyed. When called,
68 // observers must unsubscribe.
69 virtual void OnExtensionPrefValueMapDestruction() = 0;
72 virtual ~Observer() {}
75 ExtensionPrefValueMap();
76 ~ExtensionPrefValueMap() override
;
78 // KeyedService implementation.
79 void Shutdown() override
;
81 // Set an extension preference |value| for |key| of extension |ext_id|.
82 // Takes ownership of |value|.
83 // Note that regular extension pref values need to be reported to
84 // incognito and to regular ExtensionPrefStores.
85 // Precondition: the extension must be registered.
86 void SetExtensionPref(const std::string
& ext_id
,
87 const std::string
& key
,
88 extensions::ExtensionPrefsScope scope
,
91 // Remove the extension preference value for |key| of extension |ext_id|.
92 // Precondition: the extension must be registered.
93 void RemoveExtensionPref(const std::string
& ext_id
,
94 const std::string
& key
,
95 extensions::ExtensionPrefsScope scope
);
97 // Returns true if currently no extension with higher precedence controls the
98 // preference. If |incognito| is true and the extension does not have
99 // incognito permission, CanExtensionControlPref returns false.
100 // Note that this function does does not consider the existence of
101 // policies. An extension is only really able to control a preference if
102 // PrefService::Preference::IsExtensionModifiable() returns true as well.
103 bool CanExtensionControlPref(const std::string
& extension_id
,
104 const std::string
& pref_key
,
105 bool incognito
) const;
107 // Removes all "incognito session only" preference values.
108 void ClearAllIncognitoSessionOnlyPreferences();
110 // Returns true if an extension identified by |extension_id| controls the
111 // preference. This means this extension has set a preference value and no
112 // other extension with higher precedence overrides it. If |from_incognito|
113 // is not NULL, looks at incognito preferences first, and |from_incognito| is
114 // set to true if the effective pref value is coming from the incognito
115 // preferences, false if it is coming from the normal ones.
116 // Note that the this function does does not consider the existence of
117 // policies. An extension is only really able to control a preference if
118 // PrefService::Preference::IsExtensionModifiable() returns true as well.
119 bool DoesExtensionControlPref(const std::string
& extension_id
,
120 const std::string
& pref_key
,
121 bool* from_incognito
) const;
123 // Returns the ID of the extension that currently controls this preference
124 // for a regular profile. Incognito settings are ignored.
125 // Returns an empty string if this preference is not controlled by an
127 std::string
GetExtensionControllingPref(const std::string
& pref_key
) const;
129 // Tell the store it's now fully initialized.
130 void NotifyInitializationCompleted();
132 // Registers the time when an extension |ext_id| is installed.
133 void RegisterExtension(const std::string
& ext_id
,
134 const base::Time
& install_time
,
136 bool is_incognito_enabled
);
138 // Deletes all entries related to extension |ext_id|.
139 void UnregisterExtension(const std::string
& ext_id
);
141 // Hides or makes the extension preference values of the specified extension
143 void SetExtensionState(const std::string
& ext_id
, bool is_enabled
);
145 // Sets whether the extension has permission to access incognito state.
146 void SetExtensionIncognitoState(const std::string
& ext_id
,
147 bool is_incognito_enabled
);
149 // Adds an observer and notifies it about the currently stored keys.
150 void AddObserver(Observer
* observer
);
152 void RemoveObserver(Observer
* observer
);
154 const base::Value
* GetEffectivePrefValue(const std::string
& key
,
156 bool* from_incognito
) const;
159 struct ExtensionEntry
;
161 typedef std::map
<std::string
, ExtensionEntry
*> ExtensionEntryMap
;
163 const PrefValueMap
* GetExtensionPrefValueMap(
164 const std::string
& ext_id
,
165 extensions::ExtensionPrefsScope scope
) const;
167 PrefValueMap
* GetExtensionPrefValueMap(
168 const std::string
& ext_id
,
169 extensions::ExtensionPrefsScope scope
);
171 // Returns all keys of pref values that are set by the extension of |entry|,
172 // regardless whether they are set for incognito or regular pref values.
173 void GetExtensionControlledKeys(const ExtensionEntry
& entry
,
174 std::set
<std::string
>* out
) const;
176 // Returns an iterator to the extension which controls the preference |key|.
177 // If |incognito| is true, looks at incognito preferences first. In that case,
178 // if |from_incognito| is not NULL, it is set to true if the effective pref
179 // value is coming from the incognito preferences, false if it is coming from
181 ExtensionEntryMap::const_iterator
GetEffectivePrefValueController(
182 const std::string
& key
,
184 bool* from_incognito
) const;
186 void NotifyOfDestruction();
187 void NotifyPrefValueChanged(const std::string
& key
);
188 void NotifyPrefValueChanged(const std::set
<std::string
>& keys
);
190 // Mapping of which extension set which preference value. The effective
191 // preferences values (i.e. the ones with the highest precedence)
192 // are stored in ExtensionPrefStores.
193 ExtensionEntryMap entries_
;
195 // In normal Profile shutdown, Shutdown() notifies observers that we are
196 // being destroyed. In tests, it isn't called, so the notification must
197 // be done in the destructor. This bit tracks whether it has been done yet.
200 base::ObserverList
<Observer
, true> observers_
;
202 DISALLOW_COPY_AND_ASSIGN(ExtensionPrefValueMap
);
205 #endif // EXTENSIONS_BROWSER_EXTENSION_PREF_VALUE_MAP_H_