bin/mcp-webbrowser

filter: Statementstmt 17.8% 75 / 420 Branchbran 1.7% 5 / 284 Conditioncond 1.2% 4 / 321 MC/DCmcdc 0.0% 0 / 169 Subroutinesub 27.1% 16 / 59 Podpod n/a totaltotal 7.9% 100 / 1253 CC 243 SCAR 76.9
CC
243
Cov
8%
receive_message
7310.0
tab_operation
812.0
permission
240.0
CRAP 2185.2
↑ Summary lib/AnyEvent/MCP/Worker/IMAP.pm »
1 #!/opt/bin/perl
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 mcp-webbrowser - expose opted-in browser tabs to MCP
8  
9 =head1 SYNOPSIS
10  
11 mcp-webbrowser --stdio
12 mcp-webbrowser --print-url
13 mcp-webbrowser --stdio --bind 127.0.0.1:31741
14 mcp-webbrowser --connect http://host:port/TOKEN/browser
15  
16 =head1 DESCRIPTION
17  
18 The companion userscript lets you explicitly grant an MCP client access to
19 individual browser tabs. Only opted-in tabs connect to this broker over
20 WebSocket; MCP clients can then discover those tabs, execute JavaScript in
21 their page context and add DOM elements. Code can inspect or change the
22 page, including logged-in content and forms, and cause side effects. Treat
23 allowing a tab as giving the MCP client access to that page, not merely its
24 URL or title. The broker still applies its own MCP permission rules.
25  
26 This executable is the broker, not a browser extension. The userscript
27 handles per-tab authorization and page execution. The broker cannot see
28 unapproved tabs merely because the userscript is installed.
29  
30 The HTTP/WebSocket listener stays active alongside MCP stdio or an outgoing
31 C<--connect>. Listener configuration, token derivation and URL discovery follow
32 C<mcp-vim>. All enabled users authenticate ordinary MCP requests, but only
33 C<connect_user> (default C<default>) may attach browser WebSockets. Explicit
34 static tokens may still authenticate both connection types.
35 Keep the broker URL private. The userscript accepts the C<http://> URL printed
36 by C<mcp-webbrowser --print-url>, converts it to WebSocket internally and
37 appends its own opaque per-tab target ID.
38  
39 =head1 USERSCRIPT INSTALLATION AND USE
40  
41 The self-contained userscript is C<webbrowser/mcp-webbrowser.user.js> in the
42 AnyEvent::MCP source distribution. Installing the Perl distribution does
43 not install it in a browser. Install a userscript manager first; see the
44 L<ScriptCat installation guide|https://docs.scriptcat.org/en/docs/use/use/>.
45 B<ScriptCat on Chromium and Firefox has been tested> (Firefox 153 on Linux,
46 including page execution, DOM insertion and navigation). ScriptCat is available
47 for Chrome, Edge and Firefox. Tampermonkey
48 provides the required C<GM_getTab>/C<GM_saveTab> APIs and may work, but
49 has not been tested with this userscript. Other managers are unverified.
50  
51 Install the source-distribution file through your userscript manager, or
52 open the published userscript URL in the browser and install it there:
53  
54 https://dist.schmorp.de/mcp-webbrowser/1/mcp-webbrowser.user.js
55  
56 The C</1/> is the major version of the private browser/broker protocol, not
57 the MCP protocol or Perl distribution version. The published script declares
58 that URL for installation and automatic updates; updates depend on the
59 userscript manager. The source file contains the entire userscript and
60 needs no external JavaScript import.
61  
62 Start the broker (directly or through your MCP client/multibroker) and obtain
63 its listener URL with:
64  
65 mcp-webbrowser --stdio
66 mcp-webbrowser --print-url
67  
68 The second command prints the URL and exits; run it with the same service
69 name and configuration as the running broker. It does not launch a second
70 listener. On a page you want to expose, select B<Allow MCP access to this
71 tab> from the userscript menu. The first time, enter the full URL printed
72 by C<--print-url> when prompted. Do not append a tab name or convert
73 C<http> to C<ws> yourself. The URL is remembered across tabs; the grant is
74 per tab, not per website. Pages remain unexposed until explicitly allowed,
75 and the menu on an unapproved tab contains only the Allow command.
76  
77 If C<mcp-multibroker> starts this broker as a stdio child, specify
78 C<--eager> after the child's closing C<---> so the browser listener is
79 available before the first MCP tool call:
80  
81 mcp-multibroker --stdio browser --- mcp-webbrowser --- --eager
82  
83 Without C<--eager>, the child does not start until an MCP tool needs it,
84 and browser tabs cannot connect before then.
85  
86 When allowed, the menu offers B<Disconnect MCP access to this tab>, the
87 broker URL configuration and independent toggles for a tab-title indicator
88 and a notification in the page when switching to that tab. Both indicators
89 are enabled by default. The title prefix is C<[○MCP]> while connecting,
90 C<[●MCP]> when connected and C<[⊗MCP]> after prolonged failure. The toast
91 shows the same status and disappears shortly after a successful connection.
92 The indicator prefix is omitted from titles reported to the broker.
93 The permission remains granted during temporary disconnections and normally
94 survives navigation or reload until explicitly revoked.
95  
96 The menu also offers B<Mark this tab for MCP> (or B<Unmark this MCP tab>).
97 Only one connected tab is marked at a time. The C<tabs> result distinguishes
98 the marked tab from the last tab explicitly allowed through the Allow command.
99 Reconnections and navigation never update the last-allowed selection. Marking
100 a tab does not grant additional permission to execute code.
101  
102 If a tab does not connect, check that the broker is running and its URL is
103 correct. Depending on browser settings, you may also need to allow the page
104 to access a local-loopback or local-network address; browser permission
105 blocking can look like a WebSocket failure. The title and toast indicators
106 report connectivity, not a trustworthy security boundary.
107  
108 B<Note:> ScriptCat versions before 1.5.0 could lose saved per-tab permissions
109 when Chromium replaced a discarded tab. After such a discard and restoration,
110 the user had to choose Allow again. This was reported in
111 L<https://github.com/scriptscat/scriptcat/issues/1791> and fixed in 1.5.0.
112  
113 =head1 FIRST TOOL CALL
114  
115 After allowing a tab, ask your assistant:
116  
117 Call tabs and show the connected tabs. On one of them, execute
118 document.title and show its result.
119  
120 Use a C<target> returned by C<tabs> for the C<execute> call. Listing
121 requires C<read> permission; even this simple JavaScript expression needs
122 C<exec> permission, because execution has full page authority. The
123 tool returns an object containing C<value>; there is no read-only mode.
124  
125 With a multibroker prefix C<browser>, the tools are C<browser.tabs>
126 and C<browser.execute>. If no tab is listed, confirm that the userscript
127 was allowed B<on that tab> and that its indicator shows a connection.
128 Installing the userscript does not grant access to any tabs.
129 An execution error or timeout does not prove that nothing happened;
130 do not blindly retry JavaScript that changes page state.
131  
132 =head1 TOOLS
133  
134 =over
135  
136 =item tabs
137  
138 Return C<{tabs: [...]}> with each connected tab's C<target>, current document
139 C<instance_id>, integer C<revision>, and client-reported C<url> and C<title>.
140 Boolean C<last_allowed> identifies the most recent explicit Allow operation;
141 boolean C<marked> identifies the tab chosen with the Mark menu command.
142 Both selections are broker-local, survive ordinary reconnection, and are
143 cleared when their tab session retires. Neither grants execution permission.
144 The revision starts at 1 and advances on document replacement, not on an
145 ordinary WebSocket reconnect or a same-document navigation. Disconnected tab
146 identities are retained for up to 30 seconds. This is cached registration
147 metadata, not a fresh DOM inspection. Requires C<read> permission.
148  
149 =item execute
150  
151 Run C<code> on C<target>. Optional C<instance_id> and C<revision> from C<tabs>
152 reject a replaced document before dispatch. C<timeout> defaults to 30 seconds
153 and can be 1..300 seconds. With C<navigate> false (the default), the browser
154 awaits the evaluation result. Results are C<{value: JSON_VALUE}>; exceptions
155 become tool errors.
156 Replies default to a 16 KiB UTF-8 JSON limit; C<max_bytes> can raise the
157 limit to 2 MiB (minimum 1024 bytes). The size includes the JSON envelope.
158 Oversized results yield an error instead of truncating JSON or disconnecting
159 the browser. Code has already executed when the size error is returned;
160 do not blindly retry commands with side effects.
161  
162 With C<navigate> true, the broker sends the command, awaits the browser's
163 C<ready> message, and sends C<proceed> before the browser evaluates it.
164 The broker then waits for either a new document registering on the same tab
165 or an observed same-document navigation. The result has C<navigation> equal
166 to C<document> or C<same-document>, the resulting C<revision>, and reported
167 URL/title. If neither completion is observed before the timeout, it returns
168 C<navigation: "timeout"> with a reason, current revision and available metadata.
169 Timeout does not imply that the command was not executed; no automatic retry
170 is attempted. One navigation operation at a time is allowed per tab.
171  
172 The browser client injects C<code> as body of C<async () =E<gt> CODE>
173 in the page's JavaScript world. Supply only CODE, not the arrow-function
174 wrapper. For example, C<({title: document.title})> returns an object;
175 C<async () =E<gt> ({title: document.title})> returns a function and is
176 rejected as a non-JSON result. Single expression yields its value without
177 C<return>; braced function body needs explicit C<return> for multiple
178 statements.
179 C<await> works inside either form. Declarations in the body do not persist
180 between calls unless assigned to page globals. Results are awaited. This
181 does not use C<eval> or C<Function>. Syntax errors can prevent the injected
182 script from starting; a temporary page-world error listener tries to return
183 the native syntax-error message, otherwise the client reports an injection
184 failure. Source injection is serialized until each script acknowledges
185 successful compilation or fails; asynchronous execution thereafter may
186 overlap. Code has full page-context authority, with no read-only evaluation
187 mode or sandbox claim.
188  
189 Cancellation and timeout send a best-effort cancel message. They cannot undo
190 already executed code or guarantee that JavaScript stops. Disconnect fails
191 ordinary pending calls without replaying them. A navigation request persists
192 across disconnects and may complete on the next document's registration.
193 Concurrent human navigation can satisfy a pending request; success reports
194 observation, not proof that the submitted code caused the navigation.
195  
196 =item add-element
197  
198 Insert an element through the userscript's C<GM_addElement> in the content
199 sandbox. This can bypass page CSP for stylesheets and scripts. Required
200 C<target> and C<tag>; optional C<parent> is a document CSS selector
201 (default: document root). C<attributes> contains HTML attribute strings,
202 while C<properties> contains DOM property values, such as C<textContent>
203 or C<innerHTML>. Properties are supplied during element creation; attributes
204 are set explicitly as well. An unsuccessful selector or insertion is an error.
205 The result is C<{value: {tag: TAG, id: ID_OR_NULL}}> and does not assert that
206 the caller's C<id> is unique. No automatic ID or registry is maintained: give
207 elements a distinctive C<id> or C<data-*> attribute when later page-context
208 C<execute> calls need to modify or remove them. Uses C<exec> permission and
209 supports the same optional C<instance_id> and C<revision> guards as C<execute>.
210  
211 =item list-tab-operations
212  
213 Ask the connected tab for additional operations it supports. Returns
214 C<{operations: [...]}> with operation descriptions and input schemas, or an
215 empty array if none are available. Unlike MCP tools, these are discovered
216 per tab and are not registered with the broker. Requires C<read> permission.
217 Supports optional C<instance_id> and C<revision> guards.
218  
219 =item call-tab-operation
220  
221 Call a tab-specific operation previously discovered with
222 C<list-tab-operations>. Supply C<target>, C<operation> and optional
223 C<arguments>. The browser implements and authorizes the operation; the
224 broker does not interpret its name or arguments. Returns C<{value: ...}> or
225 a tool error. Requires C<exec> permission, and supports the same document
226 guards, timeout and result-size options as C<execute>.
227  
228 =back
229  
230 =head1 PERMISSIONS
231  
232 The default rule file is C<~/.config/anyevent-mcp/rules/webbrowser-default.ini>,
233 under C<$XDG_CONFIG_HOME> when set. C<--name NAME> replaces C<default>.
234 C<read> covers tab and operation discovery; C<exec> covers arbitrary JavaScript
235 and tab-operation calls. A userscript or extension may apply additional
236 permissions for individual operations.
237  
238 Persistent C<exec> permission prompts select three independent restrictions:
239 Tab (this tab or any opted-in tab), JavaScript (this exact code or any
240 JavaScript), and Site (this origin, this host and scheme on any port, or
241 any origin). Browser operations offer their own exact/any operation choices
242 instead of JavaScript choices. All restrictions must match.
243 The defaults for JavaScript execution are this tab, any JavaScript, and
244 this origin. Tab-specific grants apply across navigations within that tab
245 until it is retired; they do not grant access to other opted-in tabs.
246 Only HTTP(S) origins offer persistent scopes: opaque origins such as C<null>
247 cannot distinguish different documents, so they allow one-time decisions only.
248 Saved tab-specific grants survive navigation and reconnects within that
249 tab's session; they are not bound to the current document instance.
250 While the broker remains running, tab-specific saved rules are removed when
251 that tab is explicitly disabled or has been disconnected for five minutes.
252 This can revoke permissions for a suspended tab that later reconnects; it
253 must be approved again. If the broker exits first, old tab-specific rules
254 may remain on disk. Rules covering all opted-in tabs are unaffected.
255 Execution rules now use independent C<match.tab>, C<match.code> and
256 C<match.origin> fields. Old combined C<match.subject> execution rules no
257 longer authorize executions and require reapproval; stale tab-specific
258 rules are still removed on tab retirement.
259 Rules without C<match.origin> match all origins, as do broad C<--auto-allow>
260 overrides. The broker derives origins from the URL already reported by the
261 userscript on connect, state changes and navigation, including older clients.
262 For HTTP(S), credentials and the path/query/fragment are excluded.
263 Origin identity relies on the connected userscript's reported URL, not on
264 independently authenticating the page's location.
265 C<--ask> selects the same helper, tty, yad, mcp or auto modes as other servers.
266  
267 =head1 OPTIONS
268  
269 Shared switches are documented in
270 L<AnyEvent::MCP::Server::Simple/COMMON COMMAND-LINE OPTIONS>.
271 The following shared switch has browser-specific behavior.
272  
273 =over
274  
275 =item --ask MODE
276  
277 Permission prompts: C<helper> (default), C<tty>, C<yad>, C<mcp> or
278 C<auto>, or C<none>. tty/yad force local interface; mcp delegates to
279 client, auto prefers native elicitation when offered, and none denies
280 unmatched requests. See L</PERMISSIONS>.
281  
282 =back
283  
284 =head2 Shared-option behavior
285  
286 =over
287  
288 =item --stdio
289  
290 The browser's auxiliary HTTP/WebSocket listener stays active while
291 MCP uses stdio.
292  
293 =item --connect URL
294  
295 The auxiliary browser HTTP/WebSocket listener stays active while
296 MCP connects to an upstream multibroker.
297  
298 =item --auto-allow TOOL[,ACTION[,CAPABILITY]]
299  
300 For example, C<--auto-allow '*,*'> allows discovery and JavaScript
301 execution in explicitly opted-in tabs. The executed page code then
302 has full authority within that page; use this only when intended.
303  
304 =back
305  
306 =head1 BROWSER PROTOCOL
307  
308 WebSocket authentication happens at C</TOKEN/TARGET>. C<TARGET> is one
309 percent-encoded path component. Before ordinary messages, send:
310  
311 ["mcp-webbrowser", 1, 2, INSTANCE_ID, {"url": URL, "title": TITLE}]
312  
313 C<INSTANCE_ID> identifies one loaded document. Keep it across reconnects of that
314 document and change it on navigation/reload. The private browser protocol is
315 1.2, independent of MCP and the old xpcse browser-transfer protocol. Compatible
316 older/newer minor versions can connect. Version 1.0 clients support page
317 execution but not additional tab operations; discovery returns an empty list
318 and unsupported calls fail without disconnecting the browser.
319 The broker answers C<["welcome", TARGET, INSTANCE_ID, REVISION]>.
320 The requested target represents a browser tab: a different document with the
321 same target replaces its previous connection and increments the revision.
322 Reconnecting the same document retains that revision. A disconnected tab's
323 identity expires after 30 seconds with no pending navigation. No page is
324 exposed before a valid hello.
325  
326 A tab session starts on first hello, remains active across temporary socket
327 disconnects and document replacements, and finishes on an explicit C<quit>,
328 after 30 seconds of inactivity, or when the broker stops. Session start
329 and finish are logged at C<info>; ordinary socket connection and disconnection
330 events are logged at C<trace>. Revision and request IDs are transmitted as
331 JSON numbers, even when Perl string operations have touched those scalars.
332 Approved JavaScript is logged at C<trace> when dispatched for ordinary evaluation,
333 or when C<proceed> is sent for navigation (never during the permission prompt).
334  
335 Broker to browser:
336  
337 ["call", ID, "execute", {"code": CODE, "max_bytes": BYTES}]
338 ["call", ID, "navigate", {"code": CODE, "max_bytes": BYTES}]
339 ["call", ID, "add_element", {"tag": TAG, "parent": SELECTOR, "attributes": ATTRS, "properties": PROPS}]
340 ["call", ID, "list_tab_operations", {"max_bytes": BYTES}]
341 ["call", ID, "call_tab_operation", {"operation": NAME, "arguments": ARGS, "max_bytes": BYTES}]
342 ["proceed", ID]
343 ["cancel", ID]
344 ["marked", true|false] (protocol 1.2)
345  
346 Browser to broker:
347  
348 ["reply", ID, true, JSON_VALUE]
349 ["reply", ID, false, ERROR_STRING]
350 ["ready", ID]
351 ["navigated", ID, {"url": URL, "title": TITLE}]
352 ["state", {"url": URL, "title": TITLE}]
353 ["allowed"]
354 ["mark", true|false]
355 ["quit"]
356  
357 The browser replies C<ready> to a C<navigate> call without evaluating it.
358 Only C<proceed> starts evaluation. C<navigated> reports a completed
359 same-document navigation; a different document's hello is the other
360 completion path. C<reply> with C<false> can report an evaluation failure.
361 Only JSON values cross the wire; undefined becomes null. Errors are strings.
362 For C<list_tab_operations>, a successful reply is an array of operation
363 descriptions; an empty array means no additional operations. The broker
364 wraps it as C<{operations: [...]}>. C<call_tab_operation> uses the usual
365 C<{value: ...}> reply. The distributed userscript currently offers no
366 additional operations and returns an empty list or an operation error.
367 Late replies after cancellation are ignored. State updates replace cached
368 metadata. Malformed messages close the connection with status 1002.
369 The initial hello has a 15-second deadline; a silent connection is closed.
370  
371 The distributed userscript evaluates in the page's JavaScript world via an
372 injected script and returns serialized results through DOM events. Its
373 Navigation API listener is installed in that same world. The broker cannot
374 authenticate page-reported values or infer application-rendering readiness
375 from navigation success. The userscript retries disconnected WebSockets;
376 it has no event subscriptions or separate SPA revision counter.
377  
378 =cut
379  
380 2 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
381  
382 2 ▶use JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
383 2 ▶use Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
384 2 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
385  
386 2 ▶use AnyEvent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
387 2 ▶use AnyEvent::Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
388 2 ▶use AnyEvent::Handle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
389 2 ▶use AnyEvent::WebSocket::Server ();
Subroutine BEGIN: called CC=1 SCAR=0.0
390 2 ▶use AnyEvent::WebSocket::Message ();
Subroutine BEGIN: called CC=1 SCAR=0.0
391 2 ▶use AnyEvent::MCP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
392 2 ▶use AnyEvent::MCP::Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
393 2 ▶use AnyEvent::MCP::Server::HTTP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
394 2 ▶use AnyEvent::MCP::Server::JSONL ();
Subroutine BEGIN: called CC=1 SCAR=0.0
395 2 ▶use AnyEvent::MCP::Server::Permissions ();
Subroutine BEGIN: called CC=1 SCAR=0.0
396 2 ▶use AnyEvent::MCP::Server::Toolset ();
Subroutine BEGIN: called CC=1 SCAR=0.0
397  
398 2 ▶use AnyEvent::MCP::Server::Simple qw(mcp_getoptions mcp_tool_help mcp_log mcp_log_request);
Subroutine BEGIN: called CC=1 SCAR=0.0
399  
400 2 my $options = mcp_getoptions
401 {
402 usage => "Usage: mcp-webbrowser [OPTION]...\n",
403 path => "/",
404 description => "Expose connected browser tabs to MCP; HTTP/WebSocket stays active with stdio or --connect.",
405 examples => " mcp-webbrowser --auto-allow '*,*' # all actions in opted-in tabs",
406 }
407 ;
408 1 ▶@ARGV and die "Unexpected arguments\n";
Branch: 1 branch
BranchTrueFalseTotal
@ARGV and die "Unexpected arguments\n" 0 1 1
409  
410 1 my $ask = delete $options->{ask};
411 1 my $name = $options->{name};
412 1 my $connect = $options->{connect};
413 1 ▶my $stdio = $options->{stdio} || !defined $connect && $ENV{PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL} eq "stdio";
Condition33%2/6
$options->{'stdio'} || !defined($connect) && $ENV{'PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL'} eq 'stdio'
l!l&&r!l&&!r
001
not defined $connect and $ENV{"PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL"} eq "stdio"
!ll&&!rl&&r
010
MC/DC0%0/3
$options->{'stdio'} || !defined($connect) && $ENV{'PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL'} eq 'stdio'
$options->{'stdio'} not defined $connect $ENV{"PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL"} eq "stdio"
Truth table25%1/4
A || not(B) and C
A= $options->{'stdio'}B= not defined $connectC= $ENV{"PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL"} eq "stdio"
ABCresult
1XX1
0111
00X0
0100
414 1 ▶!($stdio && defined $connect) or die "Cannot combine --stdio and --connect\n";
Branch: 1 branch
BranchTrueFalseTotal
($stdio and defined $connect) and die "Cannot combine --stdio and --connect\n" 0 1 1
Condition33%1/3
$stdio and defined $connect
!ll&&!rl&&r
100
MC/DC0%0/2
$stdio and defined $connect
$stdio defined $connect
Truth table33%1/3
A and B
A= $stdioB= defined $connect
ABresult
0X0
100
111
415  
416 1 my $config = AnyEvent::MCP::Config::global_config ();
417 1 my %http = $config->http_settings ($name, $options->{bind});
418  
419 1 ▶$http{tokens} = $options->{tokens} if $options->{tokens};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"tokens"} 0 1 1
420 1 ▶$http{connect_tokens} = $options->{tokens} if $options->{tokens};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"tokens"} 0 1 1
421 1 my %token = map { $_ => 1 } @{$http{connect_tokens}};
422 1 my $json = JSON::XS->new->utf8->max_depth (64);
423 1 my (%connections, %pages, %tabs);
424 1 my ($last_allowed, $marked);
425 1 my $done = AE::cv;
426  
427 my $permissions = AnyEvent::MCP::Server::Permissions->new (
428 name => "webbrowser-$name",
429 actions => {
430 read => "List connected browser tabs and their reported metadata",
431 exec => "Execute JavaScript or tab operations in a connected browser page",
432 },
433 (defined $ask ? (ask => $ask) : ()),
434 1 ▶ auto_rules => [@{$options->{auto_rules} || []},
Branch: 1 branch
BranchTrueFalseTotal
defined $ask ? : 0 1 1
Condition50%1/2
$options->{'auto_rules'} || []
l!l
01
MC/DC0%0/1
$options->{'auto_rules'} || []
$options->{'auto_rules'}
Truth table50%1/2
A
A= $options->{'auto_rules'}
Aresult
11
00
435 $config->auto_rules ($name)],
436 );
437  
438 0 ▶sub page_label {
Subroutine page_label: not called CC=3 SCAR=24.8
439 0 my ($state) = @_;
440  
441 0 my ($title, $url) = @{$state}{qw(title url)};
442 0 s/[\x00-\x1f\x7f]/ /g for $title, $url;
443  
444 0 ▶ (length $title ? (substr $title, 0, 80) . " - " : "")
Branch: 1 branch
BranchTrueFalseTotal
length $title ? : 0 0 0
445 . (substr $url, 0, 160)
446 }
447  
448 0 ▶sub code_preview {
Subroutine code_preview: not called CC=3 SCAR=24.8
449 0 my ($code, $limit) = @_;
450  
451 0 ▶ $limit //= 160;
Condition0%0/2
$limit //= 160
l!l
00
MC/DC0%0/1
$limit //= 160
$limit
Truth table0%0/2
A
A= $limit
Aresult
11
00
452 0 my $short = substr $code, 0, $limit;
453 0 ▶ $short .= "..." if length $code > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if length $code > $limit 0 0 0
454 0 $json->encode ($short)
455 }
456  
457 0 ▶sub url_preview {
Subroutine url_preview: not called CC=2 SCAR=17.9
458 0 my $url = shift;
459  
460 0 $url =~ s/[?#].*//s;
461 0 $url =~ s{(://)[^/]*@}{$1};
462 0 $url =~ s/[\x00-\x1f\x7f]/ /g;
463 0 my $short = substr $url, 0, 64;
464 0 ▶ $short .= "..." if length $url > 64;
Branch: 1 branch
BranchTrueFalseTotal
if length $url > 64 0 0 0
465 0 $json->encode ($short)
466 }
467  
468 0 ▶sub browser_origin {
Subroutine browser_origin: not called CC=2 SCAR=17.9
469 0 my ($scheme, $authority) = $_[0] =~ m{\A(https?://)([^/?#]+)};
470 0 ▶ return "null" unless defined $authority;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $authority 0 0 0
471 0 $authority =~ s/.*@//;
472 0 $scheme . $authority
473 }
474  
475 0 ▶sub browser_state {
Subroutine browser_state: not called CC=1 SCAR=6.9
476 0 my ($state) = @_;
477 0 +{ %$state, origin => browser_origin ($state->{url}) }
478 }
479  
480 0 ▶sub permission {
Subroutine permission: not called CC=15 SCAR=54.8
481 0 my ($req, $action, $target, $code, $kind) = @_;
482  
483 0 my %dimensions;
484  
485 0 ▶ if (defined $target) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target) { } 0 0 0
486 0 ▶ my $page = $pages{$target} or die "Browser page is not connected\n";
Branch: 1 branch
BranchTrueFalseTotal
my $page = $pages{$target} or die "Browser page is not connected\n" 0 0 0
487 0 my $label = (page_label $page->{state}) . " (tab " . (substr $target, 0, 8) . ")";
488  
489 0 my $coder = JSON::XS->new->allow_nonref;
490 0 ▶ my $operation = defined $kind && $kind eq "operation";
Condition0%0/3
defined $kind && $kind eq 'operation'
!ll&&!rl&&r
000
MC/DC0%0/2
defined $kind && $kind eq 'operation'
defined $kind $kind eq 'operation'
Truth table0%0/3
A && B
A= defined $kindB= $kind eq 'operation'
ABresult
0X0
100
111
491 0 ▶ my $prefix = $operation ? "operation:" : "execute:";
Branch: 1 branch
BranchTrueFalseTotal
$operation ? : 0 0 0
492 0 my $id = $coder->encode ($target);
493 0 my $command = $prefix . $coder->encode ($code);
494 0 (my $preview = $code) =~ s/\s+/ /g;
495 0 ▶ $preview = substr ($preview, 0, 87) . "..." if length $preview > 90;
Branch: 1 branch
BranchTrueFalseTotal
if length $preview > 90 0 0 0
496  
497  
498 $dimensions{tab} = {
499 0 label => "Tab",
500 value => $id,
501 choices => [
502 { label => "This tab: $label", match => $id },
503 { label => "Any opted-in tab, including future tabs", match => "^" },
504 ],
505 default => $id,
506 };
507 $dimensions{code} = {
508 0 ▶ label => $operation ? "Operation" : "JavaScript",
Branch: 4 branches
BranchTrueFalseTotal
$operation ? : 0 0 0
$operation ? : 0 0 0
$operation ? : 0 0 0
$operation ? : 0 0 0
509 value => $command,
510 choices => [
511 { label => ($operation ? "This exact operation: " : "This exact code: ") . $preview,
512 match => $command },
513 { label => $operation ? "Any operation" : "Any JavaScript", match => "^$prefix" },
514 ],
515 default => $operation ? $command : "^$prefix",
516 };
517  
518 0 $target = $label;
519 0 my $origin = $page->{state}{origin};
520 0 my @origins;
521 0 ▶ if ($origin =~ m{\Ahttps?://}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($origin =~ m[\Ahttps?://]u) 0 0 0
522 0 @origins = ({ label => "This origin: $origin", match => $origin });
523 0 (my $host_origin = $origin) =~ s/:[0-9]+\z//;
524 0 push @origins, {
525 label => "This scheme and host: $host_origin (any port)",
526 match => "^" . (quotemeta $host_origin) . "(?::[0-9]+)?\\z",
527 };
528 0 push @origins, { label => "Any origin, including after navigation", match => "^" };
529 }
530 $dimensions{origin} = {
531 0 ▶ label => "Site", value => $origin, choices => \@origins,
Branch: 1 branch
BranchTrueFalseTotal
@origins ? : 0 0 0
532 (@origins ? (default => $origin) : ()),
533 };
534 } else {
535 $dimensions{subject} = {
536 0 label => "Tab discovery", value => "tabs",
537 choices => [{ label => "The list of connected browser tabs", match => "tabs" }],
538 };
539 }
540  
541 $req->require_permission ({
542 0 ▶ tool => $req->{tool}, action => $action,
Branch: 2 branches
BranchTrueFalseTotal
defined $target ? : 0 0 0
defined $kind && $kind eq 'operation' ? : 0 0 0
Condition0%0/3
defined $kind and $kind eq "operation"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $kind and $kind eq "operation"
defined $kind $kind eq "operation"
Truth table0%0/3
A and B
A= defined $kindB= $kind eq "operation"
ABresult
0X0
100
111
543 dimensions => \%dimensions,
544 sandbox_grants => [],
545 reason => defined $target ? (defined $kind && $kind eq "operation"
546 ? "Call browser operation in '$target'\n\n$code"
547 : "Execute JavaScript in '$target'\n\n$code")
548 : "List connected browser tabs or tab operations",
549 });
550 }
551  
552 0 ▶sub send_message {
Subroutine send_message: not called CC=1 SCAR=6.9
553 0 my ($page, $message) = @_;
554  
555 0 $page->{conn}->send (AnyEvent::WebSocket::Message->new (opcode => 1, body => $json->encode ($message)));
556 }
557  
558 0 ▶sub finish_call {
Subroutine finish_call: not called CC=9 SCAR=45.0
559 0 my ($page, $id, $success, $value) = @_;
560  
561 0 ▶ my $entry = delete $page->{pending}{$id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = delete $page->{"pending"}{$id} or return 0 0 0
562 0 my $req = $entry->{request};
563 0 ▶ return if $req->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $req->{"finished"} 0 0 0
564  
565 0 ▶ if ($success && $entry->{result_key} && $entry->{result_key} eq "operations" && ref $value ne "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if ($success and $entry->{'result_key'} and $entry->{'result_key'} eq 'operations' and ref $value ne 'ARRAY') { } 0 0 0
elsif ($success) { } 0 0 0
Condition0%0/9
$success and $entry->{"result_key"} and $entry->{"result_key"} eq "operations" and ref $value ne "ARRAY"
!ll&&!rl&&r
000
$success and $entry->{"result_key"} and $entry->{"result_key"} eq "operations"
!ll&&!rl&&r
000
$success and $entry->{"result_key"}
!ll&&!rl&&r
000
MC/DC0%0/4
$success and $entry->{"result_key"} and $entry->{"result_key"} eq "operations" and ref $value ne "ARRAY"
$success $entry->{"result_key"} $entry->{"result_key"} eq "operations" ref $value ne "ARRAY"
Truth table0%0/5
A and B and C and D
A= $successB= $entry->{"result_key"}C= $entry->{"result_key"} eq "operations"D= ref $value ne "ARRAY"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
566 0 $req->error ("Browser operation list is not an array");
567 } elsif ($success) {
568 0 ▶ $req->result ({ ($entry->{result_key} || "value") => $value });
Condition0%0/2
$entry->{'result_key'} || 'value'
l!l
00
MC/DC0%0/1
$entry->{'result_key'} || 'value'
$entry->{'result_key'}
Truth table0%0/2
A
A= $entry->{'result_key'}
Aresult
11
00
569 } else {
570 0 $req->error ($value);
571 }
572 }
573  
574 0 ▶sub tab_operation {
Subroutine tab_operation: not called CC=28 SCAR=67.0
575 0 my ($req, $args, $method) = @_;
576  
577 defined $args->{target} && !ref $args->{target} && length $args->{target}
578 0 ▶ or die "Invalid browser target\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{"target"} and not ref $args->{"target"} and length $args->{"target"} or die "Invalid browser target\n" 0 0 0
Condition0%0/6
defined $args->{"target"} and not ref $args->{"target"} and length $args->{"target"}
!ll&&!rl&&r
000
defined $args->{"target"} and not ref $args->{"target"}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $args->{"target"} and not ref $args->{"target"} and length $args->{"target"}
defined $args->{"target"} not ref $args->{"target"} length $args->{"target"}
Truth table0%0/4
A and not(B) and C
A= defined $args->{"target"}B= not ref $args->{"target"}C= length $args->{"target"}
ABCresult
0XX0
10X0
1100
1111
579 0 my $list = $method eq "list_tab_operations";
580 0 ▶ unless ($list) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($list) 0 0 0
581 defined $args->{operation} && !ref $args->{operation} && length $args->{operation}
582 0 ▶ or die "Invalid browser operation name\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{"operation"} and not ref $args->{"operation"} and length $args->{"operation"} or die "Invalid browser operation name\n" 0 0 0
Condition0%0/6
defined $args->{"operation"} and not ref $args->{"operation"} and length $args->{"operation"}
!ll&&!rl&&r
000
defined $args->{"operation"} and not ref $args->{"operation"}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $args->{"operation"} and not ref $args->{"operation"} and length $args->{"operation"}
defined $args->{"operation"} not ref $args->{"operation"} length $args->{"operation"}
Truth table0%0/4
A and not(B) and C
A= defined $args->{"operation"}B= not ref $args->{"operation"}C= length $args->{"operation"}
ABCresult
0XX0
10X0
1100
1111
583 0 ▶ !exists $args->{arguments} || ref $args->{arguments} eq "HASH"
Branch: 1 branch
BranchTrueFalseTotal
not exists $args->{"arguments"} or ref $args->{"arguments"} eq "HASH" or die "Browser operation arguments must be an object\n" 0 0 0
Condition0%0/3
not exists $args->{"arguments"} or ref $args->{"arguments"} eq "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
not exists $args->{"arguments"} or ref $args->{"arguments"} eq "HASH"
not exists $args->{"arguments"} ref $args->{"arguments"} eq "HASH"
Truth table0%0/3
not(A) or B
A= not exists $args->{"arguments"}B= ref $args->{"arguments"} eq "HASH"
ABresult
1X1
011
000
584 or die "Browser operation arguments must be an object\n";
585 }
586  
587 0 ▶ my $page = $pages{$args->{target}} or die "Browser page is not connected\n";
Branch: 1 branch
BranchTrueFalseTotal
my $page = $pages{$args->{"target"}} or die "Browser page is not connected\n" 0 0 0
588 !defined $args->{instance_id} || $args->{instance_id} eq $page->{instance_id}
589 0 ▶ or die "Browser document instance changed\n";
Branch: 1 branch
BranchTrueFalseTotal
not defined $args->{"instance_id"} or $args->{"instance_id"} eq $page->{"instance_id"} or die "Browser document instance changed\n" 0 0 0
Condition0%0/3
not defined $args->{"instance_id"} or $args->{"instance_id"} eq $page->{"instance_id"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $args->{"instance_id"} or $args->{"instance_id"} eq $page->{"instance_id"}
not defined $args->{"instance_id"} $args->{"instance_id"} eq $page->{"instance_id"}
Truth table0%0/3
not(A) or B
A= not defined $args->{"instance_id"}B= $args->{"instance_id"} eq $page->{"instance_id"}
ABresult
1X1
011
000
590 !defined $args->{revision} || $args->{revision} == $page->{tab}{revision}
591 0 ▶ or die "Browser document revision changed (now $page->{tab}{revision})\n";
Branch: 1 branch
BranchTrueFalseTotal
not defined $args->{"revision"} or $args->{"revision"} == $page->{"tab"}{"revision"} or die qq[Browser document revision changed (now $page->{"tab"}{"revision"})\n] 0 0 0
Condition0%0/3
not defined $args->{"revision"} or $args->{"revision"} == $page->{"tab"}{"revision"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $args->{"revision"} or $args->{"revision"} == $page->{"tab"}{"revision"}
not defined $args->{"revision"} $args->{"revision"} == $page->{"tab"}{"revision"}
Truth table0%0/3
not(A) or B
A= not defined $args->{"revision"}B= $args->{"revision"} == $page->{"tab"}{"revision"}
ABresult
1X1
011
000
592 0 ▶ !$page->{tab}{navigation} or die "Navigation already in progress for this tab\n";
Branch: 1 branch
BranchTrueFalseTotal
$page->{"tab"}{"navigation"} and die "Navigation already in progress for this tab\n" 0 0 0
593  
594 0 ▶ if ($list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list) { } 0 0 0
595 0 permission $req, "read";
596 } else {
597 permission $req, exec => $args->{target},
598 0 ▶ $json->encode ({ operation => $args->{operation}, arguments => $args->{arguments} || {} }), "operation";
Condition0%0/2
$args->{'arguments'} || {}
l!l
00
MC/DC0%0/1
$args->{'arguments'} || {}
$args->{'arguments'}
Truth table0%0/2
A
A= $args->{'arguments'}
Aresult
11
00
599 }
600 0 ▶ $pages{$args->{target}} && $pages{$args->{target}} == $page
Branch: 1 branch
BranchTrueFalseTotal
$pages{$args->{"target"}} and $pages{$args->{"target"}} == $page or die "Browser document changed while permission was pending\n" 0 0 0
Condition0%0/3
$pages{$args->{"target"}} and $pages{$args->{"target"}} == $page
!ll&&!rl&&r
000
MC/DC0%0/2
$pages{$args->{"target"}} and $pages{$args->{"target"}} == $page
$pages{$args->{"target"}} $pages{$args->{"target"}} == $page
Truth table0%0/3
A and B
A= $pages{$args->{"target"}}B= $pages{$args->{"target"}} == $page
ABresult
0X0
100
111
601 or die "Browser document changed while permission was pending\n";
602  
603 0 ▶ if ($page->{minor} < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page->{"minor"} < 1) 0 0 0
604 0 ▶ $req->result ({ operations => [] }) if $list;
Branch: 1 branch
BranchTrueFalseTotal
if $list 0 0 0
605 0 ▶ $req->error ("Browser tab operations require protocol 1.1") unless $list;
Branch: 1 branch
BranchTrueFalseTotal
unless $list 0 0 0
606 0 return;
607 }
608  
609 0 my $id = ++$page->{rpcid};
610 0 ▶ my $entry = $page->{pending}{$id} = {
Branch: 1 branch
BranchTrueFalseTotal
$list ? : 0 0 0
611 request => $req, result_key => $list ? "operations" : "value",
612 };
613 0 weaken (my $weak = $page);
614 0 ▶ $entry->{timer} = AE::timer $args->{timeout} // 30, 0, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
615 0 ▶ return unless $weak && exists $weak->{pending}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $weak and exists $weak->{"pending"}{$id} 0 0 0
Condition0%0/3
$weak and exists $weak->{"pending"}{$id}
!ll&&!rl&&r
000
MC/DC0%0/2
$weak and exists $weak->{"pending"}{$id}
$weak exists $weak->{"pending"}{$id}
Truth table0%0/3
A and B
A= $weakB= exists $weak->{"pending"}{$id}
ABresult
0X0
100
111
616 0 send_message $weak, ["cancel", $id*1];
617 0 finish_call $weak, $id, 0, "Browser tab operation timed out";
618 0 ▶ };
Condition0%0/2
$args->{"timeout"} // 30
l!l
00
MC/DC0%0/1
$args->{"timeout"} // 30
$args->{"timeout"}
Truth table0%0/2
A
A= $args->{"timeout"}
Aresult
11
00
619 0 ▶ $req->{on_cancel} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
620 0 ▶ return unless $weak && delete $weak->{pending}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $weak and delete $weak->{"pending"}{$id} 0 0 0
Condition0%0/3
$weak and delete $weak->{"pending"}{$id}
!ll&&!rl&&r
000
MC/DC0%0/2
$weak and delete $weak->{"pending"}{$id}
$weak delete $weak->{"pending"}{$id}
Truth table0%0/3
A and B
A= $weakB= delete $weak->{"pending"}{$id}
ABresult
0X0
100
111
621 0 send_message $weak, ["cancel", $id*1];
622 0 };
623 0 AE::log debug => "webbrowser $method " . $json->encode ($args->{target});
624 send_message $page, ["call", $id*1, $method, {
625 ($list ? () : (operation => $args->{operation}, arguments => $args->{arguments} || {})),
626 0 ▶ max_bytes => $args->{max_bytes} // 16 * 1024,
Branch: 1 branch
BranchTrueFalseTotal
$list ? : 0 0 0
Condition0%0/4
$args->{"arguments"} or {}
l!l
00
$args->{"max_bytes"} // 16384
l!l
00
MC/DC0%0/1
$args->{"arguments"} or {}
$args->{"arguments"}
MC/DC0%0/1
$args->{"max_bytes"} // 16384
$args->{"max_bytes"}
Truth table0%0/2
A
A= $args->{"arguments"}
Aresult
11
00
Truth table0%0/2
A
A= $args->{"max_bytes"}
Aresult
11
00
627 }];
628 }
629  
630 0 ▶sub prune_tab_rules {
Subroutine prune_tab_rules: not called CC=5 SCAR=34.0
631 0 my ($target) = @_;
632  
633 0 my $id = JSON::XS->new->allow_nonref->encode ($target);
634 my @prefixes = map {
635 0 my $prefix = "$_:$id:";
636 0 ($prefix, "^" . quotemeta $prefix)
637 } qw(execute operation);
638  
639 0 my $lock = $permissions->_lock;
640 0 my $rules = $permissions->rules;
641 my @keep = grep {
642 0 ▶ my $subject = $_->{"match.subject"} // "";
Condition0%0/2
$_->{"match.subject"} // ""
l!l
00
MC/DC0%0/1
$_->{"match.subject"} // ""
$_->{"match.subject"}
Truth table0%0/2
A
A= $_->{"match.subject"}
Aresult
11
00
643 ($_->{"match.tab"} // "") ne $id
644 0 ▶ && !grep { index ($subject, $_) == 0 } @prefixes
Condition0%0/5
($_->{'match.tab'} // '') ne $id && !grep({index($subject, $_) == 0;} @prefixes)
!ll&&!rl&&r
000
$_->{"match.tab"} // ""
l!l
00
MC/DC0%0/2
($_->{'match.tab'} // '') ne $id && !grep({index($subject, $_) == 0;} @prefixes)
$_->{"match.tab"} !grep({index($subject, $_) == 0;} @prefixes)
Truth table0%0/3
A && not(B)
A= $_->{"match.tab"}B= !grep({index($subject, $_) == 0;} @prefixes)
ABresult
0X0
100
111
645 } @$rules;
646  
647 0 ▶ return 0 if @keep == @$rules;
Branch: 1 branch
BranchTrueFalseTotal
if @keep == @$rules 0 0 0
648 0 $permissions->_write (\@keep);
649 0 @$rules - @keep
650 }
651  
652 0 ▶sub finish_tab {
Subroutine finish_tab: not called CC=9 SCAR=45.0
653 0 my ($tab, $reason) = @_;
654  
655 0 ▶ return unless $tabs{$tab->{requested}} && $tabs{$tab->{requested}} == $tab;
Branch: 1 branch
BranchTrueFalseTotal
unless $tabs{$tab->{"requested"}} and $tabs{$tab->{"requested"}} == $tab 0 0 0
Condition0%0/3
$tabs{$tab->{"requested"}} and $tabs{$tab->{"requested"}} == $tab
!ll&&!rl&&r
000
MC/DC0%0/2
$tabs{$tab->{"requested"}} and $tabs{$tab->{"requested"}} == $tab
$tabs{$tab->{"requested"}} $tabs{$tab->{"requested"}} == $tab
Truth table0%0/3
A and B
A= $tabs{$tab->{"requested"}}B= $tabs{$tab->{"requested"}} == $tab
ABresult
0X0
100
111
656 0 delete $tab->{expiry};
657 0 delete $tabs{$tab->{requested}};
658 0 ▶ undef $last_allowed if defined $last_allowed && $last_allowed eq $tab->{requested};
Branch: 1 branch
BranchTrueFalseTotal
if defined $last_allowed and $last_allowed eq $tab->{"requested"} 0 0 0
Condition0%0/3
defined $last_allowed and $last_allowed eq $tab->{"requested"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $last_allowed and $last_allowed eq $tab->{"requested"}
defined $last_allowed $last_allowed eq $tab->{"requested"}
Truth table0%0/3
A and B
A= defined $last_allowedB= $last_allowed eq $tab->{"requested"}
ABresult
0X0
100
111
659 0 ▶ undef $marked if defined $marked && $marked eq $tab->{requested};
Branch: 1 branch
BranchTrueFalseTotal
if defined $marked and $marked eq $tab->{"requested"} 0 0 0
Condition0%0/3
defined $marked and $marked eq $tab->{"requested"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $marked and $marked eq $tab->{"requested"}
defined $marked $marked eq $tab->{"requested"}
Truth table0%0/3
A and B
A= defined $markedB= $marked eq $tab->{"requested"}
ABresult
0X0
100
111
660 0 my $count = eval { prune_tab_rules ($tab->{requested}) };
661 0 ▶ AE::log warn => "$tab->{requested}: cannot clean up tab permissions: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
662 0 ▶ AE::log info => "$tab->{requested}: removed $count tab-specific permission rules" if $count;
Branch: 1 branch
BranchTrueFalseTotal
if $count 0 0 0
663 0 mcp_log "$tab->{requested}: session finished ($reason)";
664 }
665  
666 0 ▶sub expire_tab {
Subroutine expire_tab: not called CC=3 SCAR=24.8
667 0 my ($tab) = @_;
668  
669 0 delete $tab->{expiry};
670 0 ▶ return if $tab->{page} || $tab->{navigation};
Branch: 1 branch
BranchTrueFalseTotal
if $tab->{"page"} or $tab->{"navigation"} 0 0 0
Condition0%0/3
$tab->{"page"} or $tab->{"navigation"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$tab->{"page"} or $tab->{"navigation"}
$tab->{"page"} $tab->{"navigation"}
Truth table0%0/3
A or B
A= $tab->{"page"}B= $tab->{"navigation"}
ABresult
1X1
011
000
671  
672 0 ▶ $tab->{expiry} = AE::timer 300, 0, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
673 finish_tab $tab, "inactive for 300s"
674 0 ▶ unless $tab->{page} || $tab->{navigation};
Branch: 1 branch
BranchTrueFalseTotal
unless $tab->{"page"} or $tab->{"navigation"} 0 0 0
Condition0%0/3
$tab->{"page"} or $tab->{"navigation"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$tab->{"page"} or $tab->{"navigation"}
$tab->{"page"} $tab->{"navigation"}
Truth table0%0/3
A or B
A= $tab->{"page"}B= $tab->{"navigation"}
ABresult
1X1
011
000
675 0 };
676 }
677  
678 0 ▶sub finish_navigation {
Subroutine finish_navigation: not called CC=6 SCAR=37.4
679 0 my ($tab, $kind, $reason) = @_;
680  
681 0 ▶ my $entry = delete $tab->{navigation} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = delete $tab->{"navigation"} or return 0 0 0
682 0 delete $entry->{timer};
683 0 my $req = $entry->{request};
684 0 ▶ return if $req->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $req->{"finished"} 0 0 0
685  
686 0 my $page = $tab->{page};
687 $req->result ({
688 navigation => $kind,
689 revision => $tab->{revision}*1,
690 0 ▶ ($page ? (%{$page->{state}}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
$page ? : 0 0 0
defined $reason ? : 0 0 0
691 (defined $reason ? (reason => $reason) : ()),
692 });
693  
694 0 ▶ expire_tab $tab unless $page;
Branch: 1 branch
BranchTrueFalseTotal
unless $page 0 0 0
695 }
696  
697 0 ▶sub close_page {
Subroutine close_page: not called CC=8 SCAR=42.8
698 0 my ($page, $code, $reason) = @_;
699  
700 0 ▶ delete $connections{$page+0} or return;
Branch: 1 branch
BranchTrueFalseTotal
delete $connections{$page + 0} or return 0 0 0
701 0 ▶ delete $pages{$page->{target}} if defined $page->{target} && $pages{$page->{target}} == $page;
Branch: 1 branch
BranchTrueFalseTotal
if defined $page->{"target"} and $pages{$page->{"target"}} == $page 0 0 0
Condition0%0/3
defined $page->{"target"} and $pages{$page->{"target"}} == $page
!ll&&!rl&&r
000
MC/DC0%0/2
defined $page->{"target"} and $pages{$page->{"target"}} == $page
defined $page->{"target"} $pages{$page->{"target"}} == $page
Truth table0%0/3
A and B
A= defined $page->{"target"}B= $pages{$page->{"target"}} == $page
ABresult
0X0
100
111
702 0 delete $page->{hello_timer};
703  
704 0 ▶ if (my $tab = $page->{tab}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tab = $page->{"tab"}) 0 0 0
705 0 ▶ delete $tab->{page} if $tab->{page} == $page;
Branch: 1 branch
BranchTrueFalseTotal
if $tab->{"page"} == $page 0 0 0
706 0 expire_tab $tab;
707 }
708  
709 0 finish_call $page, $_, 0, $reason for keys %{$page->{pending}};
710 0 $page->{conn}->close ($code, $reason);
711 0 ▶ if (defined $page->{target}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $page->{'target'}) { } 0 0 0
712 0 mcp_log "$page->{target}: disconnected ($reason)";
713 } else {
714 0 AE::log trace => "Browser WebSocket disconnected before hello ($reason)";
715 }
716 }
717  
718 0 ▶sub valid_state {
Subroutine valid_state: not called CC=5 SCAR=34.0
719 0 my ($state) = @_;
720  
721 ref $state eq "HASH" && defined $state->{url} && !ref $state->{url}
722 && defined $state->{title} && !ref $state->{title}
723 0 ▶}
Branch: 1 branch
BranchTrueFalseTotal
ref $state eq "HASH" and defined $state->{"url"} and not ref $state->{"url"} and defined $state->{"title"} and not ref $state->{"title"} 0 0 0
Condition0%0/9
ref $state eq "HASH" and defined $state->{"url"} and not ref $state->{"url"} and defined $state->{"title"}
!ll&&!rl&&r
000
ref $state eq "HASH" and defined $state->{"url"} and not ref $state->{"url"}
!ll&&!rl&&r
000
ref $state eq "HASH" and defined $state->{"url"}
!ll&&!rl&&r
000
MC/DC0%0/4
ref $state eq "HASH" and defined $state->{"url"} and not ref $state->{"url"} and defined $state->{"title"}
ref $state eq "HASH" defined $state->{"url"} not ref $state->{"url"} defined $state->{"title"}
Truth table0%0/5
A and B and not(C) and D
A= ref $state eq "HASH"B= defined $state->{"url"}C= not ref $state->{"url"}D= defined $state->{"title"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
724  
725 0 ▶sub receive_message {
Subroutine receive_message: not called CC=85 SCAR=89.0
726 0 my ($page, $packet) = @_;
727  
728 0 ▶ ref $packet eq "ARRAY" && @$packet && defined $packet->[0] && !ref $packet->[0]
Branch: 1 branch
BranchTrueFalseTotal
ref $packet eq "ARRAY" and @$packet and defined $packet->[0] and not ref $packet->[0] or die "Invalid browser message\n" 0 0 0
Condition0%0/9
ref $packet eq "ARRAY" and @$packet and defined $packet->[0] and not ref $packet->[0]
!ll&&!rl&&r
000
ref $packet eq "ARRAY" and @$packet and defined $packet->[0]
!ll&&!rl&&r
000
ref $packet eq "ARRAY" and @$packet
!ll&&!rl&&r
000
MC/DC0%0/4
ref $packet eq "ARRAY" and @$packet and defined $packet->[0] and not ref $packet->[0]
ref $packet eq "ARRAY" @$packet defined $packet->[0] not ref $packet->[0]
Truth table0%0/5
A and B and C and not(D)
A= ref $packet eq "ARRAY"B= @$packetC= defined $packet->[0]D= not ref $packet->[0]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
729 or die "Invalid browser message\n";
730  
731 0 ▶ unless (defined $page->{target}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $page->{"target"}) 0 0 0
732 0 ▶ @$packet == 5 && $packet->[0] eq "mcp-webbrowser"
Branch: 1 branch
BranchTrueFalseTotal
@$packet == 5 and $packet->[0] eq "mcp-webbrowser" and defined $packet->[1] and not ref $packet->[1] and defined $packet->[2] and not ref $packet->[2] and defined $packet->[3] and not ref $packet->[3] and length $packet->[3] and valid_state($packet->[4]) or die "Invalid browser hello\n" 0 0 0
Condition0%0/27
@$packet == 5 and $packet->[0] eq "mcp-webbrowser" and defined $packet->[1] and not ref $packet->[1] and defined $packet->[2] and not ref $packet->[2] and defined $packet->[3] and not ref $packet->[3] and length $packet->[3] and valid_state($packet->[4])
!ll&&!rl&&r
000
@$packet == 5 and $packet->[0] eq "mcp-webbrowser" and defined $packet->[1] and not ref $packet->[1] and defined $packet->[2] and not ref $packet->[2] and defined $packet->[3] and not ref $packet->[3] and length $packet->[3]
!ll&&!rl&&r
000
@$packet == 5 and $packet->[0] eq "mcp-webbrowser" and defined $packet->[1] and not ref $packet->[1] and defined $packet->[2] and not ref $packet->[2] and defined $packet->[3] and not ref $packet->[3]
!ll&&!rl&&r
000
@$packet == 5 and $packet->[0] eq "mcp-webbrowser" and defined $packet->[1] and not ref $packet->[1] and defined $packet->[2] and not ref $packet->[2] and defined $packet->[3]
!ll&&!rl&&r
000
@$packet == 5 and $packet->[0] eq "mcp-webbrowser" and defined $packet->[1] and not ref $packet->[1] and defined $packet->[2] and not ref $packet->[2]
!ll&&!rl&&r
000
@$packet == 5 and $packet->[0] eq "mcp-webbrowser" and defined $packet->[1] and not ref $packet->[1] and defined $packet->[2]
!ll&&!rl&&r
000
@$packet == 5 and $packet->[0] eq "mcp-webbrowser" and defined $packet->[1] and not ref $packet->[1]
!ll&&!rl&&r
000
@$packet == 5 and $packet->[0] eq "mcp-webbrowser" and defined $packet->[1]
!ll&&!rl&&r
000
@$packet == 5 and $packet->[0] eq "mcp-webbrowser"
!ll&&!rl&&r
000
MC/DC0%0/10
@$packet == 5 and $packet->[0] eq "mcp-webbrowser" and defined $packet->[1] and not ref $packet->[1] and defined $packet->[2] and not ref $packet->[2] and defined $packet->[3] and not ref $packet->[3] and length $packet->[3] and valid_state($packet->[4])
@$packet == 5 $packet->[0] eq "mcp-webbrowser" defined $packet->[1] not ref $packet->[1] defined $packet->[2] not ref $packet->[2] defined $packet->[3] not ref $packet->[3] length $packet->[3] valid_state($packet->[4])
Truth table0%0/11
A and B and C and not(D) and E and not(F) and G and not(H) and I and J
A= @$packet == 5B= $packet->[0] eq "mcp-webbrowser"C= defined $packet->[1]D= not ref $packet->[1]E= defined $packet->[2]F= not ref $packet->[2]G= defined $packet->[3]H= not ref $packet->[3]I= length $packet->[3]J= valid_state($packet->[4])
ABCDEFGHIJresult
0XXXXXXXXX0
10XXXXXXXX0
110XXXXXXX0
1110XXXXXX0
11110XXXXX0
111110XXXX0
1111110XXX0
11111110XX0
111111110X0
11111111100
11111111111
733 && defined $packet->[1] && !ref $packet->[1]
734 && defined $packet->[2] && !ref $packet->[2]
735 && defined $packet->[3] && !ref $packet->[3] && length $packet->[3]
736 && valid_state ($packet->[4])
737 or die "Invalid browser hello\n";
738 0 ▶ $packet->[1] eq "1" && $packet->[2] =~ /\A(?:0|[1-9][0-9]*)\z/
Branch: 1 branch
BranchTrueFalseTotal
$packet->[1] eq 1 and $packet->[2] =~ /\A(?:0|[1-9][0-9]*)\z/u or die "Unsupported browser protocol $packet->[1].$packet->[2] (server 1.2)\n" 0 0 0
Condition0%0/3
$packet->[1] eq 1 and $packet->[2] =~ /\A(?:0|[1-9][0-9]*)\z/u
!ll&&!rl&&r
000
MC/DC0%0/2
$packet->[1] eq 1 and $packet->[2] =~ /\A(?:0|[1-9][0-9]*)\z/u
$packet->[1] eq 1 $packet->[2] =~ /\A(?:0|[1-9][0-9]*)\z/u
Truth table0%0/3
A and B
A= $packet->[1] eq 1B= $packet->[2] =~ /\A(?:0|[1-9][0-9]*)\z/u
ABresult
0X0
100
111
739 or die "Unsupported browser protocol $packet->[1].$packet->[2] (server 1.2)\n";
740  
741 0 my $new = !exists $tabs{$page->{requested}};
742 my $tab = $tabs{$page->{requested}} ||= {
743 0 ▶ requested => $page->{requested}, revision => 0,
Condition0%0/2
$tabs{$page->{"requested"}} ||= {"requested", $page->{"requested"}, "revision", 0}
l!l
00
MC/DC0%0/1
$tabs{$page->{"requested"}} ||= {"requested", $page->{"requested"}, "revision", 0}
$tabs{$page->{"requested"}}
Truth table0%0/2
A
A= $tabs{$page->{"requested"}}
Aresult
11
00
744 };
745 0 ▶ my $different = !defined $tab->{instance_id} || $tab->{instance_id} ne $packet->[3];
Condition0%0/3
!defined($tab->{'instance_id'}) || $tab->{'instance_id'} ne $packet->[3]
l!l&&r!l&&!r
000
MC/DC0%0/2
!defined($tab->{'instance_id'}) || $tab->{'instance_id'} ne $packet->[3]
!defined($tab->{'instance_id'}) $tab->{'instance_id'} ne $packet->[3]
Truth table0%0/3
not(A) || B
A= !defined($tab->{'instance_id'})B= $tab->{'instance_id'} ne $packet->[3]
ABresult
1X1
011
000
746 0 ▶ close_page $tab->{page}, 1000, "Page connection replaced" if $tab->{page};
Branch: 1 branch
BranchTrueFalseTotal
if $tab->{"page"} 0 0 0
747 0 delete $tab->{expiry};
748  
749 0 ▶ ++$tab->{revision} if $different;
Branch: 1 branch
BranchTrueFalseTotal
if $different 0 0 0
750 0 $tab->{instance_id} = $packet->[3];
751 @$page{qw(target instance_id minor state tab)} =
752 0 ($page->{requested}, $packet->[3], $packet->[2], browser_state ($packet->[4]), $tab);
753 0 $tab->{page} = $page;
754 0 delete $page->{hello_timer};
755 0 $pages{$page->{target}} = $page;
756 0 send_message $page, ["welcome", $page->{target}, $page->{instance_id}, $tab->{revision}*1];
757 send_message $page, ["marked", JSON::XS::true]
758 0 ▶ if $page->{minor} >= 2 && defined $marked && $marked eq $page->{target};
Branch: 1 branch
BranchTrueFalseTotal
if $page->{"minor"} >= 2 and defined $marked and $marked eq $page->{"target"} 0 0 0
Condition0%0/6
$page->{"minor"} >= 2 and defined $marked and $marked eq $page->{"target"}
!ll&&!rl&&r
000
$page->{"minor"} >= 2 and defined $marked
!ll&&!rl&&r
000
MC/DC0%0/3
$page->{"minor"} >= 2 and defined $marked and $marked eq $page->{"target"}
$page->{"minor"} >= 2 defined $marked $marked eq $page->{"target"}
Truth table0%0/4
A and B and C
A= $page->{"minor"} >= 2B= defined $markedC= $marked eq $page->{"target"}
ABCresult
0XX0
10X0
1100
1111
759  
760 0 ▶ if ($different && $tab->{navigation} && $tab->{navigation}{phase} eq "proceed") {
Branch: 1 branch
BranchTrueFalseTotal
if ($different and $tab->{"navigation"} and $tab->{"navigation"}{"phase"} eq "proceed") 0 0 0
Condition0%0/6
$different and $tab->{"navigation"} and $tab->{"navigation"}{"phase"} eq "proceed"
!ll&&!rl&&r
000
$different and $tab->{"navigation"}
!ll&&!rl&&r
000
MC/DC0%0/3
$different and $tab->{"navigation"} and $tab->{"navigation"}{"phase"} eq "proceed"
$different $tab->{"navigation"} $tab->{"navigation"}{"phase"} eq "proceed"
Truth table0%0/4
A and B and C
A= $differentB= $tab->{"navigation"}C= $tab->{"navigation"}{"phase"} eq "proceed"
ABCresult
0XX0
10X0
1100
1111
761 0 finish_navigation $tab, "document";
762 }
763  
764 0 ▶ mcp_log "$page->{target}: session started (" . url_preview ($page->{state}{url}) . ")" if $new;
Branch: 1 branch
BranchTrueFalseTotal
if $new 0 0 0
765 0 ▶ mcp_log "$page->{target}: connected (revision $tab->{revision})"
Branch: 1 branch
BranchTrueFalseTotal
unless $new 0 0 0
766 unless $new;
767 return
768 0 }
769  
770 0 my $type = $packet->[0];
771  
772 0 ▶ if ($type eq "reply") {
Branch: 7 branches
BranchTrueFalseTotal
if ($type eq 'reply') { } 0 0 0
elsif ($type eq 'ready') { } 0 0 0
elsif ($type eq 'navigated') { } 0 0 0
elsif ($type eq 'state') { } 0 0 0
elsif ($type eq 'allowed') { } 0 0 0
elsif ($type eq 'mark') { } 0 0 0
elsif ($type eq 'quit') { } 0 0 0
773 0 ▶ @$packet == 4 && defined $packet->[1] && !ref $packet->[1] && $packet->[1] =~ /\A[1-9][0-9]*\z/
Branch: 1 branch
BranchTrueFalseTotal
@$packet == 4 and defined $packet->[1] and not ref $packet->[1] and $packet->[1] =~ /\A[1-9][0-9]*\z/u and defined $packet->[2] and $packet->[2] eq 0 || $packet->[2] eq 1 and $packet->[2] || defined $packet->[3] && !ref($packet->[3]) or die "Invalid browser reply\n" 0 0 0
Condition0%0/27
@$packet == 4 and defined $packet->[1] and not ref $packet->[1] and $packet->[1] =~ /\A[1-9][0-9]*\z/u and defined $packet->[2] and $packet->[2] eq 0 || $packet->[2] eq 1 and $packet->[2] || defined $packet->[3] && !ref($packet->[3])
!ll&&!rl&&r
000
@$packet == 4 and defined $packet->[1] and not ref $packet->[1] and $packet->[1] =~ /\A[1-9][0-9]*\z/u and defined $packet->[2] and $packet->[2] eq 0 || $packet->[2] eq 1
!ll&&!rl&&r
000
@$packet == 4 and defined $packet->[1] and not ref $packet->[1] and $packet->[1] =~ /\A[1-9][0-9]*\z/u and defined $packet->[2]
!ll&&!rl&&r
000
@$packet == 4 and defined $packet->[1] and not ref $packet->[1] and $packet->[1] =~ /\A[1-9][0-9]*\z/u
!ll&&!rl&&r
000
@$packet == 4 and defined $packet->[1] and not ref $packet->[1]
!ll&&!rl&&r
000
@$packet == 4 and defined $packet->[1]
!ll&&!rl&&r
000
$packet->[2] eq 0 or $packet->[2] eq 1
l!l&&r!l&&!r
000
$packet->[2] or defined $packet->[3] and not ref $packet->[3]
l!l&&r!l&&!r
000
defined $packet->[3] and not ref $packet->[3]
!ll&&!rl&&r
000
MC/DC0%0/10
@$packet == 4 and defined $packet->[1] and not ref $packet->[1] and $packet->[1] =~ /\A[1-9][0-9]*\z/u and defined $packet->[2] and $packet->[2] eq 0 || $packet->[2] eq 1 and $packet->[2] || defined $packet->[3] && !ref($packet->[3])
@$packet == 4 defined $packet->[1] not ref $packet->[1] $packet->[1] =~ /\A[1-9][0-9]*\z/u defined $packet->[2] $packet->[2] eq 0 $packet->[2] eq 1 $packet->[2] defined $packet->[3] not ref $packet->[3]
Truth table0%0/14
A and B and not(C) and D and E and F or G and H or I and not(J)
A= @$packet == 4B= defined $packet->[1]C= not ref $packet->[1]D= $packet->[1] =~ /\A[1-9][0-9]*\z/uE= defined $packet->[2]F= $packet->[2] eq 0G= $packet->[2] eq 1H= $packet->[2]I= defined $packet->[3]J= not ref $packet->[3]
ABCDEFGHIJresult
0XXXXXXXXX0
10XXXXXXXX0
110XXXXXXX0
1110XXXXXX0
11110XXXXX0
1111100XXX0
111111X00X0
111111X0100
111110100X0
11111010100
111111X1XX1
111111X0111
11111011XX1
11111010111
774 && defined $packet->[2] && ($packet->[2] eq "0" || $packet->[2] eq "1")
775 && ($packet->[2] || defined $packet->[3] && !ref $packet->[3])
776 or die "Invalid browser reply\n";
777 0 my $entry = $page->{tab}{navigation};
778 0 ▶ if ($entry && $entry->{page} == $page && $entry->{id} == $packet->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry and $entry->{'page'} == $page and $entry->{'id'} == $packet->[1]) { } 0 0 0
Condition0%0/6
$entry and $entry->{"page"} == $page and $entry->{"id"} == $packet->[1]
!ll&&!rl&&r
000
$entry and $entry->{"page"} == $page
!ll&&!rl&&r
000
MC/DC0%0/3
$entry and $entry->{"page"} == $page and $entry->{"id"} == $packet->[1]
$entry $entry->{"page"} == $page $entry->{"id"} == $packet->[1]
Truth table0%0/4
A and B and C
A= $entryB= $entry->{"page"} == $pageC= $entry->{"id"} == $packet->[1]
ABCresult
0XX0
10X0
1100
1111
779 0 ▶ if (!$packet->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $packet->[2]) 0 0 0
780 0 delete $page->{tab}{navigation};
781 0 delete $entry->{timer};
782 0 ▶ $entry->{request}->error ($packet->[3]) unless $entry->{request}{finished};
Branch: 1 branch
BranchTrueFalseTotal
unless $entry->{"request"}{"finished"} 0 0 0
783 }
784 } else {
785 0 finish_call $page, @$packet[1, 2, 3];
786 }
787 } elsif ($type eq "ready") {
788 0 ▶ @$packet == 2 && defined $packet->[1] && !ref $packet->[1]
Branch: 1 branch
BranchTrueFalseTotal
@$packet == 2 and defined $packet->[1] and not ref $packet->[1] and $packet->[1] =~ /\A[1-9][0-9]*\z/u or die "Invalid navigation acknowledgement\n" 0 0 0
Condition0%0/9
@$packet == 2 and defined $packet->[1] and not ref $packet->[1] and $packet->[1] =~ /\A[1-9][0-9]*\z/u
!ll&&!rl&&r
000
@$packet == 2 and defined $packet->[1] and not ref $packet->[1]
!ll&&!rl&&r
000
@$packet == 2 and defined $packet->[1]
!ll&&!rl&&r
000
MC/DC0%0/4
@$packet == 2 and defined $packet->[1] and not ref $packet->[1] and $packet->[1] =~ /\A[1-9][0-9]*\z/u
@$packet == 2 defined $packet->[1] not ref $packet->[1] $packet->[1] =~ /\A[1-9][0-9]*\z/u
Truth table0%0/5
A and B and not(C) and D
A= @$packet == 2B= defined $packet->[1]C= not ref $packet->[1]D= $packet->[1] =~ /\A[1-9][0-9]*\z/u
ABCDresult
0XXX0
10XX0
110X0
11100
11111
789 && $packet->[1] =~ /\A[1-9][0-9]*\z/ or die "Invalid navigation acknowledgement\n";
790  
791 0 my $entry = $page->{tab}{navigation};
792 0 ▶ if ($entry && $entry->{page} == $page && $entry->{id} == $packet->[1]
Branch: 1 branch
BranchTrueFalseTotal
if ($entry and $entry->{"page"} == $page and $entry->{"id"} == $packet->[1] and $entry->{"phase"} eq "ready") 0 0 0
Condition0%0/9
$entry and $entry->{"page"} == $page and $entry->{"id"} == $packet->[1] and $entry->{"phase"} eq "ready"
!ll&&!rl&&r
000
$entry and $entry->{"page"} == $page and $entry->{"id"} == $packet->[1]
!ll&&!rl&&r
000
$entry and $entry->{"page"} == $page
!ll&&!rl&&r
000
MC/DC0%0/4
$entry and $entry->{"page"} == $page and $entry->{"id"} == $packet->[1] and $entry->{"phase"} eq "ready"
$entry $entry->{"page"} == $page $entry->{"id"} == $packet->[1] $entry->{"phase"} eq "ready"
Truth table0%0/5
A and B and C and D
A= $entryB= $entry->{"page"} == $pageC= $entry->{"id"} == $packet->[1]D= $entry->{"phase"} eq "ready"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
793 && $entry->{phase} eq "ready") {
794 0 $entry->{phase} = "proceed";
795 0 AE::log trace => "webbrowser navigate $page->{target} #$entry->{id}: " . code_preview ($entry->{code});
796 0 send_message $page, ["proceed", $packet->[1]*1];
797 }
798 } elsif ($type eq "navigated") {
799 0 ▶ @$packet == 3 && defined $packet->[1] && !ref $packet->[1]
Branch: 1 branch
BranchTrueFalseTotal
@$packet == 3 and defined $packet->[1] and not ref $packet->[1] and $packet->[1] =~ /\A[1-9][0-9]*\z/u and valid_state($packet->[2]) or die "Invalid navigation result\n" 0 0 0
Condition0%0/12
@$packet == 3 and defined $packet->[1] and not ref $packet->[1] and $packet->[1] =~ /\A[1-9][0-9]*\z/u and valid_state($packet->[2])
!ll&&!rl&&r
000
@$packet == 3 and defined $packet->[1] and not ref $packet->[1] and $packet->[1] =~ /\A[1-9][0-9]*\z/u
!ll&&!rl&&r
000
@$packet == 3 and defined $packet->[1] and not ref $packet->[1]
!ll&&!rl&&r
000
@$packet == 3 and defined $packet->[1]
!ll&&!rl&&r
000
MC/DC0%0/5
@$packet == 3 and defined $packet->[1] and not ref $packet->[1] and $packet->[1] =~ /\A[1-9][0-9]*\z/u and valid_state($packet->[2])
@$packet == 3 defined $packet->[1] not ref $packet->[1] $packet->[1] =~ /\A[1-9][0-9]*\z/u valid_state($packet->[2])
Truth table0%0/6
A and B and not(C) and D and E
A= @$packet == 3B= defined $packet->[1]C= not ref $packet->[1]D= $packet->[1] =~ /\A[1-9][0-9]*\z/uE= valid_state($packet->[2])
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
800 && $packet->[1] =~ /\A[1-9][0-9]*\z/ && valid_state ($packet->[2])
801 or die "Invalid navigation result\n";
802  
803 0 my $entry = $page->{tab}{navigation};
804 0 ▶ if ($entry && $entry->{page} == $page && $entry->{id} == $packet->[1]
Branch: 1 branch
BranchTrueFalseTotal
if ($entry and $entry->{"page"} == $page and $entry->{"id"} == $packet->[1] and $entry->{"phase"} eq "proceed") 0 0 0
Condition0%0/9
$entry and $entry->{"page"} == $page and $entry->{"id"} == $packet->[1] and $entry->{"phase"} eq "proceed"
!ll&&!rl&&r
000
$entry and $entry->{"page"} == $page and $entry->{"id"} == $packet->[1]
!ll&&!rl&&r
000
$entry and $entry->{"page"} == $page
!ll&&!rl&&r
000
MC/DC0%0/4
$entry and $entry->{"page"} == $page and $entry->{"id"} == $packet->[1] and $entry->{"phase"} eq "proceed"
$entry $entry->{"page"} == $page $entry->{"id"} == $packet->[1] $entry->{"phase"} eq "proceed"
Truth table0%0/5
A and B and C and D
A= $entryB= $entry->{"page"} == $pageC= $entry->{"id"} == $packet->[1]D= $entry->{"phase"} eq "proceed"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
805 && $entry->{phase} eq "proceed") {
806 0 $page->{state} = browser_state ($packet->[2]);
807 0 finish_navigation $page->{tab}, "same-document";
808 }
809 } elsif ($type eq "state") {
810 0 ▶ @$packet == 2 && valid_state ($packet->[1]) or die "Invalid browser state\n";
Branch: 1 branch
BranchTrueFalseTotal
@$packet == 2 and valid_state($packet->[1]) or die "Invalid browser state\n" 0 0 0
Condition0%0/3
@$packet == 2 and valid_state($packet->[1])
!ll&&!rl&&r
000
MC/DC0%0/2
@$packet == 2 and valid_state($packet->[1])
@$packet == 2 valid_state($packet->[1])
Truth table0%0/3
A and B
A= @$packet == 2B= valid_state($packet->[1])
ABresult
0X0
100
111
811 0 $page->{state} = browser_state ($packet->[1]);
812 } elsif ($type eq "allowed") {
813 0 ▶ @$packet == 1 or die "Invalid browser allowed event\n";
Branch: 1 branch
BranchTrueFalseTotal
@$packet == 1 or die "Invalid browser allowed event\n" 0 0 0
814 0 $last_allowed = $page->{target};
815 } elsif ($type eq "mark") {
816 0 ▶ @$packet == 2 && ($packet->[1] eq "0" || $packet->[1] eq "1")
Branch: 1 branch
BranchTrueFalseTotal
@$packet == 2 and $packet->[1] eq 0 || $packet->[1] eq 1 or die "Invalid browser mark event\n" 0 0 0
Condition0%0/6
@$packet == 2 and $packet->[1] eq 0 || $packet->[1] eq 1
!ll&&!rl&&r
000
$packet->[1] eq 0 or $packet->[1] eq 1
l!l&&r!l&&!r
000
MC/DC0%0/3
@$packet == 2 and $packet->[1] eq 0 || $packet->[1] eq 1
@$packet == 2 $packet->[1] eq 0 $packet->[1] eq 1
Truth table0%0/4
A and B or C
A= @$packet == 2B= $packet->[1] eq 0C= $packet->[1] eq 1
ABCresult
0XX0
1000
11X1
1011
817 or die "Invalid browser mark event\n";
818  
819 0 ▶ if ($packet->[1]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($packet->[1]) { } 0 0 0
elsif (defined $marked and $marked eq $page->{'target'}) { } 0 0 0
820 0 $marked = $page->{target};
821 ▶ } elsif (defined $marked && $marked eq $page->{target}) {
Condition0%0/3
defined $marked and $marked eq $page->{"target"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $marked and $marked eq $page->{"target"}
defined $marked $marked eq $page->{"target"}
Truth table0%0/3
A and B
A= defined $markedB= $marked eq $page->{"target"}
ABresult
0X0
100
111
822 0 undef $marked;
823 }
824  
825 0 for my $other (values %pages) {
826 0 ▶ next if $other->{minor} < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $other->{"minor"} < 2 0 0 0
827 send_message $other, ["marked", defined $marked && $marked eq $other->{target}
828 0 ▶ ? JSON::XS::true : JSON::XS::false];
Branch: 1 branch
BranchTrueFalseTotal
defined $marked && $marked eq $other->{'target'} ? : 0 0 0
Condition0%0/3
defined $marked and $marked eq $other->{"target"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $marked and $marked eq $other->{"target"}
defined $marked $marked eq $other->{"target"}
Truth table0%0/3
A and B
A= defined $markedB= $marked eq $other->{"target"}
ABresult
0X0
100
111
829 }
830 } elsif ($type eq "quit") {
831 0 ▶ @$packet == 1 or die "Invalid browser quit\n";
Branch: 1 branch
BranchTrueFalseTotal
@$packet == 1 or die "Invalid browser quit\n" 0 0 0
832 0 close_page $page, 1000, "Page disconnected";
833 finish_tab $page->{tab}, "disabled"
834 0 ▶ if $page->{tab} && !$page->{tab}{navigation};
Branch: 1 branch
BranchTrueFalseTotal
if $page->{"tab"} and not $page->{"tab"}{"navigation"} 0 0 0
Condition0%0/3
$page->{"tab"} and not $page->{"tab"}{"navigation"}
!ll&&!rl&&r
000
MC/DC0%0/2
$page->{"tab"} and not $page->{"tab"}{"navigation"}
$page->{"tab"} not $page->{"tab"}{"navigation"}
Truth table0%0/3
A and not(B)
A= $page->{"tab"}B= not $page->{"tab"}{"navigation"}
ABresult
0X0
100
111
835 } else {
836 0 die "Unknown browser message\n";
837 }
838 }
839  
840 0 ▶sub accept_browser {
Subroutine accept_browser: not called CC=1 SCAR=6.9
841 0 my ($fh) = @_;
842  
843 0 my ($requested, $rejection);
844 0 ▶ my $reject = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
845 0 $rejection = shift;
846 0 die "Browser WebSocket handshake rejected\n";
847 0 };
848 my $ws = AnyEvent::WebSocket::Server->new (
849 max_payload_size => 16 * 1024 * 1024,
850 0 ▶ handshake => sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
851 0 my ($req, $res) = @_;
852 0 my ($cookie, $target) = $req->{resource_name} =~ m{\A/([^/]+)/([^/?#]+)\z};
853 0 ▶ defined $cookie or $reject->("invalid path");
Branch: 1 branch
BranchTrueFalseTotal
defined $cookie or &$reject("invalid path") 0 0 0
854 0 ▶ $token{$cookie} or $reject->("invalid token");
Branch: 1 branch
BranchTrueFalseTotal
$token{$cookie} or &$reject("invalid token") 0 0 0
855 0 ▶ $target =~ /%(?![0-9a-fA-F]{2})/ and $reject->("invalid target");
Branch: 1 branch
BranchTrueFalseTotal
$target =~ /%(?![0-9a-fA-F]{2})/u and &$reject("invalid target") 0 0 0
856 0 $target =~ s/%([0-9a-fA-F]{2})/chr hex $1/ge;
857 0 ▶ utf8::decode $target or $reject->("invalid target");
Branch: 1 branch
BranchTrueFalseTotal
utf8::decode($target) or &$reject("invalid target") 0 0 0
858 0 ▶ length $target && $target !~ /\0/ or $reject->("invalid target");
Branch: 1 branch
BranchTrueFalseTotal
length $target and not $target =~ /\0/u or &$reject("invalid target") 0 0 0
Condition0%0/3
length $target and not $target =~ /\0/u
!ll&&!rl&&r
000
MC/DC0%0/2
length $target and not $target =~ /\0/u
length $target not $target =~ /\0/u
Truth table0%0/3
A and not(B)
A= length $targetB= not $target =~ /\0/u
ABresult
0X0
100
111
859 0 $requested = $target;
860  
861 0 $res
862 },
863 0 );
864 0 ▶ $ws->establish ($fh)->cb (sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
865 0 my $conn = eval { $_[0]->recv };
866 0 ▶ unless ($conn) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conn) 0 0 0
867 0 ▶ AE::log warn => $rejection
Branch: 1 branch
BranchTrueFalseTotal
$rejection ? : 0 0 0
Condition0%0/2
$@ || 'no connection'
l!l
00
MC/DC0%0/1
$@ || 'no connection'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
868 ? "Browser WebSocket rejected: $rejection"
869 : "Browser WebSocket establishment failed: " . ($@ || "no connection");
870 0 return;
871 }
872  
873 0 my $page = { conn => $conn, requested => $requested, pending => {}, rpcid => 0 };
874 0 $connections{$page+0} = $page;
875 0 weaken (my $weak = $page);
876 0 ▶ $conn->on (finish => sub { close_page $weak, 1000, "Page disconnected" if $weak });
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $weak 0 0 0
877 0 ▶ $conn->on (each_message => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
878 0 ▶ return unless $weak && exists $connections{$weak+0};
Branch: 1 branch
BranchTrueFalseTotal
unless $weak and exists $connections{$weak + 0} 0 0 0
Condition0%0/3
$weak and exists $connections{$weak + 0}
!ll&&!rl&&r
000
MC/DC0%0/2
$weak and exists $connections{$weak + 0}
$weak exists $connections{$weak + 0}
Truth table0%0/3
A and B
A= $weakB= exists $connections{$weak + 0}
ABresult
0X0
100
111
879 0 my $ok = eval {
880 0 ▶ $_[1]->opcode == 1 or die "Browser messages must be text\n";
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->opcode == 1 or die "Browser messages must be text\n" 0 0 0
881 0 receive_message $weak, $json->decode ($_[1]->body);
882 0 1
883 };
884 0 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
885 0 my $reason = $@;
886 0 AE::log warn => "Browser protocol error: $reason";
887 0 $reason =~ s/\n.*//s;
888 0 $reason =~ s/[^\x20-\x7e]/?/g;
889 0 close_page $weak, 1002, substr $reason, 0, 123;
890 }
891 0 });
892 0 ▶ $page->{hello_timer} = AE::timer 15, 0, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
893 0 ▶ close_page $weak, 1002, "Browser hello timed out" if $weak;
Branch: 1 branch
BranchTrueFalseTotal
if $weak 0 0 0
894 0 };
895 0 });
896 }
897  
898 1 my $toolset = AnyEvent::MCP::Server::Toolset->new;
899 $toolset->tool (tabs => "List connected tabs; last_allowed identifies the last explicit Allow action, "
900 . "marked identifies the tab selected with Mark. Neither changes execution permissions.",
901 { type => "object", properties => {}, additionalProperties => JSON::XS::false },
902 0 ▶ sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
903 0 my ($req) = @_;
904 0 permission $req, "read";
905 0 AE::log debug => "webbrowser tabs";
906 $req->result ({ tabs => [map {
907 0 my $page = $pages{$_};
908 0 ▶ +{ %{$page->{state}}, target => $_, instance_id => $page->{instance_id},
Branch: 2 branches
BranchTrueFalseTotal
defined $last_allowed && $last_allowed eq $_ ? : 0 0 0
defined $marked && $marked eq $_ ? : 0 0 0
Condition0%0/6
defined $last_allowed and $last_allowed eq $_
!ll&&!rl&&r
000
defined $marked and $marked eq $_
!ll&&!rl&&r
000
MC/DC0%0/2
defined $last_allowed and $last_allowed eq $_
defined $last_allowed $last_allowed eq $_
MC/DC0%0/2
defined $marked and $marked eq $_
defined $marked $marked eq $_
Truth table0%0/3
A and B
A= defined $last_allowedB= $last_allowed eq $_
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $markedB= $marked eq $_
ABresult
0X0
100
111
909 0 revision => $page->{tab}{revision}*1,
910 last_allowed => defined $last_allowed && $last_allowed eq $_ ? JSON::XS::true : JSON::XS::false,
911 marked => defined $marked && $marked eq $_ ? JSON::XS::true : JSON::XS::false }
912 } sort keys %pages] });
913 },
914 1 );
915 $toolset->tool (execute => "Page JS: CODE becomes body of async () => CODE; supply CODE, not arrow-function wrapper. "
916 . "Example: ({title: document.title}); NOT async () => ({title: document.title}) (returns function, rejected). "
917 . "Single expression returns implicitly; { ... } body needs explicit return; await works. "
918 . "UTF-8 JSON reply max_bytes=16384 (16 KiB) default, max 2097152 (2 MiB). Oversize errors after execution; no truncation. "
919 . "navigate=true: acknowledge before execution, "
920 . "then wait for new document or observed same-document navigation. "
921 . "Revision/instance guards reject replaced documents.",
922 {
923 type => "object",
924 properties => {
925 target => { type => "string", minLength => 1 },
926 code => { type => "string", description => "Expression or { ... } body for async () => CODE. Do not include async () => wrapper." },
927 instance_id => { type => "string", minLength => 1 },
928 revision => { type => "integer", minimum => 1 },
929 navigate => { type => "boolean", default => JSON::XS::false },
930 max_bytes => { type => "integer", minimum => 1024, maximum => 2 * 1024 * 1024, default => 16 * 1024 },
931 timeout => { type => "number", minimum => 1, maximum => 300, default => 30 },
932 },
933 required => ["target", "code"], additionalProperties => JSON::XS::false,
934 },
935 0 ▶ sub {
Subroutine __ANON__: not called CC=15 SCAR=54.8
936 0 my ($req, $args) = @_;
937  
938 0 ▶ my $page = $pages{$args->{target}} or die "Browser page is not connected\n";
Branch: 1 branch
BranchTrueFalseTotal
my $page = $pages{$args->{"target"}} or die "Browser page is not connected\n" 0 0 0
939 !defined $args->{instance_id} || $args->{instance_id} eq $page->{instance_id}
940 0 ▶ or die "Browser document instance changed\n";
Branch: 1 branch
BranchTrueFalseTotal
not defined $args->{"instance_id"} or $args->{"instance_id"} eq $page->{"instance_id"} or die "Browser document instance changed\n" 0 0 0
Condition0%0/3
not defined $args->{"instance_id"} or $args->{"instance_id"} eq $page->{"instance_id"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $args->{"instance_id"} or $args->{"instance_id"} eq $page->{"instance_id"}
not defined $args->{"instance_id"} $args->{"instance_id"} eq $page->{"instance_id"}
Truth table0%0/3
not(A) or B
A= not defined $args->{"instance_id"}B= $args->{"instance_id"} eq $page->{"instance_id"}
ABresult
1X1
011
000
941 !defined $args->{revision} || $args->{revision} == $page->{tab}{revision}
942 0 ▶ or die "Browser document revision changed (now $page->{tab}{revision})\n";
Branch: 1 branch
BranchTrueFalseTotal
not defined $args->{"revision"} or $args->{"revision"} == $page->{"tab"}{"revision"} or die qq[Browser document revision changed (now $page->{"tab"}{"revision"})\n] 0 0 0
Condition0%0/3
not defined $args->{"revision"} or $args->{"revision"} == $page->{"tab"}{"revision"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $args->{"revision"} or $args->{"revision"} == $page->{"tab"}{"revision"}
not defined $args->{"revision"} $args->{"revision"} == $page->{"tab"}{"revision"}
Truth table0%0/3
not(A) or B
A= not defined $args->{"revision"}B= $args->{"revision"} == $page->{"tab"}{"revision"}
ABresult
1X1
011
000
943 0 ▶ !$page->{tab}{navigation} or die "Navigation already in progress for this tab\n";
Branch: 1 branch
BranchTrueFalseTotal
$page->{"tab"}{"navigation"} and die "Navigation already in progress for this tab\n" 0 0 0
944 0 permission $req, exec => $args->{target}, $args->{code};
945  
946 AE::log debug => "webbrowser " . ($args->{navigate} ? "navigate " : "execute ")
947 . $json->encode ($args->{target}) . " " . url_preview ($page->{state}{url})
948 0 ▶ . " " . code_preview ($args->{code}, 20);
Branch: 1 branch
BranchTrueFalseTotal
$args->{'navigate'} ? : 0 0 0
949  
950 0 ▶ $pages{$args->{target}} && $pages{$args->{target}} == $page
Branch: 1 branch
BranchTrueFalseTotal
$pages{$args->{"target"}} and $pages{$args->{"target"}} == $page or die "Browser document changed while permission was pending\n" 0 0 0
Condition0%0/3
$pages{$args->{"target"}} and $pages{$args->{"target"}} == $page
!ll&&!rl&&r
000
MC/DC0%0/2
$pages{$args->{"target"}} and $pages{$args->{"target"}} == $page
$pages{$args->{"target"}} $pages{$args->{"target"}} == $page
Truth table0%0/3
A and B
A= $pages{$args->{"target"}}B= $pages{$args->{"target"}} == $page
ABresult
0X0
100
111
951 or die "Browser document changed while permission was pending\n";
952  
953 0 my $id = ++$page->{rpcid};
954 0 ▶ if ($args->{navigate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"navigate"}) 0 0 0
955 0 my $tab = $page->{tab};
956 my $entry = $tab->{navigation} = {
957 request => $req, page => $page, id => $id, phase => "ready", code => $args->{code},
958 0 };
959 0 ▶ $entry->{timer} = AE::timer $args->{timeout} // 30, 0, sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
960 0 ▶ return unless $tab->{navigation} && $tab->{navigation} == $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless $tab->{"navigation"} and $tab->{"navigation"} == $entry 0 0 0
Condition0%0/3
$tab->{"navigation"} and $tab->{"navigation"} == $entry
!ll&&!rl&&r
000
MC/DC0%0/2
$tab->{"navigation"} and $tab->{"navigation"} == $entry
$tab->{"navigation"} $tab->{"navigation"} == $entry
Truth table0%0/3
A and B
A= $tab->{"navigation"}B= $tab->{"navigation"} == $entry
ABresult
0X0
100
111
961 0 ▶ if ($entry->{phase} eq "proceed") {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{'phase'} eq 'proceed') { } 0 0 0
962 0 ▶ send_message $entry->{page}, ["cancel", $id*1] if $tab->{page} && $tab->{page} == $entry->{page};
Branch: 1 branch
BranchTrueFalseTotal
if $tab->{"page"} and $tab->{"page"} == $entry->{"page"} 0 0 0
Condition0%0/3
$tab->{"page"} and $tab->{"page"} == $entry->{"page"}
!ll&&!rl&&r
000
MC/DC0%0/2
$tab->{"page"} and $tab->{"page"} == $entry->{"page"}
$tab->{"page"} $tab->{"page"} == $entry->{"page"}
Truth table0%0/3
A and B
A= $tab->{"page"}B= $tab->{"page"} == $entry->{"page"}
ABresult
0X0
100
111
963 0 finish_navigation $tab, "timeout", "No completed navigation was observed before timeout";
964 } else {
965 0 delete $tab->{navigation};
966 0 ▶ send_message $page, ["cancel", $id*1] if $tab->{page} && $tab->{page} == $page;
Branch: 1 branch
BranchTrueFalseTotal
if $tab->{"page"} and $tab->{"page"} == $page 0 0 0
Condition0%0/3
$tab->{"page"} and $tab->{"page"} == $page
!ll&&!rl&&r
000
MC/DC0%0/2
$tab->{"page"} and $tab->{"page"} == $page
$tab->{"page"} $tab->{"page"} == $page
Truth table0%0/3
A and B
A= $tab->{"page"}B= $tab->{"page"} == $page
ABresult
0X0
100
111
967 0 ▶ $req->error ("Browser did not acknowledge navigation") unless $req->{finished};
Branch: 1 branch
BranchTrueFalseTotal
unless $req->{"finished"} 0 0 0
968 0 ▶ expire_tab $tab unless $tab->{page};
Branch: 1 branch
BranchTrueFalseTotal
unless $tab->{"page"} 0 0 0
969 }
970 0 ▶ };
Condition0%0/2
$args->{"timeout"} // 30
l!l
00
MC/DC0%0/1
$args->{"timeout"} // 30
$args->{"timeout"}
Truth table0%0/2
A
A= $args->{"timeout"}
Aresult
11
00
971 0 ▶ $req->{on_cancel} = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
972 0 ▶ return unless $tab->{navigation} && $tab->{navigation} == $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless $tab->{"navigation"} and $tab->{"navigation"} == $entry 0 0 0
Condition0%0/3
$tab->{"navigation"} and $tab->{"navigation"} == $entry
!ll&&!rl&&r
000
MC/DC0%0/2
$tab->{"navigation"} and $tab->{"navigation"} == $entry
$tab->{"navigation"} $tab->{"navigation"} == $entry
Truth table0%0/3
A and B
A= $tab->{"navigation"}B= $tab->{"navigation"} == $entry
ABresult
0X0
100
111
973 0 delete $tab->{navigation};
974 0 delete $entry->{timer};
975 0 ▶ send_message $page, ["cancel", $id*1] if $tab->{page} && $tab->{page} == $page;
Branch: 1 branch
BranchTrueFalseTotal
if $tab->{"page"} and $tab->{"page"} == $page 0 0 0
Condition0%0/3
$tab->{"page"} and $tab->{"page"} == $page
!ll&&!rl&&r
000
MC/DC0%0/2
$tab->{"page"} and $tab->{"page"} == $page
$tab->{"page"} $tab->{"page"} == $page
Truth table0%0/3
A and B
A= $tab->{"page"}B= $tab->{"page"} == $page
ABresult
0X0
100
111
976 0 ▶ expire_tab $tab unless $tab->{page};
Branch: 1 branch
BranchTrueFalseTotal
unless $tab->{"page"} 0 0 0
977 0 };
978 send_message $page, ["call", $id*1, "navigate", {
979 code => $args->{code},
980 0 ▶ max_bytes => $args->{max_bytes} // 16 * 1024,
Condition0%0/2
$args->{"max_bytes"} // 16384
l!l
00
MC/DC0%0/1
$args->{"max_bytes"} // 16384
$args->{"max_bytes"}
Truth table0%0/2
A
A= $args->{"max_bytes"}
Aresult
11
00
981 }];
982 0 return;
983 }
984  
985 0 my $entry = $page->{pending}{$id} = { request => $req };
986 0 weaken (my $weak = $page);
987 0 ▶ $entry->{timer} = AE::timer $args->{timeout} // 30, 0, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
988 0 ▶ return unless $weak && exists $weak->{pending}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $weak and exists $weak->{"pending"}{$id} 0 0 0
Condition0%0/3
$weak and exists $weak->{"pending"}{$id}
!ll&&!rl&&r
000
MC/DC0%0/2
$weak and exists $weak->{"pending"}{$id}
$weak exists $weak->{"pending"}{$id}
Truth table0%0/3
A and B
A= $weakB= exists $weak->{"pending"}{$id}
ABresult
0X0
100
111
989 0 send_message $weak, ["cancel", $id*1];
990 0 finish_call $weak, $id, 0, "Browser execution timed out";
991 0 ▶ };
Condition0%0/2
$args->{"timeout"} // 30
l!l
00
MC/DC0%0/1
$args->{"timeout"} // 30
$args->{"timeout"}
Truth table0%0/2
A
A= $args->{"timeout"}
Aresult
11
00
992 0 ▶ $req->{on_cancel} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
993 0 ▶ return unless $weak && delete $weak->{pending}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $weak and delete $weak->{"pending"}{$id} 0 0 0
Condition0%0/3
$weak and delete $weak->{"pending"}{$id}
!ll&&!rl&&r
000
MC/DC0%0/2
$weak and delete $weak->{"pending"}{$id}
$weak delete $weak->{"pending"}{$id}
Truth table0%0/3
A and B
A= $weakB= delete $weak->{"pending"}{$id}
ABresult
0X0
100
111
994 0 send_message $weak, ["cancel", $id*1];
995 0 };
996 0 AE::log trace => "webbrowser execute $args->{target} #$id: " . code_preview ($args->{code});
997 send_message $page, ["call", $id*1, "execute", {
998 code => $args->{code},
999 0 ▶ max_bytes => $args->{max_bytes} // 16 * 1024,
Condition0%0/2
$args->{"max_bytes"} // 16384
l!l
00
MC/DC0%0/1
$args->{"max_bytes"} // 16384
$args->{"max_bytes"}
Truth table0%0/2
A
A= $args->{"max_bytes"}
Aresult
11
00
1000 }];
1001 },
1002 1 );
1003 $toolset->tool (add_element => "Insert DOM element via userscript GM_addElement; style/script bypass page CSP. "
1004 . "Set HTML attributes via attributes; DOM properties (textContent/innerHTML) via properties. "
1005 . "parent: CSS selector; default document.documentElement. "
1006 . "Set distinctive id or data-* to find/edit/remove later via execute. "
1007 . "No automatic ID/registry.",
1008 {
1009 type => "object",
1010 properties => {
1011 target => { type => "string", minLength => 1 },
1012 tag => { type => "string", minLength => 1 },
1013 parent => { type => "string", minLength => 1 },
1014 attributes => { type => "object", additionalProperties => { type => "string" } },
1015 properties => { type => "object", additionalProperties => JSON::XS::true },
1016 instance_id => { type => "string", minLength => 1 },
1017 revision => { type => "integer", minimum => 1 },
1018 },
1019 required => ["target", "tag"], additionalProperties => JSON::XS::false,
1020 },
1021 0 ▶ sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
1022 0 my ($req, $args) = @_;
1023  
1024 0 ▶ my $page = $pages{$args->{target}} or die "Browser page is not connected\n";
Branch: 1 branch
BranchTrueFalseTotal
my $page = $pages{$args->{"target"}} or die "Browser page is not connected\n" 0 0 0
1025 !defined $args->{instance_id} || $args->{instance_id} eq $page->{instance_id}
1026 0 ▶ or die "Browser document instance changed\n";
Branch: 1 branch
BranchTrueFalseTotal
not defined $args->{"instance_id"} or $args->{"instance_id"} eq $page->{"instance_id"} or die "Browser document instance changed\n" 0 0 0
Condition0%0/3
not defined $args->{"instance_id"} or $args->{"instance_id"} eq $page->{"instance_id"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $args->{"instance_id"} or $args->{"instance_id"} eq $page->{"instance_id"}
not defined $args->{"instance_id"} $args->{"instance_id"} eq $page->{"instance_id"}
Truth table0%0/3
not(A) or B
A= not defined $args->{"instance_id"}B= $args->{"instance_id"} eq $page->{"instance_id"}
ABresult
1X1
011
000
1027 !defined $args->{revision} || $args->{revision} == $page->{tab}{revision}
1028 0 ▶ or die "Browser document revision changed (now $page->{tab}{revision})\n";
Branch: 1 branch
BranchTrueFalseTotal
not defined $args->{"revision"} or $args->{"revision"} == $page->{"tab"}{"revision"} or die qq[Browser document revision changed (now $page->{"tab"}{"revision"})\n] 0 0 0
Condition0%0/3
not defined $args->{"revision"} or $args->{"revision"} == $page->{"tab"}{"revision"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $args->{"revision"} or $args->{"revision"} == $page->{"tab"}{"revision"}
not defined $args->{"revision"} $args->{"revision"} == $page->{"tab"}{"revision"}
Truth table0%0/3
not(A) or B
A= not defined $args->{"revision"}B= $args->{"revision"} == $page->{"tab"}{"revision"}
ABresult
1X1
011
000
1029 0 ▶ !$page->{tab}{navigation} or die "Navigation already in progress for this tab\n";
Branch: 1 branch
BranchTrueFalseTotal
$page->{"tab"}{"navigation"} and die "Navigation already in progress for this tab\n" 0 0 0
1030 0 permission $req, exec => $args->{target}, "GM_addElement " . $json->encode ($args);
1031 0 ▶ $pages{$args->{target}} && $pages{$args->{target}} == $page
Branch: 1 branch
BranchTrueFalseTotal
$pages{$args->{"target"}} and $pages{$args->{"target"}} == $page or die "Browser document changed while permission was pending\n" 0 0 0
Condition0%0/3
$pages{$args->{"target"}} and $pages{$args->{"target"}} == $page
!ll&&!rl&&r
000
MC/DC0%0/2
$pages{$args->{"target"}} and $pages{$args->{"target"}} == $page
$pages{$args->{"target"}} $pages{$args->{"target"}} == $page
Truth table0%0/3
A and B
A= $pages{$args->{"target"}}B= $pages{$args->{"target"}} == $page
ABresult
0X0
100
111
1032 or die "Browser document changed while permission was pending\n";
1033  
1034 0 my $id = ++$page->{rpcid};
1035 0 my $entry = $page->{pending}{$id} = { request => $req };
1036 0 weaken (my $weak = $page);
1037 0 ▶ $entry->{timer} = AE::timer 30, 0, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1038 0 ▶ return unless $weak && exists $weak->{pending}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $weak and exists $weak->{"pending"}{$id} 0 0 0
Condition0%0/3
$weak and exists $weak->{"pending"}{$id}
!ll&&!rl&&r
000
MC/DC0%0/2
$weak and exists $weak->{"pending"}{$id}
$weak exists $weak->{"pending"}{$id}
Truth table0%0/3
A and B
A= $weakB= exists $weak->{"pending"}{$id}
ABresult
0X0
100
111
1039 0 send_message $weak, ["cancel", $id*1];
1040 0 finish_call $weak, $id, 0, "Browser add_element timed out";
1041 0 };
1042 0 ▶ $req->{on_cancel} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1043 0 ▶ return unless $weak && delete $weak->{pending}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $weak and delete $weak->{"pending"}{$id} 0 0 0
Condition0%0/3
$weak and delete $weak->{"pending"}{$id}
!ll&&!rl&&r
000
MC/DC0%0/2
$weak and delete $weak->{"pending"}{$id}
$weak delete $weak->{"pending"}{$id}
Truth table0%0/3
A and B
A= $weakB= delete $weak->{"pending"}{$id}
ABresult
0X0
100
111
1044 0 send_message $weak, ["cancel", $id*1];
1045 0 };
1046 0 AE::log debug => "webbrowser add_element $args->{target}: " . $json->encode ($args->{tag});
1047 send_message $page, ["call", $id*1, "add_element", {
1048 0 ▶ map { exists $args->{$_} ? ($_ => $args->{$_}) : () } qw(tag parent attributes properties)
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{$_} ? : 0 0 0
1049 }];
1050 },
1051 1 );
1052  
1053 $toolset->tool (list_tab_operations =>
1054 "List additional operations offered by a connected tab. Returns {operations: [...]} with descriptions and argument schemas; empty if none. Requires read permission.",
1055 {
1056 type => "object",
1057 properties => {
1058 target => { type => "string", minLength => 1 },
1059 instance_id => { type => "string", minLength => 1 },
1060 revision => { type => "integer", minimum => 1 },
1061 timeout => { type => "number", minimum => 1, maximum => 300, default => 30 },
1062 max_bytes => { type => "integer", minimum => 1024, maximum => 2 * 1024 * 1024, default => 16 * 1024 },
1063 },
1064 required => ["target"], additionalProperties => JSON::XS::false,
1065 },
1066 0 ▶ sub { tab_operation $_[0], $_[1], "list_tab_operations" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1067 1 );
1068 $toolset->tool (call_tab_operation =>
1069 "Invoke an additional operation offered by a connected tab. Find operations with list-tab-operations. The browser handles its own operation permissions; requires broker exec permission.",
1070 {
1071 type => "object",
1072 properties => {
1073 target => { type => "string", minLength => 1 },
1074 operation => { type => "string", minLength => 1 },
1075 arguments => { type => "object", default => {} },
1076 instance_id => { type => "string", minLength => 1 },
1077 revision => { type => "integer", minimum => 1 },
1078 timeout => { type => "number", minimum => 1, maximum => 300, default => 30 },
1079 max_bytes => { type => "integer", minimum => 1024, maximum => 2 * 1024 * 1024, default => 16 * 1024 },
1080 },
1081 required => ["target", "operation"], additionalProperties => JSON::XS::false,
1082 },
1083 0 ▶ sub { tab_operation $_[0], $_[1], "call_tab_operation" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1084 1 );
1085  
1086 1 mcp_tool_help ($options->{tool_help}, $toolset);
1087  
1088 my $custom_instructions = $options->{custom_instructions}
1089 // $config->{section}{"instance $name"}{custom_instructions}
1090 0 ▶ // $config->{section}{"program " . AnyEvent::MCP::Config::program_name ()}{custom_instructions};
Condition0%0/6
($options->{"custom_instructions"} // $config->{"section"}{"instance $name"}{"custom_instructions"}) // $config->{"section"}{'program ' . AnyEvent::MCP::Config::program_name()}{"custom_instructions"}
l!l&&r!l&&!r
000
$options->{"custom_instructions"} // $config->{"section"}{"instance $name"}{"custom_instructions"}
l!l&&r!l&&!r
000
MC/DC0%0/3
($options->{"custom_instructions"} // $config->{"section"}{"instance $name"}{"custom_instructions"}) // $config->{"section"}{'program ' . AnyEvent::MCP::Config::program_name()}{"custom_instructions"}
$options->{"custom_instructions"} $config->{"section"}{"instance $name"}{"custom_instructions"} $config->{"section"}{'program ' . AnyEvent::MCP::Config::program_name()}{"custom_instructions"}
Truth table0%0/4
A // B // C
A= $options->{"custom_instructions"}B= $config->{"section"}{"instance $name"}{"custom_instructions"}C= $config->{"section"}{'program ' . AnyEvent::MCP::Config::program_name()}{"custom_instructions"}
ABCresult
1XX1
01X1
0011
0000
1091 my %server = (
1092 server_info => { name => "mcp-webbrowser", version => "0" },
1093 auto_instructions => 1,
1094 short_instructions => "Inspect connected tabs; run page JavaScript; discover optional per-tab operations. Discover tools.",
1095 (defined $custom_instructions ? (custom_instructions => $custom_instructions) : ()),
1096 instructions => "Tabs have stable targets/document revisions. "
1097 . "Use tabs for current revision. execute runs async () => CODE in page; "
1098 . "expression returns implicitly; { ... } body needs return; parenthesize object literals. "
1099 . "navigate=true acknowledges before evaluation; waits for new document or SPA navigation. "
1100 . "Navigation result: document, same-document, timeout. Execution may have happened even on timeout; never retried. "
1101 . "Guard later calls with revision; metadata client-reported. "
1102 . "list-tab-operations discovers browser-provided operations for one tab; "
1103 . "call-tab-operation invokes one by name and JSON arguments.",
1104 toolsets => [$toolset], permissions => $permissions,
1105 on_request => \&mcp_log_request,
1106 0 ▶ on_error => sub { AE::log info => "mcp-tool $_[1]{tool}: $_[2]" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1107 0 ▶);
Branch: 1 branch
BranchTrueFalseTotal
defined $custom_instructions ? : 0 0 0
1108 0 my $http = AnyEvent::MCP::Server::HTTP->new (%server, %http, path => "/");
1109 0 my $stream;
1110  
1111 0 ▶if (defined $connect) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $connect) { } 0 0 0
elsif ($stdio) { } 0 0 0
1112 0 require AnyEvent::MCP::Server::Connect;
1113 $stream = AnyEvent::MCP::Server::Connect->new (
1114 0 ▶ %server, connect => $connect, on_close => sub { $done->send },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1115 0 );
1116 0 $stream->start;
1117 } elsif ($stdio) {
1118 0 binmode STDIN;
1119 0 binmode STDOUT;
1120 $stream = AnyEvent::MCP::Server::JSONL->new (
1121 0 ▶ %server, on_connection_close => sub { $done->send },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1122 0 );
1123 0 $stream->accept (\*STDIN, \*STDOUT);
1124 }
1125  
1126 0 ▶my $listener = AnyEvent::Socket::tcp_server $http{host}, $http{port}, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1127 0 ▶ my ($fh) = @_ or return;
Branch: 1 branch
BranchTrueFalseTotal
my($fh) = or return 0 0 0
1128 0 ▶ my $watcher; $watcher = AE::io $fh, 0, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1129 0 undef $watcher;
1130 0 ▶ defined recv $fh, my $magic, 1, Socket::MSG_PEEK or return;
Branch: 1 branch
BranchTrueFalseTotal
defined recv($fh, my $magic, 1, 2) or return 0 0 0
1131 0 ▶ if ($magic eq "G") {
Branch: 1 branch
BranchTrueFalseTotal
if ($magic eq 'G') { } 0 0 0
1132 0 accept_browser $fh;
1133 } else {
1134 0 $http->accept ($fh);
1135 }
1136 0 };
1137 0 };
1138 0 ▶my @signals = map { AE::signal $_, sub { $done->send } } qw(INT TERM);
Subroutine __ANON__: not called CC=1 SCAR=6.9
1139 0 $done->recv;
1140 0 undef $listener;
1141 0 close_page $_, 1000, "Browser broker stopped" for values %connections;
1142 0 finish_tab $_, "broker stopped" for values %tabs;
1143 0 ▶$stream->close if $stream;
Branch: 1 branch
BranchTrueFalseTotal
if $stream 0 0 0
1144 0 $http->close;
↑ Summary lib/AnyEvent/MCP/Worker/IMAP.pm »