[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] | 8ef650e | 2012-02-08 10:20:59 | [diff] [blame] | 9 | #include "base/values.h" |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 10 | #include "chrome/browser/extensions/api/declarative/rules_registry_service.h" |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame^] | 11 | #include "chrome/browser/extensions/extension_system_factory.h" |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 12 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 13 | #include "chrome/common/extensions/api/experimental.declarative.h" |
| 14 | #include "content/public/browser/browser_thread.h" |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 15 | |
[email protected] | 4dffaf2 | 2012-03-23 12:57:12 | [diff] [blame] | 16 | using extensions::api::experimental_declarative::Rule; |
[email protected] | eb84780 | 2012-03-10 00:03:28 | [diff] [blame] | 17 | |
[email protected] | 4dffaf2 | 2012-03-23 12:57:12 | [diff] [blame] | 18 | namespace AddRules = extensions::api::experimental_declarative::AddRules; |
| 19 | namespace GetRules = extensions::api::experimental_declarative::GetRules; |
| 20 | namespace RemoveRules = extensions::api::experimental_declarative::RemoveRules; |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 21 | |
| 22 | namespace { |
| 23 | |
| 24 | // Adds all entries from |list| to |out|. Assumes that all entries of |list| |
| 25 | // are strings. Returns true if successful. |
| 26 | bool 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] | 8ef650e | 2012-02-08 10:20:59 | [diff] [blame] | 37 | |
| 38 | namespace extensions { |
| 39 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 40 | RulesFunction::RulesFunction() : rules_registry_(NULL) {} |
| 41 | |
| 42 | RulesFunction::~RulesFunction() {} |
| 43 | |
| 44 | bool RulesFunction::RunImpl() { |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 45 | std::string event_name; |
| 46 | EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); |
[email protected] | 8ef650e | 2012-02-08 10:20:59 | [diff] [blame] | 47 | |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 48 | RulesRegistryService* rules_registry_service = |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame^] | 49 | ExtensionSystemFactory::GetForProfile(profile())-> |
| 50 | rules_registry_service(); |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 51 | 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] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 65 | } |
| 66 | |
[email protected] | 8ef650e | 2012-02-08 10:20:59 | [diff] [blame] | 67 | return true; |
| 68 | } |
| 69 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 70 | void RulesFunction::SendResponseOnUIThread() { |
| 71 | SendResponse(error_.empty()); |
| 72 | } |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 73 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 74 | bool AddRulesFunction::RunImplOnCorrectThread() { |
| 75 | scoped_ptr<AddRules::Params> params(AddRules::Params::Create(*args_)); |
| 76 | EXTENSION_FUNCTION_VALIDATE(params.get()); |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 77 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 78 | error_ = rules_registry_->AddRules(extension_id(), params->rules); |
[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 (error_.empty()) |
| 81 | result_.reset(AddRules::Result::Create(params->rules)); |
| 82 | |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 83 | return error_.empty(); |
[email protected] | 8ef650e | 2012-02-08 10:20:59 | [diff] [blame] | 84 | } |
| 85 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 86 | bool RemoveRulesFunction::RunImplOnCorrectThread() { |
| 87 | scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_)); |
| 88 | EXTENSION_FUNCTION_VALIDATE(params.get()); |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 89 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 90 | 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] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 95 | } |
| 96 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 97 | return error_.empty(); |
| 98 | } |
| 99 | |
| 100 | bool 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] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 111 | } |
| 112 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 113 | if (error_.empty()) |
| 114 | result_.reset(GetRules::Result::Create(rules)); |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 115 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 116 | return error_.empty(); |
[email protected] | 8ef650e | 2012-02-08 10:20:59 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | } // namespace extensions |