Skip to content

Commit f7de17f

Browse files
Todd Schaveyhillin
authored andcommitted
fix #647 add initial Process class to ElectronNET.API
1 parent ca74838 commit f7de17f

File tree

9 files changed

+94
-1
lines changed

9 files changed

+94
-1
lines changed

src/ElectronNET.API/Electron.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,10 @@ public static class Electron
8888
/// Control your app in the macOS dock.
8989
/// </summary>
9090
public static Dock Dock { get { return Dock.Instance; } }
91+
92+
/// <summary>
93+
/// Electeon extensions to the Nodejs process object.
94+
/// </summary>
95+
public static Process Process { get { return Process.Instance; } }
9196
}
9297
}

src/ElectronNET.API/Process.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
4+
namespace ElectronNET.API
5+
{
6+
/// <summary>
7+
/// Electron's process object is extended from the Node.js process object. It adds the
8+
/// events, properties, and methods.
9+
/// </summary>
10+
public sealed class Process
11+
{
12+
internal Process() { }
13+
14+
internal static Process Instance
15+
{
16+
get
17+
{
18+
if (_process == null)
19+
{
20+
lock (_syncRoot)
21+
{
22+
if (_process == null)
23+
{
24+
_process = new Process();
25+
}
26+
}
27+
}
28+
29+
return _process;
30+
}
31+
}
32+
33+
private static Process _process;
34+
35+
private static readonly object _syncRoot = new();
36+
37+
/// <summary>
38+
/// TBD
39+
/// </summary>
40+
/// <value></value>
41+
public async Task<string> GetExecPathAsync(CancellationToken cancellationToken = default)
42+
{
43+
cancellationToken.ThrowIfCancellationRequested();
44+
45+
var taskCompletionSource = new TaskCompletionSource<string>();
46+
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
47+
{
48+
BridgeConnector.Socket.On("process-execPathCompleted", (text) =>
49+
{
50+
BridgeConnector.Socket.Off("process-execPathCompleted");
51+
taskCompletionSource.SetResult((string) text);
52+
});
53+
54+
BridgeConnector.Socket.Emit("process-execPath");
55+
56+
return await taskCompletionSource.Task
57+
.ConfigureAwait(false);
58+
}
59+
}
60+
}
61+
}

src/ElectronNET.API/ServiceCollectionExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public static IServiceCollection AddElectron(this IServiceCollection services)
2828
.AddSingleton(provider => HostHook.Instance)
2929
.AddSingleton(provider => PowerMonitor.Instance)
3030
.AddSingleton(provider => NativeTheme.Instance)
31-
.AddSingleton(provider => Dock.Instance);
31+
.AddSingleton(provider => Dock.Instance)
32+
.AddSingleton(provider => Process.Instance);
3233
}
3334
}

src/ElectronNET.CLI/Commands/Actions/DeployEmbeddedElectronFiles.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public static void Do(string tempPath)
4242
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "browserView.js", "api.");
4343
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "powerMonitor.js", "api.");
4444
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "nativeTheme.js", "api.");
45+
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "process.js", "api.");
4546

4647
string splashscreenFolder = Path.Combine(tempPath, "splashscreen");
4748
if (Directory.Exists(splashscreenFolder) == false)

src/ElectronNET.CLI/ElectronNET.CLI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<EmbeddedResource Include="..\ElectronNET.Host\api\browserView.js" Link="ElectronHost\api\browserView.js" />
6969
<EmbeddedResource Include="..\ElectronNET.Host\api\powerMonitor.js" Link="ElectronHost\api\powerMonitor.js" />
7070
<EmbeddedResource Include="..\ElectronNET.Host\api\nativeTheme.js" Link="ElectronHost\api\nativeTheme.js" />
71+
<EmbeddedResource Include="..\ElectronNET.Host\api\process.js" Link="ElectronHost\api\process.js" />
7172
<EmbeddedResource Include="..\ElectronNET.Host\.vscode\launch.json" Link="ElectronHost\.vscode\launch.json" />
7273
<EmbeddedResource Include="..\ElectronNET.Host\.vscode\tasks.json" Link="ElectronHost\.vscode\tasks.json" />
7374
</ItemGroup>

src/ElectronNET.Host/api/process.js

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ElectronNET.Host/api/process.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ElectronNET.Host/api/process.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Socket } from 'net';
2+
let electronSocket;
3+
4+
export = (socket: Socket) => {
5+
electronSocket = socket;
6+
7+
socket.on('process-execPath', () => {
8+
const value = process.execPath;
9+
electronSocket.emit('process-execPathCompleted', value);
10+
});
11+
};

src/ElectronNET.Host/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ let nativeTheme;
1515
let dock;
1616
let launchFile;
1717
let launchUrl;
18+
let processApi;
1819

1920
let manifestJsonFileName = 'electron.manifest.json';
2021
let watchable = false;
@@ -250,6 +251,7 @@ function startSocketApiBridge(port) {
250251
if (powerMonitor === undefined) powerMonitor = require('./api/powerMonitor')(socket);
251252
if (nativeTheme === undefined) nativeTheme = require('./api/nativeTheme')(socket);
252253
if (dock === undefined) dock = require('./api/dock')(socket);
254+
if (processApi === undefined) processApi = require('./api/process')(socket);
253255

254256
socket.on('register-app-open-file-event', (id) => {
255257
global['electronsocket'] = socket;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy