Skip to content

Commit

Permalink
refactor: use helpers for command-line parsing in renderer/init.js
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak committed Jan 2, 2019
1 parent eb8dc6b commit 074527a
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 51 deletions.
10 changes: 4 additions & 6 deletions atom/browser/web_contents_preferences.cc
Expand Up @@ -228,19 +228,17 @@ void WebContentsPreferences::AppendCommandLineSwitches(
::switches::kEnableExperimentalWebPlatformFeatures);

// Check if we have node integration specified.
bool enable_node_integration = IsEnabled(options::kNodeIntegration, true);
command_line->AppendSwitchASCII(switches::kNodeIntegration,
enable_node_integration ? "true" : "false");
if (IsEnabled(options::kNodeIntegration))
command_line->AppendSwitch(switches::kNodeIntegration);

// Whether to enable node integration in Worker.
if (IsEnabled(options::kNodeIntegrationInWorker))
command_line->AppendSwitch(switches::kNodeIntegrationInWorker);

// Check if webview tag creation is enabled, default to nodeIntegration value.
// TODO(kevinsawicki): Default to false in 2.0
bool webview_tag = IsEnabled(options::kWebviewTag, enable_node_integration);
command_line->AppendSwitchASCII(switches::kWebviewTag,
webview_tag ? "true" : "false");
if (IsEnabled(options::kWebviewTag))
command_line->AppendSwitch(switches::kWebviewTag);

// If the `sandbox` option was passed to the BrowserWindow's webPreferences,
// pass `--enable-sandbox` to the renderer so it won't have any node.js
Expand Down
7 changes: 0 additions & 7 deletions atom/renderer/atom_renderer_client.h
Expand Up @@ -35,13 +35,6 @@ class AtomRendererClient : public RendererClientBase {
content::RenderFrame* render_frame) override;

private:
enum NodeIntegration {
ALL,
EXCEPT_IFRAME,
MANUAL_ENABLE_IFRAME,
DISABLE,
};

// content::ContentRendererClient:
void RenderThreadStarted() override;
void RenderFrameCreated(content::RenderFrame*) override;
Expand Down
1 change: 1 addition & 0 deletions filenames.gni
Expand Up @@ -65,6 +65,7 @@ filenames = {
"lib/common/web-view-methods.js",
"lib/renderer/callbacks-registry.js",
"lib/renderer/chrome-api.js",
"lib/renderer/command-line.js",
"lib/renderer/content-scripts-injector.js",
"lib/renderer/init.js",
"lib/renderer/inspector.js",
Expand Down
14 changes: 14 additions & 0 deletions lib/renderer/command-line.js
@@ -0,0 +1,14 @@
'use strict'

exports.hasSwitch = function (name) {
return process.argv.includes(`--${name}`)
}

exports.getSwitchValue = function (name, defaultValue, converter = value => value) {
for (const arg of process.argv) {
if (arg.indexOf(`--${name}=`) === 0) {
return converter(arg.substr(arg.indexOf('=') + 1))
}
}
return defaultValue
}
51 changes: 15 additions & 36 deletions lib/renderer/init.js
Expand Up @@ -31,41 +31,20 @@ const ipcRenderer = require('@electron/internal/renderer/ipc-renderer-internal')
require('@electron/internal/renderer/web-frame-init')()

// Process command line arguments.
let nodeIntegration = false
let webviewTag = false
let contextIsolation = false
let preloadScript = null
let preloadScripts = []
let isBackgroundPage = false
let appPath = null
let guestInstanceId = null
let openerId = null
for (const arg of process.argv) {
if (arg.indexOf('--guest-instance-id=') === 0) {
// This is a guest web view.
guestInstanceId = parseInt(arg.substr(arg.indexOf('=') + 1))
} else if (arg.indexOf('--opener-id=') === 0) {
// This is a guest BrowserWindow.
openerId = parseInt(arg.substr(arg.indexOf('=') + 1))
} else if (arg.indexOf('--node-integration=') === 0) {
nodeIntegration = arg.substr(arg.indexOf('=') + 1) === 'true'
} else if (arg.indexOf('--preload=') === 0) {
preloadScript = arg.substr(arg.indexOf('=') + 1)
} else if (arg === '--background-page') {
isBackgroundPage = true
} else if (arg.indexOf('--app-path=') === 0) {
appPath = arg.substr(arg.indexOf('=') + 1)
} else if (arg.indexOf('--webview-tag=') === 0) {
webviewTag = arg.substr(arg.indexOf('=') + 1) === 'true'
} else if (arg === '--context-isolation') {
contextIsolation = true
} else if (arg.indexOf('--preload-scripts') === 0) {
preloadScripts = arg.substr(arg.indexOf('=') + 1).split(path.delimiter)
}
}
const { hasSwitch, getSwitchValue } = require('@electron/internal/renderer/command-line')

const contextIsolation = hasSwitch('context-isolation')
let nodeIntegration = hasSwitch('node-integration')
const webviewTag = hasSwitch('webview-tag')
const isHiddenPage = hasSwitch('hidden-page')
const isBackgroundPage = hasSwitch('background-page')
const usesNativeWindowOpen = hasSwitch('native-window-open')

const hiddenPage = process.argv.includes('--hidden-page')
const usesNativeWindowOpen = process.argv.includes('--native-window-open')
const preloadScript = getSwitchValue('preload', null)
const preloadScripts = getSwitchValue('preload-scripts', [], value => value.split(path.delimiter))
const appPath = getSwitchValue('app-path', null)
const guestInstanceId = getSwitchValue('guest-instance-id', null, value => parseInt(value))
const openerId = getSwitchValue('opener-id', null, value => parseInt(value))

// The webContents preload script is loaded after the session preload scripts.
if (preloadScript) {
Expand All @@ -74,7 +53,7 @@ if (preloadScript) {

// Pass the arguments to isolatedWorld.
if (contextIsolation) {
const isolatedWorldArgs = { ipcRenderer, guestInstanceId, hiddenPage, openerId, usesNativeWindowOpen }
const isolatedWorldArgs = { ipcRenderer, guestInstanceId, isHiddenPage, openerId, usesNativeWindowOpen }
v8Util.setHiddenValue(global, 'isolated-world-args', isolatedWorldArgs)
}

Expand All @@ -91,7 +70,7 @@ if (window.location.protocol === 'chrome-devtools:') {
nodeIntegration = false
} else {
// Override default web functions.
require('@electron/internal/renderer/window-setup')(ipcRenderer, guestInstanceId, openerId, hiddenPage, usesNativeWindowOpen)
require('@electron/internal/renderer/window-setup')(ipcRenderer, guestInstanceId, openerId, isHiddenPage, usesNativeWindowOpen)

// Inject content scripts.
require('@electron/internal/renderer/content-scripts-injector')
Expand Down
2 changes: 1 addition & 1 deletion lib/sandboxed_renderer/init.js
Expand Up @@ -123,7 +123,7 @@ if (binding.guestInstanceId) {
process.guestInstanceId = parseInt(binding.guestInstanceId)
}

if (!process.guestInstanceId && preloadProcess.argv.includes('--webview-tag=true')) {
if (!process.guestInstanceId && preloadProcess.argv.includes('--webview-tag')) {
// don't allow recursive `<webview>`
require('@electron/internal/renderer/web-view/web-view').setupWebView(window)
}
Expand Down
2 changes: 1 addition & 1 deletion spec/api-browser-window-spec.js
Expand Up @@ -1971,7 +1971,7 @@ describe('BrowserWindow module', () => {
const p = emittedOnce(ipcMain, 'answer')
w.loadFile(path.join(fixtures, 'api', 'window-open-location-open.html'))
const [, args, typeofProcess] = await p
expect(args).to.include('--node-integration=false')
expect(args).not.to.include('--node-integration')
expect(args).to.include('--native-window-open')
expect(typeofProcess).to.eql('undefined')
})
Expand Down

0 comments on commit 074527a

Please sign in to comment.