energy/cef/cef-ipc-render-channel.go

54 lines
1.6 KiB
Go
Raw Normal View History

2023-03-23 11:46:34 +08:00
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
// GO render IPC通道
package cef
2023-03-23 17:33:33 +08:00
import (
"github.com/energye/energy/pkgs/channel"
"github.com/energye/energy/pkgs/json"
)
2023-03-23 11:46:34 +08:00
2023-03-24 14:31:40 +08:00
// 渲染(子)进程IPC
var renderIPC *renderIPCChan
2023-03-23 11:46:34 +08:00
// renderIPCChan
type renderIPCChan struct {
2023-03-24 14:31:40 +08:00
browserId int32 //浏览器ID
channelId int64 //通道ID 使用FrameId
ipc channel.IRenderChannel //channel
callback []func(channelId int64, data json.JSON) bool //实现回调函数
2023-03-23 11:46:34 +08:00
}
2023-03-24 14:31:40 +08:00
// renderIPCCreate 渲染进程IPC创建
2023-03-24 15:20:26 +08:00
func renderIPCCreate(browserId int32, channelId int64) {
2023-03-24 14:31:40 +08:00
if renderIPC == nil {
renderIPC = new(renderIPCChan)
2023-03-24 15:20:26 +08:00
renderIPC.browserId = browserId
renderIPC.channelId = channelId
2023-03-24 14:31:40 +08:00
renderIPC.ipc = channel.NewRender(renderIPC.channelId)
renderIPC.ipc.Handler(func(context channel.IIPCContext) {
data := context.Message().JSON()
//callback 返回 true ipc 停止遍历,否则继续遍历,直到最后一个
for _, call := range renderIPC.callback {
if call(context.ChannelId(), data) {
break
}
2023-03-23 17:33:33 +08:00
}
2023-03-23 11:46:34 +08:00
context.Free()
})
}
}
2023-03-23 17:33:33 +08:00
2023-03-24 14:31:40 +08:00
// addCallback 添加回调函数, callback 返回 true ipc 停止遍历,否则继续遍历,直到最后一个
func (m *renderIPCChan) addCallback(callback func(channelId int64, data json.JSON) bool) {
m.callback = append(m.callback, callback)
2023-03-23 17:33:33 +08:00
}