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