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