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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/api/declarative/declarative_api.h"
8 #include "base/bind_helpers.h"
9 #include "base/task_runner_util.h"
10 #include "base/values.h"
11 #include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
12 #include "chrome/browser/extensions/extension_system_factory.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/extensions/api/events.h"
15 #include "chrome/common/extensions/api/extension_api.h"
16 #include "content/public/browser/browser_thread.h"
18 using extensions::api::events::Rule;
20 namespace AddRules = extensions::api::events::Event::AddRules;
21 namespace GetRules = extensions::api::events::Event::GetRules;
22 namespace RemoveRules = extensions::api::events::Event::RemoveRules;
24 namespace extensions {
26 RulesFunction::RulesFunction() : rules_registry_(NULL) {}
28 RulesFunction::~RulesFunction() {}
30 bool RulesFunction::HasPermission() {
31 std::string event_name;
32 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
33 Feature::Availability availability =
34 ExtensionAPI::GetSharedInstance()->IsAvailable(
35 event_name, extension_, Feature::BLESSED_EXTENSION_CONTEXT,
37 return availability.is_available();
40 bool RulesFunction::RunImpl() {
41 std::string event_name;
42 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
44 RulesRegistryService* rules_registry_service =
45 RulesRegistryService::Get(GetProfile());
46 rules_registry_ = rules_registry_service->GetRulesRegistry(event_name);
47 // Raw access to this function is not available to extensions, therefore
48 // there should never be a request for a nonexisting rules registry.
49 EXTENSION_FUNCTION_VALIDATE(rules_registry_.get());
51 if (content::BrowserThread::CurrentlyOn(rules_registry_->owner_thread())) {
52 bool success = RunImplOnCorrectThread();
53 SendResponse(success);
55 scoped_refptr<base::MessageLoopProxy> message_loop_proxy =
56 content::BrowserThread::GetMessageLoopProxyForThread(
57 rules_registry_->owner_thread());
58 base::PostTaskAndReplyWithResult(
59 message_loop_proxy.get(),
61 base::Bind(&RulesFunction::RunImplOnCorrectThread, this),
62 base::Bind(&RulesFunction::SendResponse, this));
68 bool EventsEventAddRulesFunction::RunImplOnCorrectThread() {
69 scoped_ptr<AddRules::Params> params(AddRules::Params::Create(*args_));
70 EXTENSION_FUNCTION_VALIDATE(params.get());
72 error_ = rules_registry_->AddRules(extension_id(), params->rules);
75 results_ = AddRules::Results::Create(params->rules);
77 return error_.empty();
80 bool EventsEventRemoveRulesFunction::RunImplOnCorrectThread() {
81 scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_));
82 EXTENSION_FUNCTION_VALIDATE(params.get());
84 if (params->rule_identifiers.get()) {
85 error_ = rules_registry_->RemoveRules(extension_id(),
86 *params->rule_identifiers);
88 error_ = rules_registry_->RemoveAllRules(extension_id());
91 return error_.empty();
94 bool EventsEventGetRulesFunction::RunImplOnCorrectThread() {
95 scoped_ptr<GetRules::Params> params(GetRules::Params::Create(*args_));
96 EXTENSION_FUNCTION_VALIDATE(params.get());
98 std::vector<linked_ptr<Rule> > rules;
99 if (params->rule_identifiers.get()) {
100 error_ = rules_registry_->GetRules(extension_id(),
101 *params->rule_identifiers,
104 error_ = rules_registry_->GetAllRules(extension_id(), &rules);
108 results_ = GetRules::Results::Create(rules);
110 return error_.empty();
113 } // namespace extensions