[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" |
| 11 | #include "chrome/browser/extensions/extension_service.h" |
| 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] | eb84780 | 2012-03-10 00:03:28 | [diff] [blame^] | 16 | using extensions::api::experimental::Rule; |
| 17 | |
| 18 | namespace AddRules = extensions::api::experimental::AddRules; |
| 19 | namespace GetRules = extensions::api::experimental::GetRules; |
| 20 | namespace RemoveRules = extensions::api::experimental::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 = |
| 49 | profile()->GetExtensionService()->GetRulesRegistryService(); |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 50 | 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] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 64 | } |
| 65 | |
[email protected] | 8ef650e | 2012-02-08 10:20:59 | [diff] [blame] | 66 | return true; |
| 67 | } |
| 68 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 69 | void RulesFunction::SendResponseOnUIThread() { |
| 70 | SendResponse(error_.empty()); |
| 71 | } |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 72 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 73 | bool AddRulesFunction::RunImplOnCorrectThread() { |
| 74 | scoped_ptr<AddRules::Params> params(AddRules::Params::Create(*args_)); |
| 75 | EXTENSION_FUNCTION_VALIDATE(params.get()); |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 76 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 77 | error_ = rules_registry_->AddRules(extension_id(), params->rules); |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 78 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 79 | if (error_.empty()) |
| 80 | result_.reset(AddRules::Result::Create(params->rules)); |
| 81 | |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 82 | return error_.empty(); |
[email protected] | 8ef650e | 2012-02-08 10:20:59 | [diff] [blame] | 83 | } |
| 84 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 85 | bool RemoveRulesFunction::RunImplOnCorrectThread() { |
| 86 | scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_)); |
| 87 | EXTENSION_FUNCTION_VALIDATE(params.get()); |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 88 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 89 | 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] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 96 | return error_.empty(); |
| 97 | } |
| 98 | |
| 99 | bool 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] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 110 | } |
| 111 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 112 | if (error_.empty()) |
| 113 | result_.reset(GetRules::Result::Create(rules)); |
[email protected] | e0a65f7 | 2012-02-10 01:45:46 | [diff] [blame] | 114 | |
[email protected] | 4ef45ea | 2012-02-29 21:05:03 | [diff] [blame] | 115 | return error_.empty(); |
[email protected] | 8ef650e | 2012-02-08 10:20:59 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | } // namespace extensions |