Index: chrome/browser/extensions/api/declarative/rules_registry_with_cache.cc |
diff --git a/chrome/browser/extensions/api/declarative/rules_registry_with_cache.cc b/chrome/browser/extensions/api/declarative/rules_registry_with_cache.cc |
index a1e3a7c187b20c01737e8ab453eb59318a64491b..b4057c4f89b5e574d368f7d6b7c35358b3adfe82 100644 |
--- a/chrome/browser/extensions/api/declarative/rules_registry_with_cache.cc |
+++ b/chrome/browser/extensions/api/declarative/rules_registry_with_cache.cc |
@@ -42,6 +42,7 @@ std::vector<linked_ptr<extensions::RulesRegistry::Rule> > RulesFromValue( |
if (!value || !value->GetAsList(&list)) |
return rules; |
+ rules.reserve(list->GetSize()); |
for (size_t i = 0; i < list->GetSize(); ++i) { |
const base::DictionaryValue* dict = NULL; |
if (!list->GetDictionary(i, &dict)) |
@@ -111,13 +112,13 @@ std::string RulesRegistryWithCache::AddRules( |
const std::string& extension_id, |
const std::vector<linked_ptr<Rule> >& rules) { |
DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
+ RulesDictionary& current_rules = rules_[extension_id]; |
// Verify that all rule IDs are new. |
for (std::vector<linked_ptr<Rule> >::const_iterator i = |
rules.begin(); i != rules.end(); ++i) { |
const RuleId& rule_id = *((*i)->id); |
- RulesDictionaryKey key(extension_id, rule_id); |
- if (rules_.find(key) != rules_.end()) |
+ if (current_rules.find(rule_id) != current_rules.end()) |
return base::StringPrintf(kDuplicateRuleId, rule_id.c_str()); |
} |
@@ -130,8 +131,7 @@ std::string RulesRegistryWithCache::AddRules( |
for (std::vector<linked_ptr<Rule> >::const_iterator i = |
rules.begin(); i != rules.end(); ++i) { |
const RuleId& rule_id = *((*i)->id); |
- RulesDictionaryKey key(extension_id, rule_id); |
- rules_[key] = *i; |
+ current_rules[rule_id] = *i; |
} |
ProcessChangedRules(extension_id); |
@@ -149,11 +149,13 @@ std::string RulesRegistryWithCache::RemoveRules( |
return error; |
// Commit removal of rules from |rules_| on success. |
+ RulesDictionary& current_rules = rules_[extension_id]; |
for (std::vector<std::string>::const_iterator i = |
rule_identifiers.begin(); i != rule_identifiers.end(); ++i) { |
- RulesDictionaryKey lookup_key(extension_id, *i); |
- rules_.erase(lookup_key); |
+ current_rules.erase(*i); |
} |
+ if (current_rules.empty()) |
+ rules_.erase(extension_id); |
ProcessChangedRules(extension_id); |
return kSuccess; |
@@ -169,13 +171,7 @@ std::string RulesRegistryWithCache::RemoveAllRules( |
return error; |
// Commit removal of rules from |rules_| on success. |
- for (RulesDictionary::const_iterator i = rules_.begin(); |
- i != rules_.end();) { |
- const RulesDictionaryKey& key = i->first; |
- ++i; |
- if (key.first == extension_id) |
- rules_.erase(key); |
- } |
+ rules_.erase(extension_id); |
ProcessChangedRules(extension_id); |
return kSuccess; |
@@ -187,11 +183,11 @@ std::string RulesRegistryWithCache::GetRules( |
std::vector<linked_ptr<RulesRegistry::Rule> >* out) { |
DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
+ RulesDictionary& current_rules = rules_[extension_id]; |
for (std::vector<std::string>::const_iterator i = rule_identifiers.begin(); |
i != rule_identifiers.end(); ++i) { |
- RulesDictionaryKey lookup_key(extension_id, *i); |
- RulesDictionary::iterator entry = rules_.find(lookup_key); |
- if (entry != rules_.end()) |
+ RulesDictionary::iterator entry = current_rules.find(*i); |
+ if (entry != current_rules.end()) |
out->push_back(entry->second); |
} |
return kSuccess; |
@@ -202,11 +198,11 @@ std::string RulesRegistryWithCache::GetAllRules( |
std::vector<linked_ptr<RulesRegistry::Rule> >* out) { |
DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
- for (RulesDictionary::const_iterator i = rules_.begin(); |
- i != rules_.end(); ++i) { |
- const RulesDictionaryKey& key = i->first; |
- if (key.first == extension_id) |
- out->push_back(i->second); |
+ RulesDictionary& current_rules = rules_[extension_id]; |
+ out->reserve(out->size() + current_rules.size()); |
+ for (RulesDictionary::const_iterator i = current_rules.begin(); |
+ i != current_rules.end(); ++i) { |
+ out->push_back(i->second); |
} |
return kSuccess; |
} |
@@ -250,8 +246,8 @@ void RulesRegistryWithCache::ProcessChangedRules( |
const std::string& extension_id) { |
DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
- std::vector<linked_ptr<RulesRegistry::Rule> > new_rules; |
- std::string error = GetAllRules(extension_id, &new_rules); |
+ scoped_ptr<RulesVector> new_rules(new RulesVector()); |
+ std::string error = GetAllRules(extension_id, new_rules.get()); |
DCHECK_EQ(std::string(), error); |
content::BrowserThread::PostTask( |
content::BrowserThread::UI, |
@@ -259,7 +255,7 @@ void RulesRegistryWithCache::ProcessChangedRules( |
base::Bind(&RuleStorageOnUI::WriteToStorage, |
storage_on_ui_, |
extension_id, |
- base::Passed(RulesToValue(new_rules)))); |
+ base::Passed(&new_rules))); |
} |
// RulesRegistryWithCache::RuleStorageOnUI |
@@ -326,14 +322,16 @@ void RulesRegistryWithCache::RuleStorageOnUI::Init() { |
void RulesRegistryWithCache::RuleStorageOnUI::WriteToStorage( |
const std::string& extension_id, |
- scoped_ptr<base::Value> value) { |
+ scoped_ptr<RulesVector> new_rules) { |
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
if (!profile_) |
return; |
StateStore* store = ExtensionSystem::Get(profile_)->rules_store(); |
- if (store) |
- store->SetExtensionValue(extension_id, storage_key_, value.Pass()); |
+ if (store) { |
+ store->SetExtensionValue( |
+ extension_id, storage_key_, RulesToValue(*new_rules)); |
+ } |
} |
void RulesRegistryWithCache::RuleStorageOnUI::Observe( |