Skip to content

Commit

Permalink
feat: add event.senderFrame property returning the originating webFra…
Browse files Browse the repository at this point in the history
…meMain
  • Loading branch information
miniak committed Dec 1, 2020
1 parent 6c5f7a6 commit c0f074b
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/api/structures/ipc-main-event.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# IpcMainEvent Object extends `Event`

* `frameId` Integer - The ID of the renderer frame that sent this message
* `processId` Integer - The ID of the process which owns the frame
* `returnValue` any - Set this to the value to be returned in a synchronous message
* `sender` WebContents - Returns the `webContents` that sent the message
* `senderFrame` WebFrameMain _Readonly_ - The frame that sent this message
* `ports` MessagePortMain[] - A list of MessagePorts that were transferred with this message
* `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.
* `channel` String
Expand Down
2 changes: 2 additions & 0 deletions docs/api/structures/ipc-main-invoke-event.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# IpcMainInvokeEvent Object extends `Event`

* `frameId` Integer - The ID of the renderer frame that sent this message
* `processId` Integer - The ID of the process which owns the frame
* `sender` WebContents - Returns the `webContents` that sent the message
* `senderFrame` WebFrameMain _Readonly_ - The frame that sent this message
2 changes: 1 addition & 1 deletion docs/api/web-frame-main.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ win.webContents.on(
)
```

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

```javascript
Expand Down
13 changes: 11 additions & 2 deletions lib/browser/api/web-contents.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { app, ipcMain, session, deprecate, BrowserWindowConstructorOptions } from 'electron/main';
import type { MenuItem, MenuItemConstructorOptions, LoadURLOptions } from 'electron/main';
import { app, ipcMain, session, deprecate, webFrameMain } from 'electron/main';
import type { BrowserWindowConstructorOptions, MenuItem, MenuItemConstructorOptions, LoadURLOptions } from 'electron/main';

import * as url from 'url';
import * as path from 'path';
Expand Down Expand Up @@ -493,6 +493,12 @@ const addReplyInternalToEvent = (event: any) => {
});
};

const addSenderFrameToEvent = (event: any) => {
Object.defineProperty(event, 'senderFrame', {
get: () => webFrameMain.fromId(event.processId, event.frameId)
});
}

const addReturnValueToEvent = (event: any) => {
Object.defineProperty(event, 'returnValue', {
set: (value) => event.sendReply(value),
Expand Down Expand Up @@ -536,6 +542,7 @@ WebContents.prototype._init = function () {

// Dispatch IPC messages to the ipc module.
this.on('-ipc-message' as any, function (this: Electron.WebContents, event: any, internal: boolean, channel: string, args: any[]) {
addSenderFrameToEvent(event);
if (internal) {
addReplyInternalToEvent(event);
ipcMainInternal.emit(channel, event, ...args);
Expand All @@ -547,6 +554,7 @@ WebContents.prototype._init = function () {
});

this.on('-ipc-invoke' as any, function (event: any, internal: boolean, channel: string, args: any[]) {
addSenderFrameToEvent(event);
event._reply = (result: any) => event.sendReply({ result });
event._throw = (error: Error) => {
console.error(`Error occurred in handler for '${channel}':`, error);
Expand All @@ -561,6 +569,7 @@ WebContents.prototype._init = function () {
});

this.on('-ipc-message-sync' as any, function (this: Electron.WebContents, event: any, internal: boolean, channel: string, args: any[]) {
addSenderFrameToEvent(event);
addReturnValueToEvent(event);
if (internal) {
addReplyInternalToEvent(event);
Expand Down
5 changes: 4 additions & 1 deletion shell/common/gin_helper/event_emitter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "shell/common/gin_helper/event_emitter.h"

#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "shell/browser/api/event.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/object_template_builder.h"
Expand Down Expand Up @@ -67,8 +68,10 @@ v8::Local<v8::Object> CreateNativeEvent(
Dictionary dict(isolate, event);
dict.Set("sender", sender);
// Should always set frameId even when callback is null.
if (frame)
if (frame) {
dict.Set("processId", frame->GetProcess()->GetID());
dict.Set("frameId", frame->GetRoutingID());
}
return event;
}

Expand Down
8 changes: 8 additions & 0 deletions spec-main/api-subframe-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ describe('renderer nodeIntegrationInSubFrames', () => {
expect(event1[0].frameId).to.not.equal(event2[0].frameId);
expect(event1[0].frameId).to.equal(event1[2]);
expect(event2[0].frameId).to.equal(event2[2]);
expect(event1[0].senderFrame.routingId).to.equal(event1[2]);
expect(event2[0].senderFrame.routingId).to.equal(event2[2]);
});

it('should load preload scripts in nested iframes', async () => {
Expand All @@ -47,6 +49,9 @@ describe('renderer nodeIntegrationInSubFrames', () => {
expect(event1[0].frameId).to.equal(event1[2]);
expect(event2[0].frameId).to.equal(event2[2]);
expect(event3[0].frameId).to.equal(event3[2]);
expect(event1[0].senderFrame.routingId).to.equal(event1[2]);
expect(event2[0].senderFrame.routingId).to.equal(event2[2]);
expect(event3[0].senderFrame.routingId).to.equal(event3[2]);
});

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

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

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

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

0 comments on commit c0f074b

Please sign in to comment.