Chrome/DevToolsProtocol/Transport/Mojo.pm

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