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
|
|
|
|
//
|
|
|
|
//----------------------------------------
|
|
|
|
|
|
|
|
// energy 主进程 IPC
|
2023-05-31 17:41:14 +08:00
|
|
|
|
2023-03-13 17:30:13 +08:00
|
|
|
package cef
|
|
|
|
|
|
|
|
import (
|
2023-05-31 18:00:34 +08:00
|
|
|
"github.com/energye/energy/v2/cef/internal/ipc"
|
|
|
|
ipcArgument "github.com/energye/energy/v2/cef/ipc/argument"
|
|
|
|
"github.com/energye/energy/v2/cef/ipc/context"
|
|
|
|
"github.com/energye/energy/v2/consts"
|
|
|
|
"github.com/energye/energy/v2/pkgs/json"
|
2023-03-13 17:30:13 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// ipcBrowserProcess 主进程
|
|
|
|
type ipcBrowserProcess struct {
|
2023-03-23 09:16:27 +08:00
|
|
|
}
|
2023-03-13 17:30:13 +08:00
|
|
|
|
2023-03-14 18:24:18 +08:00
|
|
|
// ipcGoExecuteMethodMessage 执行 Go 监听函数
|
2023-03-21 13:18:44 +08:00
|
|
|
func (m *ipcBrowserProcess) jsExecuteGoMethodMessage(browser *ICefBrowser, frame *ICefFrame, message *ICefProcessMessage) (result bool) {
|
2023-05-31 17:41:14 +08:00
|
|
|
result = true
|
2023-03-17 14:43:31 +08:00
|
|
|
argumentListBytes := message.ArgumentList().GetBinary(0)
|
2023-03-21 13:18:44 +08:00
|
|
|
if argumentListBytes == nil {
|
|
|
|
return
|
|
|
|
}
|
2023-03-17 14:43:31 +08:00
|
|
|
var messageDataBytes []byte
|
|
|
|
if argumentListBytes.IsValid() {
|
|
|
|
size := argumentListBytes.GetSize()
|
|
|
|
messageDataBytes = make([]byte, size)
|
|
|
|
c := argumentListBytes.GetData(messageDataBytes, 0)
|
|
|
|
argumentListBytes.Free() //立即释放掉
|
|
|
|
if c == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var messageId int32
|
|
|
|
var emitName string
|
2023-05-31 17:41:14 +08:00
|
|
|
var argument ipcArgument.IList // json.JSON
|
2023-03-17 14:43:31 +08:00
|
|
|
var argumentList json.JSONArray
|
|
|
|
if messageDataBytes != nil {
|
2023-05-31 17:41:14 +08:00
|
|
|
argument = ipcArgument.UnList(messageDataBytes)
|
|
|
|
messageId = argument.MessageId()
|
|
|
|
emitName = argument.GetEventName()
|
|
|
|
if argument.JSON() != nil {
|
|
|
|
argumentList = argument.JSON().JSONArray()
|
|
|
|
}
|
2023-03-18 00:19:20 +08:00
|
|
|
messageDataBytes = nil
|
2023-03-17 14:43:31 +08:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if argumentList != nil {
|
|
|
|
argumentList.Free()
|
|
|
|
}
|
2023-03-18 00:19:20 +08:00
|
|
|
if argument != nil {
|
2023-05-31 17:41:14 +08:00
|
|
|
argument.Reset()
|
2023-03-18 00:19:20 +08:00
|
|
|
}
|
2023-03-17 14:43:31 +08:00
|
|
|
}()
|
|
|
|
argumentListBytes = nil
|
2023-03-23 09:16:27 +08:00
|
|
|
var ipcContext = m.jsExecuteGoMethod(browser.Identifier(), frame.Identifier(), emitName, argumentList)
|
2023-03-23 17:33:33 +08:00
|
|
|
if messageId != 0 { // 异步回调函数处理
|
2023-05-31 17:41:14 +08:00
|
|
|
replyMessage := &ipcArgument.List{
|
|
|
|
Id: messageId,
|
|
|
|
}
|
2023-03-23 09:16:27 +08:00
|
|
|
if ipcContext != nil {
|
|
|
|
//处理回复消息
|
|
|
|
replay := ipcContext.Replay()
|
|
|
|
if replay.Result() != nil && len(replay.Result()) > 0 {
|
2023-05-31 17:41:14 +08:00
|
|
|
replyMessage.Data = replay.Result()
|
2023-03-13 17:30:13 +08:00
|
|
|
}
|
|
|
|
}
|
2023-03-21 13:18:44 +08:00
|
|
|
frame.SendProcessMessageForJSONBytes(internalIPCJSExecuteGoEventReplay, consts.PID_RENDER, replyMessage.Bytes())
|
2023-05-31 17:41:14 +08:00
|
|
|
replyMessage.Reset()
|
2023-03-13 17:30:13 +08:00
|
|
|
}
|
2023-03-23 09:16:27 +08:00
|
|
|
if ipcContext != nil {
|
|
|
|
if ipcContext.ArgumentList() != nil {
|
|
|
|
ipcContext.ArgumentList().Free()
|
|
|
|
}
|
|
|
|
ipcContext.Result(nil)
|
2023-03-14 17:30:45 +08:00
|
|
|
}
|
2023-03-13 17:30:13 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-23 09:16:27 +08:00
|
|
|
// jsExecuteGoMethod 执行Go函数
|
2023-05-31 17:41:14 +08:00
|
|
|
func (m *ipcBrowserProcess) jsExecuteGoMethod(browserId int32, frameId int64, emitName string, argumentList json.JSONArray) context.IContext {
|
2023-03-23 09:16:27 +08:00
|
|
|
eventCallback := ipc.CheckOnEvent(emitName)
|
2023-05-31 17:41:14 +08:00
|
|
|
var ipcContext context.IContext
|
2023-03-23 09:16:27 +08:00
|
|
|
if eventCallback != nil {
|
2023-05-31 17:41:14 +08:00
|
|
|
ipcContext = context.NewContext(browserId, frameId, true, argumentList)
|
2023-03-23 09:16:27 +08:00
|
|
|
//调用监听函数
|
|
|
|
if ctxCallback := eventCallback.ContextCallback(); ctxCallback != nil {
|
|
|
|
ctxCallback.Invoke(ipcContext)
|
|
|
|
} else if argsCallback := eventCallback.ArgumentCallback(); argsCallback != nil {
|
|
|
|
argsCallback.Invoke(ipcContext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ipcContext
|
|
|
|
}
|
|
|
|
|
2023-05-31 17:41:14 +08:00
|
|
|
// goExecuteMethodMessageReply 执行Go函数回复结果
|
|
|
|
func (m *ipcBrowserProcess) goExecuteMethodMessageReply(browserId int32, frameId int64, argument ipcArgument.IList) (result bool) {
|
|
|
|
var messageId = argument.MessageId()
|
2023-03-21 16:53:02 +08:00
|
|
|
var argumentList json.JSONArray
|
2023-05-31 17:41:14 +08:00
|
|
|
if argument.JSON() != nil {
|
|
|
|
argumentList = argument.JSON().JSONArray()
|
2023-03-21 16:53:02 +08:00
|
|
|
}
|
|
|
|
if callback := ipc.CheckEmitCallback(messageId); callback != nil {
|
2023-05-31 17:41:14 +08:00
|
|
|
ipcContext := context.NewContext(browserId, frameId, false, argumentList)
|
2023-03-21 16:53:02 +08:00
|
|
|
if ctxCallback := callback.ContextCallback(); ctxCallback != nil {
|
|
|
|
ctxCallback.Invoke(ipcContext)
|
|
|
|
} else if argsCallback := callback.ArgumentCallback(); argsCallback != nil {
|
|
|
|
argsCallback.Invoke(ipcContext)
|
|
|
|
}
|
|
|
|
if ipcContext.ArgumentList() != nil {
|
|
|
|
ipcContext.ArgumentList().Free()
|
|
|
|
}
|
|
|
|
ipcContext.Result(nil)
|
|
|
|
}
|
2023-03-20 21:35:21 +08:00
|
|
|
return
|
|
|
|
}
|
2023-03-24 14:31:40 +08:00
|
|
|
|
2023-05-31 17:41:14 +08:00
|
|
|
// registerEvent Go IPC 事件监听
|
|
|
|
func (m *ipcBrowserProcess) registerEvent() {
|
|
|
|
ipc.BrowserChan().AddCallback(func(channelId int64, argument ipcArgument.IList) bool {
|
|
|
|
if argument != nil {
|
|
|
|
name := argument.GetName()
|
|
|
|
if name == internalIPCJSExecuteGoSyncEvent { //JS 同步事件
|
|
|
|
m.jsExecuteGoSyncMethodMessage(argument.BrowserId(), channelId, argument)
|
|
|
|
return true
|
|
|
|
} else if name == internalIPCGoExecuteJSEventReplay {
|
|
|
|
ipcBrowser.goExecuteMethodMessageReply(argument.BrowserId(), channelId, argument)
|
2023-03-24 15:20:26 +08:00
|
|
|
return true
|
2023-03-24 14:31:40 +08:00
|
|
|
}
|
2023-03-24 15:20:26 +08:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2023-03-24 14:31:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// jsExecuteGoSyncMethodMessage JS执行Go事件 - 同步消息处理
|
2023-05-31 17:41:14 +08:00
|
|
|
func (m *ipcBrowserProcess) jsExecuteGoSyncMethodMessage(browserId int32, frameId int64, argument ipcArgument.IList) {
|
|
|
|
var argumentList json.JSONArray
|
|
|
|
if argument.JSON() != nil {
|
|
|
|
argumentList = argument.JSON().JSONArray()
|
|
|
|
}
|
|
|
|
var emitName = argument.GetEventName()
|
2023-03-24 14:31:40 +08:00
|
|
|
var ipcContext = m.jsExecuteGoMethod(browserId, frameId, emitName, argumentList)
|
2023-05-31 17:41:14 +08:00
|
|
|
message := &ipcArgument.List{
|
|
|
|
Id: 1,
|
|
|
|
Name: internalIPCJSExecuteGoSyncEventReplay,
|
|
|
|
}
|
2023-03-24 14:31:40 +08:00
|
|
|
// 同步回调函数处理
|
|
|
|
if ipcContext != nil {
|
|
|
|
//处理回复消息
|
|
|
|
replay := ipcContext.Replay()
|
|
|
|
if replay.Result() != nil && len(replay.Result()) > 0 {
|
2023-05-31 17:41:14 +08:00
|
|
|
message.Data = replay.Result()
|
2023-03-24 14:31:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//回复结果消息
|
2023-05-31 17:41:14 +08:00
|
|
|
ipc.BrowserChan().IPC().Send(frameId, message.Bytes())
|
|
|
|
message.Reset()
|
2023-03-24 14:31:40 +08:00
|
|
|
if ipcContext != nil {
|
|
|
|
if ipcContext.ArgumentList() != nil {
|
|
|
|
ipcContext.ArgumentList().Free()
|
|
|
|
}
|
|
|
|
ipcContext.Result(nil)
|
|
|
|
}
|
|
|
|
}
|