Skip to content

Commit

Permalink
feat: add senderIsMainFrame to messages sent via `ipcRenderer.sendT…
Browse files Browse the repository at this point in the history
…o()` (#39207)

* feat: add `senderIsMainFrame` to messages sent via `ipcRenderer.sendTo()` (#38868)

* feat: add isMainFrame to events emitted via ipcRenderer.sendTo()

* chore: rename isMainFrame to senderIsMainFrame

Co-authored-by: Milan Burda <milan.burda@gmail.com>

* once -> emittedOnce

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Milan Burda <milan.burda@gmail.com>
  • Loading branch information
trop[bot] and miniak committed Jul 27, 2023
1 parent ae9593d commit e2d17db
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 24 deletions.
1 change: 1 addition & 0 deletions docs/api/structures/ipc-renderer-event.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* `sender` [IpcRenderer](../ipc-renderer.md) - The `IpcRenderer` instance that emitted the event originally
* `senderId` Integer - The `webContents.id` that sent the message, you can call `event.sender.sendTo(event.senderId, ...)` to reply to the message, see [ipcRenderer.sendTo][ipc-renderer-sendto] for more information. This only applies to messages sent from a different renderer. Messages sent directly from the main process set `event.senderId` to `0`.
* `senderIsMainFrame` boolean (optional) - Whether the message sent via [ipcRenderer.sendTo][ipc-renderer-sendto] was sent by the main frame. This is relevant when `nodeIntegrationInSubFrames` is enabled in the originating `webContents`.
* `ports` [MessagePort][][] - A list of MessagePorts that were transferred with this message

[ipc-renderer-sendto]: ../ipc-renderer.md#ipcrenderersendtowebcontentsid-channel-args
Expand Down
4 changes: 2 additions & 2 deletions lib/renderer/common-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ const isWebView = mainFrame.getWebPreference('isWebView');
// ElectronApiServiceImpl will look for the "ipcNative" hidden object when
// invoking the 'onMessage' callback.
v8Util.setHiddenValue(global, 'ipcNative', {
onMessage (internal: boolean, channel: string, ports: MessagePort[], args: any[], senderId: number) {
onMessage (internal: boolean, channel: string, ports: MessagePort[], args: any[], senderId: number, senderIsMainFrame: boolean) {
if (internal && senderId !== 0) {
console.error(`Message ${channel} sent by unexpected WebContents (${senderId})`);
return;
}
const sender = internal ? ipcRendererInternal : ipcRenderer;
sender.emit(channel, { sender, senderId, ports }, ...args);
sender.emit(channel, { sender, senderId, ...(senderId ? { senderIsMainFrame } : {}), ports }, ...args);
}
});

Expand Down
7 changes: 5 additions & 2 deletions shell/browser/api/electron_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1949,7 +1949,8 @@ void WebContents::MessageSync(

void WebContents::MessageTo(int32_t web_contents_id,
const std::string& channel,
blink::CloneableMessage arguments) {
blink::CloneableMessage arguments,
content::RenderFrameHost* render_frame_host) {
TRACE_EVENT1("electron", "WebContents::MessageTo", "channel", channel);
auto* target_web_contents = FromID(web_contents_id);

Expand All @@ -1965,8 +1966,10 @@ void WebContents::MessageTo(int32_t web_contents_id,
return;

int32_t sender_id = ID();
bool sender_is_main_frame = render_frame_host->GetParent() == nullptr;
web_frame_main->GetRendererApi()->Message(false /* internal */, channel,
std::move(arguments), sender_id);
std::move(arguments), sender_id,
sender_is_main_frame);
}
}

Expand Down
3 changes: 2 additions & 1 deletion shell/browser/api/electron_api_web_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,8 @@ class WebContents : public ExclusiveAccessContext,
content::RenderFrameHost* render_frame_host);
void MessageTo(int32_t web_contents_id,
const std::string& channel,
blink::CloneableMessage arguments);
blink::CloneableMessage arguments,
content::RenderFrameHost* render_frame_host);
void MessageHost(const std::string& channel,
blink::CloneableMessage arguments,
content::RenderFrameHost* render_frame_host);
Expand Down
3 changes: 2 additions & 1 deletion shell/browser/api/electron_api_web_frame_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ void WebFrameMain::Send(v8::Isolate* isolate,
return;

GetRendererApi()->Message(internal, channel, std::move(message),
0 /* sender_id */);
0 /* sender_id */,
false /* sender_is_main_frame */);
}

const mojo::Remote<mojom::ElectronRenderer>& WebFrameMain::GetRendererApi() {
Expand Down
3 changes: 2 additions & 1 deletion shell/browser/electron_api_ipc_handler_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ void ElectronApiIPCHandlerImpl::MessageTo(int32_t web_contents_id,
blink::CloneableMessage arguments) {
api::WebContents* api_web_contents = api::WebContents::From(web_contents());
if (api_web_contents) {
api_web_contents->MessageTo(web_contents_id, channel, std::move(arguments));
api_web_contents->MessageTo(web_contents_id, channel, std::move(arguments),
GetRenderFrameHost());
}
}

Expand Down
3 changes: 2 additions & 1 deletion shell/common/api/api.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ interface ElectronRenderer {
bool internal,
string channel,
blink.mojom.CloneableMessage arguments,
int32 sender_id);
int32 sender_id,
bool sender_is_main_frame);

ReceivePostMessage(string channel, blink.mojom.TransferableMessage message);

Expand Down
21 changes: 13 additions & 8 deletions shell/renderer/electron_api_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ void EmitIPCEvent(v8::Local<v8::Context> context,
const std::string& channel,
std::vector<v8::Local<v8::Value>> ports,
v8::Local<v8::Value> args,
int32_t sender_id) {
int32_t sender_id = 0,
bool sender_is_main_frame = false) {
auto* isolate = context->GetIsolate();

v8::HandleScope handle_scope(isolate);
Expand All @@ -91,9 +92,12 @@ void EmitIPCEvent(v8::Local<v8::Context> context,
v8::MicrotasksScope::kRunMicrotasks);

std::vector<v8::Local<v8::Value>> argv = {
gin::ConvertToV8(isolate, internal), gin::ConvertToV8(isolate, channel),
gin::ConvertToV8(isolate, ports), args,
gin::ConvertToV8(isolate, sender_id)};
gin::ConvertToV8(isolate, internal),
gin::ConvertToV8(isolate, channel),
gin::ConvertToV8(isolate, ports),
args,
gin::ConvertToV8(isolate, sender_id),
gin::ConvertToV8(isolate, sender_is_main_frame)};

InvokeIpcCallback(context, "onMessage", argv);
}
Expand Down Expand Up @@ -156,7 +160,8 @@ void ElectronApiServiceImpl::OnConnectionError() {
void ElectronApiServiceImpl::Message(bool internal,
const std::string& channel,
blink::CloneableMessage arguments,
int32_t sender_id) {
int32_t sender_id,
bool sender_is_main_frame) {
blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
if (!frame)
return;
Expand All @@ -169,7 +174,8 @@ void ElectronApiServiceImpl::Message(bool internal,

v8::Local<v8::Value> args = gin::ConvertToV8(isolate, arguments);

EmitIPCEvent(context, internal, channel, {}, args, sender_id);
EmitIPCEvent(context, internal, channel, {}, args, sender_id,
sender_is_main_frame);
}

void ElectronApiServiceImpl::ReceivePostMessage(
Expand All @@ -196,8 +202,7 @@ void ElectronApiServiceImpl::ReceivePostMessage(

std::vector<v8::Local<v8::Value>> args = {message_value};

EmitIPCEvent(context, false, channel, ports, gin::ConvertToV8(isolate, args),
0);
EmitIPCEvent(context, false, channel, ports, gin::ConvertToV8(isolate, args));
}

void ElectronApiServiceImpl::TakeHeapSnapshot(
Expand Down
3 changes: 2 additions & 1 deletion shell/renderer/electron_api_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class ElectronApiServiceImpl : public mojom::ElectronRenderer,
void Message(bool internal,
const std::string& channel,
blink::CloneableMessage arguments,
int32_t sender_id) override;
int32_t sender_id,
bool sender_is_main_frame) override;
void ReceivePostMessage(const std::string& channel,
blink::TransferableMessage message) override;
void TakeHeapSnapshot(mojo::ScopedHandle file,
Expand Down
40 changes: 38 additions & 2 deletions spec/api-ipc-renderer-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ describe('ipcRenderer module', () => {

let w: BrowserWindow;
before(async () => {
w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true,
nodeIntegrationInSubFrames: true,
contextIsolation: false
}
});
await w.loadURL('about:blank');
});
after(async () => {
Expand Down Expand Up @@ -154,7 +161,36 @@ describe('ipcRenderer module', () => {
ipcRenderer.sendTo(${contents.id}, 'ping', ${JSON.stringify(payload)})
ipcRenderer.once('pong', (event, data) => resolve(data))
})`);
expect(data).to.equal(payload);
expect(data.payload).to.equal(payload);
expect(data.senderIsMainFrame).to.be.true();
});

it('sends message to WebContents from a child frame', async () => {
const frameCreated = emittedOnce(w.webContents, 'frame-created') as Promise<[any, Electron.FrameCreatedDetails]>;

const promise = w.webContents.executeJavaScript(`new Promise(resolve => {
const iframe = document.createElement('iframe');
iframe.src = 'data:text/html,';
iframe.name = 'iframe';
document.body.appendChild(iframe);
const { ipcRenderer } = require('electron');
ipcRenderer.once('pong', (event, data) => resolve(data));
})`);

const [, details] = await frameCreated;
expect(details.frame.name).to.equal('iframe');

await emittedOnce(details.frame, 'dom-ready');

details.frame.executeJavaScript(`new Promise(resolve => {
const { ipcRenderer } = require('electron');
ipcRenderer.sendTo(${contents.id}, 'ping', ${JSON.stringify(payload)});
})`);

const data = await promise;
expect(data.payload).to.equal(payload);
expect(data.senderIsMainFrame).to.be.false();
});

it('sends message on channel with non-ASCII characters to WebContents', async () => {
Expand Down
6 changes: 3 additions & 3 deletions spec/chromium-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as path from 'path';
import * as fs from 'fs';
import * as url from 'url';
import * as ChildProcess from 'child_process';
import { EventEmitter, once } from 'events';
import { EventEmitter } from 'events';
import { promisify } from 'util';
import { ifit, ifdescribe, defer, delay, itremote } from './lib/spec-helpers';
import { AddressInfo } from 'net';
Expand Down Expand Up @@ -1159,8 +1159,8 @@ describe('chromium features', () => {
}));
w.loadURL('about:blank');
w.webContents.executeJavaScript('window.child = window.open(); child.opener = null');
const [, { webContents }] = await once(app, 'browser-window-created');
const [,, message] = await once(webContents, 'console-message');
const [, { webContents }] = await emittedOnce(app, 'browser-window-created');
const [,, message] = await emittedOnce(webContents, 'console-message');
expect(message).to.equal('{"require":"function","module":"undefined","process":"object","Buffer":"function"}');
});

Expand Down
4 changes: 2 additions & 2 deletions spec/fixtures/module/preload-ipc-ping-pong.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { ipcRenderer } = require('electron');

ipcRenderer.on('ping', function (event, payload) {
ipcRenderer.sendTo(event.senderId, 'pong', payload);
ipcRenderer.on('ping', function ({ senderId, senderIsMainFrame }, payload) {
ipcRenderer.sendTo(senderId, 'pong', { payload, senderIsMainFrame });
});

ipcRenderer.on('ping-æøåü', function (event, payload) {
Expand Down

0 comments on commit e2d17db

Please sign in to comment.