[email protected] | ef9bba1 | 2012-04-06 16:26:09 | [diff] [blame^] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/extensions/extension_pref_value_map.h" |
| 6 | |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 7 | #include "base/stl_util.h" |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 8 | #include "base/values.h" |
| 9 | #include "chrome/browser/prefs/pref_value_map.h" |
| 10 | |
[email protected] | 0865c134 | 2011-01-28 20:29:37 | [diff] [blame] | 11 | struct ExtensionPrefValueMap::ExtensionEntry { |
| 12 | // Installation time of the extension. |
| 13 | base::Time install_time; |
| 14 | // Whether extension is enabled in the profile. |
| 15 | bool enabled; |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 16 | // Extension controlled preferences for the regular profile. |
| 17 | PrefValueMap regular_profile_preferences; |
| 18 | // Persistent extension controlled preferences for the incognito profile, |
| 19 | // empty for regular profile ExtensionPrefStore. |
| 20 | PrefValueMap incognito_profile_preferences_persistent; |
[email protected] | 3168574 | 2011-05-25 23:01:56 | [diff] [blame] | 21 | // Session only extension controlled preferences for the incognito profile. |
| 22 | // These preferences are deleted when the incognito profile is destroyed. |
| 23 | PrefValueMap incognito_profile_preferences_session_only; |
[email protected] | 0865c134 | 2011-01-28 20:29:37 | [diff] [blame] | 24 | }; |
| 25 | |
[email protected] | ef9bba1 | 2012-04-06 16:26:09 | [diff] [blame^] | 26 | ExtensionPrefValueMap::ExtensionPrefValueMap() : destroyed_(false) { |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | ExtensionPrefValueMap::~ExtensionPrefValueMap() { |
[email protected] | ef9bba1 | 2012-04-06 16:26:09 | [diff] [blame^] | 30 | if (!destroyed_) { |
| 31 | NotifyOfDestruction(); |
| 32 | destroyed_ = true; |
| 33 | } |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 34 | STLDeleteValues(&entries_); |
| 35 | entries_.clear(); |
| 36 | } |
| 37 | |
[email protected] | ef9bba1 | 2012-04-06 16:26:09 | [diff] [blame^] | 38 | void ExtensionPrefValueMap::Shutdown() { |
| 39 | NotifyOfDestruction(); |
| 40 | destroyed_ = true; |
| 41 | } |
| 42 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 43 | void ExtensionPrefValueMap::SetExtensionPref(const std::string& ext_id, |
| 44 | const std::string& key, |
[email protected] | dece9a6 | 2011-06-07 17:38:59 | [diff] [blame] | 45 | ExtensionPrefsScope scope, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 46 | Value* value) { |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 47 | PrefValueMap* prefs = GetExtensionPrefValueMap(ext_id, scope); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 48 | |
| 49 | if (prefs->SetValue(key, value)) |
| 50 | NotifyPrefValueChanged(key); |
| 51 | } |
| 52 | |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 53 | void ExtensionPrefValueMap::RemoveExtensionPref( |
| 54 | const std::string& ext_id, |
| 55 | const std::string& key, |
[email protected] | dece9a6 | 2011-06-07 17:38:59 | [diff] [blame] | 56 | ExtensionPrefsScope scope) { |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 57 | PrefValueMap* prefs = GetExtensionPrefValueMap(ext_id, scope); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 58 | if (prefs->RemoveValue(key)) |
| 59 | NotifyPrefValueChanged(key); |
| 60 | } |
| 61 | |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 62 | bool ExtensionPrefValueMap::CanExtensionControlPref( |
| 63 | const std::string& extension_id, |
| 64 | const std::string& pref_key, |
| 65 | bool incognito) const { |
| 66 | ExtensionEntryMap::const_iterator ext = entries_.find(extension_id); |
| 67 | if (ext == entries_.end()) { |
| 68 | NOTREACHED(); |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | ExtensionEntryMap::const_iterator winner = |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 73 | GetEffectivePrefValueController(pref_key, incognito, NULL); |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 74 | if (winner == entries_.end()) |
| 75 | return true; |
| 76 | |
| 77 | return winner->second->install_time <= ext->second->install_time; |
| 78 | } |
| 79 | |
[email protected] | 3168574 | 2011-05-25 23:01:56 | [diff] [blame] | 80 | void ExtensionPrefValueMap::ClearAllIncognitoSessionOnlyPreferences() { |
| 81 | typedef std::set<std::string> KeySet; |
| 82 | KeySet deleted_keys; |
| 83 | |
| 84 | ExtensionEntryMap::iterator i; |
| 85 | for (i = entries_.begin(); i != entries_.end(); ++i) { |
| 86 | PrefValueMap& inc_prefs = |
| 87 | i->second->incognito_profile_preferences_session_only; |
| 88 | PrefValueMap::iterator j; |
| 89 | for (j = inc_prefs.begin(); j != inc_prefs.end(); ++j) |
| 90 | deleted_keys.insert(j->first); |
| 91 | inc_prefs.Clear(); |
| 92 | } |
| 93 | |
| 94 | KeySet::iterator k; |
| 95 | for (k = deleted_keys.begin(); k != deleted_keys.end(); ++k) |
| 96 | NotifyPrefValueChanged(*k); |
| 97 | } |
| 98 | |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 99 | bool ExtensionPrefValueMap::DoesExtensionControlPref( |
| 100 | const std::string& extension_id, |
| 101 | const std::string& pref_key, |
| 102 | bool incognito) const { |
| 103 | ExtensionEntryMap::const_iterator winner = |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 104 | GetEffectivePrefValueController(pref_key, incognito, NULL); |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 105 | if (winner == entries_.end()) |
| 106 | return false; |
| 107 | return winner->first == extension_id; |
| 108 | } |
| 109 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 110 | void ExtensionPrefValueMap::RegisterExtension(const std::string& ext_id, |
| 111 | const base::Time& install_time, |
| 112 | bool is_enabled) { |
| 113 | if (entries_.find(ext_id) != entries_.end()) |
| 114 | UnregisterExtension(ext_id); |
| 115 | entries_[ext_id] = new ExtensionEntry; |
| 116 | entries_[ext_id]->install_time = install_time; |
| 117 | entries_[ext_id]->enabled = is_enabled; |
| 118 | } |
| 119 | |
| 120 | void ExtensionPrefValueMap::UnregisterExtension(const std::string& ext_id) { |
| 121 | ExtensionEntryMap::iterator i = entries_.find(ext_id); |
| 122 | if (i == entries_.end()) |
| 123 | return; |
| 124 | std::set<std::string> keys; // keys set by this extension |
| 125 | GetExtensionControlledKeys(*(i->second), &keys); |
| 126 | |
| 127 | delete i->second; |
| 128 | entries_.erase(i); |
| 129 | |
| 130 | NotifyPrefValueChanged(keys); |
| 131 | } |
| 132 | |
| 133 | void ExtensionPrefValueMap::SetExtensionState(const std::string& ext_id, |
| 134 | bool is_enabled) { |
| 135 | ExtensionEntryMap::const_iterator i = entries_.find(ext_id); |
[email protected] | 06f9256 | 2011-04-29 19:27:31 | [diff] [blame] | 136 | // This may happen when sync sets the extension state for an |
| 137 | // extension that is not installed. |
| 138 | if (i == entries_.end()) |
| 139 | return; |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 140 | if (i->second->enabled == is_enabled) |
| 141 | return; |
| 142 | std::set<std::string> keys; // keys set by this extension |
| 143 | GetExtensionControlledKeys(*(i->second), &keys); |
| 144 | i->second->enabled = is_enabled; |
| 145 | NotifyPrefValueChanged(keys); |
| 146 | } |
| 147 | |
| 148 | PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap( |
| 149 | const std::string& ext_id, |
[email protected] | dece9a6 | 2011-06-07 17:38:59 | [diff] [blame] | 150 | ExtensionPrefsScope scope) { |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 151 | ExtensionEntryMap::const_iterator i = entries_.find(ext_id); |
| 152 | CHECK(i != entries_.end()); |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 153 | switch (scope) { |
[email protected] | dece9a6 | 2011-06-07 17:38:59 | [diff] [blame] | 154 | case kExtensionPrefsScopeRegular: |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 155 | return &(i->second->regular_profile_preferences); |
[email protected] | dece9a6 | 2011-06-07 17:38:59 | [diff] [blame] | 156 | case kExtensionPrefsScopeIncognitoPersistent: |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 157 | return &(i->second->incognito_profile_preferences_persistent); |
[email protected] | dece9a6 | 2011-06-07 17:38:59 | [diff] [blame] | 158 | case kExtensionPrefsScopeIncognitoSessionOnly: |
[email protected] | 3168574 | 2011-05-25 23:01:56 | [diff] [blame] | 159 | return &(i->second->incognito_profile_preferences_session_only); |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 160 | } |
| 161 | NOTREACHED(); |
| 162 | return NULL; |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | const PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap( |
| 166 | const std::string& ext_id, |
[email protected] | dece9a6 | 2011-06-07 17:38:59 | [diff] [blame] | 167 | ExtensionPrefsScope scope) const { |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 168 | ExtensionEntryMap::const_iterator i = entries_.find(ext_id); |
| 169 | CHECK(i != entries_.end()); |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 170 | switch (scope) { |
[email protected] | dece9a6 | 2011-06-07 17:38:59 | [diff] [blame] | 171 | case kExtensionPrefsScopeRegular: |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 172 | return &(i->second->regular_profile_preferences); |
[email protected] | dece9a6 | 2011-06-07 17:38:59 | [diff] [blame] | 173 | case kExtensionPrefsScopeIncognitoPersistent: |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 174 | return &(i->second->incognito_profile_preferences_persistent); |
[email protected] | dece9a6 | 2011-06-07 17:38:59 | [diff] [blame] | 175 | case kExtensionPrefsScopeIncognitoSessionOnly: |
[email protected] | 3168574 | 2011-05-25 23:01:56 | [diff] [blame] | 176 | return &(i->second->incognito_profile_preferences_session_only); |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 177 | } |
| 178 | NOTREACHED(); |
| 179 | return NULL; |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void ExtensionPrefValueMap::GetExtensionControlledKeys( |
| 183 | const ExtensionEntry& entry, |
| 184 | std::set<std::string>* out) const { |
| 185 | PrefValueMap::const_iterator i; |
| 186 | |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 187 | const PrefValueMap& reg_prefs = entry.regular_profile_preferences; |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 188 | for (i = reg_prefs.begin(); i != reg_prefs.end(); ++i) |
| 189 | out->insert(i->first); |
| 190 | |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 191 | const PrefValueMap& inc_prefs_pers = |
| 192 | entry.incognito_profile_preferences_persistent; |
| 193 | for (i = inc_prefs_pers.begin(); i != inc_prefs_pers.end(); ++i) |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 194 | out->insert(i->first); |
[email protected] | 3168574 | 2011-05-25 23:01:56 | [diff] [blame] | 195 | |
| 196 | const PrefValueMap& inc_prefs_session = |
| 197 | entry.incognito_profile_preferences_session_only; |
| 198 | for (i = inc_prefs_session.begin(); i != inc_prefs_session.end(); ++i) |
| 199 | out->insert(i->first); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | const Value* ExtensionPrefValueMap::GetEffectivePrefValue( |
| 203 | const std::string& key, |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 204 | bool incognito, |
| 205 | bool* from_incognito) const { |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 206 | ExtensionEntryMap::const_iterator winner = |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 207 | GetEffectivePrefValueController(key, incognito, from_incognito); |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 208 | if (winner == entries_.end()) |
| 209 | return NULL; |
| 210 | |
[email protected] | 68bf41a | 2011-03-25 16:38:31 | [diff] [blame] | 211 | const Value* value = NULL; |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 212 | const std::string& ext_id = winner->first; |
[email protected] | 3168574 | 2011-05-25 23:01:56 | [diff] [blame] | 213 | |
| 214 | // First search for incognito session only preferences. |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 215 | if (incognito) { |
| 216 | const PrefValueMap* prefs = GetExtensionPrefValueMap( |
[email protected] | dece9a6 | 2011-06-07 17:38:59 | [diff] [blame] | 217 | ext_id, kExtensionPrefsScopeIncognitoSessionOnly); |
[email protected] | 3168574 | 2011-05-25 23:01:56 | [diff] [blame] | 218 | prefs->GetValue(key, &value); |
| 219 | } |
| 220 | |
| 221 | // If no incognito session only preference exists, fall back to persistent |
| 222 | // incognito preference. |
| 223 | if (incognito && !value) { |
| 224 | const PrefValueMap* prefs = GetExtensionPrefValueMap( |
[email protected] | dece9a6 | 2011-06-07 17:38:59 | [diff] [blame] | 225 | ext_id, kExtensionPrefsScopeIncognitoPersistent); |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 226 | prefs->GetValue(key, &value); |
| 227 | } |
[email protected] | 3168574 | 2011-05-25 23:01:56 | [diff] [blame] | 228 | |
| 229 | // Finally consider a regular preference. |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 230 | if (!value) { |
| 231 | const PrefValueMap* prefs = GetExtensionPrefValueMap( |
[email protected] | dece9a6 | 2011-06-07 17:38:59 | [diff] [blame] | 232 | ext_id, kExtensionPrefsScopeRegular); |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 233 | prefs->GetValue(key, &value); |
| 234 | } |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 235 | return value; |
| 236 | } |
| 237 | |
| 238 | ExtensionPrefValueMap::ExtensionEntryMap::const_iterator |
| 239 | ExtensionPrefValueMap::GetEffectivePrefValueController( |
| 240 | const std::string& key, |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 241 | bool incognito, |
| 242 | bool* from_incognito) const { |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 243 | ExtensionEntryMap::const_iterator winner = entries_.end(); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 244 | base::Time winners_install_time; |
| 245 | |
| 246 | ExtensionEntryMap::const_iterator i; |
| 247 | for (i = entries_.begin(); i != entries_.end(); ++i) { |
| 248 | const std::string& ext_id = i->first; |
| 249 | const base::Time& install_time = i->second->install_time; |
| 250 | const bool enabled = i->second->enabled; |
| 251 | |
| 252 | if (!enabled) |
| 253 | continue; |
| 254 | if (install_time < winners_install_time) |
| 255 | continue; |
| 256 | |
[email protected] | 68bf41a | 2011-03-25 16:38:31 | [diff] [blame] | 257 | const Value* value = NULL; |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 258 | const PrefValueMap* prefs = GetExtensionPrefValueMap( |
[email protected] | dece9a6 | 2011-06-07 17:38:59 | [diff] [blame] | 259 | ext_id, kExtensionPrefsScopeRegular); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 260 | if (prefs->GetValue(key, &value)) { |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 261 | winner = i; |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 262 | winners_install_time = install_time; |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 263 | if (from_incognito) |
| 264 | *from_incognito = false; |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | if (!incognito) |
| 268 | continue; |
| 269 | |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 270 | prefs = GetExtensionPrefValueMap( |
[email protected] | dece9a6 | 2011-06-07 17:38:59 | [diff] [blame] | 271 | ext_id, kExtensionPrefsScopeIncognitoPersistent); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 272 | if (prefs->GetValue(key, &value)) { |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 273 | winner = i; |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 274 | winners_install_time = install_time; |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 275 | if (from_incognito) |
| 276 | *from_incognito = true; |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 277 | } |
[email protected] | 3168574 | 2011-05-25 23:01:56 | [diff] [blame] | 278 | |
| 279 | prefs = GetExtensionPrefValueMap( |
[email protected] | dece9a6 | 2011-06-07 17:38:59 | [diff] [blame] | 280 | ext_id, kExtensionPrefsScopeIncognitoSessionOnly); |
[email protected] | 3168574 | 2011-05-25 23:01:56 | [diff] [blame] | 281 | if (prefs->GetValue(key, &value)) { |
| 282 | winner = i; |
| 283 | winners_install_time = install_time; |
| 284 | if (from_incognito) |
| 285 | *from_incognito = true; |
| 286 | } |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 287 | } |
| 288 | return winner; |
| 289 | } |
| 290 | |
| 291 | void ExtensionPrefValueMap::AddObserver( |
| 292 | ExtensionPrefValueMap::Observer* observer) { |
| 293 | observers_.AddObserver(observer); |
| 294 | |
| 295 | // Collect all currently used keys and notify the new observer. |
| 296 | std::set<std::string> keys; |
| 297 | ExtensionEntryMap::const_iterator i; |
| 298 | for (i = entries_.begin(); i != entries_.end(); ++i) |
| 299 | GetExtensionControlledKeys(*(i->second), &keys); |
| 300 | |
| 301 | std::set<std::string>::const_iterator j; |
| 302 | for (j = keys.begin(); j != keys.end(); ++j) |
| 303 | observer->OnPrefValueChanged(*j); |
| 304 | } |
| 305 | |
| 306 | void ExtensionPrefValueMap::RemoveObserver( |
| 307 | ExtensionPrefValueMap::Observer* observer) { |
| 308 | observers_.RemoveObserver(observer); |
| 309 | } |
| 310 | |
| 311 | void ExtensionPrefValueMap::NotifyInitializationCompleted() { |
| 312 | FOR_EACH_OBSERVER(ExtensionPrefValueMap::Observer, observers_, |
| 313 | OnInitializationCompleted()); |
| 314 | } |
| 315 | |
| 316 | void ExtensionPrefValueMap::NotifyPrefValueChanged( |
| 317 | const std::set<std::string>& keys) { |
| 318 | std::set<std::string>::const_iterator i; |
| 319 | for (i = keys.begin(); i != keys.end(); ++i) |
| 320 | NotifyPrefValueChanged(*i); |
| 321 | } |
| 322 | |
| 323 | void ExtensionPrefValueMap::NotifyPrefValueChanged(const std::string& key) { |
| 324 | FOR_EACH_OBSERVER(ExtensionPrefValueMap::Observer, observers_, |
| 325 | OnPrefValueChanged(key)); |
| 326 | } |
| 327 | |
| 328 | void ExtensionPrefValueMap::NotifyOfDestruction() { |
| 329 | FOR_EACH_OBSERVER(ExtensionPrefValueMap::Observer, observers_, |
| 330 | OnExtensionPrefValueMapDestruction()); |
| 331 | } |