forked from carusyte/chromedp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
817 lines (664 loc) · 17.3 KB
/
handler.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
package chromedp
import (
"context"
"fmt"
"reflect"
"runtime"
"strings"
"sync"
"time"
"github.com/mailru/easyjson"
"github.com/knq/chromedp/cdp"
"github.com/knq/chromedp/cdp/cdputil"
"github.com/knq/chromedp/cdp/css"
"github.com/knq/chromedp/cdp/dom"
"github.com/knq/chromedp/cdp/inspector"
logdom "github.com/knq/chromedp/cdp/log"
"github.com/knq/chromedp/cdp/page"
rundom "github.com/knq/chromedp/cdp/runtime"
"github.com/knq/chromedp/client"
"github.com/knq/chromedp/cdp/network"
)
// TargetHandler manages a Chrome Debugging Protocol target.
type TargetHandler struct {
conn client.Transport
// frames is the set of encountered frames.
frames map[cdp.FrameID]*cdp.Frame
// lsnr is the map of listeners, which maps from cdp.MethodType to channels.
lsnr map[cdp.MethodType][]chan interface{}
// lsnrchs is the map of channels, which maps from channel to registered cdp.MethodType(s).
lsnrchs map[<-chan interface{}]map[cdp.MethodType]bool
// cur is the current top level frame.
cur *cdp.Frame
// qcmd is the outgoing message queue.
qcmd chan *cdp.Message
// qres is the incoming command result queue.
qres chan *cdp.Message
// qevents is the incoming event queue.
qevents chan *cdp.Message
// detached is closed when the detached event is received.
detached chan *inspector.EventDetached
// loaded is fired when the page load event is received.
loaded chan struct{}
pageWaitGroup, domWaitGroup *sync.WaitGroup
// last is the last sent message identifier.
last int64
lastm sync.Mutex
// res is the id->result channel map.
res map[int64]chan *cdp.Message
resrw sync.RWMutex
// logging funcs
logf, debugf, errorf LogFunc
sync.RWMutex
}
// NewTargetHandler creates a new handler for the specified client target.
func NewTargetHandler(t client.Target, logf, debugf, errorf LogFunc) (*TargetHandler, error) {
conn, err := client.Dial(t)
if err != nil {
return nil, err
}
return &TargetHandler{
conn: conn,
logf: logf,
debugf: debugf,
errorf: errorf,
}, nil
}
// Run starts the processing of commands and events of the client target
// provided to NewTargetHandler.
//
// Callers can stop Run by closing the passed context.
func (h *TargetHandler) Run(ctxt context.Context) error {
var err error
// reset
h.Lock()
h.frames = make(map[cdp.FrameID]*cdp.Frame)
h.lsnr = make(map[cdp.MethodType][]chan interface{})
h.lsnrchs = make(map[<-chan interface{}]map[cdp.MethodType]bool)
h.qcmd = make(chan *cdp.Message)
h.qres = make(chan *cdp.Message)
// The events channel needs to be big enough to buffer as many events as
// we might recieve at once while processing one event. This is
// necessary because the event handling may do requests which require
// reading from the qmsg channel. This can't happen if the network
// handler is blocked writing to qevents. See discussion in #75. In
// practice I haven't seen the buffer get bigger than one or two. But we
// make it large just to be safe, and panic if it is ever full, rather
// than deadlocking.
h.qevents = make(chan *cdp.Message, 1024)
h.res = make(map[int64]chan *cdp.Message)
h.detached = make(chan *inspector.EventDetached)
h.pageWaitGroup = new(sync.WaitGroup)
h.domWaitGroup = new(sync.WaitGroup)
h.Unlock()
// run
go h.run(ctxt)
// enable domains
for _, a := range []Action{
logdom.Enable(),
rundom.Enable(),
network.Enable(),
inspector.Enable(),
page.Enable(),
dom.Enable(),
css.Enable(),
} {
err = a.Do(ctxt, h)
if err != nil {
return fmt.Errorf("unable to execute %s: %v", reflect.TypeOf(a), err)
}
}
h.Lock()
// get page resources
tree, err := page.GetResourceTree().Do(ctxt, h)
if err != nil {
return fmt.Errorf("unable to get resource tree: %v", err)
}
h.frames[tree.Frame.ID] = tree.Frame
h.cur = tree.Frame
for _, c := range tree.ChildFrames {
h.frames[c.Frame.ID] = c.Frame
}
h.Unlock()
h.documentUpdated(ctxt)
return nil
}
// run handles the actual message processing to / from the web socket connection.
func (h *TargetHandler) run(ctxt context.Context) {
defer h.conn.Close()
// add cancel to context
ctxt, cancel := context.WithCancel(ctxt)
defer cancel()
go func() {
defer cancel()
for {
select {
default:
msg, err := h.read()
if err != nil {
return
}
switch {
case msg.Method != "":
select {
case h.qevents <- msg:
default:
// See discussion in #75.
panic("h.qevents is blocked!")
}
case msg.ID != 0:
h.qres <- msg
default:
h.errorf("ignoring malformed incoming message (missing id or method): %#v", msg)
}
case <-h.detached:
// FIXME: should log when detached, and reason
return
case <-ctxt.Done():
return
}
}
}()
var wg sync.WaitGroup
defer wg.Wait()
// process queues
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case ev := <-h.qevents:
err := h.processEvent(ctxt, ev)
if err != nil {
h.errorf("could not process event %s: %v", ev.Method, err)
}
case <-ctxt.Done():
return
}
}
}()
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case res := <-h.qres:
err := h.processResult(res)
if err != nil {
h.errorf("could not process result for message %d: %v", res.ID, err)
}
case <-ctxt.Done():
return
}
}
}()
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case cmd := <-h.qcmd:
err := h.processCommand(cmd)
if err != nil {
h.errorf("could not process command message %d: %v", cmd.ID, err)
}
case <-ctxt.Done():
return
}
}
}()
}
// read reads a message from the client connection.
func (h *TargetHandler) read() (*cdp.Message, error) {
// read
buf, err := h.conn.Read()
if err != nil {
return nil, err
}
h.debugf("-> %s", string(buf))
// unmarshal
msg := new(cdp.Message)
err = easyjson.Unmarshal(buf, msg)
if err != nil {
return nil, err
}
return msg, nil
}
// WaitEventLoad returns a channel which is closed when the page load event is
// fired, or the next TargetHandler.Run takes place.
func (h *TargetHandler) WaitEventLoad() <-chan struct{} {
h.Lock()
defer h.Unlock()
return h.loaded
}
// ResetEventLoad resets the event load trigger returned by WaitEventLoad.
// It is called when a navigation is requested, so that the next load can take
// place.
func (h *TargetHandler) ResetEventLoad() {
h.Lock()
defer h.Unlock()
if h.loaded != nil {
select {
case <-h.loaded: // already closed
default:
// Before replacing h.loaded with a new pipeline,
// unblock previous waiters if they are still blocked.
// This prevents them staying deadlocked for a load
// event which will never come, however it means that
// they get a phantom load event.
close(h.loaded)
h.loaded = nil
}
}
// Make a new channel for signalling the next page load event.
h.loaded = make(chan struct{})
}
// processEvent processes an incoming event.
func (h *TargetHandler) processEvent(ctxt context.Context, msg *cdp.Message) error {
if msg == nil {
return cdp.ErrChannelClosed
}
// unmarshal
ev, err := cdputil.UnmarshalMessage(msg)
if err != nil {
return err
}
propagate(h, msg.Method, ev)
switch e := ev.(type) {
case *inspector.EventDetached:
h.Lock()
h.detached <- e
h.Unlock()
return nil
case *dom.EventDocumentUpdated:
h.domWaitGroup.Wait()
h.documentUpdated(ctxt)
return nil
}
d := msg.Method.Domain()
switch d {
case "Page":
h.pageWaitGroup.Add(1)
h.pageEvent(ctxt, ev)
case "DOM":
h.domWaitGroup.Add(1)
h.domEvent(ctxt, ev)
}
return nil
}
// propagate propagates event to the listeners
func propagate(h *TargetHandler, method cdp.MethodType, ev interface{}) {
h.RLock()
defer h.RUnlock()
if lsnrs, ok := h.lsnr[method]; ok {
for _, l := range lsnrs {
l <- ev
}
}
}
// documentUpdated handles the document updated event, retrieving the document
// root for the root frame.
func (h *TargetHandler) documentUpdated(ctxt context.Context) {
f, err := h.WaitFrame(ctxt, cdp.EmptyFrameID)
if err != nil {
h.errorf("could not get current frame: %v", err)
return
}
f.Lock()
defer f.Unlock()
// invalidate nodes
if f.Root != nil {
close(f.Root.Invalidated)
}
f.Nodes = make(map[cdp.NodeID]*cdp.Node)
f.Root, err = dom.GetDocument().WithPierce(true).Do(ctxt, h)
if err != nil {
h.errorf("could not retrieve document root for %s: %v", f.ID, err)
return
}
f.Root.Invalidated = make(chan struct{})
walk(f.Nodes, f.Root)
}
// processResult processes an incoming command result.
func (h *TargetHandler) processResult(msg *cdp.Message) error {
h.resrw.RLock()
defer h.resrw.RUnlock()
ch, ok := h.res[msg.ID]
if !ok {
return fmt.Errorf("id %d not present in res map", msg.ID)
}
defer close(ch)
ch <- msg
return nil
}
// processCommand writes a command to the client connection.
func (h *TargetHandler) processCommand(cmd *cdp.Message) error {
// marshal
buf, err := easyjson.Marshal(cmd)
if err != nil {
return err
}
h.debugf("<- %s", string(buf))
return h.conn.Write(buf)
}
// emptyObj is an empty JSON object message.
var emptyObj = easyjson.RawMessage([]byte(`{}`))
// Execute executes commandType against the endpoint passed to Run, using the
// provided context and params, decoding the result of the command to res.
func (h *TargetHandler) Execute(ctxt context.Context, commandType cdp.MethodType, params easyjson.Marshaler, res easyjson.Unmarshaler) error {
var paramsBuf easyjson.RawMessage
if params == nil {
paramsBuf = emptyObj
} else {
var err error
paramsBuf, err = easyjson.Marshal(params)
if err != nil {
return err
}
}
id := h.next()
// save channel
ch := make(chan *cdp.Message, 1)
h.resrw.Lock()
h.res[id] = ch
h.resrw.Unlock()
// queue message
h.qcmd <- &cdp.Message{
ID: id,
Method: commandType,
Params: paramsBuf,
}
errch := make(chan error, 1)
go func() {
defer close(errch)
select {
case msg := <-ch:
switch {
case msg == nil:
errch <- cdp.ErrChannelClosed
case msg.Error != nil:
errch <- msg.Error
case res != nil:
errch <- easyjson.Unmarshal(msg.Result, res)
}
case <-ctxt.Done():
errch <- ctxt.Err()
}
h.resrw.Lock()
defer h.resrw.Unlock()
delete(h.res, id)
}()
return <-errch
}
// next returns the next message id.
func (h *TargetHandler) next() int64 {
h.lastm.Lock()
defer h.lastm.Unlock()
h.last++
return h.last
}
// GetRoot returns the current top level frame's root document node.
func (h *TargetHandler) GetRoot(ctxt context.Context) (*cdp.Node, error) {
var root *cdp.Node
loop:
for {
var cur *cdp.Frame
select {
default:
h.RLock()
cur = h.cur
if cur != nil {
cur.RLock()
root = cur.Root
cur.RUnlock()
}
h.RUnlock()
if cur != nil && root != nil {
break loop
}
time.Sleep(DefaultCheckDuration)
case <-ctxt.Done():
return nil, ctxt.Err()
}
}
return root, nil
}
// SetActive sets the currently active frame after a successful navigation.
func (h *TargetHandler) SetActive(ctxt context.Context, id cdp.FrameID) error {
var err error
// get frame
f, err := h.WaitFrame(ctxt, id)
if err != nil {
return err
}
h.Lock()
defer h.Unlock()
h.cur = f
return nil
}
// WaitFrame waits for a frame to be loaded using the provided context.
func (h *TargetHandler) WaitFrame(ctxt context.Context, id cdp.FrameID) (*cdp.Frame, error) {
// TODO: fix this
timeout := time.After(10 * time.Second)
loop:
for {
select {
default:
var f *cdp.Frame
var ok bool
h.RLock()
if id == cdp.EmptyFrameID {
f, ok = h.cur, h.cur != nil
} else {
f, ok = h.frames[id]
}
h.RUnlock()
if ok {
return f, nil
}
time.Sleep(DefaultCheckDuration)
case <-ctxt.Done():
return nil, ctxt.Err()
case <-timeout:
break loop
}
}
return nil, fmt.Errorf("timeout waiting for frame `%s`", id)
}
// WaitNode waits for a node to be loaded using the provided context.
func (h *TargetHandler) WaitNode(ctxt context.Context, f *cdp.Frame, id cdp.NodeID) (*cdp.Node, error) {
// TODO: fix this
timeout := time.After(10 * time.Second)
loop:
for {
select {
default:
var n *cdp.Node
var ok bool
f.RLock()
n, ok = f.Nodes[id]
f.RUnlock()
if n != nil && ok {
return n, nil
}
time.Sleep(DefaultCheckDuration)
case <-ctxt.Done():
return nil, ctxt.Err()
case <-timeout:
break loop
}
}
return nil, fmt.Errorf("timeout waiting for node `%d`", id)
}
// pageEvent handles incoming page events.
func (h *TargetHandler) pageEvent(ctxt context.Context, ev interface{}) {
defer h.pageWaitGroup.Done()
var id cdp.FrameID
var op frameOp
switch e := ev.(type) {
case *page.EventFrameNavigated:
h.Lock()
h.frames[e.Frame.ID] = e.Frame
if h.cur != nil && h.cur.ID == e.Frame.ID {
h.cur = e.Frame
}
h.Unlock()
return
case *page.EventFrameAttached:
// NOTE(pwaller):
// This happens before we have the frame object for
// e.FrameID - that only occurs in EventFrameNavigated.
// so there isn't a frame to update yet.
// I'm not sure of the use of this state - see #75.
// Another issue is that events like EventFrameStoppedLoading
// can happen even though EventFrameNavigated *never fires*,
// so these events can end up calling WaitFrame on a frame
// that never comes into existence.
// id, op = e.FrameID, frameAttached(e.ParentFrameID)
return
case *page.EventFrameDetached:
return // See note above.
case *page.EventFrameStartedLoading:
return // See note above.
case *page.EventFrameStoppedLoading:
return // See note above.
case *page.EventFrameScheduledNavigation:
return // See note above.
case *page.EventFrameClearedScheduledNavigation:
return // See note above.
case *page.EventDomContentEventFired:
return
case *page.EventLoadEventFired:
if h.loaded != nil {
// If anyone is listening for loaded, notify them now.
close(h.loaded)
h.loaded = nil
}
return
case *page.EventFrameResized:
return
case *page.EventLifecycleEvent:
return
default:
h.errorf("unhandled page event %s", reflect.TypeOf(ev))
return
}
f, err := h.WaitFrame(ctxt, id)
if err != nil {
h.errorf("could not get frame %s: %v", id, err)
return
}
h.Lock()
defer h.Unlock()
f.Lock()
defer f.Unlock()
op(f)
}
// domEvent handles incoming DOM events.
func (h *TargetHandler) domEvent(ctxt context.Context, ev interface{}) {
defer h.domWaitGroup.Done()
// wait current frame
f, err := h.WaitFrame(ctxt, cdp.EmptyFrameID)
if err != nil {
h.errorf("could not process DOM event %s: %v", reflect.TypeOf(ev), err)
return
}
var id cdp.NodeID
var op nodeOp
switch e := ev.(type) {
case *dom.EventSetChildNodes:
id, op = e.ParentID, setChildNodes(f.Nodes, e.Nodes)
case *dom.EventAttributeModified:
id, op = e.NodeID, attributeModified(e.Name, e.Value)
case *dom.EventAttributeRemoved:
id, op = e.NodeID, attributeRemoved(e.Name)
case *dom.EventInlineStyleInvalidated:
if len(e.NodeIds) == 0 {
return
}
id, op = e.NodeIds[0], inlineStyleInvalidated(e.NodeIds[1:])
case *dom.EventCharacterDataModified:
id, op = e.NodeID, characterDataModified(e.CharacterData)
case *dom.EventChildNodeCountUpdated:
id, op = e.NodeID, childNodeCountUpdated(e.ChildNodeCount)
case *dom.EventChildNodeInserted:
if e.PreviousNodeID != cdp.EmptyNodeID {
_, err = h.WaitNode(ctxt, f, e.PreviousNodeID)
if err != nil {
return
}
}
id, op = e.ParentNodeID, childNodeInserted(f.Nodes, e.PreviousNodeID, e.Node)
case *dom.EventChildNodeRemoved:
id, op = e.ParentNodeID, childNodeRemoved(f.Nodes, e.NodeID)
case *dom.EventShadowRootPushed:
id, op = e.HostID, shadowRootPushed(f.Nodes, e.Root)
case *dom.EventShadowRootPopped:
id, op = e.HostID, shadowRootPopped(f.Nodes, e.RootID)
case *dom.EventPseudoElementAdded:
id, op = e.ParentID, pseudoElementAdded(f.Nodes, e.PseudoElement)
case *dom.EventPseudoElementRemoved:
id, op = e.ParentID, pseudoElementRemoved(f.Nodes, e.PseudoElementID)
case *dom.EventDistributedNodesUpdated:
id, op = e.InsertionPointID, distributedNodesUpdated(e.DistributedNodes)
default:
h.errorf("unhandled node event %s", reflect.TypeOf(ev))
return
}
// retrieve node
n, err := h.WaitNode(ctxt, f, id)
if err != nil {
s := strings.TrimSuffix(runtime.FuncForPC(reflect.ValueOf(op).Pointer()).Name(), ".func1")
i := strings.LastIndex(s, ".")
if i != -1 {
s = s[i+1:]
}
h.errorf("could not perform (%s) operation on node %d (wait node): %v", s, id, err)
return
}
h.Lock()
defer h.Unlock()
f.Lock()
defer f.Unlock()
op(n)
}
// Listen creates a listener for the specified event types.
func (h *TargetHandler) Listen(eventTypes ...cdp.MethodType) <-chan interface{} {
h.Lock()
defer h.Unlock()
ch := make(chan interface{}, 16)
for _, evtTyp := range eventTypes {
if chlist, ok := h.lsnr[evtTyp]; ok {
chlist = append(chlist, ch)
if _, etok := h.lsnrchs[ch][evtTyp]; !etok {
h.lsnrchs[ch][evtTyp] = true
}
} else {
h.lsnr[evtTyp] = []chan interface{}{ch}
h.lsnrchs[ch] = map[cdp.MethodType]bool{evtTyp: true}
}
}
return ch
}
// Release releases a channel returned from Listen.
func (h *TargetHandler) Release(ch <-chan interface{}) {
h.Lock()
defer h.Unlock()
lsnrchs := h.lsnrchs[ch]
for evtTyp := range lsnrchs {
chs := h.lsnr[evtTyp]
for i := 0; i < len(chs); i++ {
if ch == chs[i] {
chs[i] = nil
if i == len(chs)-1 {
chs = chs[:len(chs)-1]
} else {
chs = append(chs[:i], chs[i+1:]...)
}
h.lsnr[evtTyp] = chs
break
}
}
}
delete(h.lsnrchs, ch)
}