refactor: implement inspector APIs without the remote module (#16607)
This commit is contained in:
parent
392458b252
commit
7dc565fc2e
7 changed files with 205 additions and 96 deletions
121
lib/browser/chrome-devtools.js
Normal file
121
lib/browser/chrome-devtools.js
Normal file
|
@ -0,0 +1,121 @@
|
|||
'use strict'
|
||||
|
||||
const { dialog, Menu } = require('electron')
|
||||
const fs = require('fs')
|
||||
const url = require('url')
|
||||
|
||||
const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils')
|
||||
|
||||
const convertToMenuTemplate = function (event, items) {
|
||||
return items.map(function (item) {
|
||||
const transformed = item.type === 'subMenu' ? {
|
||||
type: 'submenu',
|
||||
label: item.label,
|
||||
enabled: item.enabled,
|
||||
submenu: convertToMenuTemplate(event, item.subItems)
|
||||
} : item.type === 'separator' ? {
|
||||
type: 'separator'
|
||||
} : item.type === 'checkbox' ? {
|
||||
type: 'checkbox',
|
||||
label: item.label,
|
||||
enabled: item.enabled,
|
||||
checked: item.checked
|
||||
} : {
|
||||
type: 'normal',
|
||||
label: item.label,
|
||||
enabled: item.enabled
|
||||
}
|
||||
|
||||
if (item.id != null) {
|
||||
transformed.click = function () {
|
||||
event._replyInternal('ELECTRON_INSPECTOR_CONTEXT_MENU_CLICK', item.id)
|
||||
}
|
||||
}
|
||||
|
||||
return transformed
|
||||
})
|
||||
}
|
||||
|
||||
const getEditMenuItems = function () {
|
||||
return [
|
||||
{ role: 'undo' },
|
||||
{ role: 'redo' },
|
||||
{ type: 'separator' },
|
||||
{ role: 'cut' },
|
||||
{ role: 'copy' },
|
||||
{ role: 'paste' },
|
||||
{ role: 'pasteAndMatchStyle' },
|
||||
{ role: 'delete' },
|
||||
{ role: 'selectAll' }
|
||||
]
|
||||
}
|
||||
|
||||
const isChromeDevTools = function (pageURL) {
|
||||
const { protocol } = url.parse(pageURL)
|
||||
return protocol === 'chrome-devtools:'
|
||||
}
|
||||
|
||||
const assertChromeDevTools = function (contents, api) {
|
||||
const pageURL = contents._getURL()
|
||||
if (!isChromeDevTools(pageURL)) {
|
||||
console.error(`Blocked ${pageURL} from calling ${api}`)
|
||||
throw new Error(`Blocked ${api}`)
|
||||
}
|
||||
}
|
||||
|
||||
ipcMainUtils.handle('ELECTRON_INSPECTOR_CONTEXT_MENU', function (event, items, isEditMenu) {
|
||||
assertChromeDevTools(event.sender, 'window.InspectorFrontendHost.showContextMenuAtPoint()')
|
||||
|
||||
const template = isEditMenu ? getEditMenuItems() : convertToMenuTemplate(event, items)
|
||||
const menu = Menu.buildFromTemplate(template)
|
||||
const window = event.sender.getOwnerBrowserWindow()
|
||||
|
||||
menu.once('menu-will-close', () => {
|
||||
setTimeout(() => {
|
||||
event._replyInternal('ELECTRON_INSPECTOR_CONTEXT_MENU_CLOSE')
|
||||
})
|
||||
})
|
||||
|
||||
menu.popup({ window })
|
||||
})
|
||||
|
||||
ipcMainUtils.handle('ELECTRON_INSPECTOR_SELECT_FILE', function (event) {
|
||||
return new Promise((resolve, reject) => {
|
||||
assertChromeDevTools(event.sender, 'window.UI.createFileSelectorElement()')
|
||||
|
||||
dialog.showOpenDialog({}, function (files) {
|
||||
if (files) {
|
||||
const path = files[0]
|
||||
fs.readFile(path, (error, data) => {
|
||||
if (error) {
|
||||
reject(error)
|
||||
} else {
|
||||
resolve([path, data])
|
||||
}
|
||||
})
|
||||
} else {
|
||||
resolve([])
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
ipcMainUtils.handleSync('ELECTRON_INSPECTOR_CONFIRM', function (event, message, title) {
|
||||
return new Promise((resolve, reject) => {
|
||||
assertChromeDevTools(event.sender, 'window.confirm()')
|
||||
|
||||
if (message == null) message = ''
|
||||
if (title == null) title = ''
|
||||
|
||||
const options = {
|
||||
message: `${message}`,
|
||||
title: `${title}`,
|
||||
buttons: ['OK', 'Cancel'],
|
||||
cancelId: 1
|
||||
}
|
||||
const window = event.sender.getOwnerBrowserWindow()
|
||||
dialog.showMessageBox(window, options, (response) => {
|
||||
resolve(response === 0)
|
||||
})
|
||||
})
|
||||
})
|
|
@ -149,6 +149,9 @@ app.setPath('userData', path.join(app.getPath('appData'), app.getName()))
|
|||
app.setPath('userCache', path.join(app.getPath('cache'), app.getName()))
|
||||
app.setAppPath(packagePath)
|
||||
|
||||
// Load the chrome devtools support.
|
||||
require('@electron/internal/browser/chrome-devtools')
|
||||
|
||||
// Load the chrome extension support.
|
||||
require('@electron/internal/browser/chrome-extension')
|
||||
|
||||
|
|
29
lib/browser/ipc-main-internal-utils.js
Normal file
29
lib/browser/ipc-main-internal-utils.js
Normal file
|
@ -0,0 +1,29 @@
|
|||
'use strict'
|
||||
|
||||
const ipcMain = require('@electron/internal/browser/ipc-main-internal')
|
||||
const errorUtils = require('@electron/internal/common/error-utils')
|
||||
|
||||
const callHandler = async function (handler, event, args, reply) {
|
||||
try {
|
||||
const result = await handler(event, ...args)
|
||||
reply([null, result])
|
||||
} catch (error) {
|
||||
reply([errorUtils.serialize(error)])
|
||||
}
|
||||
}
|
||||
|
||||
exports.handle = function (channel, handler) {
|
||||
ipcMain.on(channel, (event, requestId, ...args) => {
|
||||
callHandler(handler, event, args, responseArgs => {
|
||||
event._replyInternal(`${channel}_RESPONSE_${requestId}`, ...responseArgs)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
exports.handleSync = function (channel, handler) {
|
||||
ipcMain.on(channel, (event, ...args) => {
|
||||
callHandler(handler, event, args, responseArgs => {
|
||||
event.returnValue = responseArgs
|
||||
})
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue