electron/lib/renderer/init.js

140 lines
4.5 KiB
JavaScript
Raw Normal View History

2016-02-01 19:46:02 +00:00
'use strict';
2016-01-13 03:55:49 +00:00
2016-01-14 22:20:06 +00:00
const events = require('events');
const path = require('path');
const Module = require('module');
2016-01-12 02:40:23 +00:00
2016-01-14 19:10:12 +00:00
// We modified the original process.argv to let node.js load the
// atom-renderer.js, we need to restore it here.
2016-01-12 02:40:23 +00:00
process.argv.splice(1, 1);
2016-01-14 18:35:29 +00:00
// Clear search paths.
2016-01-12 02:40:23 +00:00
require(path.resolve(__dirname, '..', '..', 'common', 'lib', 'reset-search-paths'));
2016-01-14 18:35:29 +00:00
// Import common settings.
2016-01-12 02:40:23 +00:00
require(path.resolve(__dirname, '..', '..', 'common', 'lib', 'init'));
2016-01-14 22:20:06 +00:00
var globalPaths = Module.globalPaths;
2016-01-12 02:40:23 +00:00
if (!process.env.ELECTRON_HIDE_INTERNAL_MODULES) {
globalPaths.push(path.resolve(__dirname, '..', 'api', 'lib'));
}
2016-01-14 18:35:29 +00:00
// Expose public APIs.
2016-01-12 02:40:23 +00:00
globalPaths.push(path.resolve(__dirname, '..', 'api', 'lib', 'exports'));
2016-01-14 18:35:29 +00:00
// The global variable will be used by ipc for event dispatching
2016-01-14 22:20:06 +00:00
var v8Util = process.atomBinding('v8_util');
2016-01-12 02:40:23 +00:00
v8Util.setHiddenValue(global, 'ipc', new events.EventEmitter);
2016-01-13 03:55:49 +00:00
// Use electron module after everything is ready.
const electron = require('electron');
// Call webFrame method.
electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', (event, method, args) => {
2016-02-22 14:00:21 +00:00
electron.webFrame[method].apply(electron.webFrame, args);
});
2016-02-24 10:11:09 +00:00
electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', (event, requestId, method, args) => {
2016-02-22 14:00:21 +00:00
const responseCallback = function(result) {
event.sender.send(`ELECTRON_INTERNAL_BROWSER_ASYNC_WEB_FRAME_RESPONSE_${requestId}`, result);
2016-02-22 14:00:21 +00:00
};
args.push(responseCallback);
2016-01-13 03:55:49 +00:00
electron.webFrame[method].apply(electron.webFrame, args);
});
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Process command line arguments.
2016-01-14 22:20:06 +00:00
var nodeIntegration = 'false';
var preloadScript = null;
2016-01-12 02:40:23 +00:00
2016-01-14 22:20:06 +00:00
var ref = process.argv;
2016-01-15 02:07:29 +00:00
var i, len, arg;
2016-01-12 02:40:23 +00:00
for (i = 0, len = ref.length; i < len; i++) {
2016-01-15 02:07:29 +00:00
arg = ref[i];
2016-01-12 02:40:23 +00:00
if (arg.indexOf('--guest-instance-id=') === 0) {
2016-01-14 18:35:29 +00:00
// This is a guest web view.
2016-01-12 02:40:23 +00:00
process.guestInstanceId = parseInt(arg.substr(arg.indexOf('=') + 1));
} else if (arg.indexOf('--opener-id=') === 0) {
2016-01-14 18:35:29 +00:00
// This is a guest BrowserWindow.
2016-01-12 02:40:23 +00:00
process.openerId = parseInt(arg.substr(arg.indexOf('=') + 1));
} else if (arg.indexOf('--node-integration=') === 0) {
nodeIntegration = arg.substr(arg.indexOf('=') + 1);
} else if (arg.indexOf('--preload=') === 0) {
preloadScript = arg.substr(arg.indexOf('=') + 1);
}
}
if (location.protocol === 'chrome-devtools:') {
2016-01-14 18:35:29 +00:00
// Override some inspector APIs.
2016-01-12 02:40:23 +00:00
require('./inspector');
nodeIntegration = 'true';
} else if (location.protocol === 'chrome-extension:') {
2016-01-14 18:35:29 +00:00
// Add implementations of chrome API.
2016-01-12 02:40:23 +00:00
require('./chrome-api');
nodeIntegration = 'true';
} else {
2016-01-14 18:35:29 +00:00
// Override default web functions.
2016-01-12 02:40:23 +00:00
require('./override');
2016-01-14 18:35:29 +00:00
// Load webview tag implementation.
2016-01-12 02:40:23 +00:00
if (process.guestInstanceId == null) {
require('./web-view/web-view');
require('./web-view/web-view-attributes');
}
}
if (nodeIntegration === 'true' || nodeIntegration === 'all' || nodeIntegration === 'except-iframe' || nodeIntegration === 'manual-enable-iframe') {
2016-01-14 18:35:29 +00:00
// Export node bindings to global.
2016-01-12 02:40:23 +00:00
global.require = require;
global.module = module;
2016-01-14 18:35:29 +00:00
// Set the __filename to the path of html file if it is file: protocol.
2016-01-12 02:40:23 +00:00
if (window.location.protocol === 'file:') {
2016-01-14 22:20:06 +00:00
var pathname = process.platform === 'win32' && window.location.pathname[0] === '/' ? window.location.pathname.substr(1) : window.location.pathname;
2016-01-12 02:40:23 +00:00
global.__filename = path.normalize(decodeURIComponent(pathname));
global.__dirname = path.dirname(global.__filename);
2016-01-14 18:35:29 +00:00
// Set module's filename so relative require can work as expected.
2016-01-12 02:40:23 +00:00
module.filename = global.__filename;
2016-01-14 18:35:29 +00:00
// Also search for module under the html file.
2016-01-12 02:40:23 +00:00
module.paths = module.paths.concat(Module._nodeModulePaths(global.__dirname));
} else {
global.__filename = __filename;
global.__dirname = __dirname;
}
2016-01-14 18:35:29 +00:00
// Redirect window.onerror to uncaughtException.
2016-01-12 02:40:23 +00:00
window.onerror = function(message, filename, lineno, colno, error) {
if (global.process.listeners('uncaughtException').length > 0) {
global.process.emit('uncaughtException', error);
return true;
} else {
return false;
}
};
} else {
2016-01-14 18:35:29 +00:00
// Delete Node's symbols after the Environment has been loaded.
2016-01-12 02:40:23 +00:00
process.once('loaded', function() {
delete global.process;
delete global.setImmediate;
delete global.clearImmediate;
return delete global.global;
});
}
2016-01-14 18:35:29 +00:00
// Load the script specfied by the "preload" attribute.
2016-01-12 02:40:23 +00:00
if (preloadScript) {
try {
require(preloadScript);
2016-01-14 22:20:06 +00:00
} catch (error) {
2016-01-12 02:40:23 +00:00
if (error.code === 'MODULE_NOT_FOUND') {
console.error("Unable to load preload script " + preloadScript);
} else {
console.error(error);
console.error(error.stack);
}
}
}