fix: NativeImage serialization of <webview>.capturePage() result (#20825)
This commit is contained in:
parent
c0657a4ca7
commit
1d596f616d
12 changed files with 67 additions and 44 deletions
|
@ -4,13 +4,13 @@ const clipboard = process.electronBinding('clipboard')
|
|||
|
||||
if (process.type === 'renderer') {
|
||||
const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils')
|
||||
const clipboardUtils = require('@electron/internal/common/clipboard-utils')
|
||||
const typeUtils = require('@electron/internal/common/type-utils')
|
||||
|
||||
const makeRemoteMethod = function (method) {
|
||||
return (...args) => {
|
||||
args = clipboardUtils.serialize(args)
|
||||
args = typeUtils.serialize(args)
|
||||
const result = ipcRendererUtils.invokeSync('ELECTRON_BROWSER_CLIPBOARD', method, ...args)
|
||||
return clipboardUtils.deserialize(result)
|
||||
return typeUtils.deserialize(result)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,26 +0,0 @@
|
|||
export function isPromise (val: any) {
|
||||
return (
|
||||
val &&
|
||||
val.then &&
|
||||
val.then instanceof Function &&
|
||||
val.constructor &&
|
||||
val.constructor.reject &&
|
||||
val.constructor.reject instanceof Function &&
|
||||
val.constructor.resolve &&
|
||||
val.constructor.resolve instanceof Function
|
||||
)
|
||||
}
|
||||
|
||||
const serializableTypes = [
|
||||
Boolean,
|
||||
Number,
|
||||
String,
|
||||
Date,
|
||||
Error,
|
||||
RegExp,
|
||||
ArrayBuffer
|
||||
]
|
||||
|
||||
export function isSerializableObject (value: any) {
|
||||
return value === null || ArrayBuffer.isView(value) || serializableTypes.some(type => value instanceof type)
|
||||
}
|
|
@ -1,5 +1,32 @@
|
|||
const { nativeImage, NativeImage } = process.electronBinding('native_image')
|
||||
|
||||
export function isPromise (val: any) {
|
||||
return (
|
||||
val &&
|
||||
val.then &&
|
||||
val.then instanceof Function &&
|
||||
val.constructor &&
|
||||
val.constructor.reject &&
|
||||
val.constructor.reject instanceof Function &&
|
||||
val.constructor.resolve &&
|
||||
val.constructor.resolve instanceof Function
|
||||
)
|
||||
}
|
||||
|
||||
const serializableTypes = [
|
||||
Boolean,
|
||||
Number,
|
||||
String,
|
||||
Date,
|
||||
Error,
|
||||
RegExp,
|
||||
ArrayBuffer
|
||||
]
|
||||
|
||||
export function isSerializableObject (value: any) {
|
||||
return value === null || ArrayBuffer.isView(value) || serializableTypes.some(type => value instanceof type)
|
||||
}
|
||||
|
||||
const objectMap = function (source: Object, mapper: (value: any) => any) {
|
||||
const sourceEntries = Object.entries(source)
|
||||
const targetEntries = sourceEntries.map(([key, val]) => [key, mapper(val)])
|
||||
|
@ -15,7 +42,7 @@ export function serialize (value: any): any {
|
|||
}
|
||||
} else if (Array.isArray(value)) {
|
||||
return value.map(serialize)
|
||||
} else if (value instanceof Buffer) {
|
||||
} else if (isSerializableObject(value)) {
|
||||
return value
|
||||
} else if (value instanceof Object) {
|
||||
return objectMap(value, serialize)
|
||||
|
@ -29,7 +56,7 @@ export function deserialize (value: any): any {
|
|||
return nativeImage.createFromBitmap(value.buffer, value.size)
|
||||
} else if (Array.isArray(value)) {
|
||||
return value.map(deserialize)
|
||||
} else if (value instanceof Buffer) {
|
||||
} else if (isSerializableObject(value)) {
|
||||
return value
|
||||
} else if (value instanceof Object) {
|
||||
return objectMap(value, deserialize)
|
|
@ -52,7 +52,6 @@ export const syncMethods = new Set([
|
|||
|
||||
export const asyncMethods = new Set([
|
||||
'loadURL',
|
||||
'capturePage',
|
||||
'executeJavaScript',
|
||||
'insertCSS',
|
||||
'insertText',
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue