energy/cef/cef-application-config.go

277 lines
7.7 KiB
Go
Raw Normal View History

2022-10-04 13:21:05 +08:00
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
2022-10-04 16:38:43 +08:00
// Licensed under GNU General Public License v3.0
2022-10-04 13:21:05 +08:00
//
//----------------------------------------
package cef
import (
2022-11-17 11:12:42 +08:00
"github.com/energye/energy/common"
2022-10-04 22:34:57 +08:00
. "github.com/energye/energy/consts"
2022-12-05 10:44:24 +08:00
"github.com/energye/golcl/energy/tools"
2022-10-04 13:21:05 +08:00
"github.com/energye/golcl/lcl/api"
2022-11-17 11:12:42 +08:00
"os"
"path/filepath"
2022-10-04 13:21:05 +08:00
)
// Application 支持的配置
type tCefApplicationConfig struct {
frameworkDirPath uintptr //string cef框架根目录
resourcesDirPath uintptr //string
localesDirPath uintptr //string
cache uintptr //string
userDataPath uintptr //string
language uintptr //string 语言设置
localesRequired uintptr //string 默认空,检查所有的语言环境 逗号分隔
logFile uintptr //string
mainBundlePath uintptr //string 只对 darwin 启作用
browseSubprocessPath uintptr //string 只对 非darwin 启作用
logSeverity uintptr //uint32
noSandbox uintptr //bool
disableZygote uintptr //bool 只对 linux 启作用
enableGPU uintptr //bool
singleProcess uintptr //bool 进程启动模式,默认false true:单进程 false:多进程
useMockKeyChain uintptr //bool
checkCEFFiles uintptr //bool
remoteDebuggingPort uintptr //int32
externalMessagePump uintptr //bool
multiThreadedMessageLoop uintptr //bool
chromeRuntime uintptr //bool
2022-10-04 13:21:05 +08:00
}
//创建应用全局配置
func NewApplicationConfig() *tCefApplicationConfig {
m := &tCefApplicationConfig{}
2022-10-04 22:34:57 +08:00
m.SetFrameworkDirPath(Empty)
m.SetResourcesDirPath(Empty)
m.SetLocalesDirPath(Empty)
m.SetCache(Empty)
m.SetUserDataPath(Empty)
2022-10-04 13:21:05 +08:00
m.SetLanguage(LANGUAGE_zh_CN)
2022-10-04 22:34:57 +08:00
m.SetLocalesRequired(Empty)
m.SetLogFile(Empty)
m.SetMainBundlePath(Empty)
m.SetBrowseSubprocessPath(Empty)
2022-10-04 13:21:05 +08:00
m.SetLogSeverity(LOGSEVERITY_DISABLE)
m.SetEnableGPU(enableGPU)
m.SetSingleProcess(false)
m.SetUseMockKeyChain(false)
m.SetNoSandbox(true)
m.SetDisableZygote(true)
m.SetCheckCEFFiles(false)
m.SetRemoteDebuggingPort(0)
m.SetChromeRuntime(false)
//linux >= 107.xxx 版本以后默认启用的GTK3106及以前版本默认支持GTK2但无法正常输入中文
//顾强制使用GTK3方式但又无法正常创建lcl组件到窗口中该框架只对浏览器应用做封装
//所以初衷以浏览器应用建设为目标
if common.IsLinux() {
//linux平台默认设置为false,将启用 ViewsFrameworkBrowserWindow 窗口
m.SetExternalMessagePump(false)
m.SetMultiThreadedMessageLoop(false)
} else {
m.SetExternalMessagePump(false)
m.SetMultiThreadedMessageLoop(true)
}
2022-10-04 13:21:05 +08:00
return m
}
2022-10-05 16:38:43 +08:00
//设置 Chromium Framework 编译好的二进制包根目录
//
//默认当前目录
2022-10-04 13:21:05 +08:00
func (m *tCefApplicationConfig) SetFrameworkDirPath(s string) *tCefApplicationConfig {
2022-12-03 21:56:51 +08:00
m.frameworkDirPath = api.PascalStr(s)
2022-10-04 13:21:05 +08:00
return m
}
//设置资源目录,默认当前目录
func (m *tCefApplicationConfig) SetResourcesDirPath(s string) *tCefApplicationConfig {
2022-12-03 21:56:51 +08:00
m.resourcesDirPath = api.PascalStr(s)
2022-10-04 13:21:05 +08:00
return m
}
//设置本地语言目录,默认当前目录
func (m *tCefApplicationConfig) SetLocalesDirPath(s string) *tCefApplicationConfig {
2022-12-03 21:56:51 +08:00
m.localesDirPath = api.PascalStr(s)
2022-10-04 13:21:05 +08:00
return m
}
//设置缓存目录,默认当前目录
func (m *tCefApplicationConfig) SetCache(s string) *tCefApplicationConfig {
2022-12-03 21:56:51 +08:00
m.cache = api.PascalStr(s)
2022-10-04 13:21:05 +08:00
return m
}
//设置用户数据目录,默认当前目录
func (m *tCefApplicationConfig) SetUserDataPath(s string) *tCefApplicationConfig {
2022-12-03 21:56:51 +08:00
m.userDataPath = api.PascalStr(s)
2022-10-04 13:21:05 +08:00
return m
}
//设置进程模型作用于linux-默认禁用
func (m *tCefApplicationConfig) SetDisableZygote(s bool) *tCefApplicationConfig {
2022-12-03 21:56:51 +08:00
m.disableZygote = api.PascalBool(s)
2022-10-04 13:21:05 +08:00
return m
}
//设置关闭沙盒-默认关闭
func (m *tCefApplicationConfig) SetNoSandbox(s bool) *tCefApplicationConfig {
2022-12-03 21:56:51 +08:00
m.noSandbox = api.PascalBool(s)
2022-10-04 13:21:05 +08:00
return m
}
//设置开启关闭GPU加速
func (m *tCefApplicationConfig) SetEnableGPU(s bool) *tCefApplicationConfig {
enableGPU = s
2022-12-03 21:56:51 +08:00
m.enableGPU = api.PascalBool(s)
2022-10-04 13:21:05 +08:00
return m
}
//设置进程模式true:单进程模式
func (m *tCefApplicationConfig) SetSingleProcess(s bool) *tCefApplicationConfig {
SingleProcess = s
2022-12-03 21:56:51 +08:00
m.singleProcess = api.PascalBool(s)
2022-10-04 13:21:05 +08:00
return m
}
//设置使用模拟key chain
func (m *tCefApplicationConfig) SetUseMockKeyChain(s bool) *tCefApplicationConfig {
2022-12-03 21:56:51 +08:00
m.useMockKeyChain = api.PascalBool(s)
2022-10-04 13:21:05 +08:00
return m
}
//检测CEF文件默认不检测
func (m *tCefApplicationConfig) SetCheckCEFFiles(s bool) *tCefApplicationConfig {
2022-12-03 21:56:51 +08:00
m.checkCEFFiles = api.PascalBool(s)
2022-10-04 13:21:05 +08:00
return m
}
//设置语言
func (m *tCefApplicationConfig) SetLanguage(s LANGUAGE) *tCefApplicationConfig {
2022-12-03 21:56:51 +08:00
m.language = api.PascalStr(string(s))
2022-10-04 13:21:05 +08:00
return m
}
//设置必备的本地语言支持,逗号分隔的字符串 s="zh-CN,en-US" ,默认情况下 en-US 是必须的
func (m *tCefApplicationConfig) SetLocalesRequired(s string) *tCefApplicationConfig {
2022-12-03 21:56:51 +08:00
m.localesRequired = api.PascalStr(s)
2022-10-04 13:21:05 +08:00
return m
}
//设置日志文件目录
func (m *tCefApplicationConfig) SetLogFile(s string) *tCefApplicationConfig {
2022-12-03 21:56:51 +08:00
m.logFile = api.PascalStr(s)
2022-10-04 13:21:05 +08:00
return m
}
//设置主程序绑定目录 作用于macos
func (m *tCefApplicationConfig) SetMainBundlePath(s string) *tCefApplicationConfig {
2022-12-03 21:56:51 +08:00
m.mainBundlePath = api.PascalStr(s)
2022-10-04 13:21:05 +08:00
return m
}
//设置子进程执行文件目录,一搬用于主进程过于复杂启动慢,需要独立出子进程
func (m *tCefApplicationConfig) SetBrowseSubprocessPath(s string) *tCefApplicationConfig {
2022-12-03 21:56:51 +08:00
m.browseSubprocessPath = api.PascalStr(s)
2022-10-04 13:21:05 +08:00
return m
}
//设置日志级别
func (m *tCefApplicationConfig) SetLogSeverity(s LOG) *tCefApplicationConfig {
m.logSeverity = uintptr(s)
return m
}
//设置远程调式端口 (1024 ~ 65535)
func (m *tCefApplicationConfig) SetRemoteDebuggingPort(s int32) *tCefApplicationConfig {
if s > 1024 && s < 65535 {
m.remoteDebuggingPort = uintptr(s)
}
2022-10-04 13:21:05 +08:00
return m
}
func (m *tCefApplicationConfig) SetExternalMessagePump(s bool) *tCefApplicationConfig {
m.externalMessagePump = api.PascalBool(s)
return m
}
func (m *tCefApplicationConfig) SetMultiThreadedMessageLoop(s bool) *tCefApplicationConfig {
m.multiThreadedMessageLoop = api.PascalBool(s)
return m
}
func (m *tCefApplicationConfig) SetChromeRuntime(s bool) *tCefApplicationConfig {
m.chromeRuntime = api.PascalBool(s)
return m
}
2022-10-05 17:20:20 +08:00
// GO绑定JS通用类型所属对象名定义
2022-10-04 13:21:05 +08:00
//
//默认值 gocobj
func (m *tCefApplicationConfig) SetCommonRootName(name string) {
if name == "" {
name = commonRootName
} else {
commonRootName = name
}
2022-12-05 10:44:24 +08:00
common.Proc(internale_CEFV8ValueRef_SetCommonRootName).Call(api.PascalStr(commonRootName))
2022-10-04 13:21:05 +08:00
}
2022-10-05 17:20:20 +08:00
// GO绑定JS对象类型所属对象名定义
2022-10-04 13:21:05 +08:00
//
//默认值 goobj
func (m *tCefApplicationConfig) SetObjectRootName(name string) {
if name == "" {
name = objectRootName
} else {
objectRootName = name
}
2022-12-05 10:44:24 +08:00
common.Proc(internale_CEFV8ValueRef_SetObjectRootName).Call(api.PascalStr(objectRootName))
2022-10-04 13:21:05 +08:00
}
2022-11-17 11:12:42 +08:00
//energy framework env
func (m *tCefApplicationConfig) framework() {
2022-12-08 16:01:44 +08:00
var path string
if m.frameworkDirPath == 0 {
path = libPath()
} else {
path = api.GoStr(m.frameworkDirPath)
}
2022-11-17 11:12:42 +08:00
if path != "" {
2022-12-08 16:01:44 +08:00
m.SetFrameworkDirPath(path)
2022-11-17 11:12:42 +08:00
if m.cache == 0 {
m.SetCache(filepath.Join(path, "cache"))
}
if m.userDataPath == 0 {
m.SetUserDataPath(filepath.Join(path, "userDataPath"))
}
}
}
func ceflib() string {
if common.IsWindows() {
return "libcef.dll"
} else if common.IsLinux() {
return "libcef.so"
}
return ""
}
func libPath() string {
var lib = ceflib()
2022-11-17 14:22:21 +08:00
if lib != "" {
//当前目录
2022-11-22 11:35:21 +08:00
if tools.IsExist(ExePath + Separator + lib) {
return ExePath
2022-11-17 14:22:21 +08:00
}
//环境变量
var env = os.Getenv(ENERGY_HOME_KEY)
2022-11-22 11:35:21 +08:00
if tools.IsExist(env + Separator + lib) {
2022-11-17 14:22:21 +08:00
return env
}
2022-11-17 11:12:42 +08:00
}
return ""
}