[email protected] | 489db084 | 2014-01-22 18:20:03 | [diff] [blame] | 1 | // Copyright 2014 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 | |
[email protected] | 489db084 | 2014-01-22 18:20:03 | [diff] [blame] | 5 | #include "extensions/browser/extension_pref_value_map.h" |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 6 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 7 | #include "base/values.h" |
brettw | 06650868 | 2016-02-03 08:22:02 | [diff] [blame^] | 8 | #include "components/prefs/pref_value_map.h" |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 9 | |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 10 | using extensions::ExtensionPrefsScope; |
| 11 | |
[email protected] | 0865c134 | 2011-01-28 20:29:37 | [diff] [blame] | 12 | struct ExtensionPrefValueMap::ExtensionEntry { |
| 13 | // Installation time of the extension. |
| 14 | base::Time install_time; |
| 15 | // Whether extension is enabled in the profile. |
| 16 | bool enabled; |
[email protected] | 6c16475 | 2014-03-12 00:29:15 | [diff] [blame] | 17 | // Whether the extension has access to the incognito profile. |
| 18 | bool incognito_enabled; |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 19 | // Extension controlled preferences for the regular profile. |
| 20 | PrefValueMap regular_profile_preferences; |
[email protected] | 7d3a0e3 | 2012-06-14 22:37:29 | [diff] [blame] | 21 | // Extension controlled preferences that should *only* apply to the regular |
| 22 | // profile. |
| 23 | PrefValueMap regular_only_profile_preferences; |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 24 | // Persistent extension controlled preferences for the incognito profile, |
| 25 | // empty for regular profile ExtensionPrefStore. |
| 26 | PrefValueMap incognito_profile_preferences_persistent; |
[email protected] | 3168574 | 2011-05-25 23:01:56 | [diff] [blame] | 27 | // Session only extension controlled preferences for the incognito profile. |
| 28 | // These preferences are deleted when the incognito profile is destroyed. |
| 29 | PrefValueMap incognito_profile_preferences_session_only; |
[email protected] | 0865c134 | 2011-01-28 20:29:37 | [diff] [blame] | 30 | }; |
| 31 | |
[email protected] | ef9bba1 | 2012-04-06 16:26:09 | [diff] [blame] | 32 | ExtensionPrefValueMap::ExtensionPrefValueMap() : destroyed_(false) { |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | ExtensionPrefValueMap::~ExtensionPrefValueMap() { |
[email protected] | ef9bba1 | 2012-04-06 16:26:09 | [diff] [blame] | 36 | if (!destroyed_) { |
| 37 | NotifyOfDestruction(); |
| 38 | destroyed_ = true; |
| 39 | } |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 40 | } |
| 41 | |
[email protected] | ef9bba1 | 2012-04-06 16:26:09 | [diff] [blame] | 42 | void ExtensionPrefValueMap::Shutdown() { |
| 43 | NotifyOfDestruction(); |
| 44 | destroyed_ = true; |
| 45 | } |
| 46 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 47 | void ExtensionPrefValueMap::SetExtensionPref(const std::string& ext_id, |
| 48 | const std::string& key, |
[email protected] | dece9a6 | 2011-06-07 17:38:59 | [diff] [blame] | 49 | ExtensionPrefsScope scope, |
[email protected] | 023b3d1 | 2013-12-23 18:46:49 | [diff] [blame] | 50 | base::Value* value) { |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 51 | PrefValueMap* prefs = GetExtensionPrefValueMap(ext_id, scope); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 52 | |
estade | 0bd407f | 2015-06-26 18:16:18 | [diff] [blame] | 53 | if (prefs->SetValue(key, make_scoped_ptr(value))) |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 54 | NotifyPrefValueChanged(key); |
| 55 | } |
| 56 | |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 57 | void ExtensionPrefValueMap::RemoveExtensionPref( |
| 58 | const std::string& ext_id, |
| 59 | const std::string& key, |
[email protected] | dece9a6 | 2011-06-07 17:38:59 | [diff] [blame] | 60 | ExtensionPrefsScope scope) { |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 61 | PrefValueMap* prefs = GetExtensionPrefValueMap(ext_id, scope); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 62 | if (prefs->RemoveValue(key)) |
| 63 | NotifyPrefValueChanged(key); |
| 64 | } |
| 65 | |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 66 | bool ExtensionPrefValueMap::CanExtensionControlPref( |
| 67 | const std::string& extension_id, |
| 68 | const std::string& pref_key, |
| 69 | bool incognito) const { |
| 70 | ExtensionEntryMap::const_iterator ext = entries_.find(extension_id); |
| 71 | if (ext == entries_.end()) { |
battre | c17c974 | 2015-04-10 08:24:25 | [diff] [blame] | 72 | NOTREACHED() << "Extension " << extension_id |
| 73 | << " is not registered but accesses pref " << pref_key |
| 74 | << " (incognito: " << incognito << ")." |
| 75 | << " http://crbug.com/454513"; |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 76 | return false; |
| 77 | } |
| 78 | |
[email protected] | 6c16475 | 2014-03-12 00:29:15 | [diff] [blame] | 79 | if (incognito && !ext->second->incognito_enabled) |
| 80 | return false; |
| 81 | |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 82 | ExtensionEntryMap::const_iterator winner = |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 83 | GetEffectivePrefValueController(pref_key, incognito, NULL); |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 84 | if (winner == entries_.end()) |
| 85 | return true; |
| 86 | |
| 87 | return winner->second->install_time <= ext->second->install_time; |
| 88 | } |
| 89 | |
[email protected] | 3168574 | 2011-05-25 23:01:56 | [diff] [blame] | 90 | void ExtensionPrefValueMap::ClearAllIncognitoSessionOnlyPreferences() { |
| 91 | typedef std::set<std::string> KeySet; |
| 92 | KeySet deleted_keys; |
| 93 | |
limasdf | 39e712d7 | 2015-11-19 16:00:50 | [diff] [blame] | 94 | for (const auto& entry : entries_) { |
[email protected] | 3168574 | 2011-05-25 23:01:56 | [diff] [blame] | 95 | PrefValueMap& inc_prefs = |
limasdf | 39e712d7 | 2015-11-19 16:00:50 | [diff] [blame] | 96 | entry.second->incognito_profile_preferences_session_only; |
| 97 | for (const auto& pref : inc_prefs) |
| 98 | deleted_keys.insert(pref.first); |
[email protected] | 3168574 | 2011-05-25 23:01:56 | [diff] [blame] | 99 | inc_prefs.Clear(); |
| 100 | } |
| 101 | |
limasdf | 39e712d7 | 2015-11-19 16:00:50 | [diff] [blame] | 102 | for (const auto& key : deleted_keys) |
| 103 | NotifyPrefValueChanged(key); |
[email protected] | 3168574 | 2011-05-25 23:01:56 | [diff] [blame] | 104 | } |
| 105 | |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 106 | bool ExtensionPrefValueMap::DoesExtensionControlPref( |
| 107 | const std::string& extension_id, |
| 108 | const std::string& pref_key, |
[email protected] | 42c037e | 2012-06-26 22:23:32 | [diff] [blame] | 109 | bool* from_incognito) const { |
| 110 | bool incognito = (from_incognito != NULL); |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 111 | ExtensionEntryMap::const_iterator winner = |
[email protected] | 42c037e | 2012-06-26 22:23:32 | [diff] [blame] | 112 | GetEffectivePrefValueController(pref_key, incognito, from_incognito); |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 113 | if (winner == entries_.end()) |
| 114 | return false; |
| 115 | return winner->first == extension_id; |
| 116 | } |
| 117 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 118 | void ExtensionPrefValueMap::RegisterExtension(const std::string& ext_id, |
| 119 | const base::Time& install_time, |
[email protected] | 6c16475 | 2014-03-12 00:29:15 | [diff] [blame] | 120 | bool is_enabled, |
| 121 | bool is_incognito_enabled) { |
[email protected] | ec5143b | 2013-09-17 12:03:53 | [diff] [blame] | 122 | if (entries_.find(ext_id) == entries_.end()) { |
limasdf | 39e712d7 | 2015-11-19 16:00:50 | [diff] [blame] | 123 | entries_[ext_id] = make_scoped_ptr(new ExtensionEntry); |
[email protected] | ec5143b | 2013-09-17 12:03:53 | [diff] [blame] | 124 | |
| 125 | // Only update the install time if the extension is newly installed. |
| 126 | entries_[ext_id]->install_time = install_time; |
| 127 | } |
| 128 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 129 | entries_[ext_id]->enabled = is_enabled; |
[email protected] | 6c16475 | 2014-03-12 00:29:15 | [diff] [blame] | 130 | entries_[ext_id]->incognito_enabled = is_incognito_enabled; |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void ExtensionPrefValueMap::UnregisterExtension(const std::string& ext_id) { |
| 134 | ExtensionEntryMap::iterator i = entries_.find(ext_id); |
| 135 | if (i == entries_.end()) |
| 136 | return; |
| 137 | std::set<std::string> keys; // keys set by this extension |
limasdf | 39e712d7 | 2015-11-19 16:00:50 | [diff] [blame] | 138 | GetExtensionControlledKeys(*(i->second.get()), &keys); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 139 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 140 | entries_.erase(i); |
| 141 | |
| 142 | NotifyPrefValueChanged(keys); |
| 143 | } |
| 144 | |
| 145 | void ExtensionPrefValueMap::SetExtensionState(const std::string& ext_id, |
| 146 | bool is_enabled) { |
| 147 | ExtensionEntryMap::const_iterator i = entries_.find(ext_id); |
[email protected] | 06f9256 | 2011-04-29 19:27:31 | [diff] [blame] | 148 | // This may happen when sync sets the extension state for an |
| 149 | // extension that is not installed. |
| 150 | if (i == entries_.end()) |
| 151 | return; |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 152 | if (i->second->enabled == is_enabled) |
| 153 | return; |
| 154 | std::set<std::string> keys; // keys set by this extension |
| 155 | GetExtensionControlledKeys(*(i->second), &keys); |
| 156 | i->second->enabled = is_enabled; |
| 157 | NotifyPrefValueChanged(keys); |
| 158 | } |
| 159 | |
[email protected] | 6c16475 | 2014-03-12 00:29:15 | [diff] [blame] | 160 | void ExtensionPrefValueMap::SetExtensionIncognitoState( |
| 161 | const std::string& ext_id, |
| 162 | bool is_incognito_enabled) { |
| 163 | ExtensionEntryMap::const_iterator i = entries_.find(ext_id); |
| 164 | // This may happen when sync sets the extension state for an |
| 165 | // extension that is not installed. |
| 166 | if (i == entries_.end()) |
| 167 | return; |
| 168 | if (i->second->incognito_enabled == is_incognito_enabled) |
| 169 | return; |
| 170 | std::set<std::string> keys; // keys set by this extension |
| 171 | GetExtensionControlledKeys(*(i->second), &keys); |
| 172 | i->second->incognito_enabled = is_incognito_enabled; |
| 173 | NotifyPrefValueChanged(keys); |
| 174 | } |
| 175 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 176 | PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap( |
| 177 | const std::string& ext_id, |
[email protected] | dece9a6 | 2011-06-07 17:38:59 | [diff] [blame] | 178 | ExtensionPrefsScope scope) { |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 179 | ExtensionEntryMap::const_iterator i = entries_.find(ext_id); |
| 180 | CHECK(i != entries_.end()); |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 181 | switch (scope) { |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 182 | case extensions::kExtensionPrefsScopeRegular: |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 183 | return &(i->second->regular_profile_preferences); |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 184 | case extensions::kExtensionPrefsScopeRegularOnly: |
[email protected] | 7d3a0e3 | 2012-06-14 22:37:29 | [diff] [blame] | 185 | return &(i->second->regular_only_profile_preferences); |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 186 | case extensions::kExtensionPrefsScopeIncognitoPersistent: |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 187 | return &(i->second->incognito_profile_preferences_persistent); |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 188 | case extensions::kExtensionPrefsScopeIncognitoSessionOnly: |
[email protected] | 3168574 | 2011-05-25 23:01:56 | [diff] [blame] | 189 | return &(i->second->incognito_profile_preferences_session_only); |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 190 | } |
| 191 | NOTREACHED(); |
| 192 | return NULL; |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | const PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap( |
| 196 | const std::string& ext_id, |
[email protected] | dece9a6 | 2011-06-07 17:38:59 | [diff] [blame] | 197 | ExtensionPrefsScope scope) const { |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 198 | ExtensionEntryMap::const_iterator i = entries_.find(ext_id); |
| 199 | CHECK(i != entries_.end()); |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 200 | switch (scope) { |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 201 | case extensions::kExtensionPrefsScopeRegular: |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 202 | return &(i->second->regular_profile_preferences); |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 203 | case extensions::kExtensionPrefsScopeRegularOnly: |
[email protected] | 7d3a0e3 | 2012-06-14 22:37:29 | [diff] [blame] | 204 | return &(i->second->regular_only_profile_preferences); |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 205 | case extensions::kExtensionPrefsScopeIncognitoPersistent: |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 206 | return &(i->second->incognito_profile_preferences_persistent); |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 207 | case extensions::kExtensionPrefsScopeIncognitoSessionOnly: |
[email protected] | 3168574 | 2011-05-25 23:01:56 | [diff] [blame] | 208 | return &(i->second->incognito_profile_preferences_session_only); |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 209 | } |
| 210 | NOTREACHED(); |
| 211 | return NULL; |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void ExtensionPrefValueMap::GetExtensionControlledKeys( |
| 215 | const ExtensionEntry& entry, |
| 216 | std::set<std::string>* out) const { |
| 217 | PrefValueMap::const_iterator i; |
| 218 | |
[email protected] | 7d3a0e3 | 2012-06-14 22:37:29 | [diff] [blame] | 219 | const PrefValueMap& regular_prefs = entry.regular_profile_preferences; |
| 220 | for (i = regular_prefs.begin(); i != regular_prefs.end(); ++i) |
| 221 | out->insert(i->first); |
| 222 | |
| 223 | const PrefValueMap& regular_only_prefs = |
| 224 | entry.regular_only_profile_preferences; |
| 225 | for (i = regular_only_prefs.begin(); i != regular_only_prefs.end(); ++i) |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 226 | out->insert(i->first); |
| 227 | |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 228 | const PrefValueMap& inc_prefs_pers = |
| 229 | entry.incognito_profile_preferences_persistent; |
| 230 | for (i = inc_prefs_pers.begin(); i != inc_prefs_pers.end(); ++i) |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 231 | out->insert(i->first); |
[email protected] | 3168574 | 2011-05-25 23:01:56 | [diff] [blame] | 232 | |
| 233 | const PrefValueMap& inc_prefs_session = |
| 234 | entry.incognito_profile_preferences_session_only; |
| 235 | for (i = inc_prefs_session.begin(); i != inc_prefs_session.end(); ++i) |
| 236 | out->insert(i->first); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 237 | } |
| 238 | |
[email protected] | 023b3d1 | 2013-12-23 18:46:49 | [diff] [blame] | 239 | const base::Value* ExtensionPrefValueMap::GetEffectivePrefValue( |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 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 = |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 244 | GetEffectivePrefValueController(key, incognito, from_incognito); |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 245 | if (winner == entries_.end()) |
| 246 | return NULL; |
| 247 | |
[email protected] | 023b3d1 | 2013-12-23 18:46:49 | [diff] [blame] | 248 | const base::Value* value = NULL; |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 249 | const std::string& ext_id = winner->first; |
[email protected] | 3168574 | 2011-05-25 23:01:56 | [diff] [blame] | 250 | |
| 251 | // First search for incognito session only preferences. |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 252 | if (incognito) { |
[email protected] | 6c16475 | 2014-03-12 00:29:15 | [diff] [blame] | 253 | DCHECK(winner->second->incognito_enabled); |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 254 | const PrefValueMap* prefs = GetExtensionPrefValueMap( |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 255 | ext_id, extensions::kExtensionPrefsScopeIncognitoSessionOnly); |
[email protected] | 3168574 | 2011-05-25 23:01:56 | [diff] [blame] | 256 | prefs->GetValue(key, &value); |
[email protected] | 7d3a0e3 | 2012-06-14 22:37:29 | [diff] [blame] | 257 | if (value) |
| 258 | return value; |
| 259 | |
| 260 | // If no incognito session only preference exists, fall back to persistent |
| 261 | // incognito preference. |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 262 | prefs = GetExtensionPrefValueMap( |
| 263 | ext_id, |
| 264 | extensions::kExtensionPrefsScopeIncognitoPersistent); |
[email protected] | 7d3a0e3 | 2012-06-14 22:37:29 | [diff] [blame] | 265 | prefs->GetValue(key, &value); |
| 266 | if (value) |
| 267 | return value; |
| 268 | } else { |
| 269 | // Regular-only preference. |
| 270 | const PrefValueMap* prefs = GetExtensionPrefValueMap( |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 271 | ext_id, extensions::kExtensionPrefsScopeRegularOnly); |
[email protected] | 7d3a0e3 | 2012-06-14 22:37:29 | [diff] [blame] | 272 | prefs->GetValue(key, &value); |
| 273 | if (value) |
| 274 | return value; |
[email protected] | 3168574 | 2011-05-25 23:01:56 | [diff] [blame] | 275 | } |
| 276 | |
[email protected] | 7d3a0e3 | 2012-06-14 22:37:29 | [diff] [blame] | 277 | // Regular preference. |
| 278 | const PrefValueMap* prefs = GetExtensionPrefValueMap( |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 279 | ext_id, extensions::kExtensionPrefsScopeRegular); |
[email protected] | 7d3a0e3 | 2012-06-14 22:37:29 | [diff] [blame] | 280 | prefs->GetValue(key, &value); |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 281 | return value; |
| 282 | } |
| 283 | |
| 284 | ExtensionPrefValueMap::ExtensionEntryMap::const_iterator |
| 285 | ExtensionPrefValueMap::GetEffectivePrefValueController( |
| 286 | const std::string& key, |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 287 | bool incognito, |
| 288 | bool* from_incognito) const { |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 289 | ExtensionEntryMap::const_iterator winner = entries_.end(); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 290 | base::Time winners_install_time; |
| 291 | |
| 292 | ExtensionEntryMap::const_iterator i; |
| 293 | for (i = entries_.begin(); i != entries_.end(); ++i) { |
| 294 | const std::string& ext_id = i->first; |
| 295 | const base::Time& install_time = i->second->install_time; |
| 296 | const bool enabled = i->second->enabled; |
[email protected] | 6c16475 | 2014-03-12 00:29:15 | [diff] [blame] | 297 | const bool incognito_enabled = i->second->incognito_enabled; |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 298 | |
| 299 | if (!enabled) |
| 300 | continue; |
| 301 | if (install_time < winners_install_time) |
| 302 | continue; |
[email protected] | 6c16475 | 2014-03-12 00:29:15 | [diff] [blame] | 303 | if (incognito && !incognito_enabled) |
| 304 | continue; |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 305 | |
[email protected] | 023b3d1 | 2013-12-23 18:46:49 | [diff] [blame] | 306 | const base::Value* value = NULL; |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 307 | const PrefValueMap* prefs = GetExtensionPrefValueMap( |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 308 | ext_id, extensions::kExtensionPrefsScopeRegular); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 309 | if (prefs->GetValue(key, &value)) { |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 310 | winner = i; |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 311 | winners_install_time = install_time; |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 312 | if (from_incognito) |
| 313 | *from_incognito = false; |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 314 | } |
| 315 | |
[email protected] | 7d3a0e3 | 2012-06-14 22:37:29 | [diff] [blame] | 316 | if (!incognito) { |
| 317 | const PrefValueMap* prefs = GetExtensionPrefValueMap( |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 318 | ext_id, extensions::kExtensionPrefsScopeRegularOnly); |
[email protected] | 7d3a0e3 | 2012-06-14 22:37:29 | [diff] [blame] | 319 | if (prefs->GetValue(key, &value)) { |
| 320 | winner = i; |
| 321 | winners_install_time = install_time; |
| 322 | if (from_incognito) |
| 323 | *from_incognito = false; |
| 324 | } |
| 325 | // Ignore the following prefs, because they're incognito-only. |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 326 | continue; |
[email protected] | 7d3a0e3 | 2012-06-14 22:37:29 | [diff] [blame] | 327 | } |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 328 | |
[email protected] | 9783c16 | 2011-05-25 21:45:51 | [diff] [blame] | 329 | prefs = GetExtensionPrefValueMap( |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 330 | ext_id, extensions::kExtensionPrefsScopeIncognitoPersistent); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 331 | if (prefs->GetValue(key, &value)) { |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 332 | winner = i; |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 333 | winners_install_time = install_time; |
[email protected] | c079356 | 2011-03-09 15:31:03 | [diff] [blame] | 334 | if (from_incognito) |
| 335 | *from_incognito = true; |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 336 | } |
[email protected] | 3168574 | 2011-05-25 23:01:56 | [diff] [blame] | 337 | |
| 338 | prefs = GetExtensionPrefValueMap( |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 339 | ext_id, extensions::kExtensionPrefsScopeIncognitoSessionOnly); |
[email protected] | 3168574 | 2011-05-25 23:01:56 | [diff] [blame] | 340 | if (prefs->GetValue(key, &value)) { |
| 341 | winner = i; |
| 342 | winners_install_time = install_time; |
| 343 | if (from_incognito) |
| 344 | *from_incognito = true; |
| 345 | } |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 346 | } |
| 347 | return winner; |
| 348 | } |
| 349 | |
| 350 | void ExtensionPrefValueMap::AddObserver( |
| 351 | ExtensionPrefValueMap::Observer* observer) { |
| 352 | observers_.AddObserver(observer); |
| 353 | |
| 354 | // Collect all currently used keys and notify the new observer. |
| 355 | std::set<std::string> keys; |
| 356 | ExtensionEntryMap::const_iterator i; |
| 357 | for (i = entries_.begin(); i != entries_.end(); ++i) |
| 358 | GetExtensionControlledKeys(*(i->second), &keys); |
| 359 | |
| 360 | std::set<std::string>::const_iterator j; |
| 361 | for (j = keys.begin(); j != keys.end(); ++j) |
| 362 | observer->OnPrefValueChanged(*j); |
| 363 | } |
| 364 | |
| 365 | void ExtensionPrefValueMap::RemoveObserver( |
| 366 | ExtensionPrefValueMap::Observer* observer) { |
| 367 | observers_.RemoveObserver(observer); |
| 368 | } |
| 369 | |
[email protected] | b5a507b2 | 2013-11-08 20:41:57 | [diff] [blame] | 370 | std::string ExtensionPrefValueMap::GetExtensionControllingPref( |
| 371 | const std::string& pref_key) const { |
| 372 | ExtensionEntryMap::const_iterator winner = |
| 373 | GetEffectivePrefValueController(pref_key, false, NULL); |
| 374 | if (winner == entries_.end()) |
| 375 | return std::string(); |
| 376 | return winner->first; |
| 377 | } |
| 378 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 379 | void ExtensionPrefValueMap::NotifyInitializationCompleted() { |
| 380 | FOR_EACH_OBSERVER(ExtensionPrefValueMap::Observer, observers_, |
| 381 | OnInitializationCompleted()); |
| 382 | } |
| 383 | |
| 384 | void ExtensionPrefValueMap::NotifyPrefValueChanged( |
| 385 | const std::set<std::string>& keys) { |
limasdf | 39e712d7 | 2015-11-19 16:00:50 | [diff] [blame] | 386 | for (const auto& key : keys) |
| 387 | NotifyPrefValueChanged(key); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | void ExtensionPrefValueMap::NotifyPrefValueChanged(const std::string& key) { |
| 391 | FOR_EACH_OBSERVER(ExtensionPrefValueMap::Observer, observers_, |
| 392 | OnPrefValueChanged(key)); |
| 393 | } |
| 394 | |
| 395 | void ExtensionPrefValueMap::NotifyOfDestruction() { |
| 396 | FOR_EACH_OBSERVER(ExtensionPrefValueMap::Observer, observers_, |
| 397 | OnExtensionPrefValueMapDestruction()); |
| 398 | } |