2023-03-13 17:30:13 +08:00
|
|
|
//----------------------------------------
|
|
|
|
//
|
|
|
|
// Copyright © yanghy. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under Apache License Version 2.0, January 2004
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
//----------------------------------------
|
|
|
|
|
|
|
|
package cef
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/energye/energy/common"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2023-03-22 11:46:21 +08:00
|
|
|
// ipc bind event name
|
2023-03-13 17:30:13 +08:00
|
|
|
const (
|
2023-03-15 14:29:45 +08:00
|
|
|
internalIPCKey = "ipc" // JavaScript -> ipc 事件驱动, 根对象名
|
|
|
|
internalEmit = "emit" // JavaScript -> ipc.emit 在 JavaScript 触发 GO 监听事件函数名, 异步
|
|
|
|
internalEmitSync = "emitSync" // JavaScript -> ipc.emitSync 在 JavaScript 触发 GO 监听事件函数名, 同步
|
|
|
|
internalOn = "on" // JavaScript -> ipc.on 在 JavaScript 监听事件, 提供给 GO 调用
|
2023-03-13 17:30:13 +08:00
|
|
|
)
|
2023-03-22 11:46:21 +08:00
|
|
|
|
|
|
|
// ipc message name
|
2023-03-13 17:30:13 +08:00
|
|
|
const (
|
2023-03-22 11:46:21 +08:00
|
|
|
internalIPCJSExecuteGoEvent = "JSEmitGo" // JS 触发 GO事件异步
|
|
|
|
internalIPCJSExecuteGoEventReplay = "JSEmitGoReplay" // JS 触发 GO事件异步 - 返回结果
|
|
|
|
internalIPCJSExecuteGoSyncEvent = "JSEmitSyncGo" // JS 触发 GO事件同步
|
|
|
|
internalIPCJSExecuteGoSyncEventReplay = "JSEmitSyncGoReplay" // JS 触发 GO事件同步 - 返回结果
|
|
|
|
internalIPCGoExecuteJSEvent = "GoEmitJS" // GO 触发 JS事件
|
|
|
|
internalIPCGoExecuteJSEventReplay = "GoEmitJSReplay" // GO 触发 JS事件 - 返回结果
|
2023-03-13 17:30:13 +08:00
|
|
|
)
|
|
|
|
|
2023-03-22 11:46:21 +08:00
|
|
|
// ipc process message key
|
2023-03-16 22:58:31 +08:00
|
|
|
const (
|
|
|
|
ipc_id = "id"
|
|
|
|
ipc_event = "event"
|
|
|
|
ipc_argumentList = "argumentList"
|
|
|
|
)
|
|
|
|
|
2023-03-13 17:30:13 +08:00
|
|
|
var (
|
|
|
|
internalObjectRootName = "energy" // GO 和 V8Value 绑定根对象名
|
2023-03-20 10:09:54 +08:00
|
|
|
ipcRender *ipcRenderProcess // 渲染进程 IPC
|
|
|
|
ipcBrowser *ipcBrowserProcess // 主进程 IPC
|
2023-03-13 17:30:13 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// ipcEmitHandler
|
|
|
|
type ipcEmitHandler struct {
|
|
|
|
handler *ICefV8Handler // ipc.emit handler
|
2023-03-22 11:54:15 +08:00
|
|
|
handlerSync *ICefV8Handler // ipc.emitSync handler
|
2023-03-13 17:30:13 +08:00
|
|
|
callbackList map[int32]*ipcCallback // ipc.emit callbackList *list.List
|
|
|
|
callbackMessageId int32 // ipc.emit messageId
|
|
|
|
callbackLock sync.Mutex // ipc.emit lock
|
|
|
|
}
|
|
|
|
|
|
|
|
// ipcOnHandler
|
|
|
|
type ipcOnHandler struct {
|
|
|
|
handler *ICefV8Handler // ipc.on handler
|
|
|
|
callbackList map[string]*ipcCallback // ipc.on callbackList
|
|
|
|
callbackMessageId int32 // ipc.on messageId
|
|
|
|
callbackLock sync.Mutex // ipc.emit lock
|
|
|
|
}
|
|
|
|
|
|
|
|
// ipcCallback
|
|
|
|
type ipcCallback struct {
|
2023-03-21 13:18:44 +08:00
|
|
|
function *ICefV8Value
|
2023-03-13 17:30:13 +08:00
|
|
|
}
|
|
|
|
|
2023-03-22 11:46:21 +08:00
|
|
|
// isIPCInternalKey IPC 内部 key 不允许使用
|
|
|
|
func isIPCInternalKey(key string) bool {
|
|
|
|
return key == internalIPCKey || key == internalEmit || key == internalOn || key == internalEmitSync ||
|
|
|
|
key == internalIPCJSExecuteGoEvent || key == internalIPCJSExecuteGoEventReplay ||
|
2023-03-22 11:54:15 +08:00
|
|
|
key == internalIPCGoExecuteJSEvent || key == internalIPCGoExecuteJSEventReplay ||
|
|
|
|
key == internalIPCJSExecuteGoSyncEvent || key == internalIPCJSExecuteGoSyncEventReplay
|
2023-03-22 11:46:21 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-13 17:43:22 +08:00
|
|
|
// ipcInit 初始化
|
|
|
|
func ipcInit() {
|
2023-03-13 17:54:22 +08:00
|
|
|
isSingleProcess := application.SingleProcess()
|
|
|
|
if isSingleProcess {
|
2023-03-14 17:07:15 +08:00
|
|
|
ipcBrowser = &ipcBrowserProcess{emitHandler: &ipcEmitHandler{callbackList: make(map[int32]*ipcCallback)}}
|
2023-03-13 17:43:22 +08:00
|
|
|
ipcRender = &ipcRenderProcess{
|
2023-03-13 22:40:20 +08:00
|
|
|
emitHandler: &ipcEmitHandler{callbackList: make(map[int32]*ipcCallback)},
|
|
|
|
onHandler: &ipcOnHandler{callbackList: make(map[string]*ipcCallback)},
|
2023-03-13 17:43:22 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if common.Args.IsMain() {
|
|
|
|
ipcBrowser = &ipcBrowserProcess{}
|
|
|
|
} else if common.Args.IsRender() {
|
|
|
|
ipcRender = &ipcRenderProcess{
|
2023-03-13 22:40:20 +08:00
|
|
|
emitHandler: &ipcEmitHandler{callbackList: make(map[int32]*ipcCallback)},
|
|
|
|
onHandler: &ipcOnHandler{callbackList: make(map[string]*ipcCallback)},
|
2023-03-13 17:43:22 +08:00
|
|
|
}
|
|
|
|
}
|
2023-03-13 17:30:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// addCallback
|
|
|
|
func (m *ipcEmitHandler) addCallback(callback *ipcCallback) int32 {
|
|
|
|
//return uintptr(unsafe.Pointer(m.callbackList.PushBack(callback)))
|
|
|
|
m.callbackLock.Lock()
|
|
|
|
defer m.callbackLock.Unlock()
|
|
|
|
if m.callbackMessageId == -1 {
|
|
|
|
m.callbackMessageId = 1
|
|
|
|
} else {
|
|
|
|
m.callbackMessageId++
|
|
|
|
}
|
|
|
|
m.callbackList[m.callbackMessageId] = callback
|
|
|
|
return m.callbackMessageId
|
|
|
|
}
|
|
|
|
|
|
|
|
// getCallback
|
|
|
|
func (m *ipcEmitHandler) getCallback(messageId int32) *ipcCallback {
|
|
|
|
//return (*list.Element)(unsafe.Pointer(ptr)).Value.(*ipcCallback)
|
|
|
|
m.callbackLock.Lock()
|
|
|
|
defer m.callbackLock.Unlock()
|
2023-03-15 14:29:45 +08:00
|
|
|
if callback, ok := m.callbackList[messageId]; ok {
|
|
|
|
delete(m.callbackList, messageId)
|
|
|
|
return callback
|
|
|
|
}
|
|
|
|
return nil
|
2023-03-13 17:30:13 +08:00
|
|
|
}
|
|
|
|
|
2023-03-13 22:40:20 +08:00
|
|
|
func (m *ipcEmitHandler) clear() {
|
|
|
|
for _, v := range m.callbackList {
|
|
|
|
v.free()
|
|
|
|
}
|
|
|
|
m.callbackList = make(map[int32]*ipcCallback)
|
|
|
|
}
|
|
|
|
|
2023-03-13 17:30:13 +08:00
|
|
|
// addCallback
|
|
|
|
func (m *ipcOnHandler) addCallback(eventName string, callback *ipcCallback) int32 {
|
|
|
|
//return uintptr(unsafe.Pointer(m.callbackList.PushBack(callback)))
|
|
|
|
m.callbackLock.Lock()
|
|
|
|
defer m.callbackLock.Unlock()
|
|
|
|
m.callbackList[eventName] = callback
|
|
|
|
return m.callbackMessageId
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeCallback
|
|
|
|
func (m *ipcOnHandler) removeCallback(eventName string) {
|
|
|
|
//m.callbackList.Remove((*list.Element)(unsafe.Pointer(ptr)))
|
|
|
|
m.callbackLock.Lock()
|
|
|
|
defer m.callbackLock.Unlock()
|
|
|
|
delete(m.callbackList, eventName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getCallback
|
|
|
|
func (m *ipcOnHandler) getCallback(eventName string) *ipcCallback {
|
|
|
|
//return (*list.Element)(unsafe.Pointer(ptr)).Value.(*ipcCallback)
|
|
|
|
m.callbackLock.Lock()
|
|
|
|
defer m.callbackLock.Unlock()
|
|
|
|
return m.callbackList[eventName]
|
|
|
|
}
|
|
|
|
|
2023-03-13 22:40:20 +08:00
|
|
|
func (m *ipcOnHandler) clear() {
|
|
|
|
for _, v := range m.callbackList {
|
|
|
|
v.free()
|
|
|
|
}
|
|
|
|
m.callbackList = make(map[string]*ipcCallback)
|
|
|
|
}
|
|
|
|
|
2023-03-13 17:30:13 +08:00
|
|
|
func (m *ipcCallback) free() {
|
|
|
|
if m.function != nil {
|
|
|
|
m.function.Free()
|
|
|
|
m.function = nil
|
|
|
|
}
|
|
|
|
}
|