| 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_with_cache.h" | 5 #include "chrome/browser/extensions/api/declarative/rules_registry_with_cache.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
| 9 | 9 |
| 10 namespace { | 10 namespace { |
| 11 | 11 |
| 12 const char kSuccess[] = ""; | 12 const char kSuccess[] = ""; |
| 13 const char kDuplicateRuleId[] = "Duplicate rule ID: %s"; | 13 const char kDuplicateRuleId[] = "Duplicate rule ID: %s"; |
| 14 | 14 |
| 15 } // namespace | 15 } // namespace |
| 16 | 16 |
| 17 namespace extensions { | 17 namespace extensions { |
| 18 | 18 |
| 19 RulesRegistryWithCache::RulesRegistryWithCache() {} | 19 RulesRegistryWithCache::RulesRegistryWithCache(Delegate* delegate) |
| 20 : delegate_(delegate) { |
| 21 } |
| 22 |
| 23 void RulesRegistryWithCache::AddReadyCallback(const base::Closure& callback) { |
| 24 ready_callbacks_.push_back(callback); |
| 25 } |
| 26 |
| 27 void RulesRegistryWithCache::OnReady() { |
| 28 for (size_t i = 0; i < ready_callbacks_.size(); ++i) |
| 29 ready_callbacks_[i].Run(); |
| 30 ready_callbacks_.clear(); |
| 31 } |
| 20 | 32 |
| 21 std::string RulesRegistryWithCache::AddRules( | 33 std::string RulesRegistryWithCache::AddRules( |
| 22 const std::string& extension_id, | 34 const std::string& extension_id, |
| 23 const std::vector<linked_ptr<Rule> >& rules) { | 35 const std::vector<linked_ptr<Rule> >& rules) { |
| 24 DCHECK(content::BrowserThread::CurrentlyOn(GetOwnerThread())); | 36 DCHECK(content::BrowserThread::CurrentlyOn(GetOwnerThread())); |
| 25 | 37 |
| 26 // Verify that all rule IDs are new. | 38 // Verify that all rule IDs are new. |
| 27 for (std::vector<linked_ptr<Rule> >::const_iterator i = | 39 for (std::vector<linked_ptr<Rule> >::const_iterator i = |
| 28 rules.begin(); i != rules.end(); ++i) { | 40 rules.begin(); i != rules.end(); ++i) { |
| 29 const RuleId& rule_id = *((*i)->id); | 41 const RuleId& rule_id = *((*i)->id); |
| 30 RulesDictionaryKey key(extension_id, rule_id); | 42 RulesDictionaryKey key(extension_id, rule_id); |
| 31 if (rules_.find(key) != rules_.end()) | 43 if (rules_.find(key) != rules_.end()) |
| 32 return StringPrintf(kDuplicateRuleId, rule_id.c_str()); | 44 return StringPrintf(kDuplicateRuleId, rule_id.c_str()); |
| 33 } | 45 } |
| 34 | 46 |
| 35 std::string error = AddRulesImpl(extension_id, rules); | 47 std::string error = AddRulesImpl(extension_id, rules); |
| 36 | 48 |
| 37 if (!error.empty()) | 49 if (!error.empty()) |
| 38 return error; | 50 return error; |
| 39 | 51 |
| 40 // Commit all rules into |rules_| on success. | 52 // Commit all rules into |rules_| on success. |
| 41 for (std::vector<linked_ptr<Rule> >::const_iterator i = | 53 for (std::vector<linked_ptr<Rule> >::const_iterator i = |
| 42 rules.begin(); i != rules.end(); ++i) { | 54 rules.begin(); i != rules.end(); ++i) { |
| 43 const RuleId& rule_id = *((*i)->id); | 55 const RuleId& rule_id = *((*i)->id); |
| 44 RulesDictionaryKey key(extension_id, rule_id); | 56 RulesDictionaryKey key(extension_id, rule_id); |
| 45 rules_[key] = *i; | 57 rules_[key] = *i; |
| 46 } | 58 } |
| 59 |
| 60 NotifyRulesChanged(extension_id); |
| 47 return kSuccess; | 61 return kSuccess; |
| 48 } | 62 } |
| 49 | 63 |
| 50 std::string RulesRegistryWithCache::RemoveRules( | 64 std::string RulesRegistryWithCache::RemoveRules( |
| 51 const std::string& extension_id, | 65 const std::string& extension_id, |
| 52 const std::vector<std::string>& rule_identifiers) { | 66 const std::vector<std::string>& rule_identifiers) { |
| 53 DCHECK(content::BrowserThread::CurrentlyOn(GetOwnerThread())); | 67 DCHECK(content::BrowserThread::CurrentlyOn(GetOwnerThread())); |
| 54 | 68 |
| 55 std::string error = RemoveRulesImpl(extension_id, rule_identifiers); | 69 std::string error = RemoveRulesImpl(extension_id, rule_identifiers); |
| 56 | 70 |
| 57 if (!error.empty()) | 71 if (!error.empty()) |
| 58 return error; | 72 return error; |
| 59 | 73 |
| 60 // Commit removal of rules from |rules_| on success. | 74 // Commit removal of rules from |rules_| on success. |
| 61 for (std::vector<std::string>::const_iterator i = | 75 for (std::vector<std::string>::const_iterator i = |
| 62 rule_identifiers.begin(); i != rule_identifiers.end(); ++i) { | 76 rule_identifiers.begin(); i != rule_identifiers.end(); ++i) { |
| 63 RulesDictionaryKey lookup_key(extension_id, *i); | 77 RulesDictionaryKey lookup_key(extension_id, *i); |
| 64 rules_.erase(lookup_key); | 78 rules_.erase(lookup_key); |
| 65 } | 79 } |
| 80 |
| 81 NotifyRulesChanged(extension_id); |
| 66 return kSuccess; | 82 return kSuccess; |
| 67 } | 83 } |
| 68 | 84 |
| 69 std::string RulesRegistryWithCache::RemoveAllRules( | 85 std::string RulesRegistryWithCache::RemoveAllRules( |
| 70 const std::string& extension_id) { | 86 const std::string& extension_id) { |
| 71 DCHECK(content::BrowserThread::CurrentlyOn(GetOwnerThread())); | 87 DCHECK(content::BrowserThread::CurrentlyOn(GetOwnerThread())); |
| 72 | 88 |
| 73 std::string error = RemoveAllRulesImpl(extension_id); | 89 std::string error = RemoveAllRulesImpl(extension_id); |
| 74 | 90 |
| 75 if (!error.empty()) | 91 if (!error.empty()) |
| 76 return error; | 92 return error; |
| 77 | 93 |
| 78 // Commit removal of rules from |rules_| on success. | 94 // Commit removal of rules from |rules_| on success. |
| 79 for (RulesDictionary::const_iterator i = rules_.begin(); | 95 for (RulesDictionary::const_iterator i = rules_.begin(); |
| 80 i != rules_.end();) { | 96 i != rules_.end();) { |
| 81 const RulesDictionaryKey& key = i->first; | 97 const RulesDictionaryKey& key = i->first; |
| 82 ++i; | 98 ++i; |
| 83 if (key.first == extension_id) | 99 if (key.first == extension_id) |
| 84 rules_.erase(key); | 100 rules_.erase(key); |
| 85 } | 101 } |
| 102 |
| 103 NotifyRulesChanged(extension_id); |
| 86 return kSuccess; | 104 return kSuccess; |
| 87 } | 105 } |
| 88 | 106 |
| 89 std::string RulesRegistryWithCache::GetRules( | 107 std::string RulesRegistryWithCache::GetRules( |
| 90 const std::string& extension_id, | 108 const std::string& extension_id, |
| 91 const std::vector<std::string>& rule_identifiers, | 109 const std::vector<std::string>& rule_identifiers, |
| 92 std::vector<linked_ptr<RulesRegistry::Rule> >* out) { | 110 std::vector<linked_ptr<RulesRegistry::Rule> >* out) { |
| 93 DCHECK(content::BrowserThread::CurrentlyOn(GetOwnerThread())); | 111 DCHECK(content::BrowserThread::CurrentlyOn(GetOwnerThread())); |
| 94 | 112 |
| 95 for (std::vector<std::string>::const_iterator i = rule_identifiers.begin(); | 113 for (std::vector<std::string>::const_iterator i = rule_identifiers.begin(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 117 } | 135 } |
| 118 | 136 |
| 119 void RulesRegistryWithCache::OnExtensionUnloaded( | 137 void RulesRegistryWithCache::OnExtensionUnloaded( |
| 120 const std::string& extension_id) { | 138 const std::string& extension_id) { |
| 121 DCHECK(content::BrowserThread::CurrentlyOn(GetOwnerThread())); | 139 DCHECK(content::BrowserThread::CurrentlyOn(GetOwnerThread())); |
| 122 std::string error = RemoveAllRules(extension_id); | 140 std::string error = RemoveAllRules(extension_id); |
| 123 if (!error.empty()) | 141 if (!error.empty()) |
| 124 LOG(ERROR) << error; | 142 LOG(ERROR) << error; |
| 125 } | 143 } |
| 126 | 144 |
| 145 void RulesRegistryWithCache::NotifyRulesChanged( |
| 146 const std::string& extension_id) { |
| 147 DCHECK(content::BrowserThread::CurrentlyOn(GetOwnerThread())); |
| 148 |
| 149 if (delegate_.get()) |
| 150 delegate_->OnRulesChanged(this, extension_id); |
| 151 } |
| 152 |
| 127 RulesRegistryWithCache::~RulesRegistryWithCache() {} | 153 RulesRegistryWithCache::~RulesRegistryWithCache() {} |
| 128 | 154 |
| 129 } // namespace extensions | 155 } // namespace extensions |
| OLD | NEW |