Skip to content

Commit

Permalink
Bump electron; Use exposeInMainWorld; (#41)
Browse files Browse the repository at this point in the history
* Bymp electron; Use exposeInMainWorld;

* Add test case;

Co-authored-by: Kiril Knysh <>
  • Loading branch information
kirilknysh authored Jun 15, 2021
1 parent a9c0fc9 commit 36b47ce
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 169 deletions.
19 changes: 13 additions & 6 deletions app/backends/electron/e-app/e-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,19 @@ function buildSandUrl(sand) {
}

function createWorker(options) {
const win = new BrowserWindow(Object.assign({
webPreferences: {
nodeIntegration: true,
nodeIntegrationInWorker: true,
},
}, args.browserWindow));
const win = new BrowserWindow(
Object.assign(
{
webPreferences: {
nodeIntegration: false,
nodeIntegrationInWorker: false,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js'),
},
},
args.browserWindow
)
);
const webContents = win.webContents;

if (args.showDevTools && args.browserWindow.show) {
Expand Down
20 changes: 20 additions & 0 deletions app/backends/electron/e-app/preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { contextBridge, ipcRenderer } = require('electron');

// inspired by https://stackoverflow.com/a/59888788/1404942
contextBridge.exposeInMainWorld('qd', {
onWorkerCreated(payload) {
ipcRenderer.send('wrk::created', payload);
},
onWorkerFilled(payload) {
ipcRenderer.send('wrk::filled', payload);
},
onWorkerDone(payload) {
ipcRenderer.send('wrk::done', payload);
},

onMessage(callback) {
ipcRenderer.on('message', (event, ...args) => {
callback(...args);
});
},
});
9 changes: 4 additions & 5 deletions app/backends/electron/e-app/sand/css.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
expectedLength: undefined,
qdidAttr: ''
};
var ipcRenderer = require('electron').ipcRenderer;

function evaluate(task, puzzleScope) {
// Throws a SYNTAX_ERR exception if the specified group of selectors is invalid.
Expand Down Expand Up @@ -46,7 +45,7 @@
}

function onWorkerInit() {
ipcRenderer.send('wrk::created', { path: [] });
window.qd.onWorkerCreated({ path: [] });
}

function onWorkerFill(payload) {
Expand All @@ -69,7 +68,7 @@

payload.content = undefined; // no need to transfer back the filler

ipcRenderer.send('wrk::filled', payload);
window.qd.onWorkerFilled(payload);
}

function onWorkerExec(payload) {
Expand All @@ -91,10 +90,10 @@
}
}

ipcRenderer.send('wrk::done', payload);
window.qd.onWorkerDone(payload);
}

ipcRenderer.on('message', function (event, message) {
window.qd.onMessage(function (message) {
switch (message.type) {
case 'wrk:>init':
return onWorkerInit();
Expand Down
11 changes: 5 additions & 6 deletions app/backends/electron/e-app/sand/js.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
hidden: [] // array of objects { input: {JSON}, expected: {JSON} }
};
var inputCopies = 200;
var ipcRenderer = require('electron').ipcRenderer;
window.require = stubFun;
window.process = stub;

function evaluate(task, puzzleContent) {
try {
var result = eval('(function(_, content, ipcRenderer, window, document, location, alert, $, require, process, global, setTimeout, setInterval, requestAnimationFrame, requestIdleCallback) {"use strict"; return (function (arg) {' + task.input + '})(content);}).call(stub, stub, puzzleContent, stub, stub, stub, stub, stubFun, stub, stubFun, stub, stub, stubFun, stubFun, stubFun, stubFun)');
var result = eval('(function(_, content, ipcRenderer, window, document, location, alert, $, require, process, global, setTimeout, setInterval, requestAnimationFrame, requestIdleCallback, qd) {"use strict"; return (function (arg) {' + task.input + '})(content);}).call(stub, stub, puzzleContent, stub, stub, stub, stub, stubFun, stub, stubFun, stub, stub, stubFun, stubFun, stubFun, stubFun, stub)');

return [null, result];
} catch (e) {
Expand Down Expand Up @@ -54,7 +53,7 @@
}

function onWorkerInit() {
ipcRenderer.send('wrk::created', { path: [] });
window.qd.onWorkerCreated({ path: [] });
}

function onWorkerFill(payload) {
Expand Down Expand Up @@ -104,7 +103,7 @@

payload.content = undefined; // no need to transfer back the filler

ipcRenderer.send('wrk::filled', payload);
window.qd.onWorkerFilled(payload);
}

function onWorkerExec(payload) {
Expand Down Expand Up @@ -135,10 +134,10 @@
}
}

ipcRenderer.send('wrk::done', payload);
window.qd.onWorkerDone(payload);
}

ipcRenderer.on('message', function (event, message) {
window.qd.onMessage(function (message) {
switch (message.type) {
case 'wrk:>init':
return onWorkerInit();
Expand Down
9 changes: 4 additions & 5 deletions app/backends/electron/e-app/sand/js/subworkers.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<head>
<script>
(function() {
var ipcRenderer = require('electron').ipcRenderer;
var subworkerCounter = 0;
var subworkers = {};

Expand All @@ -12,17 +11,17 @@

switch(data.type) {
case 'wrk::created':
return ipcRenderer.send('wrk::created', {
return window.qd.onWorkerCreated({
path: [subworkerId].concat(data.payload.path)
});
case 'wrk::filled':
return ipcRenderer.send('wrk::filled', {
return window.qd.onWorkerFilled({
path: [subworkerId].concat(data.payload.path),
fillerId: data.payload.fillerId
});
case 'wrk::done':
data.payload.path = [subworkerId].concat(data.payload.path)
return ipcRenderer.send('wrk::done', data.payload);
return window.qd.onWorkerDone(data.payload);
}
}

Expand Down Expand Up @@ -93,7 +92,7 @@
createSubworker(subworkerId);
}

ipcRenderer.on('message', function (event, message) {
window.qd.onMessage(function (message) {
switch (message.type) {
case 'wrk:>init':
return onWorkerInit(message.payload);
Expand Down
11 changes: 5 additions & 6 deletions app/backends/electron/e-app/sand/lodash.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
hidden: [] // array of objects { input: {JSON}, expected: {JSON} }
};
var inputCopies = 200;
var ipcRenderer = require('electron').ipcRenderer;
var _lodash = Object.freeze(_);
window.require = stubFun;
window.process = stub;

function evaluate(task, puzzleContent) {
try {
var result = eval('(function(_, content, ipcRenderer, window, document, location, alert, $, require, process, global, setTimeout, setInterval, requestAnimationFrame, requestIdleCallback) {"use strict"; return _.chain(content).' + task.input + '.value()}).call(stub, _lodash, puzzleContent, stub, stub, stub, stub, stubFun, stub, stubFun, stub, stub, stubFun, stubFun, stubFun, stubFun)');
var result = eval('(function(_, content, ipcRenderer, window, document, location, alert, $, require, process, global, setTimeout, setInterval, requestAnimationFrame, requestIdleCallback, qd) {"use strict"; return _.chain(content).' + task.input + '.value()}).call(stub, _lodash, puzzleContent, stub, stub, stub, stub, stubFun, stub, stubFun, stub, stub, stubFun, stubFun, stubFun, stubFun, stub)');

return [null, result];
} catch (e) {
Expand Down Expand Up @@ -55,7 +54,7 @@
}

function onWorkerInit() {
ipcRenderer.send('wrk::created', { path: [] });
window.qd.onWorkerCreated({ path: [] });
}

function onWorkerFill(payload) {
Expand Down Expand Up @@ -105,7 +104,7 @@

payload.content = undefined; // no need to transfer back the filler

ipcRenderer.send('wrk::filled', payload);
window.qd.onWorkerFilled(payload);
}

function onWorkerExec(payload) {
Expand Down Expand Up @@ -136,10 +135,10 @@
}
}

ipcRenderer.send('wrk::done', payload);
window.qd.onWorkerDone(payload);
}

ipcRenderer.on('message', function (event, message) {
window.qd.onMessage(function (message) {
switch (message.type) {
case 'wrk:>init':
return onWorkerInit();
Expand Down
9 changes: 4 additions & 5 deletions app/backends/electron/e-app/sand/lodash/subworkers.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<head>
<script>
(function() {
var ipcRenderer = require('electron').ipcRenderer;
var subworkerCounter = 0;
var subworkers = {};

Expand All @@ -12,17 +11,17 @@

switch(data.type) {
case 'wrk::created':
return ipcRenderer.send('wrk::created', {
return window.qd.onWorkerCreated({
path: [subworkerId].concat(data.payload.path)
});
case 'wrk::filled':
return ipcRenderer.send('wrk::filled', {
return window.qd.onWorkerFilled({
path: [subworkerId].concat(data.payload.path),
fillerId: data.payload.fillerId
});
case 'wrk::done':
data.payload.path = [subworkerId].concat(data.payload.path)
return ipcRenderer.send('wrk::done', data.payload);
return window.qd.onWorkerDone(data.payload);
}
}

Expand Down Expand Up @@ -93,7 +92,7 @@
createSubworker(subworkerId);
}

ipcRenderer.on('message', function (event, message) {
window.qd.onMessage(function (message) {
switch (message.type) {
case 'wrk:>init':
return onWorkerInit(message.payload);
Expand Down
Loading

0 comments on commit 36b47ce

Please sign in to comment.