| OLD | NEW |
| 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.h" | 5 #include "extensions/renderer/api_binding.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 base::StringPrintf("%s.%s", api_name_.c_str(), event_name.c_str()); | 283 base::StringPrintf("%s.%s", api_name_.c_str(), event_name.c_str()); |
| 284 v8::Local<v8::Object> event = | 284 v8::Local<v8::Object> event = |
| 285 event_handler->CreateEventInstance(full_event_name, context); | 285 event_handler->CreateEventInstance(full_event_name, context); |
| 286 DCHECK(!event.IsEmpty()); | 286 DCHECK(!event.IsEmpty()); |
| 287 v8::Maybe<bool> success = object->CreateDataProperty( | 287 v8::Maybe<bool> success = object->CreateDataProperty( |
| 288 context, gin::StringToSymbol(isolate, event_name), event); | 288 context, gin::StringToSymbol(isolate, event_name), event); |
| 289 DCHECK(success.IsJust()); | 289 DCHECK(success.IsJust()); |
| 290 DCHECK(success.FromJust()); | 290 DCHECK(success.FromJust()); |
| 291 } | 291 } |
| 292 | 292 |
| 293 if (binding_hooks_) |
| 294 binding_hooks_->InitializeInContext(context, api_name_); |
| 295 |
| 293 return object; | 296 return object; |
| 294 } | 297 } |
| 295 | 298 |
| 296 void APIBinding::HandleCall(const std::string& name, | 299 void APIBinding::HandleCall(const std::string& name, |
| 297 const binding::APISignature* signature, | 300 const binding::APISignature* signature, |
| 298 gin::Arguments* arguments) { | 301 gin::Arguments* arguments) { |
| 299 std::string error; | 302 std::string error; |
| 300 v8::Isolate* isolate = arguments->isolate(); | 303 v8::Isolate* isolate = arguments->isolate(); |
| 301 v8::HandleScope handle_scope(isolate); | 304 v8::HandleScope handle_scope(isolate); |
| 302 | 305 |
| 303 if (binding_hooks_) { | 306 // Since this is called synchronously from the JS entry point, |
| 304 // Check for a custom hook to handle the method. | 307 // GetCurrentContext() should always be correct. |
| 305 APIBindingHooks::HandleRequestHook handler = | 308 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
| 306 binding_hooks_->GetHandleRequest(name); | 309 |
| 307 if (!handler.is_null()) { | 310 // Check for a custom hook to handle the method. |
| 308 handler.Run(signature, arguments); | 311 if (binding_hooks_ && |
| 309 return; | 312 binding_hooks_->HandleRequest(api_name_, name, context, |
| 310 } | 313 signature, arguments)) { |
| 314 return; // Handled by a custom hook. |
| 311 } | 315 } |
| 312 | 316 |
| 313 std::unique_ptr<base::ListValue> parsed_arguments; | 317 std::unique_ptr<base::ListValue> parsed_arguments; |
| 314 v8::Local<v8::Function> callback; | 318 v8::Local<v8::Function> callback; |
| 315 { | 319 { |
| 316 v8::TryCatch try_catch(isolate); | 320 v8::TryCatch try_catch(isolate); |
| 317 parsed_arguments = | 321 parsed_arguments = |
| 318 ParseArguments(signature, arguments, *type_refs_, &callback, &error); | 322 ParseArguments(signature, arguments, *type_refs_, &callback, &error); |
| 319 if (try_catch.HasCaught()) { | 323 if (try_catch.HasCaught()) { |
| 320 DCHECK(!parsed_arguments); | 324 DCHECK(!parsed_arguments); |
| 321 try_catch.ReThrow(); | 325 try_catch.ReThrow(); |
| 322 return; | 326 return; |
| 323 } | 327 } |
| 324 } | 328 } |
| 325 if (!parsed_arguments) { | 329 if (!parsed_arguments) { |
| 326 arguments->ThrowTypeError("Invalid invocation"); | 330 arguments->ThrowTypeError("Invalid invocation"); |
| 327 return; | 331 return; |
| 328 } | 332 } |
| 329 | 333 |
| 330 // Since this is called synchronously from the JS entry point, | |
| 331 // GetCurrentContext() should always be correct. | |
| 332 method_callback_.Run(name, std::move(parsed_arguments), isolate, | 334 method_callback_.Run(name, std::move(parsed_arguments), isolate, |
| 333 isolate->GetCurrentContext(), callback); | 335 context, callback); |
| 334 } | 336 } |
| 335 | 337 |
| 336 } // namespace extensions | 338 } // namespace extensions |
| OLD | NEW |