| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/api/declarative/initializing_rules_registry.
h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/stringprintf.h" |
| 9 #include "chrome/browser/extensions/api/declarative/declarative_api_constants.h" |
| 10 |
| 11 namespace keys = extensions::declarative_api_constants; |
| 12 |
| 13 namespace { |
| 14 std::string ToId(int identifier) { |
| 15 return StringPrintf("_%d_", identifier); |
| 16 } |
| 17 } // namespace |
| 18 |
| 19 namespace extensions { |
| 20 |
| 21 InitializingRulesRegistry::InitializingRulesRegistry( |
| 22 scoped_ptr<RulesRegistry> delegate) |
| 23 : delegate_(delegate.Pass()), |
| 24 last_generated_rule_identifier_id_(0) { |
| 25 } |
| 26 |
| 27 InitializingRulesRegistry::~InitializingRulesRegistry() {} |
| 28 |
| 29 bool InitializingRulesRegistry::AddRules( |
| 30 const std::string& extension_id, |
| 31 const std::vector<DictionaryValue*>& rules, |
| 32 std::string* error) { |
| 33 if (!CheckAndFillInOptionalRules(extension_id, rules, error)) |
| 34 return false; |
| 35 FillInOptionalPriorities(rules); |
| 36 return delegate_->AddRules(extension_id, rules, error); |
| 37 } |
| 38 |
| 39 bool InitializingRulesRegistry::RemoveRules( |
| 40 const std::string& extension_id, |
| 41 const std::vector<std::string>& rule_identifiers, |
| 42 std::string* error) { |
| 43 if (!delegate_->RemoveRules(extension_id, rule_identifiers, error)) |
| 44 return false; |
| 45 RemoveUsedRuleIdentifiers(extension_id, rule_identifiers); |
| 46 return true; |
| 47 } |
| 48 |
| 49 bool InitializingRulesRegistry::RemoveAllRules( |
| 50 const std::string& extension_id, |
| 51 std::string* error) { |
| 52 if (!delegate_->RemoveAllRules(extension_id, error)) |
| 53 return false; |
| 54 RemoveAllUsedRuleIdentifiers(extension_id); |
| 55 return true; |
| 56 } |
| 57 |
| 58 void InitializingRulesRegistry::GetRules( |
| 59 const std::string& extension_id, |
| 60 const std::vector<std::string>& rule_identifiers, |
| 61 std::vector<DictionaryValue*>* out) { |
| 62 delegate_->GetRules(extension_id, rule_identifiers, out); |
| 63 } |
| 64 |
| 65 void InitializingRulesRegistry::GetAllRules( |
| 66 const std::string& extension_id, |
| 67 std::vector<DictionaryValue*>* out) { |
| 68 delegate_->GetAllRules(extension_id, out); |
| 69 } |
| 70 |
| 71 void InitializingRulesRegistry::OnExtensionUnloaded( |
| 72 const std::string& extension_id) { |
| 73 delegate_->OnExtensionUnloaded(extension_id); |
| 74 } |
| 75 |
| 76 bool InitializingRulesRegistry::IsUniqueId( |
| 77 const std::string& extension_id, |
| 78 const std::string& rule_id) const { |
| 79 RuleIdentifiersMap::const_iterator identifiers = |
| 80 used_rule_identifiers_.find(extension_id); |
| 81 if (identifiers == used_rule_identifiers_.end()) |
| 82 return true; |
| 83 return identifiers->second.find(rule_id) == identifiers->second.end(); |
| 84 } |
| 85 |
| 86 std::string InitializingRulesRegistry::GenerateUniqueId( |
| 87 std::string extension_id) { |
| 88 while (!IsUniqueId(extension_id, ToId(last_generated_rule_identifier_id_))) |
| 89 ++last_generated_rule_identifier_id_; |
| 90 std::string new_id = ToId(last_generated_rule_identifier_id_); |
| 91 used_rule_identifiers_[extension_id].insert(new_id); |
| 92 return new_id; |
| 93 } |
| 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 bool InitializingRulesRegistry::CheckAndFillInOptionalRules( |
| 109 const std::string& extension_id, |
| 110 const std::vector<DictionaryValue*>& rules, |
| 111 std::string* error) { |
| 112 // IDs we have inserted, in case we need to rollback this operation. |
| 113 std::vector<std::string> rollback_log; |
| 114 |
| 115 // First we insert all rules with existing identifier, so that generated |
| 116 // identifiers cannot collide with identifiers passed by the caller. |
| 117 for (std::vector<DictionaryValue*>::const_iterator i = |
| 118 rules.begin(); i != rules.end(); ++i) { |
| 119 DictionaryValue* rule = *i; |
| 120 if (rule->HasKey(keys::kId)) { |
| 121 std::string id; |
| 122 CHECK(rule->GetString(keys::kId, &id)); |
| 123 if (!IsUniqueId(extension_id, id)) { |
| 124 *error = "Id " + id + " was used multiple times."; |
| 125 RemoveUsedRuleIdentifiers(extension_id, rollback_log); |
| 126 return false; |
| 127 } |
| 128 used_rule_identifiers_[extension_id].insert(id); |
| 129 } |
| 130 } |
| 131 // Now we generate IDs in case they were not specificed in the rules. This |
| 132 // cannot fail so we do not need to keep track of a rollback log. |
| 133 for (std::vector<DictionaryValue*>::const_iterator i = |
| 134 rules.begin(); i != rules.end(); ++i) { |
| 135 DictionaryValue* rule = *i; |
| 136 if (!rule->HasKey(keys::kId)) |
| 137 rule->SetString(keys::kId, GenerateUniqueId(extension_id)); |
| 138 } |
| 139 return true; |
| 140 } |
| 141 |
| 142 void InitializingRulesRegistry::FillInOptionalPriorities( |
| 143 const std::vector<DictionaryValue*>& rules) { |
| 144 std::vector<DictionaryValue*>::const_iterator i; |
| 145 for (i = rules.begin(); i != rules.end(); ++i) { |
| 146 DictionaryValue* rule = *i; |
| 147 if (!rule->HasKey(keys::kPriority)) |
| 148 rule->SetInteger(keys::kPriority, 100); |
| 149 } |
| 150 } |
| 151 |
| 152 } // namespace extensions |
| OLD | NEW |