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/script_context.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 ScriptContext* context, |
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 : context_(context), api_name_(api_name), bind_to_(bind_to) {} |
16 | 19 |
17 v8::Local<v8::Object> BindingGeneratingNativeHandler::NewInstance() { | 20 v8::Local<v8::Object> BindingGeneratingNativeHandler::NewInstance() { |
18 v8::Isolate* isolate = module_system_->GetIsolate(); | 21 // This long sequence of commands effectively runs the JavaScript code, |
| 22 // such that result[bind_to] is the compiled schema for |api_name|: |
| 23 // |
| 24 // var result = {}; |
| 25 // result[bind_to] = require('binding').Binding.create(api_name).generate(); |
| 26 // return result; |
| 27 // |
| 28 // Unfortunately using the v8 APIs makes that quite verbose. |
| 29 // Each stage is marked with the code it executes. |
| 30 v8::Isolate* isolate = context_->isolate(); |
19 v8::EscapableHandleScope scope(isolate); | 31 v8::EscapableHandleScope scope(isolate); |
20 v8::Local<v8::Object> binding_module = | 32 |
21 module_system_->Require("binding")->ToObject(isolate); | 33 // Convert |api_name| and |bind_to| into their v8::Strings to pass |
22 v8::Local<v8::Object> binding = | 34 // through the v8 APIs. |
23 binding_module->Get(v8::String::NewFromUtf8(isolate, "Binding")) | 35 v8::Local<v8::String> v8_api_name; |
24 ->ToObject(isolate); | 36 v8::Local<v8::String> v8_bind_to; |
| 37 if (!ToV8String(isolate, api_name_, &v8_api_name) || |
| 38 !ToV8String(isolate, bind_to_, &v8_bind_to)) { |
| 39 NOTREACHED(); |
| 40 return v8::Local<v8::Object>(); |
| 41 } |
| 42 |
| 43 v8::Local<v8::Context> v8_context = context_->v8_context(); |
| 44 |
| 45 // require('binding'); |
| 46 v8::Local<v8::Object> binding_module; |
| 47 if (!context_->module_system()->Require("binding").ToLocal(&binding_module)) { |
| 48 NOTREACHED(); |
| 49 return v8::Local<v8::Object>(); |
| 50 } |
| 51 |
| 52 // require('binding').Binding; |
| 53 v8::Local<v8::Value> binding_value; |
| 54 v8::Local<v8::Object> binding; |
| 55 if (!GetProperty(v8_context, binding_module, "Binding", &binding_value) || |
| 56 !binding_value->ToObject(v8_context).ToLocal(&binding)) { |
| 57 NOTREACHED(); |
| 58 return v8::Local<v8::Object>(); |
| 59 } |
| 60 |
| 61 // require('binding').Binding.create; |
| 62 v8::Local<v8::Value> create_binding_value; |
| 63 if (!GetProperty(v8_context, binding, "create", &create_binding_value) || |
| 64 !create_binding_value->IsFunction()) { |
| 65 NOTREACHED(); |
| 66 return v8::Local<v8::Object>(); |
| 67 } |
25 v8::Local<v8::Function> create_binding = | 68 v8::Local<v8::Function> create_binding = |
26 binding->Get(v8::String::NewFromUtf8(isolate, "create")) | 69 create_binding_value.As<v8::Function>(); |
27 .As<v8::Function>(); | 70 |
28 v8::Local<v8::Value> argv[] = { | 71 // require('Binding').Binding.create(api_name); |
29 v8::String::NewFromUtf8(isolate, api_name_.c_str())}; | 72 v8::Local<v8::Value> argv[] = {v8_api_name}; |
30 v8::Local<v8::Object> binding_instance = | 73 v8::Local<v8::Value> binding_instance_value; |
31 create_binding->Call(binding, arraysize(argv), argv)->ToObject(isolate); | 74 v8::Local<v8::Object> binding_instance; |
32 v8::Local<v8::Function> generate = | 75 if (!CallFunction(v8_context, create_binding, binding, arraysize(argv), argv, |
33 binding_instance->Get(v8::String::NewFromUtf8(isolate, "generate")) | 76 &binding_instance_value) || |
34 .As<v8::Function>(); | 77 !binding_instance_value->ToObject(v8_context) |
| 78 .ToLocal(&binding_instance)) { |
| 79 NOTREACHED(); |
| 80 return v8::Local<v8::Object>(); |
| 81 } |
| 82 |
| 83 // require('binding').Binding.create(api_name).generate; |
| 84 v8::Local<v8::Value> generate_value; |
| 85 if (!GetProperty(v8_context, binding_instance, "generate", &generate_value) || |
| 86 !generate_value->IsFunction()) { |
| 87 NOTREACHED(); |
| 88 return v8::Local<v8::Object>(); |
| 89 } |
| 90 v8::Local<v8::Function> generate = generate_value.As<v8::Function>(); |
| 91 |
| 92 // require('binding').Binding.create(api_name).generate(); |
35 v8::Local<v8::Object> object = v8::Object::New(isolate); | 93 v8::Local<v8::Object> object = v8::Object::New(isolate); |
36 v8::Local<v8::Value> compiled_schema = | 94 v8::Local<v8::Value> compiled_schema; |
37 generate->Call(binding_instance, 0, NULL); | 95 if (!CallFunction(v8_context, generate, binding_instance, 0, nullptr, |
38 if (!compiled_schema.IsEmpty()) { | 96 &compiled_schema)) { |
39 object->Set(v8::String::NewFromUtf8(isolate, bind_to_.c_str()), | 97 NOTREACHED(); |
40 compiled_schema); | 98 return v8::Local<v8::Object>(); |
41 } | 99 } |
| 100 |
| 101 // var result = {}; |
| 102 // result[bind_to] = ...; |
| 103 if (!SetProperty(v8_context, object, v8_bind_to, compiled_schema)) { |
| 104 NOTREACHED(); |
| 105 return v8::Local<v8::Object>(); |
| 106 } |
| 107 // return result; |
42 return scope.Escape(object); | 108 return scope.Escape(object); |
43 } | 109 } |
44 | 110 |
45 } // namespace extensions | 111 } // namespace extensions |
OLD | NEW |