|
| 1 | +using System.Threading; |
| 2 | +using System.Threading.Tasks; |
| 3 | + |
| 4 | +namespace ElectronNET.API |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Manipulate the command line arguments for your app that Chromium reads. |
| 8 | + /// </summary> |
| 9 | + public sealed class CommandLine |
| 10 | + { |
| 11 | + internal CommandLine() { } |
| 12 | + |
| 13 | + internal static CommandLine Instance |
| 14 | + { |
| 15 | + get |
| 16 | + { |
| 17 | + if (_commandLine == null) |
| 18 | + { |
| 19 | + lock (_syncRoot) |
| 20 | + { |
| 21 | + if (_commandLine == null) |
| 22 | + { |
| 23 | + _commandLine = new CommandLine(); |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + return _commandLine; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + private static CommandLine _commandLine; |
| 33 | + |
| 34 | + private static object _syncRoot = new object(); |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Append a switch (with optional value) to Chromium's command line. |
| 38 | + /// </summary> |
| 39 | + /// <param name="the_switch">A command-line switch, without the leading --</param> |
| 40 | + /// <param name="value">(optional) - A value for the given switch</param> |
| 41 | + /// <remarks> |
| 42 | + /// Note: This will not affect process.argv. The intended usage of this function is to control Chromium's behavior. |
| 43 | + /// </remarks> |
| 44 | + public void AppendSwitch(string the_switch, string value = "") |
| 45 | + { |
| 46 | + BridgeConnector.Socket.Emit("appCommandLineAppendSwitch", the_switch, value); |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Append an argument to Chromium's command line. The argument will be quoted correctly. Switches will precede arguments regardless of appending order. |
| 51 | + /// |
| 52 | + /// If you're appending an argument like <code>--switch=value</code>, consider using <code>appendSwitch('switch', 'value')</code> instead. |
| 53 | + /// </summary> |
| 54 | + /// <param name="value">The argument to append to the command line</param> |
| 55 | + /// <remarks> |
| 56 | + /// Note: This will not affect process.argv. The intended usage of this function is to control Chromium's behavior. |
| 57 | + /// </remarks> |
| 58 | + public void AppendArgument(string value) |
| 59 | + { |
| 60 | + BridgeConnector.Socket.Emit("appCommandLineAppendArgument", value); |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Whether the command-line switch is present. |
| 65 | + /// </summary> |
| 66 | + /// <param name="switchName">A command-line switch</param> |
| 67 | + /// <param name="cancellationToken"></param> |
| 68 | + /// <returns>Whether the command-line switch is present.</returns> |
| 69 | + public async Task<bool> HasSwitchAsync(string switchName, CancellationToken cancellationToken = default(CancellationToken)) |
| 70 | + { |
| 71 | + cancellationToken.ThrowIfCancellationRequested(); |
| 72 | + |
| 73 | + var taskCompletionSource = new TaskCompletionSource<bool>(); |
| 74 | + using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled())) |
| 75 | + { |
| 76 | + BridgeConnector.Socket.On("appCommandLineHasSwitchCompleted", (result) => |
| 77 | + { |
| 78 | + BridgeConnector.Socket.Off("appCommandLineHasSwitchCompleted"); |
| 79 | + taskCompletionSource.SetResult((bool)result); |
| 80 | + }); |
| 81 | + |
| 82 | + BridgeConnector.Socket.Emit("appCommandLineHasSwitch", switchName); |
| 83 | + |
| 84 | + return await taskCompletionSource.Task.ConfigureAwait(false); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// The command-line switch value. |
| 90 | + /// </summary> |
| 91 | + /// <param name="switchName">A command-line switch</param> |
| 92 | + /// <param name="cancellationToken"></param> |
| 93 | + /// <returns>The command-line switch value.</returns> |
| 94 | + /// <remarks> |
| 95 | + /// Note: When the switch is not present or has no value, it returns empty string. |
| 96 | + /// </remarks> |
| 97 | + public async Task<string> GetSwitchValueAsync(string switchName, CancellationToken cancellationToken = default(CancellationToken)) |
| 98 | + { |
| 99 | + cancellationToken.ThrowIfCancellationRequested(); |
| 100 | + |
| 101 | + var taskCompletionSource = new TaskCompletionSource<string>(); |
| 102 | + using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled())) |
| 103 | + { |
| 104 | + BridgeConnector.Socket.On("appCommandLineGetSwitchValueCompleted", (result) => |
| 105 | + { |
| 106 | + BridgeConnector.Socket.Off("appCommandLineGetSwitchValueCompleted"); |
| 107 | + taskCompletionSource.SetResult((string)result); |
| 108 | + }); |
| 109 | + |
| 110 | + BridgeConnector.Socket.Emit("appCommandLineGetSwitchValue", switchName); |
| 111 | + |
| 112 | + return await taskCompletionSource.Task.ConfigureAwait(false); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments