mirror of
https://github.com/ElectronNET/Electron.NET.git
synced 2026-07-18 14:47:43 +00:00
Merge commit '64e058b0b59a4fbbd6c0ec9de2ee1850a66684c4' into feature/DI-MOC-Process
# Conflicts: # ElectronNET.API/ServiceCollectionExtensions.cs
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
using Quobject.SocketIoClientDotNet.Client;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Quobject.SocketIoClientDotNet.Client;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ElectronNET.API
|
||||
{
|
||||
@@ -40,5 +43,110 @@ namespace ElectronNET.API
|
||||
return _socket;
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<T> GetValueOverSocketAsync<T>(string eventString, string eventCompletedString)
|
||||
{
|
||||
CancellationToken cancellationToken = new();
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var taskCompletionSource = new TaskCompletionSource<T>();
|
||||
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
|
||||
{
|
||||
BridgeConnector.Socket.On(eventCompletedString, (value) =>
|
||||
{
|
||||
BridgeConnector.Socket.Off(eventCompletedString);
|
||||
|
||||
if (value == null)
|
||||
{
|
||||
Console.WriteLine($"ERROR: BridgeConnector (event: '{eventString}') returned null. Socket loop hang.");
|
||||
taskCompletionSource.SetCanceled();
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
taskCompletionSource.SetResult( new JValue(value).ToObject<T>() );
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"ERROR: BridgeConnector (event: '{eventString}') exception: {e.Message}. Socket loop hung.");
|
||||
}
|
||||
});
|
||||
|
||||
BridgeConnector.Socket.Emit(eventString);
|
||||
|
||||
return await taskCompletionSource.Task.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<T> GetObjectOverSocketAsync<T>(string eventString, string eventCompletedString)
|
||||
{
|
||||
CancellationToken cancellationToken = new();
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var taskCompletionSource = new TaskCompletionSource<T>();
|
||||
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
|
||||
{
|
||||
BridgeConnector.Socket.On(eventCompletedString, (value) =>
|
||||
{
|
||||
BridgeConnector.Socket.Off(eventCompletedString);
|
||||
|
||||
if (value == null)
|
||||
{
|
||||
Console.WriteLine($"ERROR: BridgeConnector (event: '{eventString}') returned null. Socket loop hang.");
|
||||
taskCompletionSource.SetCanceled();
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
taskCompletionSource.SetResult( ((JObject)value).ToObject<T>() );
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"ERROR: BridgeConnector (event: '{eventString}') exception: {e.Message}. Socket loop hung.");
|
||||
}
|
||||
});
|
||||
|
||||
BridgeConnector.Socket.Emit(eventString);
|
||||
|
||||
return await taskCompletionSource.Task.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<T> GetArrayOverSocketAsync<T>(string eventString, string eventCompletedString)
|
||||
{
|
||||
CancellationToken cancellationToken = new();
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var taskCompletionSource = new TaskCompletionSource<T>();
|
||||
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
|
||||
{
|
||||
BridgeConnector.Socket.On(eventCompletedString, (value) =>
|
||||
{
|
||||
BridgeConnector.Socket.Off(eventCompletedString);
|
||||
if (value == null)
|
||||
{
|
||||
Console.WriteLine($"ERROR: BridgeConnector (event: '{eventString}') returned null. Socket loop hang.");
|
||||
taskCompletionSource.SetCanceled();
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
taskCompletionSource.SetResult( ((JArray)value).ToObject<T>() );
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"ERROR: BridgeConnector (event: '{eventString}') exception: {e.Message}. Socket loop hung.");
|
||||
}
|
||||
});
|
||||
|
||||
BridgeConnector.Socket.Emit(eventString);
|
||||
|
||||
return await taskCompletionSource.Task.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,5 +88,10 @@
|
||||
/// Control your app in the macOS dock.
|
||||
/// </summary>
|
||||
public static Dock Dock { get { return Dock.Instance; } }
|
||||
|
||||
/// <summary>
|
||||
/// Electeon extensions to the Nodejs process object.
|
||||
/// </summary>
|
||||
public static Process Process { get { return Process.Instance; } }
|
||||
}
|
||||
}
|
||||
10
ElectronNET.API/Entities/ProcessVersions.cs
Normal file
10
ElectronNET.API/Entities/ProcessVersions.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace ElectronNET.API
|
||||
{
|
||||
/// <summary>
|
||||
/// An object listing the version strings specific to Electron
|
||||
/// </summary>
|
||||
/// <param name="Chrome">Value representing Chrome's version string</param>
|
||||
/// <param name="Electron">Value representing Electron's version string</param>
|
||||
/// <returns></returns>
|
||||
public record ProcessVersions(string Chrome, string Electron);
|
||||
}
|
||||
185
ElectronNET.API/Process.cs
Normal file
185
ElectronNET.API/Process.cs
Normal file
@@ -0,0 +1,185 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace ElectronNET.API
|
||||
{
|
||||
/// <summary>
|
||||
/// Electron's process object is extended from the Node.js process object. It adds the
|
||||
/// events, properties, and methods.
|
||||
/// </summary>
|
||||
public sealed class Process
|
||||
{
|
||||
internal Process() { }
|
||||
|
||||
internal static Process Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_process == null)
|
||||
{
|
||||
lock (_syncRoot)
|
||||
{
|
||||
if (_process == null)
|
||||
{
|
||||
_process = new Process();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return _process;
|
||||
}
|
||||
}
|
||||
|
||||
private static Process _process;
|
||||
|
||||
private static readonly object _syncRoot = new();
|
||||
|
||||
/// <summary>
|
||||
/// The process.execPath property returns the absolute pathname of the executable that
|
||||
/// started the Node.js process. Symbolic links, if any, are resolved.
|
||||
/// </summary>
|
||||
public Task<string> ExecPathAsync
|
||||
{
|
||||
get
|
||||
{
|
||||
return BridgeConnector.GetValueOverSocketAsync<string>(
|
||||
"process-execPath", "process-execPath-Completed");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The process.argv property returns an array containing the command-line arguments passed
|
||||
/// when the Node.js process was launched. The first element will be process.execPath. See
|
||||
/// process.argv0 if access to the original value of argv[0] is needed. The second element
|
||||
/// will be the path to the JavaScript file being executed. The remaining elements will be
|
||||
/// any additional command-line arguments
|
||||
/// </summary>
|
||||
public Task<string[]> ArgvAsync
|
||||
{
|
||||
get
|
||||
{
|
||||
return BridgeConnector.GetArrayOverSocketAsync<string[]>(
|
||||
"process-argv", "process-argv-Completed");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The process.execPath property returns the absolute pathname of the executable that
|
||||
/// started the Node.js process. Symbolic links, if any, are resolved.
|
||||
/// </summary>
|
||||
public Task<string> TypeAsync
|
||||
{
|
||||
get
|
||||
{
|
||||
return BridgeConnector.GetValueOverSocketAsync<string>(
|
||||
"process-type", "process-type-Completed");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The process.versions property returns an object listing the version strings of
|
||||
/// chrome and electron.
|
||||
/// </summary>
|
||||
public Task<ProcessVersions> VersionsAsync
|
||||
{
|
||||
get
|
||||
{
|
||||
return BridgeConnector.GetObjectOverSocketAsync<ProcessVersions>(
|
||||
"process-versions", "process-versions-Completed");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// A Boolean. When app is started by being passed as parameter to the default app, this
|
||||
/// property is true in the main process, otherwise it is false.
|
||||
/// </summary>
|
||||
public Task<bool> DefaultAppAsync
|
||||
{
|
||||
get
|
||||
{
|
||||
return BridgeConnector.GetValueOverSocketAsync<bool>(
|
||||
"process-defaultApp", "process-defaultApp-Completed");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A Boolean, true when the current renderer context is the "main" renderer frame. If you
|
||||
/// want the ID of the current frame you should use webFrame.routingId
|
||||
/// </summary>
|
||||
public Task<bool> IsMainFrameAsync
|
||||
{
|
||||
get
|
||||
{
|
||||
return BridgeConnector.GetValueOverSocketAsync<bool>(
|
||||
"process-isMainFrame", "process-isMainFrame-Completed");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A String representing the path to the resources directory.
|
||||
/// </summary>
|
||||
public Task<string> ResourcesPathAsync
|
||||
{
|
||||
get
|
||||
{
|
||||
return BridgeConnector.GetValueOverSocketAsync<string>(
|
||||
"process-resourcesPath", "process-resourcesPath-Completed");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The number of seconds the current Node.js process has been running. The return value
|
||||
/// includes fractions of a second. Use Math.floor() to get whole seconds.
|
||||
/// </summary>
|
||||
public Task<double> UpTimeAsync
|
||||
{
|
||||
get
|
||||
{
|
||||
return BridgeConnector.GetValueOverSocketAsync<double>(
|
||||
"process-uptime", "process-uptime-Completed");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The PID of the electron process
|
||||
/// </summary>
|
||||
public Task<int> PidAsync
|
||||
{
|
||||
get
|
||||
{
|
||||
return BridgeConnector.GetValueOverSocketAsync<int>(
|
||||
"process-pid", "process-pid-Completed");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The operating system CPU architecture for which the Node.js binary was compiled
|
||||
/// </summary>
|
||||
public Task<string> ArchAsync
|
||||
{
|
||||
get
|
||||
{
|
||||
return BridgeConnector.GetValueOverSocketAsync<string>(
|
||||
"process-arch", "process-arch-Completed");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A string identifying the operating system platform on which the Node.js process is running
|
||||
/// </summary>
|
||||
public Task<string> PlatformAsync
|
||||
{
|
||||
get
|
||||
{
|
||||
return BridgeConnector.GetValueOverSocketAsync<string>(
|
||||
"process-platform", "process-platform-Completed");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,7 @@ namespace ElectronNET.API
|
||||
.AddSingleton(provider => PowerMonitor.Instance)
|
||||
.AddSingleton(provider => NativeTheme.Instance)
|
||||
.AddSingleton(provider => Dock.Instance)
|
||||
.AddSingleton(provider => Process.Instance)
|
||||
.AddSingleton(provider => new ApplicationSocket { Socket = BridgeConnector.Socket, })
|
||||
// this set for proper dependency injection
|
||||
.AddSingleton<IIpcMain>(provider => IpcMain.Instance)
|
||||
|
||||
Reference in New Issue
Block a user