2019-07-23 20:41:59 +00:00
|
|
|
import * as fs from 'fs';
|
2019-02-14 22:29:20 +00:00
|
|
|
|
2021-04-21 20:59:11 +00:00
|
|
|
import { Menu } from 'electron/main';
|
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-07-28 23:43:43 +00:00
|
|
|
const { app } = bindings;
|
2019-02-14 22:29:20 +00:00
|
|
|
|
|
|
|
// Only one app object permitted.
|
|
|
|
export default app;
|
|
|
|
|
|
|
|
let dockMenu: Electron.Menu | null = null;
|
|
|
|
|
2020-03-18 17:06:41 +00:00
|
|
|
// Properties.
|
|
|
|
|
|
|
|
const nativeASGetter = app.isAccessibilitySupportEnabled;
|
|
|
|
const nativeASSetter = app.setAccessibilitySupportEnabled;
|
2020-07-28 23:43:43 +00:00
|
|
|
Object.defineProperty(app, 'accessibilitySupportEnabled', {
|
2020-03-18 17:06:41 +00:00
|
|
|
get: () => nativeASGetter.call(app),
|
|
|
|
set: (enabled) => nativeASSetter.call(app, enabled)
|
|
|
|
});
|
|
|
|
|
|
|
|
const nativeBCGetter = app.getBadgeCount;
|
|
|
|
const nativeBCSetter = app.setBadgeCount;
|
2020-07-28 23:43:43 +00:00
|
|
|
Object.defineProperty(app, 'badgeCount', {
|
2020-03-18 17:06:41 +00:00
|
|
|
get: () => nativeBCGetter.call(app),
|
|
|
|
set: (count) => nativeBCSetter.call(app, count)
|
|
|
|
});
|
|
|
|
|
|
|
|
const nativeNGetter = app.getName;
|
|
|
|
const nativeNSetter = app.setName;
|
2020-07-28 23:43:43 +00:00
|
|
|
Object.defineProperty(app, 'name', {
|
2020-03-18 17:06:41 +00:00
|
|
|
get: () => nativeNGetter.call(app),
|
|
|
|
set: (name) => nativeNSetter.call(app, name)
|
|
|
|
});
|
|
|
|
|
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)),
|
2021-10-06 20:45:58 +00:00
|
|
|
appendArgument: (arg: string) => commandLine.appendArgument(String(arg)),
|
|
|
|
removeSwitch: (theSwitch: string) => commandLine.removeSwitch(String(theSwitch))
|
2019-04-24 04:07:40 +00:00
|
|
|
} as Electron.CommandLine
|
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 () {
|
|
|
|
return Menu.getApplicationMenu();
|
|
|
|
},
|
|
|
|
set (menu: Electron.Menu | null) {
|
|
|
|
return Menu.setApplicationMenu(menu);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-29 01:41:03 +00:00
|
|
|
// The native implementation is not provided on non-windows platforms
|
|
|
|
app.setAppUserModelId = app.setAppUserModelId || (() => {});
|
|
|
|
|
2019-02-14 22:29:20 +00:00
|
|
|
if (process.platform === 'darwin') {
|
2019-05-06 15:29:01 +00:00
|
|
|
const setDockMenu = app.dock!.setMenu;
|
|
|
|
app.dock!.setMenu = (menu) => {
|
2019-02-14 22:29:20 +00:00
|
|
|
dockMenu = menu;
|
|
|
|
setDockMenu(menu);
|
|
|
|
};
|
2019-05-06 15:29:01 +00:00
|
|
|
app.dock!.getMenu = () => dockMenu;
|
2019-02-14 22:29:20 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 20:41:59 +00:00
|
|
|
if (process.platform === 'linux') {
|
|
|
|
const patternVmRSS = /^VmRSS:\s*(\d+) kB$/m;
|
|
|
|
const patternVmHWM = /^VmHWM:\s*(\d+) kB$/m;
|
|
|
|
|
|
|
|
const getStatus = (pid: number) => {
|
|
|
|
try {
|
|
|
|
return fs.readFileSync(`/proc/${pid}/status`, 'utf8');
|
|
|
|
} catch {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const getEntry = (file: string, pattern: RegExp) => {
|
|
|
|
const match = file.match(pattern);
|
|
|
|
return match ? parseInt(match[1], 10) : 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getProcessMemoryInfo = (pid: number) => {
|
|
|
|
const file = getStatus(pid);
|
|
|
|
|
|
|
|
return {
|
|
|
|
workingSetSize: getEntry(file, patternVmRSS),
|
|
|
|
peakWorkingSetSize: getEntry(file, patternVmHWM)
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const nativeFn = app.getAppMetrics;
|
|
|
|
app.getAppMetrics = () => {
|
|
|
|
const metrics = nativeFn.call(app);
|
|
|
|
for (const metric of metrics) {
|
|
|
|
metric.memory = getProcessMemoryInfo(metric.pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
return metrics;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:29:20 +00:00
|
|
|
// Routes the events to webContents.
|
2019-11-11 17:47:01 +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[]) => {
|
2019-02-14 22:29:20 +00:00
|
|
|
webContents.emit(name, event, ...args);
|
|
|
|
});
|
|
|
|
}
|