OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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.h" | 7 #include "base/stl_util.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 using extensions::ExtensionPrefsScope; |
| 12 |
11 struct ExtensionPrefValueMap::ExtensionEntry { | 13 struct ExtensionPrefValueMap::ExtensionEntry { |
12 // Installation time of the extension. | 14 // Installation time of the extension. |
13 base::Time install_time; | 15 base::Time install_time; |
14 // Whether extension is enabled in the profile. | 16 // Whether extension is enabled in the profile. |
15 bool enabled; | 17 bool enabled; |
16 // Extension controlled preferences for the regular profile. | 18 // Extension controlled preferences for the regular profile. |
17 PrefValueMap regular_profile_preferences; | 19 PrefValueMap regular_profile_preferences; |
18 // Extension controlled preferences that should *only* apply to the regular | 20 // Extension controlled preferences that should *only* apply to the regular |
19 // profile. | 21 // profile. |
20 PrefValueMap regular_only_profile_preferences; | 22 PrefValueMap regular_only_profile_preferences; |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 i->second->enabled = is_enabled; | 150 i->second->enabled = is_enabled; |
149 NotifyPrefValueChanged(keys); | 151 NotifyPrefValueChanged(keys); |
150 } | 152 } |
151 | 153 |
152 PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap( | 154 PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap( |
153 const std::string& ext_id, | 155 const std::string& ext_id, |
154 ExtensionPrefsScope scope) { | 156 ExtensionPrefsScope scope) { |
155 ExtensionEntryMap::const_iterator i = entries_.find(ext_id); | 157 ExtensionEntryMap::const_iterator i = entries_.find(ext_id); |
156 CHECK(i != entries_.end()); | 158 CHECK(i != entries_.end()); |
157 switch (scope) { | 159 switch (scope) { |
158 case kExtensionPrefsScopeRegular: | 160 case extensions::kExtensionPrefsScopeRegular: |
159 return &(i->second->regular_profile_preferences); | 161 return &(i->second->regular_profile_preferences); |
160 case kExtensionPrefsScopeRegularOnly: | 162 case extensions::kExtensionPrefsScopeRegularOnly: |
161 return &(i->second->regular_only_profile_preferences); | 163 return &(i->second->regular_only_profile_preferences); |
162 case kExtensionPrefsScopeIncognitoPersistent: | 164 case extensions::kExtensionPrefsScopeIncognitoPersistent: |
163 return &(i->second->incognito_profile_preferences_persistent); | 165 return &(i->second->incognito_profile_preferences_persistent); |
164 case kExtensionPrefsScopeIncognitoSessionOnly: | 166 case extensions::kExtensionPrefsScopeIncognitoSessionOnly: |
165 return &(i->second->incognito_profile_preferences_session_only); | 167 return &(i->second->incognito_profile_preferences_session_only); |
166 } | 168 } |
167 NOTREACHED(); | 169 NOTREACHED(); |
168 return NULL; | 170 return NULL; |
169 } | 171 } |
170 | 172 |
171 const PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap( | 173 const PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap( |
172 const std::string& ext_id, | 174 const std::string& ext_id, |
173 ExtensionPrefsScope scope) const { | 175 ExtensionPrefsScope scope) const { |
174 ExtensionEntryMap::const_iterator i = entries_.find(ext_id); | 176 ExtensionEntryMap::const_iterator i = entries_.find(ext_id); |
175 CHECK(i != entries_.end()); | 177 CHECK(i != entries_.end()); |
176 switch (scope) { | 178 switch (scope) { |
177 case kExtensionPrefsScopeRegular: | 179 case extensions::kExtensionPrefsScopeRegular: |
178 return &(i->second->regular_profile_preferences); | 180 return &(i->second->regular_profile_preferences); |
179 case kExtensionPrefsScopeRegularOnly: | 181 case extensions::kExtensionPrefsScopeRegularOnly: |
180 return &(i->second->regular_only_profile_preferences); | 182 return &(i->second->regular_only_profile_preferences); |
181 case kExtensionPrefsScopeIncognitoPersistent: | 183 case extensions::kExtensionPrefsScopeIncognitoPersistent: |
182 return &(i->second->incognito_profile_preferences_persistent); | 184 return &(i->second->incognito_profile_preferences_persistent); |
183 case kExtensionPrefsScopeIncognitoSessionOnly: | 185 case extensions::kExtensionPrefsScopeIncognitoSessionOnly: |
184 return &(i->second->incognito_profile_preferences_session_only); | 186 return &(i->second->incognito_profile_preferences_session_only); |
185 } | 187 } |
186 NOTREACHED(); | 188 NOTREACHED(); |
187 return NULL; | 189 return NULL; |
188 } | 190 } |
189 | 191 |
190 void ExtensionPrefValueMap::GetExtensionControlledKeys( | 192 void ExtensionPrefValueMap::GetExtensionControlledKeys( |
191 const ExtensionEntry& entry, | 193 const ExtensionEntry& entry, |
192 std::set<std::string>* out) const { | 194 std::set<std::string>* out) const { |
193 PrefValueMap::const_iterator i; | 195 PrefValueMap::const_iterator i; |
(...skipping 26 matching lines...) Expand all Loading... |
220 GetEffectivePrefValueController(key, incognito, from_incognito); | 222 GetEffectivePrefValueController(key, incognito, from_incognito); |
221 if (winner == entries_.end()) | 223 if (winner == entries_.end()) |
222 return NULL; | 224 return NULL; |
223 | 225 |
224 const Value* value = NULL; | 226 const Value* value = NULL; |
225 const std::string& ext_id = winner->first; | 227 const std::string& ext_id = winner->first; |
226 | 228 |
227 // First search for incognito session only preferences. | 229 // First search for incognito session only preferences. |
228 if (incognito) { | 230 if (incognito) { |
229 const PrefValueMap* prefs = GetExtensionPrefValueMap( | 231 const PrefValueMap* prefs = GetExtensionPrefValueMap( |
230 ext_id, kExtensionPrefsScopeIncognitoSessionOnly); | 232 ext_id, extensions::kExtensionPrefsScopeIncognitoSessionOnly); |
231 prefs->GetValue(key, &value); | 233 prefs->GetValue(key, &value); |
232 if (value) | 234 if (value) |
233 return value; | 235 return value; |
234 | 236 |
235 // If no incognito session only preference exists, fall back to persistent | 237 // If no incognito session only preference exists, fall back to persistent |
236 // incognito preference. | 238 // incognito preference. |
237 prefs = GetExtensionPrefValueMap(ext_id, | 239 prefs = GetExtensionPrefValueMap( |
238 kExtensionPrefsScopeIncognitoPersistent); | 240 ext_id, |
| 241 extensions::kExtensionPrefsScopeIncognitoPersistent); |
239 prefs->GetValue(key, &value); | 242 prefs->GetValue(key, &value); |
240 if (value) | 243 if (value) |
241 return value; | 244 return value; |
242 } else { | 245 } else { |
243 // Regular-only preference. | 246 // Regular-only preference. |
244 const PrefValueMap* prefs = GetExtensionPrefValueMap( | 247 const PrefValueMap* prefs = GetExtensionPrefValueMap( |
245 ext_id, kExtensionPrefsScopeRegularOnly); | 248 ext_id, extensions::kExtensionPrefsScopeRegularOnly); |
246 prefs->GetValue(key, &value); | 249 prefs->GetValue(key, &value); |
247 if (value) | 250 if (value) |
248 return value; | 251 return value; |
249 } | 252 } |
250 | 253 |
251 // Regular preference. | 254 // Regular preference. |
252 const PrefValueMap* prefs = GetExtensionPrefValueMap( | 255 const PrefValueMap* prefs = GetExtensionPrefValueMap( |
253 ext_id, kExtensionPrefsScopeRegular); | 256 ext_id, extensions::kExtensionPrefsScopeRegular); |
254 prefs->GetValue(key, &value); | 257 prefs->GetValue(key, &value); |
255 return value; | 258 return value; |
256 } | 259 } |
257 | 260 |
258 ExtensionPrefValueMap::ExtensionEntryMap::const_iterator | 261 ExtensionPrefValueMap::ExtensionEntryMap::const_iterator |
259 ExtensionPrefValueMap::GetEffectivePrefValueController( | 262 ExtensionPrefValueMap::GetEffectivePrefValueController( |
260 const std::string& key, | 263 const std::string& key, |
261 bool incognito, | 264 bool incognito, |
262 bool* from_incognito) const { | 265 bool* from_incognito) const { |
263 ExtensionEntryMap::const_iterator winner = entries_.end(); | 266 ExtensionEntryMap::const_iterator winner = entries_.end(); |
264 base::Time winners_install_time; | 267 base::Time winners_install_time; |
265 | 268 |
266 ExtensionEntryMap::const_iterator i; | 269 ExtensionEntryMap::const_iterator i; |
267 for (i = entries_.begin(); i != entries_.end(); ++i) { | 270 for (i = entries_.begin(); i != entries_.end(); ++i) { |
268 const std::string& ext_id = i->first; | 271 const std::string& ext_id = i->first; |
269 const base::Time& install_time = i->second->install_time; | 272 const base::Time& install_time = i->second->install_time; |
270 const bool enabled = i->second->enabled; | 273 const bool enabled = i->second->enabled; |
271 | 274 |
272 if (!enabled) | 275 if (!enabled) |
273 continue; | 276 continue; |
274 if (install_time < winners_install_time) | 277 if (install_time < winners_install_time) |
275 continue; | 278 continue; |
276 | 279 |
277 const Value* value = NULL; | 280 const Value* value = NULL; |
278 const PrefValueMap* prefs = GetExtensionPrefValueMap( | 281 const PrefValueMap* prefs = GetExtensionPrefValueMap( |
279 ext_id, kExtensionPrefsScopeRegular); | 282 ext_id, extensions::kExtensionPrefsScopeRegular); |
280 if (prefs->GetValue(key, &value)) { | 283 if (prefs->GetValue(key, &value)) { |
281 winner = i; | 284 winner = i; |
282 winners_install_time = install_time; | 285 winners_install_time = install_time; |
283 if (from_incognito) | 286 if (from_incognito) |
284 *from_incognito = false; | 287 *from_incognito = false; |
285 } | 288 } |
286 | 289 |
287 if (!incognito) { | 290 if (!incognito) { |
288 const PrefValueMap* prefs = GetExtensionPrefValueMap( | 291 const PrefValueMap* prefs = GetExtensionPrefValueMap( |
289 ext_id, kExtensionPrefsScopeRegularOnly); | 292 ext_id, extensions::kExtensionPrefsScopeRegularOnly); |
290 if (prefs->GetValue(key, &value)) { | 293 if (prefs->GetValue(key, &value)) { |
291 winner = i; | 294 winner = i; |
292 winners_install_time = install_time; | 295 winners_install_time = install_time; |
293 if (from_incognito) | 296 if (from_incognito) |
294 *from_incognito = false; | 297 *from_incognito = false; |
295 } | 298 } |
296 // Ignore the following prefs, because they're incognito-only. | 299 // Ignore the following prefs, because they're incognito-only. |
297 continue; | 300 continue; |
298 } | 301 } |
299 | 302 |
300 prefs = GetExtensionPrefValueMap( | 303 prefs = GetExtensionPrefValueMap( |
301 ext_id, kExtensionPrefsScopeIncognitoPersistent); | 304 ext_id, extensions::kExtensionPrefsScopeIncognitoPersistent); |
302 if (prefs->GetValue(key, &value)) { | 305 if (prefs->GetValue(key, &value)) { |
303 winner = i; | 306 winner = i; |
304 winners_install_time = install_time; | 307 winners_install_time = install_time; |
305 if (from_incognito) | 308 if (from_incognito) |
306 *from_incognito = true; | 309 *from_incognito = true; |
307 } | 310 } |
308 | 311 |
309 prefs = GetExtensionPrefValueMap( | 312 prefs = GetExtensionPrefValueMap( |
310 ext_id, kExtensionPrefsScopeIncognitoSessionOnly); | 313 ext_id, extensions::kExtensionPrefsScopeIncognitoSessionOnly); |
311 if (prefs->GetValue(key, &value)) { | 314 if (prefs->GetValue(key, &value)) { |
312 winner = i; | 315 winner = i; |
313 winners_install_time = install_time; | 316 winners_install_time = install_time; |
314 if (from_incognito) | 317 if (from_incognito) |
315 *from_incognito = true; | 318 *from_incognito = true; |
316 } | 319 } |
317 } | 320 } |
318 return winner; | 321 return winner; |
319 } | 322 } |
320 | 323 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 | 355 |
353 void ExtensionPrefValueMap::NotifyPrefValueChanged(const std::string& key) { | 356 void ExtensionPrefValueMap::NotifyPrefValueChanged(const std::string& key) { |
354 FOR_EACH_OBSERVER(ExtensionPrefValueMap::Observer, observers_, | 357 FOR_EACH_OBSERVER(ExtensionPrefValueMap::Observer, observers_, |
355 OnPrefValueChanged(key)); | 358 OnPrefValueChanged(key)); |
356 } | 359 } |
357 | 360 |
358 void ExtensionPrefValueMap::NotifyOfDestruction() { | 361 void ExtensionPrefValueMap::NotifyOfDestruction() { |
359 FOR_EACH_OBSERVER(ExtensionPrefValueMap::Observer, observers_, | 362 FOR_EACH_OBSERVER(ExtensionPrefValueMap::Observer, observers_, |
360 OnExtensionPrefValueMapDestruction()); | 363 OnExtensionPrefValueMapDestruction()); |
361 } | 364 } |
OLD | NEW |