- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / declarative / declarative_api.cc
1 // 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
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/task_runner_util.h"
10 #include "base/values.h"
11 #include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
12 #include "chrome/browser/extensions/extension_system_factory.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/extensions/api/events.h"
15 #include "chrome/common/extensions/api/extension_api.h"
16 #include "content/public/browser/browser_thread.h"
17
18 using extensions::api::events::Rule;
19
20 namespace AddRules = extensions::api::events::Event::AddRules;
21 namespace GetRules = extensions::api::events::Event::GetRules;
22 namespace RemoveRules = extensions::api::events::Event::RemoveRules;
23
24 namespace extensions {
25
26 RulesFunction::RulesFunction() : rules_registry_(NULL) {}
27
28 RulesFunction::~RulesFunction() {}
29
30 bool RulesFunction::HasPermission() {
31   std::string event_name;
32   EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
33   Feature::Availability availability =
34       ExtensionAPI::GetSharedInstance()->IsAvailable(
35           event_name, extension_, Feature::BLESSED_EXTENSION_CONTEXT,
36           source_url());
37   return availability.is_available();
38 }
39
40 bool RulesFunction::RunImpl() {
41   std::string event_name;
42   EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
43
44   RulesRegistryService* rules_registry_service =
45       RulesRegistryService::Get(GetProfile());
46   rules_registry_ = rules_registry_service->GetRulesRegistry(event_name);
47   // Raw access to this function is not available to extensions, therefore
48   // there should never be a request for a nonexisting rules registry.
49   EXTENSION_FUNCTION_VALIDATE(rules_registry_.get());
50
51   if (content::BrowserThread::CurrentlyOn(rules_registry_->owner_thread())) {
52     bool success = RunImplOnCorrectThread();
53     SendResponse(success);
54   } else {
55     scoped_refptr<base::MessageLoopProxy> message_loop_proxy =
56         content::BrowserThread::GetMessageLoopProxyForThread(
57             rules_registry_->owner_thread());
58     base::PostTaskAndReplyWithResult(
59         message_loop_proxy.get(),
60         FROM_HERE,
61         base::Bind(&RulesFunction::RunImplOnCorrectThread, this),
62         base::Bind(&RulesFunction::SendResponse, this));
63   }
64
65   return true;
66 }
67
68 bool EventsEventAddRulesFunction::RunImplOnCorrectThread() {
69   scoped_ptr<AddRules::Params> params(AddRules::Params::Create(*args_));
70   EXTENSION_FUNCTION_VALIDATE(params.get());
71
72   error_ = rules_registry_->AddRules(extension_id(), params->rules);
73
74   if (error_.empty())
75     results_ = AddRules::Results::Create(params->rules);
76
77   return error_.empty();
78 }
79
80 bool EventsEventRemoveRulesFunction::RunImplOnCorrectThread() {
81   scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_));
82   EXTENSION_FUNCTION_VALIDATE(params.get());
83
84   if (params->rule_identifiers.get()) {
85     error_ = rules_registry_->RemoveRules(extension_id(),
86                                           *params->rule_identifiers);
87   } else {
88     error_ = rules_registry_->RemoveAllRules(extension_id());
89   }
90
91   return error_.empty();
92 }
93
94 bool EventsEventGetRulesFunction::RunImplOnCorrectThread() {
95   scoped_ptr<GetRules::Params> params(GetRules::Params::Create(*args_));
96   EXTENSION_FUNCTION_VALIDATE(params.get());
97
98   std::vector<linked_ptr<Rule> > rules;
99   if (params->rule_identifiers.get()) {
100     error_ = rules_registry_->GetRules(extension_id(),
101                                        *params->rule_identifiers,
102                                        &rules);
103   } else {
104     error_ = rules_registry_->GetAllRules(extension_id(), &rules);
105   }
106
107   if (error_.empty())
108     results_ = GetRules::Results::Create(rules);
109
110   return error_.empty();
111 }
112
113 }  // namespace extensions