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 CHROME_BROWSER_EXTENSIONS_EXTENSION_PREF_VALUE_MAP_H_
6 #define CHROME_BROWSER_EXTENSIONS_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/browser_context_keyed_service/browser_context_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 BrowserContextKeyedService {
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 virtual ~ExtensionPrefValueMap();
78 // BrowserContextKeyedService implementation.
79 virtual 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
99 // Note that the this function does does not consider the existence of
100 // policies. An extension is only really able to control a preference if
101 // PrefService::Preference::IsExtensionModifiable() returns true as well.
102 bool CanExtensionControlPref(const std::string& extension_id,
103 const std::string& pref_key,
104 bool incognito) const;
106 // Removes all "incognito session only" preference values.
107 void ClearAllIncognitoSessionOnlyPreferences();
109 // Returns true if an extension identified by |extension_id| controls the
110 // preference. This means this extension has set a preference value and no
111 // other extension with higher precedence overrides it. If |from_incognito|
112 // is not NULL, looks at incognito preferences first, and |from_incognito| is
113 // set to true if the effective pref value is coming from the incognito
114 // preferences, false if it is coming from the normal ones.
115 // Note that the this function does does not consider the existence of
116 // policies. An extension is only really able to control a preference if
117 // PrefService::Preference::IsExtensionModifiable() returns true as well.
118 bool DoesExtensionControlPref(const std::string& extension_id,
119 const std::string& pref_key,
120 bool* from_incognito) const;
122 // Tell the store it's now fully initialized.
123 void NotifyInitializationCompleted();
125 // Registers the time when an extension |ext_id| is installed.
126 void RegisterExtension(const std::string& ext_id,
127 const base::Time& install_time,
130 // Deletes all entries related to extension |ext_id|.
131 void UnregisterExtension(const std::string& ext_id);
133 // Hides or makes the extension preference values of the specified extension
135 void SetExtensionState(const std::string& ext_id, bool is_enabled);
137 // Adds an observer and notifies it about the currently stored keys.
138 void AddObserver(Observer* observer);
140 void RemoveObserver(Observer* observer);
142 const base::Value* GetEffectivePrefValue(const std::string& key,
144 bool* from_incognito) const;
147 struct ExtensionEntry;
149 typedef std::map<std::string, ExtensionEntry*> ExtensionEntryMap;
151 const PrefValueMap* GetExtensionPrefValueMap(
152 const std::string& ext_id,
153 extensions::ExtensionPrefsScope scope) const;
155 PrefValueMap* GetExtensionPrefValueMap(
156 const std::string& ext_id,
157 extensions::ExtensionPrefsScope scope);
159 // Returns all keys of pref values that are set by the extension of |entry|,
160 // regardless whether they are set for incognito or regular pref values.
161 void GetExtensionControlledKeys(const ExtensionEntry& entry,
162 std::set<std::string>* out) const;
164 // Returns an iterator to the extension which controls the preference |key|.
165 // If |incognito| is true, looks at incognito preferences first. In that case,
166 // if |from_incognito| is not NULL, it is set to true if the effective pref
167 // value is coming from the incognito preferences, false if it is coming from
169 ExtensionEntryMap::const_iterator GetEffectivePrefValueController(
170 const std::string& key,
172 bool* from_incognito) const;
174 void NotifyOfDestruction();
175 void NotifyPrefValueChanged(const std::string& key);
176 void NotifyPrefValueChanged(const std::set<std::string>& keys);
178 // Mapping of which extension set which preference value. The effective
179 // preferences values (i.e. the ones with the highest precedence)
180 // are stored in ExtensionPrefStores.
181 ExtensionEntryMap entries_;
183 // In normal Profile shutdown, Shutdown() notifies observers that we are
184 // being destroyed. In tests, it isn't called, so the notification must
185 // be done in the destructor. This bit tracks whether it has been done yet.
188 ObserverList<Observer, true> observers_;
190 DISALLOW_COPY_AND_ASSIGN(ExtensionPrefValueMap);
193 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PREF_VALUE_MAP_H_