| 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/declarative_api_constants.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/extension_service.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 13 |
| 14 namespace keys = extensions::declarative_api_constants; |
| 15 |
| 16 namespace { |
| 17 |
| 18 // Adds all entries from |list| to |out|. Assumes that all entries of |list| |
| 19 // are strings. Returns true if successful. |
| 20 bool AddAllStringValues(ListValue* list, std::vector<std::string>* out) { |
| 21 for (ListValue::iterator i = list->begin(); i != list->end(); ++i) { |
| 22 std::string value; |
| 23 if (!(*i)->GetAsString(&value)) |
| 24 return false; |
| 25 out->push_back(value); |
| 26 } |
| 27 return true; |
| 28 } |
| 29 |
| 30 } // namespace |
| 8 | 31 |
| 9 namespace extensions { | 32 namespace extensions { |
| 10 | 33 |
| 11 bool AddRulesFunction::RunImpl() { | 34 bool AddRulesFunction::RunImpl() { |
| 12 // LOG(ERROR) << "AddRulesFunction called"; | 35 std::string event_name; |
| 36 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); |
| 13 | 37 |
| 14 ListValue* rules_list = NULL; | 38 ListValue* rules_list = NULL; |
| 15 EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &rules_list)); | 39 EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &rules_list)); |
| 16 | 40 |
| 17 // TODO(battre): Generate unique IDs and priorities here. | 41 std::vector<DictionaryValue*> rules; |
| 42 for (ListValue::iterator i = rules_list->begin(); |
| 43 i != rules_list->end(); |
| 44 ++i) { |
| 45 DictionaryValue* rule = NULL; |
| 46 EXTENSION_FUNCTION_VALIDATE((*i)->GetAsDictionary(&rule)); |
| 47 rules.push_back(rule); |
| 48 } |
| 49 |
| 50 RulesRegistryService* rules_registry_service = |
| 51 profile()->GetExtensionService()->GetRulesRegistryService(); |
| 52 RulesRegistry* rules_registry = |
| 53 rules_registry_service->GetRulesRegistry(event_name); |
| 54 if (!rules_registry) { |
| 55 error_ = keys::kInvalidEventName; |
| 56 return false; |
| 57 } |
| 58 |
| 59 if (!rules_registry->AddRules(extension_id(), rules, &error_)) |
| 60 return false; |
| 18 | 61 |
| 19 result_.reset(rules_list->DeepCopy()); | 62 result_.reset(rules_list->DeepCopy()); |
| 20 return true; | 63 return true; |
| 21 } | 64 } |
| 22 | 65 |
| 23 bool RemoveRulesFunction::RunImpl() { | 66 bool RemoveRulesFunction::RunImpl() { |
| 24 // LOG(ERROR) << "RemoveRulesFunction called"; | 67 std::string event_name; |
| 68 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); |
| 69 |
| 70 Value* rule_identifiers = NULL; |
| 71 EXTENSION_FUNCTION_VALIDATE(args_->Get(1, &rule_identifiers)); |
| 72 |
| 73 RulesRegistryService* rules_registry_service = |
| 74 profile()->GetExtensionService()->GetRulesRegistryService(); |
| 75 RulesRegistry* rules_registry = |
| 76 rules_registry_service->GetRulesRegistry(event_name); |
| 77 if (!rules_registry) { |
| 78 error_ = keys::kInvalidEventName; |
| 79 return false; |
| 80 } |
| 81 |
| 82 bool success = false; |
| 83 switch (rule_identifiers->GetType()) { |
| 84 case Value::TYPE_NULL: |
| 85 success = rules_registry->RemoveAllRules(extension_id(), &error_); |
| 86 break; |
| 87 case Value::TYPE_LIST: { |
| 88 std::vector<std::string> rule_identifiers_list; |
| 89 EXTENSION_FUNCTION_VALIDATE( |
| 90 AddAllStringValues(static_cast<ListValue*>(rule_identifiers), |
| 91 &rule_identifiers_list)); |
| 92 success = rules_registry->RemoveRules(extension_id(), |
| 93 rule_identifiers_list, &error_); |
| 94 break; |
| 95 } |
| 96 default: |
| 97 error_ = keys::kInvalidDatatype; |
| 98 break; |
| 99 } |
| 100 return success; |
| 101 } |
| 102 |
| 103 bool GetRulesFunction::RunImpl() { |
| 104 std::string event_name; |
| 105 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); |
| 106 |
| 107 Value* rule_identifiers = NULL; |
| 108 EXTENSION_FUNCTION_VALIDATE(args_->Get(1, &rule_identifiers)); |
| 109 |
| 110 RulesRegistryService* rules_registry_service = |
| 111 profile()->GetExtensionService()->GetRulesRegistryService(); |
| 112 RulesRegistry* rules_registry = |
| 113 rules_registry_service->GetRulesRegistry(event_name); |
| 114 if (!rules_registry) { |
| 115 error_ = keys::kInvalidEventName; |
| 116 return false; |
| 117 } |
| 118 |
| 119 std::vector<DictionaryValue*> rules; |
| 120 switch (rule_identifiers->GetType()) { |
| 121 case Value::TYPE_NULL: |
| 122 rules_registry->GetAllRules(extension_id(), &rules); |
| 123 break; |
| 124 case Value::TYPE_LIST: { |
| 125 std::vector<std::string> rule_identifiers_list; |
| 126 EXTENSION_FUNCTION_VALIDATE( |
| 127 AddAllStringValues(static_cast<ListValue*>(rule_identifiers), |
| 128 &rule_identifiers_list)); |
| 129 rules_registry->GetRules(extension_id(), rule_identifiers_list, &rules); |
| 130 break; |
| 131 } |
| 132 default: |
| 133 error_ = keys::kInvalidDatatype; |
| 134 break; |
| 135 } |
| 136 |
| 137 scoped_ptr<ListValue> result(new ListValue); |
| 138 for (std::vector<DictionaryValue*>::iterator i = rules.begin(); |
| 139 i != rules.end(); ++i) |
| 140 result->Append(*i); |
| 141 |
| 142 result_.reset(result.release()); |
| 143 |
| 25 return true; | 144 return true; |
| 26 } | 145 } |
| 27 | 146 |
| 28 bool GetRulesFunction::RunImpl() { | |
| 29 // LOG(ERROR) << "GetRulesFunction called"; | |
| 30 result_.reset(new ListValue()); | |
| 31 return true; | |
| 32 } | |
| 33 | |
| 34 } // namespace extensions | 147 } // namespace extensions |
| OLD | NEW |