Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/ElectronNET.API/API/ApiBase.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
namespace ElectronNET.API
{
using ElectronNET.API.Serialization;
using ElectronNET.Common;
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Threading.Tasks;
using ElectronNET.Common;

public abstract class ApiBase
{
Expand Down Expand Up @@ -156,8 +158,19 @@ public PropertyGetter(ApiBase apiBase, string callerName, int timeoutMs)

lock (this)
{
this.tcs?.SetResult(result);
this.tcs = null;
try
{
var value = result;
this.tcs?.SetResult(value);
}
catch (Exception ex)
{
this.tcs?.TrySetException(ex);
}
finally
{
this.tcs = null;
}
}
});

Expand All @@ -170,7 +183,7 @@ public PropertyGetter(ApiBase apiBase, string callerName, int timeoutMs)
BridgeConnector.Socket.Emit(messageName);
}

System.Threading.Tasks.Task.Delay(ApiBase.PropertyTimeout).ContinueWith(_ =>
System.Threading.Tasks.Task.Delay(PropertyTimeout).ContinueWith(_ =>
{
if (this.tcs != null)
{
Expand Down
91 changes: 43 additions & 48 deletions src/ElectronNET.API/API/App.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using ElectronNET.API.Entities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using ElectronNET.API.Entities;
using ElectronNET.API.Extensions;
using ElectronNET.API.Serialization;
using ElectronNET.Common;
using System;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using ElectronNET.API.Extensions;
using ElectronNET.Common;

// ReSharper disable InconsistentNaming

Expand Down Expand Up @@ -271,7 +270,7 @@ public event Action WebContentsCreated
/// <returns><see langword="true"/> when Chrome's accessibility support is enabled, <see langword="false"/> otherwise.</returns>
public event Action<bool> AccessibilitySupportChanged
{
add => ApiEventManager.AddEvent("app-accessibility-support-changed", GetHashCode(), _accessibilitySupportChanged, value, (args) => (bool)args);
add => ApiEventManager.AddEvent("app-accessibility-support-changed", GetHashCode(), _accessibilitySupportChanged, value, (args) => args.GetBoolean());
remove => ApiEventManager.RemoveEvent("app-accessibility-support-changed", GetHashCode(), _accessibilitySupportChanged, value);
}

Expand Down Expand Up @@ -414,10 +413,7 @@ internal static App Instance
private static App _app;
private static object _syncRoot = new object();

private readonly JsonSerializer _jsonSerializer = new JsonSerializer()
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};


/// <summary>
/// Try to close all windows. The <see cref="BeforeQuit"/> event will be emitted first. If all windows are successfully
Expand Down Expand Up @@ -475,7 +471,7 @@ public void Relaunch()
/// <param name="relaunchOptions">Options for the relaunch.</param>
public void Relaunch(RelaunchOptions relaunchOptions)
{
this.CallMethod1(JObject.FromObject(relaunchOptions, _jsonSerializer));
this.CallMethod1(relaunchOptions);
}

/// <summary>
Expand All @@ -495,7 +491,7 @@ public void Focus()
/// </summary>
public void Focus(FocusOptions focusOptions)
{
this.CallMethod1(JObject.FromObject(focusOptions, _jsonSerializer));
this.CallMethod1(focusOptions);
}

/// <summary>
Expand Down Expand Up @@ -551,11 +547,11 @@ public async Task<string> GetPathAsync(PathName pathName, CancellationToken canc
var taskCompletionSource = new TaskCompletionSource<string>();
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
{
BridgeConnector.Socket.On("appGetPathCompleted", (path) =>
BridgeConnector.Socket.On<string>("appGetPathCompleted", (path) =>
{
BridgeConnector.Socket.Off("appGetPathCompleted");

taskCompletionSource.SetResult(path.ToString());
taskCompletionSource.SetResult(path);
});

BridgeConnector.Socket.Emit("appGetPath", pathName.GetDescription());
Expand Down Expand Up @@ -720,10 +716,10 @@ public async Task<bool> SetAsDefaultProtocolClientAsync(string protocol, string
var taskCompletionSource = new TaskCompletionSource<bool>();
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
{
BridgeConnector.Socket.On("appSetAsDefaultProtocolClientCompleted", (success) =>
BridgeConnector.Socket.On<bool>("appSetAsDefaultProtocolClientCompleted", (success) =>
{
BridgeConnector.Socket.Off("appSetAsDefaultProtocolClientCompleted");
taskCompletionSource.SetResult((bool)success);
taskCompletionSource.SetResult(success);
});

BridgeConnector.Socket.Emit("appSetAsDefaultProtocolClient", protocol, path, args);
Expand Down Expand Up @@ -774,10 +770,10 @@ public async Task<bool> RemoveAsDefaultProtocolClientAsync(string protocol, stri
var taskCompletionSource = new TaskCompletionSource<bool>();
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
{
BridgeConnector.Socket.On("appRemoveAsDefaultProtocolClientCompleted", (success) =>
BridgeConnector.Socket.On<bool>("appRemoveAsDefaultProtocolClientCompleted", (success) =>
{
BridgeConnector.Socket.Off("appRemoveAsDefaultProtocolClientCompleted");
taskCompletionSource.SetResult((bool)success);
taskCompletionSource.SetResult(success);
});

BridgeConnector.Socket.Emit("appRemoveAsDefaultProtocolClient", protocol, path, args);
Expand Down Expand Up @@ -846,10 +842,10 @@ public async Task<bool> IsDefaultProtocolClientAsync(string protocol, string pat
var taskCompletionSource = new TaskCompletionSource<bool>();
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
{
BridgeConnector.Socket.On("appIsDefaultProtocolClientCompleted", (success) =>
BridgeConnector.Socket.On<bool>("appIsDefaultProtocolClientCompleted", (success) =>
{
BridgeConnector.Socket.Off("appIsDefaultProtocolClientCompleted");
taskCompletionSource.SetResult((bool)success);
taskCompletionSource.SetResult(success);
});

BridgeConnector.Socket.Emit("appIsDefaultProtocolClient", protocol, path, args);
Expand All @@ -874,13 +870,13 @@ public async Task<bool> SetUserTasksAsync(UserTask[] userTasks, CancellationToke
var taskCompletionSource = new TaskCompletionSource<bool>();
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
{
BridgeConnector.Socket.On("appSetUserTasksCompleted", (success) =>
BridgeConnector.Socket.On<bool>("appSetUserTasksCompleted", (success) =>
{
BridgeConnector.Socket.Off("appSetUserTasksCompleted");
taskCompletionSource.SetResult((bool)success);
taskCompletionSource.SetResult(success);
});

BridgeConnector.Socket.Emit("appSetUserTasks", JArray.FromObject(userTasks, _jsonSerializer));
BridgeConnector.Socket.Emit("appSetUserTasks", userTasks);

return await taskCompletionSource.Task
.ConfigureAwait(false);
Expand Down Expand Up @@ -916,7 +912,7 @@ public async Task<JumpListSettings> GetJumpListSettingsAsync(CancellationToken c
/// <param name="categories">Array of <see cref="JumpListCategory"/> objects.</param>
public void SetJumpList(JumpListCategory[] categories)
{
this.CallMethod1(JArray.FromObject(categories, _jsonSerializer));
this.CallMethod1(categories);
}

/// <summary>
Expand Down Expand Up @@ -947,19 +943,21 @@ public async Task<bool> RequestSingleInstanceLockAsync(Action<string[], string>
var taskCompletionSource = new TaskCompletionSource<bool>();
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
{
BridgeConnector.Socket.On("appRequestSingleInstanceLockCompleted", (success) =>
BridgeConnector.Socket.On<bool>("appRequestSingleInstanceLockCompleted", (success) =>
{
BridgeConnector.Socket.Off("appRequestSingleInstanceLockCompleted");
taskCompletionSource.SetResult((bool)success);
taskCompletionSource.SetResult(success);
});

BridgeConnector.Socket.Off("secondInstance");
BridgeConnector.Socket.On("secondInstance", (result) =>
BridgeConnector.Socket.On<JsonElement>("secondInstance", (result) =>
{
JArray results = (JArray)result;
string[] args = results.First.ToObject<string[]>();
string workingDirectory = results.Last.ToObject<string>();

var arr = result.EnumerateArray();
var e = arr.GetEnumerator();
e.MoveNext();
var args = e.Current.Deserialize<string[]>(JsonSerializerOptions.Default);
e.MoveNext();
var workingDirectory = e.Current.GetString();
newInstanceOpened(args, workingDirectory);
});

Expand Down Expand Up @@ -1071,13 +1069,13 @@ public async Task<int> ImportCertificateAsync(ImportCertificateOptions options,
var taskCompletionSource = new TaskCompletionSource<int>();
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
{
BridgeConnector.Socket.On("appImportCertificateCompleted", (result) =>
BridgeConnector.Socket.On<int>("appImportCertificateCompleted", (result) =>
{
BridgeConnector.Socket.Off("appImportCertificateCompleted");
taskCompletionSource.SetResult((int)result);
taskCompletionSource.SetResult(result);
});

BridgeConnector.Socket.Emit("appImportCertificate", JObject.FromObject(options, _jsonSerializer));
BridgeConnector.Socket.Emit("appImportCertificate", options);

return await taskCompletionSource.Task
.ConfigureAwait(false);
Expand Down Expand Up @@ -1127,10 +1125,10 @@ public async Task<bool> SetBadgeCountAsync(int count, CancellationToken cancella
var taskCompletionSource = new TaskCompletionSource<bool>();
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
{
BridgeConnector.Socket.On("appSetBadgeCountCompleted", (success) =>
BridgeConnector.Socket.On<bool>("appSetBadgeCountCompleted", (success) =>
{
BridgeConnector.Socket.Off("appSetBadgeCountCompleted");
taskCompletionSource.SetResult((bool)success);
taskCompletionSource.SetResult(success);
});

BridgeConnector.Socket.Emit("appSetBadgeCount", count);
Expand Down Expand Up @@ -1187,12 +1185,9 @@ public async Task<LoginItemSettings> GetLoginItemSettingsAsync(LoginItemSettings
var taskCompletionSource = new TaskCompletionSource<LoginItemSettings>();
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
{
BridgeConnector.Socket.On("appGetLoginItemSettingsCompleted", (loginItemSettings) =>
BridgeConnector.Socket.On<LoginItemSettings>("appGetLoginItemSettingsCompleted", (result) =>
{
BridgeConnector.Socket.Off("appGetLoginItemSettingsCompleted");

var result = ((JObject)loginItemSettings).ToObject<LoginItemSettings>();

taskCompletionSource.SetResult(result);
});

Expand All @@ -1202,7 +1197,7 @@ public async Task<LoginItemSettings> GetLoginItemSettingsAsync(LoginItemSettings
}
else
{
BridgeConnector.Socket.Emit("appGetLoginItemSettings", JObject.FromObject(options, _jsonSerializer));
BridgeConnector.Socket.Emit("appGetLoginItemSettings", options);
}

return await taskCompletionSource.Task
Expand All @@ -1218,7 +1213,7 @@ public async Task<LoginItemSettings> GetLoginItemSettingsAsync(LoginItemSettings
/// <param name="loginSettings"></param>
public void SetLoginItemSettings(LoginSettings loginSettings)
{
this.CallMethod1(JObject.FromObject(loginSettings, _jsonSerializer));
this.CallMethod1(loginSettings);
}

/// <summary>
Expand Down Expand Up @@ -1270,7 +1265,7 @@ public void ShowAboutPanel()
/// <param name="options">About panel options.</param>
public void SetAboutPanelOptions(AboutPanelOptions options)
{
this.CallMethod1(JObject.FromObject(options, _jsonSerializer));
this.CallMethod1(options);
}

/// <summary>
Expand Down Expand Up @@ -1306,14 +1301,14 @@ public Task<string> UserAgentFallbackAsync
{
get
{
return Task.Run<string>(() =>
return Task.Run(() =>
{
var taskCompletionSource = new TaskCompletionSource<string>();

BridgeConnector.Socket.On("appGetUserAgentFallbackCompleted", (result) =>
BridgeConnector.Socket.On<string>("appGetUserAgentFallbackCompleted", (result) =>
{
BridgeConnector.Socket.Off("appGetUserAgentFallbackCompleted");
taskCompletionSource.SetResult((string)result);
taskCompletionSource.SetResult(result);
});

BridgeConnector.Socket.Emit("appGetUserAgentFallback");
Expand Down Expand Up @@ -1364,4 +1359,4 @@ public void Once(string eventName, Action action)
public async Task Once(string eventName, Action<object> action)
=> await Events.Instance.Once(ModuleName, eventName, action).ConfigureAwait(false);
}
}
}
Loading