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); | |
Matt Perry
2011/05/24 21:04:56
Per the style guide, avoid abbrevs.
battre
2011/05/25 11:31:45
Done.
| |
125 case extension_prefs_scope::kIncognitoPersistent: | |
126 return &(i->second->inc_preferences_persistent); | |
127 } | |
123 } | 128 } |
124 | 129 |
125 const PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap( | 130 const PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap( |
126 const std::string& ext_id, | 131 const std::string& ext_id, |
127 bool incognito) const { | 132 extension_prefs_scope::Scope scope) const { |
128 ExtensionEntryMap::const_iterator i = entries_.find(ext_id); | 133 ExtensionEntryMap::const_iterator i = entries_.find(ext_id); |
129 CHECK(i != entries_.end()); | 134 CHECK(i != entries_.end()); |
130 return incognito ? &(i->second->inc_preferences) | 135 switch (scope) { |
131 : &(i->second->reg_preferences); | 136 case extension_prefs_scope::kRegular: |
137 return &(i->second->reg_preferences); | |
138 case extension_prefs_scope::kIncognitoPersistent: | |
139 return &(i->second->inc_preferences_persistent); | |
140 } | |
132 } | 141 } |
133 | 142 |
134 void ExtensionPrefValueMap::GetExtensionControlledKeys( | 143 void ExtensionPrefValueMap::GetExtensionControlledKeys( |
135 const ExtensionEntry& entry, | 144 const ExtensionEntry& entry, |
136 std::set<std::string>* out) const { | 145 std::set<std::string>* out) const { |
137 PrefValueMap::const_iterator i; | 146 PrefValueMap::const_iterator i; |
138 | 147 |
139 const PrefValueMap& reg_prefs = entry.reg_preferences; | 148 const PrefValueMap& reg_prefs = entry.reg_preferences; |
140 for (i = reg_prefs.begin(); i != reg_prefs.end(); ++i) | 149 for (i = reg_prefs.begin(); i != reg_prefs.end(); ++i) |
141 out->insert(i->first); | 150 out->insert(i->first); |
142 | 151 |
143 const PrefValueMap& inc_prefs = entry.inc_preferences; | 152 const PrefValueMap& inc_prefs_pers = entry.inc_preferences_persistent; |
144 for (i = inc_prefs.begin(); i != inc_prefs.end(); ++i) | 153 for (i = inc_prefs_pers.begin(); i != inc_prefs_pers.end(); ++i) |
145 out->insert(i->first); | 154 out->insert(i->first); |
146 } | 155 } |
147 | 156 |
148 const Value* ExtensionPrefValueMap::GetEffectivePrefValue( | 157 const Value* ExtensionPrefValueMap::GetEffectivePrefValue( |
149 const std::string& key, | 158 const std::string& key, |
150 bool incognito, | 159 bool incognito, |
151 bool* from_incognito) const { | 160 bool* from_incognito) const { |
152 ExtensionEntryMap::const_iterator winner = | 161 ExtensionEntryMap::const_iterator winner = |
153 GetEffectivePrefValueController(key, incognito, from_incognito); | 162 GetEffectivePrefValueController(key, incognito, from_incognito); |
154 if (winner == entries_.end()) | 163 if (winner == entries_.end()) |
155 return NULL; | 164 return NULL; |
156 | 165 |
157 const Value* value = NULL; | 166 const Value* value = NULL; |
158 const std::string& ext_id = winner->first; | 167 const std::string& ext_id = winner->first; |
159 if (incognito) | 168 if (incognito) { |
160 GetExtensionPrefValueMap(ext_id, true)->GetValue(key, &value); | 169 const PrefValueMap* prefs = GetExtensionPrefValueMap( |
161 if (!value) | 170 ext_id, extension_prefs_scope::kIncognitoPersistent); |
162 GetExtensionPrefValueMap(ext_id, false)->GetValue(key, &value); | 171 prefs->GetValue(key, &value); |
172 } | |
173 if (!value) { | |
174 const PrefValueMap* prefs = GetExtensionPrefValueMap( | |
175 ext_id, extension_prefs_scope::kRegular); | |
176 prefs->GetValue(key, &value); | |
177 } | |
163 return value; | 178 return value; |
164 } | 179 } |
165 | 180 |
166 ExtensionPrefValueMap::ExtensionEntryMap::const_iterator | 181 ExtensionPrefValueMap::ExtensionEntryMap::const_iterator |
167 ExtensionPrefValueMap::GetEffectivePrefValueController( | 182 ExtensionPrefValueMap::GetEffectivePrefValueController( |
168 const std::string& key, | 183 const std::string& key, |
169 bool incognito, | 184 bool incognito, |
170 bool* from_incognito) const { | 185 bool* from_incognito) const { |
171 ExtensionEntryMap::const_iterator winner = entries_.end(); | 186 ExtensionEntryMap::const_iterator winner = entries_.end(); |
172 base::Time winners_install_time; | 187 base::Time winners_install_time; |
173 | 188 |
174 ExtensionEntryMap::const_iterator i; | 189 ExtensionEntryMap::const_iterator i; |
175 for (i = entries_.begin(); i != entries_.end(); ++i) { | 190 for (i = entries_.begin(); i != entries_.end(); ++i) { |
176 const std::string& ext_id = i->first; | 191 const std::string& ext_id = i->first; |
177 const base::Time& install_time = i->second->install_time; | 192 const base::Time& install_time = i->second->install_time; |
178 const bool enabled = i->second->enabled; | 193 const bool enabled = i->second->enabled; |
179 | 194 |
180 if (!enabled) | 195 if (!enabled) |
181 continue; | 196 continue; |
182 if (install_time < winners_install_time) | 197 if (install_time < winners_install_time) |
183 continue; | 198 continue; |
184 | 199 |
185 const Value* value = NULL; | 200 const Value* value = NULL; |
186 const PrefValueMap* prefs = GetExtensionPrefValueMap(ext_id, false); | 201 const PrefValueMap* prefs = GetExtensionPrefValueMap( |
202 ext_id, extension_prefs_scope::kRegular); | |
187 if (prefs->GetValue(key, &value)) { | 203 if (prefs->GetValue(key, &value)) { |
188 winner = i; | 204 winner = i; |
189 winners_install_time = install_time; | 205 winners_install_time = install_time; |
190 if (from_incognito) | 206 if (from_incognito) |
191 *from_incognito = false; | 207 *from_incognito = false; |
192 } | 208 } |
193 | 209 |
194 if (!incognito) | 210 if (!incognito) |
195 continue; | 211 continue; |
196 | 212 |
197 prefs = GetExtensionPrefValueMap(ext_id, true); | 213 prefs = GetExtensionPrefValueMap( |
214 ext_id, extension_prefs_scope::kIncognitoPersistent); | |
198 if (prefs->GetValue(key, &value)) { | 215 if (prefs->GetValue(key, &value)) { |
199 winner = i; | 216 winner = i; |
200 winners_install_time = install_time; | 217 winners_install_time = install_time; |
201 if (from_incognito) | 218 if (from_incognito) |
202 *from_incognito = true; | 219 *from_incognito = true; |
203 } | 220 } |
204 } | 221 } |
205 return winner; | 222 return winner; |
206 } | 223 } |
207 | 224 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
239 | 256 |
240 void ExtensionPrefValueMap::NotifyPrefValueChanged(const std::string& key) { | 257 void ExtensionPrefValueMap::NotifyPrefValueChanged(const std::string& key) { |
241 FOR_EACH_OBSERVER(ExtensionPrefValueMap::Observer, observers_, | 258 FOR_EACH_OBSERVER(ExtensionPrefValueMap::Observer, observers_, |
242 OnPrefValueChanged(key)); | 259 OnPrefValueChanged(key)); |
243 } | 260 } |
244 | 261 |
245 void ExtensionPrefValueMap::NotifyOfDestruction() { | 262 void ExtensionPrefValueMap::NotifyOfDestruction() { |
246 FOR_EACH_OBSERVER(ExtensionPrefValueMap::Observer, observers_, | 263 FOR_EACH_OBSERVER(ExtensionPrefValueMap::Observer, observers_, |
247 OnExtensionPrefValueMapDestruction()); | 264 OnExtensionPrefValueMapDestruction()); |
248 } | 265 } |
OLD | NEW |