| 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/values.h" | 7 #include "base/values.h" |
| 8 #include "chrome/browser/extensions/api/declarative/rules_registry.h" |
| 9 #include "chrome/browser/extensions/api/declarative/rules_registry_service.h" |
| 10 #include "chrome/browser/extensions/extension_service.h" |
| 11 #include "chrome/browser/profiles/profile.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 std::string event_name; |
| 17 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); |
| 13 | 18 |
| 14 ListValue* rules_list = NULL; | 19 ListValue* rules_list = NULL; |
| 15 EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &rules_list)); | 20 EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &rules_list)); |
| 16 | 21 |
| 17 // TODO(battre): Generate unique IDs and priorities here. | 22 std::vector<DictionaryValue*> rules; |
| 23 for (ListValue::iterator i = rules_list->begin(); |
| 24 i != rules_list->end(); |
| 25 ++i) { |
| 26 DictionaryValue* rule; |
| 27 EXTENSION_FUNCTION_VALIDATE((*i)->GetAsDictionary(&rule)); |
| 28 rules.push_back(rule); |
| 29 } |
| 30 |
| 31 RulesRegistryService* rules_registry = |
| 32 profile()->GetExtensionService()->GetRulesRegistryService(); |
| 33 |
| 34 if (!rules_registry->AddRules(event_name, extension_id(), rules, &error_)) |
| 35 return false; |
| 18 | 36 |
| 19 result_.reset(rules_list->DeepCopy()); | 37 result_.reset(rules_list->DeepCopy()); |
| 20 return true; | 38 return true; |
| 21 } | 39 } |
| 22 | 40 |
| 23 bool RemoveRulesFunction::RunImpl() { | 41 bool RemoveRulesFunction::RunImpl() { |
| 24 // LOG(ERROR) << "RemoveRulesFunction called"; | 42 std::string event_name; |
| 43 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); |
| 44 |
| 45 ListValue* rule_identifiers_list = NULL; |
| 46 EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &rule_identifiers_list)); |
| 47 |
| 48 std::vector<std::string> rule_identifiers; |
| 49 for (ListValue::iterator i = rule_identifiers_list->begin(); |
| 50 i != rule_identifiers_list->end(); |
| 51 ++i) { |
| 52 std::string rule_id; |
| 53 EXTENSION_FUNCTION_VALIDATE((*i)->GetAsString(&rule_id)); |
| 54 rule_identifiers.push_back(rule_id); |
| 55 } |
| 56 |
| 57 RulesRegistryService* rules_registry = |
| 58 profile()->GetExtensionService()->GetRulesRegistryService(); |
| 59 |
| 60 if (!rules_registry->RemoveRules(event_name, extension_id(), rule_identifiers, |
| 61 &error_)) |
| 62 return false; |
| 63 |
| 25 return true; | 64 return true; |
| 26 } | 65 } |
| 27 | 66 |
| 28 bool GetRulesFunction::RunImpl() { | 67 bool GetRulesFunction::RunImpl() { |
| 29 // LOG(ERROR) << "GetRulesFunction called"; | 68 std::string event_name; |
| 30 result_.reset(new ListValue()); | 69 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); |
| 70 |
| 71 ListValue* rule_identifiers_list = NULL; |
| 72 EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &rule_identifiers_list)); |
| 73 |
| 74 std::vector<std::string> rule_identifiers; |
| 75 for (ListValue::iterator i = rule_identifiers_list->begin(); |
| 76 i != rule_identifiers_list->end(); |
| 77 ++i) { |
| 78 std::string rule_id; |
| 79 EXTENSION_FUNCTION_VALIDATE((*i)->GetAsString(&rule_id)); |
| 80 rule_identifiers.push_back(rule_id); |
| 81 } |
| 82 |
| 83 RulesRegistryService* rules_registry = |
| 84 profile()->GetExtensionService()->GetRulesRegistryService(); |
| 85 |
| 86 typedef std::vector<DictionaryValue*> RulesList; |
| 87 |
| 88 RulesList rules; |
| 89 rules_registry->GetRules( |
| 90 event_name, extension_id(), rule_identifiers, &rules); |
| 91 |
| 92 scoped_ptr<ListValue> result(new ListValue); |
| 93 for (RulesList::iterator i = rules.begin(); i != rules.end(); ++i) |
| 94 result->Append(*i); |
| 95 |
| 96 result_.reset(result.release()); |
| 31 return true; | 97 return true; |
| 32 } | 98 } |
| 33 | 99 |
| 34 } // namespace extensions | 100 } // namespace extensions |
| OLD | NEW |