[email protected] | 8ef650e | 2012-02-08 10:20:59 | [diff] [blame] | 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 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 7 | #include "base/bind.h" |
| 8 | #include "base/bind_helpers.h" |
[email protected] | 95991b1 | 2012-04-17 02:48:06 | [diff] [blame] | 9 | #include "base/task_runner_util.h" |
[email protected] | 8ef650e | 2012-02-08 10:20:59 | [diff] [blame] | 10 | #include "base/values.h" |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 11 | #include "chrome/browser/extensions/api/declarative/rules_registry_service.h" |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 12 | #include "chrome/browser/extensions/extension_system_factory.h" |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 13 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 8351d697 | 2012-05-16 12:35:46 | [diff] [blame] | 14 | #include "chrome/common/extensions/api/events.h" |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 15 | #include "content/public/browser/browser_thread.h" |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 16 | |
[email protected] | 8351d697 | 2012-05-16 12:35:46 | [diff] [blame] | 17 | using extensions::api::events::Rule; |
[email protected] | eb84780 | 2012-03-10 00:03:28 | [diff] [blame] | 18 | |
[email protected] | 8351d697 | 2012-05-16 12:35:46 | [diff] [blame] | 19 | namespace AddRules = extensions::api::events::Event::AddRules; |
| 20 | namespace GetRules = extensions::api::events::Event::GetRules; |
| 21 | namespace RemoveRules = extensions::api::events::Event::RemoveRules; |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 22 | |
[email protected] | 8ef650e | 2012-02-08 10:20:59 | [diff] [blame] | 23 | namespace extensions { |
| 24 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 25 | RulesFunction::RulesFunction() : rules_registry_(NULL) {} |
| 26 | |
| 27 | RulesFunction::~RulesFunction() {} |
| 28 | |
[email protected] | a6f07290 | 2012-08-09 04:04:38 | [diff] [blame] | 29 | bool RulesFunction::HasPermission() { |
| 30 | std::string event_name; |
| 31 | EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); |
| 32 | return extension_->HasAPIPermission(event_name); |
| 33 | } |
| 34 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 35 | bool RulesFunction::RunImpl() { |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 36 | std::string event_name; |
| 37 | EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); |
[email protected] | 8ef650e | 2012-02-08 10:20:59 | [diff] [blame] | 38 | |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 39 | RulesRegistryService* rules_registry_service = |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 40 | ExtensionSystemFactory::GetForProfile(profile())-> |
| 41 | rules_registry_service(); |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 42 | rules_registry_ = rules_registry_service->GetRulesRegistry(event_name); |
| 43 | // Raw access to this function is not available to extensions, therefore |
| 44 | // there should never be a request for a nonexisting rules registry. |
| 45 | EXTENSION_FUNCTION_VALIDATE(rules_registry_); |
| 46 | |
| 47 | if (content::BrowserThread::CurrentlyOn(rules_registry_->GetOwnerThread())) { |
[email protected] | 95991b1 | 2012-04-17 02:48:06 | [diff] [blame] | 48 | bool success = RunImplOnCorrectThread(); |
| 49 | SendResponse(success); |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 50 | } else { |
[email protected] | 95991b1 | 2012-04-17 02:48:06 | [diff] [blame] | 51 | scoped_refptr<base::MessageLoopProxy> message_loop_proxy = |
| 52 | content::BrowserThread::GetMessageLoopProxyForThread( |
| 53 | rules_registry_->GetOwnerThread()); |
| 54 | base::PostTaskAndReplyWithResult( |
| 55 | message_loop_proxy, |
| 56 | FROM_HERE, |
| 57 | base::Bind(&RulesFunction::RunImplOnCorrectThread, this), |
| 58 | base::Bind(&RulesFunction::SendResponse, this)); |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 59 | } |
| 60 | |
[email protected] | 8ef650e | 2012-02-08 10:20:59 | [diff] [blame] | 61 | return true; |
| 62 | } |
| 63 | |
[email protected] | 4636c83 | 2013-01-11 02:10:11 | [diff] [blame^] | 64 | bool EventsAddRulesFunction::RunImplOnCorrectThread() { |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 65 | scoped_ptr<AddRules::Params> params(AddRules::Params::Create(*args_)); |
| 66 | EXTENSION_FUNCTION_VALIDATE(params.get()); |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 67 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 68 | error_ = rules_registry_->AddRules(extension_id(), params->rules); |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 69 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 70 | if (error_.empty()) |
[email protected] | b741e8f | 2012-07-16 21:47:24 | [diff] [blame] | 71 | results_ = AddRules::Results::Create(params->rules); |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 72 | |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 73 | return error_.empty(); |
[email protected] | 8ef650e | 2012-02-08 10:20:59 | [diff] [blame] | 74 | } |
| 75 | |
[email protected] | 4636c83 | 2013-01-11 02:10:11 | [diff] [blame^] | 76 | bool EventsRemoveRulesFunction::RunImplOnCorrectThread() { |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 77 | scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_)); |
| 78 | EXTENSION_FUNCTION_VALIDATE(params.get()); |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 79 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 80 | if (params->rule_identifiers.get()) { |
| 81 | error_ = rules_registry_->RemoveRules(extension_id(), |
| 82 | *params->rule_identifiers); |
| 83 | } else { |
| 84 | error_ = rules_registry_->RemoveAllRules(extension_id()); |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 85 | } |
| 86 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 87 | return error_.empty(); |
| 88 | } |
| 89 | |
[email protected] | 4636c83 | 2013-01-11 02:10:11 | [diff] [blame^] | 90 | bool EventsGetRulesFunction::RunImplOnCorrectThread() { |
[email protected] | 37862f6c | 2012-10-26 08:54:54 | [diff] [blame] | 91 | scoped_ptr<GetRules::Params> params(GetRules::Params::Create(*args_)); |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 92 | EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 93 | |
| 94 | std::vector<linked_ptr<Rule> > rules; |
| 95 | if (params->rule_identifiers.get()) { |
| 96 | error_ = rules_registry_->GetRules(extension_id(), |
| 97 | *params->rule_identifiers, |
| 98 | &rules); |
| 99 | } else { |
| 100 | error_ = rules_registry_->GetAllRules(extension_id(), &rules); |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 101 | } |
| 102 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 103 | if (error_.empty()) |
[email protected] | b741e8f | 2012-07-16 21:47:24 | [diff] [blame] | 104 | results_ = GetRules::Results::Create(rules); |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 105 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 106 | return error_.empty(); |
[email protected] | 8ef650e | 2012-02-08 10:20:59 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | } // namespace extensions |