10529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
50529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "extensions/renderer/binding_generating_native_handler.h"
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "extensions/renderer/module_system.h"
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace extensions {
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BindingGeneratingNativeHandler::BindingGeneratingNativeHandler(
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ModuleSystem* module_system,
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& api_name,
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& bind_to)
150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    : module_system_(module_system), api_name_(api_name), bind_to_(bind_to) {}
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)v8::Handle<v8::Object> BindingGeneratingNativeHandler::NewInstance() {
18a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  v8::Isolate* isolate = module_system_->GetIsolate();
19a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  v8::EscapableHandleScope scope(isolate);
207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  v8::Handle<v8::Object> binding_module =
217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      module_system_->Require("binding")->ToObject();
220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  v8::Handle<v8::Object> binding =
230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      binding_module->Get(v8::String::NewFromUtf8(isolate, "Binding"))
240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          ->ToObject();
250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  v8::Handle<v8::Function> create_binding =
260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      binding->Get(v8::String::NewFromUtf8(isolate, "create"))
270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          .As<v8::Function>();
280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  v8::Handle<v8::Value> argv[] = {
290529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      v8::String::NewFromUtf8(isolate, api_name_.c_str())};
307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  v8::Handle<v8::Object> binding_instance =
317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      create_binding->Call(binding, arraysize(argv), argv)->ToObject();
320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  v8::Handle<v8::Function> generate =
330529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      binding_instance->Get(v8::String::NewFromUtf8(isolate, "generate"))
340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          .As<v8::Function>();
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  v8::Local<v8::Object> object = v8::Object::New(isolate);
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  v8::Handle<v8::Value> compiled_schema =
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      generate->Call(binding_instance, 0, NULL);
38a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (!compiled_schema.IsEmpty()) {
39a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    object->Set(v8::String::NewFromUtf8(isolate, bind_to_.c_str()),
40a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                compiled_schema);
41a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
42a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return scope.Escape(object);
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}  // namespace extensions
46