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"
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::core_api::events::Rule
;
28 namespace AddRules
= extensions::core_api::events::Event::AddRules
;
29 namespace GetRules
= extensions::core_api::events::Event::GetRules
;
30 namespace RemoveRules
= extensions::core_api::events::Event::RemoveRules
;
33 namespace extensions
{
37 const char kDeclarativeEventPrefix
[] = "declarative";
39 void ConvertBinaryDictionaryValuesToBase64(base::DictionaryValue
* dict
);
41 // Encodes |binary| as base64 and returns a new StringValue populated with the
43 scoped_ptr
<base::StringValue
> ConvertBinaryToBase64(base::BinaryValue
* binary
) {
44 std::string binary_data
= std::string(binary
->GetBuffer(), binary
->GetSize());
46 base::Base64Encode(binary_data
, &data64
);
47 return scoped_ptr
<base::StringValue
>(new base::StringValue(data64
));
50 // Parses through |args| replacing any BinaryValues with base64 encoded
51 // StringValues. Recurses over any nested ListValues, and calls
52 // ConvertBinaryDictionaryValuesToBase64 for any nested DictionaryValues.
53 void ConvertBinaryListElementsToBase64(base::ListValue
* args
) {
55 for (base::ListValue::iterator iter
= args
->begin(); iter
!= args
->end();
57 if ((*iter
)->IsType(base::Value::TYPE_BINARY
)) {
58 base::BinaryValue
* binary
= NULL
;
59 if (args
->GetBinary(index
, &binary
))
60 args
->Set(index
, ConvertBinaryToBase64(binary
).release());
61 } else if ((*iter
)->IsType(base::Value::TYPE_LIST
)) {
62 base::ListValue
* list
;
63 (*iter
)->GetAsList(&list
);
64 ConvertBinaryListElementsToBase64(list
);
65 } else if ((*iter
)->IsType(base::Value::TYPE_DICTIONARY
)) {
66 base::DictionaryValue
* dict
;
67 (*iter
)->GetAsDictionary(&dict
);
68 ConvertBinaryDictionaryValuesToBase64(dict
);
73 // Parses through |dict| replacing any BinaryValues with base64 encoded
74 // StringValues. Recurses over any nested DictionaryValues, and calls
75 // ConvertBinaryListElementsToBase64 for any nested ListValues.
76 void ConvertBinaryDictionaryValuesToBase64(base::DictionaryValue
* dict
) {
77 for (base::DictionaryValue::Iterator
iter(*dict
); !iter
.IsAtEnd();
79 if (iter
.value().IsType(base::Value::TYPE_BINARY
)) {
80 base::BinaryValue
* binary
= NULL
;
81 if (dict
->GetBinary(iter
.key(), &binary
))
82 dict
->Set(iter
.key(), ConvertBinaryToBase64(binary
).release());
83 } else if (iter
.value().IsType(base::Value::TYPE_LIST
)) {
84 const base::ListValue
* list
;
85 iter
.value().GetAsList(&list
);
86 ConvertBinaryListElementsToBase64(const_cast<base::ListValue
*>(list
));
87 } else if (iter
.value().IsType(base::Value::TYPE_DICTIONARY
)) {
88 const base::DictionaryValue
* dict
;
89 iter
.value().GetAsDictionary(&dict
);
90 ConvertBinaryDictionaryValuesToBase64(
91 const_cast<base::DictionaryValue
*>(dict
));
98 RulesFunction::RulesFunction()
99 : rules_registry_(NULL
) {
102 RulesFunction::~RulesFunction() {}
104 bool RulesFunction::HasPermission() {
105 std::string event_name
;
106 EXTENSION_FUNCTION_VALIDATE(args_
->GetString(0, &event_name
));
107 int web_view_instance_id
= 0;
108 EXTENSION_FUNCTION_VALIDATE(args_
->GetInteger(1, &web_view_instance_id
));
110 // <webview> embedders use the declarativeWebRequest API via
111 // <webview>.onRequest.
112 if (web_view_instance_id
!= 0 &&
113 extension_
->permissions_data()->HasAPIPermission(
114 extensions::APIPermission::kWebView
))
116 return ExtensionFunction::HasPermission();
119 bool RulesFunction::RunAsync() {
120 std::string event_name
;
121 EXTENSION_FUNCTION_VALIDATE(args_
->GetString(0, &event_name
));
123 int web_view_instance_id
= 0;
124 EXTENSION_FUNCTION_VALIDATE(args_
->GetInteger(1, &web_view_instance_id
));
125 int embedder_process_id
= render_frame_host()->GetProcess()->GetID();
127 bool from_web_view
= web_view_instance_id
!= 0;
128 // If we are not operating on a particular <webview>, then the key is 0.
129 int rules_registry_id
= RulesRegistryService::kDefaultRulesRegistryID
;
131 // Sample event names:
132 // webViewInternal.declarativeWebRequest.onRequest.
133 // webViewInternal.declarativeWebRequest.onMessage.
134 // The "webViewInternal." prefix is removed from the event name.
135 std::size_t found
= event_name
.find(kDeclarativeEventPrefix
);
136 EXTENSION_FUNCTION_VALIDATE(found
!= std::string::npos
);
137 event_name
= event_name
.substr(found
);
139 rules_registry_id
= WebViewGuest::GetOrGenerateRulesRegistryID(
140 embedder_process_id
, web_view_instance_id
);
143 // The following call will return a NULL pointer for apps_shell, but should
144 // never be called there anyways.
145 rules_registry_
= RulesRegistryService::Get(browser_context())->
146 GetRulesRegistry(rules_registry_id
, event_name
);
147 DCHECK(rules_registry_
.get());
148 // Raw access to this function is not available to extensions, therefore
149 // there should never be a request for a nonexisting rules registry.
150 EXTENSION_FUNCTION_VALIDATE(rules_registry_
.get());
152 if (content::BrowserThread::CurrentlyOn(rules_registry_
->owner_thread())) {
153 bool success
= RunAsyncOnCorrectThread();
154 SendResponse(success
);
156 scoped_refptr
<base::SingleThreadTaskRunner
> thread_task_runner
=
157 content::BrowserThread::GetMessageLoopProxyForThread(
158 rules_registry_
->owner_thread());
159 base::PostTaskAndReplyWithResult(
160 thread_task_runner
.get(), FROM_HERE
,
161 base::Bind(&RulesFunction::RunAsyncOnCorrectThread
, this),
162 base::Bind(&RulesFunction::SendResponse
, this));
168 bool EventsEventAddRulesFunction::RunAsyncOnCorrectThread() {
169 ConvertBinaryListElementsToBase64(args_
.get());
170 scoped_ptr
<AddRules::Params
> params(AddRules::Params::Create(*args_
));
171 EXTENSION_FUNCTION_VALIDATE(params
.get());
173 error_
= rules_registry_
->AddRules(extension_id(), params
->rules
);
176 results_
= AddRules::Results::Create(params
->rules
);
178 return error_
.empty();
181 bool EventsEventRemoveRulesFunction::RunAsyncOnCorrectThread() {
182 scoped_ptr
<RemoveRules::Params
> params(RemoveRules::Params::Create(*args_
));
183 EXTENSION_FUNCTION_VALIDATE(params
.get());
185 if (params
->rule_identifiers
.get()) {
186 error_
= rules_registry_
->RemoveRules(extension_id(),
187 *params
->rule_identifiers
);
189 error_
= rules_registry_
->RemoveAllRules(extension_id());
192 return error_
.empty();
195 bool EventsEventGetRulesFunction::RunAsyncOnCorrectThread() {
196 scoped_ptr
<GetRules::Params
> params(GetRules::Params::Create(*args_
));
197 EXTENSION_FUNCTION_VALIDATE(params
.get());
199 std::vector
<linked_ptr
<Rule
> > rules
;
200 if (params
->rule_identifiers
.get()) {
201 rules_registry_
->GetRules(
202 extension_id(), *params
->rule_identifiers
, &rules
);
204 rules_registry_
->GetAllRules(extension_id(), &rules
);
207 results_
= GetRules::Results::Create(rules
);
212 } // namespace extensions