Skip to content

Commit f0280f4

Browse files
Merge pull request #560 from nfichter/ipc-to-browser-view
Allow IpcMain to send IPC messages to BrowserViews
2 parents 9f78098 + 2d93d95 commit f0280f4

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

ElectronNET.API/IpcMain.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,45 @@ public void Send(BrowserWindow browserWindow, string channel, params object[] da
179179
}
180180
}
181181

182+
/// <summary>
183+
/// Send a message to the BrowserView renderer process asynchronously via channel, you can also send
184+
/// arbitrary arguments. Arguments will be serialized in JSON internally and hence
185+
/// no functions or prototype chain will be included. The renderer process handles it by
186+
/// listening for channel with ipcRenderer module.
187+
/// </summary>
188+
/// <param name="browserView">BrowserView with channel.</param>
189+
/// <param name="channel">Channelname.</param>
190+
/// <param name="data">Arguments data.</param>
191+
public void Send(BrowserView browserView, string channel, params object[] data)
192+
{
193+
List<JObject> jobjects = new List<JObject>();
194+
List<JArray> jarrays = new List<JArray>();
195+
List<object> objects = new List<object>();
196+
197+
foreach (var parameterObject in data)
198+
{
199+
if(parameterObject.GetType().IsArray || parameterObject.GetType().IsGenericType && parameterObject is IEnumerable)
200+
{
201+
jarrays.Add(JArray.FromObject(parameterObject, _jsonSerializer));
202+
} else if(parameterObject.GetType().IsClass && !parameterObject.GetType().IsPrimitive && !(parameterObject is string))
203+
{
204+
jobjects.Add(JObject.FromObject(parameterObject, _jsonSerializer));
205+
} else if(parameterObject.GetType().IsPrimitive || (parameterObject is string))
206+
{
207+
objects.Add(parameterObject);
208+
}
209+
}
210+
211+
if(jobjects.Count > 0 || jarrays.Count > 0)
212+
{
213+
BridgeConnector.Socket.Emit("sendToIpcRendererBrowserView", browserView.Id, channel, jarrays.ToArray(), jobjects.ToArray(), objects.ToArray());
214+
}
215+
else
216+
{
217+
BridgeConnector.Socket.Emit("sendToIpcRendererBrowserView", browserView.Id, channel, data);
218+
}
219+
}
220+
182221
private JsonSerializer _jsonSerializer = new JsonSerializer()
183222
{
184223
ContractResolver = new CamelCasePropertyNamesContractResolver(),

ElectronNET.Host/api/ipc.js

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

ElectronNET.Host/api/ipc.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.

ElectronNET.Host/api/ipc.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ipcMain, BrowserWindow } from 'electron';
1+
import { ipcMain, BrowserWindow, BrowserView } from 'electron';
22
let electronSocket;
33

44
export = (socket: SocketIO.Socket) => {
@@ -38,4 +38,19 @@ export = (socket: SocketIO.Socket) => {
3838
window.webContents.send(channel, ...data);
3939
}
4040
});
41+
42+
socket.on('sendToIpcRendererBrowserView', (id, channel, ...data) => {
43+
const browserViews: BrowserView[] = (global['browserViews'] = global['browserViews'] || []) as BrowserView[];
44+
let view: BrowserView = null;
45+
for (let i = 0; i < browserViews.length; i++) {
46+
if (browserViews[i]['id'] === id) {
47+
view = browserViews[i];
48+
break;
49+
}
50+
}
51+
52+
if (view) {
53+
view.webContents.send(channel, ...data);
54+
}
55+
});
4156
};

0 commit comments

Comments
 (0)