| 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/test_rules_registry.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/stl_util.h" |
| 11 #include "chrome/browser/extensions/api/declarative/declarative_api_constants.h" |
| 12 |
| 13 namespace keys = extensions::declarative_api_constants; |
| 14 |
| 15 namespace { |
| 16 |
| 17 std::string GetRuleId(DictionaryValue* rule) { |
| 18 std::string rule_id; |
| 19 CHECK(rule->GetString(keys::kId, &rule_id)); |
| 20 return rule_id; |
| 21 } |
| 22 |
| 23 } // namespace |
| 24 |
| 25 namespace extensions { |
| 26 |
| 27 TestRulesRegistry::TestRulesRegistry() { |
| 28 } |
| 29 |
| 30 TestRulesRegistry::~TestRulesRegistry() { |
| 31 STLDeleteValues(&rules_); |
| 32 } |
| 33 |
| 34 bool TestRulesRegistry::AddRules( |
| 35 const std::string& extension_id, |
| 36 const std::vector<DictionaryValue*>& rules, |
| 37 std::string* error) { |
| 38 std::vector<DictionaryValue*>::const_iterator i; |
| 39 for (i = rules.begin(); i != rules.end(); ++i) { |
| 40 std::string rule_id = GetRuleId(*i); |
| 41 // TODO: relax this (check first and abort with returning false). |
| 42 CHECK(rules_.find(rule_id) == rules_.end()); |
| 43 rules_[rule_id] = (*i)->DeepCopy(); |
| 44 } |
| 45 return true; |
| 46 } |
| 47 |
| 48 bool TestRulesRegistry::RemoveRules( |
| 49 const std::string& extension_id, |
| 50 const std::vector<std::string>& rule_identifiers, |
| 51 std::string* error) { |
| 52 if (rule_identifiers.empty()) { |
| 53 STLDeleteValues(&rules_); |
| 54 } else { |
| 55 std::vector<std::string>::const_iterator i; |
| 56 for (i = rule_identifiers.begin(); i != rule_identifiers.end(); ++i) { |
| 57 std::map<std::string, DictionaryValue*>::iterator entry = rules_.find(*i); |
| 58 // TODO: relax this (check first and abort with returning false). |
| 59 CHECK(entry != rules_.end()); |
| 60 delete entry->second; |
| 61 rules_.erase(entry); |
| 62 } |
| 63 } |
| 64 return true; |
| 65 } |
| 66 |
| 67 void TestRulesRegistry::GetRules( |
| 68 const std::string& extension_id, |
| 69 const std::vector<std::string>& rule_identifiers, |
| 70 std::vector<DictionaryValue*>* out) { |
| 71 if (rule_identifiers.empty()) { |
| 72 std::map<std::string, DictionaryValue*>::const_iterator i; |
| 73 for (i = rules_.begin(); i != rules_.end(); ++i) |
| 74 out->push_back(i->second->DeepCopy()); |
| 75 } else { |
| 76 std::vector<std::string>::const_iterator i; |
| 77 for (i = rule_identifiers.begin(); i != rule_identifiers.end(); ++i) { |
| 78 std::map<std::string, DictionaryValue*>::iterator entry = rules_.find(*i); |
| 79 // TODO: relax this (check first and abort with returning false). |
| 80 CHECK(entry != rules_.end()); |
| 81 out->push_back(entry->second->DeepCopy()); |
| 82 } |
| 83 } |
| 84 } |
| 85 |
| 86 void TestRulesRegistry::OnExtensionUnloaded(const std::string& extension_id) { |
| 87 std::vector<std::string> no_rule_identifiers; |
| 88 std::string error; |
| 89 if (!RemoveRules(extension_id, no_rule_identifiers, &error)) |
| 90 LOG(ERROR) << error; |
| 91 } |
| 92 |
| 93 } // namespace extensions |
| OLD | NEW |