refactor: implement clipboard APIs without the remote module (#17200)
This commit is contained in:
parent
43ef561d48
commit
3a091cdea4
10 changed files with 118 additions and 45 deletions
|
@ -4,8 +4,10 @@ const electron = require('electron')
|
|||
const { EventEmitter } = require('events')
|
||||
const fs = require('fs')
|
||||
const util = require('util')
|
||||
|
||||
const v8Util = process.atomBinding('v8_util')
|
||||
const eventBinding = process.atomBinding('event')
|
||||
const clipboard = process.atomBinding('clipboard')
|
||||
|
||||
const { isPromise } = electron
|
||||
|
||||
|
@ -16,6 +18,7 @@ const objectsRegistry = require('@electron/internal/browser/objects-registry')
|
|||
const guestViewManager = require('@electron/internal/browser/guest-view-manager')
|
||||
const bufferUtils = require('@electron/internal/common/buffer-utils')
|
||||
const errorUtils = require('@electron/internal/common/error-utils')
|
||||
const clipboardUtils = require('@electron/internal/common/clipboard-utils')
|
||||
|
||||
const hasProp = {}.hasOwnProperty
|
||||
|
||||
|
@ -488,12 +491,24 @@ ipcMainUtils.handle('ELECTRON_BROWSER_GET_LAST_WEB_PREFERENCES', function (event
|
|||
return event.sender.getLastWebPreferences()
|
||||
})
|
||||
|
||||
ipcMainUtils.handle('ELECTRON_BROWSER_CLIPBOARD_READ_FIND_TEXT', function (event) {
|
||||
return electron.clipboard.readFindText()
|
||||
})
|
||||
// Methods not listed in this set are called directly in the renderer process.
|
||||
const allowedClipboardMethods = (() => {
|
||||
switch (process.platform) {
|
||||
case 'darwin':
|
||||
return new Set(['readFindText', 'writeFindText'])
|
||||
case 'linux':
|
||||
return new Set(Object.keys(clipboard))
|
||||
default:
|
||||
return new Set()
|
||||
}
|
||||
})()
|
||||
|
||||
ipcMainUtils.handle('ELECTRON_BROWSER_CLIPBOARD_WRITE_FIND_TEXT', function (event, text) {
|
||||
return electron.clipboard.writeFindText(text)
|
||||
ipcMainUtils.handle('ELECTRON_BROWSER_CLIPBOARD', function (event, method, ...args) {
|
||||
if (!allowedClipboardMethods.has(method)) {
|
||||
throw new Error(`Invalid method: ${method}`)
|
||||
}
|
||||
|
||||
return clipboardUtils.serialize(electron.clipboard[method](...clipboardUtils.deserialize(args)))
|
||||
})
|
||||
|
||||
const readFile = util.promisify(fs.readFile)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue