Expose builtin v8 modules to AtomSandboxedRendererClient

- Adapt node.cc code that implements `process.binding` to create a similar
  object in AtomSandboxedRendererClient.
- Replace the ipc binding object passed to `lib/sandboxed_renderer/init.js` by
  the new binding object.
- Refactor the initialization script to use this new object to fetch the ipc
  binding and store as a hidden value using the `v8_util` module.

This change also required applying a patch to node.js, so the submodule commit
was updated.
This commit is contained in:
Thiago de Arruda 2017-02-20 10:59:39 -03:00
parent 85d66d2413
commit d78f3cae7b
7 changed files with 89 additions and 31 deletions

View file

@ -11,6 +11,7 @@
#include "atom/common/api/api_messages.h"
#include "atom/common/native_mate_converters/string16_converter.h"
#include "atom/common/native_mate_converters/value_converter.h"
#include "atom/common/node_includes.h"
#include "atom/common/options_switches.h"
#include "atom/renderer/api/atom_api_renderer_ipc.h"
#include "atom/renderer/atom_render_view_observer.h"
@ -21,6 +22,8 @@
#include "content/public/renderer/render_view.h"
#include "content/public/renderer/render_view_observer.h"
#include "ipc/ipc_message_macros.h"
#include "native_mate/converter.h"
#include "native_mate/dictionary.h"
#include "third_party/WebKit/public/web/WebFrame.h"
#include "third_party/WebKit/public/web/WebKit.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h"
@ -31,7 +34,57 @@ namespace atom {
namespace {
const std::string kBindingKey = "binding";
const std::string kIpcKey = "ipc";
const std::string kModuleCacheKey = "native-module-cache";
v8::Local<v8::Object> GetModuleCache(v8::Isolate* isolate) {
mate::Dictionary global(isolate, isolate->GetCurrentContext()->Global());
v8::Local<v8::Value> cache;
if (!global.GetHidden(kModuleCacheKey, &cache)) {
cache = v8::Object::New(isolate);
global.SetHidden(kModuleCacheKey, cache);
}
return cache->ToObject();
}
// adapted from node.cc
v8::Local<v8::Value> GetBinding(v8::Isolate* isolate, v8::Local<v8::String> key,
mate::Arguments* margs) {
v8::Local<v8::Object> exports;
std::string module_key = mate::V8ToString(key);
mate::Dictionary cache(isolate, GetModuleCache(isolate));
if (cache.Get(module_key.c_str(), &exports)) {
return exports;
}
auto mod = node::get_builtin_module(module_key.c_str());
if (!mod) {
char errmsg[1024];
snprintf(errmsg, sizeof(errmsg), "No such module: %s", module_key.c_str());
margs->ThrowError(errmsg);
return exports;
}
exports = v8::Object::New(isolate);
DCHECK_EQ(mod->nm_register_func, nullptr);
DCHECK_NE(mod->nm_context_register_func, nullptr);
mod->nm_context_register_func(exports, v8::Null(isolate),
isolate->GetCurrentContext(), mod->nm_priv);
cache.Set(module_key.c_str(), exports);
return exports;
}
void InitializeBindings(v8::Local<v8::Object> binding,
v8::Local<v8::Context> context) {
auto isolate = context->GetIsolate();
mate::Dictionary b(isolate, binding);
b.SetMethod("get", GetBinding);
}
class AtomSandboxedRenderFrameObserver : public content::RenderFrameObserver {
public:
@ -100,7 +153,7 @@ class AtomSandboxedRenderViewObserver : public AtomRenderViewObserver {
mate::ConvertToV8(isolate, channel),
mate::ConvertToV8(isolate, args)
};
renderer_client_->InvokeBindingCallback(
renderer_client_->InvokeIpcCallback(
context,
"onMessage",
std::vector<v8::Local<v8::Value>>(argv, argv + 2));
@ -158,17 +211,13 @@ void AtomSandboxedRendererClient::DidCreateScriptContext(
script->Run(context).ToLocalChecked());
// Create and initialize the binding object
auto binding = v8::Object::New(isolate);
api::Initialize(binding, v8::Null(isolate), context, nullptr);
InitializeBindings(binding, context);
v8::Local<v8::Value> args[] = {
binding,
mate::ConvertToV8(isolate, preload_script)
};
// Execute the function with proper arguments
ignore_result(func->Call(context, v8::Null(isolate), 2, args));
// Store the bindingt privately for handling messages from the main process.
auto binding_key = mate::ConvertToV8(isolate, kBindingKey)->ToString();
auto private_binding_key = v8::Private::ForApi(isolate, binding_key);
context->Global()->SetPrivate(context, private_binding_key, binding);
}
void AtomSandboxedRendererClient::WillReleaseScriptContext(
@ -176,15 +225,15 @@ void AtomSandboxedRendererClient::WillReleaseScriptContext(
auto isolate = context->GetIsolate();
v8::HandleScope handle_scope(isolate);
v8::Context::Scope context_scope(context);
InvokeBindingCallback(context, "onExit", std::vector<v8::Local<v8::Value>>());
InvokeIpcCallback(context, "onExit", std::vector<v8::Local<v8::Value>>());
}
void AtomSandboxedRendererClient::InvokeBindingCallback(
void AtomSandboxedRendererClient::InvokeIpcCallback(
v8::Handle<v8::Context> context,
std::string callback_name,
std::vector<v8::Handle<v8::Value>> args) {
auto isolate = context->GetIsolate();
auto binding_key = mate::ConvertToV8(isolate, kBindingKey)->ToString();
auto binding_key = mate::ConvertToV8(isolate, kIpcKey)->ToString();
auto private_binding_key = v8::Private::ForApi(isolate, binding_key);
auto global_object = context->Global();
v8::Local<v8::Value> value;

View file

@ -21,7 +21,7 @@ class AtomSandboxedRendererClient : public content::ContentRendererClient {
v8::Handle<v8::Context> context, content::RenderFrame* render_frame);
void WillReleaseScriptContext(
v8::Handle<v8::Context> context, content::RenderFrame* render_frame);
void InvokeBindingCallback(v8::Handle<v8::Context> context,
void InvokeIpcCallback(v8::Handle<v8::Context> context,
std::string callback_name,
std::vector<v8::Handle<v8::Value>> args);
// content::ContentRendererClient:

View file

@ -51,6 +51,7 @@
'lib/common/api/module-list.js',
'lib/common/api/native-image.js',
'lib/common/api/shell.js',
'lib/common/atom-binding-setup.js',
'lib/common/init.js',
'lib/common/parse-features-string.js',
'lib/common/reset-search-paths.js',

View file

@ -0,0 +1,13 @@
module.exports = function atomBindingSetup (binding, processType) {
return function atomBinding (name) {
try {
return binding(`atom_${processType}_${name}`)
} catch (error) {
if (/No such module/.test(error.message)) {
return binding(`atom_common_${name}`)
} else {
throw error
}
}
}
}

View file

@ -1,18 +1,6 @@
const timers = require('timers')
const {binding} = process
process.atomBinding = function (name) {
try {
return binding(`atom_${process.type}_${name}`)
} catch (error) {
if (/No such module/.test(error.message)) {
return binding(`atom_common_${name}`)
} else {
throw error
}
}
}
process.atomBinding = require('./atom-binding-setup')(process.binding, process.type)
// setImmediate and process.nextTick makes use of uv_check and uv_prepare to
// run the callbacks, however since we only run uv loop on requests, the

View file

@ -2,25 +2,32 @@
// in filenames.gypi so they get built into the preload_bundle.js bundle
/* eslint no-eval: "off" */
/* global binding, preloadPath, process, Buffer */
/* global binding, preloadPath, Buffer */
const events = require('events')
const atomBinding = require('../common/atom-binding-setup')(binding.get, 'renderer')
const v8Util = atomBinding('v8_util')
const ipc = atomBinding('ipc')
const ipcRenderer = new events.EventEmitter()
const proc = new events.EventEmitter()
// eval in window scope:
// http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.2
const geval = eval
require('../renderer/api/ipc-renderer-setup')(ipcRenderer, binding)
require('../renderer/api/ipc-renderer-setup')(ipcRenderer, ipc)
binding.onMessage = function (channel, args) {
ipc.onMessage = function (channel, args) {
ipcRenderer.emit(channel, ...args)
}
binding.onExit = function () {
ipc.onExit = function () {
proc.emit('exit')
}
v8Util.setHiddenValue(global, 'ipc', ipc)
const preloadModules = new Map([
['electron', {
ipcRenderer: ipcRenderer

2
vendor/node vendored

@ -1 +1 @@
Subproject commit 0f84d972a1b48b7da361f9717ff43349a7946abd
Subproject commit a6663598aa78832e7955cb93c51a098eac787abb