From 6530067b85194c1fc2c7b0349a629a4b24650e6a Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Tue, 12 Oct 2021 09:06:16 +0900 Subject: [PATCH] feat: add commandLine.removeSwitch (#31327) * feat: add commandLine.removeSwitch In some cases apps may want to remove Chromium command line switches to avoid certain Chromium behaviors being used, E.g. remote-debugging-port or gpu-launcher * fix: add missing removeSwitch to app.ts Co-authored-by: Samuel Attard Co-authored-by: Milan Burda --- docs/api/command-line.md | 9 +++++++++ lib/browser/api/app.ts | 3 ++- shell/common/api/electron_api_command_line.cc | 6 ++++++ spec-main/api-app-spec.ts | 15 +++++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/docs/api/command-line.md b/docs/api/command-line.md index cae1d5bf9993f..0eee3e4f344ed 100644 --- a/docs/api/command-line.md +++ b/docs/api/command-line.md @@ -53,3 +53,12 @@ Returns `Boolean` - Whether the command-line switch is present. Returns `String` - The command-line switch value. **Note:** When the switch is not present or has no value, it returns empty string. + +#### `commandLine.removeSwitch(switch)` + +* `switch` String - A command-line switch + +Removes the specified switch from Chromium's command line. + +**Note:** This will not affect `process.argv`. The intended usage of this function is to +control Chromium's behavior. diff --git a/lib/browser/api/app.ts b/lib/browser/api/app.ts index 64c6dbdc82b60..f952dcd0194b3 100644 --- a/lib/browser/api/app.ts +++ b/lib/browser/api/app.ts @@ -39,7 +39,8 @@ Object.assign(app, { hasSwitch: (theSwitch: string) => commandLine.hasSwitch(String(theSwitch)), getSwitchValue: (theSwitch: string) => commandLine.getSwitchValue(String(theSwitch)), appendSwitch: (theSwitch: string, value?: string) => commandLine.appendSwitch(String(theSwitch), typeof value === 'undefined' ? value : String(value)), - appendArgument: (arg: string) => commandLine.appendArgument(String(arg)) + appendArgument: (arg: string) => commandLine.appendArgument(String(arg)), + removeSwitch: (theSwitch: string) => commandLine.removeSwitch(String(theSwitch)) } as Electron.CommandLine }); diff --git a/shell/common/api/electron_api_command_line.cc b/shell/common/api/electron_api_command_line.cc index 0a944501365b8..7b96384ad0e2a 100644 --- a/shell/common/api/electron_api_command_line.cc +++ b/shell/common/api/electron_api_command_line.cc @@ -42,6 +42,11 @@ void AppendSwitch(const std::string& switch_string, command_line->AppendSwitch(switch_string); } +void RemoveSwitch(const std::string& switch_string) { + auto* command_line = base::CommandLine::ForCurrentProcess(); + command_line->RemoveSwitch(switch_string); +} + void AppendArg(const std::string& arg) { auto* command_line = base::CommandLine::ForCurrentProcess(); @@ -56,6 +61,7 @@ void Initialize(v8::Local exports, dict.SetMethod("hasSwitch", &HasSwitch); dict.SetMethod("getSwitchValue", &GetSwitchValue); dict.SetMethod("appendSwitch", &AppendSwitch); + dict.SetMethod("removeSwitch", &RemoveSwitch); dict.SetMethod("appendArgument", &AppendArg); } diff --git a/spec-main/api-app-spec.ts b/spec-main/api-app-spec.ts index 7144d96dfe23e..877bf62b5e0be 100644 --- a/spec-main/api-app-spec.ts +++ b/spec-main/api-app-spec.ts @@ -1624,6 +1624,21 @@ describe('app module', () => { }); }); + describe('commandLine.removeSwitch', () => { + it('no-ops a non-existent switch', async () => { + expect(app.commandLine.hasSwitch('foobar3')).to.equal(false); + app.commandLine.removeSwitch('foobar3'); + expect(app.commandLine.hasSwitch('foobar3')).to.equal(false); + }); + + it('removes an existing switch', async () => { + app.commandLine.appendSwitch('foobar3', 'test'); + expect(app.commandLine.hasSwitch('foobar3')).to.equal(true); + app.commandLine.removeSwitch('foobar3'); + expect(app.commandLine.hasSwitch('foobar3')).to.equal(false); + }); + }); + ifdescribe(process.platform === 'darwin')('app.setSecureKeyboardEntryEnabled', () => { it('changes Secure Keyboard Entry is enabled', () => { app.setSecureKeyboardEntryEnabled(true);