Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: extensions/renderer/api_binding_hooks.cc

Issue 2609553003: [Extensions Bindings] Allow custom hooks to return values, throw (Closed)
Patch Set: woot Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "extensions/renderer/api_binding_hooks.h" 5 #include "extensions/renderer/api_binding_hooks.h"
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/strings/stringprintf.h" 8 #include "base/strings/stringprintf.h"
9 #include "base/supports_user_data.h" 9 #include "base/supports_user_data.h"
10 #include "extensions/renderer/api_signature.h" 10 #include "extensions/renderer/api_signature.h"
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 js_hooks_source_ = std::move(source); 180 js_hooks_source_ = std::move(source);
181 js_resource_name_ = std::move(resource_name); 181 js_resource_name_ = std::move(resource_name);
182 } 182 }
183 183
184 APIBindingHooks::RequestResult APIBindingHooks::HandleRequest( 184 APIBindingHooks::RequestResult APIBindingHooks::HandleRequest(
185 const std::string& api_name, 185 const std::string& api_name,
186 const std::string& method_name, 186 const std::string& method_name,
187 v8::Local<v8::Context> context, 187 v8::Local<v8::Context> context,
188 const APISignature* signature, 188 const APISignature* signature,
189 std::vector<v8::Local<v8::Value>>* arguments, 189 std::vector<v8::Local<v8::Value>>* arguments,
190 const ArgumentSpec::RefMap& type_refs) { 190 const ArgumentSpec::RefMap& type_refs,
191 v8::Local<v8::Value>* return_value) {
191 // Easy case: a native custom hook. 192 // Easy case: a native custom hook.
192 auto request_hooks_iter = request_hooks_.find(method_name); 193 auto request_hooks_iter = request_hooks_.find(method_name);
193 if (request_hooks_iter != request_hooks_.end()) { 194 if (request_hooks_iter != request_hooks_.end()) {
194 RequestResult result = 195 RequestResult result =
195 request_hooks_iter->second.Run( 196 request_hooks_iter->second.Run(
196 signature, context, arguments, type_refs); 197 signature, context, arguments, type_refs, return_value);
197 // Right now, it doesn't make sense to register a request handler that 198 // Right now, it doesn't make sense to register a request handler that
198 // doesn't handle the request. 199 // doesn't handle the request.
199 DCHECK_NE(RequestResult::NOT_HANDLED, result); 200 DCHECK_NE(RequestResult::NOT_HANDLED, result);
200 return result; 201 return result;
201 } 202 }
202 203
203 // Harder case: looking up a custom hook registered on the context (since 204 // Harder case: looking up a custom hook registered on the context (since
204 // these are JS, each context has a separate instance). 205 // these are JS, each context has a separate instance).
205 v8::Local<v8::Object> hook_interface_object = 206 v8::Local<v8::Object> hook_interface_object =
206 GetJSHookInterfaceObject(api_name, context, false); 207 GetJSHookInterfaceObject(api_name, context, false);
(...skipping 30 matching lines...) Expand all
237 std::string error; 238 std::string error;
238 bool success = signature->ParseArgumentsToV8(context, *arguments, type_refs, 239 bool success = signature->ParseArgumentsToV8(context, *arguments, type_refs,
239 &parsed_v8_args, &error); 240 &parsed_v8_args, &error);
240 if (try_catch.HasCaught()) { 241 if (try_catch.HasCaught()) {
241 try_catch.ReThrow(); 242 try_catch.ReThrow();
242 return RequestResult::THROWN; 243 return RequestResult::THROWN;
243 } 244 }
244 if (!success) 245 if (!success)
245 return RequestResult::INVALID_INVOCATION; 246 return RequestResult::INVALID_INVOCATION;
246 247
247 run_js_.Run(handle_request, context, parsed_v8_args.size(), 248 v8::Global<v8::Value> global_result =
248 parsed_v8_args.data()); 249 run_js_.Run(handle_request, context, parsed_v8_args.size(),
250 parsed_v8_args.data());
251 if (try_catch.HasCaught()) {
252 try_catch.ReThrow();
253 return RequestResult::THROWN;
254 }
255 if (!global_result.IsEmpty())
256 *return_value = global_result.Get(isolate);
249 return RequestResult::HANDLED; 257 return RequestResult::HANDLED;
250 } 258 }
251 259
252 return RequestResult::NOT_HANDLED; 260 return RequestResult::NOT_HANDLED;
253 } 261 }
254 262
255 void APIBindingHooks::InitializeInContext( 263 void APIBindingHooks::InitializeInContext(
256 v8::Local<v8::Context> context, 264 v8::Local<v8::Context> context,
257 const std::string& api_name) { 265 const std::string& api_name) {
258 if (js_hooks_source_.IsEmpty()) 266 if (js_hooks_source_.IsEmpty())
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 if (result.IsEmpty() || 301 if (result.IsEmpty() ||
294 !gin::Converter<std::vector<v8::Local<v8::Value>>>::FromV8( 302 !gin::Converter<std::vector<v8::Local<v8::Value>>>::FromV8(
295 context->GetIsolate(), result, &new_args)) { 303 context->GetIsolate(), result, &new_args)) {
296 return false; 304 return false;
297 } 305 }
298 arguments->swap(new_args); 306 arguments->swap(new_args);
299 return true; 307 return true;
300 } 308 }
301 309
302 } // namespace extensions 310 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698