Files
Electron.NET/ElectronNET.API/IpcMain.cs

269 lines
10 KiB
C#
Raw Normal View History

2017-10-15 21:39:52 +02:00
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
2017-10-07 01:32:19 +02:00
namespace ElectronNET.API
{
/// <summary>
/// Communicate asynchronously from the main process to renderer processes.
/// </summary>
2017-10-14 17:58:16 +02:00
public sealed class IpcMain
2017-10-07 01:32:19 +02:00
{
2017-10-14 17:58:16 +02:00
private static IpcMain _ipcMain;
2021-09-15 11:14:45 +02:00
private static readonly object _syncRoot = new();
2017-10-07 01:32:19 +02:00
internal IpcMain() { }
2017-10-14 17:58:16 +02:00
internal static IpcMain Instance
2017-10-07 01:32:19 +02:00
{
2017-10-14 17:58:16 +02:00
get
{
if(_ipcMain == null)
{
2017-11-04 00:16:14 +01:00
lock (_syncRoot)
{
if(_ipcMain == null)
{
_ipcMain = new IpcMain();
}
}
2017-10-14 17:58:16 +02:00
}
return _ipcMain;
}
2017-10-07 01:32:19 +02:00
}
2017-10-14 17:58:16 +02:00
2021-09-02 18:29:02 +02:00
public static bool IsConnected => BridgeConnector.IsConnected;
2017-10-07 01:32:19 +02:00
/// <summary>
/// Listens to channel, when a new message arrives listener would be called with
/// listener(event, args...).
/// </summary>
/// <param name="channel">Channelname.</param>
/// <param name="listener">Callback Method.</param>
public void On(string channel, Action<object> listener)
{
2021-07-12 19:50:39 +02:00
BridgeConnector.Emit("registerIpcMainChannel", channel);
BridgeConnector.Off(channel);
2021-09-01 20:42:49 +02:00
BridgeConnector.On<object[]>(channel, (args) =>
{
2021-07-12 19:50:39 +02:00
var objectArray = FormatArguments(args);
2021-09-01 20:42:49 +02:00
if (objectArray.Count == 1)
{
listener(objectArray.First());
}
else
{
listener(objectArray);
}
});
}
2021-09-01 20:42:49 +02:00
/// <summary>
/// Listens to channel, when a new message arrives listener would be called with
/// listener(event, args...). This listner will keep the window event sender id
/// </summary>
/// <param name="channel">Channelname.</param>
/// <param name="listener">Callback Method.</param>
public void OnWithId(string channel, Action<(int browserId, int webContentId, object arguments)> listener)
2021-09-01 20:42:49 +02:00
{
BridgeConnector.Emit("registerIpcMainChannelWithId", channel);
BridgeConnector.Off(channel);
BridgeConnector.On<ArgsAndIds>(channel, (data) =>
2021-09-01 20:42:49 +02:00
{
var objectArray = FormatArguments(data.args);
if (objectArray.Count == 1)
{
listener((data.id, data.wcId, objectArray.First()));
2021-09-01 20:42:49 +02:00
}
else
{
listener((data.id, data.wcId, objectArray));
2021-09-01 20:42:49 +02:00
}
});
}
private class ArgsAndIds
2021-09-01 20:42:49 +02:00
{
public int id { get; set; }
public int wcId { get; set; }
2021-09-01 20:42:49 +02:00
public object[] args { get; set; }
}
2021-07-12 19:50:39 +02:00
private List<object> FormatArguments(object[] objectArray)
{
2021-07-12 19:50:39 +02:00
return objectArray.Where(o => o is object).ToList();
2017-10-07 01:32:19 +02:00
}
/// <summary>
/// Send a message to the renderer process synchronously via channel,
/// you can also send arbitrary arguments.
///
/// Note: Sending a synchronous message will block the whole renderer process,
/// unless you know what you are doing you should never use it.
/// </summary>
/// <param name="channel"></param>
/// <param name="listener"></param>
public void OnSync(string channel, Func<object, object> listener)
{
2021-07-12 19:50:39 +02:00
BridgeConnector.Emit("registerSyncIpcMainChannel", channel);
BridgeConnector.On<object[]>(channel, (args) => {
var objectArray = FormatArguments(args);
object parameter;
if (objectArray.Count == 1)
{
parameter = objectArray.First();
}
else
{
parameter = objectArray;
}
var result = listener(parameter);
2021-07-12 19:50:39 +02:00
BridgeConnector.Emit(channel + "Sync", result);
});
}
/// <summary>
/// Adds a one time listener method for the event. This listener is invoked only
/// the next time a message is sent to channel, after which it is removed.
/// </summary>
/// <param name="channel">Channelname.</param>
/// <param name="listener">Callback Method.</param>
2017-10-07 01:32:19 +02:00
public void Once(string channel, Action<object> listener)
{
2021-07-12 19:50:39 +02:00
BridgeConnector.Emit("registerOnceIpcMainChannel", channel);
BridgeConnector.Once<object[]>(channel, (args) =>
{
2021-07-12 19:50:39 +02:00
var objectArray = FormatArguments(args);
if (objectArray.Count == 1)
{
listener(objectArray.First());
}
else
{
listener(objectArray);
}
});
2017-10-07 01:32:19 +02:00
}
/// <summary>
/// Removes listeners of the specified channel.
/// </summary>
/// <param name="channel">Channelname.</param>
2017-10-07 01:32:19 +02:00
public void RemoveAllListeners(string channel)
{
2021-07-12 19:50:39 +02:00
BridgeConnector.Emit("removeAllListenersIpcMainChannel", channel);
2017-10-07 01:32:19 +02:00
}
/// <summary>
/// Send a message to the renderer process asynchronously via channel, you can also send
/// arbitrary arguments. Arguments will be serialized in JSON internally and hence
/// no functions or prototype chain will be included. The renderer process handles it by
/// listening for channel with ipcRenderer module.
/// </summary>
2017-10-15 21:39:52 +02:00
/// <param name="browserWindow">BrowserWindow with channel.</param>
/// <param name="channel">Channelname.</param>
/// <param name="data">Arguments data.</param>
2017-10-15 21:39:52 +02:00
public void Send(BrowserWindow browserWindow, string channel, params object[] data)
2017-10-07 01:32:19 +02:00
{
2021-09-15 11:14:45 +02:00
var objectsWithCorrectSerialization = new List<object>
{
browserWindow.Id,
channel
};
foreach (var parameterObject in data)
{
if(parameterObject.GetType().IsArray || parameterObject.GetType().IsGenericType && parameterObject is IEnumerable)
{
2021-09-02 16:45:03 +02:00
objectsWithCorrectSerialization.Add(JArray.FromObject(parameterObject, _jsonSerializer));
}
else if(parameterObject.GetType().IsClass && !parameterObject.GetType().IsPrimitive && !(parameterObject is string))
{
2021-09-02 16:45:03 +02:00
objectsWithCorrectSerialization.Add(JObject.FromObject(parameterObject, _jsonSerializer));
}
else if(parameterObject.GetType().IsPrimitive || (parameterObject is string))
{
2021-09-02 16:45:03 +02:00
objectsWithCorrectSerialization.Add(parameterObject);
}
}
2021-09-02 16:45:03 +02:00
BridgeConnector.Emit("sendToIpcRenderer", objectsWithCorrectSerialization.ToArray());
2017-10-07 01:32:19 +02:00
}
2017-10-15 21:39:52 +02:00
/// <summary>
/// Send a message to the BrowserView renderer process asynchronously via channel, you can also send
/// arbitrary arguments. Arguments will be serialized in JSON internally and hence
/// no functions or prototype chain will be included. The renderer process handles it by
/// listening for channel with ipcRenderer module.
/// </summary>
/// <param name="browserView">BrowserView with channel.</param>
/// <param name="channel">Channelname.</param>
/// <param name="data">Arguments data.</param>
public void Send(BrowserView browserView, string channel, params object[] data)
{
2021-09-15 11:14:45 +02:00
List<JObject> jobjects = new();
List<JArray> jarrays = new();
List<object> objects = new();
foreach (var parameterObject in data)
{
if(parameterObject.GetType().IsArray || parameterObject.GetType().IsGenericType && parameterObject is IEnumerable)
{
jarrays.Add(JArray.FromObject(parameterObject, _jsonSerializer));
} else if(parameterObject.GetType().IsClass && !parameterObject.GetType().IsPrimitive && !(parameterObject is string))
{
jobjects.Add(JObject.FromObject(parameterObject, _jsonSerializer));
} else if(parameterObject.GetType().IsPrimitive || (parameterObject is string))
{
objects.Add(parameterObject);
}
}
if(jobjects.Count > 0 || jarrays.Count > 0)
{
2021-07-12 19:50:39 +02:00
BridgeConnector.Emit("sendToIpcRendererBrowserView", browserView.Id, channel, jarrays.ToArray(), jobjects.ToArray(), objects.ToArray());
}
else
{
2021-07-12 19:50:39 +02:00
BridgeConnector.Emit("sendToIpcRendererBrowserView", browserView.Id, channel, data);
}
}
/// <summary>
/// Log a message to the console output pipe. This is used when running with "detachedProcess" : true on the electron.manifest.json,
/// as in that case we can't open pipes to read the console output from the child process anymore
/// </summary>
/// <param name="text">Message to log</param>
public static void ConsoleLog(string text)
{
BridgeConnector.Emit("console-stdout", text);
}
/// <summary>
/// Log a message to the console error pipe. This is used when running with "detachedProcess" : true on the electron.manifest.json,
/// as in that case we can't open pipes to read the console output from the child process anymore
/// </summary>
/// <param name="text">Message to log</param>
public static void ConsoleError(string text)
{
BridgeConnector.Emit("console-stderr", text);
}
2021-09-15 11:14:45 +02:00
private readonly JsonSerializer _jsonSerializer = new()
2017-10-15 21:39:52 +02:00
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore
};
2017-10-07 01:32:19 +02:00
}
}