bin/mcp-webbrowser

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