fix: optional postMessage transfer arg (#32433)

This commit is contained in:
Shelley Vohr 2022-01-13 14:52:48 +01:00 committed by GitHub
parent 1aaa0fad69
commit 6c88e3b8e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 2 deletions

View file

@ -210,7 +210,7 @@ void WebFrameMain::PostMessage(v8::Isolate* isolate,
} }
std::vector<gin::Handle<MessagePort>> wrapped_ports; std::vector<gin::Handle<MessagePort>> wrapped_ports;
if (transfer) { if (transfer && !transfer.value()->IsUndefined()) {
if (!gin::ConvertFromV8(isolate, *transfer, &wrapped_ports)) { if (!gin::ConvertFromV8(isolate, *transfer, &wrapped_ports)) {
isolate->ThrowException(v8::Exception::Error( isolate->ThrowException(v8::Exception::Error(
gin::StringToV8(isolate, "Invalid value for transfer"))); gin::StringToV8(isolate, "Invalid value for transfer")));

View file

@ -146,7 +146,7 @@ class IPCRenderer : public gin::Wrappable<IPCRenderer>,
} }
std::vector<v8::Local<v8::Object>> transferables; std::vector<v8::Local<v8::Object>> transferables;
if (transfer) { if (transfer && !transfer.value()->IsUndefined()) {
if (!gin::ConvertFromV8(isolate, *transfer, &transferables)) { if (!gin::ConvertFromV8(isolate, *transfer, &transferables)) {
thrower.ThrowTypeError("Invalid value for transfer"); thrower.ThrowTypeError("Invalid value for transfer");
return; return;

View file

@ -216,6 +216,18 @@ describe('ipc module', () => {
expect(port).to.be.an.instanceOf(EventEmitter); expect(port).to.be.an.instanceOf(EventEmitter);
}); });
it('can sent a message without a transfer', async () => {
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
w.loadURL('about:blank');
const p = emittedOnce(ipcMain, 'port');
await w.webContents.executeJavaScript(`(${function () {
require('electron').ipcRenderer.postMessage('port', 'hi');
}})()`);
const [ev, msg] = await p;
expect(msg).to.equal('hi');
expect(ev.ports).to.deep.equal([]);
});
it('can communicate between main and renderer', async () => { it('can communicate between main and renderer', async () => {
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } }); const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
w.loadURL('about:blank'); w.loadURL('about:blank');