blob: 8c5735297e1e6fd9c39857ca1ca5293afd481305 [file] [log] [blame]
[email protected]8ef650e2012-02-08 10:20:591// 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.
4
5#include "chrome/browser/extensions/api/declarative/declarative_api.h"
6
[email protected]4ef45ea2012-02-29 21:05:037#include "base/bind.h"
8#include "base/bind_helpers.h"
[email protected]95991b12012-04-17 02:48:069#include "base/task_runner_util.h"
[email protected]8ef650e2012-02-08 10:20:5910#include "base/values.h"
[email protected]e0a65f72012-02-10 01:45:4611#include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
[email protected]b813ed72012-04-05 08:21:3612#include "chrome/browser/extensions/extension_system_factory.h"
[email protected]e0a65f72012-02-10 01:45:4613#include "chrome/browser/profiles/profile.h"
[email protected]8351d6972012-05-16 12:35:4614#include "chrome/common/extensions/api/events.h"
[email protected]4ef45ea2012-02-29 21:05:0315#include "content/public/browser/browser_thread.h"
[email protected]e0a65f72012-02-10 01:45:4616
[email protected]8351d6972012-05-16 12:35:4617using extensions::api::events::Rule;
[email protected]eb847802012-03-10 00:03:2818
[email protected]8351d6972012-05-16 12:35:4619namespace AddRules = extensions::api::events::Event::AddRules;
20namespace GetRules = extensions::api::events::Event::GetRules;
21namespace RemoveRules = extensions::api::events::Event::RemoveRules;
[email protected]e0a65f72012-02-10 01:45:4622
[email protected]8ef650e2012-02-08 10:20:5923namespace extensions {
24
[email protected]4ef45ea2012-02-29 21:05:0325RulesFunction::RulesFunction() : rules_registry_(NULL) {}
26
27RulesFunction::~RulesFunction() {}
28
[email protected]a6f072902012-08-09 04:04:3829bool RulesFunction::HasPermission() {
30 std::string event_name;
31 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
32 return extension_->HasAPIPermission(event_name);
33}
34
[email protected]4ef45ea2012-02-29 21:05:0335bool RulesFunction::RunImpl() {
[email protected]e0a65f72012-02-10 01:45:4636 std::string event_name;
37 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
[email protected]8ef650e2012-02-08 10:20:5938
[email protected]e0a65f72012-02-10 01:45:4639 RulesRegistryService* rules_registry_service =
[email protected]b813ed72012-04-05 08:21:3640 ExtensionSystemFactory::GetForProfile(profile())->
41 rules_registry_service();
[email protected]4ef45ea2012-02-29 21:05:0342 rules_registry_ = rules_registry_service->GetRulesRegistry(event_name);
43 // Raw access to this function is not available to extensions, therefore
44 // there should never be a request for a nonexisting rules registry.
45 EXTENSION_FUNCTION_VALIDATE(rules_registry_);
46
47 if (content::BrowserThread::CurrentlyOn(rules_registry_->GetOwnerThread())) {
[email protected]95991b12012-04-17 02:48:0648 bool success = RunImplOnCorrectThread();
49 SendResponse(success);
[email protected]4ef45ea2012-02-29 21:05:0350 } else {
[email protected]95991b12012-04-17 02:48:0651 scoped_refptr<base::MessageLoopProxy> message_loop_proxy =
52 content::BrowserThread::GetMessageLoopProxyForThread(
53 rules_registry_->GetOwnerThread());
54 base::PostTaskAndReplyWithResult(
55 message_loop_proxy,
56 FROM_HERE,
57 base::Bind(&RulesFunction::RunImplOnCorrectThread, this),
58 base::Bind(&RulesFunction::SendResponse, this));
[email protected]e0a65f72012-02-10 01:45:4659 }
60
[email protected]8ef650e2012-02-08 10:20:5961 return true;
62}
63
[email protected]4636c832013-01-11 02:10:1164bool EventsAddRulesFunction::RunImplOnCorrectThread() {
[email protected]4ef45ea2012-02-29 21:05:0365 scoped_ptr<AddRules::Params> params(AddRules::Params::Create(*args_));
66 EXTENSION_FUNCTION_VALIDATE(params.get());
[email protected]e0a65f72012-02-10 01:45:4667
[email protected]4ef45ea2012-02-29 21:05:0368 error_ = rules_registry_->AddRules(extension_id(), params->rules);
[email protected]e0a65f72012-02-10 01:45:4669
[email protected]4ef45ea2012-02-29 21:05:0370 if (error_.empty())
[email protected]b741e8f2012-07-16 21:47:2471 results_ = AddRules::Results::Create(params->rules);
[email protected]4ef45ea2012-02-29 21:05:0372
[email protected]e0a65f72012-02-10 01:45:4673 return error_.empty();
[email protected]8ef650e2012-02-08 10:20:5974}
75
[email protected]4636c832013-01-11 02:10:1176bool EventsRemoveRulesFunction::RunImplOnCorrectThread() {
[email protected]4ef45ea2012-02-29 21:05:0377 scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_));
78 EXTENSION_FUNCTION_VALIDATE(params.get());
[email protected]e0a65f72012-02-10 01:45:4679
[email protected]4ef45ea2012-02-29 21:05:0380 if (params->rule_identifiers.get()) {
81 error_ = rules_registry_->RemoveRules(extension_id(),
82 *params->rule_identifiers);
83 } else {
84 error_ = rules_registry_->RemoveAllRules(extension_id());
[email protected]e0a65f72012-02-10 01:45:4685 }
86
[email protected]4ef45ea2012-02-29 21:05:0387 return error_.empty();
88}
89
[email protected]4636c832013-01-11 02:10:1190bool EventsGetRulesFunction::RunImplOnCorrectThread() {
[email protected]37862f6c2012-10-26 08:54:5491 scoped_ptr<GetRules::Params> params(GetRules::Params::Create(*args_));
[email protected]4ef45ea2012-02-29 21:05:0392 EXTENSION_FUNCTION_VALIDATE(params.get());
93
94 std::vector<linked_ptr<Rule> > rules;
95 if (params->rule_identifiers.get()) {
96 error_ = rules_registry_->GetRules(extension_id(),
97 *params->rule_identifiers,
98 &rules);
99 } else {
100 error_ = rules_registry_->GetAllRules(extension_id(), &rules);
[email protected]e0a65f72012-02-10 01:45:46101 }
102
[email protected]4ef45ea2012-02-29 21:05:03103 if (error_.empty())
[email protected]b741e8f2012-07-16 21:47:24104 results_ = GetRules::Results::Create(rules);
[email protected]e0a65f72012-02-10 01:45:46105
[email protected]4ef45ea2012-02-29 21:05:03106 return error_.empty();
[email protected]8ef650e2012-02-08 10:20:59107}
108
109} // namespace extensions