blob: 72d9fc150dfb8c8d305fe63a6ba1da3f0f875fbb [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]8ef650e2012-02-08 10:20:599#include "base/values.h"
[email protected]e0a65f72012-02-10 01:45:4610#include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
[email protected]b813ed72012-04-05 08:21:3611#include "chrome/browser/extensions/extension_system_factory.h"
[email protected]e0a65f72012-02-10 01:45:4612#include "chrome/browser/profiles/profile.h"
[email protected]4ef45ea2012-02-29 21:05:0313#include "chrome/common/extensions/api/experimental.declarative.h"
14#include "content/public/browser/browser_thread.h"
[email protected]e0a65f72012-02-10 01:45:4615
[email protected]4dffaf22012-03-23 12:57:1216using extensions::api::experimental_declarative::Rule;
[email protected]eb847802012-03-10 00:03:2817
[email protected]4dffaf22012-03-23 12:57:1218namespace AddRules = extensions::api::experimental_declarative::AddRules;
19namespace GetRules = extensions::api::experimental_declarative::GetRules;
20namespace RemoveRules = extensions::api::experimental_declarative::RemoveRules;
[email protected]e0a65f72012-02-10 01:45:4621
22namespace {
23
24// Adds all entries from |list| to |out|. Assumes that all entries of |list|
25// are strings. Returns true if successful.
26bool AddAllStringValues(ListValue* list, std::vector<std::string>* out) {
27 for (ListValue::iterator i = list->begin(); i != list->end(); ++i) {
28 std::string value;
29 if (!(*i)->GetAsString(&value))
30 return false;
31 out->push_back(value);
32 }
33 return true;
34}
35
36} // namespace
[email protected]8ef650e2012-02-08 10:20:5937
38namespace extensions {
39
[email protected]4ef45ea2012-02-29 21:05:0340RulesFunction::RulesFunction() : rules_registry_(NULL) {}
41
42RulesFunction::~RulesFunction() {}
43
44bool RulesFunction::RunImpl() {
[email protected]e0a65f72012-02-10 01:45:4645 std::string event_name;
46 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
[email protected]8ef650e2012-02-08 10:20:5947
[email protected]e0a65f72012-02-10 01:45:4648 RulesRegistryService* rules_registry_service =
[email protected]b813ed72012-04-05 08:21:3649 ExtensionSystemFactory::GetForProfile(profile())->
50 rules_registry_service();
[email protected]4ef45ea2012-02-29 21:05:0351 rules_registry_ = rules_registry_service->GetRulesRegistry(event_name);
52 // Raw access to this function is not available to extensions, therefore
53 // there should never be a request for a nonexisting rules registry.
54 EXTENSION_FUNCTION_VALIDATE(rules_registry_);
55
56 if (content::BrowserThread::CurrentlyOn(rules_registry_->GetOwnerThread())) {
57 RunImplOnCorrectThread();
58 SendResponseOnUIThread();
59 } else {
60 content::BrowserThread::PostTaskAndReply(
61 rules_registry_->GetOwnerThread(), FROM_HERE,
62 base::Bind(base::IgnoreResult(&RulesFunction::RunImplOnCorrectThread),
63 this),
64 base::Bind(&RulesFunction::SendResponseOnUIThread, this));
[email protected]e0a65f72012-02-10 01:45:4665 }
66
[email protected]8ef650e2012-02-08 10:20:5967 return true;
68}
69
[email protected]4ef45ea2012-02-29 21:05:0370void RulesFunction::SendResponseOnUIThread() {
71 SendResponse(error_.empty());
72}
[email protected]e0a65f72012-02-10 01:45:4673
[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())
81 result_.reset(AddRules::Result::Create(params->rules));
82
[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())
114 result_.reset(GetRules::Result::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