Chrome/DevToolsProtocol.pm

filter: Statementstmt 27.1% 89 / 328 Branchbran 0.0% 0 / 84 Conditioncond 0.0% 0 / 60 MC/DCmcdc 0.0% 0 / 33 Subroutinesub 24.7% 21 / 85 Podpod 79.4% 27 / 34 totaltotal 21.9% 137 / 624 CC 72 SCAR 52.4
CC
72
Cov
19%
on_response
756.0
connect
420.0
build_url
30.0
CRAP 189.5
« Chrome/DevToolsProtocol/Target.pm ↑ Summary WWW/Mechanize/Chrome/URLBlacklist.pm »
1 package Chrome::DevToolsProtocol;
2 69 ▶use 5.012; # for say, //
Subroutine BEGIN: called CC=1 SCAR=0.0
3 69 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 69 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 69 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 69 ▶use PerlX::Maybe;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 69 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 69 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 69 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 69 ▶use Future::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 69 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 69 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 69 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 69 ▶use Chrome::DevToolsProtocol::Transport;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 69 ▶use Scalar::Util 'weaken', 'isweak';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 69 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 69 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 69 with 'MooX::Role::EventEmitter';
20  
21 69 our $VERSION = '0.80';
22 69 our @CARP_NOT;
23  
24 =head1 NAME
25  
26 Chrome::DevToolsProtocol - asynchronous dispatcher for the DevTools protocol
27  
28 =head1 SYNOPSIS
29  
30 # Usually, WWW::Mechanize::Chrome automatically creates a driver for you
31 my $driver = Chrome::DevToolsProtocol->new(
32 port => 9222,
33 host => '127.0.0.1',
34 auto_close => 0,
35 error_handler => sub {
36 # Reraise the error
37 croak $_[1]
38 },
39 );
40 $driver->connect( new_tab => 1 )->get
41  
42 =head1 METHODS
43  
44 =head2 C<< ->new( %args ) >>
45  
46 my $driver = Chrome::DevToolsProtocol->new(
47 port => 9222,
48 host => '127.0.0.1',
49 auto_close => 0,
50 error_handler => sub {
51 # Reraise the error
52 croak $_[1]
53 },
54 );
55  
56 These members can mostly be set through the constructor arguments:
57  
58 =over 4
59  
60 =cut
61  
62 0 ▶sub _build_log( $self ) {
Subroutine _build_log: not called CC=1 SCAR=6.9
63 0 require Log::Log4perl;
64 0 Log::Log4perl->get_logger(__PACKAGE__);
65 }
66  
67 =item B<host>
68  
69 The hostname to connect to
70  
71 =cut
72  
73 69 has 'host' => (
74 is => 'ro',
75 default => '127.0.0.1',
76 );
77  
78 =item B<port>
79  
80 The port to connect to
81  
82 =cut
83  
84 69 has 'port' => (
85 is => 'ro',
86 default => 9222,
87 );
88  
89 =item B<json>
90  
91 The JSON decoder used
92  
93 =cut
94  
95 has 'json' => (
96 is => 'ro',
97 0 ▶ default => sub { JSON->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
98 69 );
99  
100 =item B<ua>
101  
102 The L<Future::HTTP> instance to talk to the Chrome DevTools
103  
104 =cut
105  
106 has 'ua' => (
107 is => 'ro',
108 0 ▶ default => sub { Future::HTTP->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
109 69 );
110  
111 =item B<tab>
112  
113 Which tab to reuse (if any)
114  
115 =cut
116  
117 69 has 'tab' => (
118 is => 'rw',
119 );
120  
121 =item B<log>
122  
123 A premade L<Log::Log4perl> object to act as logger
124  
125 =cut
126  
127 69 has '_log' => (
128 is => 'ro',
129 default => \&_build_log,
130 );
131  
132 has 'receivers' => (
133 is => 'ro',
134 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 69 );
136  
137 69 has 'reader_fh' => (
138 is => 'ro',
139 );
140  
141 69 has 'writer_fh' => (
142 is => 'ro',
143 );
144  
145 has '_one_shot' => (
146 is => 'ro',
147 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
148 69 );
149  
150 has 'listener' => (
151 is => 'ro',
152 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
153 69 );
154  
155 has 'sequence_number' => (
156 is => 'rw',
157 0 ▶ default => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
158 69 );
159  
160 =item B<transport>
161  
162 The event-loop specific transport backend
163  
164 =cut
165  
166 69 has 'transport' => (
167 is => 'ro',
168 handles => ['future','sleep'],
169 );
170  
171 69 has 'is_connected' => (
172 is => 'rw',
173 );
174  
175 0 ▶around BUILDARGS => sub( $orig, $class, %args ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
176 0 $args{ _log } = delete $args{ 'log' };
177 0 $class->$orig( %args )
178 69 };
179  
180 =back
181  
182 =head2 C<< ->future >>
183  
184 my $f = $driver->future();
185  
186 Returns a backend-specific generic future
187  
188 =head2 C<< ->endpoint >>
189  
190 my $url = $driver->endpoint();
191  
192 Returns the URL endpoint to talk to for the connected tab
193  
194 =cut
195  
196 69 has 'json_log_fh' => (
197 is => 'rw', # actually, it isn't really rw, but set-once
198 );
199  
200 =head2 C<< json_log_fh >>
201  
202 Filehandle where all communications will be logged to, one line
203 per message/response. Each line will be of the format
204  
205 { type: "message", payload: { ... } }
206 { type: "reply", payload: { ... } }
207 { type: "event", payload: { ... } }
208  
209 The filehandle must be opened as :raw , as UTF-8 encoded bytes will
210 be written to it.
211  
212 =cut
213  
214 69 has 'endpoint' => (
215 is => 'rw', # actually, it isn't really rw, but set-once
216 );
217  
218 =head2 C<< ->add_listener >>
219  
220 my $l = $driver->add_listener(
221 'Page.domContentEventFired',
222 sub {
223 warn "The DOMContent event was fired";
224 },
225 );
226  
227 # ...
228  
229 undef $l; # stop listening
230  
231 Adds a callback for the given event name. The callback will be removed once
232 the return value goes out of scope.
233  
234 =cut
235  
236 0 ▶sub add_listener( $self, $event, $callback ) {
Subroutine add_listener: not called CC=2 SCAR=17.9
Pod documented
237 0 my $listener = Chrome::DevToolsProtocol::EventListener->new(
238 protocol => $self,
239 callback => $callback,
240 event => $event,
241 );
242 0 ▶ $self->listener->{ $event } ||= [];
Condition0%0/2
$self->listener->{$event} ||= []
l!l
00
MC/DC0%0/1
$self->listener->{$event} ||= []
$self->listener->{$event}
Truth table0%0/2
A
A= $self->listener->{$event}
Aresult
11
00
243 0 push @{ $self->listener->{ $event }}, $listener;
244 0 weaken $self->listener->{ $event }->[-1];
245 0 return $listener
246 }
247  
248 =head2 C<< ->remove_listener >>
249  
250 $driver->remove_listener($l);
251  
252 Explicitly remove a listener.
253  
254 =cut
255  
256 0 ▶sub remove_listener( $self, $listener ) {
Subroutine remove_listener: not called CC=4 SCAR=30.0
Pod documented
257 # $listener->{event} can be undef during global destruction
258 0 ▶ if( my $event = $listener->event ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $event = $listener->event) 0 0 0
259 0 ▶ my $l = $self->listener->{ $event } ||= [];
Condition0%0/2
$self->listener->{$event} ||= []
l!l
00
MC/DC0%0/1
$self->listener->{$event} ||= []
$self->listener->{$event}
Truth table0%0/2
A
A= $self->listener->{$event}
Aresult
11
00
260 0 @{$l} = grep { $_ != $listener }
261 0 grep { defined $_ }
262 0 @{$self->listener->{ $event }};
263 # re-weaken our references
264 0 for (0..$#$l) {
265 0 weaken $l->[$_];
266 };
267 };
268 }
269  
270 =head2 C<< ->log >>
271  
272 $driver->log('debug', "Warbling doodads", { doodad => 'this' } );
273  
274 Log a message
275  
276 =cut
277  
278 0 ▶sub log( $self, $level, $message, @args ) {
Subroutine log: not called CC=3 SCAR=24.8
Pod documented
279 0 my $logger = $self->_log;
280 0 ▶ if( !@args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @args) { } 0 0 0
281 0 $logger->$level( $message )
282 } else {
283 0 my $enabled = "is_$level";
284 0 ▶ $logger->$level( join " ", $message, Dumper @args )
Branch: 1 branch
BranchTrueFalseTotal
if $logger->$enabled 0 0 0
285 if( $logger->$enabled );
286 };
287 }
288  
289 =head2 C<< ->connect >>
290  
291 my $f = $driver->connect()->get;
292  
293 Asynchronously connect to the Chrome browser, returning a Future.
294  
295 =cut
296  
297 0 ▶sub connect( $self, %args ) {
Subroutine connect: not called CC=20 SCAR=60.4
Pod documented
298 0 my $s = $self;
299 0 weaken $s;
300 # If we are still connected to a different tab, disconnect from it
301 0 ▶ if( $self->transport and ref $self->transport and $self->transport->type ne 'pipe') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->transport and ref $self->transport and $self->transport->type ne "pipe") 0 0 0
Condition0%0/6
$self->transport and ref $self->transport and $self->transport->type ne "pipe"
!ll&&!rl&&r
000
$self->transport and ref $self->transport
!ll&&!rl&&r
000
MC/DC0%0/3
$self->transport and ref $self->transport and $self->transport->type ne "pipe"
$self->transport ref $self->transport $self->transport->type ne "pipe"
Truth table0%0/4
A and B and C
A= $self->transportB= ref $self->transportC= $self->transport->type ne "pipe"
ABCresult
0XX0
10X0
1100
1111
302 0 $self->transport->close();
303 };
304 # Kick off the connect
305 0 my $endpoint;
306 0 ▶ $args{ writer_fh } //= $self->writer_fh;
Condition0%0/3
$args{"writer_fh"} //= $self->writer_fh
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"writer_fh"} //= $self->writer_fh
$args{"writer_fh"} $self->writer_fh
Truth table0%0/3
A //= B
A= $args{"writer_fh"}B= $self->writer_fh
ABresult
1X1
011
000
307 0 ▶ $args{ reader_fh } //= $self->reader_fh;
Condition0%0/3
$args{"reader_fh"} //= $self->reader_fh
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"reader_fh"} //= $self->reader_fh
$args{"reader_fh"} $self->reader_fh
Truth table0%0/3
A //= B
A= $args{"reader_fh"}B= $self->reader_fh
ABresult
1X1
011
000
308 0 ▶ $args{ endpoint } //= $self->endpoint;
Condition0%0/3
$args{"endpoint"} //= $self->endpoint
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"endpoint"} //= $self->endpoint
$args{"endpoint"} $self->endpoint
Truth table0%0/3
A //= B
A= $args{"endpoint"}B= $self->endpoint
ABresult
1X1
011
000
309 0 ▶ if( $args{ writer_fh } and $args{ reader_fh }) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'writer_fh'} and $args{'reader_fh'}) { } 0 0 0
elsif ($args{'endpoint'}) { } 0 0 0
Condition0%0/3
$args{"writer_fh"} and $args{"reader_fh"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"writer_fh"} and $args{"reader_fh"}
$args{"writer_fh"} $args{"reader_fh"}
Truth table0%0/3
A and B
A= $args{"writer_fh"}B= $args{"reader_fh"}
ABresult
0X0
100
111
310 # Pipe connection
311 0 ▶ $args{ transport } ||= 'Chrome::DevToolsProtocol::Transport::Pipe';
Condition0%0/2
$args{"transport"} ||= "Chrome::DevToolsProtocol::Transport::Pipe"
l!l
00
MC/DC0%0/1
$args{"transport"} ||= "Chrome::DevToolsProtocol::Transport::Pipe"
$args{"transport"}
Truth table0%0/2
A
A= $args{"transport"}
Aresult
11
00
312 $endpoint = {
313 reader_fh => $args{ reader_fh },
314 writer_fh => $args{ writer_fh },
315 0 };
316  
317 } elsif( $args{ endpoint }) {
318 0 ▶ $endpoint = $args{ endpoint } || $self->endpoint;
Condition0%0/3
$args{'endpoint'} || $self->endpoint
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'endpoint'} || $self->endpoint
$args{'endpoint'} $self->endpoint
Truth table0%0/3
A || B
A= $args{'endpoint'}B= $self->endpoint
ABresult
1X1
011
000
319 0 $self->log('trace', "Using endpoint $endpoint");
320  
321 # Set these values so anyone reusing the parameters will find them
322 0 my $ws = URI->new( $endpoint );
323 0 ▶ if( $s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s) 0 0 0
324 0 $s->{port} = $ws->port;
325 0 $s->{host} = $ws->host;
326 };
327 };
328  
329 0 my $got_endpoint;
330 0 ▶ if( ! $endpoint ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $endpoint) { } 0 0 0
331 0 ▶ if( ! $self->port ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->port) 0 0 0
332 0 die "Can't connect without knowing the port?! " . $self->port;
333 };
334 0 ▶ $got_endpoint = $self->version_info()->then(sub( $info ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
335 0 $s->log('debug', "Found webSocket URL", $info );
336 #$self->tab( $info );
337 0 return Future->done( $info->{webSocketDebuggerUrl} );
338 0 });
339  
340 } else {
341 0 $got_endpoint = Future->done( $endpoint );
342 0 ▶ if( ! ref $endpoint ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $endpoint) 0 0 0
343 # We need to somehow find the tab id for our tab, so let's fake it:
344 0 ▶ $endpoint =~ m!/([^/]+)$!
Branch: 1 branch
BranchTrueFalseTotal
$endpoint =~ m[/([^/]+)$]u or die "Couldn't find tab id in '${endpoint}'" 0 0 0
345 or die "Couldn't find tab id in '$endpoint'";
346 $s->{tab} = {
347 0 targetId => $1,
348 };
349 };
350 };
351 0 ▶ $got_endpoint = $got_endpoint->then(sub($endpoint) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
352 0 $s->{ endpoint } = $endpoint;
353 0 return Future->done( $endpoint );
354 0 ▶ })->catch(sub(@args) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
355 #croak @args;
356 0 Future->fail( @args );
357 0 });
358 my $transport = delete $args{ transport }
359 0 ▶ || $self->transport
Condition0%0/5
delete $args{'transport'} || $self->transport || 'Chrome::DevToolsProtocol::Transport'
l!l
00
delete $args{"transport"} or $self->transport
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $args{'transport'} || $self->transport || 'Chrome::DevToolsProtocol::Transport'
delete $args{"transport"} $self->transport
Truth table0%0/3
A or B
A= delete $args{"transport"}B= $self->transport
ABresult
1X1
011
000
360 || 'Chrome::DevToolsProtocol::Transport';
361 0 ▶ if( ! ref $transport ) { # it's a classname
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $transport) 0 0 0
362 0 (my $transport_module = $transport) =~ s!::!/!g;
363 0 $transport_module .= '.pm';
364 0 require $transport_module;
365 0 $self->{transport} = $transport->new;
366 0 $transport = $self->{transport};
367 };
368 0 ▶ return $transport->connect( $self, $got_endpoint, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
369 0 ▶ if( $s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s) 0 0 0
370 0 $s->log( @_ )
371 }
372 #else {
373 # # We are in cleanup somewhere?!
374 # warn "@_";
375 #}
376 } )
377 #->on_ready(sub {
378 # use Data::Dumper;
379 # warn Dumper \@_;
380 #})
381 0 ▶ ->on_done(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
382 0 $s->is_connected(1);
383 0 });
384 };
385  
386 =head2 C<< ->close >>
387  
388 $driver->close();
389  
390 Shut down the connection to Chrome
391  
392 =cut
393  
394 0 ▶sub close( $self ) {
Subroutine close: not called CC=3 SCAR=24.8
Pod documented
395 0 ▶ if( my $t = $self->transport) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $self->transport) 0 0 0
396 0 ▶ if( ref $t ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $t) 0 0 0
397 0 undef $self->{transport};
398 0 $t->close();
399 };
400 };
401 };
402  
403 =head2 C<< ->sleep >>
404  
405 $driver->sleep(0.2)->get;
406  
407 Sleep for the amount of seconds in an event-loop compatible way
408  
409 =cut
410  
411 0 ▶sub DESTROY( $self ) {
Subroutine DESTROY: not called CC=1 SCAR=6.9
412 0 delete $self->{ua};
413 0 $self->close;
414 }
415  
416 =head2 C<< ->one_shot >>
417  
418 my $f = $driver->one_shot('Page.domContentEventFired')->get;
419  
420 Returns a future that resolves when the event is received
421  
422 =cut
423  
424 0 ▶sub one_shot( $self, @events ) {
Subroutine one_shot: not called CC=1 SCAR=6.9
Pod documented
425 0 my $result = $self->transport->future;
426 0 my $ref = $result;
427 0 weaken $ref;
428 0 my %events;
429 0 undef @events{ @events };
430 0 push @{ $self->_one_shot }, { events => \%events, future => \$ref };
431 0 $result
432 };
433  
434 69 my %stack;
435 my $r;
436 0 ▶sub on_response( $self, $connection, $message ) {
Subroutine on_response: not called CC=27 SCAR=66.3
Pod undocumented
437 0 my $response = eval { $self->json->decode( $message ) };
438 0 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
439 0 $self->log('error', $@ );
440 0 warn $message;
441 0 return;
442 };
443  
444 0 ▶ if( my $fh = $self->json_log_fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = $self->json_log_fh) 0 0 0
445 0 say $fh $self->json->encode( { type => 'response', payload => $response } );
446 };
447  
448 0 ▶ if( ! exists $response->{id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $response->{'id'}) { } 0 0 0
449 # Generic message, dispatch that:
450 0 ▶ if( my $error = $response->{error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $response->{"error"}) 0 0 0
451 0 $self->log('error', "Error response from Chrome", $error );
452 0 return;
453 };
454  
455 0 ▶ (my $handler) = grep { exists $_->{events}->{ $response->{method} } and ${$_->{future}} } @{ $self->_one_shot };
Condition0%0/3
exists $_->{'events'}{$response->{'method'}} && ${$_->{'future'};}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $_->{'events'}{$response->{'method'}} && ${$_->{'future'};}
exists $_->{'events'}{$response->{'method'}} ${$_->{'future'};}
Truth table0%0/3
A && B
A= exists $_->{'events'}{$response->{'method'}}B= ${$_->{'future'};}
ABresult
0X0
100
111
456 0 my $handled;
457 0 ▶ if( $handler ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler) 0 0 0
458 0 $self->log( 'trace', "Dispatching one-shot event", $response );
459 0 ${ $handler->{future} }->done( $response );
460  
461 # Remove the handler we just invoked
462 0 ▶ @{ $self->_one_shot } = grep { $_ and ${$_->{future}} and $_ != $handler } @{ $self->_one_shot };
Condition0%0/6
$_ && ${$_->{'future'};} && $_ != $handler
!ll&&!rl&&r
000
$_ and ${$_->{"future"};}
!ll&&!rl&&r
000
MC/DC0%0/3
$_ && ${$_->{'future'};} && $_ != $handler
$_ ${$_->{"future"};} $_ != $handler
Truth table0%0/4
A and B && C
A= $_B= ${$_->{"future"};}C= $_ != $handler
ABCresult
0XX0
10X0
1100
1111
463  
464 0 $handled++;
465 };
466  
467 0 ▶ if( my $listeners = $self->listener->{ $response->{method} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $listeners = $self->listener->{$response->{"method"}}) 0 0 0
468 0 @$listeners = grep { defined $_ } @$listeners;
469 #if( $self->_log->is_trace ) {
470 # if( $response->{method} ne 'Target.receivedMessageFromTarget' ) {
471 # $self->log( 'trace', "Notifying listeners", $response );
472 # };
473 #} else {
474 # if( $response->{method} ne 'Target.receivedMessageFromTarget' ) {
475 # $self->log( 'debug', sprintf "Notifying listeners for '%s'", $response->{method} );
476 # };
477 #};
478 0 for my $listener (@$listeners) {
479 0 eval {
480 0 $listener->notify( $response );
481 };
482 0 ▶ $self->log('error', $@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
483 0 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
484 };
485 # re-weaken our references
486 0 for (0..$#$listeners) {
487 0 ▶ weaken $listeners->[$_]
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::is_weak($listeners->[$_]) 0 0 0
488 if not isweak $listeners->[$_];
489 };
490 #if( $response->{method} ne 'Target.receivedMessageFromTarget' ) {
491 # $self->log('trace', "Message handled", $response);
492 #};
493  
494 0 $handled++;
495 };
496  
497 0 ▶ if( @{ $self->subscribers('message')} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->subscribers("message");}) 0 0 0
498 0 $self->emit('message', $response );
499 0 $handled++;
500 };
501  
502 0 ▶ if( ! $handled ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $handled) 0 0 0
503 0 ▶ if( $self->_log->is_trace ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_log->is_trace) { } 0 0 0
504 0 $self->log( 'trace', "Ignored message", $response );
505 } else {
506 0 my $frameId = $response->{params}->{frameId};
507 0 my $requestId = $response->{params}->{requestId};
508 0 ▶ if( $frameId || $requestId ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frameId or $requestId) { } 0 0 0
Condition0%0/3
$frameId or $requestId
l!l&&r!l&&!r
000
MC/DC0%0/2
$frameId or $requestId
$frameId $requestId
Truth table0%0/3
A or B
A= $frameIdB= $requestId
ABresult
1X1
011
000
509 0 ▶ $self->log( 'debug', sprintf "Ignoring '%s' (%s:%s)", $response->{method}, $frameId || '-', $requestId || '-');
Condition0%0/4
$frameId || '-'
l!l
00
$requestId || '-'
l!l
00
MC/DC0%0/1
$frameId || '-'
$frameId
MC/DC0%0/1
$requestId || '-'
$requestId
Truth table0%0/2
A
A= $frameId
Aresult
11
00
Truth table0%0/2
A
A= $requestId
Aresult
11
00
510 } else {
511 0 $self->log( 'debug', sprintf "Ignoring '%s'", $response->{method} );
512 };
513 };
514  
515 };
516 } else {
517  
518 0 my $id = $response->{id};
519 0 my $receiver = delete $self->{receivers}->{ $id };
520  
521 0 ▶ if( ! $receiver) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $receiver) { } 0 0 0
elsif ($receiver eq 'ignore') { } 0 0 0
elsif ($response->{'error'}) { } 0 0 0
522 0 $self->log( 'debug', "Ignored response to unknown receiver", $response )
523  
524 } elsif( $receiver eq 'ignore') {
525 # silently ignore that reply
526  
527 } elsif( $response->{error} ) {
528 0 $self->log( 'debug', "Replying to error $response->{id}", $response );
529 0 ▶ $receiver->die( join "\n", $response->{error}->{message},$response->{error}->{data} // '',$response->{error}->{code} // '');
Condition0%0/4
$response->{"error"}{"data"} // ""
l!l
00
$response->{"error"}{"code"} // ""
l!l
00
MC/DC0%0/1
$response->{"error"}{"data"} // ""
$response->{"error"}{"data"}
MC/DC0%0/1
$response->{"error"}{"code"} // ""
$response->{"error"}{"code"}
Truth table0%0/2
A
A= $response->{"error"}{"data"}
Aresult
11
00
Truth table0%0/2
A
A= $response->{"error"}{"code"}
Aresult
11
00
530 } else {
531 0 $self->log( 'trace', "Replying to $response->{id}", $response );
532 0 $receiver->done( $response->{result} );
533 };
534 };
535 }
536  
537 0 ▶sub next_sequence( $self ) {
Subroutine next_sequence: not called CC=1 SCAR=6.9
Pod undocumented
538 0 my( $val ) = $self->current_sequence;
539 0 $self->sequence_number( $val+1 );
540 0 $val
541 };
542  
543 0 ▶sub current_sequence( $self ) {
Subroutine current_sequence: not called CC=1 SCAR=6.9
Pod undocumented
544 0 $self->sequence_number
545 };
546  
547 0 ▶sub build_url( $self, %options ) {
Subroutine build_url: not called CC=5 SCAR=34.0
Pod undocumented
548 0 my $url;
549 0 ▶ if( ! ($options{ host } || $options{ port })
Branch: 1 branch
BranchTrueFalseTotal
if (not $options{'host'} || $options{'port'} and $self->{'endpoint'}) { } 0 0 0
Condition0%0/6
not $options{"host"} || $options{"port"} and $self->{"endpoint"}
!ll&&!rl&&r
000
$options{'host'} || $options{'port'}
l!l&&r!l&&!r
000
MC/DC0%0/3
not $options{"host"} || $options{"port"} and $self->{"endpoint"}
$options{'host'} $options{'port'} $self->{"endpoint"}
Truth table0%0/4
not(A || B) and C
A= $options{'host'}B= $options{'port'}C= $self->{"endpoint"}
ABCresult
1XX0
01X0
0000
0011
550 and $self->{endpoint}) {
551 # recycle our endpoint if we have it
552 0 $url = URI->new($self->{ endpoint });
553 0 $url->scheme('http');
554 0 $url->path('json');
555 0 $url = "$url";
556 } else {
557 0 $url = URI->new('json','http');
558 0 $url->scheme('http');
559 0 $url->host( $self->host );
560 0 $url->port( $self->port );
561 0 $url = "$url";
562 };
563 0 ▶ $url .= '/' . $options{domain} if $options{ domain };
Branch: 1 branch
BranchTrueFalseTotal
if $options{"domain"} 0 0 0
564 0 $url
565 };
566  
567 =head2 C<< $chrome->json_get >>
568  
569 my $data = $driver->json_get( 'version' )->get;
570  
571 Requests an URL and returns decoded JSON from the future
572  
573 =cut
574  
575 0 ▶sub json_get($self, $domain, %options) {
Subroutine json_get: not called CC=1 SCAR=6.9
Pod documented
576 0 my $url = $self->build_url( domain => $domain, %options );
577 0 $self->log('trace', "Fetching JSON from $url");
578 0 my $req = $self->ua->http_get( $url );
579 0 ▶ $req->then( sub( $payload, $headers ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
580 0 $self->log('trace', "JSON response", $payload);
581 0 Future->done( $self->json->decode( $payload ))
582 0 });
583 };
584  
585 0 ▶sub _send_packet( $self, $response, $method, %params ) {
Subroutine _send_packet: not called CC=5 SCAR=34.0
586 0 my $id = $self->next_sequence;
587 0 ▶ if( $response ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) 0 0 0
588 0 $self->{receivers}->{ $id } = $response;
589 };
590  
591 0 my $msg = {
592 id => 0+$id,
593 method => $method,
594 params => \%params
595 };
596 0 my $payload = eval {
597 0 $self->json->encode($msg);
598 };
599 0 ▶ if( my $err = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 0 0
600 0 $self->log('error', $@ );
601 };
602  
603 0 ▶ if( my $fh = $self->json_log_fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = $self->json_log_fh) 0 0 0
604 0 say $fh
605 $self->json->encode({
606 type => "message",
607 payload => $msg
608 });
609 };
610 0 ▶if( $method ne 'Target.sendMessageToTarget' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method ne "Target.sendMessageToTarget") 0 0 0
611 0 $self->log( 'trace', "Sent '$method' message", $payload );
612 };
613 0 my $result;
614 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
615 0 $result = $self->transport->send( $payload );
616 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
617 0 $self->log( 'error', "Sent '$method' message", $payload );
618 0 $self->log('error', $_ );
619 0 $result = Future->fail( $_ );
620 0 };
621 0 return $result
622 }
623  
624 =head2 C<< $chrome->send_packet >>
625  
626 $chrome->send_packet('Page.handleJavaScriptDialog',
627 accept => JSON::true,
628 );
629  
630 Sends a JSON packet to the remote end
631  
632 =cut
633  
634 0 ▶sub send_packet( $self, $topic, %params ) {
Subroutine send_packet: not called CC=1 SCAR=6.9
Pod documented
635 0 $self->_send_packet( undef, $topic, %params )
636 }
637  
638 =head2 C<< $chrome->send_message >>
639  
640 my $future = $chrome->send_message('DOM.querySelectorAll',
641 selector => 'p',
642 nodeId => $node,
643 );
644 my $nodes = $future->get;
645  
646 This function expects a response. The future will not be resolved until Chrome
647 has sent a response to this query.
648  
649 =cut
650  
651 0 ▶sub send_message( $self, $method, %params ) {
Subroutine send_message: not called CC=1 SCAR=6.9
Pod documented
652 0 my $response = $self->future;
653 0 $self->_send_packet( $response, $method, %params )->retain;
654 0 $response
655 }
656  
657 =head2 C<< $chrome->callFunctionOn >>
658  
659 =cut
660  
661 0 ▶sub callFunctionOn( $self, $function, %options ) {
Subroutine callFunctionOn: not called CC=1 SCAR=6.9
Pod documented
662 $self->send_message('Runtime.callFunctionOn',
663 functionDeclaration => $function,
664 returnByValue => JSON::true,
665 arguments => $options{ arguments },
666 objectId => $options{ objectId },
667 0 %options
668 )
669 };
670  
671 =head2 C<< $chrome->evaluate >>
672  
673 =cut
674  
675 0 ▶sub evaluate( $self, $string, %options ) {
Subroutine evaluate: not called CC=1 SCAR=6.9
Pod documented
676 0 $self->send_message('Runtime.evaluate',
677 expression => $string,
678 returnByValue => JSON::true,
679 %options
680 )
681 };
682  
683 =head2 C<< $chrome->eval >>
684  
685 my $result = $chrome->eval('2+2');
686  
687 Evaluates a Javascript string and returns the result.
688  
689 =cut
690  
691 0 ▶sub eval( $self, $string ) {
Subroutine eval: not called CC=1 SCAR=6.9
Pod documented
692 0 ▶ $self->evaluate( $string )->then(sub( $result ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
693 Future->done( $result->{result}->{value} )
694 0 });
695 };
696  
697 =head2 C<< $chrome->version_info >>
698  
699 print $chrome->version_info->get->{"Protocol-Version"};
700  
701 Returns the implemented ChromeDevTooslProtocol protocol version.
702  
703 =cut
704  
705 0 ▶sub version_info($self) {
Subroutine version_info: not called CC=1 SCAR=6.9
Pod documented
706 0 ▶ $self->json_get( 'version' )->then( sub( $payload ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
707 0 Future->done( $payload );
708 0 });
709 };
710  
711 =head2 C<< $chrome->protocol_version >>
712  
713 print $chrome->protocol_version->get;
714  
715 =cut
716  
717 0 ▶sub protocol_version($self) {
Subroutine protocol_version: not called CC=1 SCAR=6.9
Pod documented
718 0 ▶ $self->version_info->then( sub( $payload ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
719 0 Future->done( $payload->{"Protocol-Version"});
720 0 });
721 };
722  
723 =head2 C<< $target->getVersion >>
724  
725 Returns information about the Chrome instance we are connected to.
726  
727 =cut
728  
729 0 ▶sub getVersion( $self ) {
Subroutine getVersion: not called CC=1 SCAR=6.9
Pod documented
730 0 $self->send_message( 'Browser.getVersion' )
731 }
732  
733 =head2 C<< $chrome->get_domains >>
734  
735 my $schema = $chrome->get_domains->get;
736  
737 Returns the topics of this Chrome DevToolsProtocol implementation.
738  
739 =cut
740  
741 0 ▶sub get_domains( $self ) {
Subroutine get_domains: not called CC=1 SCAR=6.9
Pod documented
742 0 $self->send_message('Schema.getDomains');
743 }
744  
745 =head2 C<< $chrome->list_tabs >>
746  
747 my @tabs = $chrome->list_tabs->get();
748  
749 =cut
750  
751 0 ▶sub list_tabs( $self, $type = 'page' ) {
Subroutine list_tabs: not called CC=2 SCAR=17.9
Pod documented
752 0 ▶ return $self->json_get('list')->then(sub( $info ) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
753 0 ▶ @$info = grep { defined $type ? $_->{type} =~ /$type/i : 1 } @$info;
Branch: 1 branch
BranchTrueFalseTotal
defined $type ? : 0 0 0
754 0 Future->done( @$info );
755 0 });
756 };
757  
758 =head2 C<< $chrome->new_tab >>
759  
760 my $new_tab = $chrome->new_tab('https://www.google.com')->get;
761  
762 =cut
763  
764 0 ▶sub new_tab( $self, $url=undef, %options ) {
Subroutine new_tab: not called CC=2 SCAR=17.9
Pod documented
765 #my $u = $url ? '?' . $url : '';
766 0 $self->log('trace', "Creating new tab");
767 0 $self->createTarget( url => $url, %options );
768 };
769  
770 =head2 C<< $chrome->activate_tab >>
771  
772 $chrome->activate_tab( $tab )->get
773  
774 Brings the tab to the foreground of the application
775  
776 =cut
777  
778 0 ▶sub activate_tab( $self, $tab ) {
Subroutine activate_tab: not called CC=1 SCAR=6.9
Pod documented
779 0 my $url = $self->build_url( domain => 'activate/' . $tab->{id} );
780 0 $self->ua->http_get( $url );
781 };
782  
783 =head2 C<< $chrome->close_tab >>
784  
785 $chrome->close_tab( $tab )->get
786  
787 Closes the tab
788  
789 =cut
790  
791 0 ▶sub close_tab( $self, $tab ) {
Subroutine close_tab: not called CC=1 SCAR=6.9
Pod documented
792 0 my $url = $self->build_url( domain => 'close/' . $tab->{id} );
793 $self->ua->http_get( $url, headers => { 'Connection' => 'close' } )
794 ->catch(
795 0 ▶ sub{ #use Data::Dumper; warn Dumper \@_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
796 0 Future->done
797 0 });
798 };
799  
800 =head2 C<< $chrome->getTargets >>
801  
802 my @targets = $chrome->getTargets->get;
803  
804 Gets the list of available targets
805  
806 =cut
807  
808 0 ▶sub getTargets( $self ) {
Subroutine getTargets: not called CC=1 SCAR=6.9
Pod documented
809 0 ▶ $self->send_message('Target.getTargets')->then(sub( $info ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
810 #use Data::Dumper; warn Dumper $info;
811 0 Future->done( @{$info->{targetInfos}})
812 0 });
813 }
814  
815 =head2 C<< $target->getTargetInfo >>
816  
817 my $info = $chrome->getTargetInfo( $targetId )->get;
818 print $info->{title};
819  
820 Returns information about the current target
821  
822 =cut
823  
824 0 ▶sub getTargetInfo( $self, $targetId=undef ) {
Subroutine getTargetInfo: not called CC=2 SCAR=17.9
Pod documented
825 $self->send_message('Target.getTargetInfo',
826 maybe targetId => $targetId
827 0 ▶ )->then(sub( $info ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
828 Future->done( $info->{targetInfo})
829 0 });
830 }
831  
832 =head2 C<< $target->createTarget >>
833  
834 my $targetId = $chrome->createTarget(
835 url => 'about:blank',
836 width => 1280,
837 height => 800,
838 newWindow => JSON::false,
839 background => JSON::false,
840 )->get;
841 print $targetId;
842  
843 Creates a new target, optionally in a new window
844  
845 =cut
846  
847 0 ▶sub createTarget( $self, %options ) {
Subroutine createTarget: not called CC=2 SCAR=17.9
Pod documented
848 0 ▶ $options{ url } //= 'about:blank';
Condition0%0/2
$options{"url"} //= "about:blank"
l!l
00
MC/DC0%0/1
$options{"url"} //= "about:blank"
$options{"url"}
Truth table0%0/2
A
A= $options{"url"}
Aresult
11
00
849 $self->send_message('Target.createTarget',
850 0 ▶ %options )->then(sub( $info ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
851 Future->done( $info->{targetId})
852 0 });
853 }
854  
855 =head2 C<< $target->attachToTarget >>
856  
857 my $sessionId = $chrome->attachToTarget(
858 targetId => $targetId,
859 )->get;
860 print $sessionId;
861  
862 Attaches to the target so we receive events generated by the target
863  
864 =cut
865  
866 0 ▶sub attachToTarget( $self, %options ) {
Subroutine attachToTarget: not called CC=1 SCAR=6.9
Pod documented
867 $self->send_message('Target.attachToTarget',
868 0 ▶ %options )->then(sub( $info ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
869 Future->done( $info->{sessionId})
870 0 });
871 }
872  
873 =head2 C<< $target->closeTarget >>
874  
875 my $targetId = $chrome->closeTarget(
876 targetId => $targetId
877 )->get;
878  
879 Creates a new target
880  
881 =cut
882  
883 0 ▶sub closeTarget( $self, %options ) {
Subroutine closeTarget: not called CC=1 SCAR=6.9
Pod documented
884 0 $self->send_message('Target.closeTarget',
885 %options )
886 }
887  
888 =head2 C<< $target->getWindowForTarget >>
889  
890 my $info = $chrome->getWindowForTarget( $targetId )->get;
891 print $info->{windowId};
892  
893 Returns information about the window of the current target
894  
895 =cut
896  
897 0 ▶sub getWindowForTarget( $self, $targetId ) {
Subroutine getWindowForTarget: not called CC=1 SCAR=6.9
Pod documented
898 0 $self->send_message('Browser.getWindowForTarget',
899 targetId => $targetId
900 );
901 }
902  
903 =head2 C<< $target->setWindowBoundsForTarget >>
904  
905 my $info = $chrome->getWindowForTarget( $targetId )->get;
906 $info->{bounds}->{width} = 100;
907 $chrome->setWindowBounds( $info->{windowId}, $info->{bounds} )->get;
908  
909 Sets the window dimensions of the current target
910  
911 =cut
912  
913 0 ▶sub setWindowBounds( $self, $windowId, $bounds ) {
Subroutine setWindowBounds: not called CC=1 SCAR=6.9
Pod undocumented
914 0 $self->send_message('Browser.setWindowBounds',
915 windowId => 0+$windowId,
916 bounds => $bounds,
917 );
918 }
919  
920 =head2 C<< $chrome->getBrowserContexts >>
921  
922 my @browserContextIds = $chrome->getBrowserContexts->get;
923  
924 Gets the list of available browser contexts. These are separate sets of user
925 cookies etc.
926  
927 =cut
928  
929 0 ▶sub getBrowserContexts( $self ) {
Subroutine getBrowserContexts: not called CC=1 SCAR=6.9
Pod documented
930 0 ▶ $self->send_message('Target.getBrowserContexts')->then(sub( $info ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
931 #use Data::Dumper; warn Dumper $info;
932 0 Future->done( @{$info->{browserContextIds}})
933 0 });
934 }
935  
936  
937 package
938 Chrome::DevToolsProtocol::EventListener;
939 69 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
940 69 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
941 69 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
942 69 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
943 69 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
944  
945 69 our $VERSION = '0.80';
946  
947 69 has 'protocol' => (
948 is => 'ro',
949 weak_ref => 1,
950 );
951  
952 69 has 'callback' => (
953 is => 'ro',
954 );
955  
956 69 has 'event' => (
957 is => 'ro',
958 );
959  
960 0 ▶around BUILDARGS => sub( $orig, $class, %args ) {
Subroutine __ANON__: not called CC=4 SCAR=30.0
961 0 ▶ croak "Need an event" unless $args{ event };
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"event"} 0 0 0
962 0 ▶ croak "Need a callback" unless $args{ callback };
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"callback"} 0 0 0
963 0 ▶ croak "Need a DevToolsProtocol in protocol" unless $args{ protocol };
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"protocol"} 0 0 0
964 0 return $class->$orig( %args )
965 69 };
966  
967 0 ▶sub notify( $self, @info ) {
Subroutine notify: not called CC=1 SCAR=6.9
Pod undocumented
968 0 $self->callback->( @info )
969 }
970  
971 0 ▶sub unregister( $self ) {
Subroutine unregister: not called CC=2 SCAR=17.9
Pod undocumented
972 0 ▶ $self->protocol->remove_listener( $self )
Branch: 1 branch
BranchTrueFalseTotal
if $self->protocol 0 0 0
973 if $self->protocol; # it's a weak ref so it might have gone away already
974 0 undef $self->{protocol};
975 }
976  
977 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
978 0 $_[0]->unregister
979 }
980  
981 69 1;
982  
983 =head1 SEE ALSO
984  
985 The unofficial Chrome debugger API documentation at
986 L<https://github.com/buggerjs/bugger-daemon/blob/master/README.md#api>
987  
988 Chrome DevTools at L<https://chromedevtools.github.io/devtools-protocol/1-2>
989  
990 =head1 REPOSITORY
991  
992 The public repository of this module is
993 L<https://github.com/Corion/www-mechanize-chrome>.
994  
995 =head1 SUPPORT
996  
997 The public support forum of this module is L<https://perlmonks.org/>.
998  
999 =head1 BUG TRACKER
1000  
1001 Please report bugs in this module via the Github bug queue at
1002 L<https://github.com/Corion/WWW-Mechanize-Chrome/issues>
1003  
1004 =head1 AUTHOR
1005  
1006 Max Maischein C<corion@cpan.org>
1007  
1008 =head1 COPYRIGHT (c)
1009  
1010 Copyright 2010-2026 by Max Maischein C<corion@cpan.org>.
1011  
1012 =head1 LICENSE
1013  
1014 This module is released under the same terms as Perl itself.
1015  
1016 =cut
« Chrome/DevToolsProtocol/Target.pm ↑ Summary WWW/Mechanize/Chrome/URLBlacklist.pm »