forked from n-air-app/n-air-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
561 lines (456 loc) · 16.8 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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
'use strict';
////////////////////////////////////////////////////////////////////////////////
// Set Up Environment Variables
////////////////////////////////////////////////////////////////////////////////
const pjson = require('./package.json');
if (pjson.env === 'production') {
process.env.NODE_ENV = 'production';
}
if (pjson.name === 'n-air-app-unstable') {
process.env.NAIR_UNSTABLE = true;
}
if (process.env.NODE_ENV !== 'production' && process.env.NAIR_UNSTABLE) {
pjson.name = 'n-air-app-unstable';
}
if (pjson.name === 'n-air-app-preview') {
process.env.NAIR_PREVIEW = true;
}
if (pjson.name === 'n-air-app-ipc') {
process.env.NAIR_IPC = true;
}
process.env.NAIR_VERSION = pjson.version;
process.env.NAIR_PRODUCT_NAME = pjson.buildProductName;
if (!process.env.NAIR_LICENSE_API_KEY && pjson.getlicensenair_key) {
process.env.NAIR_LICENSE_API_KEY = pjson.getlicensenair_key;
}
////////////////////////////////////////////////////////////////////////////////
// Modules and other Requires
////////////////////////////////////////////////////////////////////////////////
const { app, BrowserWindow, ipcMain, session, crashReporter, dialog, webContents, shell } = require('electron');
const electron = require('electron');
const fs = require('fs');
const { Updater } = require('./updater/Updater.js');
const uuid = require('uuid/v4');
const rimraf = require('rimraf');
const path = require('path');
const windowStateKeeper = require('electron-window-state');
const { URL } = require('url');
const obs = require('obs-studio-node');
app.disableHardwareAcceleration();
////////////////////////////////////////////////////////////////////////////////
// Main Program
////////////////////////////////////////////////////////////////////////////////
function log(...args) {
if (!process.env.NAIR_DISABLE_MAIN_LOGGING) {
console.log(...args);
}
}
// We use a special cache directory for running tests
if (process.env.NAIR_CACHE_DIR) {
app.setPath('appData', process.env.NAIR_CACHE_DIR);
}
app.setPath('userData', path.join(app.getPath('appData'), pjson.name));
if (process.argv.includes('--clearCacheDir')) {
// __installer.exe は electron-updater 差分アップデートの比較元になるので消してはいけない
const rmPath = path.join(app.getPath('userData'), '!(__installer.exe)');
log('clear cache directory!: ', rmPath);
rimraf.sync(rmPath);
}
// Windows
let mainWindow;
let childWindow;
// Somewhat annoyingly, this is needed so that the child window
// can differentiate between a user closing it vs the app
// closing the windows before exit.
let allowMainWindowClose = false;
let shutdownStarted = false;
let appShutdownTimeout;
global.indexUrl = 'file://' + __dirname + '/index.html';
function openDevTools() {
childWindow.webContents.openDevTools({ mode: 'undocked' });
mainWindow.webContents.openDevTools({ mode: 'undocked' });
}
function startApp() {
const isDevMode = (process.env.NODE_ENV !== 'production') && (process.env.NODE_ENV !== 'test');
{ // Initialize obs-studio-server
// Set up environment variables for IPC.
process.env.NAIR_IPC_PATH = "nair-".concat(uuid());
process.env.NAIR_IPC_USERDATA = app.getPath('userData');
// Host a new IPC Server and connect to it.
obs.IPC.host(process.env.NAIR_IPC_PATH);
obs.NodeObs.SetWorkingDirectory(path.join(
app.getAppPath().replace('app.asar', 'app.asar.unpacked'),
'node_modules',
'obs-studio-node')
);
}
const Raven = require('raven-js');
function handleFinishedReport() {
dialog.showErrorBox(`予期せぬエラー`,
'予期しないエラーが発生したため、アプリケーションをシャットダウンします。ご不便をおかけして申し訳ありません。\n' +
'この件に関する情報はデバッグ目的で送信されました。不具合を解決するためにご協力いただきありがとうございます。');
if (app) {
app.quit();
}
}
if (pjson.env === 'production') {
const params = !!process.env.NAIR_UNSTABLE
? {project: '5372801', key: '819e76e51864453aafd28c6d0473881f'} // crash-reporter-unstable
: {project: '1520076', key: 'd965eea4b2254c2b9f38d2346fb8a472'}; // crash-reporter
Raven.config(`https://${params.key}@o170115.ingest.sentry.io/${params.project}`, {
release: process.env.NAIR_VERSION
}).install(function (err, initialErr, eventId) {
handleFinishedReport();
});
crashReporter.start({
productName: 'n-air-app',
companyName: 'n-air-app',
submitURL:
`https://o170115.ingest.sentry.io/api/${params.project}/minidump/` +
`?sentry_key=${params.key}`,
extra: {
version: pjson.version,
processType: 'main'
}
});
}
const mainWindowState = windowStateKeeper({
defaultWidth: 1600,
defaultHeight: 1000
});
const mainWindowIsVisible = electron.screen.getAllDisplays().some(display => (
display.workArea.x < mainWindowState.x + mainWindowState.width &&
mainWindowState.x < display.workArea.x + display.workArea.width &&
display.workArea.y < mainWindowState.y &&
mainWindowState.y < display.workArea.y + display.workArea.height
));
mainWindow = new BrowserWindow({
minWidth: 800,
minHeight: 600,
width: mainWindowState.width,
height: mainWindowState.height,
show: false,
frame: false,
title: process.env.NAIR_PRODUCT_NAME,
...(mainWindowIsVisible ? {
x: mainWindowState.x,
y: mainWindowState.y
} : {})
});
mainWindowState.manage(mainWindow);
mainWindow.setMenu(null);
// wait until devtools will be opened and load app into window
// it allows to start application with clean cache
// and handle breakpoints on startup
const LOAD_DELAY = 2000;
setTimeout(() => {
mainWindow.loadURL(`${global.indexUrl}?windowId=main`);
}, isDevMode ? LOAD_DELAY : 0);
mainWindow.on('close', e => {
if (!shutdownStarted) {
shutdownStarted = true;
childWindow.destroy();
mainWindow.send('shutdown');
// We give the main window 10 seconds to acknowledge a request
// to shut down. Otherwise, we just close it.
appShutdownTimeout = setTimeout(() => {
allowMainWindowClose = true;
if (!mainWindow.isDestroyed()) mainWindow.close();
}, 10 * 1000);
}
if (!allowMainWindowClose) e.preventDefault();
});
ipcMain.on('acknowledgeShutdown', () => {
if (appShutdownTimeout) clearTimeout(appShutdownTimeout);
});
ipcMain.on('shutdownComplete', () => {
allowMainWindowClose = true;
mainWindow.close();
});
// Initialize the keylistener
require('node-libuiohook').startHook();
mainWindow.on('closed', () => {
require('node-libuiohook').stopHook();
session.defaultSession.flushStorageData();
app.quit();
});
// Pre-initialize the child window
childWindow = new BrowserWindow({
parent: mainWindow,
minimizable: false,
show: false,
frame: false
});
childWindow.setMenu(null);
// The child window is never closed, it just hides in the
// background until it is needed.
childWindow.on('close', e => {
if (!shutdownStarted) {
childWindow.send('closeWindow');
// Prevent the window from actually closing
e.preventDefault();
}
});
// simple messaging system for services between windows
// WARNING! the child window use synchronous requests and will be frozen
// until main window asynchronous response
const requests = { };
function sendRequest(request, event = null) {
mainWindow.webContents.send('services-request', request);
if (!event) return;
requests[request.id] = Object.assign({}, request, { event });
}
// use this function to call some service method from the main process
function callService(resource, method, ...args) {
sendRequest({
jsonrpc: '2.0',
method,
params: {
resource,
args
}
});
}
ipcMain.on('services-ready', () => {
childWindow.loadURL(`${global.indexUrl}?windowId=child`);
});
ipcMain.on('services-request', (event, payload) => {
sendRequest(payload, event);
});
ipcMain.on('services-response', (event, response) => {
if (!requests[response.id]) return;
requests[response.id].event.returnValue = response;
delete requests[response.id];
});
ipcMain.on('services-message', (event, payload) => {
const windows = BrowserWindow.getAllWindows();
windows.forEach(window => {
if (window.id === mainWindow.id || window.isDestroyed()) return;
window.webContents.send('services-message', payload);
});
});
if (isDevMode) {
require('devtron').install();
// Vue dev tools appears to cause strange non-deterministic
// interference with certain NodeJS APIs, expecially asynchronous
// IO from the renderer process. Enable at your own risk.
// const devtoolsInstaller = require('electron-devtools-installer');
// devtoolsInstaller.default(devtoolsInstaller.VUEJS_DEVTOOLS);
// setTimeout(() => {
// openDevTools();
// }, 10 * 1000);
}
}
app.setAsDefaultProtocolClient('nair');
// This ensures that only one copy of our app can run at once.
const shouldQuit = app.makeSingleInstance(argv => {
// Check for protocol links in the argv of the other process
argv.forEach(arg => {
if (arg.match(/^nair:\/\//)) {
mainWindow.send('protocolLink', arg);
}
});
// Someone tried to run a second instance, we should focus our window.
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.focus();
}
});
if (shouldQuit) {
app.exit();
}
function copyFile(src, dest) {
if (!fs.existsSync(src)) {
log(`copyFile: ${src} not found!`);
return;
}
const stat = fs.statSync(src);
if (fs.existsSync(dest)) {
const cache = fs.statSync(dest);
if (stat.size === cache.size && stat.mtime === cache.mtime) {
log('copyFile: the same file exists. skip.');
return;
}
}
try {
fs.copyFileSync(src, dest);
fs.utimesSync(dest, stat.atime, stat.mtime);
} catch (e) {
log(`copyFile Error: ${e.name}: ${e.message}`);
}
}
app.on('ready', () => {
if ((process.env.NODE_ENV === 'production') || process.env.NAIR_FORCE_AUTO_UPDATE) {
// copy the original installer file so that it can be found for differential updating
const nsisInstallerFileName = '__installer.exe';
const installerPath = path.join(app.getPath('appData'), process.env.NAIR_PRODUCT_NAME, nsisInstallerFileName);
const cachePath = path.join(app.getPath('userData'), nsisInstallerFileName);
log(`copying ${installerPath} to ${cachePath}...`);
copyFile(installerPath, cachePath);
(new Updater(startApp)).run();
} else {
startApp();
}
});
app.on('quit', (e, exitCode) => {
obs.IPC.disconnect();
});
ipcMain.on('openDevTools', () => {
openDevTools();
});
ipcMain.on('window-showChildWindow', (event, windowOptions) => {
if (windowOptions.size.width && windowOptions.size.height) {
// Center the child window on the main window
// For some unknown reason, electron sometimes gets into a
// weird state where this will always fail. Instead, we
// should recover by simply setting the size and forgetting
// about the bounds.
try {
const mainWindowBounds = mainWindow.getBounds();
const mainWindowDisplay = electron.screen.getDisplayMatching(mainWindowBounds);
const targetWorkArea = mainWindowDisplay.workArea;
const width = Math.min(windowOptions.size.width, targetWorkArea.width);
const height = Math.min(windowOptions.size.height, targetWorkArea.height);
childWindow.show();
childWindow.restore();
childWindow.setMinimumSize(width, height);
if (windowOptions.center) {
const baseChildY = mainWindowBounds.y;
const overflowsBottom = Math.max(0, baseChildY + height - targetWorkArea.y - targetWorkArea.height);
const overflowsTop = Math.max(0, targetWorkArea.y - baseChildY);
const baseChildX = (mainWindowBounds.x + (mainWindowBounds.width / 2)) - (width / 2);
const overflowsRight = Math.max(0, baseChildX + width - targetWorkArea.x - targetWorkArea.width);
const overflowsLeft = Math.max(0, targetWorkArea.x - baseChildX);
const childX = baseChildX + overflowsLeft - overflowsRight;
const childY = baseChildY + overflowsTop - overflowsBottom;
childWindow.setBounds({
x: Math.floor(childX),
y: Math.floor(childY),
width,
height
});
}
} catch (err) {
log('Recovering from error:', err);
const workAreaSize = electron.screen.getPrimaryDisplay().workAreaSize;
const width = Math.min(windowOptions.size.width, workAreaSize.width);
const height = Math.min(windowOptions.size.height, workAreaSize.height);
childWindow.setMinimumSize(width, height);
childWindow.setSize(width, height);
childWindow.center();
}
childWindow.focus();
}
});
ipcMain.on('window-closeChildWindow', (event) => {
// never close the child window, hide it instead
childWindow.hide();
});
ipcMain.on('window-focusMain', () => {
mainWindow.focus();
});
function preventClose(e) {
if (!shutdownStarted) {
e.preventDefault();
}
}
ipcMain.on('window-preventClose', (event, id) => {
const window = BrowserWindow.fromId(id);
window.addListener('close', preventClose);
});
ipcMain.on('window-allowClose', (event, id) => {
const window = BrowserWindow.fromId(id);
window.removeListener('close', preventClose);
});
/**
* 番組作成・編集画面からログアウトを封じる処理
* rendererプロセスからは遷移前に止められないのでここに実装がある
* @see https://github.com/electron/electron/pull/11679#issuecomment-359180722
**/
function preventLogout(e, url) {
const urlObj = new URL(url);
const isLogout = (
/^https?:$/.test(urlObj.protocol) &&
/^live2?\.nicovideo\.jp$/.test(urlObj.hostname) &&
/^\/logout$/.test(urlObj.pathname)
);
if (isLogout) {
e.preventDefault();
}
}
ipcMain.on('window-preventLogout', (event, id) => {
const window = BrowserWindow.fromId(id);
window.webContents.on('will-navigate', preventLogout);
});
/**
* 新ウィンドウ表示は既定のブラウザで開かせる処理
* rendererプロセスからは処理を止められないのでここに実装がある
* @see https://github.com/electron/electron/pull/11679#issuecomment-359180722
**/
function preventNewWindow(e, url) {
e.preventDefault();
shell.openExternal(url);
}
ipcMain.on('window-preventNewWindow', (_event, id) => {
const window = BrowserWindow.fromId(id);
window.webContents.on('new-window', preventNewWindow);
});
// The main process acts as a hub for various windows
// syncing their vuex stores.
let registeredStores = {};
ipcMain.on('vuex-register', event => {
let win = BrowserWindow.fromWebContents(event.sender);
let windowId = win.id;
// Register can be received multiple times if the window is
// refreshed. We only want to register it once.
if (!registeredStores[windowId]) {
registeredStores[windowId] = win;
log('Registered vuex stores: ', Object.keys(registeredStores));
// Make sure we unregister is when it is closed
win.on('closed', () => {
delete registeredStores[windowId];
log('Registered vuex stores: ', Object.keys(registeredStores));
});
}
if (windowId !== mainWindow.id) {
// Tell the mainWindow to send its current store state
// to the newly registered window
mainWindow.webContents.send('vuex-sendState', windowId);
}
});
// Proxy vuex-mutation events to all other subscribed windows
ipcMain.on('vuex-mutation', (event, mutation) => {
const senderWindow = BrowserWindow.fromWebContents(event.sender);
if (senderWindow && !senderWindow.isDestroyed()) {
const windowId = senderWindow.id;
Object.keys(registeredStores).filter(id => id !== windowId.toString()).forEach(id => {
const win = registeredStores[id];
if (!win.isDestroyed()) win.webContents.send('vuex-mutation', mutation);
});
}
});
ipcMain.on('restartApp', () => {
// prevent unexpected cache clear
const args = process.argv.slice(1).filter(x => x !== '--clearCacheDir');
app.relaunch( {args} );
// Closing the main window starts the shut down sequence
mainWindow.close();
});
ipcMain.on('requestSourceAttributes', (e, names) => {
const sizes = require('obs-studio-node').getSourcesSize(names);
e.sender.send('notifySourceAttributes', sizes);
});
ipcMain.on('requestPerformanceStatistics', (e) => {
const stats = getObs().OBS_API_getPerformanceStatistics();
e.sender.send('notifyPerformanceStatistics', stats);
});
ipcMain.on('webContents-preventNavigation', (e, id) => {
webContents.fromId(id).on('will-navigate', e => {
e.preventDefault();
});
});
ipcMain.on('getMainWindowWebContentsId', e => {
e.returnValue = mainWindow.webContents.id;
});