forked from cztomczak/cef2go
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcefRequestHandler.go
235 lines (193 loc) · 6.43 KB
/
cefRequestHandler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright (c) 2014 The cef2go authors. All rights reserved.
// License: BSD 3-clause.
// Website: https://github.com/fromkeith/cef2go
package cef2go
/*
#include <stdlib.h>
#include "include/capi/cef_client_capi.h"
#include "include/capi/cef_request_handler_capi.h"
extern void initialize_request_handler(struct _cef_request_handler_t * requestHandler);
extern void cef_allow_certificate_error_callback_t_cont(struct _cef_allow_certificate_error_callback_t* self, int allow);
*/
import "C"
import (
"unsafe"
)
var (
requestHandlerMap = make(map[unsafe.Pointer]RequestHandler)
)
// Wraps the callbacks done to _cef_request_handler_t (partial implementation of callbacks)
type RequestHandler interface {
/** Triggered before browsing to page. Return 1 to cancel transition. 0 to continue. */
OnBeforeBrowse(browser CefBrowserT, frame CefFrameT, request CefRequestT, isRedirect int) int
OnBeforeResourceLoad(browser CefBrowserT, frame CefFrameT, request CefRequestT) int
/** Triggered when we encounter an invalid SSL cert. Return 1 to and call callback.cont() to continue or cancel the request.
Return 0 to immediatly cancel the request.*/
OnCertificateError(errorCode CefErrorCode, requestUrl string, errorCallback CefCertErrorCallbackT) int
GetRequestHandlerT() RequestHandlerT
}
type RequestHandlerT struct {
CStruct *C.struct__cef_request_handler_t
}
func (r RequestHandlerT) AddRef() {
AddRef(unsafe.Pointer(r.CStruct))
}
func (r RequestHandlerT) Release() {
Release(unsafe.Pointer(r.CStruct))
}
type CefCertErrorCallbackT struct {
CStruct *C.struct__cef_allow_certificate_error_callback_t
}
func (c CefCertErrorCallbackT) Release() {
Release(unsafe.Pointer(c.CStruct))
}
func (c CefCertErrorCallbackT) Cont(allow int) {
C.cef_allow_certificate_error_callback_t_cont(c.CStruct, C.int(allow))
}
//export go_OnBeforeBrowse
func go_OnBeforeBrowse(
self *C.struct__cef_request_handler_t,
browser *C.struct__cef_browser_t,
frame *C.struct__cef_frame_t,
request *C.struct__cef_request_t,
is_redirect int) int {
if handler, ok := requestHandlerMap[unsafe.Pointer(self)]; ok {
return handler.OnBeforeBrowse(
CefBrowserT{browser},
CefFrameT{frame},
CefRequestT{request},
is_redirect,
)
}
CefBrowserT{browser}.Release()
CefRequestT{request}.Release()
CefFrameT{frame}.Release()
return 0
}
//export go_OnBeforeResourceLoad
func go_OnBeforeResourceLoad(
self *C.struct__cef_request_handler_t,
browser *C.struct__cef_browser_t,
frame *C.struct__cef_frame_t,
request *C.struct__cef_request_t) int {
if handler, ok := requestHandlerMap[unsafe.Pointer(self)]; ok {
return handler.OnBeforeResourceLoad(
CefBrowserT{browser},
CefFrameT{frame},
CefRequestT{request},
)
}
CefBrowserT{browser}.Release()
CefFrameT{frame}.Release()
CefRequestT{request}.Release()
return 0
}
//export go_GetResourceHandler
func go_GetResourceHandler(
self *C.struct__cef_request_handler_t,
browser *C.struct__cef_browser_t,
frame *C.struct__cef_frame_t,
request *C.struct__cef_request_t) *C.struct__cef_resource_handler_t {
CefBrowserT{browser}.Release()
CefFrameT{frame}.Release()
CefRequestT{request}.Release()
return nil
}
//export go_OnResourceRedirect
func go_OnResourceRedirect(
self *C.struct__cef_request_handler_t,
browser *C.struct__cef_browser_t,
frame *C.struct__cef_frame_t,
old_url * C.char,
new_url * C.char) {
CefBrowserT{browser}.Release()
CefFrameT{frame}.Release()
}
//export go_GetAuthCredentials
func go_GetAuthCredentials(
self *C.struct__cef_request_handler_t,
browser *C.struct__cef_browser_t,
frame *C.struct__cef_frame_t,
isProxy int,
host * C.char,
port int,
realm * C.char,
scheme * C.char,
callback *C.struct__cef_auth_callback_t) int {
CefBrowserT{browser}.Release()
CefFrameT{frame}.Release()
Release(unsafe.Pointer(callback))
return 1
}
//export go_OnQuotaRequest
func go_OnQuotaRequest(
self *C.struct__cef_request_handler_t,
browser *C.struct__cef_browser_t,
origin_url *C.char,
new_size int64,
callback *C.struct__cef_quota_callback_t) int {
CefBrowserT{browser}.Release()
Release(unsafe.Pointer(callback))
return 1
}
//export go_OnProtocolExecution
func go_OnProtocolExecution(
self *C.struct__cef_request_handler_t,
browser *C.struct__cef_browser_t,
url *C.char,
allow_os_execution unsafe.Pointer) {
Logger.Infof("go_OnProtocolExecution")
CefBrowserT{browser}.Release()
}
//export go_OnCertificateError
func go_OnCertificateError(
self *C.struct__cef_request_handler_t,
cert_error int, //C.enum_cef_errorcode_t,
request_url *C.char,
callback *C.struct__cef_allow_certificate_error_callback_t) int {
if handler, ok := requestHandlerMap[unsafe.Pointer(self)]; ok {
return handler.OnCertificateError(
CefErrorCode(cert_error),
C.GoString(request_url),
CefCertErrorCallbackT{callback},
)
}
CefCertErrorCallbackT{callback}.Release()
return 0
}
//export go_OnBeforePluginLoad
func go_OnBeforePluginLoad(
self *C.struct__cef_request_handler_t,
browser *C.struct__cef_browser_t,
url *C.char,
policy_url *C.char,
info *C.struct__cef_web_plugin_info_t) int {
CefBrowserT{browser}.Release()
Release(unsafe.Pointer(info))
return 1
}
//export go_OnPluginCrashed
func go_OnPluginCrashed(
self *C.struct__cef_request_handler_t,
browser *C.struct__cef_browser_t,
plugin_path *C.char) {
CefBrowserT{browser}.Release()
}
//export go_OnRenderProcessTerminated
func go_OnRenderProcessTerminated(
self *C.struct__cef_request_handler_t,
browser *C.struct__cef_browser_t,
status int, //C.enum_cef_termination_status_t
) {
CefBrowserT{browser}.Release()
}
func NewRequestHandlerT(request RequestHandler) RequestHandlerT {
var handler RequestHandlerT
handler.CStruct = (*C.struct__cef_request_handler_t)(
C.calloc(1, C.sizeof_struct__cef_request_handler_t))
C.initialize_request_handler(handler.CStruct)
go_AddRef(unsafe.Pointer(handler.CStruct))
Logger.Infof("_RequestHandler: %x", unsafe.Pointer(handler.CStruct))
requestHandlerMap[unsafe.Pointer(handler.CStruct)] = request
return handler
}