| 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/declarative_api.h" | 5 #include "chrome/browser/extensions/api/declarative/declarative_api.h" |
| 6 | 6 |
| 7 #include "base/bind.h" |
| 7 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "chrome/browser/extensions/api/declarative/rules_registry.h" |
| 10 #include "chrome/browser/extensions/api/declarative/rules_registry_service.h" |
| 11 #include "chrome/browser/extensions/api/declarative/rules_registry_service_facto
ry.h" |
| 8 | 12 |
| 9 namespace extensions { | 13 namespace extensions { |
| 10 | 14 |
| 11 bool AddRulesFunction::RunImpl() { | 15 bool AddRulesFunction::RunImpl() { |
| 12 // LOG(ERROR) << "AddRulesFunction called"; | 16 // The name of the event object which had the addRules() function that was |
| 17 // called. |
| 18 std::string event_name; |
| 19 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); |
| 20 |
| 21 ListValue* rules_list; |
| 22 EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &rules_list)); |
| 23 |
| 24 std::vector<DictionaryValue*> rules; |
| 25 for (ListValue::iterator i = rules_list->begin(); |
| 26 i != rules_list->end(); |
| 27 ++i) { |
| 28 DictionaryValue* rule; |
| 29 EXTENSION_FUNCTION_VALIDATE((*i)->GetAsDictionary(&rule)); |
| 30 rules.push_back(rule); |
| 31 } |
| 32 |
| 33 RulesRegistryService* rules_registry = |
| 34 RulesRegistryServiceFactory::GetInstance()->GetForProfile(profile_); |
| 35 |
| 36 EXTENSION_FUNCTION_VALIDATE( |
| 37 rules_registry->AddRules( |
| 38 event_name, |
| 39 extension_id(), |
| 40 rules, |
| 41 base::Bind(&AddRulesFunction::SetError, this))); |
| 13 | 42 |
| 14 // We always return an empty list here. The binding of return values | 43 // We always return an empty list here. The binding of return values |
| 15 // is handled in JavaScript by event.js. | 44 // is handled in JavaScript by event.js. |
| 16 result_.reset(new ListValue()); | 45 result_.reset(new ListValue()); |
| 17 return true; | 46 return true; |
| 18 } | 47 } |
| 19 | 48 |
| 20 bool RemoveRulesFunction::RunImpl() { | 49 bool RemoveRulesFunction::RunImpl() { |
| 21 // LOG(ERROR) << "RemoveRulesFunction called"; | 50 std::string event_name; |
| 51 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); |
| 52 |
| 53 ListValue* rule_identifiers_list; |
| 54 EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &rule_identifiers_list)); |
| 55 |
| 56 std::vector<std::string> rule_identifiers; |
| 57 for (ListValue::iterator i = rule_identifiers_list->begin(); |
| 58 i != rule_identifiers_list->end(); |
| 59 ++i) { |
| 60 std::string rule_id; |
| 61 EXTENSION_FUNCTION_VALIDATE((*i)->GetAsString(&rule_id)); |
| 62 rule_identifiers.push_back(rule_id); |
| 63 } |
| 64 |
| 65 RulesRegistryService* rules_registry = |
| 66 RulesRegistryServiceFactory::GetInstance()->GetForProfile(profile_); |
| 67 |
| 68 EXTENSION_FUNCTION_VALIDATE( |
| 69 rules_registry->RemoveRules( |
| 70 event_name, |
| 71 extension_id(), |
| 72 rule_identifiers, |
| 73 base::Bind(&AddRulesFunction::SetError, this))); |
| 74 |
| 22 return true; | 75 return true; |
| 23 } | 76 } |
| 24 | 77 |
| 25 bool GetRulesFunction::RunImpl() { | 78 bool GetRulesFunction::RunImpl() { |
| 26 // LOG(ERROR) << "GetRulesFunction called"; | 79 std::string event_name; |
| 27 result_.reset(new ListValue()); | 80 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); |
| 81 |
| 82 ListValue* rule_identifiers_list; |
| 83 EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &rule_identifiers_list)); |
| 84 |
| 85 std::vector<std::string> rule_identifiers; |
| 86 for (ListValue::iterator i = rule_identifiers_list->begin(); |
| 87 i != rule_identifiers_list->end(); |
| 88 ++i) { |
| 89 std::string rule_id; |
| 90 EXTENSION_FUNCTION_VALIDATE((*i)->GetAsString(&rule_id)); |
| 91 rule_identifiers.push_back(rule_id); |
| 92 } |
| 93 |
| 94 RulesRegistryService* rules_registry = |
| 95 RulesRegistryServiceFactory::GetInstance()->GetForProfile(profile_); |
| 96 |
| 97 typedef std::vector<DictionaryValue*> RulesList; |
| 98 |
| 99 RulesList rules; |
| 100 rules_registry->GetRules( |
| 101 event_name, extension_id(), rule_identifiers, &rules); |
| 102 |
| 103 scoped_ptr<ListValue> result(new ListValue); |
| 104 for (RulesList::iterator i = rules.begin(); i != rules.end(); ++i) |
| 105 result->Append(*i); |
| 106 |
| 107 result_.reset(result.release()); |
| 28 return true; | 108 return true; |
| 29 } | 109 } |
| 30 | 110 |
| 31 } // namespace extensions | 111 } // namespace extensions |
| OLD | NEW |