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 3 // found in the LICENSE file. 4 5 #include "extensions/renderer/binding_generating_native_handler.h" 6 7 #include "extensions/renderer/module_system.h" 8 9 namespace extensions { 10 11 BindingGeneratingNativeHandler::BindingGeneratingNativeHandler( 12 ModuleSystem* module_system, 13 const std::string& api_name, 14 const std::string& bind_to) 15 : module_system_(module_system), api_name_(api_name), bind_to_(bind_to) {} 16 17 v8::Handle<v8::Object> BindingGeneratingNativeHandler::NewInstance() { 18 v8::Isolate* isolate = module_system_->GetIsolate(); 19 v8::EscapableHandleScope scope(isolate); 20 v8::Handle<v8::Object> binding_module = 21 module_system_->Require("binding")->ToObject(); 22 v8::Handle<v8::Object> binding = 23 binding_module->Get(v8::String::NewFromUtf8(isolate, "Binding")) 24 ->ToObject(); 25 v8::Handle<v8::Function> create_binding = 26 binding->Get(v8::String::NewFromUtf8(isolate, "create")) 27 .As<v8::Function>(); 28 v8::Handle<v8::Value> argv[] = { 29 v8::String::NewFromUtf8(isolate, api_name_.c_str())}; 30 v8::Handle<v8::Object> binding_instance = 31 create_binding->Call(binding, arraysize(argv), argv)->ToObject(); 32 v8::Handle<v8::Function> generate = 33 binding_instance->Get(v8::String::NewFromUtf8(isolate, "generate")) 34 .As<v8::Function>(); 35 v8::Local<v8::Object> object = v8::Object::New(isolate); 36 v8::Handle<v8::Value> compiled_schema = 37 generate->Call(binding_instance, 0, NULL); 38 if (!compiled_schema.IsEmpty()) { 39 object->Set(v8::String::NewFromUtf8(isolate, bind_to_.c_str()), 40 compiled_schema); 41 } 42 return scope.Escape(object); 43 } 44 45 } // namespace extensions 46