forked from particl/particl-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
427 lines (348 loc) · 12 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
const electron = require('electron');
const path = require('path');
const fs = require('fs');
const _url = require('url');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
/* correct userData to respect Linux standards */
if (process.platform === 'linux') {
app.setName(app.getName().toLowerCase().split(' ').join('-'));
app.setPath('userData', `${app.getPath('appData')}/${app.getName()}`);
}
// Ensure only one application instance is currently running
const instanceLock = app.requestSingleInstanceLock();
if (!instanceLock) {
app.exit();
}
// Set up logging: turn off file logging until we know that the target directory is created and writeable
let log;
let mainWindow;
let mainTrayInstance;
try {
log = require('electron-log');
log.transports.file.level = false;
log.transports.console.level = 'debug';
} catch(err) {
console.log('Fatal Error: Failed to setup logging: ', err);
}
if (!log) {
app.exit();
}
// Create if not existing the base application path
const appUserDataPath = app.getPath('userData');
try {
[
appUserDataPath
].map(path => !fs.existsSync(path) && fs.mkdirSync(path));
} catch(err) {
log.error('Cannot write to filesystem: ', err);
app.exit();
}
const modManager = require('./modules/init');
// Obtain system configuration/settings: set the relevant settings base path (can only be done once, so do it now)
const settingsManager = require('./modules/settingsManager');
settingsManager.setBasePathDir(appUserDataPath);
if (!settingsManager.loadDefaultSettings()) {
log.error('Invalid default settings!');
app.exit();
}
// Ensure relevant additional directories are created
try {
Object.values(settingsManager.getSettings(null, 'PATHS')).map(path => !fs.existsSync(path) && fs.mkdirSync(path));
} catch(err) {
log.error('Cannot write to filesystem: ', err);
app.exit();
}
// Default system setup is complete, and relevant directories are created, so turn on file logging and configure the logger correctly
log.transports.file.fileName = 'application.log';
log.transports.file.resolvePath = (variables) => {
return path.join(settingsManager.getSettings(null, 'PATHS').logs, variables.fileName);
}
log.transports.console.level = log.transports.file.level = settingsManager.getSettings(null, 'DEBUGGING_LEVEL');
log.hooks.push(
(message, transport) => {
message.data = message.data.map(m => typeof m === 'string' ? m.replaceAll(app.getPath('home'), '<USER_HOME_PATH>') : m);
return message;
}
);
if (process.platform === 'win32') {
// Fix for windows 10 regarding notifications, etc
try {
const appId = require('./buildConfiguration.json').appId;
if (typeof appId === 'string' && appId.length > 0) {
app.setAppUserModelId(appId);
}
} catch (err) {
log.error('Failed setting win32 AppUserModelId: ', err);
}
}
log.info(`Initializing ${app.getName()} : ${app.getVersion()}`);
/**
* CONFIGURE APPLICATION EVENTS
*/
// What to do if an attempt was made to launch a second instance
app.on('second-instance', () => {
if (BrowserWindow.getAllWindows().length > 0) {
const win = BrowserWindow.getAllWindows()[0];
if (win.isMinimized()) {
win.restore();
}
win.focus();
}
});
// Run these commands when the application is ready
app.whenReady().then(() => {
log.debug('Application startup configuration: ', JSON.stringify(settingsManager.getSettings(), null, 2));
// Prevent untrusted/unused permissions from automatically being allowed
// See https://www.electronjs.org/docs/tutorial/security#4-handle-session-permission-requests-from-remote-content
electron.session.defaultSession.setPermissionRequestHandler((webContents, permission, callback) => {
if (settingsManager.getSettings(null, 'APP_PERMISSIONS').includes(permission)) {
return callback(true);
}
return callback(false);
});
app.on('activate', () => {
// handles macOS behaviour: closing all windows does not quit the application, so re-activating the application needs to re-create the window
if (BrowserWindow.getAllWindows().length === 0) {
createMainGUI();
}
});
createMainGUI();
}).catch((err) => {
log.error('System crash event detected: ', err);
if (app.isReady()) {
openCrashWindow();
} else {
app.on('ready', () => openCrashWindow());
}
});
// Remove the menu from the created window
app.on('browser-window-created', function (e, window) {
window.setMenu(null);
});
app.on('web-contents-created', (event, contents) => {
/**
* This shouldn't technically be necessary to implement, but this takes measure to preventing webviews created in the DOM after the page has loaded DO NOT
* get the opportunity to obtain node integration (electron gives them their own )
* See https://www.electronjs.org/docs/tutorial/security#11-verify-webview-options-before-creation
*/
contents.on('will-attach-webview', (event, webPreferences, params) => {
// Strip away preload scripts if unused or verify their location is legitimate
delete webPreferences.preload;
delete webPreferences.preloadURL;
// Disable Node.js integration
webPreferences.nodeIntegration = false;
webPreferences.sandbox = true;
// Verify URL being loaded
if (!params.src.startsWith('http://localhost') || !params.src.startsWith('http://127.0.0')) {
event.preventDefault();
}
});
// Handle external URIs;
contents.on('new-window', (event, url) => {
event.preventDefault();
let matchesAllowedURL = false;
const allowedUrlTypes = settingsManager.getSettings(null, 'ALLOWED_EXTERNAL_URLS');
let allowedUrls = [];
if (allowedUrlTypes && Array.isArray(allowedUrlTypes.default) && Array.isArray(allowedUrlTypes.custom)) {
allowedUrls = [...allowedUrlTypes.default, ...allowedUrlTypes.custom];
}
for (const allowedUrl of allowedUrls) {
const testUrl = allowedUrl.endsWith('/') ? allowedUrl : `${allowedUrl}/`;
if ((url === allowedUrl) || url.startsWith(testUrl)) {
matchesAllowedURL = true;
break;
}
}
if (matchesAllowedURL) {
setImmediate(() => {
electron.shell.openExternal(url, {activate: true});
});
return;
}
const errorWin = createNewWindow(`${url}`, 500, 500, true);
errorWin.once('ready-to-show', () => errorWin.show());
errorWin.loadURL(_url.format({
protocol: 'file:',
pathname: path.join(
__dirname,
`${settingsManager.getSettings(null, 'MODE') === 'developer' ? 'src' : 'dist'}`,
'assets',
'modals',
'errorExternalUrlOpen.html'
),
slashes: true
}));
});
});
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
// Run just prior to the application quitting... run before the 'quit' event.
// This event is hooked instead of 'quit' so as to provide sufficient time for shutting down modules, etc.
app.once('will-quit', async function beforeQuit(event) {
app.removeListener('will-quit', beforeQuit);
event.preventDefault();
log.info('Shutdown requested...');
let closingWindow;
try {
// Display a 'modal'-like window indicating that the application is shutting down
closingWindow = createNewWindow('Closing Particl Desktop', 320, 500, false);
closingWindow.loadURL(_url.format({
protocol: 'file:',
pathname: path.join(
__dirname,
`${settingsManager.getSettings(null, 'MODE') === 'developer' ? 'src' : 'dist'}`,
'assets',
'modals',
'closing.html'
),
slashes: true
}));
closingWindow.setClosable(false);
} catch (err) {
log.error('Failed creating closing modal window -> ', err);
}
if (mainTrayInstance) {
try {
mainTrayInstance.destroy();
} catch (err) {
log.error('Failed to cleanup Tray item... deferring to system cleanup');
}
}
log.info('Cleaning up modules.');
await modManager.cleanup(true).catch(() => {
// do nothing, here just to ensure that we prevent errors from aborting the shutdown process
});
if (closingWindow) {
closingWindow.setClosable(true);
closingWindow.close();
}
log.info('Shutdown complete.');
app.quit();
});
function createNewWindow(title, minHeight, minWidth, showFrame = true) {
return new BrowserWindow({
title: title,
width: minWidth,
minWidth: minWidth,
height: minHeight,
minHeight: minHeight,
icon: path.join(__dirname, 'resources', 'icon.png'),
backgroundColor: '#222828',
frame: showFrame !== false,
darkTheme: true,
webPreferences: {
backgroundThrottling: false,
webviewTag: false,
nodeIntegration: false,
sandbox: true,
contextIsolation: true,
webSecurity: true,
enableRemoteModule: false,
preload: path.join(__dirname, 'preload.js')
}
});
}
function openCrashWindow() {
const errorWindow = createNewWindow('Particl Desktop Error', 320, 500, true);
errorWindow.loadURL(_url.format({
protocol: 'file:',
pathname: path.join(__dirname, `${settingsManager.getSettings(null, 'MODE') === 'developer' ? 'src' : 'dist'}`, 'assets', 'modals', 'crash.html'),
slashes: true
}));
errorWindow.setClosable(true);
errorWindow.on('closed', function () {
app.quit();
});
}
function createMainGUI() {
log.info('Building up UI elements...');
createSystemTray();
const win = createNewWindow('Particl Desktop', 675, 1270, true);
win.setMenuBarVisibility(false);
win.setAutoHideMenuBar(true);
const url = settingsManager.getSettings(null, 'MODE') === 'developer' ?
'http://localhost:4200' :
_url.format({
protocol: 'file:',
pathname: path.join(__dirname, 'dist', 'index.html'),
slashes: true
});
win.loadURL(url);
if (settingsManager.getSettings(null, 'STARTUP_WITH_DEVTOOLS') === true) {
win.webContents.openDevTools();
}
// win.on('closed', async () => {
// log.info('Tearing down UI');
// // TODO: stop other UI functionality: notifications, marketplace, etc
// });
mainWindow = win;
}
function createSystemTray() {
if (mainTrayInstance) {
return;
}
let trayIconFilename = 'icon.png';
if (process.platform === 'darwin') {
electron.Menu.setApplicationMenu(electron.Menu.buildFromTemplate([
{
label: app.getName(),
submenu: [
{ label: 'Quit', role: 'quit' }
]
},
{
label: 'Edit',
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
{ role: 'pasteandmatchstyle' },
{ role: 'delete' },
{ role: 'selectall' }
]
}
]));
trayIconFilename = 'iconTemplate.png';
} else {
electron.Menu.setApplicationMenu(null);
if (process.platform === 'win32') {
trayIconFilename = 'icon.ico';
}
}
mainTrayInstance = new electron.Tray(path.join(__dirname, 'resources', trayIconFilename));
mainTrayInstance.setToolTip(`${app.getName()} ${app.getVersion()}`);
mainTrayInstance.setContextMenu(electron.Menu.buildFromTemplate([
{
label: 'View',
submenu: [
{ label: 'Open Dev Tools', click() { mainWindow.webContents.openDevTools(); } }
]
},
{
role: 'window',
submenu: [
{ label: 'Close', click() { app.quit() } },
{ label: 'Hide', click() { mainWindow.hide(); } },
{ label: 'Show', click() { mainWindow.show(); } },
{ label: 'Maximize', click() { mainWindow.maximize(); } }
]
},
{
role: 'help',
submenu: [
{ label: 'About ' + app.getName(), click() { electron.shell.openExternal('https://particl.io/#about'); } },
{ label: 'Visit Particl.io', click() { electron.shell.openExternal('https://particl.io'); } },
{ label: 'Visit Electron', click() { electron.shell.openExternal('https://electron.atom.io'); } }
]
}
]));
}