Skip to content

Commit 1a1bd61

Browse files
Merge fix: Support launching app with file for win and linux #648
2 parents 709780f + 64e058b commit 1a1bd61

File tree

11 files changed

+450
-1
lines changed

11 files changed

+450
-1
lines changed

ElectronNET.API/BridgeConnector.cs

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.Linq;
@@ -298,6 +298,7 @@ private static SocketIO Socket
298298
}
299299
}
300300

301+
<<<<<<< HEAD
301302
private class CamelCaseNewtonsoftJsonSerializer : NewtonsoftJsonSerializer
302303
{
303304
public CamelCaseNewtonsoftJsonSerializer(int eio) : base(eio)
@@ -314,5 +315,111 @@ public override JsonSerializerSettings CreateOptions()
314315
};
315316
}
316317
}
318+
=======
319+
public static async Task<T> GetValueOverSocketAsync<T>(string eventString, string eventCompletedString)
320+
{
321+
CancellationToken cancellationToken = new();
322+
cancellationToken.ThrowIfCancellationRequested();
323+
324+
var taskCompletionSource = new TaskCompletionSource<T>();
325+
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
326+
{
327+
BridgeConnector.Socket.On(eventCompletedString, (value) =>
328+
{
329+
BridgeConnector.Socket.Off(eventCompletedString);
330+
331+
if (value == null)
332+
{
333+
Console.WriteLine($"ERROR: BridgeConnector (event: '{eventString}') returned null. Socket loop hang.");
334+
taskCompletionSource.SetCanceled();
335+
return;
336+
}
337+
338+
try
339+
{
340+
taskCompletionSource.SetResult( new JValue(value).ToObject<T>() );
341+
}
342+
catch (Exception e)
343+
{
344+
Console.WriteLine($"ERROR: BridgeConnector (event: '{eventString}') exception: {e.Message}. Socket loop hung.");
345+
}
346+
});
347+
348+
BridgeConnector.Socket.Emit(eventString);
349+
350+
return await taskCompletionSource.Task.ConfigureAwait(false);
351+
}
352+
}
353+
354+
public static async Task<T> GetObjectOverSocketAsync<T>(string eventString, string eventCompletedString)
355+
{
356+
CancellationToken cancellationToken = new();
357+
cancellationToken.ThrowIfCancellationRequested();
358+
359+
var taskCompletionSource = new TaskCompletionSource<T>();
360+
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
361+
{
362+
BridgeConnector.Socket.On(eventCompletedString, (value) =>
363+
{
364+
BridgeConnector.Socket.Off(eventCompletedString);
365+
366+
if (value == null)
367+
{
368+
Console.WriteLine($"ERROR: BridgeConnector (event: '{eventString}') returned null. Socket loop hang.");
369+
taskCompletionSource.SetCanceled();
370+
return;
371+
}
372+
373+
try
374+
{
375+
taskCompletionSource.SetResult( ((JObject)value).ToObject<T>() );
376+
}
377+
catch (Exception e)
378+
{
379+
Console.WriteLine($"ERROR: BridgeConnector (event: '{eventString}') exception: {e.Message}. Socket loop hung.");
380+
}
381+
});
382+
383+
BridgeConnector.Socket.Emit(eventString);
384+
385+
return await taskCompletionSource.Task.ConfigureAwait(false);
386+
}
387+
}
388+
389+
public static async Task<T> GetArrayOverSocketAsync<T>(string eventString, string eventCompletedString)
390+
{
391+
CancellationToken cancellationToken = new();
392+
cancellationToken.ThrowIfCancellationRequested();
393+
394+
var taskCompletionSource = new TaskCompletionSource<T>();
395+
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
396+
{
397+
BridgeConnector.Socket.On(eventCompletedString, (value) =>
398+
{
399+
BridgeConnector.Socket.Off(eventCompletedString);
400+
if (value == null)
401+
{
402+
Console.WriteLine($"ERROR: BridgeConnector (event: '{eventString}') returned null. Socket loop hang.");
403+
taskCompletionSource.SetCanceled();
404+
return;
405+
}
406+
407+
try
408+
{
409+
taskCompletionSource.SetResult( ((JArray)value).ToObject<T>() );
410+
}
411+
catch (Exception e)
412+
{
413+
Console.WriteLine($"ERROR: BridgeConnector (event: '{eventString}') exception: {e.Message}. Socket loop hung.");
414+
}
415+
});
416+
417+
BridgeConnector.Socket.Emit(eventString);
418+
419+
return await taskCompletionSource.Task.ConfigureAwait(false);
420+
}
421+
}
422+
423+
>>>>>>> 64e058b0b59a4fbbd6c0ec9de2ee1850a66684c4
317424
}
318425
}

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
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace ElectronNET.API
2+
{
3+
/// <summary>
4+
/// An object listing the version strings specific to Electron
5+
/// </summary>
6+
/// <param name="Chrome">Value representing Chrome's version string</param>
7+
/// <param name="Electron">Value representing Electron's version string</param>
8+
/// <returns></returns>
9+
public record ProcessVersions(string Chrome, string Electron);
10+
}

ElectronNET.API/Process.cs

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
using Newtonsoft.Json;
4+
using Newtonsoft.Json.Linq;
5+
6+
namespace ElectronNET.API
7+
{
8+
/// <summary>
9+
/// Electron's process object is extended from the Node.js process object. It adds the
10+
/// events, properties, and methods.
11+
/// </summary>
12+
public sealed class Process
13+
{
14+
internal Process() { }
15+
16+
internal static Process Instance
17+
{
18+
get
19+
{
20+
if (_process == null)
21+
{
22+
lock (_syncRoot)
23+
{
24+
if (_process == null)
25+
{
26+
_process = new Process();
27+
}
28+
}
29+
}
30+
31+
return _process;
32+
}
33+
}
34+
35+
private static Process _process;
36+
37+
private static readonly object _syncRoot = new();
38+
39+
/// <summary>
40+
/// The process.execPath property returns the absolute pathname of the executable that
41+
/// started the Node.js process. Symbolic links, if any, are resolved.
42+
/// </summary>
43+
public Task<string> ExecPathAsync
44+
{
45+
get
46+
{
47+
return BridgeConnector.GetValueOverSocketAsync<string>(
48+
"process-execPath", "process-execPath-Completed");
49+
}
50+
}
51+
52+
/// <summary>
53+
/// The process.argv property returns an array containing the command-line arguments passed
54+
/// when the Node.js process was launched. The first element will be process.execPath. See
55+
/// process.argv0 if access to the original value of argv[0] is needed. The second element
56+
/// will be the path to the JavaScript file being executed. The remaining elements will be
57+
/// any additional command-line arguments
58+
/// </summary>
59+
public Task<string[]> ArgvAsync
60+
{
61+
get
62+
{
63+
return BridgeConnector.GetArrayOverSocketAsync<string[]>(
64+
"process-argv", "process-argv-Completed");
65+
}
66+
}
67+
68+
/// <summary>
69+
/// The process.execPath property returns the absolute pathname of the executable that
70+
/// started the Node.js process. Symbolic links, if any, are resolved.
71+
/// </summary>
72+
public Task<string> TypeAsync
73+
{
74+
get
75+
{
76+
return BridgeConnector.GetValueOverSocketAsync<string>(
77+
"process-type", "process-type-Completed");
78+
}
79+
}
80+
81+
82+
/// <summary>
83+
/// The process.versions property returns an object listing the version strings of
84+
/// chrome and electron.
85+
/// </summary>
86+
public Task<ProcessVersions> VersionsAsync
87+
{
88+
get
89+
{
90+
return BridgeConnector.GetObjectOverSocketAsync<ProcessVersions>(
91+
"process-versions", "process-versions-Completed");
92+
}
93+
}
94+
95+
96+
/// <summary>
97+
/// A Boolean. When app is started by being passed as parameter to the default app, this
98+
/// property is true in the main process, otherwise it is false.
99+
/// </summary>
100+
public Task<bool> DefaultAppAsync
101+
{
102+
get
103+
{
104+
return BridgeConnector.GetValueOverSocketAsync<bool>(
105+
"process-defaultApp", "process-defaultApp-Completed");
106+
}
107+
}
108+
109+
/// <summary>
110+
/// A Boolean, true when the current renderer context is the "main" renderer frame. If you
111+
/// want the ID of the current frame you should use webFrame.routingId
112+
/// </summary>
113+
public Task<bool> IsMainFrameAsync
114+
{
115+
get
116+
{
117+
return BridgeConnector.GetValueOverSocketAsync<bool>(
118+
"process-isMainFrame", "process-isMainFrame-Completed");
119+
}
120+
}
121+
122+
/// <summary>
123+
/// A String representing the path to the resources directory.
124+
/// </summary>
125+
public Task<string> ResourcesPathAsync
126+
{
127+
get
128+
{
129+
return BridgeConnector.GetValueOverSocketAsync<string>(
130+
"process-resourcesPath", "process-resourcesPath-Completed");
131+
}
132+
}
133+
134+
/// <summary>
135+
/// The number of seconds the current Node.js process has been running. The return value
136+
/// includes fractions of a second. Use Math.floor() to get whole seconds.
137+
/// </summary>
138+
public Task<double> UpTimeAsync
139+
{
140+
get
141+
{
142+
return BridgeConnector.GetValueOverSocketAsync<double>(
143+
"process-uptime", "process-uptime-Completed");
144+
}
145+
}
146+
147+
/// <summary>
148+
/// The PID of the electron process
149+
/// </summary>
150+
public Task<int> PidAsync
151+
{
152+
get
153+
{
154+
return BridgeConnector.GetValueOverSocketAsync<int>(
155+
"process-pid", "process-pid-Completed");
156+
}
157+
}
158+
159+
160+
/// <summary>
161+
/// The operating system CPU architecture for which the Node.js binary was compiled
162+
/// </summary>
163+
public Task<string> ArchAsync
164+
{
165+
get
166+
{
167+
return BridgeConnector.GetValueOverSocketAsync<string>(
168+
"process-arch", "process-arch-Completed");
169+
}
170+
}
171+
172+
/// <summary>
173+
/// A string identifying the operating system platform on which the Node.js process is running
174+
/// </summary>
175+
public Task<string> PlatformAsync
176+
{
177+
get
178+
{
179+
return BridgeConnector.GetValueOverSocketAsync<string>(
180+
"process-platform", "process-platform-Completed");
181+
}
182+
}
183+
184+
}
185+
}

ElectronNET.API/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,6 @@ public static IServiceCollection AddElectron(this IServiceCollection services)
5050
.AddSingleton<INativeTheme>(provider => NativeTheme.Instance)
5151
.AddSingleton<IDock>(provider => Dock.Instance)
5252
.AddSingleton<IApplicationSocket>(provider => provider.GetService<ApplicationSocket>());
53+
.AddSingleton(provider => Process.Instance);
5354
}
5455
}

ElectronNET.CLI/Commands/Actions/DeployEmbeddedElectronFiles.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public static void Do(string tempPath)
3535
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "browserView.js", "api.");
3636
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "powerMonitor.js", "api.");
3737
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "nativeTheme.js", "api.");
38+
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "process.js", "api.");
3839

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

ElectronNET.CLI/ElectronNET.CLI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
<EmbeddedResource Include="..\ElectronNET.Host\api\browserView.js" Link="ElectronHost\api\browserView.js" />
7474
<EmbeddedResource Include="..\ElectronNET.Host\api\powerMonitor.js" Link="ElectronHost\api\powerMonitor.js" />
7575
<EmbeddedResource Include="..\ElectronNET.Host\api\nativeTheme.js" Link="ElectronHost\api\nativeTheme.js" />
76+
<EmbeddedResource Include="..\ElectronNET.Host\api\process.js" Link="ElectronHost\api\process.js" />
7677
</ItemGroup>
7778

7879
<ItemGroup>

0 commit comments

Comments
 (0)