Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: extensions/renderer/binding_generating_native_handler.cc

Issue 1192763002: extensions: Use V8 Maybe APIs in NativeHandler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 v8::Local<v8::Object> binding_module;
46 // require('binding');
not at google - send to devlin 2015/06/24 00:34:56 I would have preferred for each of these comments
bashi 2015/06/24 03:30:44 Done.
47 if (!context_->module_system()->Require("binding")
48 ->ToObject(v8_context)
49 .ToLocal(&binding_module)) {
50 NOTREACHED();
51 return v8::Local<v8::Object>();
52 }
53
54 v8::Local<v8::Value> binding_value;
55 v8::Local<v8::Object> binding;
56 // require('binding').Binding;
57 if (!GetProperty(v8_context, binding_module, "Binding", &binding_value) ||
58 !binding_value->ToObject(v8_context).ToLocal(&binding)) {
59 NOTREACHED();
60 return v8::Local<v8::Object>();
61 }
62
63 v8::Local<v8::Value> create_binding_value;
64 // require('Binding').Binding.create;
65 if (!GetProperty(v8_context, binding, "create", &create_binding_value) ||
66 !create_binding_value->IsFunction()) {
67 NOTREACHED();
68 return v8::Local<v8::Object>();
69 }
25 v8::Local<v8::Function> create_binding = 70 v8::Local<v8::Function> create_binding =
26 binding->Get(v8::String::NewFromUtf8(isolate, "create")) 71 create_binding_value.As<v8::Function>();
27 .As<v8::Function>(); 72
28 v8::Local<v8::Value> argv[] = { 73 v8::Local<v8::Value> argv[] = {v8_api_name};
29 v8::String::NewFromUtf8(isolate, api_name_.c_str())}; 74 v8::Local<v8::Value> binding_instance_value;
30 v8::Local<v8::Object> binding_instance = 75 v8::Local<v8::Object> binding_instance;
31 create_binding->Call(binding, arraysize(argv), argv)->ToObject(isolate); 76 // require('Binding').Binding.create(api_name);
32 v8::Local<v8::Function> generate = 77 if (!CallFunction(v8_context, create_binding, binding, arraysize(argv), argv,
33 binding_instance->Get(v8::String::NewFromUtf8(isolate, "generate")) 78 &binding_instance_value) ||
34 .As<v8::Function>(); 79 !binding_instance_value->ToObject(v8_context)
80 .ToLocal(&binding_instance)) {
81 NOTREACHED();
82 return v8::Local<v8::Object>();
83 }
84
85 v8::Local<v8::Value> generate_value;
86 // require('Binding').Binding.create(api_name).generate;
87 if (!GetProperty(v8_context, binding_instance, "generate", &generate_value) ||
88 !generate_value->IsFunction()) {
89 NOTREACHED();
90 return v8::Local<v8::Object>();
91 }
92 v8::Local<v8::Function> generate = generate_value.As<v8::Function>();
93
35 v8::Local<v8::Object> object = v8::Object::New(isolate); 94 v8::Local<v8::Object> object = v8::Object::New(isolate);
36 v8::Local<v8::Value> compiled_schema = 95 v8::Local<v8::Value> compiled_schema;
37 generate->Call(binding_instance, 0, NULL); 96 // require('Binding').Binding.create(api_name).generate();
38 if (!compiled_schema.IsEmpty()) { 97 if (!CallFunction(v8_context, generate, binding_instance, 0, nullptr,
39 object->Set(v8::String::NewFromUtf8(isolate, bind_to_.c_str()), 98 &compiled_schema)) {
40 compiled_schema); 99 NOTREACHED();
100 return v8::Local<v8::Object>();
41 } 101 }
102
103 // var result = {};
104 // result[bind_to] = ...;
105 if (!SetProperty(v8_context, object, v8_bind_to, compiled_schema)) {
106 NOTREACHED();
107 return v8::Local<v8::Object>();
108 }
109 // return result;
42 return scope.Escape(object); 110 return scope.Escape(object);
43 } 111 }
44 112
45 } // namespace extensions 113 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/renderer/binding_generating_native_handler.h ('k') | extensions/renderer/dispatcher.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698