forked from cztomczak/cef2go
-
Notifications
You must be signed in to change notification settings - Fork 7
/
cefDisplayHandler.go
146 lines (124 loc) · 3.8 KB
/
cefDisplayHandler.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
// 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_display_handler_capi.h"
extern void initialize_display_handler(struct _cef_display_handler_t * displayHandler);
*/
import "C"
import (
"unsafe"
)
type DisplayHandler interface {
OnAddressChange(browser CefBrowserT, frame CefFrameT, url string)
OnTitleChange(browser CefBrowserT, title string)
OnToolTip(browser CefBrowserT, text string) bool
OnStatusMessage(browser CefBrowserT, value string)
OnConsoleMessage(browser CefBrowserT, message, source string, line int) bool
GetDisplayHandlerT() DisplayHandlerT
}
var (
displayHandlerMap = make(map[unsafe.Pointer]DisplayHandler)
)
type DisplayHandlerT struct {
CStruct *C.struct__cef_display_handler_t
}
func (r DisplayHandlerT) AddRef() {
AddRef(unsafe.Pointer(r.CStruct))
}
func (r DisplayHandlerT) Release() {
Release(unsafe.Pointer(r.CStruct))
}
//export go_OnAddressChange
func go_OnAddressChange(self *C.struct__cef_display_handler_t,
browser * C.struct__cef_browser_t,
frame *C.struct__cef_frame_t,
url *C.char) {
if handler, ok := displayHandlerMap[unsafe.Pointer(self)]; ok {
handler.OnAddressChange(
CefBrowserT{browser},
CefFrameT{frame},
C.GoString(url),
)
return
}
CefBrowserT{browser}.Release()
CefFrameT{frame}.Release()
}
//export go_OnTitleChange
func go_OnTitleChange(self *C.struct__cef_display_handler_t,
browser * C.struct__cef_browser_t,
title *C.char) {
if handler, ok := displayHandlerMap[unsafe.Pointer(self)]; ok {
handler.OnTitleChange(
CefBrowserT{browser},
C.GoString(title),
)
return
}
CefBrowserT{browser}.Release()
}
//export go_OnTooltip
func go_OnTooltip(self *C.struct__cef_display_handler_t,
browser * C.struct__cef_browser_t,
text *C.char) int {
if handler, ok := displayHandlerMap[unsafe.Pointer(self)]; ok {
bVal := handler.OnToolTip(
CefBrowserT{browser},
C.GoString(text),
)
if bVal {
return 1
}
return 0
}
CefBrowserT{browser}.Release()
return 0
}
//export go_OnStatusMessage
func go_OnStatusMessage(self *C.struct__cef_display_handler_t,
browser * C.struct__cef_browser_t,
value *C.char) {
if handler, ok := displayHandlerMap[unsafe.Pointer(self)]; ok {
handler.OnStatusMessage(
CefBrowserT{browser},
C.GoString(value),
)
return
}
CefBrowserT{browser}.Release()
}
//export go_OnConsoleMessage
func go_OnConsoleMessage(self *C.struct__cef_display_handler_t,
browser * C.struct__cef_browser_t,
message *C.char,
source *C.char,
line C.int) int {
if handler, ok := displayHandlerMap[unsafe.Pointer(self)]; ok {
bVal := handler.OnConsoleMessage(
CefBrowserT{browser},
C.GoString(message),
C.GoString(source),
int(line),
)
if bVal {
return 1
}
return 0
}
CefBrowserT{browser}.Release()
return 0
}
func NewDisplayHandlerT(display DisplayHandler) DisplayHandlerT {
var handler DisplayHandlerT
handler.CStruct = (*C.struct__cef_display_handler_t)(
C.calloc(1, C.sizeof_struct__cef_display_handler_t))
C.initialize_display_handler(handler.CStruct)
go_AddRef(unsafe.Pointer(handler.CStruct))
Logger.Infof("_DisplayHandler: %x", unsafe.Pointer(handler.CStruct))
displayHandlerMap[unsafe.Pointer(handler.CStruct)] = display
return handler
}