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