U: Go execution IPC listening event changed to asynchronous execution

This commit is contained in:
杨红岩 2024-04-11 15:50:54 +08:00
parent 1ae26aa249
commit 9ebd50bfbf
4 changed files with 51 additions and 43 deletions

View File

@ -19,7 +19,7 @@ import (
"strings"
)
// appOnContextCreated 创建应用上下文 - 默认实现
// 创建应用上下文 - 默认实现
func appOnContextCreated(browser *ICefBrowser, frame *ICefFrame, context *ICefV8Context) {
process.Current.SetBrowserId(browser.Identifier()) // 当前进程 browserID
process.Current.SetFrameId(frame.Identifier()) // 当前进程 frameId
@ -29,17 +29,17 @@ func appOnContextCreated(browser *ICefBrowser, frame *ICefFrame, context *ICefV8
makeProcess(browser, frame, context) // process make
}
// appMainRunCallback 应用运行 - 默认实现
// 应用运行 - 默认实现
func appMainRunCallback() {
ipcBrowser.registerEvent() // browser ipc
}
// appWebKitInitialized - webkit - 默认实现
// webkit - 默认实现
func appWebKitInitialized() {
dragExtensionHandler() // drag extension handler
}
// renderProcessMessageReceived 渲染进程消息 - 默认实现
// 渲染进程消息 - 默认实现
func renderProcessMessageReceived(browser *ICefBrowser, frame *ICefFrame, sourceProcess consts.CefProcessId, message *ICefProcessMessage) (result bool) {
if message.Name() == internalIPCJSExecuteGoEventReplay {
result = ipcRender.ipcJSExecuteGoEventMessageReply(browser, frame, sourceProcess, message)
@ -49,7 +49,7 @@ func renderProcessMessageReceived(browser *ICefBrowser, frame *ICefFrame, source
return
}
// regCustomSchemes 注册自定义协议 - 默认实现
// 注册自定义协议 - 默认实现
func regCustomSchemes(registrar *TCefSchemeRegistrarRef) {
if localLoadRes.enable() {
// 以下几种默认的协议不去注册
@ -66,7 +66,7 @@ func regCustomSchemes(registrar *TCefSchemeRegistrarRef) {
}
}
// browserProcessMessageReceived 主进程消息 - 默认实现
// 主进程消息 - 默认实现
func browserProcessMessageReceived(browser *ICefBrowser, frame *ICefFrame, message *ICefProcessMessage) (result bool) {
if message.Name() == internalIPCJSExecuteGoEvent {
result = ipcBrowser.jsExecuteGoMethodMessage(browser, frame, message)

View File

@ -24,7 +24,7 @@ import (
type ipcBrowserProcess struct {
}
// ipcGoExecuteMethodMessage 执行 Go 监听函数
// 执行 Go 监听函数
func (m *ipcBrowserProcess) jsExecuteGoMethodMessage(browser *ICefBrowser, frame *ICefFrame, message *ICefProcessMessage) (result bool) {
result = true
argumentListBytes := message.ArgumentList().GetBinary(0)
@ -54,45 +54,53 @@ func (m *ipcBrowserProcess) jsExecuteGoMethodMessage(browser *ICefBrowser, frame
}
messageDataBytes = nil
}
defer func() {
if argumentList != nil {
argumentList.Free()
}
if argument != nil {
argument.Reset()
}
}()
argumentListBytes = nil
var ipcContext = m.jsExecuteGoMethod(browser.Identifier(), frame.Identifier(), emitName, argumentList)
if messageId != 0 { // 异步回调函数处理
replyMessage := &ipcArgument.List{
Id: messageId,
browser = BrowserRef.UnWrap(browser) // 必须 Warp
frame = FrameRef.UnWrap(frame) // 必须 Warp
browserID := browser.Identifier()
frameID := frame.Identifier()
go func() { // 开启线程, 异步执行
defer func() {
if argumentList != nil {
argumentList.Free()
}
if argument != nil {
argument.Reset()
}
frame.Free()
browser.Free()
}()
var ipcContext = m.jsExecuteGoMethod(browserID, frameID, emitName, argumentList)
if messageId != 0 { // 异步回调函数处理
replyMessage := &ipcArgument.List{
Id: messageId,
}
if ipcContext != nil {
//处理回复消息
replay := ipcContext.Replay()
if replay.Result() != nil && len(replay.Result()) > 0 {
replyMessage.Data = replay.Result()
}
}
if application.IsSpecVer49() {
// CEF49
browser.SendProcessMessageForJSONBytes(internalIPCJSExecuteGoEventReplay, consts.PID_RENDER, replyMessage.Bytes())
} else {
frame.SendProcessMessageForJSONBytes(internalIPCJSExecuteGoEventReplay, consts.PID_RENDER, replyMessage.Bytes())
}
replyMessage.Reset()
}
if ipcContext != nil {
//处理回复消息
replay := ipcContext.Replay()
if replay.Result() != nil && len(replay.Result()) > 0 {
replyMessage.Data = replay.Result()
if ipcContext.ArgumentList() != nil {
ipcContext.ArgumentList().Free()
}
ipcContext.Result(nil)
}
if application.IsSpecVer49() {
// CEF49
browser.SendProcessMessageForJSONBytes(internalIPCJSExecuteGoEventReplay, consts.PID_RENDER, replyMessage.Bytes())
} else {
frame.SendProcessMessageForJSONBytes(internalIPCJSExecuteGoEventReplay, consts.PID_RENDER, replyMessage.Bytes())
}
replyMessage.Reset()
}
if ipcContext != nil {
if ipcContext.ArgumentList() != nil {
ipcContext.ArgumentList().Free()
}
ipcContext.Result(nil)
}
}()
return
}
// jsExecuteGoMethod 执行Go函数
// 执行Go函数
func (m *ipcBrowserProcess) jsExecuteGoMethod(browserId int32, frameId int64, emitName string, argumentList json.JSONArray) context.IContext {
eventCallback := ipc.CheckOnEvent(emitName)
var ipcContext context.IContext
@ -108,7 +116,7 @@ func (m *ipcBrowserProcess) jsExecuteGoMethod(browserId int32, frameId int64, em
return ipcContext
}
// goExecuteMethodMessageReply 执行Go函数回复结果
// 执行Go函数回复结果
func (m *ipcBrowserProcess) goExecuteMethodMessageReply(browserId int32, frameId int64, argument ipcArgument.IList) (result bool) {
var messageId = argument.MessageId()
var argumentList json.JSONArray
@ -130,7 +138,7 @@ func (m *ipcBrowserProcess) goExecuteMethodMessageReply(browserId int32, frameId
return
}
// registerEvent Go IPC 事件监听
// Go IPC 事件监听
func (m *ipcBrowserProcess) registerEvent() {
ipc.BrowserChan().AddCallback(func(channelId int64, argument ipcArgument.IList) bool {
if argument != nil {
@ -175,7 +183,7 @@ func (m *ipcBrowserProcess) registerEvent() {
})
}
// jsExecuteGoSyncMethodMessage JS执行Go事件 - 同步消息处理
// JS执行Go事件 - 同步消息处理
func (m *ipcBrowserProcess) jsExecuteGoSyncMethodMessage(browserId int32, frameId int64, argument ipcArgument.IList) {
var argumentList json.JSONArray
if argument.JSON() != nil {

View File

@ -658,7 +658,7 @@ func (*browser) UnWrap(data *ICefBrowser) *ICefBrowser {
if result == 0 {
return nil
}
data.base.Free(data.Instance())
//data.base.Free(data.Instance())
data.instance = getInstance(result)
return data
}

View File

@ -286,7 +286,7 @@ func (m *frameRef) UnWrap(data *ICefFrame) *ICefFrame {
if result == 0 {
return nil
}
data.base.Free(data.Instance())
//data.base.Free(data.Instance())
data.instance = getInstance(result)
return data
}