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