| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 std::string RulesRegistry::RemoveRules( | 144 std::string RulesRegistry::RemoveRules( |
| 145 const std::string& extension_id, | 145 const std::string& extension_id, |
| 146 const std::vector<std::string>& rule_identifiers) { | 146 const std::vector<std::string>& rule_identifiers) { |
| 147 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 147 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
| 148 | 148 |
| 149 std::string error = RemoveRulesImpl(extension_id, rule_identifiers); | 149 std::string error = RemoveRulesImpl(extension_id, rule_identifiers); |
| 150 | 150 |
| 151 if (!error.empty()) | 151 if (!error.empty()) |
| 152 return error; | 152 return error; |
| 153 | 153 |
| 154 // Commit removal of rules from |rules_| on success. | 154 for (std::vector<std::string>::const_iterator i = rule_identifiers.begin(); |
| 155 for (std::vector<std::string>::const_iterator i = | 155 i != rule_identifiers.end(); |
| 156 rule_identifiers.begin(); i != rule_identifiers.end(); ++i) { | 156 ++i) { |
| 157 RulesDictionaryKey lookup_key(extension_id, *i); | 157 RulesDictionaryKey lookup_key(extension_id, *i); |
| 158 rules_.erase(lookup_key); | 158 rules_.erase(lookup_key); |
| 159 } | 159 } |
| 160 | 160 |
| 161 MaybeProcessChangedRules(extension_id); | 161 MaybeProcessChangedRules(extension_id); |
| 162 RemoveUsedRuleIdentifiers(extension_id, rule_identifiers); | 162 RemoveUsedRuleIdentifiers(extension_id, rule_identifiers); |
| 163 return kSuccess; | 163 return kSuccess; |
| 164 } | 164 } |
| 165 | 165 |
| 166 std::string RulesRegistry::RemoveAllRules(const std::string& extension_id) { | 166 std::string RulesRegistry::RemoveAllRules(const std::string& extension_id) { |
| 167 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 167 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
| 168 | 168 |
| 169 std::string error = RemoveAllRulesImpl(extension_id); | 169 std::string error = RemoveAllRulesImpl(extension_id); |
| 170 | 170 |
| 171 if (!error.empty()) | 171 if (!error.empty()) |
| 172 return error; | 172 return error; |
| 173 | 173 |
| 174 // Commit removal of rules from |rules_| on success. | |
| 175 for (RulesDictionary::const_iterator i = rules_.begin(); | 174 for (RulesDictionary::const_iterator i = rules_.begin(); |
| 176 i != rules_.end();) { | 175 i != rules_.end();) { |
| 177 const RulesDictionaryKey& key = i->first; | 176 const RulesDictionaryKey& key = i->first; |
| 178 ++i; | 177 ++i; |
| 179 if (key.first == extension_id) | 178 if (key.first == extension_id) |
| 180 rules_.erase(key); | 179 rules_.erase(key); |
| 181 } | 180 } |
| 182 | 181 |
| 183 MaybeProcessChangedRules(extension_id); | 182 MaybeProcessChangedRules(extension_id); |
| 184 RemoveAllUsedRuleIdentifiers(extension_id); | 183 RemoveAllUsedRuleIdentifiers(extension_id); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 206 for (RulesDictionary::const_iterator i = rules_.begin(); | 205 for (RulesDictionary::const_iterator i = rules_.begin(); |
| 207 i != rules_.end(); ++i) { | 206 i != rules_.end(); ++i) { |
| 208 const RulesDictionaryKey& key = i->first; | 207 const RulesDictionaryKey& key = i->first; |
| 209 if (key.first == extension_id) | 208 if (key.first == extension_id) |
| 210 out->push_back(i->second); | 209 out->push_back(i->second); |
| 211 } | 210 } |
| 212 } | 211 } |
| 213 | 212 |
| 214 void RulesRegistry::OnExtensionUnloaded(const std::string& extension_id) { | 213 void RulesRegistry::OnExtensionUnloaded(const std::string& extension_id) { |
| 215 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 214 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
| 215 std::string error = RemoveAllRulesImpl(extension_id); |
| 216 if (!error.empty()) |
| 217 LOG(ERROR) << error; |
| 218 } |
| 219 |
| 220 void RulesRegistry::OnExtensionUninstalled(const std::string& extension_id) { |
| 221 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
| 216 std::string error = RemoveAllRules(extension_id); | 222 std::string error = RemoveAllRules(extension_id); |
| 217 if (!error.empty()) | 223 if (!error.empty()) |
| 218 LOG(ERROR) << error; | 224 LOG(ERROR) << error; |
| 219 used_rule_identifiers_.erase(extension_id); | 225 } |
| 226 |
| 227 void RulesRegistry::OnExtensionLoaded(const std::string& extension_id) { |
| 228 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
| 229 std::vector<linked_ptr<Rule> > rules; |
| 230 GetAllRules(extension_id, &rules); |
| 231 std::string error = AddRulesImpl(extension_id, rules); |
| 232 if (!error.empty()) |
| 233 LOG(ERROR) << error; |
| 220 } | 234 } |
| 221 | 235 |
| 222 size_t RulesRegistry::GetNumberOfUsedRuleIdentifiersForTesting() const { | 236 size_t RulesRegistry::GetNumberOfUsedRuleIdentifiersForTesting() const { |
| 223 size_t entry_count = 0u; | 237 size_t entry_count = 0u; |
| 224 for (RuleIdentifiersMap::const_iterator extension = | 238 for (RuleIdentifiersMap::const_iterator extension = |
| 225 used_rule_identifiers_.begin(); | 239 used_rule_identifiers_.begin(); |
| 226 extension != used_rule_identifiers_.end(); | 240 extension != used_rule_identifiers_.end(); |
| 227 ++extension) { | 241 ++extension) { |
| 228 // Each extension is counted as 1 just for being there. Otherwise we miss | 242 // Each extension is counted as 1 just for being there. Otherwise we miss |
| 229 // keys with empty values. | 243 // keys with empty values. |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 for (i = identifiers.begin(); i != identifiers.end(); ++i) | 361 for (i = identifiers.begin(); i != identifiers.end(); ++i) |
| 348 used_rule_identifiers_[extension_id].erase(*i); | 362 used_rule_identifiers_[extension_id].erase(*i); |
| 349 } | 363 } |
| 350 | 364 |
| 351 void RulesRegistry::RemoveAllUsedRuleIdentifiers( | 365 void RulesRegistry::RemoveAllUsedRuleIdentifiers( |
| 352 const std::string& extension_id) { | 366 const std::string& extension_id) { |
| 353 used_rule_identifiers_.erase(extension_id); | 367 used_rule_identifiers_.erase(extension_id); |
| 354 } | 368 } |
| 355 | 369 |
| 356 } // namespace extensions | 370 } // namespace extensions |
| OLD | NEW |