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 // Extension controlled preferences for the regular profile. | 16 // Extension controlled preferences for the regular profile. |
17 PrefValueMap regular_profile_preferences; | 17 PrefValueMap regular_profile_preferences; |
18 // Persistent extension controlled preferences for the incognito profile, | 18 // Persistent extension controlled preferences for the incognito profile, |
19 // empty for regular profile ExtensionPrefStore. | 19 // empty for regular profile ExtensionPrefStore. |
20 PrefValueMap incognito_profile_preferences_persistent; | 20 PrefValueMap incognito_profile_preferences_persistent; |
| 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; |
21 }; | 24 }; |
22 | 25 |
23 ExtensionPrefValueMap::ExtensionPrefValueMap() { | 26 ExtensionPrefValueMap::ExtensionPrefValueMap() { |
24 } | 27 } |
25 | 28 |
26 ExtensionPrefValueMap::~ExtensionPrefValueMap() { | 29 ExtensionPrefValueMap::~ExtensionPrefValueMap() { |
27 NotifyOfDestruction(); | 30 NotifyOfDestruction(); |
28 STLDeleteValues(&entries_); | 31 STLDeleteValues(&entries_); |
29 entries_.clear(); | 32 entries_.clear(); |
30 } | 33 } |
(...skipping 28 matching lines...) Expand all Loading... |
59 } | 62 } |
60 | 63 |
61 ExtensionEntryMap::const_iterator winner = | 64 ExtensionEntryMap::const_iterator winner = |
62 GetEffectivePrefValueController(pref_key, incognito, NULL); | 65 GetEffectivePrefValueController(pref_key, incognito, NULL); |
63 if (winner == entries_.end()) | 66 if (winner == entries_.end()) |
64 return true; | 67 return true; |
65 | 68 |
66 return winner->second->install_time <= ext->second->install_time; | 69 return winner->second->install_time <= ext->second->install_time; |
67 } | 70 } |
68 | 71 |
| 72 void ExtensionPrefValueMap::ClearAllIncognitoSessionOnlyPreferences() { |
| 73 typedef std::set<std::string> KeySet; |
| 74 KeySet deleted_keys; |
| 75 |
| 76 ExtensionEntryMap::iterator i; |
| 77 for (i = entries_.begin(); i != entries_.end(); ++i) { |
| 78 PrefValueMap& inc_prefs = |
| 79 i->second->incognito_profile_preferences_session_only; |
| 80 PrefValueMap::iterator j; |
| 81 for (j = inc_prefs.begin(); j != inc_prefs.end(); ++j) |
| 82 deleted_keys.insert(j->first); |
| 83 inc_prefs.Clear(); |
| 84 } |
| 85 |
| 86 KeySet::iterator k; |
| 87 for (k = deleted_keys.begin(); k != deleted_keys.end(); ++k) |
| 88 NotifyPrefValueChanged(*k); |
| 89 } |
| 90 |
69 bool ExtensionPrefValueMap::DoesExtensionControlPref( | 91 bool ExtensionPrefValueMap::DoesExtensionControlPref( |
70 const std::string& extension_id, | 92 const std::string& extension_id, |
71 const std::string& pref_key, | 93 const std::string& pref_key, |
72 bool incognito) const { | 94 bool incognito) const { |
73 ExtensionEntryMap::const_iterator winner = | 95 ExtensionEntryMap::const_iterator winner = |
74 GetEffectivePrefValueController(pref_key, incognito, NULL); | 96 GetEffectivePrefValueController(pref_key, incognito, NULL); |
75 if (winner == entries_.end()) | 97 if (winner == entries_.end()) |
76 return false; | 98 return false; |
77 return winner->first == extension_id; | 99 return winner->first == extension_id; |
78 } | 100 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap( | 140 PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap( |
119 const std::string& ext_id, | 141 const std::string& ext_id, |
120 extension_prefs_scope::Scope scope) { | 142 extension_prefs_scope::Scope scope) { |
121 ExtensionEntryMap::const_iterator i = entries_.find(ext_id); | 143 ExtensionEntryMap::const_iterator i = entries_.find(ext_id); |
122 CHECK(i != entries_.end()); | 144 CHECK(i != entries_.end()); |
123 switch (scope) { | 145 switch (scope) { |
124 case extension_prefs_scope::kRegular: | 146 case extension_prefs_scope::kRegular: |
125 return &(i->second->regular_profile_preferences); | 147 return &(i->second->regular_profile_preferences); |
126 case extension_prefs_scope::kIncognitoPersistent: | 148 case extension_prefs_scope::kIncognitoPersistent: |
127 return &(i->second->incognito_profile_preferences_persistent); | 149 return &(i->second->incognito_profile_preferences_persistent); |
| 150 case extension_prefs_scope::kIncognitoSessionOnly: |
| 151 return &(i->second->incognito_profile_preferences_session_only); |
128 } | 152 } |
129 NOTREACHED(); | 153 NOTREACHED(); |
130 return NULL; | 154 return NULL; |
131 } | 155 } |
132 | 156 |
133 const PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap( | 157 const PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap( |
134 const std::string& ext_id, | 158 const std::string& ext_id, |
135 extension_prefs_scope::Scope scope) const { | 159 extension_prefs_scope::Scope scope) const { |
136 ExtensionEntryMap::const_iterator i = entries_.find(ext_id); | 160 ExtensionEntryMap::const_iterator i = entries_.find(ext_id); |
137 CHECK(i != entries_.end()); | 161 CHECK(i != entries_.end()); |
138 switch (scope) { | 162 switch (scope) { |
139 case extension_prefs_scope::kRegular: | 163 case extension_prefs_scope::kRegular: |
140 return &(i->second->regular_profile_preferences); | 164 return &(i->second->regular_profile_preferences); |
141 case extension_prefs_scope::kIncognitoPersistent: | 165 case extension_prefs_scope::kIncognitoPersistent: |
142 return &(i->second->incognito_profile_preferences_persistent); | 166 return &(i->second->incognito_profile_preferences_persistent); |
| 167 case extension_prefs_scope::kIncognitoSessionOnly: |
| 168 return &(i->second->incognito_profile_preferences_session_only); |
143 } | 169 } |
144 NOTREACHED(); | 170 NOTREACHED(); |
145 return NULL; | 171 return NULL; |
146 } | 172 } |
147 | 173 |
148 void ExtensionPrefValueMap::GetExtensionControlledKeys( | 174 void ExtensionPrefValueMap::GetExtensionControlledKeys( |
149 const ExtensionEntry& entry, | 175 const ExtensionEntry& entry, |
150 std::set<std::string>* out) const { | 176 std::set<std::string>* out) const { |
151 PrefValueMap::const_iterator i; | 177 PrefValueMap::const_iterator i; |
152 | 178 |
153 const PrefValueMap& reg_prefs = entry.regular_profile_preferences; | 179 const PrefValueMap& reg_prefs = entry.regular_profile_preferences; |
154 for (i = reg_prefs.begin(); i != reg_prefs.end(); ++i) | 180 for (i = reg_prefs.begin(); i != reg_prefs.end(); ++i) |
155 out->insert(i->first); | 181 out->insert(i->first); |
156 | 182 |
157 const PrefValueMap& inc_prefs_pers = | 183 const PrefValueMap& inc_prefs_pers = |
158 entry.incognito_profile_preferences_persistent; | 184 entry.incognito_profile_preferences_persistent; |
159 for (i = inc_prefs_pers.begin(); i != inc_prefs_pers.end(); ++i) | 185 for (i = inc_prefs_pers.begin(); i != inc_prefs_pers.end(); ++i) |
160 out->insert(i->first); | 186 out->insert(i->first); |
| 187 |
| 188 const PrefValueMap& inc_prefs_session = |
| 189 entry.incognito_profile_preferences_session_only; |
| 190 for (i = inc_prefs_session.begin(); i != inc_prefs_session.end(); ++i) |
| 191 out->insert(i->first); |
161 } | 192 } |
162 | 193 |
163 const Value* ExtensionPrefValueMap::GetEffectivePrefValue( | 194 const Value* ExtensionPrefValueMap::GetEffectivePrefValue( |
164 const std::string& key, | 195 const std::string& key, |
165 bool incognito, | 196 bool incognito, |
166 bool* from_incognito) const { | 197 bool* from_incognito) const { |
167 ExtensionEntryMap::const_iterator winner = | 198 ExtensionEntryMap::const_iterator winner = |
168 GetEffectivePrefValueController(key, incognito, from_incognito); | 199 GetEffectivePrefValueController(key, incognito, from_incognito); |
169 if (winner == entries_.end()) | 200 if (winner == entries_.end()) |
170 return NULL; | 201 return NULL; |
171 | 202 |
172 const Value* value = NULL; | 203 const Value* value = NULL; |
173 const std::string& ext_id = winner->first; | 204 const std::string& ext_id = winner->first; |
| 205 |
| 206 // First search for incognito session only preferences. |
174 if (incognito) { | 207 if (incognito) { |
175 const PrefValueMap* prefs = GetExtensionPrefValueMap( | 208 const PrefValueMap* prefs = GetExtensionPrefValueMap( |
| 209 ext_id, extension_prefs_scope::kIncognitoSessionOnly); |
| 210 prefs->GetValue(key, &value); |
| 211 } |
| 212 |
| 213 // If no incognito session only preference exists, fall back to persistent |
| 214 // incognito preference. |
| 215 if (incognito && !value) { |
| 216 const PrefValueMap* prefs = GetExtensionPrefValueMap( |
176 ext_id, extension_prefs_scope::kIncognitoPersistent); | 217 ext_id, extension_prefs_scope::kIncognitoPersistent); |
177 prefs->GetValue(key, &value); | 218 prefs->GetValue(key, &value); |
178 } | 219 } |
| 220 |
| 221 // Finally consider a regular preference. |
179 if (!value) { | 222 if (!value) { |
180 const PrefValueMap* prefs = GetExtensionPrefValueMap( | 223 const PrefValueMap* prefs = GetExtensionPrefValueMap( |
181 ext_id, extension_prefs_scope::kRegular); | 224 ext_id, extension_prefs_scope::kRegular); |
182 prefs->GetValue(key, &value); | 225 prefs->GetValue(key, &value); |
183 } | 226 } |
184 return value; | 227 return value; |
185 } | 228 } |
186 | 229 |
187 ExtensionPrefValueMap::ExtensionEntryMap::const_iterator | 230 ExtensionPrefValueMap::ExtensionEntryMap::const_iterator |
188 ExtensionPrefValueMap::GetEffectivePrefValueController( | 231 ExtensionPrefValueMap::GetEffectivePrefValueController( |
(...skipping 28 matching lines...) Expand all Loading... |
217 continue; | 260 continue; |
218 | 261 |
219 prefs = GetExtensionPrefValueMap( | 262 prefs = GetExtensionPrefValueMap( |
220 ext_id, extension_prefs_scope::kIncognitoPersistent); | 263 ext_id, extension_prefs_scope::kIncognitoPersistent); |
221 if (prefs->GetValue(key, &value)) { | 264 if (prefs->GetValue(key, &value)) { |
222 winner = i; | 265 winner = i; |
223 winners_install_time = install_time; | 266 winners_install_time = install_time; |
224 if (from_incognito) | 267 if (from_incognito) |
225 *from_incognito = true; | 268 *from_incognito = true; |
226 } | 269 } |
| 270 |
| 271 prefs = GetExtensionPrefValueMap( |
| 272 ext_id, extension_prefs_scope::kIncognitoSessionOnly); |
| 273 if (prefs->GetValue(key, &value)) { |
| 274 winner = i; |
| 275 winners_install_time = install_time; |
| 276 if (from_incognito) |
| 277 *from_incognito = true; |
| 278 } |
227 } | 279 } |
228 return winner; | 280 return winner; |
229 } | 281 } |
230 | 282 |
231 void ExtensionPrefValueMap::AddObserver( | 283 void ExtensionPrefValueMap::AddObserver( |
232 ExtensionPrefValueMap::Observer* observer) { | 284 ExtensionPrefValueMap::Observer* observer) { |
233 observers_.AddObserver(observer); | 285 observers_.AddObserver(observer); |
234 | 286 |
235 // Collect all currently used keys and notify the new observer. | 287 // Collect all currently used keys and notify the new observer. |
236 std::set<std::string> keys; | 288 std::set<std::string> keys; |
(...skipping 25 matching lines...) Expand all Loading... |
262 | 314 |
263 void ExtensionPrefValueMap::NotifyPrefValueChanged(const std::string& key) { | 315 void ExtensionPrefValueMap::NotifyPrefValueChanged(const std::string& key) { |
264 FOR_EACH_OBSERVER(ExtensionPrefValueMap::Observer, observers_, | 316 FOR_EACH_OBSERVER(ExtensionPrefValueMap::Observer, observers_, |
265 OnPrefValueChanged(key)); | 317 OnPrefValueChanged(key)); |
266 } | 318 } |
267 | 319 |
268 void ExtensionPrefValueMap::NotifyOfDestruction() { | 320 void ExtensionPrefValueMap::NotifyOfDestruction() { |
269 FOR_EACH_OBSERVER(ExtensionPrefValueMap::Observer, observers_, | 321 FOR_EACH_OBSERVER(ExtensionPrefValueMap::Observer, observers_, |
270 OnExtensionPrefValueMapDestruction()); | 322 OnExtensionPrefValueMapDestruction()); |
271 } | 323 } |
OLD | NEW |