blob: ce226fd6f844af5d5bb6f93f5ab77440cc1c965a [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
23namespace {
24
25// Adds all entries from |list| to |out|. Assumes that all entries of |list|
26// are strings. Returns true if successful.
27bool AddAllStringValues(ListValue* list, std::vector<std::string>* out) {
28 for (ListValue::iterator i = list->begin(); i != list->end(); ++i) {
29 std::string value;
30 if (!(*i)->GetAsString(&value))
31 return false;
32 out->push_back(value);
33 }
34 return true;
35}
36
37} // namespace
[email protected]8ef650e2012-02-08 10:20:5938
39namespace extensions {
40
[email protected]4ef45ea2012-02-29 21:05:0341RulesFunction::RulesFunction() : rules_registry_(NULL) {}
42
43RulesFunction::~RulesFunction() {}
44
[email protected]a6f072902012-08-09 04:04:3845bool RulesFunction::HasPermission() {
46 std::string event_name;
47 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
48 return extension_->HasAPIPermission(event_name);
49}
50
[email protected]4ef45ea2012-02-29 21:05:0351bool RulesFunction::RunImpl() {
[email protected]e0a65f72012-02-10 01:45:4652 std::string event_name;
53 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
[email protected]8ef650e2012-02-08 10:20:5954
[email protected]e0a65f72012-02-10 01:45:4655 RulesRegistryService* rules_registry_service =
[email protected]b813ed72012-04-05 08:21:3656 ExtensionSystemFactory::GetForProfile(profile())->
57 rules_registry_service();
[email protected]4ef45ea2012-02-29 21:05:0358 rules_registry_ = rules_registry_service->GetRulesRegistry(event_name);
59 // Raw access to this function is not available to extensions, therefore
60 // there should never be a request for a nonexisting rules registry.
61 EXTENSION_FUNCTION_VALIDATE(rules_registry_);
62
63 if (content::BrowserThread::CurrentlyOn(rules_registry_->GetOwnerThread())) {
[email protected]95991b12012-04-17 02:48:0664 bool success = RunImplOnCorrectThread();
65 SendResponse(success);
[email protected]4ef45ea2012-02-29 21:05:0366 } else {
[email protected]95991b12012-04-17 02:48:0667 scoped_refptr<base::MessageLoopProxy> message_loop_proxy =
68 content::BrowserThread::GetMessageLoopProxyForThread(
69 rules_registry_->GetOwnerThread());
70 base::PostTaskAndReplyWithResult(
71 message_loop_proxy,
72 FROM_HERE,
73 base::Bind(&RulesFunction::RunImplOnCorrectThread, this),
74 base::Bind(&RulesFunction::SendResponse, this));
[email protected]e0a65f72012-02-10 01:45:4675 }
76
[email protected]8ef650e2012-02-08 10:20:5977 return true;
78}
79
[email protected]4ef45ea2012-02-29 21:05:0380bool AddRulesFunction::RunImplOnCorrectThread() {
81 scoped_ptr<AddRules::Params> params(AddRules::Params::Create(*args_));
82 EXTENSION_FUNCTION_VALIDATE(params.get());
[email protected]e0a65f72012-02-10 01:45:4683
[email protected]4ef45ea2012-02-29 21:05:0384 error_ = rules_registry_->AddRules(extension_id(), params->rules);
[email protected]e0a65f72012-02-10 01:45:4685
[email protected]4ef45ea2012-02-29 21:05:0386 if (error_.empty())
[email protected]b741e8f2012-07-16 21:47:2487 results_ = AddRules::Results::Create(params->rules);
[email protected]4ef45ea2012-02-29 21:05:0388
[email protected]e0a65f72012-02-10 01:45:4689 return error_.empty();
[email protected]8ef650e2012-02-08 10:20:5990}
91
[email protected]4ef45ea2012-02-29 21:05:0392bool RemoveRulesFunction::RunImplOnCorrectThread() {
93 scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_));
94 EXTENSION_FUNCTION_VALIDATE(params.get());
[email protected]e0a65f72012-02-10 01:45:4695
[email protected]4ef45ea2012-02-29 21:05:0396 if (params->rule_identifiers.get()) {
97 error_ = rules_registry_->RemoveRules(extension_id(),
98 *params->rule_identifiers);
99 } else {
100 error_ = rules_registry_->RemoveAllRules(extension_id());
[email protected]e0a65f72012-02-10 01:45:46101 }
102
[email protected]4ef45ea2012-02-29 21:05:03103 return error_.empty();
104}
105
106bool GetRulesFunction::RunImplOnCorrectThread() {
107 scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_));
108 EXTENSION_FUNCTION_VALIDATE(params.get());
109
110 std::vector<linked_ptr<Rule> > rules;
111 if (params->rule_identifiers.get()) {
112 error_ = rules_registry_->GetRules(extension_id(),
113 *params->rule_identifiers,
114 &rules);
115 } else {
116 error_ = rules_registry_->GetAllRules(extension_id(), &rules);
[email protected]e0a65f72012-02-10 01:45:46117 }
118
[email protected]4ef45ea2012-02-29 21:05:03119 if (error_.empty())
[email protected]b741e8f2012-07-16 21:47:24120 results_ = GetRules::Results::Create(rules);
[email protected]e0a65f72012-02-10 01:45:46121
[email protected]4ef45ea2012-02-29 21:05:03122 return error_.empty();
[email protected]8ef650e2012-02-08 10:20:59123}
124
125} // namespace extensions