Skip to content

Commit cec6378

Browse files
authored
feat: add event.senderFrame property returning the originating webFrameMain (electron#26764)
1 parent 9908cc3 commit cec6378

File tree

7 files changed

+25
-14
lines changed

7 files changed

+25
-14
lines changed

docs/api/structures/ipc-main-event.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* `frameId` Integer - The ID of the renderer frame that sent this message
55
* `returnValue` any - Set this to the value to be returned in a synchronous message
66
* `sender` WebContents - Returns the `webContents` that sent the message
7+
* `senderFrame` WebFrameMain _Readonly_ - The frame that sent this message
78
* `ports` MessagePortMain[] - A list of MessagePorts that were transferred with this message
89
* `reply` Function - A function that will send an IPC message to the renderer frame that sent the original message that you are currently handling. You should use this method to "reply" to the sent message in order to guarantee the reply will go to the correct process and frame.
910
* `channel` String

docs/api/structures/ipc-main-invoke-event.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
* `processId` Integer - The internal ID of the renderer process that sent this message
44
* `frameId` Integer - The ID of the renderer frame that sent this message
55
* `sender` WebContents - Returns the `webContents` that sent the message
6+
* `senderFrame` WebFrameMain _Readonly_ - The frame that sent this message

docs/api/web-frame-main.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ win.webContents.on(
2626
)
2727
```
2828

29-
You can also access frames of existing pages by using the `webFrame` property
29+
You can also access frames of existing pages by using the `mainFrame` property
3030
of [`WebContents`](web-contents.md).
3131

3232
```javascript
@@ -57,8 +57,8 @@ These methods can be accessed from the `webFrameMain` module:
5757

5858
### `webFrameMain.fromId(processId, routingId)`
5959

60-
* `processId` Integer - An `Integer` representing the id of the process which owns the frame.
61-
* `routingId` Integer - An `Integer` representing the unique frame id in the
60+
* `processId` Integer - An `Integer` representing the internal ID of the process which owns the frame.
61+
* `routingId` Integer - An `Integer` representing the unique frame ID in the
6262
current renderer process. Routing IDs can be retrieved from `WebFrameMain`
6363
instances (`frame.routingId`) and are also passed by frame
6464
specific `WebContents` navigation events (e.g. `did-frame-navigate`).

lib/browser/api/web-contents.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { app, ipcMain, session, deprecate, BrowserWindowConstructorOptions } from 'electron/main';
2-
import type { MenuItem, MenuItemConstructorOptions, LoadURLOptions } from 'electron/main';
1+
import { app, ipcMain, session, deprecate, webFrameMain } from 'electron/main';
2+
import type { BrowserWindowConstructorOptions, MenuItem, MenuItemConstructorOptions, LoadURLOptions } from 'electron/main';
33

44
import * as url from 'url';
55
import * as path from 'path';
@@ -462,6 +462,13 @@ const addReplyToEvent = (event: any) => {
462462
};
463463
};
464464

465+
const addSenderFrameToEvent = (event: any) => {
466+
const { processId, frameId } = event;
467+
Object.defineProperty(event, 'senderFrame', {
468+
get: () => webFrameMain.fromId(processId, frameId)
469+
});
470+
};
471+
465472
const addReturnValueToEvent = (event: any) => {
466473
Object.defineProperty(event, 'returnValue', {
467474
set: (value) => event.sendReply(value),
@@ -505,6 +512,7 @@ WebContents.prototype._init = function () {
505512

506513
// Dispatch IPC messages to the ipc module.
507514
this.on('-ipc-message' as any, function (this: Electron.WebContents, event: any, internal: boolean, channel: string, args: any[]) {
515+
addSenderFrameToEvent(event);
508516
if (internal) {
509517
ipcMainInternal.emit(channel, event, ...args);
510518
} else {
@@ -515,6 +523,7 @@ WebContents.prototype._init = function () {
515523
});
516524

517525
this.on('-ipc-invoke' as any, function (event: any, internal: boolean, channel: string, args: any[]) {
526+
addSenderFrameToEvent(event);
518527
event._reply = (result: any) => event.sendReply({ result });
519528
event._throw = (error: Error) => {
520529
console.error(`Error occurred in handler for '${channel}':`, error);
@@ -529,6 +538,7 @@ WebContents.prototype._init = function () {
529538
});
530539

531540
this.on('-ipc-message-sync' as any, function (this: Electron.WebContents, event: any, internal: boolean, channel: string, args: any[]) {
541+
addSenderFrameToEvent(event);
532542
addReturnValueToEvent(event);
533543
if (internal) {
534544
ipcMainInternal.emit(channel, event, ...args);

shell/common/gin_converters/frame_converter.cc

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,8 @@
44

55
#include "shell/common/gin_converters/frame_converter.h"
66

7-
#include <string>
8-
#include <vector>
9-
107
#include "content/public/browser/render_frame_host.h"
118
#include "shell/browser/api/electron_api_web_frame_main.h"
12-
#include "shell/common/gin_converters/blink_converter.h"
13-
#include "shell/common/gin_converters/callback_converter.h"
14-
#include "shell/common/gin_converters/gurl_converter.h"
15-
#include "shell/common/gin_helper/dictionary.h"
169

1710
namespace gin {
1811

shell/common/gin_converters/frame_converter.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#ifndef SHELL_COMMON_GIN_CONVERTERS_FRAME_CONVERTER_H_
66
#define SHELL_COMMON_GIN_CONVERTERS_FRAME_CONVERTER_H_
77

8-
#include <utility>
9-
108
#include "gin/converter.h"
119

1210
namespace content {

spec-main/api-subframe-spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ describe('renderer nodeIntegrationInSubFrames', () => {
3535
expect(event1[0].frameId).to.not.equal(event2[0].frameId);
3636
expect(event1[0].frameId).to.equal(event1[2]);
3737
expect(event2[0].frameId).to.equal(event2[2]);
38+
expect(event1[0].senderFrame.routingId).to.equal(event1[2]);
39+
expect(event2[0].senderFrame.routingId).to.equal(event2[2]);
3840
});
3941

4042
it('should load preload scripts in nested iframes', async () => {
@@ -47,6 +49,9 @@ describe('renderer nodeIntegrationInSubFrames', () => {
4749
expect(event1[0].frameId).to.equal(event1[2]);
4850
expect(event2[0].frameId).to.equal(event2[2]);
4951
expect(event3[0].frameId).to.equal(event3[2]);
52+
expect(event1[0].senderFrame.routingId).to.equal(event1[2]);
53+
expect(event2[0].senderFrame.routingId).to.equal(event2[2]);
54+
expect(event3[0].senderFrame.routingId).to.equal(event3[2]);
5055
});
5156

5257
it('should correctly reply to the main frame with using event.reply', async () => {
@@ -57,6 +62,7 @@ describe('renderer nodeIntegrationInSubFrames', () => {
5762
event1[0].reply('preload-ping');
5863
const details = await pongPromise;
5964
expect(details[1]).to.equal(event1[0].frameId);
65+
expect(details[1]).to.equal(event1[0].senderFrame.routingId);
6066
});
6167

6268
it('should correctly reply to the sub-frames with using event.reply', async () => {
@@ -67,6 +73,7 @@ describe('renderer nodeIntegrationInSubFrames', () => {
6773
event2[0].reply('preload-ping');
6874
const details = await pongPromise;
6975
expect(details[1]).to.equal(event2[0].frameId);
76+
expect(details[1]).to.equal(event2[0].senderFrame.routingId);
7077
});
7178

7279
it('should correctly reply to the nested sub-frames with using event.reply', async () => {
@@ -77,6 +84,7 @@ describe('renderer nodeIntegrationInSubFrames', () => {
7784
event3[0].reply('preload-ping');
7885
const details = await pongPromise;
7986
expect(details[1]).to.equal(event3[0].frameId);
87+
expect(details[1]).to.equal(event3[0].senderFrame.routingId);
8088
});
8189

8290
it('should not expose globals in main world', async () => {

0 commit comments

Comments
 (0)