OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/extension_pref_value_map.h" | 5 #include "chrome/browser/extensions/extension_pref_value_map.h" |
6 | 6 |
7 #include "base/stl_util-inl.h" | 7 #include "base/stl_util-inl.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chrome/browser/prefs/pref_value_map.h" | 9 #include "chrome/browser/prefs/pref_value_map.h" |
10 | 10 |
11 struct ExtensionPrefValueMap::ExtensionEntry { | 11 struct ExtensionPrefValueMap::ExtensionEntry { |
12 // Installation time of the extension. | 12 // Installation time of the extension. |
13 base::Time install_time; | 13 base::Time install_time; |
14 // Whether extension is enabled in the profile. | 14 // Whether extension is enabled in the profile. |
15 bool enabled; | 15 bool enabled; |
16 // Regular preferences. | 16 // Extension controlled preferences for the regular profile. |
17 PrefValueMap reg_preferences; | 17 PrefValueMap regular_profile_preferences; |
18 // Incognito preferences, empty for regular ExtensionPrefStore. | 18 // Persistent extension controlled preferences for the incognito profile, |
19 PrefValueMap inc_preferences; | 19 // empty for regular profile ExtensionPrefStore. |
| 20 PrefValueMap incognito_profile_preferences_persistent; |
20 }; | 21 }; |
21 | 22 |
22 ExtensionPrefValueMap::ExtensionPrefValueMap() { | 23 ExtensionPrefValueMap::ExtensionPrefValueMap() { |
23 } | 24 } |
24 | 25 |
25 ExtensionPrefValueMap::~ExtensionPrefValueMap() { | 26 ExtensionPrefValueMap::~ExtensionPrefValueMap() { |
26 NotifyOfDestruction(); | 27 NotifyOfDestruction(); |
27 STLDeleteValues(&entries_); | 28 STLDeleteValues(&entries_); |
28 entries_.clear(); | 29 entries_.clear(); |
29 } | 30 } |
30 | 31 |
31 void ExtensionPrefValueMap::SetExtensionPref(const std::string& ext_id, | 32 void ExtensionPrefValueMap::SetExtensionPref(const std::string& ext_id, |
32 const std::string& key, | 33 const std::string& key, |
33 bool incognito, | 34 extension_prefs_scope::Scope scope, |
34 Value* value) { | 35 Value* value) { |
35 PrefValueMap* prefs = GetExtensionPrefValueMap(ext_id, incognito); | 36 PrefValueMap* prefs = GetExtensionPrefValueMap(ext_id, scope); |
36 | 37 |
37 if (prefs->SetValue(key, value)) | 38 if (prefs->SetValue(key, value)) |
38 NotifyPrefValueChanged(key); | 39 NotifyPrefValueChanged(key); |
39 } | 40 } |
40 | 41 |
41 void ExtensionPrefValueMap::RemoveExtensionPref(const std::string& ext_id, | 42 void ExtensionPrefValueMap::RemoveExtensionPref( |
42 const std::string& key, | 43 const std::string& ext_id, |
43 bool incognito) { | 44 const std::string& key, |
44 PrefValueMap* prefs = GetExtensionPrefValueMap(ext_id, incognito); | 45 extension_prefs_scope::Scope scope) { |
| 46 PrefValueMap* prefs = GetExtensionPrefValueMap(ext_id, scope); |
45 if (prefs->RemoveValue(key)) | 47 if (prefs->RemoveValue(key)) |
46 NotifyPrefValueChanged(key); | 48 NotifyPrefValueChanged(key); |
47 } | 49 } |
48 | 50 |
49 bool ExtensionPrefValueMap::CanExtensionControlPref( | 51 bool ExtensionPrefValueMap::CanExtensionControlPref( |
50 const std::string& extension_id, | 52 const std::string& extension_id, |
51 const std::string& pref_key, | 53 const std::string& pref_key, |
52 bool incognito) const { | 54 bool incognito) const { |
53 ExtensionEntryMap::const_iterator ext = entries_.find(extension_id); | 55 ExtensionEntryMap::const_iterator ext = entries_.find(extension_id); |
54 if (ext == entries_.end()) { | 56 if (ext == entries_.end()) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 if (i->second->enabled == is_enabled) | 110 if (i->second->enabled == is_enabled) |
109 return; | 111 return; |
110 std::set<std::string> keys; // keys set by this extension | 112 std::set<std::string> keys; // keys set by this extension |
111 GetExtensionControlledKeys(*(i->second), &keys); | 113 GetExtensionControlledKeys(*(i->second), &keys); |
112 i->second->enabled = is_enabled; | 114 i->second->enabled = is_enabled; |
113 NotifyPrefValueChanged(keys); | 115 NotifyPrefValueChanged(keys); |
114 } | 116 } |
115 | 117 |
116 PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap( | 118 PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap( |
117 const std::string& ext_id, | 119 const std::string& ext_id, |
118 bool incognito) { | 120 extension_prefs_scope::Scope scope) { |
119 ExtensionEntryMap::const_iterator i = entries_.find(ext_id); | 121 ExtensionEntryMap::const_iterator i = entries_.find(ext_id); |
120 CHECK(i != entries_.end()); | 122 CHECK(i != entries_.end()); |
121 return incognito ? &(i->second->inc_preferences) | 123 switch (scope) { |
122 : &(i->second->reg_preferences); | 124 case extension_prefs_scope::kRegular: |
| 125 return &(i->second->regular_profile_preferences); |
| 126 case extension_prefs_scope::kIncognitoPersistent: |
| 127 return &(i->second->incognito_profile_preferences_persistent); |
| 128 } |
| 129 NOTREACHED(); |
| 130 return NULL; |
123 } | 131 } |
124 | 132 |
125 const PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap( | 133 const PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap( |
126 const std::string& ext_id, | 134 const std::string& ext_id, |
127 bool incognito) const { | 135 extension_prefs_scope::Scope scope) const { |
128 ExtensionEntryMap::const_iterator i = entries_.find(ext_id); | 136 ExtensionEntryMap::const_iterator i = entries_.find(ext_id); |
129 CHECK(i != entries_.end()); | 137 CHECK(i != entries_.end()); |
130 return incognito ? &(i->second->inc_preferences) | 138 switch (scope) { |
131 : &(i->second->reg_preferences); | 139 case extension_prefs_scope::kRegular: |
| 140 return &(i->second->regular_profile_preferences); |
| 141 case extension_prefs_scope::kIncognitoPersistent: |
| 142 return &(i->second->incognito_profile_preferences_persistent); |
| 143 } |
| 144 NOTREACHED(); |
| 145 return NULL; |
132 } | 146 } |
133 | 147 |
134 void ExtensionPrefValueMap::GetExtensionControlledKeys( | 148 void ExtensionPrefValueMap::GetExtensionControlledKeys( |
135 const ExtensionEntry& entry, | 149 const ExtensionEntry& entry, |
136 std::set<std::string>* out) const { | 150 std::set<std::string>* out) const { |
137 PrefValueMap::const_iterator i; | 151 PrefValueMap::const_iterator i; |
138 | 152 |
139 const PrefValueMap& reg_prefs = entry.reg_preferences; | 153 const PrefValueMap& reg_prefs = entry.regular_profile_preferences; |
140 for (i = reg_prefs.begin(); i != reg_prefs.end(); ++i) | 154 for (i = reg_prefs.begin(); i != reg_prefs.end(); ++i) |
141 out->insert(i->first); | 155 out->insert(i->first); |
142 | 156 |
143 const PrefValueMap& inc_prefs = entry.inc_preferences; | 157 const PrefValueMap& inc_prefs_pers = |
144 for (i = inc_prefs.begin(); i != inc_prefs.end(); ++i) | 158 entry.incognito_profile_preferences_persistent; |
| 159 for (i = inc_prefs_pers.begin(); i != inc_prefs_pers.end(); ++i) |
145 out->insert(i->first); | 160 out->insert(i->first); |
146 } | 161 } |
147 | 162 |
148 const Value* ExtensionPrefValueMap::GetEffectivePrefValue( | 163 const Value* ExtensionPrefValueMap::GetEffectivePrefValue( |
149 const std::string& key, | 164 const std::string& key, |
150 bool incognito, | 165 bool incognito, |
151 bool* from_incognito) const { | 166 bool* from_incognito) const { |
152 ExtensionEntryMap::const_iterator winner = | 167 ExtensionEntryMap::const_iterator winner = |
153 GetEffectivePrefValueController(key, incognito, from_incognito); | 168 GetEffectivePrefValueController(key, incognito, from_incognito); |
154 if (winner == entries_.end()) | 169 if (winner == entries_.end()) |
155 return NULL; | 170 return NULL; |
156 | 171 |
157 const Value* value = NULL; | 172 const Value* value = NULL; |
158 const std::string& ext_id = winner->first; | 173 const std::string& ext_id = winner->first; |
159 if (incognito) | 174 if (incognito) { |
160 GetExtensionPrefValueMap(ext_id, true)->GetValue(key, &value); | 175 const PrefValueMap* prefs = GetExtensionPrefValueMap( |
161 if (!value) | 176 ext_id, extension_prefs_scope::kIncognitoPersistent); |
162 GetExtensionPrefValueMap(ext_id, false)->GetValue(key, &value); | 177 prefs->GetValue(key, &value); |
| 178 } |
| 179 if (!value) { |
| 180 const PrefValueMap* prefs = GetExtensionPrefValueMap( |
| 181 ext_id, extension_prefs_scope::kRegular); |
| 182 prefs->GetValue(key, &value); |
| 183 } |
163 return value; | 184 return value; |
164 } | 185 } |
165 | 186 |
166 ExtensionPrefValueMap::ExtensionEntryMap::const_iterator | 187 ExtensionPrefValueMap::ExtensionEntryMap::const_iterator |
167 ExtensionPrefValueMap::GetEffectivePrefValueController( | 188 ExtensionPrefValueMap::GetEffectivePrefValueController( |
168 const std::string& key, | 189 const std::string& key, |
169 bool incognito, | 190 bool incognito, |
170 bool* from_incognito) const { | 191 bool* from_incognito) const { |
171 ExtensionEntryMap::const_iterator winner = entries_.end(); | 192 ExtensionEntryMap::const_iterator winner = entries_.end(); |
172 base::Time winners_install_time; | 193 base::Time winners_install_time; |
173 | 194 |
174 ExtensionEntryMap::const_iterator i; | 195 ExtensionEntryMap::const_iterator i; |
175 for (i = entries_.begin(); i != entries_.end(); ++i) { | 196 for (i = entries_.begin(); i != entries_.end(); ++i) { |
176 const std::string& ext_id = i->first; | 197 const std::string& ext_id = i->first; |
177 const base::Time& install_time = i->second->install_time; | 198 const base::Time& install_time = i->second->install_time; |
178 const bool enabled = i->second->enabled; | 199 const bool enabled = i->second->enabled; |
179 | 200 |
180 if (!enabled) | 201 if (!enabled) |
181 continue; | 202 continue; |
182 if (install_time < winners_install_time) | 203 if (install_time < winners_install_time) |
183 continue; | 204 continue; |
184 | 205 |
185 const Value* value = NULL; | 206 const Value* value = NULL; |
186 const PrefValueMap* prefs = GetExtensionPrefValueMap(ext_id, false); | 207 const PrefValueMap* prefs = GetExtensionPrefValueMap( |
| 208 ext_id, extension_prefs_scope::kRegular); |
187 if (prefs->GetValue(key, &value)) { | 209 if (prefs->GetValue(key, &value)) { |
188 winner = i; | 210 winner = i; |
189 winners_install_time = install_time; | 211 winners_install_time = install_time; |
190 if (from_incognito) | 212 if (from_incognito) |
191 *from_incognito = false; | 213 *from_incognito = false; |
192 } | 214 } |
193 | 215 |
194 if (!incognito) | 216 if (!incognito) |
195 continue; | 217 continue; |
196 | 218 |
197 prefs = GetExtensionPrefValueMap(ext_id, true); | 219 prefs = GetExtensionPrefValueMap( |
| 220 ext_id, extension_prefs_scope::kIncognitoPersistent); |
198 if (prefs->GetValue(key, &value)) { | 221 if (prefs->GetValue(key, &value)) { |
199 winner = i; | 222 winner = i; |
200 winners_install_time = install_time; | 223 winners_install_time = install_time; |
201 if (from_incognito) | 224 if (from_incognito) |
202 *from_incognito = true; | 225 *from_incognito = true; |
203 } | 226 } |
204 } | 227 } |
205 return winner; | 228 return winner; |
206 } | 229 } |
207 | 230 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 | 262 |
240 void ExtensionPrefValueMap::NotifyPrefValueChanged(const std::string& key) { | 263 void ExtensionPrefValueMap::NotifyPrefValueChanged(const std::string& key) { |
241 FOR_EACH_OBSERVER(ExtensionPrefValueMap::Observer, observers_, | 264 FOR_EACH_OBSERVER(ExtensionPrefValueMap::Observer, observers_, |
242 OnPrefValueChanged(key)); | 265 OnPrefValueChanged(key)); |
243 } | 266 } |
244 | 267 |
245 void ExtensionPrefValueMap::NotifyOfDestruction() { | 268 void ExtensionPrefValueMap::NotifyOfDestruction() { |
246 FOR_EACH_OBSERVER(ExtensionPrefValueMap::Observer, observers_, | 269 FOR_EACH_OBSERVER(ExtensionPrefValueMap::Observer, observers_, |
247 OnExtensionPrefValueMapDestruction()); | 270 OnExtensionPrefValueMapDestruction()); |
248 } | 271 } |
OLD | NEW |