Skip to content

Commit c01ef40

Browse files
Todd Schaveyhillin
authored andcommitted
fix #647 add to ElectronNET.API Process member interfaces for various fields
1 parent dc019ad commit c01ef40

File tree

6 files changed

+314
-66
lines changed

6 files changed

+314
-66
lines changed

src/ElectronNET.API/BridgeConnector.cs

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
namespace ElectronNET.API
1+
using System;
2+
using Newtonsoft.Json.Linq;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
6+
namespace ElectronNET.API
27
{
38
internal static class BridgeConnector
49
{
@@ -28,6 +33,82 @@ public static SocketIoFacade Socket
2833

2934
return _socket;
3035
}
36+
}
37+
38+
public static async Task<T> GetValueOverSocketAsync<T>(string eventString, string eventCompletedString)
39+
{
40+
CancellationToken cancellationToken = new();
41+
cancellationToken.ThrowIfCancellationRequested();
42+
43+
var taskCompletionSource = new TaskCompletionSource<T>();
44+
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
45+
{
46+
BridgeConnector.Socket.On(eventCompletedString, (value) =>
47+
{
48+
BridgeConnector.Socket.Off(eventCompletedString);
49+
50+
if (value == null)
51+
{
52+
Console.WriteLine($"ERROR: BridgeConnector (event: '{eventString}') returned null. Socket loop hang.");
53+
taskCompletionSource.SetCanceled();
54+
return;
55+
}
56+
57+
try
58+
{
59+
taskCompletionSource.SetResult( new JValue(value).ToObject<T>() );
60+
}
61+
catch (Exception e)
62+
{
63+
Console.WriteLine($"ERROR: BridgeConnector (event: '{eventString}') exception: {e.Message}. Socket loop hung.");
64+
}
65+
});
66+
67+
BridgeConnector.Socket.Emit(eventString);
68+
69+
return await taskCompletionSource.Task.ConfigureAwait(false);
70+
}
71+
}
72+
73+
public static async Task<T> GetObjectOverSocketAsync<T>(string eventString, string eventCompletedString)
74+
{
75+
CancellationToken cancellationToken = new();
76+
cancellationToken.ThrowIfCancellationRequested();
77+
78+
var taskCompletionSource = new TaskCompletionSource<T>();
79+
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
80+
{
81+
BridgeConnector.Socket.On(eventCompletedString, (value) =>
82+
{
83+
BridgeConnector.Socket.Off(eventCompletedString);
84+
taskCompletionSource.SetResult( ((JObject)value).ToObject<T>() );
85+
});
86+
87+
BridgeConnector.Socket.Emit(eventString);
88+
89+
return await taskCompletionSource.Task.ConfigureAwait(false);
90+
}
91+
}
92+
93+
public static async Task<T> GetArrayOverSocketAsync<T>(string eventString, string eventCompletedString)
94+
{
95+
CancellationToken cancellationToken = new();
96+
cancellationToken.ThrowIfCancellationRequested();
97+
98+
var taskCompletionSource = new TaskCompletionSource<T>();
99+
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
100+
{
101+
BridgeConnector.Socket.On(eventCompletedString, (value) =>
102+
{
103+
BridgeConnector.Socket.Off(eventCompletedString);
104+
taskCompletionSource.SetResult( ((JArray)value).ToObject<T>() );
105+
});
106+
107+
BridgeConnector.Socket.Emit(eventString);
108+
109+
return await taskCompletionSource.Task.ConfigureAwait(false);
110+
}
31111
}
112+
32113
}
33114
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace ElectronNET.API
2+
{
3+
/// <summary>
4+
///
5+
/// </summary>
6+
public class Versions
7+
{
8+
/// <summary>
9+
/// Gets or sets a value representing Chrome's version string.
10+
/// </summary>
11+
public string Chrome { get; set; }
12+
13+
/// <summary>
14+
/// Gets or sets a value representing Electron's version string.
15+
/// </summary>
16+
public bool Electron { get; set; }
17+
}
18+
}

src/ElectronNET.API/Process.cs

Lines changed: 111 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -37,92 +37,145 @@ internal static Process Instance
3737
private static readonly object _syncRoot = new();
3838

3939
/// <summary>
40-
/// The process.execPath property returns the absolute pathname of the executable that started the Node.js process. Symbolic links, if any, are resolved.
41-
/// </summary>
42-
/// <example>
43-
/// <code>
44-
/// var path = await Electron.Process.ExecPathAsync;
45-
/// </code>
46-
/// </example>
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>
4743
public Task<string> ExecPathAsync
4844
{
4945
get
5046
{
51-
CancellationToken cancellationToken = new();
52-
cancellationToken.ThrowIfCancellationRequested();
53-
54-
var taskCompletionSource = new TaskCompletionSource<string>();
55-
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
56-
{
57-
BridgeConnector.Socket.On("process-execPathCompleted", (text) =>
58-
{
59-
BridgeConnector.Socket.Off("process-execPathCompleted");
60-
taskCompletionSource.SetResult((string) text);
61-
});
62-
63-
BridgeConnector.Socket.Emit("process-execPath");
64-
65-
return taskCompletionSource.Task;
66-
}
47+
return BridgeConnector.GetValueOverSocketAsync<string>(
48+
"process-execPath", "process-execPath-Completed");
6749
}
6850
}
6951

7052
/// <summary>
71-
/// TBD
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
7258
/// </summary>
73-
/// <value></value>
7459
public Task<string[]> ArgvAsync
7560
{
7661
get
7762
{
78-
CancellationToken cancellationToken = new();
79-
cancellationToken.ThrowIfCancellationRequested();
63+
return BridgeConnector.GetArrayOverSocketAsync<string[]>(
64+
"process-argv", "process-argv-Completed");
65+
}
66+
}
8067

81-
var taskCompletionSource = new TaskCompletionSource<string[]>();
82-
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
83-
{
84-
BridgeConnector.Socket.On("process-argvCompleted", (value) =>
85-
{
86-
BridgeConnector.Socket.Off("process-argvCompleted");
87-
taskCompletionSource.SetResult( ((JArray)value).ToObject<string[]>() );
88-
});
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+
///
84+
/// </summary>
85+
public Task<Versions> VersionsAsync
86+
{
87+
get
88+
{
89+
return BridgeConnector.GetValueOverSocketAsync<Versions>(
90+
"process-versions", "process-versions-Completed");
91+
}
92+
}
93+
94+
95+
/// <summary>
96+
///
97+
/// </summary>
98+
public Task<bool> DefaultAppAsync
99+
{
100+
get
101+
{
102+
return BridgeConnector.GetValueOverSocketAsync<bool>(
103+
"process-defaultApp", "process-defaultApp-Completed");
104+
}
105+
}
89106

90-
BridgeConnector.Socket.Emit("process-argv");
107+
/// <summary>
108+
///
109+
/// </summary>
110+
public Task<bool> IsMainFrameAsync
111+
{
112+
get
113+
{
114+
return BridgeConnector.GetValueOverSocketAsync<bool>(
115+
"process-isMainFrame", "process-isMainFrame-Completed");
116+
}
117+
}
91118

92-
return taskCompletionSource.Task;
93-
}
119+
/// <summary>
120+
///
121+
/// </summary>
122+
public Task<string> ResourcesPathAsync
123+
{
124+
get
125+
{
126+
return BridgeConnector.GetValueOverSocketAsync<string>(
127+
"process-resourcesPath", "process-resourcesPath-Completed");
94128
}
95129
}
96130

97131
/// <summary>
98-
/// The process.execPath property returns the absolute pathname of the executable that started the Node.js process. Symbolic links, if any, are resolved.
132+
///
99133
/// </summary>
100-
/// <example>
101-
/// <code>
102-
/// var path = await Electron.Process.ExecPathAsync;
103-
/// </code>
104-
/// </example>
105-
public Task<string> TypeAsync
134+
public Task<double> UpTimeAsync
106135
{
107136
get
108137
{
109-
CancellationToken cancellationToken = new();
110-
cancellationToken.ThrowIfCancellationRequested();
138+
return BridgeConnector.GetValueOverSocketAsync<double>(
139+
"process-uptime", "process-uptime-Completed");
140+
}
141+
}
111142

112-
var taskCompletionSource = new TaskCompletionSource<string>();
113-
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
114-
{
115-
BridgeConnector.Socket.On("process-typeCompleted", (text) =>
116-
{
117-
BridgeConnector.Socket.Off("process-typeCompleted");
118-
taskCompletionSource.SetResult((string) text);
119-
});
143+
/// <summary>
144+
///
145+
/// </summary>
146+
public Task<int> PidAsync
147+
{
148+
get
149+
{
150+
return BridgeConnector.GetValueOverSocketAsync<int>(
151+
"process-pid", "process-pid-Completed");
152+
}
153+
}
120154

121-
BridgeConnector.Socket.Emit("process-type");
122155

123-
return taskCompletionSource.Task;
124-
}
156+
/// <summary>
157+
///
158+
/// </summary>
159+
public Task<string> ArchAsync
160+
{
161+
get
162+
{
163+
return BridgeConnector.GetValueOverSocketAsync<string>(
164+
"process-arch", "process-arch-Completed");
165+
}
166+
}
167+
168+
/// <summary>
169+
///
170+
/// </summary>
171+
public Task<string> PlatformAsync
172+
{
173+
get
174+
{
175+
return BridgeConnector.GetValueOverSocketAsync<string>(
176+
"process-platform", "process-platform-Completed");
125177
}
126178
}
179+
127180
}
128181
}

src/ElectronNET.Host/api/process.js

Lines changed: 47 additions & 3 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 & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)