SymphonyElectron/js/mainApiMgr.js

98 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-03-01 18:32:21 -06:00
'use strict';
/**
* This module runs in the main process and handles api calls
* from the renderer process.
*/
const electron = require('electron');
const windowMgr = require('./windowMgr.js');
const log = require('./log.js');
2017-03-06 23:09:10 -06:00
const badgeCount = require('./badgeCount.js');
2017-03-01 18:32:21 -06:00
2017-03-07 16:44:31 -06:00
const apiEnums = require('./enums/api.js');
const apiCmds = apiEnums.cmds;
const apiName = apiEnums.apiName;
// can be overridden for testing
let checkValidWindow = true;
2017-03-01 18:32:21 -06:00
/**
* Ensure events comes from a window that we have created.
* @param {EventEmitter} event node emitter event to be tested
* @return {Boolean} returns true if exists otherwise false
*/
function isValidWindow(event) {
if (!checkValidWindow) {
return true;
}
var result = false;
2017-03-01 18:32:21 -06:00
if (event && event.sender) {
// validate that event sender is from window we created
2017-03-03 18:07:48 -06:00
const browserWin = electron.BrowserWindow.fromWebContents(event.sender);
const winKey = event.sender.browserWindowOptions &&
2017-04-18 11:02:25 -05:00
event.sender.browserWindowOptions.winKey;
2017-03-01 18:32:21 -06:00
result = windowMgr.hasWindow(browserWin, winKey);
2017-03-01 18:32:21 -06:00
}
if (!result) {
/* eslint-disable no-console */
2017-04-18 11:02:25 -05:00
console.log('invalid window try to perform action, ignoring action');
/* eslint-enable no-console */
}
return result;
2017-03-01 18:32:21 -06:00
}
/**
* Handle API related ipc messages from renderers. Only messages from windows
* we have created are allowed.
*/
2017-03-07 16:44:31 -06:00
electron.ipcMain.on(apiName, (event, arg) => {
2017-03-01 18:32:21 -06:00
if (!isValidWindow(event)) {
return;
}
if (!arg) {
return;
}
2017-03-07 16:44:31 -06:00
if (arg.cmd === apiCmds.isOnline && typeof arg.isOnline === 'boolean') {
2017-03-01 18:32:21 -06:00
windowMgr.setIsOnline(arg.isOnline);
return;
}
2017-03-07 16:44:31 -06:00
if (arg.cmd === apiCmds.setBadgeCount && typeof arg.count === 'number') {
2017-03-06 23:09:10 -06:00
badgeCount.show(arg.count);
return;
}
2017-03-07 16:44:31 -06:00
if (arg.cmd === apiCmds.badgeDataUrl && typeof arg.dataUrl === 'string' &&
2017-03-06 23:09:10 -06:00
typeof arg.count === 'number') {
badgeCount.setDataUrl(arg.dataUrl, arg.count);
return;
}
2017-04-18 11:02:25 -05:00
if (arg.cmd === apiCmds.activate && typeof arg.windowName === 'string') {
windowMgr.activate(arg.windowName);
2017-03-01 18:32:21 -06:00
return;
}
if (arg.cmd === apiCmds.registerBoundsChange) {
windowMgr.setBoundsChangeWindow(event.sender);
}
2017-04-18 11:02:25 -05:00
if (arg.cmd === apiCmds.registerLogger) {
// renderer window that has a registered logger from JS.
log.setLogWindow(event.sender);
2017-03-01 18:32:21 -06:00
}
});
// expose these methods primarily for testing...
module.exports = {
shouldCheckValidWindow: function(shouldCheck) {
checkValidWindow = shouldCheck;
}
}