Reintroduce #648
This commit is contained in:
Florian Rappl
2025-06-22 07:53:30 +02:00
committed by GitHub
11 changed files with 445 additions and 2 deletions

View File

@@ -1,4 +1,9 @@
namespace ElectronNET.API
using System;
using Newtonsoft.Json.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace ElectronNET.API
{
internal static class BridgeConnector
{
@@ -28,6 +33,82 @@
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);
taskCompletionSource.SetResult( ((JObject)value).ToObject<T>() );
});
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);
taskCompletionSource.SetResult( ((JArray)value).ToObject<T>() );
});
BridgeConnector.Socket.Emit(eventString);
return await taskCompletionSource.Task.ConfigureAwait(false);
}
}
}
}

View File

@@ -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; } }
}
}

View 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);
}

View 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.GetValueOverSocketAsync<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");
}
}
}
}

View File

@@ -28,6 +28,7 @@ namespace ElectronNET.API
.AddSingleton(provider => HostHook.Instance)
.AddSingleton(provider => PowerMonitor.Instance)
.AddSingleton(provider => NativeTheme.Instance)
.AddSingleton(provider => Dock.Instance);
.AddSingleton(provider => Dock.Instance)
.AddSingleton(provider => Process.Instance);
}
}

View File

@@ -42,6 +42,7 @@ namespace ElectronNET.CLI.Commands.Actions
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "browserView.js", "api.");
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "powerMonitor.js", "api.");
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "nativeTheme.js", "api.");
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "process.js", "api.");
string splashscreenFolder = Path.Combine(tempPath, "splashscreen");
if (Directory.Exists(splashscreenFolder) == false)

View File

@@ -68,6 +68,7 @@
<EmbeddedResource Include="..\ElectronNET.Host\api\browserView.js" Link="ElectronHost\api\browserView.js" />
<EmbeddedResource Include="..\ElectronNET.Host\api\powerMonitor.js" Link="ElectronHost\api\powerMonitor.js" />
<EmbeddedResource Include="..\ElectronNET.Host\api\nativeTheme.js" Link="ElectronHost\api\nativeTheme.js" />
<EmbeddedResource Include="..\ElectronNET.Host\api\process.js" Link="ElectronHost\api\process.js" />
<EmbeddedResource Include="..\ElectronNET.Host\.vscode\launch.json" Link="ElectronHost\.vscode\launch.json" />
<EmbeddedResource Include="..\ElectronNET.Host\.vscode\tasks.json" Link="ElectronHost\.vscode\tasks.json" />
</ItemGroup>

View File

@@ -0,0 +1,62 @@
"use strict";
let electronSocket;
module.exports = (socket) => {
electronSocket = socket;
socket.on('process-execPath', () => {
const value = process.execPath;
electronSocket.emit('process-execPath-Completed', value);
});
socket.on('process-argv', () => {
const value = process.argv;
electronSocket.emit('process-argv-Completed', value);
});
socket.on('process-type', () => {
const value = process.type;
electronSocket.emit('process-type-Completed', value);
});
socket.on('process-versions', () => {
const value = process.versions;
electronSocket.emit('process-versions-Completed', value);
});
socket.on('process-defaultApp', () => {
if (process.defaultApp === undefined) {
electronSocket.emit('process-defaultApp-Completed', false);
return;
}
electronSocket.emit('process-defaultApp-Completed', process.defaultApp);
});
socket.on('process-isMainFrame', () => {
if (process.isMainFrame === undefined) {
electronSocket.emit('process-isMainFrame-Completed', false);
return;
}
electronSocket.emit('process-isMainFrame-Completed', process.isMainFrame);
});
socket.on('process-resourcesPath', () => {
const value = process.resourcesPath;
electronSocket.emit('process-resourcesPath-Completed', value);
});
socket.on('process-uptime', () => {
let value = process.uptime();
if (value === undefined) {
value = -1;
}
electronSocket.emit('process-uptime-Completed', value);
});
socket.on('process-pid', () => {
if (process.pid === undefined) {
electronSocket.emit('process-pid-Completed', -1);
return;
}
electronSocket.emit('process-pid-Completed', process.pid);
});
socket.on('process-arch', () => {
const value = process.arch;
electronSocket.emit('process-arch-Completed', value);
});
socket.on('process-platform', () => {
const value = process.platform;
electronSocket.emit('process-platform-Completed', value);
});
};
//# sourceMappingURL=process.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"process.js","sourceRoot":"","sources":["process.ts"],"names":[],"mappings":";AACA,IAAI,cAAc,CAAC;AAEnB,iBAAS,CAAC,MAAc,EAAE,EAAE;IACxB,cAAc,GAAG,MAAM,CAAC;IAExB,MAAM,CAAC,EAAE,CAAC,kBAAkB,EAAE,GAAG,EAAE;QAC/B,MAAM,KAAK,GAAG,OAAO,CAAC,QAAQ,CAAC;QAC/B,cAAc,CAAC,IAAI,CAAC,4BAA4B,EAAE,KAAK,CAAC,CAAC;IAC7D,CAAC,CAAC,CAAC;IAEH,MAAM,CAAC,EAAE,CAAC,cAAc,EAAE,GAAG,EAAE;QAC3B,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC;QAC3B,cAAc,CAAC,IAAI,CAAC,wBAAwB,EAAE,KAAK,CAAC,CAAC;IACzD,CAAC,CAA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AAG,OAAO,CAAC,MAAM,EAAE,CAAC;QAC7B,IAAI,KAAK,KAAK,SAAS,EAAE;YACrB,KAAK,GAAG,CAAC,CAAC,CAAC;SACd;QACD,cAAc,CAAC,IAAI,CAAC,0BAA0B,EAAE,KAAK,CAAC,CAAC;IAC3D,CAAC,CAAC,CAAC;IAEH,MAAM,CAAC,EAAE,CAAC,aAAa,EAAE,GAAG,EAAE;QAC1B,IAAI,OAAO,CAAC,GAAG,KAAK,SAAS,EAAE;YAC3B,cAAc,CAAC,IAAI,CAAC,uBAAuB,EAAE,CAAC,CAAC,CAAC,CAAC;YACjD,OAAO;SACV;QACD,cAAc,CAAC,IAAI,CAAC,uBAAuB,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC;IAC9D,CAAC,CAAC,CAAC;IAEH,MAAM,CAAC,EAAE,CAAC,cAAc,EAAE,GAAG,EAAE;QAC3B,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC;QAC3B,cAAc,CAAC,IAAI,CAAC,wBAAwB,EAAE,KAAK,CAAC,CAAC;IACzD,CAAC,CAAC,CAAC;IAEH,MAAM,CAAC,EAAE,CAAC,kBAAkB,EAAE,GAAG,EAAE;QAC/B,MAAM,KAAK,GAAG,OAAO,CAAC,QAAQ,CAAC;QAC/B,cAAc,CAAC,IAAI,CAAC,4BAA4B,EAAE,KAAK,CAAC,CAAC;IAC7D,CAAC,CAAC,CAAA;AACN,CAAC,CAAC"}

View File

@@ -0,0 +1,73 @@
import { Socket } from 'net';
let electronSocket;
export = (socket: Socket) => {
electronSocket = socket;
socket.on('process-execPath', () => {
const value = process.execPath;
electronSocket.emit('process-execPath-Completed', value);
});
socket.on('process-argv', () => {
const value = process.argv;
electronSocket.emit('process-argv-Completed', value);
});
socket.on('process-type', () => {
const value = process.type;
electronSocket.emit('process-type-Completed', value);
});
socket.on('process-versions', () => {
const value = process.versions;
electronSocket.emit('process-versions-Completed', value);
});
socket.on('process-defaultApp', () => {
if (process.defaultApp === undefined) {
electronSocket.emit('process-defaultApp-Completed', false);
return;
}
electronSocket.emit('process-defaultApp-Completed', process.defaultApp);
});
socket.on('process-isMainFrame', () => {
if (process.isMainFrame === undefined) {
electronSocket.emit('process-isMainFrame-Completed', false);
return;
}
electronSocket.emit('process-isMainFrame-Completed', process.isMainFrame);
});
socket.on('process-resourcesPath', () => {
const value = process.resourcesPath;
electronSocket.emit('process-resourcesPath-Completed', value);
});
socket.on('process-uptime', () => {
let value = process.uptime();
if (value === undefined) {
value = -1;
}
electronSocket.emit('process-uptime-Completed', value);
});
socket.on('process-pid', () => {
if (process.pid === undefined) {
electronSocket.emit('process-pid-Completed', -1);
return;
}
electronSocket.emit('process-pid-Completed', process.pid);
});
socket.on('process-arch', () => {
const value = process.arch;
electronSocket.emit('process-arch-Completed', value);
});
socket.on('process-platform', () => {
const value = process.platform;
electronSocket.emit('process-platform-Completed', value);
})
};

View File

@@ -15,9 +15,31 @@ let nativeTheme;
let dock;
let launchFile;
let launchUrl;
let processApi;
let manifestJsonFileName = 'electron.manifest.json';
let watchable = false;
// handle for opening the app with a file for win and linux
if (process && process.argv.length > 1) {
let firstAppArgument = process.argv[1];
// With invoked via electronize, the first argument is the path to the main.js.
// Per issue #337, the /args switch can also be present. If either are present,
// we need to check the subsequent argument.
if (firstAppArgument === '..\\..\\main.js' || firstAppArgument === '../../main.js' || firstAppArgument === '/args') {
if (process.argv.length > 2) {
firstAppArgument = process.argv[2];
}
}
// only append the first app arg if it is not already a switch
if (!firstAppArgument.startsWith("--")) {
app.commandLine.appendSwitch("open-file", firstAppArgument);
}
}
if (app.commandLine.hasSwitch('manifest')) {
manifestJsonFileName = app.commandLine.getSwitchValue('manifest');
}
@@ -229,6 +251,7 @@ function startSocketApiBridge(port) {
if (powerMonitor === undefined) powerMonitor = require('./api/powerMonitor')(socket);
if (nativeTheme === undefined) nativeTheme = require('./api/nativeTheme')(socket);
if (dock === undefined) dock = require('./api/dock')(socket);
if (processApi === undefined) processApi = require('./api/process')(socket);
socket.on('register-app-open-file-event', (id) => {
global['electronsocket'] = socket;