Embed setup bundle for preload scripts in sandboxed renderers.
Add a gyp target that creates a browserify bundle starting with `lib/sandboxed_renderer/init.js`, which is embedded into the executable using the `atom_js2c` target. The goal of this bundle is to provide a very basic environment for preload scripts where a `require` function is available.
This commit is contained in:
parent
695509e267
commit
72154b64ed
6 changed files with 101 additions and 4 deletions
34
electron.gyp
34
electron.gyp
|
@ -423,11 +423,44 @@
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}, # target atom_js2c_copy
|
}, # target atom_js2c_copy
|
||||||
|
{
|
||||||
|
'target_name': 'atom_browserify',
|
||||||
|
'type': 'none',
|
||||||
|
'dependencies': [
|
||||||
|
# depend on this target to ensure the '<(js2c_input_dir)' is created
|
||||||
|
'atom_js2c_copy',
|
||||||
|
],
|
||||||
|
'actions': [
|
||||||
|
{
|
||||||
|
'action_name': 'atom_browserify',
|
||||||
|
'inputs': [
|
||||||
|
'<@(browserify_entries)',
|
||||||
|
# Any js file under `lib/` can be included in the preload bundle.
|
||||||
|
# Add all js sources as dependencies so any change to a js file will
|
||||||
|
# trigger a rebuild of the bundle(and consequently of js2c).
|
||||||
|
'<@(js_sources)',
|
||||||
|
],
|
||||||
|
'outputs': [
|
||||||
|
'<(js2c_input_dir)/preload_bundle.js',
|
||||||
|
],
|
||||||
|
'action': [
|
||||||
|
'npm',
|
||||||
|
'run',
|
||||||
|
'browserify',
|
||||||
|
'--',
|
||||||
|
'<@(browserify_entries)',
|
||||||
|
'-o',
|
||||||
|
'<@(_outputs)',
|
||||||
|
],
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}, # target atom_browserify
|
||||||
{
|
{
|
||||||
'target_name': 'atom_js2c',
|
'target_name': 'atom_js2c',
|
||||||
'type': 'none',
|
'type': 'none',
|
||||||
'dependencies': [
|
'dependencies': [
|
||||||
'atom_js2c_copy',
|
'atom_js2c_copy',
|
||||||
|
'atom_browserify',
|
||||||
],
|
],
|
||||||
'actions': [
|
'actions': [
|
||||||
{
|
{
|
||||||
|
@ -435,6 +468,7 @@
|
||||||
'inputs': [
|
'inputs': [
|
||||||
# List all input files that should trigger a rebuild with js2c
|
# List all input files that should trigger a rebuild with js2c
|
||||||
'<@(js2c_sources)',
|
'<@(js2c_sources)',
|
||||||
|
'<(js2c_input_dir)/preload_bundle.js',
|
||||||
],
|
],
|
||||||
'outputs': [
|
'outputs': [
|
||||||
'<(SHARED_INTERMEDIATE_DIR)/atom_natives.h',
|
'<(SHARED_INTERMEDIATE_DIR)/atom_natives.h',
|
||||||
|
|
|
@ -70,6 +70,9 @@
|
||||||
'lib/renderer/extensions/storage.js',
|
'lib/renderer/extensions/storage.js',
|
||||||
'lib/renderer/extensions/web-navigation.js',
|
'lib/renderer/extensions/web-navigation.js',
|
||||||
],
|
],
|
||||||
|
'browserify_entries': [
|
||||||
|
'lib/sandboxed_renderer/init.js',
|
||||||
|
],
|
||||||
'js2c_sources': [
|
'js2c_sources': [
|
||||||
'lib/common/asar.js',
|
'lib/common/asar.js',
|
||||||
'lib/common/asar_init.js',
|
'lib/common/asar_init.js',
|
||||||
|
|
56
lib/sandboxed_renderer/init.js
Normal file
56
lib/sandboxed_renderer/init.js
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
/* eslint no-eval: "off" */
|
||||||
|
/* global binding, preloadPath, process, Buffer */
|
||||||
|
const events = require('events')
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
binding.onMessage = function (channel, args) {
|
||||||
|
ipcRenderer.emit.apply(ipcRenderer, [channel].concat(args))
|
||||||
|
}
|
||||||
|
|
||||||
|
binding.onExit = function () {
|
||||||
|
proc.emit('exit')
|
||||||
|
}
|
||||||
|
|
||||||
|
const preloadModules = new Map([
|
||||||
|
['electron', {
|
||||||
|
ipcRenderer: ipcRenderer
|
||||||
|
}]
|
||||||
|
])
|
||||||
|
|
||||||
|
function preloadRequire (module) {
|
||||||
|
if (preloadModules.has(module)) {
|
||||||
|
return preloadModules.get(module)
|
||||||
|
}
|
||||||
|
throw new Error('module not found')
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch the source for the preload
|
||||||
|
let preloadSrc = ipcRenderer.sendSync('ELECTRON_BROWSER_READ_FILE', preloadPath)
|
||||||
|
if (preloadSrc.err) {
|
||||||
|
throw new Error(preloadSrc.err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wrap the source into a function receives a `require` function as argument.
|
||||||
|
// Browserify bundles can make use of this, as explained in:
|
||||||
|
// https://github.com/substack/node-browserify#multiple-bundles
|
||||||
|
//
|
||||||
|
// For example, the user can create a browserify bundle with:
|
||||||
|
//
|
||||||
|
// $ browserify -x electron preload.js > renderer.js
|
||||||
|
//
|
||||||
|
// and any `require('electron')` calls in `preload.js` will work as expected
|
||||||
|
// since browserify won't try to include `electron` in the bundle and will fall
|
||||||
|
// back to the `preloadRequire` function above.
|
||||||
|
let preloadWrapperSrc = `(function(require, process, Buffer, global) {
|
||||||
|
${preloadSrc.data}
|
||||||
|
})`
|
||||||
|
|
||||||
|
let preloadFn = geval(preloadWrapperSrc)
|
||||||
|
preloadFn(preloadRequire, proc, Buffer, global)
|
|
@ -3,6 +3,7 @@
|
||||||
"version": "1.4.1",
|
"version": "1.4.1",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"asar": "^0.11.0",
|
"asar": "^0.11.0",
|
||||||
|
"browserify": "^13.1.0",
|
||||||
"electabul": "~0.0.4",
|
"electabul": "~0.0.4",
|
||||||
"electron-docs-linter": "^1.6.2",
|
"electron-docs-linter": "^1.6.2",
|
||||||
"request": "*",
|
"request": "*",
|
||||||
|
@ -25,6 +26,7 @@
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"bootstrap": "python ./script/bootstrap.py",
|
"bootstrap": "python ./script/bootstrap.py",
|
||||||
|
"browserify": "browserify",
|
||||||
"build": "python ./script/build.py -c D",
|
"build": "python ./script/build.py -c D",
|
||||||
"clean": "python ./script/clean.py",
|
"clean": "python ./script/clean.py",
|
||||||
"coverage": "npm run instrument-code-coverage && npm test -- --use-instrumented-asar",
|
"coverage": "npm run instrument-code-coverage && npm test -- --use-instrumented-asar",
|
||||||
|
|
|
@ -14,10 +14,7 @@ SOURCE_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
|
||||||
VENDOR_DIR = os.path.join(SOURCE_ROOT, 'vendor')
|
VENDOR_DIR = os.path.join(SOURCE_ROOT, 'vendor')
|
||||||
PYTHON_26_URL = 'https://chromium.googlesource.com/chromium/deps/python_26'
|
PYTHON_26_URL = 'https://chromium.googlesource.com/chromium/deps/python_26'
|
||||||
|
|
||||||
if os.environ.has_key('CI'):
|
NPM = 'npm'
|
||||||
NPM = os.path.join(SOURCE_ROOT, 'node_modules', '.bin', 'npm')
|
|
||||||
else:
|
|
||||||
NPM = 'npm'
|
|
||||||
if sys.platform in ['win32', 'cygwin']:
|
if sys.platform in ['win32', 'cygwin']:
|
||||||
NPM += '.cmd'
|
NPM += '.cmd'
|
||||||
|
|
||||||
|
|
|
@ -65,6 +65,11 @@ def main():
|
||||||
execute([npm, 'install', 'npm@2.12.1'])
|
execute([npm, 'install', 'npm@2.12.1'])
|
||||||
|
|
||||||
log_versions()
|
log_versions()
|
||||||
|
# Add "./node_modules/.bin" to the beginning of $PATH, which will ensure
|
||||||
|
# future "npm" invocations use the right version.
|
||||||
|
node_bin_dir = os.path.join(SOURCE_ROOT, 'node_modules', '.bin')
|
||||||
|
os.environ['PATH'] = os.path.pathsep.join([node_bin_dir,
|
||||||
|
os.environ.get('PATH', '')])
|
||||||
|
|
||||||
is_release = os.environ.has_key('ELECTRON_RELEASE')
|
is_release = os.environ.has_key('ELECTRON_RELEASE')
|
||||||
args = ['--target_arch=' + target_arch]
|
args = ['--target_arch=' + target_arch]
|
||||||
|
|
Loading…
Reference in a new issue