Skip to content

Commit

Permalink
feat: add commandLine.removeSwitch (#31327)
Browse files Browse the repository at this point in the history
* 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 <samuel.r.attard@gmail.com>
Co-authored-by: Milan Burda <milan.burda@gmail.com>
  • Loading branch information
3 people committed Oct 12, 2021
1 parent 8626aa2 commit 6530067
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
9 changes: 9 additions & 0 deletions docs/api/command-line.md
Expand Up @@ -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.
3 changes: 2 additions & 1 deletion lib/browser/api/app.ts
Expand Up @@ -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
});

Expand Down
6 changes: 6 additions & 0 deletions shell/common/api/electron_api_command_line.cc
Expand Up @@ -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();

Expand All @@ -56,6 +61,7 @@ void Initialize(v8::Local<v8::Object> exports,
dict.SetMethod("hasSwitch", &HasSwitch);
dict.SetMethod("getSwitchValue", &GetSwitchValue);
dict.SetMethod("appendSwitch", &AppendSwitch);
dict.SetMethod("removeSwitch", &RemoveSwitch);
dict.SetMethod("appendArgument", &AppendArg);
}

Expand Down
15 changes: 15 additions & 0 deletions spec-main/api-app-spec.ts
Expand Up @@ -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);
Expand Down

0 comments on commit 6530067

Please sign in to comment.