Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/binding_generating_native_handler.h" | 5 #include "extensions/renderer/binding_generating_native_handler.h" |
| 6 | 6 |
| 7 #include "extensions/renderer/module_system.h" | 7 #include "extensions/renderer/module_system.h" |
| 8 #include "extensions/renderer/v8_helpers.h" | |
| 8 | 9 |
| 9 namespace extensions { | 10 namespace extensions { |
| 10 | 11 |
| 12 using namespace v8_helpers; | |
| 13 | |
| 11 BindingGeneratingNativeHandler::BindingGeneratingNativeHandler( | 14 BindingGeneratingNativeHandler::BindingGeneratingNativeHandler( |
| 12 ModuleSystem* module_system, | 15 ModuleSystem* module_system, |
| 13 const std::string& api_name, | 16 const std::string& api_name, |
| 14 const std::string& bind_to) | 17 const std::string& bind_to) |
| 15 : module_system_(module_system), api_name_(api_name), bind_to_(bind_to) {} | 18 : module_system_(module_system), api_name_(api_name), bind_to_(bind_to) {} |
| 16 | 19 |
| 17 v8::Local<v8::Object> BindingGeneratingNativeHandler::NewInstance() { | 20 v8::MaybeLocal<v8::Object> BindingGeneratingNativeHandler::NewInstance() { |
|
not at google - send to devlin
2015/06/19 17:23:54
NewInstance doesn't run any third-party code so it
bashi
2015/06/23 00:10:49
Done.
| |
| 18 v8::Isolate* isolate = module_system_->GetIsolate(); | 21 v8::Isolate* isolate = module_system_->GetIsolate(); |
| 19 v8::EscapableHandleScope scope(isolate); | 22 v8::EscapableHandleScope scope(isolate); |
| 20 v8::Local<v8::Object> binding_module = | 23 |
| 21 module_system_->Require("binding")->ToObject(isolate); | 24 v8::Local<v8::String> v8_api_name; |
| 22 v8::Local<v8::Object> binding = | 25 v8::Local<v8::String> v8_bind_to; |
| 23 binding_module->Get(v8::String::NewFromUtf8(isolate, "Binding")) | 26 if (!ToV8String(isolate, api_name_.c_str(), &v8_api_name) || |
| 24 ->ToObject(isolate); | 27 !ToV8String(isolate, bind_to_.c_str(), &v8_bind_to)) |
| 28 return v8::MaybeLocal<v8::Object>(); | |
| 29 | |
| 30 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | |
|
not at google - send to devlin
2015/06/19 17:23:54
Use the context() property rather than the current
bashi
2015/06/23 00:10:49
BindingGeneratingNativeHandler doesn't have contex
not at google - send to devlin
2015/06/23 00:36:48
Ah right. It should be pretty straightforward to c
bashi
2015/06/23 00:49:41
Done. Thanks for the suggestion!
| |
| 31 v8::Local<v8::Object> binding_module; | |
| 32 if (!module_system_->Require("binding")->ToObject(context).ToLocal( | |
| 33 &binding_module)) | |
| 34 return v8::MaybeLocal<v8::Object>(); | |
| 35 | |
| 36 v8::Local<v8::Value> binding_value; | |
| 37 v8::Local<v8::Object> binding; | |
| 38 if (!GetProperty(context, binding_module, "Binding", &binding_value) || | |
| 39 !binding_value->ToObject(context).ToLocal(&binding)) | |
| 40 return v8::MaybeLocal<v8::Object>(); | |
| 41 | |
| 42 v8::Local<v8::Value> create_binding_value; | |
| 43 if (!GetProperty(context, binding, "create", &create_binding_value) || | |
| 44 !create_binding_value->IsFunction()) | |
| 45 return v8::MaybeLocal<v8::Object>(); | |
| 25 v8::Local<v8::Function> create_binding = | 46 v8::Local<v8::Function> create_binding = |
| 26 binding->Get(v8::String::NewFromUtf8(isolate, "create")) | 47 create_binding_value.As<v8::Function>(); |
| 27 .As<v8::Function>(); | 48 |
| 28 v8::Local<v8::Value> argv[] = { | 49 v8::Local<v8::Value> argv[] = {v8_api_name}; |
| 29 v8::String::NewFromUtf8(isolate, api_name_.c_str())}; | 50 v8::Local<v8::Value> binding_instance_value; |
| 30 v8::Local<v8::Object> binding_instance = | 51 v8::Local<v8::Object> binding_instance; |
| 31 create_binding->Call(binding, arraysize(argv), argv)->ToObject(isolate); | 52 if (!CallFunction(context, create_binding, binding, arraysize(argv), argv, |
| 32 v8::Local<v8::Function> generate = | 53 &binding_instance_value) || |
| 33 binding_instance->Get(v8::String::NewFromUtf8(isolate, "generate")) | 54 !binding_instance_value->ToObject(context).ToLocal(&binding_instance)) |
| 34 .As<v8::Function>(); | 55 return v8::MaybeLocal<v8::Object>(); |
| 56 | |
| 57 v8::Local<v8::Value> generate_value; | |
| 58 if (!GetProperty(context, binding_instance, "generate", &generate_value) || | |
| 59 !generate_value->IsFunction()) | |
| 60 return v8::MaybeLocal<v8::Object>(); | |
| 61 v8::Local<v8::Function> generate = generate_value.As<v8::Function>(); | |
| 62 | |
| 35 v8::Local<v8::Object> object = v8::Object::New(isolate); | 63 v8::Local<v8::Object> object = v8::Object::New(isolate); |
| 36 v8::Local<v8::Value> compiled_schema = | 64 v8::Local<v8::Value> compiled_schema; |
| 37 generate->Call(binding_instance, 0, NULL); | 65 if (!CallFunction(context, generate, binding_instance, 0, nullptr, |
| 38 if (!compiled_schema.IsEmpty()) { | 66 &compiled_schema)) |
| 39 object->Set(v8::String::NewFromUtf8(isolate, bind_to_.c_str()), | 67 return v8::MaybeLocal<v8::Object>(); |
| 40 compiled_schema); | 68 |
| 41 } | 69 if (!SetProperty(context, object, v8_bind_to, compiled_schema)) |
| 70 return v8::MaybeLocal<v8::Object>(); | |
| 42 return scope.Escape(object); | 71 return scope.Escape(object); |
| 43 } | 72 } |
| 44 | 73 |
| 45 } // namespace extensions | 74 } // namespace extensions |
| OLD | NEW |