chore: refactor browser IPC into TS and app API into TS (#16921)
* chore: refactor browser IPC into typescript * chore: refactor app.ts into Typescript * Refactors app.dock into cpp * Removes app.launcher which has not existed for 3 years * Removes 2 deprecated APIs (that have been deprecated for more than one major) * Refactors deprecate.ts as well
This commit is contained in:
parent
4ccd6d5900
commit
5790869a3f
16 changed files with 258 additions and 201 deletions
|
@ -1,113 +0,0 @@
|
|||
'use strict'
|
||||
|
||||
const bindings = process.atomBinding('app')
|
||||
const commandLine = process.atomBinding('command_line')
|
||||
const path = require('path')
|
||||
const { app, App } = bindings
|
||||
|
||||
// Only one app object permitted.
|
||||
module.exports = app
|
||||
|
||||
const electron = require('electron')
|
||||
const { deprecate, Menu } = electron
|
||||
const { EventEmitter } = require('events')
|
||||
|
||||
let dockMenu = null
|
||||
|
||||
// App is an EventEmitter.
|
||||
Object.setPrototypeOf(App.prototype, EventEmitter.prototype)
|
||||
EventEmitter.call(app)
|
||||
|
||||
Object.assign(app, {
|
||||
setApplicationMenu (menu) {
|
||||
return Menu.setApplicationMenu(menu)
|
||||
},
|
||||
getApplicationMenu () {
|
||||
return Menu.getApplicationMenu()
|
||||
},
|
||||
commandLine: {
|
||||
hasSwitch: (...args) => commandLine.hasSwitch(...args.map(String)),
|
||||
getSwitchValue: (...args) => commandLine.getSwitchValue(...args.map(String)),
|
||||
appendSwitch: (...args) => commandLine.appendSwitch(...args.map(String)),
|
||||
appendArgument: (...args) => commandLine.appendArgument(...args.map(String))
|
||||
},
|
||||
enableMixedSandbox () {
|
||||
deprecate.log(`'enableMixedSandbox' is deprecated. Mixed-sandbox mode is now enabled by default. You can safely remove the call to enableMixedSandbox().`)
|
||||
}
|
||||
})
|
||||
|
||||
app.getFileIcon = deprecate.promisify(app.getFileIcon)
|
||||
|
||||
const nativeAppMetrics = app.getAppMetrics
|
||||
app.getAppMetrics = () => {
|
||||
const metrics = nativeAppMetrics.call(app)
|
||||
for (const metric of metrics) {
|
||||
if ('memory' in metric) {
|
||||
deprecate.removeProperty(metric, 'memory')
|
||||
}
|
||||
}
|
||||
|
||||
return metrics
|
||||
}
|
||||
|
||||
app.isPackaged = (() => {
|
||||
const execFile = path.basename(process.execPath).toLowerCase()
|
||||
if (process.platform === 'win32') {
|
||||
return execFile !== 'electron.exe'
|
||||
}
|
||||
return execFile !== 'electron'
|
||||
})()
|
||||
|
||||
if (process.platform === 'darwin') {
|
||||
app.dock = {
|
||||
bounce (type = 'informational') {
|
||||
return bindings.dockBounce(type)
|
||||
},
|
||||
cancelBounce: bindings.dockCancelBounce,
|
||||
downloadFinished: bindings.dockDownloadFinished,
|
||||
setBadge: bindings.dockSetBadgeText,
|
||||
getBadge: bindings.dockGetBadgeText,
|
||||
hide: bindings.dockHide,
|
||||
show: bindings.dockShow,
|
||||
isVisible: bindings.dockIsVisible,
|
||||
setMenu (menu) {
|
||||
dockMenu = menu
|
||||
bindings.dockSetMenu(menu)
|
||||
},
|
||||
getMenu () {
|
||||
return dockMenu
|
||||
},
|
||||
setIcon: bindings.dockSetIcon
|
||||
}
|
||||
}
|
||||
|
||||
if (process.platform === 'linux') {
|
||||
app.launcher = {
|
||||
setBadgeCount: bindings.unityLauncherSetBadgeCount,
|
||||
getBadgeCount: bindings.unityLauncherGetBadgeCount,
|
||||
isCounterBadgeAvailable: bindings.unityLauncherAvailable,
|
||||
isUnityRunning: bindings.unityLauncherAvailable
|
||||
}
|
||||
}
|
||||
|
||||
app.allowNTLMCredentialsForAllDomains = function (allow) {
|
||||
deprecate.warn('app.allowNTLMCredentialsForAllDomains', 'session.allowNTLMCredentialsForDomains')
|
||||
const domains = allow ? '*' : ''
|
||||
if (!this.isReady()) {
|
||||
this.commandLine.appendSwitch('auth-server-whitelist', domains)
|
||||
} else {
|
||||
electron.session.defaultSession.allowNTLMCredentialsForDomains(domains)
|
||||
}
|
||||
}
|
||||
|
||||
// Routes the events to webContents.
|
||||
const events = ['login', 'certificate-error', 'select-client-certificate']
|
||||
for (const name of events) {
|
||||
app.on(name, (event, webContents, ...args) => {
|
||||
webContents.emit(name, event, ...args)
|
||||
})
|
||||
}
|
||||
|
||||
// Wrappers for native classes.
|
||||
const { DownloadItem } = process.atomBinding('download_item')
|
||||
Object.setPrototypeOf(DownloadItem.prototype, EventEmitter.prototype)
|
68
lib/browser/api/app.ts
Normal file
68
lib/browser/api/app.ts
Normal file
|
@ -0,0 +1,68 @@
|
|||
import * as path from 'path'
|
||||
|
||||
import * as electron from 'electron'
|
||||
import { EventEmitter } from 'events'
|
||||
|
||||
const bindings = process.atomBinding('app')
|
||||
const commandLine = process.atomBinding('command_line')
|
||||
const { app, App } = bindings
|
||||
|
||||
// Only one app object permitted.
|
||||
export default app
|
||||
|
||||
const { deprecate, Menu } = electron
|
||||
|
||||
let dockMenu: Electron.Menu | null = null
|
||||
|
||||
// App is an EventEmitter.
|
||||
Object.setPrototypeOf(App.prototype, EventEmitter.prototype)
|
||||
EventEmitter.call(app as any)
|
||||
|
||||
Object.assign(app, {
|
||||
setApplicationMenu (menu: Electron.Menu | null) {
|
||||
return Menu.setApplicationMenu(menu)
|
||||
},
|
||||
getApplicationMenu () {
|
||||
return Menu.getApplicationMenu()
|
||||
},
|
||||
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))
|
||||
} as Electron.CommandLine,
|
||||
enableMixedSandbox () {
|
||||
deprecate.log(`'enableMixedSandbox' is deprecated. Mixed-sandbox mode is now enabled by default. You can safely remove the call to enableMixedSandbox().`)
|
||||
}
|
||||
})
|
||||
|
||||
app.getFileIcon = deprecate.promisify(app.getFileIcon)
|
||||
|
||||
app.isPackaged = (() => {
|
||||
const execFile = path.basename(process.execPath).toLowerCase()
|
||||
if (process.platform === 'win32') {
|
||||
return execFile !== 'electron.exe'
|
||||
}
|
||||
return execFile !== 'electron'
|
||||
})()
|
||||
|
||||
if (process.platform === 'darwin') {
|
||||
const setDockMenu = app.dock.setMenu
|
||||
app.dock.setMenu = (menu) => {
|
||||
dockMenu = menu
|
||||
setDockMenu(menu)
|
||||
}
|
||||
app.dock.getMenu = () => dockMenu
|
||||
}
|
||||
|
||||
// Routes the events to webContents.
|
||||
const events = ['login', 'certificate-error', 'select-client-certificate']
|
||||
for (const name of events) {
|
||||
app.on(name as 'login', (event, webContents, ...args: any[]) => {
|
||||
webContents.emit(name, event, ...args)
|
||||
})
|
||||
}
|
||||
|
||||
// Wrappers for native classes.
|
||||
const { DownloadItem } = process.atomBinding('download_item')
|
||||
Object.setPrototypeOf(DownloadItem.prototype, EventEmitter.prototype)
|
|
@ -1,10 +1,8 @@
|
|||
'use strict'
|
||||
|
||||
const { EventEmitter } = require('events')
|
||||
import { EventEmitter } from 'events'
|
||||
|
||||
const emitter = new EventEmitter()
|
||||
|
||||
// Do not throw exception when channel name is "error".
|
||||
emitter.on('error', () => {})
|
||||
|
||||
module.exports = emitter
|
||||
export default emitter
|
Loading…
Add table
Add a link
Reference in a new issue