OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/api/declarative/rules_registry.h" | 5 #include "chrome/browser/extensions/api/declarative/rules_registry.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 last_generated_rule_identifier_id_(0) { | 88 last_generated_rule_identifier_id_(0) { |
89 if (cache_delegate) { | 89 if (cache_delegate) { |
90 cache_delegate_ = cache_delegate->GetWeakPtr(); | 90 cache_delegate_ = cache_delegate->GetWeakPtr(); |
91 cache_delegate->Init(this); | 91 cache_delegate->Init(this); |
92 } | 92 } |
93 } | 93 } |
94 | 94 |
95 std::string RulesRegistry::AddRulesNoFill( | 95 std::string RulesRegistry::AddRulesNoFill( |
96 const std::string& extension_id, | 96 const std::string& extension_id, |
97 const std::vector<linked_ptr<Rule> >& rules) { | 97 const std::vector<linked_ptr<Rule> >& rules) { |
98 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 98 DCHECK_CURRENTLY_ON(owner_thread()); |
99 | 99 |
100 // Verify that all rule IDs are new. | 100 // Verify that all rule IDs are new. |
101 for (std::vector<linked_ptr<Rule> >::const_iterator i = | 101 for (std::vector<linked_ptr<Rule> >::const_iterator i = |
102 rules.begin(); i != rules.end(); ++i) { | 102 rules.begin(); i != rules.end(); ++i) { |
103 const RuleId& rule_id = *((*i)->id); | 103 const RuleId& rule_id = *((*i)->id); |
104 // Every rule should have a priority assigned. | 104 // Every rule should have a priority assigned. |
105 DCHECK((*i)->priority); | 105 DCHECK((*i)->priority); |
106 RulesDictionaryKey key(extension_id, rule_id); | 106 RulesDictionaryKey key(extension_id, rule_id); |
107 if (rules_.find(key) != rules_.end()) | 107 if (rules_.find(key) != rules_.end()) |
108 return base::StringPrintf(kDuplicateRuleId, rule_id.c_str()); | 108 return base::StringPrintf(kDuplicateRuleId, rule_id.c_str()); |
(...skipping 12 matching lines...) Expand all Loading... |
121 rules_[key] = *i; | 121 rules_[key] = *i; |
122 } | 122 } |
123 | 123 |
124 MaybeProcessChangedRules(extension_id); | 124 MaybeProcessChangedRules(extension_id); |
125 return kSuccess; | 125 return kSuccess; |
126 } | 126 } |
127 | 127 |
128 std::string RulesRegistry::AddRules( | 128 std::string RulesRegistry::AddRules( |
129 const std::string& extension_id, | 129 const std::string& extension_id, |
130 const std::vector<linked_ptr<Rule> >& rules) { | 130 const std::vector<linked_ptr<Rule> >& rules) { |
131 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 131 DCHECK_CURRENTLY_ON(owner_thread()); |
132 | 132 |
133 std::string error = CheckAndFillInOptionalRules(extension_id, rules); | 133 std::string error = CheckAndFillInOptionalRules(extension_id, rules); |
134 if (!error.empty()) | 134 if (!error.empty()) |
135 return error; | 135 return error; |
136 FillInOptionalPriorities(rules); | 136 FillInOptionalPriorities(rules); |
137 | 137 |
138 return AddRulesNoFill(extension_id, rules); | 138 return AddRulesNoFill(extension_id, rules); |
139 } | 139 } |
140 | 140 |
141 std::string RulesRegistry::RemoveRules( | 141 std::string RulesRegistry::RemoveRules( |
142 const std::string& extension_id, | 142 const std::string& extension_id, |
143 const std::vector<std::string>& rule_identifiers) { | 143 const std::vector<std::string>& rule_identifiers) { |
144 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 144 DCHECK_CURRENTLY_ON(owner_thread()); |
145 | 145 |
146 std::string error = RemoveRulesImpl(extension_id, rule_identifiers); | 146 std::string error = RemoveRulesImpl(extension_id, rule_identifiers); |
147 | 147 |
148 if (!error.empty()) | 148 if (!error.empty()) |
149 return error; | 149 return error; |
150 | 150 |
151 for (std::vector<std::string>::const_iterator i = rule_identifiers.begin(); | 151 for (std::vector<std::string>::const_iterator i = rule_identifiers.begin(); |
152 i != rule_identifiers.end(); | 152 i != rule_identifiers.end(); |
153 ++i) { | 153 ++i) { |
154 RulesDictionaryKey lookup_key(extension_id, *i); | 154 RulesDictionaryKey lookup_key(extension_id, *i); |
155 rules_.erase(lookup_key); | 155 rules_.erase(lookup_key); |
156 } | 156 } |
157 | 157 |
158 MaybeProcessChangedRules(extension_id); | 158 MaybeProcessChangedRules(extension_id); |
159 RemoveUsedRuleIdentifiers(extension_id, rule_identifiers); | 159 RemoveUsedRuleIdentifiers(extension_id, rule_identifiers); |
160 return kSuccess; | 160 return kSuccess; |
161 } | 161 } |
162 | 162 |
163 std::string RulesRegistry::RemoveAllRules(const std::string& extension_id) { | 163 std::string RulesRegistry::RemoveAllRules(const std::string& extension_id) { |
164 std::string result = RulesRegistry::RemoveAllRulesNoStoreUpdate(extension_id); | 164 std::string result = RulesRegistry::RemoveAllRulesNoStoreUpdate(extension_id); |
165 MaybeProcessChangedRules(extension_id); // Now update the prefs and store. | 165 MaybeProcessChangedRules(extension_id); // Now update the prefs and store. |
166 return result; | 166 return result; |
167 } | 167 } |
168 | 168 |
169 std::string RulesRegistry::RemoveAllRulesNoStoreUpdate( | 169 std::string RulesRegistry::RemoveAllRulesNoStoreUpdate( |
170 const std::string& extension_id) { | 170 const std::string& extension_id) { |
171 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 171 DCHECK_CURRENTLY_ON(owner_thread()); |
172 | 172 |
173 std::string error = RemoveAllRulesImpl(extension_id); | 173 std::string error = RemoveAllRulesImpl(extension_id); |
174 | 174 |
175 if (!error.empty()) | 175 if (!error.empty()) |
176 return error; | 176 return error; |
177 | 177 |
178 for (RulesDictionary::const_iterator i = rules_.begin(); | 178 for (RulesDictionary::const_iterator i = rules_.begin(); |
179 i != rules_.end();) { | 179 i != rules_.end();) { |
180 const RulesDictionaryKey& key = i->first; | 180 const RulesDictionaryKey& key = i->first; |
181 ++i; | 181 ++i; |
182 if (key.first == extension_id) | 182 if (key.first == extension_id) |
183 rules_.erase(key); | 183 rules_.erase(key); |
184 } | 184 } |
185 | 185 |
186 RemoveAllUsedRuleIdentifiers(extension_id); | 186 RemoveAllUsedRuleIdentifiers(extension_id); |
187 return kSuccess; | 187 return kSuccess; |
188 } | 188 } |
189 | 189 |
190 void RulesRegistry::GetRules(const std::string& extension_id, | 190 void RulesRegistry::GetRules(const std::string& extension_id, |
191 const std::vector<std::string>& rule_identifiers, | 191 const std::vector<std::string>& rule_identifiers, |
192 std::vector<linked_ptr<Rule> >* out) { | 192 std::vector<linked_ptr<Rule> >* out) { |
193 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 193 DCHECK_CURRENTLY_ON(owner_thread()); |
194 | 194 |
195 for (std::vector<std::string>::const_iterator i = rule_identifiers.begin(); | 195 for (std::vector<std::string>::const_iterator i = rule_identifiers.begin(); |
196 i != rule_identifiers.end(); ++i) { | 196 i != rule_identifiers.end(); ++i) { |
197 RulesDictionaryKey lookup_key(extension_id, *i); | 197 RulesDictionaryKey lookup_key(extension_id, *i); |
198 RulesDictionary::iterator entry = rules_.find(lookup_key); | 198 RulesDictionary::iterator entry = rules_.find(lookup_key); |
199 if (entry != rules_.end()) | 199 if (entry != rules_.end()) |
200 out->push_back(entry->second); | 200 out->push_back(entry->second); |
201 } | 201 } |
202 } | 202 } |
203 | 203 |
204 void RulesRegistry::GetAllRules(const std::string& extension_id, | 204 void RulesRegistry::GetAllRules(const std::string& extension_id, |
205 std::vector<linked_ptr<Rule> >* out) { | 205 std::vector<linked_ptr<Rule> >* out) { |
206 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 206 DCHECK_CURRENTLY_ON(owner_thread()); |
207 | 207 |
208 for (RulesDictionary::const_iterator i = rules_.begin(); | 208 for (RulesDictionary::const_iterator i = rules_.begin(); |
209 i != rules_.end(); ++i) { | 209 i != rules_.end(); ++i) { |
210 const RulesDictionaryKey& key = i->first; | 210 const RulesDictionaryKey& key = i->first; |
211 if (key.first == extension_id) | 211 if (key.first == extension_id) |
212 out->push_back(i->second); | 212 out->push_back(i->second); |
213 } | 213 } |
214 } | 214 } |
215 | 215 |
216 void RulesRegistry::OnExtensionUnloaded(const std::string& extension_id) { | 216 void RulesRegistry::OnExtensionUnloaded(const std::string& extension_id) { |
217 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 217 DCHECK_CURRENTLY_ON(owner_thread()); |
218 std::string error = RemoveAllRulesImpl(extension_id); | 218 std::string error = RemoveAllRulesImpl(extension_id); |
219 if (!error.empty()) | 219 if (!error.empty()) |
220 LOG(ERROR) << error; | 220 LOG(ERROR) << error; |
221 } | 221 } |
222 | 222 |
223 void RulesRegistry::OnExtensionUninstalled(const std::string& extension_id) { | 223 void RulesRegistry::OnExtensionUninstalled(const std::string& extension_id) { |
224 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 224 DCHECK_CURRENTLY_ON(owner_thread()); |
225 std::string error = RemoveAllRulesNoStoreUpdate(extension_id); | 225 std::string error = RemoveAllRulesNoStoreUpdate(extension_id); |
226 if (!error.empty()) | 226 if (!error.empty()) |
227 LOG(ERROR) << error; | 227 LOG(ERROR) << error; |
228 } | 228 } |
229 | 229 |
230 void RulesRegistry::OnExtensionLoaded(const std::string& extension_id) { | 230 void RulesRegistry::OnExtensionLoaded(const std::string& extension_id) { |
231 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 231 DCHECK_CURRENTLY_ON(owner_thread()); |
232 std::vector<linked_ptr<Rule> > rules; | 232 std::vector<linked_ptr<Rule> > rules; |
233 GetAllRules(extension_id, &rules); | 233 GetAllRules(extension_id, &rules); |
234 std::string error = AddRulesImpl(extension_id, rules); | 234 std::string error = AddRulesImpl(extension_id, rules); |
235 if (!error.empty()) | 235 if (!error.empty()) |
236 LOG(ERROR) << error; | 236 LOG(ERROR) << error; |
237 } | 237 } |
238 | 238 |
239 size_t RulesRegistry::GetNumberOfUsedRuleIdentifiersForTesting() const { | 239 size_t RulesRegistry::GetNumberOfUsedRuleIdentifiersForTesting() const { |
240 size_t entry_count = 0u; | 240 size_t entry_count = 0u; |
241 for (RuleIdentifiersMap::const_iterator extension = | 241 for (RuleIdentifiersMap::const_iterator extension = |
242 used_rule_identifiers_.begin(); | 242 used_rule_identifiers_.begin(); |
243 extension != used_rule_identifiers_.end(); | 243 extension != used_rule_identifiers_.end(); |
244 ++extension) { | 244 ++extension) { |
245 // Each extension is counted as 1 just for being there. Otherwise we miss | 245 // Each extension is counted as 1 just for being there. Otherwise we miss |
246 // keys with empty values. | 246 // keys with empty values. |
247 entry_count += 1u + extension->second.size(); | 247 entry_count += 1u + extension->second.size(); |
248 } | 248 } |
249 return entry_count; | 249 return entry_count; |
250 } | 250 } |
251 | 251 |
252 void RulesRegistry::DeserializeAndAddRules( | 252 void RulesRegistry::DeserializeAndAddRules( |
253 const std::string& extension_id, | 253 const std::string& extension_id, |
254 scoped_ptr<base::Value> rules) { | 254 scoped_ptr<base::Value> rules) { |
255 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 255 DCHECK_CURRENTLY_ON(owner_thread()); |
256 | 256 |
257 AddRulesNoFill(extension_id, RulesFromValue(rules.get())); | 257 AddRulesNoFill(extension_id, RulesFromValue(rules.get())); |
258 } | 258 } |
259 | 259 |
260 RulesRegistry::~RulesRegistry() { | 260 RulesRegistry::~RulesRegistry() { |
261 } | 261 } |
262 | 262 |
263 void RulesRegistry::MarkReady(base::Time storage_init_time) { | 263 void RulesRegistry::MarkReady(base::Time storage_init_time) { |
264 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 264 DCHECK_CURRENTLY_ON(owner_thread()); |
265 | 265 |
266 if (!storage_init_time.is_null()) { | 266 if (!storage_init_time.is_null()) { |
267 UMA_HISTOGRAM_TIMES("Extensions.DeclarativeRulesStorageInitialization", | 267 UMA_HISTOGRAM_TIMES("Extensions.DeclarativeRulesStorageInitialization", |
268 base::Time::Now() - storage_init_time); | 268 base::Time::Now() - storage_init_time); |
269 } | 269 } |
270 | 270 |
271 ready_.Signal(); | 271 ready_.Signal(); |
272 } | 272 } |
273 | 273 |
274 void RulesRegistry::ProcessChangedRules(const std::string& extension_id) { | 274 void RulesRegistry::ProcessChangedRules(const std::string& extension_id) { |
275 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 275 DCHECK_CURRENTLY_ON(owner_thread()); |
276 | 276 |
277 DCHECK(ContainsKey(process_changed_rules_requested_, extension_id)); | 277 DCHECK(ContainsKey(process_changed_rules_requested_, extension_id)); |
278 process_changed_rules_requested_[extension_id] = NOT_SCHEDULED_FOR_PROCESSING; | 278 process_changed_rules_requested_[extension_id] = NOT_SCHEDULED_FOR_PROCESSING; |
279 | 279 |
280 std::vector<linked_ptr<Rule> > new_rules; | 280 std::vector<linked_ptr<Rule> > new_rules; |
281 GetAllRules(extension_id, &new_rules); | 281 GetAllRules(extension_id, &new_rules); |
282 content::BrowserThread::PostTask( | 282 content::BrowserThread::PostTask( |
283 content::BrowserThread::UI, | 283 content::BrowserThread::UI, |
284 FROM_HERE, | 284 FROM_HERE, |
285 base::Bind(&RulesCacheDelegate::WriteToStorage, | 285 base::Bind(&RulesCacheDelegate::WriteToStorage, |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 for (i = identifiers.begin(); i != identifiers.end(); ++i) | 372 for (i = identifiers.begin(); i != identifiers.end(); ++i) |
373 used_rule_identifiers_[extension_id].erase(*i); | 373 used_rule_identifiers_[extension_id].erase(*i); |
374 } | 374 } |
375 | 375 |
376 void RulesRegistry::RemoveAllUsedRuleIdentifiers( | 376 void RulesRegistry::RemoveAllUsedRuleIdentifiers( |
377 const std::string& extension_id) { | 377 const std::string& extension_id) { |
378 used_rule_identifiers_.erase(extension_id); | 378 used_rule_identifiers_.erase(extension_id); |
379 } | 379 } |
380 | 380 |
381 } // namespace extensions | 381 } // namespace extensions |
OLD | NEW |