| 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_maybe_helpers.h" |
| 8 | 9 |
| 9 namespace extensions { | 10 namespace extensions { |
| 10 | 11 |
| 11 BindingGeneratingNativeHandler::BindingGeneratingNativeHandler( | 12 BindingGeneratingNativeHandler::BindingGeneratingNativeHandler( |
| 12 ModuleSystem* module_system, | 13 ModuleSystem* module_system, |
| 13 const std::string& api_name, | 14 const std::string& api_name, |
| 14 const std::string& bind_to) | 15 const std::string& bind_to) |
| 15 : module_system_(module_system), api_name_(api_name), bind_to_(bind_to) {} | 16 : module_system_(module_system), api_name_(api_name), bind_to_(bind_to) {} |
| 16 | 17 |
| 17 v8::Local<v8::Object> BindingGeneratingNativeHandler::NewInstance() { | 18 v8::MaybeLocal<v8::Object> BindingGeneratingNativeHandler::NewInstance() { |
| 19 if (api_name_.size() >= v8::String::kMaxLength || |
| 20 bind_to_.size() >= v8::String::kMaxLength) |
| 21 return v8::MaybeLocal<v8::Object>(); |
| 22 |
| 18 v8::Isolate* isolate = module_system_->GetIsolate(); | 23 v8::Isolate* isolate = module_system_->GetIsolate(); |
| 19 v8::EscapableHandleScope scope(isolate); | 24 v8::EscapableHandleScope scope(isolate); |
| 20 v8::Local<v8::Object> binding_module = | 25 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
| 21 module_system_->Require("binding")->ToObject(isolate); | 26 v8::Local<v8::Object> binding_module; |
| 22 v8::Local<v8::Object> binding = | 27 if (!module_system_->Require("binding")->ToObject(context).ToLocal( |
| 23 binding_module->Get(v8::String::NewFromUtf8(isolate, "Binding")) | 28 &binding_module)) |
| 24 ->ToObject(isolate); | 29 return v8::MaybeLocal<v8::Object>(); |
| 30 |
| 31 v8::Local<v8::Value> binding_value; |
| 32 v8::Local<v8::Object> binding; |
| 33 if (!binding_module->Get(context, ToV8String(isolate, "Binding")) |
| 34 .ToLocal(&binding_value) || |
| 35 !binding_value->ToObject(context).ToLocal(&binding)) |
| 36 return v8::MaybeLocal<v8::Object>(); |
| 37 |
| 38 v8::Local<v8::Value> create_binding_value; |
| 39 if (!binding->Get(context, ToV8String(isolate, "create")) |
| 40 .ToLocal(&create_binding_value) || |
| 41 !create_binding_value->IsFunction()) |
| 42 return v8::MaybeLocal<v8::Object>(); |
| 25 v8::Local<v8::Function> create_binding = | 43 v8::Local<v8::Function> create_binding = |
| 26 binding->Get(v8::String::NewFromUtf8(isolate, "create")) | 44 create_binding_value.As<v8::Function>(); |
| 27 .As<v8::Function>(); | 45 |
| 28 v8::Local<v8::Value> argv[] = { | 46 v8::Local<v8::Value> argv[] = {ToV8String(isolate, api_name_.c_str())}; |
| 29 v8::String::NewFromUtf8(isolate, api_name_.c_str())}; | 47 v8::Local<v8::Value> binding_instance_value; |
| 30 v8::Local<v8::Object> binding_instance = | 48 v8::Local<v8::Object> binding_instance; |
| 31 create_binding->Call(binding, arraysize(argv), argv)->ToObject(isolate); | 49 if (!create_binding->Call(context, binding, arraysize(argv), argv) |
| 32 v8::Local<v8::Function> generate = | 50 .ToLocal(&binding_instance_value) || |
| 33 binding_instance->Get(v8::String::NewFromUtf8(isolate, "generate")) | 51 !binding_instance_value->ToObject(context).ToLocal(&binding_instance)) |
| 34 .As<v8::Function>(); | 52 return v8::MaybeLocal<v8::Object>(); |
| 53 |
| 54 v8::Local<v8::Value> generate_value; |
| 55 if (!binding_instance->Get(context, ToV8String(isolate, "generate")) |
| 56 .ToLocal(&generate_value) || |
| 57 !generate_value->IsFunction()) |
| 58 return v8::MaybeLocal<v8::Object>(); |
| 59 v8::Local<v8::Function> generate = generate_value.As<v8::Function>(); |
| 60 |
| 35 v8::Local<v8::Object> object = v8::Object::New(isolate); | 61 v8::Local<v8::Object> object = v8::Object::New(isolate); |
| 36 v8::Local<v8::Value> compiled_schema = | 62 v8::Local<v8::Value> compiled_schema; |
| 37 generate->Call(binding_instance, 0, NULL); | 63 if (!generate->Call(context, binding_instance, 0, NULL) |
| 38 if (!compiled_schema.IsEmpty()) { | 64 .ToLocal(&compiled_schema)) |
| 39 object->Set(v8::String::NewFromUtf8(isolate, bind_to_.c_str()), | 65 return v8::MaybeLocal<v8::Object>(); |
| 40 compiled_schema); | 66 |
| 41 } | 67 SetProperty(context, object, ToV8String(isolate, bind_to_.c_str()), |
| 68 compiled_schema); |
| 42 return scope.Escape(object); | 69 return scope.Escape(object); |
| 43 } | 70 } |
| 44 | 71 |
| 45 } // namespace extensions | 72 } // namespace extensions |
| OLD | NEW |