OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 #pragma once | |
8 | |
9 #include <map> | |
10 #include <set> | |
11 #include <string> | |
12 | |
13 #include "base/time.h" | |
14 #include "chrome/browser/prefs/value_map_pref_store.h" | |
15 | |
16 // Non-persistent data container that is shared by ExtensionPrefStores. All | |
17 // extension pref values (incognito and regular) are stored herein and | |
18 // provided to ExtensionPrefStores. | |
19 // | |
20 // The semantics of the ExtensionPrefValueMap are: | |
21 // - The precedence of extensions is determined by their installation time. | |
22 // The extension that has been installed later takes higher precedence. | |
23 // - If two extensions set a value for the same preference, the following | |
24 // rules determine which value becomes effective (visible). | |
25 // - The effective regular extension pref value is determined by the regular | |
26 // extension pref value of the extension with the highest precedence. | |
27 // - The effective incognito extension pref value is determined by the incognito | |
28 // extension pref value of the extension with the highest precedence, unless | |
29 // another extension with higher precedence overrides it with a regular | |
30 // extension pref value. | |
31 // | |
32 // The following table illustrates the behavior: | |
33 // A.reg | A.inc | B.reg | B.inc | E.reg | E.inc | |
34 // 1 | - | - | - | 1 | 1 | |
35 // 1 | 2 | - | - | 1 | 2 | |
36 // 1 | - | 3 | - | 3 | 3 | |
37 // 1 | - | - | 4 | 1 | 4 | |
38 // 1 | 2 | 3 | - | 3 | 3(!) | |
39 // 1 | 2 | - | 4 | 1 | 4 | |
40 // 1 | 2 | 3 | 4 | 3 | 4 | |
41 // A = extension A, B = extension B, E = effective value | |
42 // .reg = regular value | |
43 // .inc = incognito value | |
44 // Extension B has higher precedence than A. | |
45 class ExtensionPrefValueMap { | |
46 public: | |
47 // Observer interface for monitoring ExtensionPrefValueMap. | |
48 class Observer { | |
49 public: | |
50 virtual ~Observer() {} | |
51 | |
52 // Called when the value for the given |key| set by one of the extensions | |
53 // changes. This does not necessarily mean that the effective value has | |
54 // changed. | |
55 virtual void OnPrefValueChanged(const std::string& key) = 0; | |
56 // Notification about the ExtensionPrefValueMap being fully initialized. | |
57 virtual void OnInitializationCompleted() = 0; | |
58 // Called when the ExtensionPrefValueMap is being destroyed. When called, | |
59 // observers must unsubscribe. | |
60 virtual void OnExtensionPrefValueMapDestruction() = 0; | |
61 }; | |
62 | |
63 ExtensionPrefValueMap(); | |
64 virtual ~ExtensionPrefValueMap(); | |
65 | |
66 // Set an extension preference |value| for |key| of extension |ext_id|. | |
67 // Takes ownership of |value|. | |
68 // Note that regular extension pref values need to be reported to | |
69 // incognito and to regular ExtensionPrefStores. | |
70 // Precondition: the extension must be registered. | |
71 void SetExtensionPref(const std::string& ext_id, | |
72 const std::string& key, | |
73 bool incognito, | |
74 Value* value); | |
75 | |
76 // Remove the extension preference value for |key| of extension |ext_id|. | |
77 // Precondition: the extension must be registered. | |
78 void RemoveExtensionPref(const std::string& ext_id, | |
79 const std::string& key, | |
80 bool incognito); | |
81 | |
82 // Tell the store it's now fully initialized. | |
83 void NotifyInitializationCompleted(); | |
84 | |
85 // Registers the time when an extension |ext_id| is installed. | |
86 void RegisterExtension(const std::string& ext_id, | |
87 const base::Time& install_time, | |
88 bool is_enabled); | |
89 | |
90 // Deletes all entries related to extension |ext_id|. | |
91 void UnregisterExtension(const std::string& ext_id); | |
92 | |
93 // Hides or makes the extension preference values of the specified extension | |
94 // visible. | |
95 void SetExtensionState(const std::string& ext_id, bool is_enabled); | |
96 | |
97 // Adds an observer and notifies it about the currently stored keys. | |
98 void AddObserver(Observer* observer); | |
danno
2011/01/14 13:05:09
Perhaps the observer interface should be protected
battre
2011/01/20 17:59:29
Discussed this with mnissler and decided not to do
| |
99 | |
100 void RemoveObserver(Observer* observer); | |
101 | |
102 const Value* GetEffectivePrefValue(const std::string& key, | |
103 bool incognito) const; | |
104 | |
105 private: | |
106 struct ExtensionEntry { | |
107 // Installation time of the extension. | |
108 base::Time install_time; | |
109 // Whether extension is enabled in the profile. | |
110 bool enabled; | |
111 // Regular preferences. | |
112 PrefValueMap reg_preferences; | |
113 // Incognito preferences, empty for regular ExtensionPrefStore. | |
114 PrefValueMap inc_preferences; | |
115 }; | |
116 | |
117 typedef std::map<std::string, ExtensionEntry*> ExtensionEntryMap; | |
118 | |
119 const PrefValueMap* GetExtensionPrefValueMap(const std::string& ext_id, | |
120 bool incognito) const; | |
121 | |
122 PrefValueMap* GetExtensionPrefValueMap(const std::string& ext_id, | |
123 bool incognito); | |
124 | |
125 // Returns all keys of pref values that are set by the extension of |entry|, | |
126 // regardless whether they are set for incognito or regular pref values. | |
127 void GetExtensionControlledKeys(const ExtensionEntry& entry, | |
128 std::set<std::string>* out) const; | |
129 | |
130 void NotifyOfDestruction(); | |
131 void NotifyPrefValueChanged(const std::string& key); | |
132 void NotifyPrefValueChanged(const std::set<std::string>& keys); | |
133 | |
134 // Mapping of which extension set which preference value. The effective | |
135 // preferences values (i.e. the ones with the highest precedence) | |
136 // are stored in ExtensionPrefStores. | |
137 ExtensionEntryMap entries_; | |
138 | |
139 ObserverList<Observer, true> observers_; | |
140 | |
141 DISALLOW_COPY_AND_ASSIGN(ExtensionPrefValueMap); | |
142 }; | |
143 | |
144 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PREF_VALUE_MAP_H_ | |
OLD | NEW |