diff --git a/cef/application.go b/cef/application.go index 2b980683500a1f4916cff5ec3d3bf7cfc4822cef..5fa056cde8ea9a11f55dab870910f42179080667 100644 --- a/cef/application.go +++ b/cef/application.go @@ -51,6 +51,9 @@ func NewApplication(disableRegisDefaultEvent ...bool) *TCEFApplication { return application } +// CreateApplication +// 创建CEF Application +// 初始化CEF时必须创建,多进程模式每个application配置都应该相同 func CreateApplication() *TCEFApplication { var result uintptr imports.Proc(def.CEFApplication_Create).Call(uintptr(unsafe.Pointer(&result))) @@ -134,10 +137,14 @@ func (m *TCEFApplication) Free() { } } +// AddCustomCommandLine +// 添加自定义进程启动时添加的命令行参数 func (m *TCEFApplication) AddCustomCommandLine(commandLine, value string) { imports.Proc(def.AddCustomCommandLine).Call(api.PascalStr(commandLine), api.PascalStr(value)) } +// SetOnRegCustomSchemes +// 自定义协议注册回调函数 func (m *TCEFApplication) SetOnRegCustomSchemes(fn GlobalCEFAppEventOnRegCustomSchemes) { m.onRegCustomSchemes = fn } @@ -155,32 +162,44 @@ func (m *TCEFApplication) defaultSetOnRegCustomSchemes() { }) } -// TODO TCefPreferenceRegistrarRef +// SetOnRegisterCustomPreferences +// TODO 该函数还未完全实现 func (m *TCEFApplication) SetOnRegisterCustomPreferences(fn GlobalCEFAppEventOnRegisterCustomPreferences) { imports.Proc(def.CEFGlobalApp_SetOnRegisterCustomPreferences).Call(api.MakeEventDataPtr(fn)) } +// SetOnContextInitialized +// 上下文初始化 func (m *TCEFApplication) SetOnContextInitialized(fn GlobalCEFAppEventOnContextInitialized) { imports.Proc(def.CEFGlobalApp_SetOnContextInitialized).Call(api.MakeEventDataPtr(fn)) } -// 启动子进程之前自定义命令行参数设置 +// SetOnBeforeChildProcessLaunch +// 启动子进程之前自定义命令行参数设置 func (m *TCEFApplication) SetOnBeforeChildProcessLaunch(fn GlobalCEFAppEventOnBeforeChildProcessLaunch) { imports.Proc(def.CEFGlobalApp_SetOnBeforeChildProcessLaunch).Call(api.MakeEventDataPtr(fn)) } +// SetOnGetDefaultClient +// 获取并返回CefClient, 我们自己创建并返回到 *ICefClient = myCefClient func (m *TCEFApplication) SetOnGetDefaultClient(fn GlobalCEFAppEventOnGetDefaultClient) { imports.Proc(def.CEFGlobalApp_SetOnGetDefaultClient).Call(api.MakeEventDataPtr(fn)) } +// SetOnGetLocalizedString +// 获取并返回本地化 func (m *TCEFApplication) SetOnGetLocalizedString(fn GlobalCEFAppEventOnGetLocalizedString) { imports.Proc(def.CEFGlobalApp_SetOnGetLocalizedString).Call(api.MakeEventDataPtr(fn)) } +// SetOnGetDataResource +// 获取并返回本地资源 func (m *TCEFApplication) SetOnGetDataResource(fn GlobalCEFAppEventOnGetDataResource) { imports.Proc(def.CEFGlobalApp_SetOnGetDataResource).Call(api.MakeEventDataPtr(fn)) } +// SetOnGetDataResourceForScale +// 获取并返回本地资源大小 func (m *TCEFApplication) SetOnGetDataResourceForScale(fn GlobalCEFAppEventOnGetDataResourceForScale) { imports.Proc(def.CEFGlobalApp_SetOnGetDataResourceForScale).Call(api.MakeEventDataPtr(fn)) } @@ -243,7 +262,8 @@ func (m *TCEFApplication) SetOnFocusedNodeChanged(fn GlobalCEFAppEventOnFocusedN imports.Proc(def.CEFGlobalApp_SetOnFocusedNodeChanged).Call(api.MakeEventDataPtr(fn)) } -// 进程间通信处理消息接收 +// SetOnProcessMessageReceived +// 进程间通信处理消息接收回调函数 func (m *TCEFApplication) SetOnProcessMessageReceived(fn RenderProcessMessageReceived) { m.onProcessMessageReceived = fn } diff --git a/cef/internal/ipc/ipc.go b/cef/internal/ipc/ipc.go index 9d65d5baee1fb9e9af6e56748684cb033f200265..aadb162262c31b8a97750e3c1c6b6905beda2315 100644 --- a/cef/internal/ipc/ipc.go +++ b/cef/internal/ipc/ipc.go @@ -158,9 +158,9 @@ func EmitTarget(name string, tag target.ITarget, argument ...any) { if name == "" { return } - if tag != nil && tag.TargetType() == target.TgGo { - if tag.ChannelId() > 0 { - emitSendToGoChannel(0, tag.ChannelId(), name, argument) + if tag != nil { + if (tag.ChannelId() > 0 && tag.TargetType() == target.TgGoSub) || (tag.TargetType() == target.TgGoMain) { + emitSendToGoChannel(0, tag, name, argument) return } } @@ -177,10 +177,10 @@ func EmitTargetAndCallback(name string, tag target.ITarget, argument []any, fn a return } var messageId int32 = 0 - if tag != nil && tag.TargetType() == target.TgGo { - if tag.ChannelId() > 0 { + if tag != nil { + if (tag.ChannelId() > 0 && tag.TargetType() == target.TgGoSub) || (tag.TargetType() == target.TgGoMain) { messageId = browser.addEmitCallback(fn) - emitSendToGoChannel(messageId, tag.ChannelId(), name, argument) + emitSendToGoChannel(messageId, tag, name, argument) return } } diff --git a/cef/internal/ipc/send.go b/cef/internal/ipc/send.go index e8e985f2773dcb0752d8f98c80f4cf11b299caf6..fed8d692003d35407ac36c526df6fbd7ebe0dcf3 100644 --- a/cef/internal/ipc/send.go +++ b/cef/internal/ipc/send.go @@ -12,11 +12,12 @@ package ipc import ( "github.com/energye/energy/v2/cef/ipc/argument" + "github.com/energye/energy/v2/cef/ipc/target" ) // emitSendToChannel // trigger the specified target Go channel event -func emitSendToGoChannel(messageId int32, channelId int64, eventName string, arguments []any) { +func emitSendToGoChannel(messageId int32, tag target.ITarget, eventName string, arguments []any) { message := &argument.List{ Id: messageId, Name: InternalIPCGoExecuteGoEvent, @@ -24,10 +25,14 @@ func emitSendToGoChannel(messageId int32, channelId int64, eventName string, arg Data: arguments, } if isMainProcess { - BrowserChan().IPC().Send(channelId, message.Bytes()) + BrowserChan().IPC().Send(tag.ChannelId(), message.Bytes()) } else { message.BId = RenderChan().BrowserId() - RenderChan().IPC().SendToChannel(channelId, message.Bytes()) + if tag.TargetType() == target.TgGoSub { + RenderChan().IPC().SendToChannel(tag.ChannelId(), message.Bytes()) + } else if tag.TargetType() == target.TgGoMain { + RenderChan().IPC().Send(message.Bytes()) + } } message.Reset() } diff --git a/cef/ipc/target/target.go b/cef/ipc/target/target.go index aa8b7f788a41d17698480a87e8db2c2ed556e3a4..7a5d9b7ea424e3779f7ed3dea40efe30bc59c69a 100644 --- a/cef/ipc/target/target.go +++ b/cef/ipc/target/target.go @@ -14,12 +14,14 @@ package target // Type // 0: Trigger the JS event of the specified target process -// 1: Trigger GO events for the specified target process +// 1: Trigger TgGoSub events for the specified target sub process +// 2: Trigger TgGoMain events for the specified target main process type Type int8 const ( - TgJs Type = iota //JS Event - TgGo //GO Event + TgJs Type = iota //JS Event + TgGoSub //GO Event sub + TgGoMain //GO Event main ) // ITarget @@ -43,7 +45,7 @@ type Target struct { // browserId: browser window ID // channelId: IPC channelID, frameId or GO IPC channelID // targetType: Optional parameter, target type default 0 -// Type: TgJs:JS Event, TgGo:GO Event +// Type: TgJs:JS Event, TgGoSub:GO Sub Event, TgGoMain:GO Main Event func NewTarget(browserId int32, channelId int64, targetType ...Type) ITarget { m := &Target{ browseId: browserId, @@ -55,6 +57,14 @@ func NewTarget(browserId int32, channelId int64, targetType ...Type) ITarget { return m } +// NewTargetMain Create a new Emit target Main Process +// targetType: TgGoMain +func NewTargetMain() ITarget { + return &Target{ + targetType: TgGoMain, + } +} + // TargetType // target type // 0: Trigger JS event