2020-11-07 17:59:37 +02:00
|
|
|
import Plugin from '@joplin/lib/services/plugins/Plugin';
|
|
|
|
import BasePluginRunner from '@joplin/lib/services/plugins/BasePluginRunner';
|
|
|
|
import executeSandboxCall from '@joplin/lib/services/plugins/utils/executeSandboxCall';
|
|
|
|
import Global from '@joplin/lib/services/plugins/api/Global';
|
2020-10-09 19:35:46 +02:00
|
|
|
import bridge from '../bridge';
|
2020-11-07 17:59:37 +02:00
|
|
|
import Setting from '@joplin/lib/models/Setting';
|
|
|
|
import { EventHandlers } from '@joplin/lib/services/plugins/utils/mapEventHandlersToIds';
|
|
|
|
import shim from '@joplin/lib/shim';
|
2020-11-27 01:31:31 +02:00
|
|
|
import Logger from '@joplin/lib/Logger';
|
2021-12-27 20:12:21 +02:00
|
|
|
import BackOffHandler from './BackOffHandler';
|
2020-10-09 19:35:46 +02:00
|
|
|
const ipcRenderer = require('electron').ipcRenderer;
|
|
|
|
|
2020-11-27 01:31:31 +02:00
|
|
|
const logger = Logger.create('PluginRunner');
|
|
|
|
|
2020-12-01 16:08:41 +02:00
|
|
|
// Electron error messages are useless so wrap the renderer call and print
|
|
|
|
// additional information when an error occurs.
|
|
|
|
function ipcRendererSend(message: string, args: any) {
|
|
|
|
try {
|
|
|
|
return ipcRenderer.send(message, args);
|
|
|
|
} catch (error) {
|
|
|
|
logger.error('Could not send IPC message:', message, ': ', args, error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
enum PluginMessageTarget {
|
|
|
|
MainWindow = 'mainWindow',
|
|
|
|
Plugin = 'plugin',
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface PluginMessage {
|
2020-11-12 21:29:22 +02:00
|
|
|
target: PluginMessageTarget;
|
|
|
|
pluginId: string;
|
|
|
|
callbackId?: string;
|
|
|
|
path?: string;
|
|
|
|
args?: any[];
|
|
|
|
result?: any;
|
|
|
|
error?: any;
|
|
|
|
mainWindowCallbackId?: string;
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
2020-10-21 02:43:06 +02:00
|
|
|
let callbackIndex = 1;
|
2020-11-12 21:13:28 +02:00
|
|
|
const callbackPromises: any = {};
|
2020-10-21 02:43:06 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
function mapEventIdsToHandlers(pluginId: string, arg: any) {
|
2020-10-09 19:35:46 +02:00
|
|
|
if (Array.isArray(arg)) {
|
|
|
|
for (let i = 0; i < arg.length; i++) {
|
|
|
|
arg[i] = mapEventIdsToHandlers(pluginId, arg[i]);
|
|
|
|
}
|
|
|
|
return arg;
|
|
|
|
} else if (typeof arg === 'string' && arg.indexOf('___plugin_event_') === 0) {
|
|
|
|
const eventId = arg;
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
return async (...args: any[]) => {
|
2020-10-21 02:43:06 +02:00
|
|
|
const callbackId = `cb_${pluginId}_${Date.now()}_${callbackIndex++}`;
|
|
|
|
|
|
|
|
const promise = new Promise((resolve, reject) => {
|
|
|
|
callbackPromises[callbackId] = { resolve, reject };
|
|
|
|
});
|
|
|
|
|
2020-12-01 16:08:41 +02:00
|
|
|
ipcRendererSend('pluginMessage', {
|
2020-10-21 02:43:06 +02:00
|
|
|
callbackId: callbackId,
|
2020-10-09 19:35:46 +02:00
|
|
|
target: PluginMessageTarget.Plugin,
|
|
|
|
pluginId: pluginId,
|
|
|
|
eventId: eventId,
|
|
|
|
args: args,
|
|
|
|
});
|
2020-10-21 02:43:06 +02:00
|
|
|
|
|
|
|
return promise;
|
2020-10-09 19:35:46 +02:00
|
|
|
};
|
|
|
|
} else if (arg === null) {
|
|
|
|
return null;
|
|
|
|
} else if (arg === undefined) {
|
|
|
|
return undefined;
|
|
|
|
} else if (typeof arg === 'object') {
|
|
|
|
for (const n in arg) {
|
|
|
|
arg[n] = mapEventIdsToHandlers(pluginId, arg[n]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class PluginRunner extends BasePluginRunner {
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
protected eventHandlers_: EventHandlers = {};
|
2021-12-27 20:12:21 +02:00
|
|
|
private backOffHandlers_: Record<string, BackOffHandler> = {};
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2021-12-27 20:12:21 +02:00
|
|
|
public constructor() {
|
2020-10-09 19:35:46 +02:00
|
|
|
super();
|
|
|
|
|
|
|
|
this.eventHandler = this.eventHandler.bind(this);
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
private async eventHandler(eventHandlerId: string, args: any[]) {
|
2020-10-09 19:35:46 +02:00
|
|
|
const cb = this.eventHandlers_[eventHandlerId];
|
|
|
|
return cb(...args);
|
|
|
|
}
|
|
|
|
|
2022-05-17 15:08:25 +02:00
|
|
|
// @ts-ignore
|
2021-12-27 20:12:21 +02:00
|
|
|
private backOffHandler(pluginId: string): BackOffHandler {
|
|
|
|
if (!this.backOffHandlers_[pluginId]) {
|
|
|
|
this.backOffHandlers_[pluginId] = new BackOffHandler(pluginId);
|
|
|
|
}
|
|
|
|
return this.backOffHandlers_[pluginId];
|
|
|
|
}
|
|
|
|
|
|
|
|
public async run(plugin: Plugin, pluginApi: Global) {
|
2020-10-09 19:35:46 +02:00
|
|
|
const scriptPath = `${Setting.value('tempDir')}/plugin_${plugin.id}.js`;
|
|
|
|
await shim.fsDriver().writeFile(scriptPath, plugin.scriptText, 'utf8');
|
|
|
|
|
|
|
|
const pluginWindow = bridge().newBrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true,
|
2021-10-01 20:35:27 +02:00
|
|
|
contextIsolation: false,
|
2020-10-09 19:35:46 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-10-01 20:35:27 +02:00
|
|
|
require('@electron/remote/main').enable(pluginWindow.webContents);
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
bridge().electronApp().registerPluginWindow(plugin.id, pluginWindow);
|
|
|
|
|
2021-12-28 15:17:59 +02:00
|
|
|
void pluginWindow.loadURL(`${require('url').format({
|
2020-10-09 19:35:46 +02:00
|
|
|
pathname: require('path').join(__dirname, 'plugin_index.html'),
|
|
|
|
protocol: 'file:',
|
|
|
|
slashes: true,
|
|
|
|
})}?pluginId=${encodeURIComponent(plugin.id)}&pluginScript=${encodeURIComponent(`file://${scriptPath}`)}`);
|
|
|
|
|
2020-11-19 14:34:49 +02:00
|
|
|
if (plugin.devMode) {
|
|
|
|
pluginWindow.webContents.once('dom-ready', () => {
|
2023-06-12 18:30:47 +02:00
|
|
|
// Need to open with a delay, otherwise it doesn't show up
|
|
|
|
setTimeout(() => {
|
|
|
|
pluginWindow.webContents.openDevTools({ mode: 'detach' });
|
|
|
|
}, 3000);
|
2020-11-19 14:34:49 +02:00
|
|
|
});
|
|
|
|
}
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
ipcRenderer.on('pluginMessage', async (_event: any, message: PluginMessage) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
if (message.target !== PluginMessageTarget.MainWindow) return;
|
|
|
|
if (message.pluginId !== plugin.id) return;
|
|
|
|
|
2020-10-21 02:43:06 +02:00
|
|
|
if (message.mainWindowCallbackId) {
|
|
|
|
const promise = callbackPromises[message.mainWindowCallbackId];
|
|
|
|
|
|
|
|
if (!promise) {
|
|
|
|
console.error('Got a callback without matching promise: ', message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.error) {
|
|
|
|
promise.reject(message.error);
|
|
|
|
} else {
|
|
|
|
promise.resolve(message.result);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const mappedArgs = mapEventIdsToHandlers(plugin.id, message.args);
|
|
|
|
const fullPath = `joplin.${message.path}`;
|
|
|
|
|
2020-11-27 01:31:31 +02:00
|
|
|
// Don't log complete HTML code, which can be long, for setHtml calls
|
|
|
|
const debugMappedArgs = fullPath.includes('setHtml') ? '<hidden>' : mappedArgs;
|
2020-12-01 16:08:41 +02:00
|
|
|
logger.debug(`Got message (3): ${fullPath}`, debugMappedArgs);
|
2020-10-21 02:43:06 +02:00
|
|
|
|
2022-04-19 16:52:32 +02:00
|
|
|
this.recordCallStat(plugin.id);
|
|
|
|
|
2022-05-17 15:08:25 +02:00
|
|
|
// try {
|
|
|
|
// await this.backOffHandler(plugin.id).wait(fullPath, debugMappedArgs);
|
|
|
|
// } catch (error) {
|
|
|
|
// logger.error(error);
|
|
|
|
// return;
|
|
|
|
// }
|
2021-12-27 20:12:21 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
let result: any = null;
|
|
|
|
let error: any = null;
|
2020-10-21 02:43:06 +02:00
|
|
|
try {
|
|
|
|
result = await executeSandboxCall(plugin.id, pluginApi, fullPath, mappedArgs, this.eventHandler);
|
|
|
|
} catch (e) {
|
|
|
|
error = e ? e : new Error('Unknown error');
|
|
|
|
}
|
|
|
|
|
2020-12-01 16:08:41 +02:00
|
|
|
ipcRendererSend('pluginMessage', {
|
2020-10-21 02:43:06 +02:00
|
|
|
target: PluginMessageTarget.Plugin,
|
|
|
|
pluginId: plugin.id,
|
|
|
|
pluginCallbackId: message.callbackId,
|
|
|
|
result: result,
|
|
|
|
error: error,
|
|
|
|
});
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|