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