energy/cef/types-request-context-handler.go

129 lines
4.0 KiB
Go
Raw Normal View History

2023-05-31 17:41:14 +08:00
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
package cef
import (
"github.com/energye/energy/v2/cef/internal/def"
2023-05-31 18:00:34 +08:00
"github.com/energye/energy/v2/common/imports"
"github.com/energye/energy/v2/consts"
2023-05-31 17:41:14 +08:00
"github.com/energye/golcl/lcl"
"github.com/energye/golcl/lcl/api"
"unsafe"
)
// ************************** creates ************************** //
// RequestContextHandlerRef -> ICefRequestContextHandler
var RequestContextHandlerRef requestContextHandler
type requestContextHandler uintptr
func (*requestContextHandler) New() *ICefRequestContextHandler {
var result uintptr
imports.Proc(def.RequestContextHandlerRef_Create).Call(uintptr(unsafe.Pointer(&result)))
2023-05-31 17:41:14 +08:00
if result != 0 {
return &ICefRequestContextHandler{instance: unsafe.Pointer(result), ct: consts.CtTClient}
}
return nil
}
func (*requestContextHandler) NewForChromium(chromium IChromium) *ICefRequestContextHandler {
if chromium == nil || chromium.Instance() == 0 {
return nil
}
var result uintptr
imports.Proc(def.RequestContextHandlerRef_CreateForChromium).Call(chromium.Instance(), uintptr(unsafe.Pointer(&result)))
2023-05-31 17:41:14 +08:00
if result != 0 {
return &ICefRequestContextHandler{instance: unsafe.Pointer(result), ct: consts.CtChromium}
}
return nil
}
// ************************** impl ************************** //
// Instance 实例
func (m *ICefRequestContextHandler) Instance() uintptr {
if m == nil {
return 0
}
return uintptr(m.instance)
}
func (m *ICefRequestContextHandler) Free() {
if m.instance != nil {
m.base.Free(m.Instance())
m.instance = nil
}
}
func (m *ICefRequestContextHandler) IsValid() bool {
if m == nil || m.instance == nil {
return false
}
return m.instance != nil
}
func (m *ICefRequestContextHandler) IsTClientEvent() bool {
return m.ct == consts.CtTClient
}
func (m *ICefRequestContextHandler) IsChromiumEvent() bool {
return m.ct == consts.CtChromium
}
func (m *ICefRequestContextHandler) SetOnRequestContextInitialized(fn onRequestContextInitialized) {
if !m.IsValid() || m.IsChromiumEvent() {
return
}
imports.Proc(def.RequestContextHandler_OnRequestContextInitialized).Call(m.Instance(), api.MakeEventDataPtr(fn))
2023-05-31 17:41:14 +08:00
}
func (m *ICefRequestContextHandler) SetGetResourceRequestHandler(fn getResourceRequestHandler) {
if !m.IsValid() || m.IsChromiumEvent() {
return
}
imports.Proc(def.RequestContextHandler_GetResourceRequestHandler).Call(m.Instance(), api.MakeEventDataPtr(fn))
2023-05-31 17:41:14 +08:00
}
type onRequestContextInitialized func(requestContext *ICefRequestContext)
type getResourceRequestHandler func(browser *ICefBrowser, frame *ICefFrame, request *ICefRequest, isNavigation, isDownload bool, requestInitiator string) (disableDefaultHandling bool, resourceRequestHandler *ICefResourceRequestHandler)
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 onRequestContextInitialized:
requestContext := &ICefRequestContext{instance: getPtr(0)}
fn.(onRequestContextInitialized)(requestContext)
case getResourceRequestHandler:
browse := &ICefBrowser{instance: getPtr(0)}
frame := &ICefFrame{instance: getPtr(1)}
request := &ICefRequest{instance: getPtr(2)}
isNavigation, isDownload := api.GoBool(getVal(3)), api.GoBool(getVal(4))
requestInitiator := api.GoStr(getVal(5))
disableDefaultHandlingPtr := (*bool)(getPtr(6))
resourceRequestHandlerPtr := (*uintptr)(getPtr(7))
disableDefaultHandling, resourceRequestHandler := fn.(getResourceRequestHandler)(browse, frame, request, isNavigation, isDownload, requestInitiator)
*disableDefaultHandlingPtr = disableDefaultHandling
if resourceRequestHandler != nil && resourceRequestHandler.IsValid() {
*resourceRequestHandlerPtr = resourceRequestHandler.Instance()
} else {
*resourceRequestHandlerPtr = 0
}
default:
return false
}
return true
})
}