@@ -20,6 +20,7 @@ type browserContextImpl struct {
20
20
options * BrowserNewContextOptions
21
21
pages []Page
22
22
routes []* routeHandlerEntry
23
+ webSocketRoutes []* webSocketRouteHandler
23
24
ownedPage Page
24
25
browser * browserImpl
25
26
serviceWorkers []Worker
@@ -44,7 +45,7 @@ func (b *browserContextImpl) SetDefaultNavigationTimeout(timeout float64) {
44
45
45
46
func (b * browserContextImpl ) setDefaultNavigationTimeoutImpl (timeout * float64 ) {
46
47
b .timeoutSettings .SetDefaultNavigationTimeout (timeout )
47
- b .channel .SendNoReply ("setDefaultNavigationTimeoutNoReply" , true , map [string ]interface {}{
48
+ b .channel .SendNoReplyInternal ("setDefaultNavigationTimeoutNoReply" , map [string ]interface {}{
48
49
"timeout" : timeout ,
49
50
})
50
51
}
@@ -55,7 +56,7 @@ func (b *browserContextImpl) SetDefaultTimeout(timeout float64) {
55
56
56
57
func (b * browserContextImpl ) setDefaultTimeoutImpl (timeout * float64 ) {
57
58
b .timeoutSettings .SetDefaultTimeout (timeout )
58
- b .channel .SendNoReply ("setDefaultTimeoutNoReply" , true , map [string ]interface {}{
59
+ b .channel .SendNoReplyInternal ("setDefaultTimeoutNoReply" , map [string ]interface {}{
59
60
"timeout" : timeout ,
60
61
})
61
62
}
@@ -541,7 +542,7 @@ func (b *browserContextImpl) onBinding(binding *bindingCallImpl) {
541
542
if ! ok || function == nil {
542
543
return
543
544
}
544
- binding .Call (function )
545
+ go binding .Call (function )
545
546
}
546
547
547
548
func (b * browserContextImpl ) onClose () {
@@ -572,58 +573,56 @@ func (b *browserContextImpl) onPage(page Page) {
572
573
}
573
574
574
575
func (b * browserContextImpl ) onRoute (route * routeImpl ) {
575
- go func () {
576
- b .Lock ()
577
- route .context = b
578
- page := route .Request ().(* requestImpl ).safePage ()
579
- routes := make ([]* routeHandlerEntry , len (b .routes ))
580
- copy (routes , b .routes )
581
- b .Unlock ()
576
+ b .Lock ()
577
+ route .context = b
578
+ page := route .Request ().(* requestImpl ).safePage ()
579
+ routes := make ([]* routeHandlerEntry , len (b .routes ))
580
+ copy (routes , b .routes )
581
+ b .Unlock ()
582
582
583
- checkInterceptionIfNeeded := func () {
584
- b .Lock ()
585
- defer b .Unlock ()
586
- if len (b .routes ) == 0 {
587
- _ , err := b .connection .WrapAPICall (func () (interface {}, error ) {
588
- err := b .updateInterceptionPatterns ()
589
- return nil , err
590
- }, true )
591
- if err != nil {
592
- logger .Printf ("could not update interception patterns: %v\n " , err )
593
- }
583
+ checkInterceptionIfNeeded := func () {
584
+ b .Lock ()
585
+ defer b .Unlock ()
586
+ if len (b .routes ) == 0 {
587
+ _ , err := b .connection .WrapAPICall (func () (interface {}, error ) {
588
+ err := b .updateInterceptionPatterns ()
589
+ return nil , err
590
+ }, true )
591
+ if err != nil {
592
+ logger .Printf ("could not update interception patterns: %v\n " , err )
594
593
}
595
594
}
595
+ }
596
596
597
- url := route .Request ().URL ()
598
- for _ , handlerEntry := range routes {
599
- // If the page or the context was closed we stall all requests right away.
600
- if (page != nil && page .closeWasCalled ) || b .closeWasCalled {
601
- return
602
- }
603
- if ! handlerEntry .Matches (url ) {
604
- continue
605
- }
606
- if ! slices .ContainsFunc (b .routes , func (entry * routeHandlerEntry ) bool {
607
- return entry == handlerEntry
608
- }) {
609
- continue
610
- }
611
- if handlerEntry .WillExceed () {
612
- b .routes = slices .DeleteFunc (b .routes , func (rhe * routeHandlerEntry ) bool {
613
- return rhe == handlerEntry
614
- })
615
- }
616
- handled := handlerEntry .Handle (route )
617
- checkInterceptionIfNeeded ()
618
- yes := <- handled
619
- if yes {
620
- return
621
- }
597
+ url := route .Request ().URL ()
598
+ for _ , handlerEntry := range routes {
599
+ // If the page or the context was closed we stall all requests right away.
600
+ if (page != nil && page .closeWasCalled ) || b .closeWasCalled {
601
+ return
622
602
}
623
- // If the page is closed or unrouteAll() was called without waiting and interception disabled,
624
- // the method will throw an error - silence it.
625
- _ = route .internalContinue (true )
626
- }()
603
+ if ! handlerEntry .Matches (url ) {
604
+ continue
605
+ }
606
+ if ! slices .ContainsFunc (b .routes , func (entry * routeHandlerEntry ) bool {
607
+ return entry == handlerEntry
608
+ }) {
609
+ continue
610
+ }
611
+ if handlerEntry .WillExceed () {
612
+ b .routes = slices .DeleteFunc (b .routes , func (rhe * routeHandlerEntry ) bool {
613
+ return rhe == handlerEntry
614
+ })
615
+ }
616
+ handled := handlerEntry .Handle (route )
617
+ checkInterceptionIfNeeded ()
618
+ yes := <- handled
619
+ if yes {
620
+ return
621
+ }
622
+ }
623
+ // If the page is closed or unrouteAll() was called without waiting and interception disabled,
624
+ // the method will throw an error - silence it.
625
+ _ = route .internalContinue (true )
627
626
}
628
627
629
628
func (b * browserContextImpl ) updateInterceptionPatterns () error {
@@ -726,6 +725,40 @@ func (b *browserContextImpl) OnWebError(fn func(WebError)) {
726
725
b .On ("weberror" , fn )
727
726
}
728
727
728
+ func (b * browserContextImpl ) RouteWebSocket (url interface {}, handler func (WebSocketRoute )) error {
729
+ b .Lock ()
730
+ defer b .Unlock ()
731
+ b .webSocketRoutes = slices .Insert (b .webSocketRoutes , 0 , newWebSocketRouteHandler (newURLMatcher (url , b .options .BaseURL ), handler ))
732
+
733
+ return b .updateWebSocketInterceptionPatterns ()
734
+ }
735
+
736
+ func (b * browserContextImpl ) onWebSocketRoute (wr WebSocketRoute ) {
737
+ b .Lock ()
738
+ index := slices .IndexFunc (b .webSocketRoutes , func (r * webSocketRouteHandler ) bool {
739
+ return r .Matches (wr .URL ())
740
+ })
741
+ if index == - 1 {
742
+ b .Unlock ()
743
+ _ , err := wr .ConnectToServer ()
744
+ if err != nil {
745
+ logger .Println (err )
746
+ }
747
+ return
748
+ }
749
+ handler := b .webSocketRoutes [index ]
750
+ b .Unlock ()
751
+ handler .Handle (wr )
752
+ }
753
+
754
+ func (b * browserContextImpl ) updateWebSocketInterceptionPatterns () error {
755
+ patterns := prepareWebSocketRouteHandlerInterceptionPatterns (b .webSocketRoutes )
756
+ _ , err := b .channel .Send ("setWebSocketInterceptionPatterns" , map [string ]interface {}{
757
+ "patterns" : patterns ,
758
+ })
759
+ return err
760
+ }
761
+
729
762
func (b * browserContextImpl ) effectiveCloseReason () * string {
730
763
b .Lock ()
731
764
defer b .Unlock ()
@@ -758,15 +791,22 @@ func newBrowserContext(parent *channelOwner, objectType string, guid string, ini
758
791
bt .request = fromChannel (initializer ["requestContext" ]).(* apiRequestContextImpl )
759
792
bt .clock = newClock (bt )
760
793
bt .channel .On ("bindingCall" , func (params map [string ]interface {}) {
761
- go bt .onBinding (fromChannel (params ["binding" ]).(* bindingCallImpl ))
794
+ bt .onBinding (fromChannel (params ["binding" ]).(* bindingCallImpl ))
762
795
})
763
796
764
797
bt .channel .On ("close" , bt .onClose )
765
798
bt .channel .On ("page" , func (payload map [string ]interface {}) {
766
799
bt .onPage (fromChannel (payload ["page" ]).(* pageImpl ))
767
800
})
768
801
bt .channel .On ("route" , func (params map [string ]interface {}) {
769
- bt .onRoute (fromChannel (params ["route" ]).(* routeImpl ))
802
+ bt .channel .CreateTask (func () {
803
+ bt .onRoute (fromChannel (params ["route" ]).(* routeImpl ))
804
+ })
805
+ })
806
+ bt .channel .On ("webSocketRoute" , func (params map [string ]interface {}) {
807
+ bt .channel .CreateTask (func () {
808
+ bt .onWebSocketRoute (fromChannel (params ["webSocketRoute" ]).(* webSocketRouteImpl ))
809
+ })
770
810
})
771
811
bt .channel .On ("backgroundPage" , bt .onBackgroundPage )
772
812
bt .channel .On ("serviceWorker" , func (params map [string ]interface {}) {
0 commit comments