|
| 1 | +using System.Threading; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Newtonsoft.Json; |
| 4 | +using Newtonsoft.Json.Linq; |
| 5 | +using ElectronNET.API.Entities; |
| 6 | + |
| 7 | +namespace ElectronNET.API |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Electron's process object is extended from the Node.js process object. It adds the |
| 11 | + /// events, properties, and methods. |
| 12 | + /// </summary> |
| 13 | + public sealed class Process |
| 14 | + { |
| 15 | + internal Process() { } |
| 16 | + |
| 17 | + internal static Process Instance |
| 18 | + { |
| 19 | + get |
| 20 | + { |
| 21 | + if (_process == null) |
| 22 | + { |
| 23 | + lock (_syncRoot) |
| 24 | + { |
| 25 | + if (_process == null) |
| 26 | + { |
| 27 | + _process = new Process(); |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + return _process; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private static Process _process; |
| 37 | + |
| 38 | + private static readonly object _syncRoot = new(); |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// The process.execPath property returns the absolute pathname of the executable that |
| 42 | + /// started the Node.js process. Symbolic links, if any, are resolved. |
| 43 | + /// </summary> |
| 44 | + public Task<string> ExecPathAsync |
| 45 | + { |
| 46 | + get |
| 47 | + { |
| 48 | + var taskCompletionSource = new TaskCompletionSource<string>(); |
| 49 | + |
| 50 | + BridgeConnector.Socket.On("process-execPath-Completed", (result) => |
| 51 | + { |
| 52 | + BridgeConnector.Socket.Off("process-execPath-Completed"); |
| 53 | + taskCompletionSource.SetResult(result.ToString()); |
| 54 | + }); |
| 55 | + |
| 56 | + BridgeConnector.Socket.Emit("process-execPath"); |
| 57 | + return taskCompletionSource.Task; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// The process.argv property returns an array containing the command-line arguments passed |
| 63 | + /// when the Node.js process was launched. The first element will be process.execPath. See |
| 64 | + /// process.argv0 if access to the original value of argv[0] is needed. The second element |
| 65 | + /// will be the path to the JavaScript file being executed. The remaining elements will be |
| 66 | + /// any additional command-line arguments |
| 67 | + /// </summary> |
| 68 | + public Task<string[]> ArgvAsync |
| 69 | + { |
| 70 | + get |
| 71 | + { |
| 72 | + var taskCompletionSource = new TaskCompletionSource<string[]>(); |
| 73 | + |
| 74 | + BridgeConnector.Socket.On("process-argv-Completed", (result) => |
| 75 | + { |
| 76 | + BridgeConnector.Socket.Off("process-argv-Completed"); |
| 77 | + taskCompletionSource.SetResult(((JArray)result).ToObject<string[]>()); |
| 78 | + }); |
| 79 | + |
| 80 | + BridgeConnector.Socket.Emit("process-argv"); |
| 81 | + return taskCompletionSource.Task; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// The process.execPath property returns the absolute pathname of the executable that |
| 87 | + /// started the Node.js process. Symbolic links, if any, are resolved. |
| 88 | + /// </summary> |
| 89 | + public Task<string> TypeAsync |
| 90 | + { |
| 91 | + get |
| 92 | + { |
| 93 | + var taskCompletionSource = new TaskCompletionSource<string>(); |
| 94 | + |
| 95 | + BridgeConnector.Socket.On("process-type-Completed", (result) => |
| 96 | + { |
| 97 | + BridgeConnector.Socket.Off("process-type-Completed"); |
| 98 | + taskCompletionSource.SetResult(result.ToString()); |
| 99 | + }); |
| 100 | + |
| 101 | + BridgeConnector.Socket.Emit("process-type"); |
| 102 | + return taskCompletionSource.Task; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// The process.versions property returns an object listing the version strings of |
| 109 | + /// chrome and electron. |
| 110 | + /// </summary> |
| 111 | + public Task<ProcessVersions> VersionsAsync |
| 112 | + { |
| 113 | + get |
| 114 | + { |
| 115 | + var taskCompletionSource = new TaskCompletionSource<ProcessVersions>(); |
| 116 | + |
| 117 | + BridgeConnector.Socket.On("process-versions-Completed", (result) => |
| 118 | + { |
| 119 | + BridgeConnector.Socket.Off("process-versions-Completed"); |
| 120 | + taskCompletionSource.SetResult(((JObject)result).ToObject<ProcessVersions>()); |
| 121 | + }); |
| 122 | + |
| 123 | + BridgeConnector.Socket.Emit("process-versions"); |
| 124 | + return taskCompletionSource.Task; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + /// <summary> |
| 130 | + /// A Boolean. When app is started by being passed as parameter to the default app, this |
| 131 | + /// property is true in the main process, otherwise it is false. |
| 132 | + /// </summary> |
| 133 | + public Task<bool> DefaultAppAsync |
| 134 | + { |
| 135 | + get |
| 136 | + { |
| 137 | + var taskCompletionSource = new TaskCompletionSource<bool>(); |
| 138 | + |
| 139 | + BridgeConnector.Socket.On("process-defaultApp-Completed", (result) => |
| 140 | + { |
| 141 | + BridgeConnector.Socket.Off("process-defaultApp-Completed"); |
| 142 | + taskCompletionSource.SetResult(bool.Parse(result.ToString())); |
| 143 | + }); |
| 144 | + |
| 145 | + BridgeConnector.Socket.Emit("process-defaultApp"); |
| 146 | + return taskCompletionSource.Task; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /// <summary> |
| 151 | + /// A Boolean, true when the current renderer context is the "main" renderer frame. If you |
| 152 | + /// want the ID of the current frame you should use webFrame.routingId |
| 153 | + /// </summary> |
| 154 | + public Task<bool> IsMainFrameAsync |
| 155 | + { |
| 156 | + get |
| 157 | + { |
| 158 | + var taskCompletionSource = new TaskCompletionSource<bool>(); |
| 159 | + |
| 160 | + BridgeConnector.Socket.On("process-isMainFrame-Completed", (result) => |
| 161 | + { |
| 162 | + BridgeConnector.Socket.Off("process-isMainFrame-Completed"); |
| 163 | + taskCompletionSource.SetResult(bool.Parse(result.ToString())); |
| 164 | + }); |
| 165 | + |
| 166 | + BridgeConnector.Socket.Emit("process-isMainFrame"); |
| 167 | + return taskCompletionSource.Task; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + /// <summary> |
| 172 | + /// A String representing the path to the resources directory. |
| 173 | + /// </summary> |
| 174 | + public Task<string> ResourcesPathAsync |
| 175 | + { |
| 176 | + get |
| 177 | + { |
| 178 | + var taskCompletionSource = new TaskCompletionSource<string>(); |
| 179 | + |
| 180 | + BridgeConnector.Socket.On("process-resourcesPath-Completed", (result) => |
| 181 | + { |
| 182 | + BridgeConnector.Socket.Off("process-resourcesPath-Completed"); |
| 183 | + taskCompletionSource.SetResult(result.ToString()); |
| 184 | + }); |
| 185 | + |
| 186 | + BridgeConnector.Socket.Emit("process-resourcesPath"); |
| 187 | + return taskCompletionSource.Task; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + /// <summary> |
| 192 | + /// The number of seconds the current Node.js process has been running. The return value |
| 193 | + /// includes fractions of a second. Use Math.floor() to get whole seconds. |
| 194 | + /// </summary> |
| 195 | + public Task<double> UpTimeAsync |
| 196 | + { |
| 197 | + get |
| 198 | + { |
| 199 | + var taskCompletionSource = new TaskCompletionSource<double>(); |
| 200 | + |
| 201 | + BridgeConnector.Socket.On("process-uptime-Completed", (result) => |
| 202 | + { |
| 203 | + BridgeConnector.Socket.Off("process-uptime-Completed"); |
| 204 | + taskCompletionSource.SetResult(double.Parse(result.ToString())); |
| 205 | + }); |
| 206 | + |
| 207 | + BridgeConnector.Socket.Emit("process-uptime"); |
| 208 | + return taskCompletionSource.Task; |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + /// <summary> |
| 213 | + /// The PID of the electron process |
| 214 | + /// </summary> |
| 215 | + public Task<int> PidAsync |
| 216 | + { |
| 217 | + get |
| 218 | + { |
| 219 | + var taskCompletionSource = new TaskCompletionSource<int>(); |
| 220 | + |
| 221 | + BridgeConnector.Socket.On("process-pid-Completed", (result) => |
| 222 | + { |
| 223 | + BridgeConnector.Socket.Off("process-pid-Completed"); |
| 224 | + taskCompletionSource.SetResult(int.Parse(result.ToString())); |
| 225 | + }); |
| 226 | + |
| 227 | + BridgeConnector.Socket.Emit("process-pid"); |
| 228 | + return taskCompletionSource.Task; |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + |
| 233 | + /// <summary> |
| 234 | + /// The operating system CPU architecture for which the Node.js binary was compiled |
| 235 | + /// </summary> |
| 236 | + public Task<string> ArchAsync |
| 237 | + { |
| 238 | + get |
| 239 | + { |
| 240 | + var taskCompletionSource = new TaskCompletionSource<string>(); |
| 241 | + |
| 242 | + BridgeConnector.Socket.On("process-arch-Completed", (result) => |
| 243 | + { |
| 244 | + BridgeConnector.Socket.Off("process-arch-Completed"); |
| 245 | + taskCompletionSource.SetResult(result.ToString()); |
| 246 | + }); |
| 247 | + |
| 248 | + BridgeConnector.Socket.Emit("process-arch"); |
| 249 | + return taskCompletionSource.Task; |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + /// <summary> |
| 254 | + /// A string identifying the operating system platform on which the Node.js process is running |
| 255 | + /// </summary> |
| 256 | + public Task<string> PlatformAsync |
| 257 | + { |
| 258 | + get |
| 259 | + { |
| 260 | + var taskCompletionSource = new TaskCompletionSource<string>(); |
| 261 | + |
| 262 | + BridgeConnector.Socket.On("process-platform-Completed", (result) => |
| 263 | + { |
| 264 | + BridgeConnector.Socket.Off("process-platform-Completed"); |
| 265 | + taskCompletionSource.SetResult(result.ToString()); |
| 266 | + }); |
| 267 | + |
| 268 | + BridgeConnector.Socket.Emit("process-platform"); |
| 269 | + return taskCompletionSource.Task; |
| 270 | + } |
| 271 | + } |
| 272 | + |
| 273 | + } |
| 274 | +} |
0 commit comments