using System.Threading; using System.Threading.Tasks; namespace ElectronNET.API { /// /// Manipulate the command line arguments for your app that Chromium reads. /// public sealed class CommandLine { internal CommandLine() { } internal static CommandLine Instance { get { if (_commandLine == null) { lock (_syncRoot) { if (_commandLine == null) { _commandLine = new CommandLine(); } } } return _commandLine; } } private static CommandLine _commandLine; private static object _syncRoot = new object(); /// /// Append a switch (with optional value) to Chromium's command line. /// /// A command-line switch, without the leading -- /// (optional) - A value for the given switch /// /// Note: This will not affect process.argv. The intended usage of this function is to control Chromium's behavior. /// public void AppendSwitch(string the_switch, string value = "") { BridgeConnector.Socket.Emit("appCommandLineAppendSwitch", the_switch, value); } /// /// Append an argument to Chromium's command line. The argument will be quoted correctly. Switches will precede arguments regardless of appending order. /// /// If you're appending an argument like --switch=value, consider using appendSwitch('switch', 'value') instead. /// /// The argument to append to the command line /// /// Note: This will not affect process.argv. The intended usage of this function is to control Chromium's behavior. /// public void AppendArgument(string value) { BridgeConnector.Socket.Emit("appCommandLineAppendArgument", value); } /// /// Whether the command-line switch is present. /// /// A command-line switch /// /// Whether the command-line switch is present. public async Task HasSwitchAsync(string switchName, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); var taskCompletionSource = new TaskCompletionSource(); using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled())) { BridgeConnector.Socket.On("appCommandLineHasSwitchCompleted", (result) => { BridgeConnector.Socket.Off("appCommandLineHasSwitchCompleted"); taskCompletionSource.SetResult((bool)result); }); BridgeConnector.Socket.Emit("appCommandLineHasSwitch", switchName); return await taskCompletionSource.Task.ConfigureAwait(false); } } /// /// The command-line switch value. /// /// A command-line switch /// /// The command-line switch value. /// /// Note: When the switch is not present or has no value, it returns empty string. /// public async Task GetSwitchValueAsync(string switchName, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); var taskCompletionSource = new TaskCompletionSource(); using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled())) { BridgeConnector.Socket.On("appCommandLineGetSwitchValueCompleted", (result) => { BridgeConnector.Socket.Off("appCommandLineGetSwitchValueCompleted"); taskCompletionSource.SetResult((string)result); }); BridgeConnector.Socket.Emit("appCommandLineGetSwitchValue", switchName); return await taskCompletionSource.Task.ConfigureAwait(false); } } } }