Chromium Code Reviews| 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; | |
|
not at google - send to devlin
2012/02/07 03:25:10
= NULL
battre
2012/02/07 18:45:33
Done.
| |
| 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 | |
|
not at google - send to devlin
2012/02/07 03:25:10
my biased opinion based on how similar code in the
battre
2012/02/07 18:45:33
Done.
| |
| 45 std::vector<std::string> rule_identifiers; | |
| 46 | |
| 47 Value* rule_identifiers_list_value = NULL; | |
| 48 EXTENSION_FUNCTION_VALIDATE(args_->Get(1, &rule_identifiers_list_value)); | |
| 49 | |
| 50 ListValue* rule_identifiers_list = NULL; | |
| 51 if (!rule_identifiers_list_value->IsType(Value::TYPE_NULL)) { | |
| 52 EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &rule_identifiers_list)); | |
|
not at google - send to devlin
2012/02/07 03:25:10
This could fail if the extension does a call like
not at google - send to devlin
2012/02/07 05:08:01
(this is a bit moot given my other comments anyway
| |
| 53 | |
| 54 for (ListValue::iterator i = rule_identifiers_list->begin(); | |
| 55 i != rule_identifiers_list->end(); | |
| 56 ++i) { | |
| 57 std::string rule_id; | |
| 58 EXTENSION_FUNCTION_VALIDATE((*i)->GetAsString(&rule_id)); | |
| 59 rule_identifiers.push_back(rule_id); | |
| 60 } | |
| 61 | |
| 62 // If the extension passed an empty list, this means no rules can be | |
| 63 // contained in the list and we bail out. | |
| 64 if (rule_identifiers.empty()) | |
| 65 return true; | |
|
not at google - send to devlin
2012/02/07 03:25:10
This is still a confusing API, where an empty list
battre
2012/02/07 18:45:33
Done.
| |
| 66 } | |
| 67 | |
| 68 RulesRegistryService* rules_registry = | |
| 69 profile()->GetExtensionService()->GetRulesRegistryService(); | |
| 70 | |
| 71 return rules_registry->RemoveRules(event_name, extension_id(), | |
| 72 rule_identifiers, &error_); | |
| 73 } | |
| 74 | |
| 75 bool GetRulesFunction::RunImpl() { | |
| 76 std::string event_name; | |
| 77 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); | |
| 78 | |
|
not at google - send to devlin
2012/02/07 03:25:10
Same comment here as in RemoveRulesFunction, both
battre
2012/02/07 18:45:33
Done.
| |
| 79 std::vector<std::string> rule_identifiers; | |
| 80 | |
| 81 Value* rule_identifiers_list_value = NULL; | |
| 82 EXTENSION_FUNCTION_VALIDATE(args_->Get(1, &rule_identifiers_list_value)); | |
| 83 | |
| 84 ListValue* rule_identifiers_list = NULL; | |
| 85 if (!rule_identifiers_list_value->IsType(Value::TYPE_NULL)) { | |
| 86 EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &rule_identifiers_list)); | |
| 87 | |
| 88 for (ListValue::iterator i = rule_identifiers_list->begin(); | |
| 89 i != rule_identifiers_list->end(); | |
| 90 ++i) { | |
| 91 std::string rule_id; | |
| 92 EXTENSION_FUNCTION_VALIDATE((*i)->GetAsString(&rule_id)); | |
| 93 rule_identifiers.push_back(rule_id); | |
| 94 } | |
| 95 | |
| 96 // If the extension passed an empty list, this means no rules can be | |
| 97 // contained in the list and we bail out. | |
| 98 if (rule_identifiers.empty()) { | |
| 99 result_.reset(new ListValue); | |
| 100 return true; | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 RulesRegistryService* rules_registry = | |
| 105 profile()->GetExtensionService()->GetRulesRegistryService(); | |
| 106 | |
| 107 typedef std::vector<DictionaryValue*> RulesList; | |
|
not at google - send to devlin
2012/02/07 03:25:10
IMO typedef unnecessary.
battre
2012/02/07 18:45:33
Done.
| |
| 108 | |
| 109 RulesList rules; | |
| 110 rules_registry->GetRules( | |
| 111 event_name, extension_id(), rule_identifiers, &rules); | |
| 112 | |
| 113 scoped_ptr<ListValue> result(new ListValue); | |
| 114 for (RulesList::iterator i = rules.begin(); i != rules.end(); ++i) | |
| 115 result->Append(*i); | |
| 116 | |
| 117 result_.reset(result.release()); | |
| 25 return true; | 118 return true; |
| 26 } | 119 } |
| 27 | 120 |
| 28 bool GetRulesFunction::RunImpl() { | |
| 29 // LOG(ERROR) << "GetRulesFunction called"; | |
| 30 result_.reset(new ListValue()); | |
| 31 return true; | |
| 32 } | |
| 33 | |
| 34 } // namespace extensions | 121 } // namespace extensions |
| OLD | NEW |