SymphonyElectron/js/main.js

235 lines
6.8 KiB
JavaScript
Raw Normal View History

2017-02-14 19:44:15 -06:00
'use strict';
2016-11-07 14:31:39 -06:00
const electron = require('electron');
2017-02-10 20:20:09 -06:00
const packageJSON = require('../package.json');
2016-11-07 14:31:39 -06:00
const menuTemplate = require('./menuTemplate.js');
2017-02-10 20:20:09 -06:00
const path = require('path');
const app = electron.app;
const nodeURL = require('url');
const getConfig = require('./getConfig.js');
const { isMac, isDevEnv, getGuid } = require('./utils.js');
2017-02-28 16:45:04 -06:00
const loadErrors = require('./dialogs/showLoadError.js');
// show dialog when certificate errors occur
require('./dialogs/showCertError.js');
2017-02-12 18:57:56 -06:00
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
let windows = {};
2016-11-08 18:32:04 -06:00
let willQuitApp = false;
2017-02-28 16:45:04 -06:00
let isOnline = true;
2016-09-26 21:39:43 -05:00
2017-02-12 18:57:56 -06:00
if (require('electron-squirrel-startup')) {
return;
}
2016-11-07 14:31:39 -06:00
function createMainWindow (url) {
let key = getGuid();
2017-02-12 18:57:56 -06:00
mainWindow = new electron.BrowserWindow({
title: 'Symphony',
width: 1024, height: 768,
2017-02-28 16:45:04 -06:00
show: true,
2017-02-12 18:57:56 -06:00
webPreferences: {
2017-02-12 19:11:02 -06:00
sandbox: true,
2017-02-12 18:57:56 -06:00
nodeIntegration: false,
2017-02-14 19:44:15 -06:00
preload: path.join(__dirname, '/preload.js'),
2017-02-12 18:57:56 -06:00
winKey: key
}
});
2017-02-28 16:45:04 -06:00
function retry() {
if (isOnline) {
mainWindow.webContents && mainWindow.webContents.reload();
} else {
loadErrors.showNetworkConnectivityError(mainWindow, url, retry);
}
}
// content can be cached and will still finish load but
// we might not have netowrk connectivity, so warn the user.
mainWindow.webContents.once('did-finish-load', function() {
if (!isOnline) {
loadErrors.showNetworkConnectivityError(mainWindow, url, retry);
}
});
2017-02-28 16:45:04 -06:00
mainWindow.webContents.once('did-fail-load', function(event, errorCode,
errorDesc, validatedURL, isMainFrame) {
loadErrors.showLoadFailure(mainWindow, url, errorDesc, errorCode, retry);
});
2017-02-12 18:57:56 -06:00
2017-02-28 16:45:04 -06:00
storeWindowKey(key, mainWindow);
mainWindow.loadURL(url);
2017-02-12 19:11:02 -06:00
2017-02-12 18:57:56 -06:00
const menu = electron.Menu.buildFromTemplate(menuTemplate(app));
electron.Menu.setApplicationMenu(menu);
mainWindow.on('close', function(e) {
if (willQuitApp) {
mainWindow = null;
return;
}
// mac should hide window when hitting x close
if (isMac) {
2017-02-12 18:57:56 -06:00
mainWindow.hide();
e.preventDefault();
}
});
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
2016-11-08 18:32:04 -06:00
mainWindow = null;
2017-02-12 18:57:56 -06:00
});
// open external links in default browser - window.open
mainWindow.webContents.on('new-window', function(event, url) {
event.preventDefault();
electron.shell.openExternal(url);
});
}
function storeWindowKey(key, browserWin) {
windows[key] = browserWin;
}
/**
* 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 (event && event.sender) {
// validate that event sender is from window we created
2017-02-14 19:44:15 -06:00
let browserWin = electron.BrowserWindow.fromWebContents(event.sender);
2017-02-12 18:57:56 -06:00
let winKey = event.sender.browserWindowOptions &&
event.sender.browserWindowOptions.webPreferences &&
event.sender.browserWindowOptions.webPreferences.winKey;
if (browserWin instanceof electron.BrowserWindow) {
let win = windows[winKey];
return win && win === browserWin;
}
2016-11-08 18:32:04 -06:00
}
2017-02-12 18:57:56 -06:00
return false;
2016-09-26 21:39:43 -05:00
}
2017-02-14 19:44:15 -06:00
/**
* Only permit certain cmds for some windows
* @param {EventEmitter} event node emitter event to be tested
* @param {String} cmd cmd name
* @return {Boolean} true if cmd is allowed for window, otherwise false
*/
function isCmdAllowed(event, cmd) {
if (event && event.sender && cmd) {
// validate that event sender is from window we created
let browserWin = electron.BrowserWindow.fromWebContents(event.sender);
if (browserWin === mainWindow) {
// allow all commands for main window
return true;
} else {
// allow only certain cmds for child windows
// e.g., open cmd not allowed for child windows
return (arg.cmd !== 'open');
}
}
return false;
}
2017-02-13 18:31:42 -06:00
/**
* Handle ipc messages from renderers. Only messages from windows we have
* created are allowed.
*/
2017-02-10 20:20:09 -06:00
electron.ipcMain.on('symphony-msg', (event, arg) => {
2017-02-12 18:57:56 -06:00
if (!isValidWindow(event)) {
console.log('invalid window try to perform action, ignoring action.');
return;
}
2017-02-14 19:44:15 -06:00
if (!isCmdAllowed(event, arg && arg.cmd)) {
2017-02-28 16:45:04 -06:00
console.log('cmd not allowed for this window: ' + arg.cmd);
return;
}
if (arg && arg.cmd === 'isOnline') {
isOnline = arg.isOnline;
2017-02-14 19:44:15 -06:00
return;
}
2017-02-10 20:20:09 -06:00
if (arg && arg.cmd === 'open' && arg.url) {
2017-02-12 18:57:56 -06:00
let width = arg.width || 1024;
let height = arg.height || 768;
let title = arg.title || 'Symphony';
let winKey = getGuid();
2017-02-10 20:20:09 -06:00
2017-02-12 18:57:56 -06:00
let childWindow = new electron.BrowserWindow({
2017-02-10 20:20:09 -06:00
title: title,
width: width,
height: height,
webPreferences: {
2017-02-12 19:11:02 -06:00
sandbox: true,
2017-02-10 20:20:09 -06:00
nodeIntegration: false,
2017-02-14 19:44:15 -06:00
preload: path.join(__dirname, '/preload.js'),
2017-02-12 18:57:56 -06:00
winKey: winKey
2017-02-10 20:20:09 -06:00
}
});
2017-02-12 18:57:56 -06:00
storeWindowKey(winKey, childWindow);
2017-02-10 20:20:09 -06:00
childWindow.loadURL(arg.url);
return;
}
});
2017-02-13 18:31:42 -06:00
/**
* This method will be called when Electron has finished
* initialization and is ready to create browser windows.
* Some APIs can only be used after this event occurs.
*/
app.on('ready', getUrlAndOpenMainWindow);
function getUrlAndOpenMainWindow() {
getConfig().then(function(config) {
let protocol = '';
// add https protocol if none found.
let parsedUrl = nodeURL.parse(config.url);
if (!parsedUrl.protocol) {
protocol = 'https';
}
var url = nodeURL.format({
protocol: protocol,
slahes: true,
pathname: parsedUrl.href
});
createMainWindow(url);
}).catch(function(err) {
2017-02-28 16:45:04 -06:00
let title = 'Error loading configuration';
electron.dialog.showErrorBox(title, title + ': ' + err);
});
}
2016-11-08 18:32:04 -06:00
app.on('before-quit', function() {
willQuitApp = true;
});
2016-09-26 21:39:43 -05:00
app.on('window-all-closed', function () {
2017-02-12 18:57:56 -06:00
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (!isMac) {
2017-02-12 18:57:56 -06:00
app.quit();
}
});
2016-09-26 21:39:43 -05:00
app.on('activate', function () {
2017-02-12 18:57:56 -06:00
if (mainWindow === null) {
getUrlAndOpenMainWindow();
2017-02-12 18:57:56 -06:00
} else {
mainWindow.show();
}
2016-11-07 14:31:39 -06:00
});