| Index: chrome/browser/extensions/api/declarative/declarative_api.cc
|
| diff --git a/chrome/browser/extensions/api/declarative/declarative_api.cc b/chrome/browser/extensions/api/declarative/declarative_api.cc
|
| index e7fccc8586d5e24c67f2a997d350acb394f9ee46..99be5f3b08facebffbf5252352b719db316d6e5e 100644
|
| --- a/chrome/browser/extensions/api/declarative/declarative_api.cc
|
| +++ b/chrome/browser/extensions/api/declarative/declarative_api.cc
|
| @@ -4,14 +4,16 @@
|
|
|
| #include "chrome/browser/extensions/api/declarative/declarative_api.h"
|
|
|
| +#include "base/bind.h"
|
| +#include "base/bind_helpers.h"
|
| #include "base/values.h"
|
| -#include "chrome/browser/extensions/api/declarative/declarative_api_constants.h"
|
| -#include "chrome/browser/extensions/api/declarative/rules_registry.h"
|
| #include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
|
| #include "chrome/browser/extensions/extension_service.h"
|
| #include "chrome/browser/profiles/profile.h"
|
| +#include "chrome/common/extensions/api/experimental.declarative.h"
|
| +#include "content/public/browser/browser_thread.h"
|
|
|
| -namespace keys = extensions::declarative_api_constants;
|
| +using namespace extensions::api::experimental;
|
|
|
| namespace {
|
|
|
| @@ -31,120 +33,82 @@ bool AddAllStringValues(ListValue* list, std::vector<std::string>* out) {
|
|
|
| namespace extensions {
|
|
|
| -bool AddRulesFunction::RunImpl() {
|
| - std::string event_name;
|
| - EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
|
| +RulesFunction::RulesFunction() : rules_registry_(NULL) {}
|
|
|
| - ListValue* rules_list = NULL;
|
| - EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &rules_list));
|
| +RulesFunction::~RulesFunction() {}
|
|
|
| - std::vector<DictionaryValue*> rules;
|
| - for (ListValue::iterator i = rules_list->begin();
|
| - i != rules_list->end();
|
| - ++i) {
|
| - DictionaryValue* rule = NULL;
|
| - EXTENSION_FUNCTION_VALIDATE((*i)->GetAsDictionary(&rule));
|
| - rules.push_back(rule);
|
| - }
|
| +bool RulesFunction::RunImpl() {
|
| + std::string event_name;
|
| + EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
|
|
|
| RulesRegistryService* rules_registry_service =
|
| profile()->GetExtensionService()->GetRulesRegistryService();
|
| - RulesRegistry* rules_registry =
|
| - rules_registry_service->GetRulesRegistry(event_name);
|
| - if (!rules_registry) {
|
| - error_ = keys::kInvalidEventName;
|
| - return false;
|
| + rules_registry_ = rules_registry_service->GetRulesRegistry(event_name);
|
| + // Raw access to this function is not available to extensions, therefore
|
| + // there should never be a request for a nonexisting rules registry.
|
| + EXTENSION_FUNCTION_VALIDATE(rules_registry_);
|
| +
|
| + if (content::BrowserThread::CurrentlyOn(rules_registry_->GetOwnerThread())) {
|
| + RunImplOnCorrectThread();
|
| + SendResponseOnUIThread();
|
| + } else {
|
| + content::BrowserThread::PostTaskAndReply(
|
| + rules_registry_->GetOwnerThread(), FROM_HERE,
|
| + base::Bind(base::IgnoreResult(&RulesFunction::RunImplOnCorrectThread),
|
| + this),
|
| + base::Bind(&RulesFunction::SendResponseOnUIThread, this));
|
| }
|
|
|
| - error_ = rules_registry->AddRules(extension_id(), rules);
|
| - if (!error_.empty())
|
| - return false;
|
| -
|
| - result_.reset(rules_list->DeepCopy());
|
| return true;
|
| }
|
|
|
| -bool RemoveRulesFunction::RunImpl() {
|
| - std::string event_name;
|
| - EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
|
| +void RulesFunction::SendResponseOnUIThread() {
|
| + SendResponse(error_.empty());
|
| +}
|
|
|
| - Value* rule_identifiers = NULL;
|
| - EXTENSION_FUNCTION_VALIDATE(args_->Get(1, &rule_identifiers));
|
| +bool AddRulesFunction::RunImplOnCorrectThread() {
|
| + scoped_ptr<AddRules::Params> params(AddRules::Params::Create(*args_));
|
| + EXTENSION_FUNCTION_VALIDATE(params.get());
|
|
|
| - RulesRegistryService* rules_registry_service =
|
| - profile()->GetExtensionService()->GetRulesRegistryService();
|
| - RulesRegistry* rules_registry =
|
| - rules_registry_service->GetRulesRegistry(event_name);
|
| - if (!rules_registry) {
|
| - error_ = keys::kInvalidEventName;
|
| - return false;
|
| - }
|
| + error_ = rules_registry_->AddRules(extension_id(), params->rules);
|
| +
|
| + if (error_.empty())
|
| + result_.reset(AddRules::Result::Create(params->rules));
|
|
|
| - switch (rule_identifiers->GetType()) {
|
| - case Value::TYPE_NULL:
|
| - error_ = rules_registry->RemoveAllRules(extension_id());
|
| - break;
|
| - case Value::TYPE_LIST: {
|
| - std::vector<std::string> rule_identifiers_list;
|
| - EXTENSION_FUNCTION_VALIDATE(
|
| - AddAllStringValues(static_cast<ListValue*>(rule_identifiers),
|
| - &rule_identifiers_list));
|
| - error_ = rules_registry->RemoveRules(extension_id(),
|
| - rule_identifiers_list);
|
| - break;
|
| - }
|
| - default:
|
| - error_ = keys::kInvalidDatatype;
|
| - break;
|
| - }
|
| return error_.empty();
|
| }
|
|
|
| -bool GetRulesFunction::RunImpl() {
|
| - std::string event_name;
|
| - EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
|
| -
|
| - Value* rule_identifiers = NULL;
|
| - EXTENSION_FUNCTION_VALIDATE(args_->Get(1, &rule_identifiers));
|
| +bool RemoveRulesFunction::RunImplOnCorrectThread() {
|
| + scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_));
|
| + EXTENSION_FUNCTION_VALIDATE(params.get());
|
|
|
| - RulesRegistryService* rules_registry_service =
|
| - profile()->GetExtensionService()->GetRulesRegistryService();
|
| - RulesRegistry* rules_registry =
|
| - rules_registry_service->GetRulesRegistry(event_name);
|
| - if (!rules_registry) {
|
| - error_ = keys::kInvalidEventName;
|
| - return false;
|
| + if (params->rule_identifiers.get()) {
|
| + error_ = rules_registry_->RemoveRules(extension_id(),
|
| + *params->rule_identifiers);
|
| + } else {
|
| + error_ = rules_registry_->RemoveAllRules(extension_id());
|
| }
|
|
|
| - std::vector<DictionaryValue*> rules;
|
| - switch (rule_identifiers->GetType()) {
|
| - case Value::TYPE_NULL:
|
| - error_ = rules_registry->GetAllRules(extension_id(), &rules);
|
| - break;
|
| - case Value::TYPE_LIST: {
|
| - std::vector<std::string> rule_identifiers_list;
|
| - EXTENSION_FUNCTION_VALIDATE(
|
| - AddAllStringValues(static_cast<ListValue*>(rule_identifiers),
|
| - &rule_identifiers_list));
|
| - error_ = rules_registry->GetRules(extension_id(), rule_identifiers_list,
|
| - &rules);
|
| - break;
|
| - }
|
| - default:
|
| - error_ = keys::kInvalidDatatype;
|
| - break;
|
| - }
|
| + return error_.empty();
|
| +}
|
|
|
| - if (!error_.empty())
|
| - return false;
|
| +bool GetRulesFunction::RunImplOnCorrectThread() {
|
| + scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_));
|
| + EXTENSION_FUNCTION_VALIDATE(params.get());
|
| +
|
| + std::vector<linked_ptr<Rule> > rules;
|
| + if (params->rule_identifiers.get()) {
|
| + error_ = rules_registry_->GetRules(extension_id(),
|
| + *params->rule_identifiers,
|
| + &rules);
|
| + } else {
|
| + error_ = rules_registry_->GetAllRules(extension_id(), &rules);
|
| + }
|
|
|
| - scoped_ptr<ListValue> result(new ListValue);
|
| - for (std::vector<DictionaryValue*>::iterator i = rules.begin();
|
| - i != rules.end(); ++i)
|
| - result->Append(*i);
|
| - result_.reset(result.release());
|
| + if (error_.empty())
|
| + result_.reset(GetRules::Result::Create(rules));
|
|
|
| - return true;
|
| + return error_.empty();
|
| }
|
|
|
| } // namespace extensions
|
|
|