-
Notifications
You must be signed in to change notification settings - Fork 21
/
callback.go
235 lines (220 loc) · 6.22 KB
/
callback.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
235
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
package callback
import (
goJSON "encoding/json"
"github.com/energye/energy/v2/cef/ipc/context"
"github.com/energye/energy/v2/pkgs/json"
"reflect"
)
var argumentChannelType = reflect.TypeOf(new(IChannel)).Elem()
// EmitContextCallback IPC context callback
type EmitContextCallback func(context context.IContext)
// IChannel
//
// The channel ID of the parameter type callback function
// Used for listening to events and receiving parameters from the event channel source
type IChannel interface {
BrowserId() int32 //Receive Browser Window ID
ChannelId() int64 //Receive Channel ID
}
// Callback IPC Listening callback function
// 1. Callback Function - Context Mode
// 2. Callback Function - Argument Mode
type Callback struct {
IsAsync bool // JS ipc.emit mode
Context *ContextCallback // 1 Context
Argument *ArgumentCallback // 2 Argument
}
// ContextCallback
//
// Callback function with context
type ContextCallback struct {
Callback EmitContextCallback
}
// ArgumentCallback
//
// Callback function with parameters
type ArgumentCallback struct {
Callback *reflect.Value
}
type argumentChannel struct {
browserId int32
channelId int64
}
func (m *argumentChannel) BrowserId() int32 {
return m.browserId
}
func (m *argumentChannel) ChannelId() int64 {
return m.channelId
}
// ContextCallback
//
// Return context parameter callback
func (m *Callback) ContextCallback() *ContextCallback {
if m.Context == nil {
return nil
}
return m.Context
}
// Invoke context function
func (m *ContextCallback) Invoke(context context.IContext) {
// call
m.Callback(context)
resultValues := context.Replay().Result()
if len(resultValues) > 0 {
// call result
resultArgument := make([]interface{}, len(resultValues), len(resultValues))
for i, result := range resultValues {
switch result.(type) {
case error:
resultArgument[i] = result.(error).Error()
default:
resultArgument[i] = result
}
}
// result
context.Result(resultArgument...)
} else {
context.Result(nil)
}
}
// ArgumentCallback
//
// return argument list callback function
func (m *Callback) ArgumentCallback() *ArgumentCallback {
if m.Argument == nil {
return nil
}
return m.Argument
}
// Invoke argument list function
func (m *ArgumentCallback) Invoke(context context.IContext) {
var (
argsList json.JSONArray
argsSize int
inArgsValues []reflect.Value
)
argsList = context.ArgumentList()
if argsList != nil {
argsSize = argsList.Size()
}
rt := m.Callback.Type()
inArgsCount := rt.NumIn()
inArgsValues = make([]reflect.Value, inArgsCount)
var inIdx = 0
for i := 0; i < inArgsCount; i++ {
inType := rt.In(i)
if inIdx < argsSize {
argsValue := argsList.GetByIndex(inIdx)
if argsValue != nil {
switch inType.Kind() {
case reflect.String:
inArgsValues[i] = reflect.ValueOf(argsValue.String())
case reflect.Int:
inArgsValues[i] = reflect.ValueOf(argsValue.Int())
case reflect.Int8:
inArgsValues[i] = reflect.ValueOf(int8(argsValue.Int()))
case reflect.Int16:
inArgsValues[i] = reflect.ValueOf(int16(argsValue.Int()))
case reflect.Int32:
inArgsValues[i] = reflect.ValueOf(int32(argsValue.Int()))
case reflect.Int64:
inArgsValues[i] = reflect.ValueOf(int64(argsValue.Int()))
case reflect.Uint:
inArgsValues[i] = reflect.ValueOf(uint(argsValue.Int()))
case reflect.Uint8:
inArgsValues[i] = reflect.ValueOf(uint8(argsValue.Int()))
case reflect.Uint16:
inArgsValues[i] = reflect.ValueOf(uint16(argsValue.Int()))
case reflect.Uint32:
inArgsValues[i] = reflect.ValueOf(uint32(argsValue.Int()))
case reflect.Uint64:
inArgsValues[i] = reflect.ValueOf(uint64(argsValue.Int()))
case reflect.Uintptr:
inArgsValues[i] = reflect.ValueOf(uintptr(argsValue.Int()))
case reflect.Float32:
inArgsValues[i] = reflect.ValueOf(float32(argsValue.Float()))
case reflect.Float64:
inArgsValues[i] = reflect.ValueOf(argsValue.Float())
case reflect.Bool:
inArgsValues[i] = reflect.ValueOf(argsValue.Bool())
case reflect.Struct:
if argsValue.IsObject() {
// struct
if jsonBytes := argsValue.Bytes(); jsonBytes != nil {
v := reflect.New(inType)
if err := goJSON.Unmarshal(jsonBytes, v.Interface()); err == nil {
inArgsValues[i] = v.Elem()
}
}
}
case reflect.Map:
if argsValue.IsObject() {
// map key=string : value != interface
if inType.Elem().Kind() != reflect.Interface {
if jsonBytes := argsValue.Bytes(); jsonBytes != nil {
vv := reflect.New(inType)
if err := goJSON.Unmarshal(jsonBytes, vv.Interface()); err == nil {
inArgsValues[i] = vv.Elem()
}
}
} else {
inArgsValues[i] = reflect.ValueOf(argsValue.Data())
}
}
case reflect.Slice:
if argsValue.IsArray() {
// slice value != interface
if inType.Elem().Kind() != reflect.Interface {
if jsonBytes := argsValue.Bytes(); jsonBytes != nil {
vv := reflect.New(inType)
if err := goJSON.Unmarshal(jsonBytes, vv.Interface()); err == nil {
inArgsValues[i] = vv.Elem()
}
}
} else {
inArgsValues[i] = reflect.ValueOf(argsValue.Data())
}
}
}
}
}
if !inArgsValues[i].IsValid() {
newValue := reflect.New(inType).Elem()
if newValue.Type().Implements(argumentChannelType) {
newValue.Set(reflect.ValueOf(&argumentChannel{browserId: context.BrowserId(), channelId: context.FrameId()}))
}
inArgsValues[i] = newValue
} else {
inIdx++
}
}
// call
resultValues := m.Callback.Call(inArgsValues)
if len(resultValues) > 0 {
// call result
resultArgument := make([]interface{}, len(resultValues), len(resultValues))
for i, result := range resultValues {
res := result.Interface()
switch res.(type) {
case error:
resultArgument[i] = res.(error).Error()
default:
resultArgument[i] = res
}
}
// result
context.Result(resultArgument...)
} else {
// result nil
context.Result(nil)
}
}