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-20 00:20:01 +08:00
|
|
|
|
// CEF Chromium组件
|
2022-10-04 13:21:05 +08:00
|
|
|
|
package cef
|
|
|
|
|
|
|
|
|
|
import (
|
2022-10-04 22:34:57 +08:00
|
|
|
|
. "github.com/energye/energy/consts"
|
2022-10-04 13:21:05 +08:00
|
|
|
|
"github.com/energye/golcl/lcl"
|
2023-01-16 22:29:28 +08:00
|
|
|
|
"github.com/energye/golcl/lcl/types"
|
2022-10-04 13:21:05 +08:00
|
|
|
|
"sync"
|
|
|
|
|
"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 {
|
2022-12-07 22:19:43 +08:00
|
|
|
|
*lcl.TComponent
|
2023-01-16 22:29:28 +08:00
|
|
|
|
instance unsafe.Pointer
|
|
|
|
|
cfg *tCefChromiumConfig
|
2023-03-01 14:18:39 +08:00
|
|
|
|
browser *ICefBrowser
|
2023-01-16 22:29:28 +08:00
|
|
|
|
emitLock *sync.Mutex
|
|
|
|
|
browserHandle types.HWND
|
|
|
|
|
widgetHandle types.HWND
|
|
|
|
|
renderHandle types.HWND
|
2023-03-16 09:13:31 +08:00
|
|
|
|
isSending bool
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 00:20:01 +08:00
|
|
|
|
// NewChromium 创建一个新的 TCEFChromium
|
2022-12-01 15:24:22 +08:00
|
|
|
|
func NewChromium(owner lcl.IComponent, config *tCefChromiumConfig) IChromium {
|
2022-10-04 13:21:05 +08:00
|
|
|
|
m := new(TCEFChromium)
|
|
|
|
|
if config != nil {
|
|
|
|
|
m.cfg = config
|
|
|
|
|
} else {
|
|
|
|
|
m.cfg = NewChromiumConfig()
|
|
|
|
|
}
|
2022-12-05 12:07:58 +08:00
|
|
|
|
m.instance = unsafe.Pointer(_CEFChromium_Create(lcl.CheckPtr(owner), uintptr(unsafe.Pointer(m.cfg))))
|
2022-10-04 13:21:05 +08:00
|
|
|
|
m.emitLock = new(sync.Mutex)
|
2023-02-04 23:52:15 +08:00
|
|
|
|
m.initDefault()
|
2022-10-04 13:21:05 +08:00
|
|
|
|
return m
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-04 23:52:15 +08:00
|
|
|
|
// 默认的初始配置
|
|
|
|
|
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 组件实例指针
|
2022-12-05 12:07:58 +08:00
|
|
|
|
func (m *TCEFChromium) Instance() uintptr {
|
2023-03-01 20:25:21 +08:00
|
|
|
|
if m == nil || m.instance == nil {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
2022-12-05 12:07:58 +08:00
|
|
|
|
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) {
|
2022-12-05 12:07:58 +08:00
|
|
|
|
_CEFChromium_ExecuteJavaScript(uintptr(m.instance), code, scriptURL, startLine)
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
2023-03-14 12:13:30 +08:00
|
|
|
|
//// Emit
|
|
|
|
|
//// 触发JS监听的事件-异步执行
|
|
|
|
|
////
|
|
|
|
|
//// EmitTarget 接收目标, nil:mainBrowser&mainFrame, 可传递browser和指定浏览器窗口,JS监听事件的接收
|
|
|
|
|
//func (m *TCEFChromium) Emit(eventName string, args ipc.IArgumentList, target ipc.IEmitTarget) ProcessMessageError {
|
|
|
|
|
// if eventName == "" {
|
|
|
|
|
// return PMErr_NAME_IS_NULL
|
|
|
|
|
// }
|
|
|
|
|
// m.emitLock.Lock()
|
|
|
|
|
// defer m.emitLock.Unlock()
|
|
|
|
|
// var (
|
|
|
|
|
// browseId int32
|
|
|
|
|
// frameId int64
|
|
|
|
|
// )
|
|
|
|
|
// if args == nil {
|
|
|
|
|
// args = ipc.NewArgumentList()
|
|
|
|
|
// }
|
|
|
|
|
// if target == nil {
|
|
|
|
|
// bsr := m.Browser()
|
|
|
|
|
// browseId = bsr.Identifier()
|
|
|
|
|
// frameId = bsr.MainFrame().Identifier()
|
|
|
|
|
// } else {
|
|
|
|
|
// browseId = target.GetBrowserId()
|
|
|
|
|
// frameId = target.GetFrameId()
|
|
|
|
|
// if m.BrowserById(browseId).GetFrameById(frameId) == nil {
|
|
|
|
|
// return PMErr_NOT_FOUND_FRAME
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// var idx = args.Size()
|
|
|
|
|
// args.SetInt32(idx, int32(Tm_Async))
|
|
|
|
|
// args.SetInt32(idx+1, 0)
|
|
|
|
|
// args.SetString(idx+2, eventName, true)
|
|
|
|
|
// m.browseEmitJsOnEvent(browseId, frameId, ipc.Ln_IPC_GoEmitJS, args)
|
|
|
|
|
// return PME_OK
|
|
|
|
|
//}
|
2022-10-04 13:21:05 +08:00
|
|
|
|
//
|
2023-03-14 12:13:30 +08:00
|
|
|
|
//// EmitAndCallback
|
|
|
|
|
//// 触发JS监听的事件-异步执行-带回调
|
|
|
|
|
////
|
|
|
|
|
//// EmitTarget 接收目标, nil = mainBrowser mainFrame
|
|
|
|
|
//func (m *TCEFChromium) EmitAndCallback(eventName string, args ipc.IArgumentList, target ipc.IEmitTarget, callback ipc.IPCCallback) ProcessMessageError {
|
|
|
|
|
// if eventName == "" {
|
|
|
|
|
// return PMErr_NAME_IS_NULL
|
|
|
|
|
// }
|
|
|
|
|
// m.emitLock.Lock()
|
|
|
|
|
// defer m.emitLock.Unlock()
|
|
|
|
|
// var (
|
|
|
|
|
// browseId int32
|
|
|
|
|
// frameId int64
|
|
|
|
|
// ipcId = executeJS.msgID.New()
|
|
|
|
|
// idx = args.Size()
|
|
|
|
|
// )
|
|
|
|
|
// if args == nil {
|
|
|
|
|
// args = ipc.NewArgumentList()
|
|
|
|
|
// }
|
|
|
|
|
// if target == nil {
|
|
|
|
|
// bsr := m.Browser()
|
|
|
|
|
// browseId = bsr.Identifier()
|
|
|
|
|
// frameId = bsr.MainFrame().Identifier()
|
|
|
|
|
// } else {
|
|
|
|
|
// browseId = target.GetBrowserId()
|
|
|
|
|
// frameId = target.GetFrameId()
|
|
|
|
|
// if m.BrowserById(browseId).GetFrameById(frameId) == nil {
|
|
|
|
|
// return PMErr_NOT_FOUND_FRAME
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// args.SetInt32(idx, int32(Tm_Callback))
|
|
|
|
|
// args.SetInt32(idx+1, ipcId)
|
|
|
|
|
// args.SetString(idx+2, eventName, true)
|
|
|
|
|
// executeJS.emitCallback.EmitCollection.Store(ipcId, callback)
|
|
|
|
|
// m.browseEmitJsOnEvent(browseId, frameId, ipc.Ln_IPC_GoEmitJS, args)
|
|
|
|
|
// return PME_OK
|
|
|
|
|
//}
|
2022-10-04 13:21:05 +08:00
|
|
|
|
//
|
2023-03-14 12:13:30 +08:00
|
|
|
|
//// EmitAndReturn
|
|
|
|
|
//// 触发JS监听的事件-同步执行-阻塞UI主线程
|
|
|
|
|
////
|
|
|
|
|
//// 使用不当会造成 UI线程 锁死,一搬不在与JS监听中使用,与其它子进程通信时使用
|
|
|
|
|
////
|
|
|
|
|
//// EmitTarget 接收目标, nil = mainBrowser mainFrame
|
|
|
|
|
//func (m *TCEFChromium) EmitAndReturn(eventName string, args ipc.IArgumentList, target ipc.IEmitTarget) (ipc.IIPCContext, ProcessMessageError) {
|
|
|
|
|
// if eventName == "" {
|
|
|
|
|
// return nil, PMErr_NAME_IS_NULL
|
|
|
|
|
// }
|
|
|
|
|
// m.emitLock.Lock()
|
|
|
|
|
// defer m.emitLock.Unlock()
|
|
|
|
|
// var (
|
|
|
|
|
// browseId int32
|
|
|
|
|
// frameId int64
|
|
|
|
|
// ipcId = executeJS.msgID.New()
|
|
|
|
|
// idx = args.Size()
|
|
|
|
|
// )
|
|
|
|
|
// if args == nil {
|
|
|
|
|
// args = ipc.NewArgumentList()
|
|
|
|
|
// }
|
|
|
|
|
// if target == nil {
|
|
|
|
|
// bsr := m.Browser()
|
|
|
|
|
// browseId = bsr.Identifier()
|
|
|
|
|
// frameId = bsr.MainFrame().Identifier()
|
|
|
|
|
// } else {
|
|
|
|
|
// browseId = target.GetBrowserId()
|
|
|
|
|
// frameId = target.GetFrameId()
|
|
|
|
|
// if m.BrowserById(browseId).GetFrameById(frameId) == nil {
|
|
|
|
|
// return nil, PMErr_NOT_FOUND_FRAME
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// args.SetInt32(idx, int32(Tm_Sync))
|
|
|
|
|
// args.SetInt32(idx+1, ipcId)
|
|
|
|
|
// args.SetString(idx+2, eventName, true)
|
|
|
|
|
// var callback = func(emitAsync *ipc.EmitSyncCollection, ipcId int32) ipc.IIPCContext {
|
|
|
|
|
// emitAsync.Mutex.Lock()
|
|
|
|
|
// defer emitAsync.Mutex.Unlock()
|
|
|
|
|
// var chn = make(chan ipc.IIPCContext)
|
|
|
|
|
// var ret ipc.IIPCContext
|
|
|
|
|
// emitAsync.EmitCollection.Store(ipcId, chn)
|
|
|
|
|
// ret = <-chn //锁住当前线程
|
|
|
|
|
// executeJS.emitSync.EmitCollection.Delete(ipcId)
|
|
|
|
|
// return ret
|
|
|
|
|
// }
|
|
|
|
|
// m.browseEmitJsOnEvent(browseId, frameId, ipc.Ln_IPC_GoEmitJS, args)
|
|
|
|
|
// return callback(executeJS.emitSync, ipcId), PME_OK
|
|
|
|
|
//}
|