[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 | |
| 23 | namespace { |
| 24 | |
| 25 | // Adds all entries from |list| to |out|. Assumes that all entries of |list| |
| 26 | // are strings. Returns true if successful. |
| 27 | bool 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] | 8ef650e | 2012-02-08 10:20:59 | [diff] [blame] | 38 | |
| 39 | namespace extensions { |
| 40 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 41 | RulesFunction::RulesFunction() : rules_registry_(NULL) {} |
| 42 | |
| 43 | RulesFunction::~RulesFunction() {} |
| 44 | |
[email protected] | a6f07290 | 2012-08-09 04:04:38 | [diff] [blame^] | 45 | bool RulesFunction::HasPermission() { |
| 46 | std::string event_name; |
| 47 | EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); |
| 48 | return extension_->HasAPIPermission(event_name); |
| 49 | } |
| 50 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 51 | bool RulesFunction::RunImpl() { |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 52 | std::string event_name; |
| 53 | EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); |
[email protected] | 8ef650e | 2012-02-08 10:20:59 | [diff] [blame] | 54 | |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 55 | RulesRegistryService* rules_registry_service = |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 56 | ExtensionSystemFactory::GetForProfile(profile())-> |
| 57 | rules_registry_service(); |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 58 | rules_registry_ = rules_registry_service->GetRulesRegistry(event_name); |
| 59 | // Raw access to this function is not available to extensions, therefore |
| 60 | // there should never be a request for a nonexisting rules registry. |
| 61 | EXTENSION_FUNCTION_VALIDATE(rules_registry_); |
| 62 | |
| 63 | if (content::BrowserThread::CurrentlyOn(rules_registry_->GetOwnerThread())) { |
[email protected] | 95991b1 | 2012-04-17 02:48:06 | [diff] [blame] | 64 | bool success = RunImplOnCorrectThread(); |
| 65 | SendResponse(success); |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 66 | } else { |
[email protected] | 95991b1 | 2012-04-17 02:48:06 | [diff] [blame] | 67 | scoped_refptr<base::MessageLoopProxy> message_loop_proxy = |
| 68 | content::BrowserThread::GetMessageLoopProxyForThread( |
| 69 | rules_registry_->GetOwnerThread()); |
| 70 | base::PostTaskAndReplyWithResult( |
| 71 | message_loop_proxy, |
| 72 | FROM_HERE, |
| 73 | base::Bind(&RulesFunction::RunImplOnCorrectThread, this), |
| 74 | base::Bind(&RulesFunction::SendResponse, this)); |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 75 | } |
| 76 | |
[email protected] | 8ef650e | 2012-02-08 10:20:59 | [diff] [blame] | 77 | return true; |
| 78 | } |
| 79 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 80 | bool AddRulesFunction::RunImplOnCorrectThread() { |
| 81 | scoped_ptr<AddRules::Params> params(AddRules::Params::Create(*args_)); |
| 82 | EXTENSION_FUNCTION_VALIDATE(params.get()); |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 83 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 84 | error_ = rules_registry_->AddRules(extension_id(), params->rules); |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 85 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 86 | if (error_.empty()) |
[email protected] | b741e8f | 2012-07-16 21:47:24 | [diff] [blame] | 87 | results_ = AddRules::Results::Create(params->rules); |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 88 | |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 89 | return error_.empty(); |
[email protected] | 8ef650e | 2012-02-08 10:20:59 | [diff] [blame] | 90 | } |
| 91 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 92 | bool RemoveRulesFunction::RunImplOnCorrectThread() { |
| 93 | scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_)); |
| 94 | EXTENSION_FUNCTION_VALIDATE(params.get()); |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 95 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 96 | if (params->rule_identifiers.get()) { |
| 97 | error_ = rules_registry_->RemoveRules(extension_id(), |
| 98 | *params->rule_identifiers); |
| 99 | } else { |
| 100 | error_ = rules_registry_->RemoveAllRules(extension_id()); |
[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 | return error_.empty(); |
| 104 | } |
| 105 | |
| 106 | bool GetRulesFunction::RunImplOnCorrectThread() { |
| 107 | scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_)); |
| 108 | EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 109 | |
| 110 | std::vector<linked_ptr<Rule> > rules; |
| 111 | if (params->rule_identifiers.get()) { |
| 112 | error_ = rules_registry_->GetRules(extension_id(), |
| 113 | *params->rule_identifiers, |
| 114 | &rules); |
| 115 | } else { |
| 116 | error_ = rules_registry_->GetAllRules(extension_id(), &rules); |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 117 | } |
| 118 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 119 | if (error_.empty()) |
[email protected] | b741e8f | 2012-07-16 21:47:24 | [diff] [blame] | 120 | results_ = GetRules::Results::Create(rules); |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 121 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 122 | return error_.empty(); |
[email protected] | 8ef650e | 2012-02-08 10:20:59 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | } // namespace extensions |