From 3df6d337f350d4b3b2e68cf8bec48b432e078da5 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Mon, 24 Jul 2023 14:27:30 +0200 Subject: [PATCH] feat: add `senderIsMainFrame` to messages sent via `ipcRenderer.sendTo()` (#38868) * feat: add isMainFrame to events emitted via ipcRenderer.sendTo() * chore: rename isMainFrame to senderIsMainFrame --- docs/api/structures/ipc-renderer-event.md | 1 + lib/renderer/common-init.ts | 4 +- .../browser/api/electron_api_web_contents.cc | 7 +++- shell/browser/api/electron_api_web_contents.h | 3 +- .../api/electron_api_web_frame_main.cc | 3 +- .../browser/electron_api_ipc_handler_impl.cc | 3 +- shell/common/api/api.mojom | 3 +- shell/renderer/electron_api_service_impl.cc | 21 ++++++---- shell/renderer/electron_api_service_impl.h | 3 +- spec/api-ipc-renderer-spec.ts | 40 ++++++++++++++++++- spec/fixtures/module/preload-ipc-ping-pong.js | 4 +- 11 files changed, 71 insertions(+), 21 deletions(-) diff --git a/docs/api/structures/ipc-renderer-event.md b/docs/api/structures/ipc-renderer-event.md index 0f9c1686744..8c4fe1bceea 100644 --- a/docs/api/structures/ipc-renderer-event.md +++ b/docs/api/structures/ipc-renderer-event.md @@ -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-deprecated diff --git a/lib/renderer/common-init.ts b/lib/renderer/common-init.ts index bfec34419dd..d4e4bfb910c 100644 --- a/lib/renderer/common-init.ts +++ b/lib/renderer/common-init.ts @@ -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); } }); diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index 83ef431252f..f1f21d35e66 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -2031,7 +2031,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); @@ -2047,8 +2048,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); } } diff --git a/shell/browser/api/electron_api_web_contents.h b/shell/browser/api/electron_api_web_contents.h index e1528083277..9273cdf95ad 100644 --- a/shell/browser/api/electron_api_web_contents.h +++ b/shell/browser/api/electron_api_web_contents.h @@ -431,7 +431,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); diff --git a/shell/browser/api/electron_api_web_frame_main.cc b/shell/browser/api/electron_api_web_frame_main.cc index a1d06620f2d..6bd620b99b0 100644 --- a/shell/browser/api/electron_api_web_frame_main.cc +++ b/shell/browser/api/electron_api_web_frame_main.cc @@ -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& WebFrameMain::GetRendererApi() { diff --git a/shell/browser/electron_api_ipc_handler_impl.cc b/shell/browser/electron_api_ipc_handler_impl.cc index 864a670726c..2a05bd9d762 100644 --- a/shell/browser/electron_api_ipc_handler_impl.cc +++ b/shell/browser/electron_api_ipc_handler_impl.cc @@ -81,7 +81,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()); } } diff --git a/shell/common/api/api.mojom b/shell/common/api/api.mojom index 4515b82ac48..fdf082c25c0 100644 --- a/shell/common/api/api.mojom +++ b/shell/common/api/api.mojom @@ -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); diff --git a/shell/renderer/electron_api_service_impl.cc b/shell/renderer/electron_api_service_impl.cc index ab11b64ec39..b1c980a72d2 100644 --- a/shell/renderer/electron_api_service_impl.cc +++ b/shell/renderer/electron_api_service_impl.cc @@ -83,7 +83,8 @@ void EmitIPCEvent(v8::Local context, const std::string& channel, std::vector> ports, v8::Local 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); @@ -92,9 +93,12 @@ void EmitIPCEvent(v8::Local context, v8::MicrotasksScope::kRunMicrotasks); std::vector> 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); } @@ -157,7 +161,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; @@ -170,7 +175,8 @@ void ElectronApiServiceImpl::Message(bool internal, v8::Local 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( @@ -197,8 +203,7 @@ void ElectronApiServiceImpl::ReceivePostMessage( std::vector> 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( diff --git a/shell/renderer/electron_api_service_impl.h b/shell/renderer/electron_api_service_impl.h index 51225208f44..0d35b51283c 100644 --- a/shell/renderer/electron_api_service_impl.h +++ b/shell/renderer/electron_api_service_impl.h @@ -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, diff --git a/spec/api-ipc-renderer-spec.ts b/spec/api-ipc-renderer-spec.ts index 446eecb389b..23d0c450b7e 100644 --- a/spec/api-ipc-renderer-spec.ts +++ b/spec/api-ipc-renderer-spec.ts @@ -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 () => { @@ -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 = once(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 once(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 () => { diff --git a/spec/fixtures/module/preload-ipc-ping-pong.js b/spec/fixtures/module/preload-ipc-ping-pong.js index 41d4c753822..6fb1834628d 100644 --- a/spec/fixtures/module/preload-ipc-ping-pong.js +++ b/spec/fixtures/module/preload-ipc-ping-pong.js @@ -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) {