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