Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to set arbitrary arguments in a renderer process #11850

Merged
merged 1 commit into from
Feb 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions atom/browser/web_contents_preferences.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@ void WebContentsPreferences::AppendExtraCommandLineSwitches(
LOG(ERROR) << "preload url must be file:// protocol.";
}

// Custom args for renderer process
base::Value* customArgs;
if ((web_preferences.Get(options::kCustomArgs, &customArgs))
&& (customArgs->is_list())) {
for (const base::Value& customArg : customArgs->GetList()) {
if (customArg.is_string()) {
command_line->AppendArg(customArg.GetString());
}
}
}

// Run Electron APIs and preload script in isolated world
bool isolated;
if (web_preferences.GetBoolean(options::kContextIsolation, &isolated) &&
Expand Down
2 changes: 2 additions & 0 deletions atom/common/options_switches.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ const char kNodeIntegrationInWorker[] = "nodeIntegrationInWorker";
// Enable the web view tag.
const char kWebviewTag[] = "webviewTag";

const char kCustomArgs[] = "additionalArguments";

} // namespace options

namespace switches {
Expand Down
1 change: 1 addition & 0 deletions atom/common/options_switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ extern const char kBlinkFeatures[];
extern const char kDisableBlinkFeatures[];
extern const char kNodeIntegrationInWorker[];
extern const char kWebviewTag[];
extern const char kCustomArgs[];

} // namespace options

Expand Down
3 changes: 3 additions & 0 deletions docs/api/browser-window.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ It creates a new `BrowserWindow` with native properties as set by the `options`.
script. You can use the `will-attach-webview` event on [webContents](web-contents.md)
to strip away the `preload` script and to validate or alter the
`<webview>`'s initial settings.
* `additionArguments` String[] (optional) - A list of strings that will be appended
to `process.argv` in the renderer process of this app. Useful for passing small
bits of data down to renderer process preload scripts.

When setting minimum or maximum window size with `minWidth`/`maxWidth`/
`minHeight`/`maxHeight`, it only constrains the users. It won't prevent you from
Expand Down
36 changes: 36 additions & 0 deletions spec/api-browser-window-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,42 @@ describe('BrowserWindow module', () => {
})
})

describe('"additionalArguments" option', () => {
it('adds extra args to process.argv in the renderer process', (done) => {
const preload = path.join(fixtures, 'module', 'check-arguments.js')
ipcMain.once('answer', (event, argv) => {
assert.ok(argv.includes('--my-magic-arg'))
done()
})
w.destroy()
w = new BrowserWindow({
show: false,
webPreferences: {
preload: preload,
additionalArguments: ['--my-magic-arg']
}
})
w.loadURL(`file://${path.join(fixtures, 'api', 'blank.html')}`)
})

it('adds extra value args to process.argv in the renderer process', (done) => {
const preload = path.join(fixtures, 'module', 'check-arguments.js')
ipcMain.once('answer', (event, argv) => {
assert.ok(argv.includes('--my-magic-arg=foo'))
done()
})
w.destroy()
w = new BrowserWindow({
show: false,
webPreferences: {
preload: preload,
additionalArguments: ['--my-magic-arg=foo']
}
})
w.loadURL(`file://${path.join(fixtures, 'api', 'blank.html')}`)
})
})

Copy link
Member

@ckerr ckerr Feb 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 on the tests and docs, thanks for adding these in the initial pass ❤️

edit: oh but do make the linter happy too

  /home/builduser/project/spec/api-browser-window-spec.js:1079:12: Unexpected trailing comma.
  /home/builduser/project/spec/api-browser-window-spec.js:1096:12: Unexpected trailing comma.

describe('"node-integration" option', () => {
it('disables node integration when specified to false', (done) => {
const preload = path.join(fixtures, 'module', 'send-later.js')
Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/module/check-arguments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var ipcRenderer = require('electron').ipcRenderer
window.onload = function () {
ipcRenderer.send('answer', process.argv)
}