| 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/initializing_rules_registry.
h" | 5 #include "chrome/browser/extensions/api/declarative/initializing_rules_registry.
h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 | 10 |
| 11 namespace { | 11 namespace { |
| 12 std::string ToId(int identifier) { | 12 std::string ToId(int identifier) { |
| 13 return StringPrintf("_%d_", identifier); | 13 return StringPrintf("_%d_", identifier); |
| 14 } | 14 } |
| 15 } // namespace | 15 } // namespace |
| 16 | 16 |
| 17 namespace extensions { | 17 namespace extensions { |
| 18 | 18 |
| 19 InitializingRulesRegistry::InitializingRulesRegistry( | 19 InitializingRulesRegistry::InitializingRulesRegistry( |
| 20 scoped_refptr<RulesRegistry> delegate) | 20 scoped_refptr<RulesRegistry> delegate) |
| 21 : delegate_(delegate), | 21 : delegate_(delegate), |
| 22 last_generated_rule_identifier_id_(0) { | 22 last_generated_rule_identifier_id_(0) { |
| 23 } | 23 } |
| 24 | 24 |
| 25 InitializingRulesRegistry::~InitializingRulesRegistry() {} | |
| 26 | |
| 27 std::string InitializingRulesRegistry::AddRules( | 25 std::string InitializingRulesRegistry::AddRules( |
| 28 const std::string& extension_id, | 26 const std::string& extension_id, |
| 29 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) { | 27 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) { |
| 30 std::string error = CheckAndFillInOptionalRules(extension_id, rules); | 28 std::string error = CheckAndFillInOptionalRules(extension_id, rules); |
| 31 if (!error.empty()) | 29 if (!error.empty()) |
| 32 return error; | 30 return error; |
| 33 FillInOptionalPriorities(rules); | 31 FillInOptionalPriorities(rules); |
| 34 return delegate_->AddRules(extension_id, rules); | 32 return delegate_->AddRules(extension_id, rules); |
| 35 } | 33 } |
| 36 | 34 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 | 66 |
| 69 void InitializingRulesRegistry::OnExtensionUnloaded( | 67 void InitializingRulesRegistry::OnExtensionUnloaded( |
| 70 const std::string& extension_id) { | 68 const std::string& extension_id) { |
| 71 delegate_->OnExtensionUnloaded(extension_id); | 69 delegate_->OnExtensionUnloaded(extension_id); |
| 72 } | 70 } |
| 73 | 71 |
| 74 content::BrowserThread::ID InitializingRulesRegistry::GetOwnerThread() const { | 72 content::BrowserThread::ID InitializingRulesRegistry::GetOwnerThread() const { |
| 75 return delegate_->GetOwnerThread(); | 73 return delegate_->GetOwnerThread(); |
| 76 } | 74 } |
| 77 | 75 |
| 76 InitializingRulesRegistry::~InitializingRulesRegistry() {} |
| 77 |
| 78 bool InitializingRulesRegistry::IsUniqueId( | 78 bool InitializingRulesRegistry::IsUniqueId( |
| 79 const std::string& extension_id, | 79 const std::string& extension_id, |
| 80 const std::string& rule_id) const { | 80 const std::string& rule_id) const { |
| 81 RuleIdentifiersMap::const_iterator identifiers = | 81 RuleIdentifiersMap::const_iterator identifiers = |
| 82 used_rule_identifiers_.find(extension_id); | 82 used_rule_identifiers_.find(extension_id); |
| 83 if (identifiers == used_rule_identifiers_.end()) | 83 if (identifiers == used_rule_identifiers_.end()) |
| 84 return true; | 84 return true; |
| 85 return identifiers->second.find(rule_id) == identifiers->second.end(); | 85 return identifiers->second.find(rule_id) == identifiers->second.end(); |
| 86 } | 86 } |
| 87 | 87 |
| 88 std::string InitializingRulesRegistry::GenerateUniqueId( | 88 std::string InitializingRulesRegistry::GenerateUniqueId( |
| 89 const std::string& extension_id) { | 89 const std::string& extension_id) { |
| 90 while (!IsUniqueId(extension_id, ToId(last_generated_rule_identifier_id_))) | 90 while (!IsUniqueId(extension_id, ToId(last_generated_rule_identifier_id_))) |
| 91 ++last_generated_rule_identifier_id_; | 91 ++last_generated_rule_identifier_id_; |
| 92 return ToId(last_generated_rule_identifier_id_); | 92 return ToId(last_generated_rule_identifier_id_); |
| 93 } | 93 } |
| 94 | 94 |
| 95 void InitializingRulesRegistry::RemoveUsedRuleIdentifiers( | |
| 96 const std::string& extension_id, | |
| 97 const std::vector<std::string>& identifiers) { | |
| 98 std::vector<std::string>::const_iterator i; | |
| 99 for (i = identifiers.begin(); i != identifiers.end(); ++i) | |
| 100 used_rule_identifiers_[extension_id].erase(*i); | |
| 101 } | |
| 102 | |
| 103 void InitializingRulesRegistry::RemoveAllUsedRuleIdentifiers( | |
| 104 const std::string& extension_id) { | |
| 105 used_rule_identifiers_.erase(extension_id); | |
| 106 } | |
| 107 | |
| 108 std::string InitializingRulesRegistry::CheckAndFillInOptionalRules( | 95 std::string InitializingRulesRegistry::CheckAndFillInOptionalRules( |
| 109 const std::string& extension_id, | 96 const std::string& extension_id, |
| 110 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) { | 97 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) { |
| 111 // IDs we have inserted, in case we need to rollback this operation. | 98 // IDs we have inserted, in case we need to rollback this operation. |
| 112 std::vector<std::string> rollback_log; | 99 std::vector<std::string> rollback_log; |
| 113 | 100 |
| 114 // First we insert all rules with existing identifier, so that generated | 101 // First we insert all rules with existing identifier, so that generated |
| 115 // identifiers cannot collide with identifiers passed by the caller. | 102 // identifiers cannot collide with identifiers passed by the caller. |
| 116 for (std::vector<linked_ptr<RulesRegistry::Rule> >::const_iterator i = | 103 for (std::vector<linked_ptr<RulesRegistry::Rule> >::const_iterator i = |
| 117 rules.begin(); i != rules.end(); ++i) { | 104 rules.begin(); i != rules.end(); ++i) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 140 | 127 |
| 141 void InitializingRulesRegistry::FillInOptionalPriorities( | 128 void InitializingRulesRegistry::FillInOptionalPriorities( |
| 142 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) { | 129 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) { |
| 143 std::vector<linked_ptr<RulesRegistry::Rule> >::const_iterator i; | 130 std::vector<linked_ptr<RulesRegistry::Rule> >::const_iterator i; |
| 144 for (i = rules.begin(); i != rules.end(); ++i) { | 131 for (i = rules.begin(); i != rules.end(); ++i) { |
| 145 if (!(*i)->priority.get()) | 132 if (!(*i)->priority.get()) |
| 146 (*i)->priority.reset(new int(DEFAULT_PRIORITY)); | 133 (*i)->priority.reset(new int(DEFAULT_PRIORITY)); |
| 147 } | 134 } |
| 148 } | 135 } |
| 149 | 136 |
| 137 void InitializingRulesRegistry::RemoveUsedRuleIdentifiers( |
| 138 const std::string& extension_id, |
| 139 const std::vector<std::string>& identifiers) { |
| 140 std::vector<std::string>::const_iterator i; |
| 141 for (i = identifiers.begin(); i != identifiers.end(); ++i) |
| 142 used_rule_identifiers_[extension_id].erase(*i); |
| 143 } |
| 144 |
| 145 void InitializingRulesRegistry::RemoveAllUsedRuleIdentifiers( |
| 146 const std::string& extension_id) { |
| 147 used_rule_identifiers_.erase(extension_id); |
| 148 } |
| 149 |
| 150 } // namespace extensions | 150 } // namespace extensions |
| OLD | NEW |