blob: 8c9b856832d02efdb34ffb48d3d9741a618cb0fc [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
45bool RulesFunction::RunImpl() {
[email protected]e0a65f72012-02-10 01:45:4646 std::string event_name;
47 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
[email protected]8ef650e2012-02-08 10:20:5948
[email protected]e0a65f72012-02-10 01:45:4649 RulesRegistryService* rules_registry_service =
[email protected]b813ed72012-04-05 08:21:3650 ExtensionSystemFactory::GetForProfile(profile())->
51 rules_registry_service();
[email protected]4ef45ea2012-02-29 21:05:0352 rules_registry_ = rules_registry_service->GetRulesRegistry(event_name);
53 // Raw access to this function is not available to extensions, therefore
54 // there should never be a request for a nonexisting rules registry.
55 EXTENSION_FUNCTION_VALIDATE(rules_registry_);
56
57 if (content::BrowserThread::CurrentlyOn(rules_registry_->GetOwnerThread())) {
[email protected]95991b12012-04-17 02:48:0658 bool success = RunImplOnCorrectThread();
59 SendResponse(success);
[email protected]4ef45ea2012-02-29 21:05:0360 } else {
[email protected]95991b12012-04-17 02:48:0661 scoped_refptr<base::MessageLoopProxy> message_loop_proxy =
62 content::BrowserThread::GetMessageLoopProxyForThread(
63 rules_registry_->GetOwnerThread());
64 base::PostTaskAndReplyWithResult(
65 message_loop_proxy,
66 FROM_HERE,
67 base::Bind(&RulesFunction::RunImplOnCorrectThread, this),
68 base::Bind(&RulesFunction::SendResponse, this));
[email protected]e0a65f72012-02-10 01:45:4669 }
70
[email protected]8ef650e2012-02-08 10:20:5971 return true;
72}
73
[email protected]4ef45ea2012-02-29 21:05:0374bool AddRulesFunction::RunImplOnCorrectThread() {
75 scoped_ptr<AddRules::Params> params(AddRules::Params::Create(*args_));
76 EXTENSION_FUNCTION_VALIDATE(params.get());
[email protected]e0a65f72012-02-10 01:45:4677
[email protected]4ef45ea2012-02-29 21:05:0378 error_ = rules_registry_->AddRules(extension_id(), params->rules);
[email protected]e0a65f72012-02-10 01:45:4679
[email protected]4ef45ea2012-02-29 21:05:0380 if (error_.empty())
[email protected]b741e8f2012-07-16 21:47:2481 results_ = AddRules::Results::Create(params->rules);
[email protected]4ef45ea2012-02-29 21:05:0382
[email protected]e0a65f72012-02-10 01:45:4683 return error_.empty();
[email protected]8ef650e2012-02-08 10:20:5984}
85
[email protected]4ef45ea2012-02-29 21:05:0386bool RemoveRulesFunction::RunImplOnCorrectThread() {
87 scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_));
88 EXTENSION_FUNCTION_VALIDATE(params.get());
[email protected]e0a65f72012-02-10 01:45:4689
[email protected]4ef45ea2012-02-29 21:05:0390 if (params->rule_identifiers.get()) {
91 error_ = rules_registry_->RemoveRules(extension_id(),
92 *params->rule_identifiers);
93 } else {
94 error_ = rules_registry_->RemoveAllRules(extension_id());
[email protected]e0a65f72012-02-10 01:45:4695 }
96
[email protected]4ef45ea2012-02-29 21:05:0397 return error_.empty();
98}
99
100bool GetRulesFunction::RunImplOnCorrectThread() {
101 scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_));
102 EXTENSION_FUNCTION_VALIDATE(params.get());
103
104 std::vector<linked_ptr<Rule> > rules;
105 if (params->rule_identifiers.get()) {
106 error_ = rules_registry_->GetRules(extension_id(),
107 *params->rule_identifiers,
108 &rules);
109 } else {
110 error_ = rules_registry_->GetAllRules(extension_id(), &rules);
[email protected]e0a65f72012-02-10 01:45:46111 }
112
[email protected]4ef45ea2012-02-29 21:05:03113 if (error_.empty())
[email protected]b741e8f2012-07-16 21:47:24114 results_ = GetRules::Results::Create(rules);
[email protected]e0a65f72012-02-10 01:45:46115
[email protected]4ef45ea2012-02-29 21:05:03116 return error_.empty();
[email protected]8ef650e2012-02-08 10:20:59117}
118
119} // namespace extensions