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