« Chrome/DevToolsProtocol/Transport.pm
↑ Summary
Chrome/DevToolsProtocol/Transport/Pipe/NetAsync.pm »
| 1 | package Chrome::DevToolsProtocol::Transport::Pipe::Mojo; | ||
| 2 | use strict; | ||
| 3 | use Moo 2; | ||
| 4 | no warnings 'experimental::signatures'; | ||
| 5 | use feature 'signatures'; | ||
| 6 | use Scalar::Util 'weaken'; | ||
| 7 | |||
| 8 | use Mojo::IOLoop::Stream; | ||
| 9 | use Future::Mojo; | ||
| 10 | |||
| 11 | our $VERSION = '0.80'; | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | Chrome::DevToolsProtocol::Transport::Pipe::Mojo - EXPERIMENTAL Local pipe backend for Chrome communication | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | my $t = Chrome::DevToolsProtocol::Transport::Pipe::Mojo->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 'reader' => ( | ||
| 41 | is => 'rw', | ||
| 42 | ); | ||
| 43 | |||
| 44 | has 'writer' => ( | ||
| 45 | is => 'rw', | ||
| 46 | ); | ||
| 47 | |||
| 48 | sub connect( $self, $handler, $got_endpoint, $logger ) { | ||
| 49 | $logger ||= sub{}; | ||
| 50 | weaken $handler; | ||
| 51 | |||
| 52 | my $buffer; | ||
| 53 | weaken( my $s = $self ); | ||
| 54 | |||
| 55 | $got_endpoint->then( sub( $endpoint ) { | ||
| 56 | die "Got an undefined endpoint" unless defined $endpoint; | ||
| 57 | $self->{reader} = Mojo::IOLoop::Stream->new($endpoint->{reader_fh}); | ||
| 58 | $self->reader->on(read => sub( $stream, $bytes ) { | ||
| 59 | $buffer .= $bytes; | ||
| 60 | while($buffer =~ s!^(.*?)\0!!) { | ||
| 61 | #warn "[[$1]]"; | ||
| 62 | my $line = $1; | ||
| 63 | $handler->on_response( $s, $line ); | ||
| 64 | }; | ||
| 65 | }); | ||
| 66 | $self->{writer} = Mojo::IOLoop::Stream->new($endpoint->{writer_fh}); | ||
| 67 | $self->writer->on( error => sub { | ||
| 68 | use Data::Dumper; | ||
| 69 | warn Dumper \@_; | ||
| 70 | }); | ||
| 71 | $self->reader->start; | ||
| 72 | $self->writer->start; | ||
| 73 | Future->done( $self ); | ||
| 74 | }); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub send( $self, $message ) { | ||
| 78 | $self->writer->write( $message . "\0" ); | ||
| 79 | $self->future->done(1); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub close( $self ) { | ||
| 83 | for my $c (delete $self->{reader}, delete $self->{writer}) { | ||
| 84 | if( $c) { | ||
| 85 | $c->close | ||
| 86 | }; | ||
| 87 | }; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub future { | ||
| 91 | my $f = Future::Mojo->new; | ||
| 92 | # for tracing where futures get lost later | ||
| 93 | # use Carp 'cluck'; cluck "new: $f"; | ||
| 94 | return $f | ||
| 95 | } | ||
| 96 | |||
| 97 | =head2 C<< $transport->sleep( $seconds ) >> | ||
| 98 | |||
| 99 | $transport->sleep( 10 )->get; # wait for 10 seconds | ||
| 100 | |||
| 101 | Returns a Future that will be resolved in the number of seconds given. | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | sub sleep( $self, $seconds ) { | ||
| 106 | Future::Mojo->new_timer( $seconds ) | ||
| 107 | } | ||
| 108 | |||
| 109 | 1; | ||
| 110 | |||
| 111 | =head1 SEE ALSO | ||
| 112 | |||
| 113 | The factory class for transports | ||
| 114 | |||
| 115 | L<Chrome::DevToolsProtocol::Transport::Pipe> | ||
| 116 | |||
| 117 | =head1 REPOSITORY | ||
| 118 | |||
| 119 | The public repository of this module is | ||
| 120 | L<https://github.com/Corion/www-mechanize-chrome>. | ||
| 121 | |||
| 122 | =head1 SUPPORT | ||
| 123 | |||
| 124 | The public support forum of this module is L<https://perlmonks.org/>. | ||
| 125 | |||
| 126 | =head1 BUG TRACKER | ||
| 127 | |||
| 128 | Please report bugs in this module via the Github bug queue at | ||
| 129 | L<https://github.com/Corion/WWW-Mechanize-Chrome/issues> | ||
| 130 | |||
| 131 | =head1 AUTHOR | ||
| 132 | |||
| 133 | Max Maischein C<corion@cpan.org> | ||
| 134 | |||
| 135 | =head1 COPYRIGHT (c) | ||
| 136 | |||
| 137 | Copyright 2010-2026 by Max Maischein C<corion@cpan.org>. | ||
| 138 | |||
| 139 | =head1 LICENSE | ||
| 140 | |||
| 141 | This module is released under the same terms as Perl itself. | ||
| 142 | |||
| 143 | =cut |