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

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

* chore: rename isMainFrame to senderIsMainFrame
This commit is contained in:
Milan Burda 2023-07-24 14:27:30 +02:00 коммит произвёл GitHub
Родитель 09e6e4b9a7
Коммит 3df6d337f3
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
11 изменённых файлов: 71 добавлений и 21 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -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);
}
});

Просмотреть файл

@ -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);
}
}

Просмотреть файл

@ -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);

Просмотреть файл

@ -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() {

Просмотреть файл

@ -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());
}
}

Просмотреть файл

@ -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);

Просмотреть файл

@ -83,7 +83,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);
@ -92,9 +93,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);
}
@ -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<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(
@ -197,8 +203,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(

Просмотреть файл

@ -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,

Просмотреть файл

@ -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 () => {

Просмотреть файл

@ -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) {