2022-10-04 13:21:05 +08:00
|
|
|
|
//----------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Copyright © yanghy. All Rights Reserved.
|
|
|
|
|
//
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// Licensed under Apache License Version 2.0, January 2004
|
|
|
|
|
//
|
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2022-10-04 13:21:05 +08:00
|
|
|
|
//
|
|
|
|
|
//----------------------------------------
|
|
|
|
|
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// 应用程序
|
2022-10-04 13:21:05 +08:00
|
|
|
|
package cef
|
|
|
|
|
|
|
|
|
|
import (
|
2022-12-13 11:49:32 +08:00
|
|
|
|
"fmt"
|
2023-02-13 21:25:35 +08:00
|
|
|
|
"github.com/energye/energy/common"
|
|
|
|
|
"github.com/energye/energy/common/imports"
|
2022-10-04 22:34:57 +08:00
|
|
|
|
. "github.com/energye/energy/consts"
|
|
|
|
|
"github.com/energye/energy/ipc"
|
2022-12-13 11:49:32 +08:00
|
|
|
|
"github.com/energye/energy/logger"
|
|
|
|
|
"github.com/energye/golcl/lcl"
|
2022-10-04 13:21:05 +08:00
|
|
|
|
"github.com/energye/golcl/lcl/api"
|
|
|
|
|
"unsafe"
|
|
|
|
|
)
|
|
|
|
|
|
2023-01-09 09:25:17 +08:00
|
|
|
|
var application *TCEFApplication
|
|
|
|
|
|
2023-02-28 18:41:12 +08:00
|
|
|
|
// TCEFApplication CEF应用对象
|
2022-10-04 13:21:05 +08:00
|
|
|
|
type TCEFApplication struct {
|
2022-12-04 18:27:20 +08:00
|
|
|
|
instance unsafe.Pointer
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-28 18:41:12 +08:00
|
|
|
|
// NewApplication 创建CEF应用
|
|
|
|
|
//
|
|
|
|
|
// 参数: disableRegisDefaultEvent = true 时不会注册默认事件
|
|
|
|
|
func NewApplication(disableRegisDefaultEvent ...bool) *TCEFApplication {
|
|
|
|
|
if application == nil {
|
|
|
|
|
var result uintptr
|
|
|
|
|
imports.Proc(internale_CEFApplication_Create).Call(uintptr(unsafe.Pointer(&result)))
|
|
|
|
|
application = &TCEFApplication{instance: unsafe.Pointer(result)}
|
|
|
|
|
if len(disableRegisDefaultEvent) == 0 || !disableRegisDefaultEvent[0] {
|
|
|
|
|
application.registerDefaultEvent()
|
|
|
|
|
}
|
|
|
|
|
application.initDefaultProperties()
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
2023-01-09 09:25:17 +08:00
|
|
|
|
return application
|
2022-12-15 23:13:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-28 18:41:12 +08:00
|
|
|
|
// AddCrDelegate MacOSX Delegate
|
|
|
|
|
func (m *TCEFApplication) AddCrDelegate() {
|
|
|
|
|
imports.Proc(internale_CEF_AddCrDelegate).Call()
|
2022-12-15 23:13:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-28 18:41:12 +08:00
|
|
|
|
//registerDefaultEvent 注册默认事件
|
2022-12-15 23:13:27 +08:00
|
|
|
|
func (m *TCEFApplication) registerDefaultEvent() {
|
|
|
|
|
m.defaultSetOnContextCreated()
|
|
|
|
|
m.defaultSetOnProcessMessageReceived()
|
|
|
|
|
m.defaultSetOnBeforeChildProcessLaunch()
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-28 18:41:12 +08:00
|
|
|
|
// Instance 实例
|
2022-12-04 18:27:20 +08:00
|
|
|
|
func (m *TCEFApplication) Instance() uintptr {
|
|
|
|
|
return uintptr(m.instance)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-06 15:25:28 +08:00
|
|
|
|
// 启动主进程
|
2022-10-04 13:21:05 +08:00
|
|
|
|
func (m *TCEFApplication) StartMainProcess() bool {
|
2022-12-04 18:27:20 +08:00
|
|
|
|
if m.instance != nullptr {
|
2023-02-13 21:25:35 +08:00
|
|
|
|
logger.Debug("application single exe,", common.Args.ProcessType(), "process start")
|
|
|
|
|
r1, _, _ := imports.Proc(internale_CEFStartMainProcess).Call(m.Instance())
|
2022-12-15 23:13:27 +08:00
|
|
|
|
return api.GoBool(r1)
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-28 18:41:12 +08:00
|
|
|
|
// StartSubProcess 启动子进程, 如果指定了子进程执行程序, 将执行指定的子进程程序
|
2022-12-09 21:08:30 +08:00
|
|
|
|
func (m *TCEFApplication) StartSubProcess() (result bool) {
|
2022-12-04 18:27:20 +08:00
|
|
|
|
if m.instance != nullptr {
|
2023-02-13 21:25:35 +08:00
|
|
|
|
logger.Debug("application multiple exe,", common.Args.ProcessType(), "process start")
|
|
|
|
|
r1, _, _ := imports.Proc(internale_CEFStartSubProcess).Call(m.Instance())
|
2022-12-09 21:08:30 +08:00
|
|
|
|
result = api.GoBool(r1)
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-28 18:41:12 +08:00
|
|
|
|
// RunMessageLoop 消息轮询
|
2022-12-15 23:13:27 +08:00
|
|
|
|
func (m *TCEFApplication) RunMessageLoop() {
|
2023-02-06 15:25:28 +08:00
|
|
|
|
defer func() {
|
|
|
|
|
logger.Debug("application run message loop end")
|
|
|
|
|
api.EnergyLibRelease()
|
|
|
|
|
}()
|
|
|
|
|
logger.Debug("application run message loop start")
|
2023-02-13 21:25:35 +08:00
|
|
|
|
imports.Proc(internale_CEFApplication_RunMessageLoop).Call()
|
2022-12-15 23:13:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-28 18:41:12 +08:00
|
|
|
|
// QuitMessageLoop 退出消息轮询
|
2022-12-27 21:38:01 +08:00
|
|
|
|
func (m *TCEFApplication) QuitMessageLoop() {
|
2023-02-06 15:25:28 +08:00
|
|
|
|
logger.Debug("application quit message loop")
|
2023-02-13 21:25:35 +08:00
|
|
|
|
imports.Proc(internale_CEFApplication_QuitMessageLoop).Call()
|
2022-12-27 21:38:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-09 21:08:30 +08:00
|
|
|
|
func (m *TCEFApplication) StopScheduler() {
|
2023-02-13 21:25:35 +08:00
|
|
|
|
imports.Proc(internale_CEFApplication_StopScheduler).Call()
|
2022-12-09 21:08:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *TCEFApplication) Destroy() {
|
2023-02-13 21:25:35 +08:00
|
|
|
|
imports.Proc(internale_CEFApplication_Destroy).Call()
|
2022-12-09 21:08:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-04 13:21:05 +08:00
|
|
|
|
func (m *TCEFApplication) Free() {
|
2022-12-04 18:27:20 +08:00
|
|
|
|
if m.instance != nullptr {
|
2023-02-13 21:25:35 +08:00
|
|
|
|
imports.Proc(internale_CEFApplication_Free).Call()
|
2022-12-04 18:27:20 +08:00
|
|
|
|
m.instance = nullptr
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-06 15:25:28 +08:00
|
|
|
|
// 上下文件创建回调
|
2022-10-04 13:21:05 +08:00
|
|
|
|
//
|
2023-02-06 15:25:28 +08:00
|
|
|
|
// 返回值 false 将会创建 render进程的IPC和GO绑定变量
|
2022-10-04 13:21:05 +08:00
|
|
|
|
//
|
2023-02-06 15:25:28 +08:00
|
|
|
|
// 对于一些不想GO绑定变量的URL地址,实现该函数,通过 frame.Url
|
2022-10-04 13:21:05 +08:00
|
|
|
|
func (m *TCEFApplication) SetOnContextCreated(fn GlobalCEFAppEventOnContextCreated) {
|
2023-02-13 21:25:35 +08:00
|
|
|
|
imports.Proc(internale_CEFGlobalApp_SetOnContextCreated).Call(api.MakeEventDataPtr(fn))
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
2022-12-05 10:44:24 +08:00
|
|
|
|
|
2022-10-04 13:21:05 +08:00
|
|
|
|
func (m *TCEFApplication) defaultSetOnContextCreated() {
|
|
|
|
|
m.SetOnContextCreated(func(browse *ICefBrowser, frame *ICefFrame, context *ICefV8Context) bool {
|
|
|
|
|
return false
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 23:13:27 +08:00
|
|
|
|
func (m *TCEFApplication) SetOnContextInitialized(fn GlobalCEFAppEventOnContextInitialized) {
|
2023-02-13 21:25:35 +08:00
|
|
|
|
imports.Proc(internale_CEFGlobalApp_SetOnContextInitialized).Call(api.MakeEventDataPtr(fn))
|
2022-12-15 23:13:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-06 15:25:28 +08:00
|
|
|
|
// 初始化设置全局回调
|
2022-10-04 13:21:05 +08:00
|
|
|
|
func (m *TCEFApplication) SetOnWebKitInitialized(fn GlobalCEFAppEventOnWebKitInitialized) {
|
2023-02-13 21:25:35 +08:00
|
|
|
|
imports.Proc(internale_CEFGlobalApp_SetOnWebKitInitialized).Call(api.MakeEventDataPtr(fn))
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-06 15:25:28 +08:00
|
|
|
|
// 进程间通信处理消息接收
|
2022-10-04 13:21:05 +08:00
|
|
|
|
func (m *TCEFApplication) SetOnProcessMessageReceived(fn RenderProcessMessageReceived) {
|
2023-02-13 21:25:35 +08:00
|
|
|
|
imports.Proc(internale_CEFGlobalApp_SetOnProcessMessageReceived).Call(api.MakeEventDataPtr(fn))
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
|
|
|
|
func (m *TCEFApplication) defaultSetOnProcessMessageReceived() {
|
2023-02-26 21:17:28 +08:00
|
|
|
|
m.SetOnProcessMessageReceived(func(browse *ICefBrowser, frame *ICefFrame, sourceProcess CefProcessId, processMessage *ICefProcessMessage) bool {
|
2022-10-04 13:21:05 +08:00
|
|
|
|
return false
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *TCEFApplication) AddCustomCommandLine(commandLine, value string) {
|
2023-02-13 21:25:35 +08:00
|
|
|
|
imports.Proc(internale_AddCustomCommandLine).Call(api.PascalStr(commandLine), api.PascalStr(value))
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-06 15:25:28 +08:00
|
|
|
|
// 启动子进程之前自定义命令行参数设置
|
2022-10-04 13:21:05 +08:00
|
|
|
|
func (m *TCEFApplication) SetOnBeforeChildProcessLaunch(fn GlobalCEFAppEventOnBeforeChildProcessLaunch) {
|
2023-02-13 21:25:35 +08:00
|
|
|
|
imports.Proc(internale_CEFGlobalApp_SetOnBeforeChildProcessLaunch).Call(api.MakeEventDataPtr(fn))
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
2023-02-28 18:41:12 +08:00
|
|
|
|
|
2022-10-04 13:21:05 +08:00
|
|
|
|
func (m *TCEFApplication) defaultSetOnBeforeChildProcessLaunch() {
|
|
|
|
|
m.SetOnBeforeChildProcessLaunch(func(commandLine *TCefCommandLine) {})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *TCEFApplication) SetOnBrowserDestroyed(fn GlobalCEFAppEventOnBrowserDestroyed) {
|
2023-02-13 21:25:35 +08:00
|
|
|
|
imports.Proc(internale_CEFGlobalApp_SetOnBrowserDestroyed).Call(api.MakeEventDataPtr(fn))
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *TCEFApplication) SetOnLoadStart(fn GlobalCEFAppEventOnRenderLoadStart) {
|
2023-02-13 21:25:35 +08:00
|
|
|
|
imports.Proc(internale_CEFGlobalApp_SetOnLoadStart).Call(api.MakeEventDataPtr(fn))
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *TCEFApplication) SetOnLoadEnd(fn GlobalCEFAppEventOnRenderLoadEnd) {
|
2023-02-13 21:25:35 +08:00
|
|
|
|
imports.Proc(internale_CEFGlobalApp_SetOnLoadEnd).Call(api.MakeEventDataPtr(fn))
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *TCEFApplication) SetOnLoadError(fn GlobalCEFAppEventOnRenderLoadError) {
|
2023-02-13 21:25:35 +08:00
|
|
|
|
imports.Proc(internale_CEFGlobalApp_SetOnLoadError).Call(api.MakeEventDataPtr(fn))
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *TCEFApplication) SetOnLoadingStateChange(fn GlobalCEFAppEventOnRenderLoadingStateChange) {
|
2023-02-13 21:25:35 +08:00
|
|
|
|
imports.Proc(internale_CEFGlobalApp_SetOnLoadingStateChange).Call(api.MakeEventDataPtr(fn))
|
2022-12-07 15:50:59 +08:00
|
|
|
|
}
|
2022-12-12 17:34:57 +08:00
|
|
|
|
|
|
|
|
|
func (m *TCEFApplication) SetOnGetDefaultClient(fn GlobalCEFAppEventOnGetDefaultClient) {
|
2023-02-13 21:25:35 +08:00
|
|
|
|
imports.Proc(internale_CEFGlobalApp_SetOnGetDefaultClient).Call(api.MakeEventDataPtr(fn))
|
2022-12-12 17:34:57 +08:00
|
|
|
|
}
|
2022-12-13 11:49:32 +08:00
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
lcl.RegisterExtEventCallback(func(fn interface{}, getVal func(idx int) uintptr) bool {
|
|
|
|
|
getPtr := func(i int) unsafe.Pointer {
|
|
|
|
|
return unsafe.Pointer(getVal(i))
|
|
|
|
|
}
|
|
|
|
|
switch fn.(type) {
|
|
|
|
|
case GlobalCEFAppEventOnBrowserDestroyed:
|
2023-02-26 19:14:49 +08:00
|
|
|
|
fn.(GlobalCEFAppEventOnBrowserDestroyed)(&ICefBrowser{instance: getPtr(0)})
|
2022-12-13 11:49:32 +08:00
|
|
|
|
case GlobalCEFAppEventOnRenderLoadStart:
|
2023-02-26 19:14:49 +08:00
|
|
|
|
browser := &ICefBrowser{instance: getPtr(0)}
|
|
|
|
|
frame := &ICefFrame{instance: getPtr(1)}
|
2022-12-13 11:49:32 +08:00
|
|
|
|
fn.(GlobalCEFAppEventOnRenderLoadStart)(browser, frame, TCefTransitionType(getVal(2)))
|
|
|
|
|
case GlobalCEFAppEventOnRenderLoadEnd:
|
2023-02-26 19:14:49 +08:00
|
|
|
|
browser := &ICefBrowser{instance: getPtr(0)}
|
|
|
|
|
frame := &ICefFrame{getPtr(1)}
|
2022-12-13 11:49:32 +08:00
|
|
|
|
fn.(GlobalCEFAppEventOnRenderLoadEnd)(browser, frame, int32(getVal(2)))
|
|
|
|
|
case GlobalCEFAppEventOnRenderLoadError:
|
2023-02-26 19:14:49 +08:00
|
|
|
|
browser := &ICefBrowser{instance: getPtr(0)}
|
|
|
|
|
frame := &ICefFrame{getPtr(1)}
|
2022-12-13 11:49:32 +08:00
|
|
|
|
fn.(GlobalCEFAppEventOnRenderLoadError)(browser, frame, TCefErrorCode(getVal(2)), api.GoStr(getVal(3)), api.GoStr(getVal(4)))
|
|
|
|
|
case GlobalCEFAppEventOnRenderLoadingStateChange:
|
2023-02-26 19:14:49 +08:00
|
|
|
|
browser := &ICefBrowser{instance: getPtr(0)}
|
|
|
|
|
frame := &ICefFrame{getPtr(1)}
|
2022-12-13 11:49:32 +08:00
|
|
|
|
fn.(GlobalCEFAppEventOnRenderLoadingStateChange)(browser, frame, api.GoBool(getVal(2)), api.GoBool(getVal(3)), api.GoBool(getVal(4)))
|
|
|
|
|
case RenderProcessMessageReceived:
|
2023-02-26 19:14:49 +08:00
|
|
|
|
browser := &ICefBrowser{instance: getPtr(0)}
|
|
|
|
|
frame := &ICefFrame{getPtr(1)}
|
2023-02-26 21:17:28 +08:00
|
|
|
|
processMessage := &ICefProcessMessage{instance: getPtr(3)}
|
2022-12-13 11:49:32 +08:00
|
|
|
|
var result = (*bool)(getPtr(4))
|
|
|
|
|
*result = fn.(RenderProcessMessageReceived)(browser, frame, CefProcessId(getVal(2)), processMessage)
|
|
|
|
|
case GlobalCEFAppEventOnContextCreated:
|
2023-02-26 19:14:49 +08:00
|
|
|
|
browser := &ICefBrowser{instance: getPtr(0)}
|
|
|
|
|
frame := &ICefFrame{instance: getPtr(1)}
|
|
|
|
|
ctx := &ICefV8Context{instance: getPtr(2)}
|
2022-12-13 11:49:32 +08:00
|
|
|
|
var result = fn.(GlobalCEFAppEventOnContextCreated)(browser, frame, ctx)
|
|
|
|
|
if !result {
|
2023-03-01 18:01:34 +08:00
|
|
|
|
appOnContextCreated(browser, frame)
|
2022-12-13 11:49:32 +08:00
|
|
|
|
}
|
|
|
|
|
case GlobalCEFAppEventOnWebKitInitialized:
|
|
|
|
|
fn.(GlobalCEFAppEventOnWebKitInitialized)()
|
2022-12-15 23:13:27 +08:00
|
|
|
|
case GlobalCEFAppEventOnContextInitialized:
|
|
|
|
|
fn.(GlobalCEFAppEventOnContextInitialized)()
|
2022-12-13 11:49:32 +08:00
|
|
|
|
case GlobalCEFAppEventOnBeforeChildProcessLaunch:
|
|
|
|
|
commands := (*uintptr)(getPtr(0))
|
|
|
|
|
commandLine := &TCefCommandLine{commandLines: make(map[string]string)}
|
|
|
|
|
ipc.IPC.SetPort()
|
|
|
|
|
commandLine.AppendSwitch(MAINARGS_NETIPCPORT, fmt.Sprintf("%d", ipc.IPC.Port()))
|
|
|
|
|
fn.(GlobalCEFAppEventOnBeforeChildProcessLaunch)(commandLine)
|
|
|
|
|
*commands = api.PascalStr(commandLine.toString())
|
|
|
|
|
case GlobalCEFAppEventOnGetDefaultClient:
|
|
|
|
|
client := (*uintptr)(getPtr(0))
|
|
|
|
|
getClient := &ICefClient{instance: unsafe.Pointer(client)}
|
|
|
|
|
fn.(GlobalCEFAppEventOnGetDefaultClient)(getClient)
|
|
|
|
|
*client = uintptr(getClient.instance)
|
|
|
|
|
default:
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
}
|