Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / extensions / browser / api / declarative / declarative_api.cc
blob715c71c223f2eb5e9233b8265bf37105b62780db
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.
5 #include "extensions/browser/api/declarative/declarative_api.h"
7 #include "base/base64.h"
8 #include "base/bind.h"
9 #include "base/bind_helpers.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/task_runner_util.h"
12 #include "base/values.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/render_frame_host.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "content/public/browser/web_contents.h"
17 #include "extensions/browser/api/declarative/rules_registry_service.h"
18 #include "extensions/browser/api/extensions_api_client.h"
19 #include "extensions/browser/extension_system.h"
20 #include "extensions/browser/guest_view/web_view/web_view_constants.h"
21 #include "extensions/browser/guest_view/web_view/web_view_guest.h"
22 #include "extensions/common/api/events.h"
23 #include "extensions/common/extension_api.h"
24 #include "extensions/common/permissions/permissions_data.h"
26 using extensions::api::events::Rule;
28 namespace AddRules = extensions::api::events::Event::AddRules;
29 namespace GetRules = extensions::api::events::Event::GetRules;
30 namespace RemoveRules = extensions::api::events::Event::RemoveRules;
32 namespace extensions {
34 namespace {
36 const char kDeclarativeEventPrefix[] = "declarative";
38 void ConvertBinaryDictionaryValuesToBase64(base::DictionaryValue* dict);
40 // Encodes |binary| as base64 and returns a new StringValue populated with the
41 // encoded string.
42 scoped_ptr<base::StringValue> ConvertBinaryToBase64(base::BinaryValue* binary) {
43 std::string binary_data = std::string(binary->GetBuffer(), binary->GetSize());
44 std::string data64;
45 base::Base64Encode(binary_data, &data64);
46 return scoped_ptr<base::StringValue>(new base::StringValue(data64));
49 // Parses through |args| replacing any BinaryValues with base64 encoded
50 // StringValues. Recurses over any nested ListValues, and calls
51 // ConvertBinaryDictionaryValuesToBase64 for any nested DictionaryValues.
52 void ConvertBinaryListElementsToBase64(base::ListValue* args) {
53 size_t index = 0;
54 for (base::ListValue::iterator iter = args->begin(); iter != args->end();
55 ++iter, ++index) {
56 if ((*iter)->IsType(base::Value::TYPE_BINARY)) {
57 base::BinaryValue* binary = NULL;
58 if (args->GetBinary(index, &binary))
59 args->Set(index, ConvertBinaryToBase64(binary).release());
60 } else if ((*iter)->IsType(base::Value::TYPE_LIST)) {
61 base::ListValue* list;
62 (*iter)->GetAsList(&list);
63 ConvertBinaryListElementsToBase64(list);
64 } else if ((*iter)->IsType(base::Value::TYPE_DICTIONARY)) {
65 base::DictionaryValue* dict;
66 (*iter)->GetAsDictionary(&dict);
67 ConvertBinaryDictionaryValuesToBase64(dict);
72 // Parses through |dict| replacing any BinaryValues with base64 encoded
73 // StringValues. Recurses over any nested DictionaryValues, and calls
74 // ConvertBinaryListElementsToBase64 for any nested ListValues.
75 void ConvertBinaryDictionaryValuesToBase64(base::DictionaryValue* dict) {
76 for (base::DictionaryValue::Iterator iter(*dict); !iter.IsAtEnd();
77 iter.Advance()) {
78 if (iter.value().IsType(base::Value::TYPE_BINARY)) {
79 base::BinaryValue* binary = NULL;
80 if (dict->GetBinary(iter.key(), &binary))
81 dict->Set(iter.key(), ConvertBinaryToBase64(binary).release());
82 } else if (iter.value().IsType(base::Value::TYPE_LIST)) {
83 const base::ListValue* list;
84 iter.value().GetAsList(&list);
85 ConvertBinaryListElementsToBase64(const_cast<base::ListValue*>(list));
86 } else if (iter.value().IsType(base::Value::TYPE_DICTIONARY)) {
87 const base::DictionaryValue* dict;
88 iter.value().GetAsDictionary(&dict);
89 ConvertBinaryDictionaryValuesToBase64(
90 const_cast<base::DictionaryValue*>(dict));
95 } // namespace
97 RulesFunction::RulesFunction()
98 : rules_registry_(NULL) {
101 RulesFunction::~RulesFunction() {}
103 bool RulesFunction::HasPermission() {
104 std::string event_name;
105 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
106 int web_view_instance_id = 0;
107 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &web_view_instance_id));
109 // <webview> embedders use the declarativeWebRequest API via
110 // <webview>.onRequest.
111 if (web_view_instance_id != 0 &&
112 extension_->permissions_data()->HasAPIPermission(
113 extensions::APIPermission::kWebView))
114 return true;
115 return ExtensionFunction::HasPermission();
118 bool RulesFunction::RunAsync() {
119 std::string event_name;
120 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
122 int web_view_instance_id = 0;
123 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &web_view_instance_id));
124 int embedder_process_id = render_frame_host()->GetProcess()->GetID();
126 bool from_web_view = web_view_instance_id != 0;
127 // If we are not operating on a particular <webview>, then the key is 0.
128 int rules_registry_id = RulesRegistryService::kDefaultRulesRegistryID;
129 if (from_web_view) {
130 // Sample event names:
131 // webViewInternal.declarativeWebRequest.onRequest.
132 // webViewInternal.declarativeWebRequest.onMessage.
133 // The "webViewInternal." prefix is removed from the event name.
134 std::size_t found = event_name.find(kDeclarativeEventPrefix);
135 EXTENSION_FUNCTION_VALIDATE(found != std::string::npos);
136 event_name = event_name.substr(found);
138 rules_registry_id = WebViewGuest::GetOrGenerateRulesRegistryID(
139 embedder_process_id, web_view_instance_id);
142 // The following call will return a NULL pointer for apps_shell, but should
143 // never be called there anyways.
144 rules_registry_ = RulesRegistryService::Get(browser_context())->
145 GetRulesRegistry(rules_registry_id, event_name);
146 DCHECK(rules_registry_.get());
147 // Raw access to this function is not available to extensions, therefore
148 // there should never be a request for a nonexisting rules registry.
149 EXTENSION_FUNCTION_VALIDATE(rules_registry_.get());
151 if (content::BrowserThread::CurrentlyOn(rules_registry_->owner_thread())) {
152 bool success = RunAsyncOnCorrectThread();
153 SendResponse(success);
154 } else {
155 scoped_refptr<base::SingleThreadTaskRunner> thread_task_runner =
156 content::BrowserThread::GetMessageLoopProxyForThread(
157 rules_registry_->owner_thread());
158 base::PostTaskAndReplyWithResult(
159 thread_task_runner.get(), FROM_HERE,
160 base::Bind(&RulesFunction::RunAsyncOnCorrectThread, this),
161 base::Bind(&RulesFunction::SendResponse, this));
164 return true;
167 bool EventsEventAddRulesFunction::RunAsyncOnCorrectThread() {
168 ConvertBinaryListElementsToBase64(args_.get());
169 scoped_ptr<AddRules::Params> params(AddRules::Params::Create(*args_));
170 EXTENSION_FUNCTION_VALIDATE(params.get());
172 error_ = rules_registry_->AddRules(extension_id(), params->rules);
174 if (error_.empty())
175 results_ = AddRules::Results::Create(params->rules);
177 return error_.empty();
180 bool EventsEventRemoveRulesFunction::RunAsyncOnCorrectThread() {
181 scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_));
182 EXTENSION_FUNCTION_VALIDATE(params.get());
184 if (params->rule_identifiers.get()) {
185 error_ = rules_registry_->RemoveRules(extension_id(),
186 *params->rule_identifiers);
187 } else {
188 error_ = rules_registry_->RemoveAllRules(extension_id());
191 return error_.empty();
194 bool EventsEventGetRulesFunction::RunAsyncOnCorrectThread() {
195 scoped_ptr<GetRules::Params> params(GetRules::Params::Create(*args_));
196 EXTENSION_FUNCTION_VALIDATE(params.get());
198 std::vector<linked_ptr<Rule> > rules;
199 if (params->rule_identifiers.get()) {
200 rules_registry_->GetRules(
201 extension_id(), *params->rule_identifiers, &rules);
202 } else {
203 rules_registry_->GetAllRules(extension_id(), &rules);
206 results_ = GetRules::Results::Create(rules);
208 return true;
211 } // namespace extensions