asarfuckery/appasar/stable/app_bootstrap/bootstrap.js

144 lines
4.3 KiB
JavaScript
Raw Normal View History

2019-01-17 18:22:05 +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-03-12 17:01:00 +00:00
const { app, Menu } = require('electron');
2019-01-17 18:22:05 +00:00
2019-03-12 17:01:00 +00:00
const buildInfo = require('./buildInfo');
2019-01-17 18:22:05 +00:00
app.setVersion(buildInfo.version);
// expose releaseChannel to a global, since it's used by splash screen
global.releaseChannel = buildInfo.releaseChannel;
2019-03-12 17:01:00 +00:00
const errorHandler = require('./errorHandler');
2019-01-17 18:22:05 +00:00
errorHandler.init();
2019-03-12 17:01:00 +00:00
const paths = require('../common/paths');
2019-01-17 18:22:05 +00:00
paths.init(buildInfo);
global.modulePath = paths.getModulePath();
2019-03-12 17:01:00 +00:00
const appSettings = require('./appSettings');
2019-01-17 18:22:05 +00:00
appSettings.init();
2019-03-12 17:01:00 +00:00
const Constants = require('./Constants');
const GPUSettings = require('./GPUSettings');
2019-01-17 18:22:05 +00:00
2020-02-25 18:24:06 +00:00
function setupHardwareAcceleration() {
const settings = appSettings.getSettings();
const electronMajor = parseInt(process.versions.electron.split('.')[0]);
const allowed = process.env.DISCORD_ENABLE_HARDWARE_ACCELERATION || buildInfo.releaseChannel === 'development' || !(electronMajor === 7 && process.platform === 'darwin');
// TODO: this is a copy of gpuSettings.getEnableHardwareAcceleration
if (!allowed || !settings.get('enableHardwareAcceleration', true)) {
app.disableHardwareAcceleration();
}
2019-01-17 18:22:05 +00:00
}
2020-02-25 18:24:06 +00:00
setupHardwareAcceleration();
2019-01-17 18:22:05 +00:00
// [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-03-12 17:01:00 +00:00
console.log(`${Constants.APP_NAME} ${app.getVersion()}`);
2019-01-17 18:22:05 +00:00
2019-03-12 17:01:00 +00:00
let preventStartup = false;
2019-01-17 18:22:05 +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-03-12 17:01:00 +00:00
const { handleStartupEvent } = require('./squirrelUpdate');
2019-01-17 18:22:05 +00:00
// TODO: Isn't using argv[1] fragile?
2019-03-12 17:01:00 +00:00
const squirrelCommand = process.argv[1];
2019-01-17 18:22:05 +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-03-12 17:01:00 +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:22:05 +00:00
function startUpdate() {
2019-03-12 17:01:00 +00:00
console.log('Starting updater.');
const startMinimized = hasArgvFlag('--start-minimized');
2019-01-17 18:22:05 +00:00
2019-03-12 17:01:00 +00:00
appUpdater.update(startMinimized, () => {
2019-01-17 18:22:05 +00:00
try {
coreModule = requireNative('discord_desktop_core');
coreModule.startup({
2019-03-12 17:01:00 +00:00
paths,
splashScreen,
moduleUpdater,
autoStart,
buildInfo,
appSettings,
Constants,
GPUSettings
2019-01-17 18:22:05 +00:00
});
} catch (err) {
return errorHandler.fatal(err);
}
2019-03-12 17:01:00 +00:00
}, () => {
2019-01-17 18:22:05 +00:00
coreModule.setMainWindowVisible(!startMinimized);
});
}
function startApp() {
2019-03-12 17:01:00 +00:00
console.log('Starting app.');
2019-01-17 18:22:05 +00:00
paths.cleanOldVersions(buildInfo);
2019-03-12 17:01:00 +00:00
const startupMenu = require('./startupMenu');
2019-01-17 18:22:05 +00:00
Menu.setApplicationMenu(startupMenu);
2019-03-12 17:01:00 +00:00
const multiInstance = hasArgvFlag('--multi-instance');
2019-01-17 18:22:05 +00:00
if (multiInstance) {
startUpdate();
} else {
2019-03-12 17:01:00 +00:00
singleInstance.create(startUpdate, args => {
2019-01-17 18:22:05 +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 {
if (app.isReady()) {
startApp();
} else {
app.once('ready', startApp);
}
}