blob: 34e3283ea925f52b8582ab89b542db43cb24c9b0 [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"
11#include "chrome/browser/extensions/extension_service.h"
12#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]eb847802012-03-10 00:03:2816using extensions::api::experimental::Rule;
17
18namespace AddRules = extensions::api::experimental::AddRules;
19namespace GetRules = extensions::api::experimental::GetRules;
20namespace RemoveRules = extensions::api::experimental::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 =
49 profile()->GetExtensionService()->GetRulesRegistryService();
[email protected]4ef45ea2012-02-29 21:05:0350 rules_registry_ = rules_registry_service->GetRulesRegistry(event_name);
51 // Raw access to this function is not available to extensions, therefore
52 // there should never be a request for a nonexisting rules registry.
53 EXTENSION_FUNCTION_VALIDATE(rules_registry_);
54
55 if (content::BrowserThread::CurrentlyOn(rules_registry_->GetOwnerThread())) {
56 RunImplOnCorrectThread();
57 SendResponseOnUIThread();
58 } else {
59 content::BrowserThread::PostTaskAndReply(
60 rules_registry_->GetOwnerThread(), FROM_HERE,
61 base::Bind(base::IgnoreResult(&RulesFunction::RunImplOnCorrectThread),
62 this),
63 base::Bind(&RulesFunction::SendResponseOnUIThread, this));
[email protected]e0a65f72012-02-10 01:45:4664 }
65
[email protected]8ef650e2012-02-08 10:20:5966 return true;
67}
68
[email protected]4ef45ea2012-02-29 21:05:0369void RulesFunction::SendResponseOnUIThread() {
70 SendResponse(error_.empty());
71}
[email protected]e0a65f72012-02-10 01:45:4672
[email protected]4ef45ea2012-02-29 21:05:0373bool AddRulesFunction::RunImplOnCorrectThread() {
74 scoped_ptr<AddRules::Params> params(AddRules::Params::Create(*args_));
75 EXTENSION_FUNCTION_VALIDATE(params.get());
[email protected]e0a65f72012-02-10 01:45:4676
[email protected]4ef45ea2012-02-29 21:05:0377 error_ = rules_registry_->AddRules(extension_id(), params->rules);
[email protected]e0a65f72012-02-10 01:45:4678
[email protected]4ef45ea2012-02-29 21:05:0379 if (error_.empty())
80 result_.reset(AddRules::Result::Create(params->rules));
81
[email protected]e0a65f72012-02-10 01:45:4682 return error_.empty();
[email protected]8ef650e2012-02-08 10:20:5983}
84
[email protected]4ef45ea2012-02-29 21:05:0385bool RemoveRulesFunction::RunImplOnCorrectThread() {
86 scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_));
87 EXTENSION_FUNCTION_VALIDATE(params.get());
[email protected]e0a65f72012-02-10 01:45:4688
[email protected]4ef45ea2012-02-29 21:05:0389 if (params->rule_identifiers.get()) {
90 error_ = rules_registry_->RemoveRules(extension_id(),
91 *params->rule_identifiers);
92 } else {
93 error_ = rules_registry_->RemoveAllRules(extension_id());
[email protected]e0a65f72012-02-10 01:45:4694 }
95
[email protected]4ef45ea2012-02-29 21:05:0396 return error_.empty();
97}
98
99bool GetRulesFunction::RunImplOnCorrectThread() {
100 scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_));
101 EXTENSION_FUNCTION_VALIDATE(params.get());
102
103 std::vector<linked_ptr<Rule> > rules;
104 if (params->rule_identifiers.get()) {
105 error_ = rules_registry_->GetRules(extension_id(),
106 *params->rule_identifiers,
107 &rules);
108 } else {
109 error_ = rules_registry_->GetAllRules(extension_id(), &rules);
[email protected]e0a65f72012-02-10 01:45:46110 }
111
[email protected]4ef45ea2012-02-29 21:05:03112 if (error_.empty())
113 result_.reset(GetRules::Result::Create(rules));
[email protected]e0a65f72012-02-10 01:45:46114
[email protected]4ef45ea2012-02-29 21:05:03115 return error_.empty();
[email protected]8ef650e2012-02-08 10:20:59116}
117
118} // namespace extensions