Chromium Code Reviews| 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..05349f2590a5e4ee807e331f9e7adb9888fb7038 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/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_declarative; |
| namespace { |
| @@ -31,120 +33,80 @@ bool AddAllStringValues(ListValue* list, std::vector<std::string>* out) { |
| namespace extensions { |
| -bool AddRulesFunction::RunImpl() { |
| +RulesFunction::RulesFunction() : rules_registry_(NULL) {} |
| + |
| +bool RulesFunction::RunImpl() { |
| std::string event_name; |
| EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); |
| - ListValue* rules_list = NULL; |
| - EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &rules_list)); |
| - |
| - 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); |
| - } |
| - |
| 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); |
| + 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(&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)); |
| - |
| - Value* rule_identifiers = NULL; |
| - EXTENSION_FUNCTION_VALIDATE(args_->Get(1, &rule_identifiers)); |
| - |
| - RulesRegistryService* rules_registry_service = |
| - profile()->GetExtensionService()->GetRulesRegistryService(); |
| - RulesRegistry* rules_registry = |
| - rules_registry_service->GetRulesRegistry(event_name); |
| - if (!rules_registry) { |
| - error_ = keys::kInvalidEventName; |
| - return false; |
| - } |
| +void RulesFunction::SendResponseOnUIThread() { |
| + SendResponse(error_.empty() && !bad_message_); |
|
not at google - send to devlin
2012/02/20 11:50:51
I think that you don't need to check for bad_messa
battre
2012/02/28 22:29:18
Done. - My goal is to get rid of SendResponseOnUIT
|
| +} |
| - 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; |
| +void AddRulesFunction::RunImplOnCorrectThread() { |
| + scoped_ptr<AddRules::Params> params(AddRules::Params::Create(*args_)); |
| + if (!params.get()) { |
| + bad_message_ = true; |
| + return; |
| } |
| - return error_.empty(); |
| -} |
| -bool GetRulesFunction::RunImpl() { |
| - std::string event_name; |
| - EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); |
| + error_ = rules_registry_->AddRules(extension_id(), params->rules); |
| - Value* rule_identifiers = NULL; |
| - EXTENSION_FUNCTION_VALIDATE(args_->Get(1, &rule_identifiers)); |
| + if (error_.empty()) |
| + result_.reset(AddRules::Result::Create(params->rules)); |
| +} |
| - RulesRegistryService* rules_registry_service = |
| - profile()->GetExtensionService()->GetRulesRegistryService(); |
| - RulesRegistry* rules_registry = |
| - rules_registry_service->GetRulesRegistry(event_name); |
| - if (!rules_registry) { |
| - error_ = keys::kInvalidEventName; |
| - return false; |
| +void RemoveRulesFunction::RunImplOnCorrectThread() { |
| + scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_)); |
| + if (!params.get()) { |
| + bad_message_ = true; |
| + return; |
| } |
| - 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; |
| + if (params->rule_identifiers.get()) { |
| + error_ = rules_registry_->RemoveRules(extension_id(), |
| + *params->rule_identifiers); |
| + } else { |
| + error_ = rules_registry_->RemoveAllRules(extension_id()); |
| } |
| +} |
| - if (!error_.empty()) |
| - return false; |
| +void GetRulesFunction::RunImplOnCorrectThread() { |
| + scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_)); |
| + if (!params.get()) { |
| + bad_message_ = true; |
| + return; |
| + } |
| - 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()); |
| + 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); |
| + } |
| - return true; |
| + if (error_.empty()) |
| + result_.reset(GetRules::Result::Create(rules)); |
| } |
| } // namespace extensions |