Chrome/DevToolsProtocol/Transport/Pipe/AnyEvent.pm untested

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 92 CC 2 SCAR 9.3
CC
2
Cov
0%
connect
6.0
send
2.0
close
2.0
CRAP 2.5
1 package Chrome::DevToolsProtocol::Transport::Pipe::AnyEvent;
2 use strict;
3 use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
4 use Moo 2;
5 no warnings 'experimental::signatures';
6 use feature 'signatures';
7 use Scalar::Util 'weaken';
8  
9 use Carp qw(croak);
10  
11 use AnyEvent;
12 use AnyEvent::Future qw(as_future_cb);
13  
14 our $VERSION = '0.80';
15 our @CARP_NOT = ();
16  
17 =head1 NAME
18  
19 Chrome::DevToolsProtocol::Transport::Pipe::AnyEvent- EXPERIMENTAL Local pipe backend for Chrome communication
20  
21 =head1 SYNOPSIS
22  
23 my $t = Chrome::DevToolsProtocol::Transport::Pipe::AnyEvent->new;
24 $t->connect( $handler, $got_endpoint, $logger)
25 ->then(sub {
26 my( $connection ) = @_;
27 print "We are connected\n";
28 });
29  
30 =head1 DESCRIPTION
31  
32 This is an experimental backend communicating with Chrome using a pipe
33 of two file descriptors.
34  
35 This requires Chrome v72+.
36  
37 =cut
38  
39 has 'type' => (
40 is => 'ro',
41 default => 'pipe'
42 );
43  
44 has 'endpoint' => (
45 is => 'rw',
46 );
47  
48 has 'reader' => (
49 is => 'rw',
50 );
51  
52 has 'writer' => (
53 is => 'rw',
54 );
55  
56 sub connect( $self, $handler, $got_endpoint, $logger ) {
57 $logger ||= sub{};
58 weaken $handler;
59  
60 my $buffer = '';
61  
62 weaken( my $s = $self );
63  
64 $got_endpoint->then( sub( $endpoint ) {
65 die "Got an undefined endpoint" unless defined $endpoint;
66 $self->endpoint( $endpoint ); # keep a reference
67 my $flags = fcntl( $endpoint->{ reader_fh }, F_GETFL, 0 );
68 fcntl( $endpoint->{ reader_fh }, F_SETFL, $flags | O_NONBLOCK )
69 or warn "Can't make the pipe nonblocking: $!";
70 my $reader = AnyEvent->io(
71 fh => $endpoint->{ reader_fh },
72 poll => 'r',
73 cb => sub {
74 # Append to our buffer
75 sysread( $s->endpoint->{ reader_fh }, $buffer, 4096, length($buffer));
76 while($buffer =~ s!^(.*?)\0!!) {
77 my $line = $1;
78 $handler->on_response( $s, $line );
79 };
80 },
81 );
82 $self->reader($reader);
83  
84 # We cheat here and write synchronously ...
85 #my $writer = AnyEvent->io(
86 # fh => $endpoint->{ writer_fh },
87 # poll => 'w',
88 #);
89 $endpoint->{writer_fh}->autoflush(1);
90 $self->writer($endpoint->{writer_fh});
91 Future->done( $self );
92 });
93 }
94  
95 sub send( $self, $message ) {
96 print { $self->writer } $message . "\0"
97 or warn $!;
98 $self->future->done(1);
99 }
100  
101 sub close( $self ) {
102 delete $self->{reader};
103 delete $self->{writer};
104 }
105  
106 sub future {
107 AnyEvent::Future->new
108 }
109  
110 =head2 C<< $transport->sleep( $seconds ) >>
111  
112 $transport->sleep( 10 )->get; # wait for 10 seconds
113  
114 Returns a Future that will be resolved in the number of seconds given.
115  
116 =cut
117  
118 sub sleep( $self, $seconds ) {
119 AnyEvent::Future->new_delay( after => $seconds );
120 }
121  
122 1;
123  
124 =head1 KNOWN BUGS
125  
126 Launching a second instance of WWW::Mechanize::Chrome not connected to the
127 same Chrome instance when using the pipe backend for communication will crash.
128  
129 I'm not sure who is at fault, AnyEvent or Chrome.
130  
131 =head1 SEE ALSO
132  
133 The factory class for transports
134  
135 L<Chrome::DevToolsProtocol::Transport::Pipe>
136  
137 =head1 REPOSITORY
138  
139 The public repository of this module is
140 L<https://github.com/Corion/www-mechanize-chrome>.
141  
142 =head1 SUPPORT
143  
144 The public support forum of this module is L<https://perlmonks.org/>.
145  
146 =head1 BUG TRACKER
147  
148 Please report bugs in this module via the Github bug queue at
149 L<https://github.com/Corion/WWW-Mechanize-Chrome/issues>
150  
151 =head1 AUTHOR
152  
153 Max Maischein C<corion@cpan.org>
154  
155 =head1 COPYRIGHT (c)
156  
157 Copyright 2010-2026 by Max Maischein C<corion@cpan.org>.
158  
159 =head1 LICENSE
160  
161 This module is released under the same terms as Perl itself.
162  
163 =cut