| 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 std::string InitializingRulesRegistry::AddRules( |
| 30 const std::string& extension_id, |
| 31 const std::vector<DictionaryValue*>& rules) { |
| 32 std::string error = CheckAndFillInOptionalRules(extension_id, rules); |
| 33 if (!error.empty()) |
| 34 return error; |
| 35 FillInOptionalPriorities(rules); |
| 36 return delegate_->AddRules(extension_id, rules); |
| 37 } |
| 38 |
| 39 std::string InitializingRulesRegistry::RemoveRules( |
| 40 const std::string& extension_id, |
| 41 const std::vector<std::string>& rule_identifiers) { |
| 42 std::string error = delegate_->RemoveRules(extension_id, rule_identifiers); |
| 43 if (!error.empty()) |
| 44 return error; |
| 45 RemoveUsedRuleIdentifiers(extension_id, rule_identifiers); |
| 46 return ""; |
| 47 } |
| 48 |
| 49 std::string InitializingRulesRegistry::RemoveAllRules( |
| 50 const std::string& extension_id) { |
| 51 std::string error = delegate_->RemoveAllRules(extension_id); |
| 52 if (!error.empty()) |
| 53 return error; |
| 54 RemoveAllUsedRuleIdentifiers(extension_id); |
| 55 return ""; |
| 56 } |
| 57 |
| 58 std::string InitializingRulesRegistry::GetRules( |
| 59 const std::string& extension_id, |
| 60 const std::vector<std::string>& rule_identifiers, |
| 61 std::vector<DictionaryValue*>* out) { |
| 62 return delegate_->GetRules(extension_id, rule_identifiers, out); |
| 63 } |
| 64 |
| 65 std::string InitializingRulesRegistry::GetAllRules( |
| 66 const std::string& extension_id, |
| 67 std::vector<DictionaryValue*>* out) { |
| 68 return 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 std::string InitializingRulesRegistry::CheckAndFillInOptionalRules( |
| 109 const std::string& extension_id, |
| 110 const std::vector<DictionaryValue*>& rules) { |
| 111 // IDs we have inserted, in case we need to rollback this operation. |
| 112 std::vector<std::string> rollback_log; |
| 113 |
| 114 // First we insert all rules with existing identifier, so that generated |
| 115 // identifiers cannot collide with identifiers passed by the caller. |
| 116 for (std::vector<DictionaryValue*>::const_iterator i = |
| 117 rules.begin(); i != rules.end(); ++i) { |
| 118 DictionaryValue* rule = *i; |
| 119 if (rule->HasKey(keys::kId)) { |
| 120 std::string id; |
| 121 CHECK(rule->GetString(keys::kId, &id)); |
| 122 if (!IsUniqueId(extension_id, id)) { |
| 123 RemoveUsedRuleIdentifiers(extension_id, rollback_log); |
| 124 return "Id " + id + " was used multiple times."; |
| 125 } |
| 126 used_rule_identifiers_[extension_id].insert(id); |
| 127 } |
| 128 } |
| 129 // Now we generate IDs in case they were not specificed in the rules. This |
| 130 // cannot fail so we do not need to keep track of a rollback log. |
| 131 for (std::vector<DictionaryValue*>::const_iterator i = |
| 132 rules.begin(); i != rules.end(); ++i) { |
| 133 DictionaryValue* rule = *i; |
| 134 if (!rule->HasKey(keys::kId)) |
| 135 rule->SetString(keys::kId, GenerateUniqueId(extension_id)); |
| 136 } |
| 137 return ""; |
| 138 } |
| 139 |
| 140 void InitializingRulesRegistry::FillInOptionalPriorities( |
| 141 const std::vector<DictionaryValue*>& rules) { |
| 142 std::vector<DictionaryValue*>::const_iterator i; |
| 143 for (i = rules.begin(); i != rules.end(); ++i) { |
| 144 DictionaryValue* rule = *i; |
| 145 if (!rule->HasKey(keys::kPriority)) |
| 146 rule->SetInteger(keys::kPriority, 100); |
| 147 } |
| 148 } |
| 149 |
| 150 } // namespace extensions |
| OLD | NEW |