-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.go
178 lines (167 loc) · 4.47 KB
/
api.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
package webkitgtk
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"text/template"
)
func apiClient(bindings map[string]apiBinding) string {
calls := make(map[string][]string)
for api, binding := range bindings {
for fn := range binding {
calls[api] = append(calls[api], fn)
}
}
var buf strings.Builder
if err := apiClientTmpl.Execute(&buf, struct {
Calls map[string][]string
}{
Calls: calls,
}); err != nil {
panic(err)
}
return buf.String()
}
var apiClientTmpl = template.Must(template.New("api.js").Parse(`(function(document,window) {
class WebkitAPI {
constructor() {
this._id = 0;
this._calls = new Map();
}
resolve(id, data) {
this._calls.get(id)[0](JSON.parse(data));
this._calls.delete(id);
}
reject(id, err) {
this._calls.get(id)[1](err);
this._calls.delete(id);
}
request(api, fn, obj) {
let id = this._id++;
let self = this;
let msg = id.toString()+" "+api+" "+fn;
if (obj) msg += " "+JSON.stringify(obj);
return new Promise((resolve, reject) => {
self._calls.set(id, [resolve, reject]);
window.webkit.messageHandlers.api.postMessage(msg);
});
}
}
window.webkitAPI = new WebkitAPI();
{{range $api, $calls := .Calls}}window.{{$api}} = {};
{{range $calls}}window.{{$api}}.{{.}} = (obj) => window.webkitAPI.request("{{$api}}", "{{.}}", obj);{{end}}{{end}}
})(document.cloneNode(),globalThis.window);`))
func apiHandler(bindings map[string]apiBinding, eval func(string), log func(interface{}, ...interface{})) func(string) {
return func(req string) {
var id, api, fn string
var cur int
for i, c := range req {
if c == ' ' {
if id == "" {
id = req[:i]
cur = i + 1
} else if api == "" {
api = req[cur:i]
cur = i + 1
} else {
fn = req[cur:i]
cur = i + 1
break
}
}
}
if fn == "" {
fn = req[cur:]
cur = len(req)
}
if id == "" || api == "" || fn == "" {
log("api error", "error", "invalid request", "request", req)
return
}
log("api request", "id", id, "api", api, "fn", fn)
binding, ok := bindings[api]
if !ok {
eval("webkitAPI.reject(" + string(id) + ",'api not found')")
return
}
go func() {
reply, err := binding.call(fn, req[cur:])
if err != nil {
log("api reject", "id", id, "error", err)
eval("webkitAPI.reject(" + string(id) + ",'" + err.Error() + "')")
return
}
log("api resolve", "id", id, "reply", reply)
eval("webkitAPI.resolve(" + id + ",'" + reply + "')")
}()
}
}
type apiBinding map[string]func(string) (string, error)
func apiBind(api interface{}) (apiBinding, error) {
value := reflect.ValueOf(api)
if value.Kind() != reflect.Ptr || value.Elem().Kind() != reflect.Struct {
return nil, fmt.Errorf("api is not a struct pointer")
}
binding := make(apiBinding)
for i := 0; i < value.NumMethod(); i++ {
method := value.Method(i)
fn := value.Type().Method(i).Name
fn = strings.ToLower(string(fn[0])) + fn[1:]
if _, exists := binding[fn]; exists {
return nil, fmt.Errorf("function %s already exists", fn)
}
var hasInput, hasOutput bool
var inputType reflect.Type
inputCount := method.Type().NumIn()
if inputCount > 0 {
if inputCount > 1 {
return nil, fmt.Errorf("function has too many inputs")
}
hasInput = true
inputType = method.Type().In(0)
}
outputCount := method.Type().NumOut()
if outputCount < 1 || !method.Type().Out(outputCount-1).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
return nil, fmt.Errorf("function requires an error as last output")
}
if outputCount > 1 {
if outputCount > 2 {
return nil, fmt.Errorf("function has too many outputs")
}
hasOutput = true
}
binding[fn] = func(s string) (string, error) {
var inputs []reflect.Value
if hasInput {
input := reflect.New(inputType).Elem()
if err := json.Unmarshal([]byte(s), input.Addr().Interface()); err != nil {
return "", err
}
inputs = []reflect.Value{input}
}
outputs := method.Call(inputs)
if len(outputs) != outputCount {
return "", fmt.Errorf("call did not return enought values")
}
if err := outputs[outputCount-1].Interface(); err != nil {
return "", err.(error)
}
if hasOutput {
output, err := json.Marshal(outputs[0].Interface())
if err != nil {
return "", err
}
return string(output), nil
}
return "", nil
}
}
return binding, nil
}
func (api apiBinding) call(name string, input string) (string, error) {
if fn, ok := api[name]; ok {
return fn(input)
}
return "", fmt.Errorf("function %s not found", name)
}