energy/cef/chromium.go

83 lines
1.9 KiB
Go
Raw Normal View History

2022-10-04 13:21:05 +08:00
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// 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-05-31 17:41:14 +08:00
// CEF Chromium 组件
2022-10-04 13:21:05 +08:00
package cef
import (
2023-05-31 18:00:34 +08:00
. "github.com/energye/energy/v2/consts"
2022-10-04 13:21:05 +08:00
"github.com/energye/golcl/lcl"
"github.com/energye/golcl/lcl/types"
2022-10-04 13:21:05 +08:00
"unsafe"
)
2023-02-20 00:20:01 +08:00
// IChromium 组件接口
2022-10-04 13:21:05 +08:00
type IChromium interface {
IChromiumProc
IChromiumEvent
}
2023-02-20 00:20:01 +08:00
// TCEFChromium 组件
2022-10-04 13:21:05 +08:00
type TCEFChromium struct {
*lcl.TComponent
instance unsafe.Pointer
2023-06-19 12:41:52 +08:00
options IChromiumOptions
config *TCefChromiumConfig
2023-03-01 14:18:39 +08:00
browser *ICefBrowser
2023-05-31 17:41:14 +08:00
idBrowsers map[int32]*ICefBrowser
browserHandle types.HWND
widgetHandle types.HWND
renderHandle types.HWND
2023-03-16 21:19:27 +08:00
initialized bool
2022-10-04 13:21:05 +08:00
}
2023-02-20 00:20:01 +08:00
// NewChromium 创建一个新的 TCEFChromium
2023-06-19 12:41:52 +08:00
func NewChromium(owner lcl.IComponent, config *TCefChromiumConfig) IChromium {
2022-10-04 13:21:05 +08:00
m := new(TCEFChromium)
if config != nil {
2023-06-19 12:41:52 +08:00
m.config = config
2022-10-04 13:21:05 +08:00
} else {
2023-06-19 12:41:52 +08:00
m.config = NewChromiumConfig()
2022-10-04 13:21:05 +08:00
}
2023-06-19 12:41:52 +08:00
m.instance = unsafe.Pointer(_CEFChromium_Create(lcl.CheckPtr(owner)))
m.initDefault()
2023-06-19 12:41:52 +08:00
m.options = NewChromiumOptions(m)
2022-10-04 13:21:05 +08:00
return m
}
// 默认的初始配置
func (m *TCEFChromium) initDefault() {
//通过设置这些首选项,可以降低/避免WebRTC的IP泄漏
m.SetWebRTCIPHandlingPolicy(HpDisableNonProxiedUDP)
m.SetWebRTCMultipleRoutes(STATE_DISABLED)
m.SetWebRTCNonproxiedUDP(STATE_DISABLED)
}
2023-02-20 00:20:01 +08:00
// Instance 组件实例指针
func (m *TCEFChromium) Instance() uintptr {
2023-03-01 20:25:21 +08:00
if m == nil || m.instance == nil {
return 0
}
return uintptr(m.instance)
}
2023-02-20 00:20:01 +08:00
// ExecuteJavaScript
2022-10-04 13:21:05 +08:00
// 执行JS代码
//
// code: js代码
//
2023-02-20 00:20:01 +08:00
// scriptURL: js脚本地址 默认about:blank
2022-10-04 13:21:05 +08:00
//
// startLine: js脚本启始执行行号
func (m *TCEFChromium) ExecuteJavaScript(code, scriptURL string, startLine int32) {
_CEFChromium_ExecuteJavaScript(uintptr(m.instance), code, scriptURL, startLine)
2022-10-04 13:21:05 +08:00
}