2020-03-20 20:28:31 +00:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
2019-02-14 22:29:20 +00:00
|
|
|
|
2020-07-13 16:58:49 +00:00
|
|
|
import { deprecate, Menu } from 'electron/main';
|
2020-03-20 20:28:31 +00:00
|
|
|
import { EventEmitter } from 'events';
|
2019-02-14 22:29:20 +00:00
|
|
|
|
2020-06-23 03:32:45 +00:00
|
|
|
const bindings = process._linkedBinding('electron_browser_app');
|
|
|
|
const commandLine = process._linkedBinding('electron_common_command_line');
|
2020-03-20 20:28:31 +00:00
|
|
|
const { app, App } = bindings;
|
2019-02-14 22:29:20 +00:00
|
|
|
|
|
|
|
// Only one app object permitted.
|
2020-03-20 20:28:31 +00:00
|
|
|
export default app;
|
2019-02-14 22:29:20 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
let dockMenu: Electron.Menu | null = null;
|
2019-02-14 22:29:20 +00:00
|
|
|
|
|
|
|
// App is an EventEmitter.
|
2020-03-20 20:28:31 +00:00
|
|
|
Object.setPrototypeOf(App.prototype, EventEmitter.prototype);
|
|
|
|
EventEmitter.call(app as any);
|
2019-02-14 22:29:20 +00:00
|
|
|
|
2020-03-18 17:06:41 +00:00
|
|
|
// Properties.
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const nativeASGetter = app.isAccessibilitySupportEnabled;
|
|
|
|
const nativeASSetter = app.setAccessibilitySupportEnabled;
|
2020-03-18 17:06:41 +00:00
|
|
|
Object.defineProperty(App.prototype, 'accessibilitySupportEnabled', {
|
|
|
|
get: () => nativeASGetter.call(app),
|
|
|
|
set: (enabled) => nativeASSetter.call(app, enabled)
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-03-18 17:06:41 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const nativeBCGetter = app.getBadgeCount;
|
|
|
|
const nativeBCSetter = app.setBadgeCount;
|
2020-03-18 17:06:41 +00:00
|
|
|
Object.defineProperty(App.prototype, 'badgeCount', {
|
|
|
|
get: () => nativeBCGetter.call(app),
|
|
|
|
set: (count) => nativeBCSetter.call(app, count)
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-03-18 17:06:41 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const nativeNGetter = app.getName;
|
|
|
|
const nativeNSetter = app.setName;
|
2020-03-18 17:06:41 +00:00
|
|
|
Object.defineProperty(App.prototype, 'name', {
|
|
|
|
get: () => nativeNGetter.call(app),
|
|
|
|
set: (name) => nativeNSetter.call(app, name)
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-03-18 17:06:41 +00:00
|
|
|
|
2019-02-14 22:29:20 +00:00
|
|
|
Object.assign(app, {
|
|
|
|
commandLine: {
|
|
|
|
hasSwitch: (theSwitch: string) => commandLine.hasSwitch(String(theSwitch)),
|
|
|
|
getSwitchValue: (theSwitch: string) => commandLine.getSwitchValue(String(theSwitch)),
|
|
|
|
appendSwitch: (theSwitch: string, value?: string) => commandLine.appendSwitch(String(theSwitch), typeof value === 'undefined' ? value : String(value)),
|
|
|
|
appendArgument: (arg: string) => commandLine.appendArgument(String(arg))
|
2019-04-24 04:07:40 +00:00
|
|
|
} as Electron.CommandLine
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-02-14 22:29:20 +00:00
|
|
|
|
2019-04-02 20:36:57 +00:00
|
|
|
// we define this here because it'd be overly complicated to
|
|
|
|
// do in native land
|
|
|
|
Object.defineProperty(app, 'applicationMenu', {
|
|
|
|
get () {
|
2020-03-20 20:28:31 +00:00
|
|
|
return Menu.getApplicationMenu();
|
2019-04-02 20:36:57 +00:00
|
|
|
},
|
|
|
|
set (menu: Electron.Menu | null) {
|
2020-03-20 20:28:31 +00:00
|
|
|
return Menu.setApplicationMenu(menu);
|
2019-04-02 20:36:57 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-04-02 20:36:57 +00:00
|
|
|
|
2019-07-26 23:12:59 +00:00
|
|
|
App.prototype.isPackaged = (() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const execFile = path.basename(process.execPath).toLowerCase();
|
2019-02-14 22:29:20 +00:00
|
|
|
if (process.platform === 'win32') {
|
2020-03-20 20:28:31 +00:00
|
|
|
return execFile !== 'electron.exe';
|
2019-02-14 22:29:20 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
return execFile !== 'electron';
|
|
|
|
})();
|
2019-02-14 22:29:20 +00:00
|
|
|
|
2019-04-16 18:22:51 +00:00
|
|
|
app._setDefaultAppPaths = (packagePath) => {
|
|
|
|
// Set the user path according to application's name.
|
2020-03-20 20:28:31 +00:00
|
|
|
app.setPath('userData', path.join(app.getPath('appData'), app.name!));
|
|
|
|
app.setPath('userCache', path.join(app.getPath('cache'), app.name!));
|
|
|
|
app.setAppPath(packagePath);
|
2019-04-16 18:22:51 +00:00
|
|
|
|
|
|
|
// Add support for --user-data-dir=
|
2019-06-13 23:03:02 +00:00
|
|
|
if (app.commandLine.hasSwitch('user-data-dir')) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const userDataDir = app.commandLine.getSwitchValue('user-data-dir');
|
|
|
|
if (path.isAbsolute(userDataDir)) app.setPath('userData', userDataDir);
|
2019-04-16 18:22:51 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2019-04-16 18:22:51 +00:00
|
|
|
|
2019-02-14 22:29:20 +00:00
|
|
|
if (process.platform === 'darwin') {
|
2020-03-20 20:28:31 +00:00
|
|
|
const setDockMenu = app.dock!.setMenu;
|
2019-05-06 15:29:01 +00:00
|
|
|
app.dock!.setMenu = (menu) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
dockMenu = menu;
|
|
|
|
setDockMenu(menu);
|
|
|
|
};
|
|
|
|
app.dock!.getMenu = () => dockMenu;
|
2019-02-14 22:29:20 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 20:41:59 +00:00
|
|
|
if (process.platform === 'linux') {
|
2020-03-20 20:28:31 +00:00
|
|
|
const patternVmRSS = /^VmRSS:\s*(\d+) kB$/m;
|
|
|
|
const patternVmHWM = /^VmHWM:\s*(\d+) kB$/m;
|
2019-07-23 20:41:59 +00:00
|
|
|
|
|
|
|
const getStatus = (pid: number) => {
|
|
|
|
try {
|
2020-03-20 20:28:31 +00:00
|
|
|
return fs.readFileSync(`/proc/${pid}/status`, 'utf8');
|
2019-07-23 20:41:59 +00:00
|
|
|
} catch {
|
2020-03-20 20:28:31 +00:00
|
|
|
return '';
|
2019-07-23 20:41:59 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2019-07-23 20:41:59 +00:00
|
|
|
|
|
|
|
const getEntry = (file: string, pattern: RegExp) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const match = file.match(pattern);
|
|
|
|
return match ? parseInt(match[1], 10) : 0;
|
|
|
|
};
|
2019-07-23 20:41:59 +00:00
|
|
|
|
|
|
|
const getProcessMemoryInfo = (pid: number) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const file = getStatus(pid);
|
2019-07-23 20:41:59 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
workingSetSize: getEntry(file, patternVmRSS),
|
|
|
|
peakWorkingSetSize: getEntry(file, patternVmHWM)
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
|
|
|
};
|
2019-07-23 20:41:59 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const nativeFn = app.getAppMetrics;
|
2019-07-23 20:41:59 +00:00
|
|
|
app.getAppMetrics = () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const metrics = nativeFn.call(app);
|
2019-07-23 20:41:59 +00:00
|
|
|
for (const metric of metrics) {
|
2020-03-20 20:28:31 +00:00
|
|
|
metric.memory = getProcessMemoryInfo(metric.pid);
|
2019-07-23 20:41:59 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
return metrics;
|
|
|
|
};
|
2019-07-23 20:41:59 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 22:29:20 +00:00
|
|
|
// Routes the events to webContents.
|
2020-03-20 20:28:31 +00:00
|
|
|
const events = ['certificate-error', 'select-client-certificate'];
|
2019-02-14 22:29:20 +00:00
|
|
|
for (const name of events) {
|
2019-11-11 17:47:01 +00:00
|
|
|
app.on(name as 'certificate-error', (event, webContents, ...args: any[]) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
webContents.emit(name, event, ...args);
|
|
|
|
});
|
2019-02-14 22:29:20 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 22:03:52 +00:00
|
|
|
// Deprecate allowRendererProcessReuse but only if they set it to false, no need to log if
|
|
|
|
// they are setting it to true
|
2020-03-20 20:28:31 +00:00
|
|
|
deprecate.removeProperty(app, 'allowRendererProcessReuse', [false]);
|