asarfuckery/appasar/canary/app_bootstrap/bootstrap.js

140 lines
4 KiB
JavaScript
Raw Normal View History

2019-01-17 18:00:12 +00:00
'use strict';
// bootstrap, or what runs before the rest of desktop does
// responsible for handling updates and updating modules before continuing startup
if (process.platform === 'linux') {
// Some people are reporting audio problems on Linux that are fixed by setting
// an environment variable PULSE_LATENCY_MSEC=30 -- the "real" fix is to see
// what conditions require this and set this then (also to set it directly in
// our webrtc setup code rather than here) but this should fix the bug for now.
if (process.env.PULSE_LATENCY_MSEC === undefined) {
process.env.PULSE_LATENCY_MSEC = 30;
}
}
2019-01-18 17:51:44 +00:00
const { app, Menu } = require('electron');
2019-01-17 18:00:12 +00:00
2019-01-18 17:51:44 +00:00
const buildInfo = require('./buildInfo');
2019-01-17 18:00:12 +00:00
app.setVersion(buildInfo.version);
// expose releaseChannel to a global, since it's used by splash screen
global.releaseChannel = buildInfo.releaseChannel;
2019-01-18 17:51:44 +00:00
const errorHandler = require('./errorHandler');
2019-01-17 18:00:12 +00:00
errorHandler.init();
2019-01-18 17:51:44 +00:00
const paths = require('../common/paths');
2019-01-17 18:00:12 +00:00
paths.init(buildInfo);
global.modulePath = paths.getModulePath();
2019-01-18 17:51:44 +00:00
const appSettings = require('./appSettings');
2019-01-17 18:00:12 +00:00
appSettings.init();
2019-01-18 17:51:44 +00:00
const Constants = require('./Constants');
const GPUSettings = require('./GPUSettings');
2019-01-17 18:00:12 +00:00
2019-01-18 17:51:44 +00:00
const settings = appSettings.getSettings();
2019-01-17 18:00:12 +00:00
// TODO: this is a copy of gpuSettings.getEnableHardwareAcceleration
if (!settings.get('enableHardwareAcceleration', true)) {
app.disableHardwareAcceleration();
}
// [adill] disables color correction based on monitor's color profile
app.commandLine.appendSwitch('force-color-profile', 'srgb');
// [adill] work around chrome 66 disabling autoplay by default
app.commandLine.appendSwitch('autoplay-policy', 'no-user-gesture-required');
function hasArgvFlag(flag) {
return (process.argv || []).slice(1).includes(flag);
}
2019-01-18 17:51:44 +00:00
console.log(`${Constants.APP_NAME} ${app.getVersion()}`);
2019-01-17 18:00:12 +00:00
2019-01-18 17:51:44 +00:00
let preventStartup = false;
2019-01-17 18:00:12 +00:00
if (process.platform === 'win32') {
// this tells Windows (in particular Windows 10) which icon to associate your app with, important for correctly
// pinning app to task bar.
app.setAppUserModelId(Constants.APP_ID);
2019-01-18 17:51:44 +00:00
const { handleStartupEvent } = require('./squirrelUpdate');
2019-01-17 18:00:12 +00:00
// TODO: Isn't using argv[1] fragile?
2019-01-18 17:51:44 +00:00
const squirrelCommand = process.argv[1];
2019-01-17 18:00:12 +00:00
// TODO: Should `Discord` be a constant in this case? It's a protocol.
// TODO: Is protocol case sensitive?
if (handleStartupEvent('Discord', app, squirrelCommand)) {
preventStartup = true;
}
}
2019-01-18 17:51:44 +00:00
const singleInstance = require('./singleInstance');
const appUpdater = require('./appUpdater');
const moduleUpdater = require('../common/moduleUpdater');
const splashScreen = require('./splashScreen');
const autoStart = require('./autoStart');
const requireNative = require('./requireNative');
let coreModule;
2019-01-17 18:00:12 +00:00
function startUpdate() {
2019-01-18 17:51:44 +00:00
const startMinimized = hasArgvFlag('--start-minimized');
2019-01-17 18:00:12 +00:00
2019-01-18 17:51:44 +00:00
appUpdater.update(startMinimized, () => {
2019-01-17 18:00:12 +00:00
try {
coreModule = requireNative('discord_desktop_core');
coreModule.startup({
2019-01-18 17:51:44 +00:00
paths,
splashScreen,
moduleUpdater,
autoStart,
buildInfo,
appSettings,
Constants,
GPUSettings
2019-01-17 18:00:12 +00:00
});
} catch (err) {
return errorHandler.fatal(err);
}
2019-01-18 17:51:44 +00:00
}, () => {
2019-01-17 18:00:12 +00:00
coreModule.setMainWindowVisible(!startMinimized);
});
}
function startApp() {
paths.cleanOldVersions(buildInfo);
2019-01-18 17:51:44 +00:00
const startupMenu = require('./startupMenu');
2019-01-17 18:00:12 +00:00
Menu.setApplicationMenu(startupMenu);
2019-01-18 17:51:44 +00:00
const multiInstance = hasArgvFlag('--multi-instance');
2019-01-17 18:00:12 +00:00
if (multiInstance) {
startUpdate();
} else {
2019-01-18 17:51:44 +00:00
singleInstance.create(startUpdate, args => {
2019-01-17 18:00:12 +00:00
// TODO: isn't relying on index 0 awfully fragile?
if (args != null && args.length > 0 && args[0] === '--squirrel-uninstall') {
app.quit();
return;
}
if (coreModule) {
coreModule.handleSingleInstance(args);
} else {
appUpdater.focusSplash();
}
});
}
}
if (preventStartup) {
console.log('Startup prevented.');
// TODO: shouldn't we exit out?
} else {
console.log('Starting updater.');
if (app.isReady()) {
startApp();
} else {
app.once('ready', startApp);
}
}