OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_PREF_VALUE_MAP_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_PREF_VALUE_MAP_H_ | |
7 | |
8 #include <map> | |
9 #include <set> | |
10 #include <string> | |
11 | |
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" | |
18 | |
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. | |
22 // | |
23 // The semantics of the ExtensionPrefValueMap are: | |
24 // - A regular setting applies to regular browsing sessions as well as incognito | |
25 // browsing sessions. | |
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 | |
28 // extension. | |
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 | |
31 // extension. | |
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. | |
36 // | |
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 | |
50 // | |
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 { | |
57 public: | |
58 // Observer interface for monitoring ExtensionPrefValueMap. | |
59 class Observer { | |
60 public: | |
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 | |
63 // changed. | |
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; | |
70 | |
71 protected: | |
72 virtual ~Observer() {} | |
73 }; | |
74 | |
75 ExtensionPrefValueMap(); | |
76 virtual ~ExtensionPrefValueMap(); | |
77 | |
78 // BrowserContextKeyedService implementation. | |
79 virtual void Shutdown() OVERRIDE; | |
80 | |
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, | |
89 base::Value* value); | |
90 | |
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); | |
96 | |
97 // Returns true if currently no extension with higher precedence controls the | |
98 // preference. | |
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; | |
105 | |
106 // Removes all "incognito session only" preference values. | |
107 void ClearAllIncognitoSessionOnlyPreferences(); | |
108 | |
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; | |
121 | |
122 // Returns the ID of the extension that currently controls this preference. | |
123 // Returns an empty string if this preference is not controlled by an | |
124 // extension. | |
125 std::string GetExtensionControllingPref(const std::string& pref_key) const; | |
126 | |
127 // Tell the store it's now fully initialized. | |
128 void NotifyInitializationCompleted(); | |
129 | |
130 // Registers the time when an extension |ext_id| is installed. | |
131 void RegisterExtension(const std::string& ext_id, | |
132 const base::Time& install_time, | |
133 bool is_enabled); | |
134 | |
135 // Deletes all entries related to extension |ext_id|. | |
136 void UnregisterExtension(const std::string& ext_id); | |
137 | |
138 // Hides or makes the extension preference values of the specified extension | |
139 // visible. | |
140 void SetExtensionState(const std::string& ext_id, bool is_enabled); | |
141 | |
142 // Adds an observer and notifies it about the currently stored keys. | |
143 void AddObserver(Observer* observer); | |
144 | |
145 void RemoveObserver(Observer* observer); | |
146 | |
147 const base::Value* GetEffectivePrefValue(const std::string& key, | |
148 bool incognito, | |
149 bool* from_incognito) const; | |
150 | |
151 private: | |
152 struct ExtensionEntry; | |
153 | |
154 typedef std::map<std::string, ExtensionEntry*> ExtensionEntryMap; | |
155 | |
156 const PrefValueMap* GetExtensionPrefValueMap( | |
157 const std::string& ext_id, | |
158 extensions::ExtensionPrefsScope scope) const; | |
159 | |
160 PrefValueMap* GetExtensionPrefValueMap( | |
161 const std::string& ext_id, | |
162 extensions::ExtensionPrefsScope scope); | |
163 | |
164 // Returns all keys of pref values that are set by the extension of |entry|, | |
165 // regardless whether they are set for incognito or regular pref values. | |
166 void GetExtensionControlledKeys(const ExtensionEntry& entry, | |
167 std::set<std::string>* out) const; | |
168 | |
169 // Returns an iterator to the extension which controls the preference |key|. | |
170 // If |incognito| is true, looks at incognito preferences first. In that case, | |
171 // if |from_incognito| is not NULL, it is set to true if the effective pref | |
172 // value is coming from the incognito preferences, false if it is coming from | |
173 // the normal ones. | |
174 ExtensionEntryMap::const_iterator GetEffectivePrefValueController( | |
175 const std::string& key, | |
176 bool incognito, | |
177 bool* from_incognito) const; | |
178 | |
179 void NotifyOfDestruction(); | |
180 void NotifyPrefValueChanged(const std::string& key); | |
181 void NotifyPrefValueChanged(const std::set<std::string>& keys); | |
182 | |
183 // Mapping of which extension set which preference value. The effective | |
184 // preferences values (i.e. the ones with the highest precedence) | |
185 // are stored in ExtensionPrefStores. | |
186 ExtensionEntryMap entries_; | |
187 | |
188 // In normal Profile shutdown, Shutdown() notifies observers that we are | |
189 // being destroyed. In tests, it isn't called, so the notification must | |
190 // be done in the destructor. This bit tracks whether it has been done yet. | |
191 bool destroyed_; | |
192 | |
193 ObserverList<Observer, true> observers_; | |
194 | |
195 DISALLOW_COPY_AND_ASSIGN(ExtensionPrefValueMap); | |
196 }; | |
197 | |
198 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PREF_VALUE_MAP_H_ | |
OLD | NEW |