Skip to content

Commit

Permalink
feat: add event.processId to be usable with webFrameMain.fromId()
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak committed Dec 1, 2020
1 parent 6c5f7a6 commit 2ef55ad
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/api/structures/ipc-main-event.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 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
* `ports` MessagePortMain[] - A list of MessagePorts that were transferred with this message
Expand Down
1 change: 1 addition & 0 deletions docs/api/structures/ipc-main-invoke-event.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# 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
11 changes: 10 additions & 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 All @@ -51,6 +51,15 @@ async function main () {
main()
```

You can access the frame, which sent an IPC message using `webFrameMain.fromId(event.processId, event.frameId)`.
```javascript
const { ipcMain, webFrameMain } = require('electron')

ipcMain.on('ping', (event) => {
console.log(`Message from: ${webFrameMain.fromId(event.processId, event.frameId).url}`)
})
```

## Methods

These methods can be accessed from the `webFrameMain` module:
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
5 changes: 4 additions & 1 deletion spec-main/api-subframe-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as path from 'path';
import * as http from 'http';
import { emittedNTimes, emittedOnce } from './events-helpers';
import { closeWindow } from './window-helpers';
import { app, BrowserWindow, ipcMain } from 'electron/main';
import { app, BrowserWindow, ipcMain, webFrameMain } from 'electron/main';
import { AddressInfo } from 'net';
import { ifdescribe } from './spec-helpers';

Expand Down Expand Up @@ -35,6 +35,7 @@ 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(webFrameMain.fromId(event2[0].processId, event2[0].frameId).name).to.equal('frame');
});

it('should load preload scripts in nested iframes', async () => {
Expand All @@ -47,6 +48,8 @@ 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(webFrameMain.fromId(event2[0].processId, event2[0].frameId).name).to.equal('frame-with-frame');
expect(webFrameMain.fromId(event3[0].processId, event3[0].frameId).name).to.equal('frame');
});

it('should correctly reply to the main frame with using event.reply', async () => {
Expand Down
2 changes: 1 addition & 1 deletion spec-main/fixtures/sub-frames/frame-container.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
</head>
<body>
This is the root page
<iframe src="./frame.html"></iframe>
<iframe src="./frame.html" name="frame"></iframe>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
</head>
<body>
This is the root page
<iframe src="./frame-with-frame.html"></iframe>
<iframe src="./frame-with-frame.html" name="frame-with-frame"></iframe>
</body>
</html>

0 comments on commit 2ef55ad

Please sign in to comment.