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:
parent
09e6e4b9a7
commit
3df6d337f3
11 changed files with 71 additions and 21 deletions
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
* `sender` [IpcRenderer](../ipc-renderer.md) - The `IpcRenderer` instance that emitted the event originally
|
* `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`.
|
* `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
|
* `ports` [MessagePort][][] - A list of MessagePorts that were transferred with this message
|
||||||
|
|
||||||
[ipc-renderer-sendto]: ../ipc-renderer.md#ipcrenderersendtowebcontentsid-channel-args-deprecated
|
[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
|
// ElectronApiServiceImpl will look for the "ipcNative" hidden object when
|
||||||
// invoking the 'onMessage' callback.
|
// invoking the 'onMessage' callback.
|
||||||
v8Util.setHiddenValue(global, 'ipcNative', {
|
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) {
|
if (internal && senderId !== 0) {
|
||||||
console.error(`Message ${channel} sent by unexpected WebContents (${senderId})`);
|
console.error(`Message ${channel} sent by unexpected WebContents (${senderId})`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const sender = internal ? ipcRendererInternal : ipcRenderer;
|
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,
|
void WebContents::MessageTo(int32_t web_contents_id,
|
||||||
const std::string& channel,
|
const std::string& channel,
|
||||||
blink::CloneableMessage arguments) {
|
blink::CloneableMessage arguments,
|
||||||
|
content::RenderFrameHost* render_frame_host) {
|
||||||
TRACE_EVENT1("electron", "WebContents::MessageTo", "channel", channel);
|
TRACE_EVENT1("electron", "WebContents::MessageTo", "channel", channel);
|
||||||
auto* target_web_contents = FromID(web_contents_id);
|
auto* target_web_contents = FromID(web_contents_id);
|
||||||
|
|
||||||
|
@ -2047,8 +2048,10 @@ void WebContents::MessageTo(int32_t web_contents_id,
|
||||||
return;
|
return;
|
||||||
|
|
||||||
int32_t sender_id = ID();
|
int32_t sender_id = ID();
|
||||||
|
bool sender_is_main_frame = render_frame_host->GetParent() == nullptr;
|
||||||
web_frame_main->GetRendererApi()->Message(false /* internal */, channel,
|
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);
|
content::RenderFrameHost* render_frame_host);
|
||||||
void MessageTo(int32_t web_contents_id,
|
void MessageTo(int32_t web_contents_id,
|
||||||
const std::string& channel,
|
const std::string& channel,
|
||||||
blink::CloneableMessage arguments);
|
blink::CloneableMessage arguments,
|
||||||
|
content::RenderFrameHost* render_frame_host);
|
||||||
void MessageHost(const std::string& channel,
|
void MessageHost(const std::string& channel,
|
||||||
blink::CloneableMessage arguments,
|
blink::CloneableMessage arguments,
|
||||||
content::RenderFrameHost* render_frame_host);
|
content::RenderFrameHost* render_frame_host);
|
||||||
|
|
|
@ -185,7 +185,8 @@ void WebFrameMain::Send(v8::Isolate* isolate,
|
||||||
return;
|
return;
|
||||||
|
|
||||||
GetRendererApi()->Message(internal, channel, std::move(message),
|
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() {
|
const mojo::Remote<mojom::ElectronRenderer>& WebFrameMain::GetRendererApi() {
|
||||||
|
|
|
@ -81,7 +81,8 @@ void ElectronApiIPCHandlerImpl::MessageTo(int32_t web_contents_id,
|
||||||
blink::CloneableMessage arguments) {
|
blink::CloneableMessage arguments) {
|
||||||
api::WebContents* api_web_contents = api::WebContents::From(web_contents());
|
api::WebContents* api_web_contents = api::WebContents::From(web_contents());
|
||||||
if (api_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,
|
bool internal,
|
||||||
string channel,
|
string channel,
|
||||||
blink.mojom.CloneableMessage arguments,
|
blink.mojom.CloneableMessage arguments,
|
||||||
int32 sender_id);
|
int32 sender_id,
|
||||||
|
bool sender_is_main_frame);
|
||||||
|
|
||||||
ReceivePostMessage(string channel, blink.mojom.TransferableMessage message);
|
ReceivePostMessage(string channel, blink.mojom.TransferableMessage message);
|
||||||
|
|
||||||
|
|
|
@ -83,7 +83,8 @@ void EmitIPCEvent(v8::Local<v8::Context> context,
|
||||||
const std::string& channel,
|
const std::string& channel,
|
||||||
std::vector<v8::Local<v8::Value>> ports,
|
std::vector<v8::Local<v8::Value>> ports,
|
||||||
v8::Local<v8::Value> args,
|
v8::Local<v8::Value> args,
|
||||||
int32_t sender_id) {
|
int32_t sender_id = 0,
|
||||||
|
bool sender_is_main_frame = false) {
|
||||||
auto* isolate = context->GetIsolate();
|
auto* isolate = context->GetIsolate();
|
||||||
|
|
||||||
v8::HandleScope handle_scope(isolate);
|
v8::HandleScope handle_scope(isolate);
|
||||||
|
@ -92,9 +93,12 @@ void EmitIPCEvent(v8::Local<v8::Context> context,
|
||||||
v8::MicrotasksScope::kRunMicrotasks);
|
v8::MicrotasksScope::kRunMicrotasks);
|
||||||
|
|
||||||
std::vector<v8::Local<v8::Value>> argv = {
|
std::vector<v8::Local<v8::Value>> argv = {
|
||||||
gin::ConvertToV8(isolate, internal), gin::ConvertToV8(isolate, channel),
|
gin::ConvertToV8(isolate, internal),
|
||||||
gin::ConvertToV8(isolate, ports), args,
|
gin::ConvertToV8(isolate, channel),
|
||||||
gin::ConvertToV8(isolate, sender_id)};
|
gin::ConvertToV8(isolate, ports),
|
||||||
|
args,
|
||||||
|
gin::ConvertToV8(isolate, sender_id),
|
||||||
|
gin::ConvertToV8(isolate, sender_is_main_frame)};
|
||||||
|
|
||||||
InvokeIpcCallback(context, "onMessage", argv);
|
InvokeIpcCallback(context, "onMessage", argv);
|
||||||
}
|
}
|
||||||
|
@ -157,7 +161,8 @@ void ElectronApiServiceImpl::OnConnectionError() {
|
||||||
void ElectronApiServiceImpl::Message(bool internal,
|
void ElectronApiServiceImpl::Message(bool internal,
|
||||||
const std::string& channel,
|
const std::string& channel,
|
||||||
blink::CloneableMessage arguments,
|
blink::CloneableMessage arguments,
|
||||||
int32_t sender_id) {
|
int32_t sender_id,
|
||||||
|
bool sender_is_main_frame) {
|
||||||
blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
|
blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
|
||||||
if (!frame)
|
if (!frame)
|
||||||
return;
|
return;
|
||||||
|
@ -170,7 +175,8 @@ void ElectronApiServiceImpl::Message(bool internal,
|
||||||
|
|
||||||
v8::Local<v8::Value> args = gin::ConvertToV8(isolate, arguments);
|
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(
|
void ElectronApiServiceImpl::ReceivePostMessage(
|
||||||
|
@ -197,8 +203,7 @@ void ElectronApiServiceImpl::ReceivePostMessage(
|
||||||
|
|
||||||
std::vector<v8::Local<v8::Value>> args = {message_value};
|
std::vector<v8::Local<v8::Value>> args = {message_value};
|
||||||
|
|
||||||
EmitIPCEvent(context, false, channel, ports, gin::ConvertToV8(isolate, args),
|
EmitIPCEvent(context, false, channel, ports, gin::ConvertToV8(isolate, args));
|
||||||
0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ElectronApiServiceImpl::TakeHeapSnapshot(
|
void ElectronApiServiceImpl::TakeHeapSnapshot(
|
||||||
|
|
|
@ -35,7 +35,8 @@ class ElectronApiServiceImpl : public mojom::ElectronRenderer,
|
||||||
void Message(bool internal,
|
void Message(bool internal,
|
||||||
const std::string& channel,
|
const std::string& channel,
|
||||||
blink::CloneableMessage arguments,
|
blink::CloneableMessage arguments,
|
||||||
int32_t sender_id) override;
|
int32_t sender_id,
|
||||||
|
bool sender_is_main_frame) override;
|
||||||
void ReceivePostMessage(const std::string& channel,
|
void ReceivePostMessage(const std::string& channel,
|
||||||
blink::TransferableMessage message) override;
|
blink::TransferableMessage message) override;
|
||||||
void TakeHeapSnapshot(mojo::ScopedHandle file,
|
void TakeHeapSnapshot(mojo::ScopedHandle file,
|
||||||
|
|
|
@ -9,7 +9,14 @@ describe('ipcRenderer module', () => {
|
||||||
|
|
||||||
let w: BrowserWindow;
|
let w: BrowserWindow;
|
||||||
before(async () => {
|
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');
|
await w.loadURL('about:blank');
|
||||||
});
|
});
|
||||||
after(async () => {
|
after(async () => {
|
||||||
|
@ -154,7 +161,36 @@ describe('ipcRenderer module', () => {
|
||||||
ipcRenderer.sendTo(${contents.id}, 'ping', ${JSON.stringify(payload)})
|
ipcRenderer.sendTo(${contents.id}, 'ping', ${JSON.stringify(payload)})
|
||||||
ipcRenderer.once('pong', (event, data) => resolve(data))
|
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 () => {
|
it('sends message on channel with non-ASCII characters to WebContents', async () => {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
const { ipcRenderer } = require('electron');
|
const { ipcRenderer } = require('electron');
|
||||||
|
|
||||||
ipcRenderer.on('ping', function (event, payload) {
|
ipcRenderer.on('ping', function ({ senderId, senderIsMainFrame }, payload) {
|
||||||
ipcRenderer.sendTo(event.senderId, 'pong', payload);
|
ipcRenderer.sendTo(senderId, 'pong', { payload, senderIsMainFrame });
|
||||||
});
|
});
|
||||||
|
|
||||||
ipcRenderer.on('ping-æøåü', function (event, payload) {
|
ipcRenderer.on('ping-æøåü', function (event, payload) {
|
||||||
|
|
Loading…
Reference in a new issue