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 (
|
2023-06-11 00:10:55 +08:00
|
|
|
"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 ************************** //
|
|
|
|
|
|
|
|
// ResourceRequestHandlerRef -> ICefResourceRequestHandler
|
|
|
|
var ResourceRequestHandlerRef resourceRequestHandler
|
|
|
|
|
|
|
|
type resourceRequestHandler uintptr
|
|
|
|
|
|
|
|
func (*resourceRequestHandler) New() *ICefResourceRequestHandler {
|
|
|
|
var result uintptr
|
2023-06-11 00:10:55 +08:00
|
|
|
imports.Proc(def.ResourceRequestHandlerRef_Create).Call(uintptr(unsafe.Pointer(&result)))
|
2023-05-31 17:41:14 +08:00
|
|
|
if result != 0 {
|
2023-07-03 17:23:26 +08:00
|
|
|
return &ICefResourceRequestHandler{instance: unsafe.Pointer(result)}
|
2023-05-31 17:41:14 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*resourceRequestHandler) NewForChromium(chromium IChromium) *ICefResourceRequestHandler {
|
|
|
|
var result uintptr
|
2023-06-11 00:10:55 +08:00
|
|
|
imports.Proc(def.ResourceRequestHandlerRef_CreateForChromium).Call(chromium.Instance(), uintptr(unsafe.Pointer(&result)))
|
2023-05-31 17:41:14 +08:00
|
|
|
if result != 0 {
|
2023-07-03 17:23:26 +08:00
|
|
|
return &ICefResourceRequestHandler{instance: unsafe.Pointer(result), ct: consts.CtOther}
|
2023-05-31 17:41:14 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ************************** impl ************************** //
|
|
|
|
|
|
|
|
// Instance 实例
|
|
|
|
func (m *ICefResourceRequestHandler) Instance() uintptr {
|
|
|
|
if m == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return uintptr(m.instance)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ICefResourceRequestHandler) Free() {
|
|
|
|
if m.instance != nil {
|
|
|
|
m.base.Free(m.Instance())
|
|
|
|
m.instance = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ICefResourceRequestHandler) IsValid() bool {
|
|
|
|
if m == nil || m.instance == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return m.instance != nil
|
|
|
|
}
|
|
|
|
|
2023-07-03 17:23:26 +08:00
|
|
|
func (m *ICefResourceRequestHandler) IsSelfOwnEvent() bool {
|
|
|
|
return m.ct == consts.CtSelfOwn
|
2023-05-31 17:41:14 +08:00
|
|
|
}
|
|
|
|
|
2023-07-03 17:23:26 +08:00
|
|
|
func (m *ICefResourceRequestHandler) IsOtherEvent() bool {
|
|
|
|
return m.ct == consts.CtOther
|
2023-05-31 17:41:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ICefResourceRequestHandler) SetGetCookieAccessFilter(fn getCookieAccessFilter) {
|
2023-07-03 17:23:26 +08:00
|
|
|
if !m.IsValid() || m.IsOtherEvent() {
|
2023-05-31 17:41:14 +08:00
|
|
|
return
|
|
|
|
}
|
2023-06-11 00:10:55 +08:00
|
|
|
imports.Proc(def.ResourceRequestHandler_GetCookieAccessFilter).Call(m.Instance(), api.MakeEventDataPtr(fn))
|
2023-05-31 17:41:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ICefResourceRequestHandler) SetOnBeforeResourceLoad(fn onBeforeResourceLoad) {
|
2023-07-03 17:23:26 +08:00
|
|
|
if !m.IsValid() || m.IsOtherEvent() {
|
2023-05-31 17:41:14 +08:00
|
|
|
return
|
|
|
|
}
|
2023-06-11 00:10:55 +08:00
|
|
|
imports.Proc(def.ResourceRequestHandler_OnBeforeResourceLoad).Call(m.Instance(), api.MakeEventDataPtr(fn))
|
2023-05-31 17:41:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ICefResourceRequestHandler) SetGetResourceHandler(fn getResourceHandler) {
|
2023-07-03 17:23:26 +08:00
|
|
|
if !m.IsValid() || m.IsOtherEvent() {
|
2023-05-31 17:41:14 +08:00
|
|
|
return
|
|
|
|
}
|
2023-06-11 00:10:55 +08:00
|
|
|
imports.Proc(def.ResourceRequestHandler_GetResourceHandler).Call(m.Instance(), api.MakeEventDataPtr(fn))
|
2023-05-31 17:41:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ICefResourceRequestHandler) SetOnResourceRedirect(fn onResourceRedirect) {
|
2023-07-03 17:23:26 +08:00
|
|
|
if !m.IsValid() || m.IsOtherEvent() {
|
2023-05-31 17:41:14 +08:00
|
|
|
return
|
|
|
|
}
|
2023-06-11 00:10:55 +08:00
|
|
|
imports.Proc(def.ResourceRequestHandler_OnResourceRedirect).Call(m.Instance(), api.MakeEventDataPtr(fn))
|
2023-05-31 17:41:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ICefResourceRequestHandler) SetOnResourceResponse(fn onResourceResponse) {
|
2023-07-03 17:23:26 +08:00
|
|
|
if !m.IsValid() || m.IsOtherEvent() {
|
2023-05-31 17:41:14 +08:00
|
|
|
return
|
|
|
|
}
|
2023-06-11 00:10:55 +08:00
|
|
|
imports.Proc(def.ResourceRequestHandler_OnResourceResponse).Call(m.Instance(), api.MakeEventDataPtr(fn))
|
2023-05-31 17:41:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ICefResourceRequestHandler) SetGetResourceResponseFilter(fn getResourceResponseFilter) {
|
2023-07-03 17:23:26 +08:00
|
|
|
if !m.IsValid() || m.IsOtherEvent() {
|
2023-05-31 17:41:14 +08:00
|
|
|
return
|
|
|
|
}
|
2023-06-11 00:10:55 +08:00
|
|
|
imports.Proc(def.ResourceRequestHandler_GetResourceResponseFilter).Call(m.Instance(), api.MakeEventDataPtr(fn))
|
2023-05-31 17:41:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ICefResourceRequestHandler) SetOnResourceLoadComplete(fn onResourceLoadComplete) {
|
2023-07-03 17:23:26 +08:00
|
|
|
if !m.IsValid() || m.IsOtherEvent() {
|
2023-05-31 17:41:14 +08:00
|
|
|
return
|
|
|
|
}
|
2023-06-11 00:10:55 +08:00
|
|
|
imports.Proc(def.ResourceRequestHandler_OnResourceLoadComplete).Call(m.Instance(), api.MakeEventDataPtr(fn))
|
2023-05-31 17:41:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ICefResourceRequestHandler) SetOnProtocolExecution(fn onProtocolExecution) {
|
2023-07-03 17:23:26 +08:00
|
|
|
if !m.IsValid() || m.IsOtherEvent() {
|
2023-05-31 17:41:14 +08:00
|
|
|
return
|
|
|
|
}
|
2023-06-11 00:10:55 +08:00
|
|
|
imports.Proc(def.ResourceRequestHandler_OnProtocolExecution).Call(m.Instance(), api.MakeEventDataPtr(fn))
|
2023-05-31 17:41:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ************************** events ************************** //
|
|
|
|
|
|
|
|
type getCookieAccessFilter func(browser *ICefBrowser, frame *ICefFrame, request *ICefRequest) (filter *ICefCookieAccessFilter)
|
|
|
|
type onBeforeResourceLoad func(browser *ICefBrowser, frame *ICefFrame, request *ICefRequest, callback *ICefCallback) consts.TCefReturnValue
|
|
|
|
type getResourceHandler func(browser *ICefBrowser, frame *ICefFrame, request *ICefRequest) (resourceHandler *ICefResourceHandler)
|
|
|
|
type onResourceRedirect func(browser *ICefBrowser, frame *ICefFrame, request *ICefRequest, response *ICefResponse) (newUrl string)
|
|
|
|
type onResourceResponse func(browser *ICefBrowser, frame *ICefFrame, request *ICefRequest, response *ICefResponse) bool
|
|
|
|
type getResourceResponseFilter func(browser *ICefBrowser, frame *ICefFrame, request *ICefRequest, response *ICefResponse) (responseFilter *ICefResponseFilter)
|
|
|
|
type onResourceLoadComplete func(browser *ICefBrowser, frame *ICefFrame, request *ICefRequest, status consts.TCefUrlRequestStatus, receivedContentLength int64)
|
|
|
|
type onProtocolExecution func(browser *ICefBrowser, frame *ICefFrame, request *ICefRequest) (allowOsExecution bool)
|
|
|
|
|
|
|
|
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 getCookieAccessFilter:
|
|
|
|
browse := &ICefBrowser{instance: getPtr(0)}
|
|
|
|
frame := &ICefFrame{instance: getPtr(1)}
|
|
|
|
request := &ICefRequest{instance: getPtr(2)}
|
|
|
|
filterPtr := (*uintptr)(getPtr(3))
|
|
|
|
filter := fn.(getCookieAccessFilter)(browse, frame, request)
|
|
|
|
if filter != nil && filter.IsValid() {
|
|
|
|
*filterPtr = filter.Instance()
|
|
|
|
}
|
|
|
|
case onBeforeResourceLoad:
|
|
|
|
browse := &ICefBrowser{instance: getPtr(0)}
|
|
|
|
frame := &ICefFrame{instance: getPtr(1)}
|
|
|
|
request := &ICefRequest{instance: getPtr(2)}
|
|
|
|
callback := &ICefCallback{instance: getPtr(3)}
|
|
|
|
returnValuePtr := (*consts.TCefReturnValue)(getPtr(4))
|
|
|
|
returnValue := fn.(onBeforeResourceLoad)(browse, frame, request, callback)
|
|
|
|
*returnValuePtr = returnValue
|
|
|
|
case getResourceHandler:
|
|
|
|
browse := &ICefBrowser{instance: getPtr(0)}
|
|
|
|
frame := &ICefFrame{instance: getPtr(1)}
|
|
|
|
request := &ICefRequest{instance: getPtr(2)}
|
|
|
|
resourceHandlerPtr := (*uintptr)(getPtr(3))
|
|
|
|
resourceHandler := fn.(getResourceHandler)(browse, frame, request)
|
|
|
|
if resourceHandler != nil && resourceHandler.IsValid() {
|
|
|
|
*resourceHandlerPtr = resourceHandler.Instance()
|
|
|
|
} else {
|
|
|
|
*resourceHandlerPtr = 0
|
|
|
|
}
|
|
|
|
case onResourceRedirect:
|
|
|
|
browse := &ICefBrowser{instance: getPtr(0)}
|
|
|
|
frame := &ICefFrame{instance: getPtr(1)}
|
|
|
|
request := &ICefRequest{instance: getPtr(2)}
|
|
|
|
response := &ICefResponse{instance: getPtr(3)}
|
|
|
|
newUrlPtr := (*uintptr)(getPtr(4))
|
|
|
|
newUrl := fn.(onResourceRedirect)(browse, frame, request, response)
|
|
|
|
*newUrlPtr = api.PascalStr(newUrl)
|
|
|
|
case onResourceResponse:
|
|
|
|
browse := &ICefBrowser{instance: getPtr(0)}
|
|
|
|
frame := &ICefFrame{instance: getPtr(1)}
|
|
|
|
request := &ICefRequest{instance: getPtr(2)}
|
|
|
|
response := &ICefResponse{instance: getPtr(3)}
|
|
|
|
result := (*bool)(getPtr(4))
|
|
|
|
*result = fn.(onResourceResponse)(browse, frame, request, response)
|
|
|
|
case getResourceResponseFilter:
|
|
|
|
browse := &ICefBrowser{instance: getPtr(0)}
|
|
|
|
frame := &ICefFrame{instance: getPtr(1)}
|
|
|
|
request := &ICefRequest{instance: getPtr(2)}
|
|
|
|
response := &ICefResponse{instance: getPtr(3)}
|
|
|
|
responseFilterPtr := (*uintptr)(getPtr(4))
|
|
|
|
responseFilter := fn.(getResourceResponseFilter)(browse, frame, request, response)
|
|
|
|
if responseFilter != nil && responseFilter.IsValid() {
|
|
|
|
*responseFilterPtr = responseFilter.Instance()
|
|
|
|
} else {
|
|
|
|
*responseFilterPtr = 0
|
|
|
|
}
|
|
|
|
case onResourceLoadComplete:
|
|
|
|
browse := &ICefBrowser{instance: getPtr(0)}
|
|
|
|
frame := &ICefFrame{instance: getPtr(1)}
|
|
|
|
request := &ICefRequest{instance: getPtr(2)}
|
|
|
|
status := consts.TCefUrlRequestStatus(getVal(3))
|
|
|
|
receivedContentLength := *(*int64)(getPtr(4))
|
|
|
|
fn.(onResourceLoadComplete)(browse, frame, request, status, receivedContentLength)
|
|
|
|
case onProtocolExecution:
|
|
|
|
browse := &ICefBrowser{instance: getPtr(0)}
|
|
|
|
frame := &ICefFrame{instance: getPtr(1)}
|
|
|
|
request := &ICefRequest{instance: getPtr(2)}
|
|
|
|
allowOsExecution := (*bool)(getPtr(3))
|
|
|
|
*allowOsExecution = fn.(onProtocolExecution)(browse, frame, request)
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|