File Coverage

blib/lib/Chrome/DevToolsProtocol/Transport/Mojo.pm
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


line stmt bran cond sub pod time code
1             package Chrome::DevToolsProtocol::Transport::Mojo;
2 1     1   1911 use strict;
  1         21  
  1         30  
3 1     1   8 use Filter::signatures;
  1         1  
  1         8  
4 1     1   660 use Moo 2;
  1         12076  
  1         6  
5 1     1   1539 no warnings 'experimental::signatures';
  1         2  
  1         36  
6 1     1   6 use feature 'signatures';
  1         1  
  1         90  
7 1     1   6 use Scalar::Util 'weaken';
  1         23  
  1         55  
8              
9 1     1   261 use Mojo::UserAgent;
  0            
  0            
10             use Future::Mojo;
11              
12             our $VERSION = '0.70';
13              
14             =head1 NAME
15              
16             Chrome::DevToolsProtocol::Transport::Mojo - Mojolicious backend for Chrome communication
17              
18             =head1 SYNOPSIS
19              
20             my $got_endpoint = Future->done( "ws://..." );
21             my $t = Chrome::DevToolsProtocol::Transport::Mojo->new;
22             $t->connect( $handler, $got_endpoint, $logger)
23             ->then(sub {
24             my( $connection ) = @_;
25             print "We are connected\n";
26             });
27              
28             =cut
29              
30             has 'type' => (
31             is => 'ro',
32             default => 'websocket'
33             );
34              
35             has connection => (
36             is => 'rw',
37             );
38              
39             has ua => (
40             is => 'rw',
41             );
42              
43             sub connect( $self, $handler, $got_endpoint, $logger ) {
44             $logger ||= sub{};
45             weaken $handler;
46             weaken(my $s = $self);
47              
48             $got_endpoint->then( sub( $endpoint ) {
49             $self->{ua} ||= Mojo::UserAgent->new();
50             my $client = $s->ua;
51              
52             $logger->('debug',"Connecting to $endpoint");
53             die "Got an undefined endpoint" unless defined $endpoint;
54             my $res = $s->future;
55             #$client->on( 'start' => sub { $logger->('trace', "Starting transaction", @_ )});
56             $client->websocket( $endpoint, { 'Sec-WebSocket-Extensions' => 'permessage-deflate' }, sub( $ua, $tx ) {
57             # On error we get an Mojolicious::Transaction::HTTP here
58             if( $tx->is_websocket) {
59             $logger->('trace',"Connected to $endpoint");
60             $tx->max_websocket_size( 1024*1024*10 );
61             $res->done( $tx );
62             } else {
63             my $msg = "Couldn't connect to endpoint '$endpoint': " . $tx->res->error->{message};
64             $logger->('trace', $msg);
65             #$tx->finish();
66             $res->fail( $msg );
67             }
68             });
69             $res
70              
71             })->then( sub( $c ) {
72             my $connection = $c;
73             $s->{connection} ||= $connection;
74              
75             # Kick off the continous polling
76             $connection->on( message => sub( $connection,$message) {
77             warn "Hmm - the Websocket handler went away but I got a message for them" if( ! $handler );
78             $handler->on_response( $connection, $message )
79             });
80              
81             my $res = Future->done( $s );
82             #undef $self;
83             $res
84             });
85             }
86              
87             sub send( $self, $message ) {
88             $self->connection->send( $message );
89             $self->future->done(1);
90             }
91              
92             sub close( $self ) {
93             if( my $c = delete $self->{connection}) {
94             $c->finish
95             };
96             delete $self->{ua};
97             }
98              
99             sub future {
100             Future::Mojo->new
101             }
102              
103             =head2 C<< $transport->sleep( $seconds ) >>
104              
105             $transport->sleep( 10 )->get; # wait for 10 seconds
106              
107             Returns a Future that will be resolved in the number of seconds given.
108              
109             =cut
110              
111             sub sleep( $self, $seconds ) {
112             Future::Mojo->new_timer( $seconds )
113             }
114              
115             1;
116              
117             =head1 REQUIRED ADDITIONAL MODULES
118              
119             This module needs additional modules that are not installed by the default
120             installation of WWW::Mechanize::Chrome:
121              
122             L<Mojo::UserAgent>
123              
124             L<Future::Mojo>
125              
126             =head1 REPOSITORY
127              
128             The public repository of this module is
129             L<https://github.com/Corion/www-mechanize-chrome>.
130              
131             =head1 SUPPORT
132              
133             The public support forum of this module is L<https://perlmonks.org/>.
134              
135             =head1 BUG TRACKER
136              
137             Please report bugs in this module via the Github bug queue at
138             L<https://github.com/Corion/WWW-Mechanize-Chrome/issues>
139              
140             =head1 AUTHOR
141              
142             Max Maischein C<corion@cpan.org>
143              
144             =head1 COPYRIGHT (c)
145              
146             Copyright 2010-2023 by Max Maischein C<corion@cpan.org>.
147              
148             =head1 LICENSE
149              
150             This module is released under the same terms as Perl itself.
151              
152             =cut