« Chrome/DevToolsProtocol/Transport/Pipe/Mojo.pm
↑ Summary
Chrome/DevToolsProtocol/Transport/Pipe/AnyEvent.pm »
| 1 | package Chrome::DevToolsProtocol::Transport::Pipe::NetAsync; | ||
| 2 | use strict; | ||
| 3 | use Moo 2; | ||
| 4 | no warnings 'experimental::signatures'; | ||
| 5 | use feature 'signatures'; | ||
| 6 | use Scalar::Util 'weaken'; | ||
| 7 | |||
| 8 | use IO::Async::Loop; | ||
| 9 | use IO::Async::Stream; | ||
| 10 | |||
| 11 | our $VERSION = '0.80'; | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | Chrome::DevToolsProtocol::Transport::Pipe::NetAsync - EXPERIMENTAL Local pipe backend for Chrome communication | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | my $t = Chrome::DevToolsProtocol::Transport::Pipe::NetAsync->new; | ||
| 20 | $t->connect( $handler, $got_endpoint, $logger) | ||
| 21 | ->then(sub { | ||
| 22 | my( $connection ) = @_; | ||
| 23 | print "We are connected\n"; | ||
| 24 | }); | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | This is an experimental backend communicating with Chrome using a pipe | ||
| 29 | of two file descriptors. | ||
| 30 | |||
| 31 | This requires Chrome v72+. | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | has 'type' => ( | ||
| 36 | is => 'ro', | ||
| 37 | default => 'pipe' | ||
| 38 | ); | ||
| 39 | |||
| 40 | has 'loop' => ( | ||
| 41 | is => 'lazy', | ||
| 42 | default => sub { | ||
| 43 | IO::Async::Loop->new(), | ||
| 44 | }, | ||
| 45 | ); | ||
| 46 | |||
| 47 | has 'connection' => ( | ||
| 48 | is => 'rw', | ||
| 49 | ); | ||
| 50 | |||
| 51 | sub connect( $self, $handler, $got_endpoint, $logger ) { | ||
| 52 | $logger ||= sub{}; | ||
| 53 | weaken $handler; | ||
| 54 | my $buffer; | ||
| 55 | weaken( my $s = $self ); | ||
| 56 | $got_endpoint->then( sub( $endpoint ) { | ||
| 57 | die "Got an undefined endpoint" unless defined $endpoint; | ||
| 58 | $self->{connection} = IO::Async::Stream->new( | ||
| 59 | read_handle => $endpoint->{ reader_fh }, | ||
| 60 | write_handle => $endpoint->{ writer_fh }, | ||
| 61 | on_write_error => sub { | ||
| 62 | use Data::Dumper; | ||
| 63 | warn Dumper \@_; | ||
| 64 | }, | ||
| 65 | on_read => sub( $self, $buffref, $eof ) { | ||
| 66 | while($$buffref =~ s!^(.*?)\0!!) { | ||
| 67 | #warn "[[$1]]"; | ||
| 68 | my $line = $1; | ||
| 69 | $handler->on_response( $s, $line ); | ||
| 70 | }; | ||
| 71 | }, | ||
| 72 | ); | ||
| 73 | $self->loop->add( $self->connection ); | ||
| 74 | Future->done( $self ); | ||
| 75 | }); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub send( $self, $message ) { | ||
| 79 | $self->connection->write( $message . "\0" ); | ||
| 80 | $self->future->done(1); | ||
| 81 | } | ||
| 82 | |||
| 83 | sub close( $self ) { | ||
| 84 | my $c = delete $self->{connection}; | ||
| 85 | my $l = delete $self->{loop}; | ||
| 86 | if( $c ) { | ||
| 87 | #warn "*** Closing!"; | ||
| 88 | $c->close_now; | ||
| 89 | }; | ||
| 90 | #if( $l ) { | ||
| 91 | # $l->remove( $c ); | ||
| 92 | #}; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub DESTROY { | ||
| 96 | $_[0]->close(); | ||
| 97 | } | ||
| 98 | |||
| 99 | sub future( $self ) { | ||
| 100 | my $res = $self->loop->new_future; | ||
| 101 | return $res | ||
| 102 | } | ||
| 103 | |||
| 104 | =head2 C<< $transport->sleep( $seconds ) >> | ||
| 105 | |||
| 106 | $transport->sleep( 10 )->get; # wait for 10 seconds | ||
| 107 | |||
| 108 | Returns a Future that will be resolved in the number of seconds given. | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | sub sleep( $self, $seconds ) { | ||
| 113 | $self->loop->delay_future( after => $seconds ) | ||
| 114 | } | ||
| 115 | |||
| 116 | 1; | ||
| 117 | |||
| 118 | =head1 SEE ALSO | ||
| 119 | |||
| 120 | The factory class for transports | ||
| 121 | |||
| 122 | L<Chrome::DevToolsProtocol::Transport::Pipe> | ||
| 123 | |||
| 124 | =head1 REPOSITORY | ||
| 125 | |||
| 126 | The public repository of this module is | ||
| 127 | L<https://github.com/Corion/www-mechanize-chrome>. | ||
| 128 | |||
| 129 | =head1 SUPPORT | ||
| 130 | |||
| 131 | The public support forum of this module is L<https://perlmonks.org/>. | ||
| 132 | |||
| 133 | =head1 BUG TRACKER | ||
| 134 | |||
| 135 | Please report bugs in this module via the Github bug queue at | ||
| 136 | L<https://github.com/Corion/WWW-Mechanize-Chrome/issues> | ||
| 137 | |||
| 138 | =head1 AUTHOR | ||
| 139 | |||
| 140 | Max Maischein C<corion@cpan.org> | ||
| 141 | |||
| 142 | =head1 COPYRIGHT (c) | ||
| 143 | |||
| 144 | Copyright 2010-2026 by Max Maischein C<corion@cpan.org>. | ||
| 145 | |||
| 146 | =head1 LICENSE | ||
| 147 | |||
| 148 | This module is released under the same terms as Perl itself. | ||
| 149 | |||
| 150 | =cut |