-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
cdp.go
276 lines (253 loc) · 6.11 KB
/
cdp.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package runn
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"reflect"
"sync"
"time"
"github.com/chromedp/chromedp"
"github.com/k1LoW/donegroup"
)
const cdpNewKey = "new"
const (
cdpTimeoutByStep = 60 * time.Second
cdpWindowWidth = 1920
cdpWindowHeight = 1080
)
type cdpRunner struct {
name string
ctx context.Context //nostyle:contexts
cancel context.CancelFunc
store map[string]any
opts []chromedp.ExecAllocatorOption
timeoutByStep time.Duration
mu sync.Mutex
// operatorID - The id of the operator for which the runner is defined.
operatorID string
}
type CDPActions []CDPAction
type CDPAction struct {
Fn string
Args map[string]any
}
func newCDPRunner(name, remote string) (*cdpRunner, error) {
if remote != cdpNewKey {
return nil, errors.New("remote connect mode is planned, but not yet implemented")
}
opts := append(chromedp.DefaultExecAllocatorOptions[:],
chromedp.WindowSize(cdpWindowWidth, cdpWindowHeight),
)
if os.Getenv("RUNN_DISABLE_HEADLESS") != "" {
opts = append(opts,
chromedp.Flag("headless", false),
chromedp.Flag("hide-scrollbars", false),
chromedp.Flag("mute-audio", false),
)
}
return &cdpRunner{
name: name,
store: map[string]any{},
opts: opts,
timeoutByStep: cdpTimeoutByStep,
}, nil
}
func (rnr *cdpRunner) Close() error {
rnr.mu.Lock()
defer rnr.mu.Unlock()
if rnr.cancel == nil {
return nil
}
rnr.cancel()
rnr.ctx = nil
rnr.cancel = nil
return nil
}
func (rnr *cdpRunner) Renew() error {
if err := rnr.Close(); err != nil {
return err
}
rnr.store = map[string]any{}
return nil
}
func (rnr *cdpRunner) Run(ctx context.Context, s *step) error {
o := s.parent
cas, err := parseCDPActions(s.cdpActions, o.expandBeforeRecord)
if err != nil {
return fmt.Errorf("failed to parse: %w", err)
}
if err := rnr.run(ctx, cas, s); err != nil {
return fmt.Errorf("failed to run: %w", err)
}
return nil
}
func (rnr *cdpRunner) run(ctx context.Context, cas CDPActions, s *step) error {
o := s.parent
if rnr.ctx == nil {
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), rnr.opts...)
ctxx, _ := chromedp.NewContext(allocCtx)
rnr.ctx = ctxx
rnr.cancel = cancel
// Merge run() function context and runner (chrome) context
context.AfterFunc(ctxx, func() {
_ = rnr.Close()
})
if err := donegroup.Cleanup(ctx, func() error {
// In the case of Reused runners, leave the cleanup to the main cleanup
if o.id != rnr.operatorID {
return nil
}
return rnr.Renew()
}); err != nil {
return err
}
}
o.capturers.captureCDPStart(rnr.name)
defer o.capturers.captureCDPEnd(rnr.name)
// Set a timeout (cdpTimeoutByStep) for each step because Chrome operations may get stuck depending on the actions: specified.
called := false
defer func() {
called = true
}()
timer := time.NewTimer(rnr.timeoutByStep)
go func() {
<-timer.C
if !called {
rnr.Close()
}
}()
before := []chromedp.Action{
chromedp.EmulateViewport(cdpWindowWidth, cdpWindowHeight),
}
if err := chromedp.Run(rnr.ctx, before...); err != nil {
return err
}
for i, ca := range cas {
o.capturers.captureCDPAction(ca)
k, fn, err := findCDPFn(ca.Fn)
if err != nil {
return fmt.Errorf("actions[%d] error: %w", i, err)
}
if k == "latestTab" {
infos, err := chromedp.Targets(rnr.ctx)
if err != nil {
return err
}
latestCtx, _ := chromedp.NewContext(rnr.ctx, chromedp.WithTargetID(infos[0].TargetID))
rnr.ctx = latestCtx
continue
}
as, err := rnr.evalAction(ca, s)
if err != nil {
return fmt.Errorf("actions[%d] error: %w", i, err)
}
if err := chromedp.Run(rnr.ctx, as...); err != nil {
return fmt.Errorf("actions[%d] error: %w", i, err)
}
ras := fn.Args.ResArgs()
if len(ras) > 0 {
// capture
res := map[string]any{}
for _, arg := range ras {
v := rnr.store[arg.Key]
switch vv := v.(type) {
case *string:
res[arg.Key] = *vv
case *map[string]string:
res[arg.Key] = *vv
case *[]byte:
res[arg.Key] = *vv
default:
res[arg.Key] = vv
}
}
o.capturers.captureCDPResponse(ca, res)
}
}
// record
r := map[string]any{}
for k, v := range rnr.store {
switch vv := v.(type) {
case *string:
r[k] = *vv
case *map[string]string:
r[k] = *vv
case *[]byte:
r[k] = *vv
default:
r[k] = vv
}
}
o.record(r)
rnr.store = map[string]any{} // clear
return nil
}
func (rnr *cdpRunner) evalAction(ca CDPAction, s *step) ([]chromedp.Action, error) {
o := s.parent
_, fn, err := findCDPFn(ca.Fn)
if err != nil {
return nil, err
}
// path resolution for setUploadFile.path
if ca.Fn == "setUploadFile" {
p, ok := ca.Args["path"]
if !ok {
return nil, fmt.Errorf("invalid action: %v: arg %q not found", ca, "path")
}
pp, ok := p.(string)
if !ok {
return nil, fmt.Errorf("invalid action: %v", ca)
}
if !filepath.IsAbs(pp) {
ca.Args["path"] = filepath.Join(o.root, pp)
}
}
fv := reflect.ValueOf(fn.Fn)
var vs []reflect.Value
for i, a := range fn.Args {
switch a.Typ {
case CDPArgTypeArg:
v, ok := ca.Args[a.Key]
if !ok {
return nil, fmt.Errorf("invalid action: %v: arg %q not found", ca, a.Key)
}
if v == nil {
return nil, fmt.Errorf("invalid action arg: %s.%s = %v", ca.Fn, a.Key, v)
}
vs = append(vs, reflect.ValueOf(v))
case CDPArgTypeRes:
k := a.Key
switch reflect.TypeOf(fn.Fn).In(i).Elem().Kind() {
case reflect.String:
var v string
rnr.store[k] = &v
vs = append(vs, reflect.ValueOf(&v))
case reflect.Map:
// e.g. attributes
v := map[string]string{}
rnr.store[k] = &v
vs = append(vs, reflect.ValueOf(&v))
case reflect.Slice:
var v []byte
rnr.store[k] = &v
vs = append(vs, reflect.ValueOf(&v))
default:
return nil, fmt.Errorf("invalid action: %v", ca)
}
default:
return nil, fmt.Errorf("invalid action: %v", ca)
}
}
res := fv.Call(vs)
a, ok := res[0].Interface().(chromedp.Action)
if ok {
return []chromedp.Action{a}, nil
}
as, ok := res[0].Interface().([]chromedp.Action)
if ok {
return as, nil
}
return nil, fmt.Errorf("invalid action: %v", ca)
}