bin/mcp-webbrowser

filter: Statementstmt 92.6% 355 / 383 Branchbran 67.4% 170 / 252 Conditioncond 51.3% 149 / 290 MC/DCmcdc 23.1% 35 / 151 Subroutinesub 87.9% 51 / 58 Podpod n/a totaltotal 67.0% 760 / 1134 CC 215 SCAR 45.8
CC
215
Cov
73%
receive_message
337.0
__ANON__
42.0
tab_operation
41.0
CRAP 97.7
« blib/lib/AnyEvent/MCP/Server/Simple.pm ↑ 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 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 currently loses the saved per-tab permission when
103 Chromium replaces a discarded tab. After such a discard and restoration,
104 you must choose Allow again. This is tracked in
105 L<https://github.com/scriptscat/scriptcat/issues/1791>.
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 JavaScript exec prompts offer this exact code in this target, any code in
229 this target, or any code in all targets. Operation exec prompts offer
230 corresponding scopes for browser operations. Independently, each exec request
231 offers this origin, the same scheme and host on any port, or any origin.
232 Only HTTP(S) origins offer persistent scopes: opaque origins such as C<null>
233 cannot distinguish different documents, so they allow one-time decisions only.
234 Saved target grants also cover future tabs using the same target name.
235 Saved rules use C<match.subject> and C<match.origin> as independent fields.
236 Rules without C<match.origin> match all origins, as do broad C<--auto-allow>
237 overrides. The broker derives origins from the URL already reported by the
238 userscript on connect, state changes and navigation, including older clients.
239 For HTTP(S), credentials and the path/query/fragment are excluded.
240 Origin identity relies on the connected userscript's reported URL, not on
241 independently authenticating the page's location.
242 C<--ask> selects the same helper, tty, yad, mcp or auto modes as other servers.
243  
244 =head1 OPTIONS
245  
246 Shared switches are documented in
247 L<AnyEvent::MCP::Server::Simple/COMMON COMMAND-LINE OPTIONS>.
248 The following switch is specific to C<mcp-webbrowser>.
249  
250 =over
251  
252 =item --ask MODE
253  
254 Permission prompts: C<helper> (default), C<tty>, C<yad>, C<mcp> or
255 C<auto>. tty/yad force local interface; mcp delegates to client,
256 auto prefers native elicitation when offered. See L</PERMISSIONS>.
257  
258 =back
259  
260 =head2 Shared-option behavior
261  
262 =over
263  
264 =item --stdio
265  
266 The browser's auxiliary HTTP/WebSocket listener stays active while
267 MCP uses stdio.
268  
269 =item --connect URL
270  
271 The auxiliary browser HTTP/WebSocket listener stays active while
272 MCP connects to an upstream multibroker.
273  
274 =item --auto-allow TOOL[,ACTION[,CAPABILITY]]
275  
276 For example, C<--auto-allow '*,*'> allows discovery and JavaScript
277 execution in explicitly opted-in tabs. The executed page code then
278 has full authority within that page; use this only when intended.
279  
280 =back
281  
282 =head1 BROWSER PROTOCOL
283  
284 WebSocket authentication happens at C</TOKEN/TARGET>. C<TARGET> is one
285 percent-encoded path component. Before ordinary messages, send:
286  
287 ["mcp-webbrowser", 1, 1, INSTANCE_ID, {"url": URL, "title": TITLE}]
288  
289 C<INSTANCE_ID> identifies one loaded document. Keep it across reconnects of that
290 document and change it on navigation/reload. The private browser protocol is
291 1.1, independent of MCP and the old xpcse browser-transfer protocol. Compatible
292 older/newer minor versions can connect. Version 1.0 clients support page
293 execution but not additional tab operations; discovery returns an empty list
294 and unsupported calls fail without disconnecting the browser.
295 The broker answers C<["welcome", TARGET, INSTANCE_ID, REVISION]>.
296 The requested target represents a browser tab: a different document with the
297 same target replaces its previous connection and increments the revision.
298 Reconnecting the same document retains that revision. A disconnected tab's
299 identity expires after 30 seconds with no pending navigation. No page is
300 exposed before a valid hello.
301  
302 A tab session starts on first hello, remains active across temporary socket
303 disconnects and document replacements, and finishes on an explicit C<quit>,
304 after 30 seconds of inactivity, or when the broker stops. Session start
305 and finish are logged at C<info>; ordinary socket connection and disconnection
306 events are logged at C<trace>. Revision and request IDs are transmitted as
307 JSON numbers, even when Perl string operations have touched those scalars.
308 Approved JavaScript is logged at C<trace> when dispatched for ordinary evaluation,
309 or when C<proceed> is sent for navigation (never during the permission prompt).
310  
311 Broker to browser:
312  
313 ["call", ID, "execute", {"code": CODE, "max_bytes": BYTES}]
314 ["call", ID, "navigate", {"code": CODE, "max_bytes": BYTES}]
315 ["call", ID, "add_element", {"tag": TAG, "parent": SELECTOR, "attributes": ATTRS, "properties": PROPS}]
316 ["call", ID, "list_tab_operations", {"max_bytes": BYTES}]
317 ["call", ID, "call_tab_operation", {"operation": NAME, "arguments": ARGS, "max_bytes": BYTES}]
318 ["proceed", ID]
319 ["cancel", ID]
320  
321 Browser to broker:
322  
323 ["reply", ID, true, JSON_VALUE]
324 ["reply", ID, false, ERROR_STRING]
325 ["ready", ID]
326 ["navigated", ID, {"url": URL, "title": TITLE}]
327 ["state", {"url": URL, "title": TITLE}]
328 ["quit"]
329  
330 The browser replies C<ready> to a C<navigate> call without evaluating it.
331 Only C<proceed> starts evaluation. C<navigated> reports a completed
332 same-document navigation; a different document's hello is the other
333 completion path. C<reply> with C<false> can report an evaluation failure.
334 Only JSON values cross the wire; undefined becomes null. Errors are strings.
335 For C<list_tab_operations>, a successful reply is an array of operation
336 descriptions; an empty array means no additional operations. The broker
337 wraps it as C<{operations: [...]}>. C<call_tab_operation> uses the usual
338 C<{value: ...}> reply. The distributed userscript currently offers no
339 additional operations and returns an empty list or an operation error.
340 Late replies after cancellation are ignored. State updates replace cached
341 metadata. Malformed messages close the connection with status 1002.
342 The initial hello has a 15-second deadline; a silent connection is closed.
343  
344 The distributed userscript evaluates in the page's JavaScript world via an
345 injected script and returns serialized results through DOM events. Its
346 Navigation API listener is installed in that same world. The broker cannot
347 authenticate page-reported values or infer application-rendering readiness
348 from navigation success. The userscript retries disconnected WebSockets;
349 it has no event subscriptions or separate SPA revision counter.
350  
351 =cut
352  
353 4 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
354  
355 4 ▶use JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
356 4 ▶use Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
357 4 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
358  
359 4 ▶use AnyEvent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
360 4 ▶use AnyEvent::Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
361 4 ▶use AnyEvent::Handle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
362 4 ▶use AnyEvent::WebSocket::Server ();
Subroutine BEGIN: called CC=1 SCAR=0.0
363 4 ▶use AnyEvent::WebSocket::Message ();
Subroutine BEGIN: called CC=1 SCAR=0.0
364 4 ▶use AnyEvent::MCP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
365 4 ▶use AnyEvent::MCP::Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
366 4 ▶use AnyEvent::MCP::Server::HTTP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
367 4 ▶use AnyEvent::MCP::Server::JSONL ();
Subroutine BEGIN: called CC=1 SCAR=0.0
368 4 ▶use AnyEvent::MCP::Server::Permissions ();
Subroutine BEGIN: called CC=1 SCAR=0.0
369 4 ▶use AnyEvent::MCP::Server::Toolset ();
Subroutine BEGIN: called CC=1 SCAR=0.0
370  
371 4 ▶use AnyEvent::MCP::Server::Simple qw(mcp_getoptions mcp_tool_help mcp_log mcp_log_request);
Subroutine BEGIN: called CC=1 SCAR=0.0
372  
373 4 my $options = mcp_getoptions ({
374 usage => "Usage: mcp-webbrowser [OPTION]...\n",
375 path => "/",
376 description => "Expose connected browser tabs to MCP; HTTP/WebSocket stays active with stdio or --connect.",
377 options => [["--ask MODE", "Ask via helper (default), tty, yad, mcp, or auto"]],
378 examples => " mcp-webbrowser --auto-allow '*,*' # all actions in opted-in tabs",
379 }, "ask=s" => \my $ask);
380 3 ▶@ARGV and die "Unexpected arguments\n";
Branch: 1 branch
BranchTrueFalseTotal
@ARGV and die "Unexpected arguments\n" 0 3 3
381 3 my $name = $options->{name};
382 3 my $connect = $options->{connect};
383 3 ▶my $stdio = $options->{stdio} || !defined $connect && $ENV{PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL} eq "stdio";
Condition66%4/6
$options->{'stdio'} || !defined($connect) && $ENV{'PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL'} eq 'stdio'
l!l&&r!l&&!r
102
not defined $connect and $ENV{"PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL"} eq "stdio"
!ll&&!rl&&r
110
MC/DC33%1/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 table75%3/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
384 3 ▶!($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 3 3
Condition66%2/3
$stdio and defined $connect
!ll&&!rl&&r
210
MC/DC0%0/2
$stdio and defined $connect
$stdio defined $connect
Truth table66%2/3
A and B
A= $stdioB= defined $connect
ABresult
0X0
100
111
385  
386 3 my $config = AnyEvent::MCP::Config->global;
387 3 my %http = $config->http_settings ($name, $options->{bind});
388 3 ▶$http{tokens} = $options->{tokens} if $options->{tokens};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"tokens"} 1 2 3
389 3 ▶$http{connect_tokens} = $options->{tokens} if $options->{tokens};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"tokens"} 1 2 3
390 3 my %token = map { $_ => 1 } @{$http{connect_tokens}};
391 3 my $json = JSON::XS->new->utf8->max_depth (64);
392 3 my (%connections, %pages, %tabs);
393 3 my $done = AE::cv;
394  
395 my $permissions = AnyEvent::MCP::Server::Permissions->new (
396 name => "webbrowser-$name",
397 actions => {
398 read => "List connected browser tabs and their reported metadata",
399 exec => "Execute JavaScript or tab operations in a connected browser page",
400 },
401 (defined $ask ? (ask => $ask) : ()),
402 3 ▶ auto_rules => [@{$options->{auto_rules} || []},
Branch: 1 branch
BranchTrueFalseTotal
defined $ask ? : 0 3 3
Condition50%1/2
$options->{'auto_rules'} || []
l!l
03
MC/DC0%0/1
$options->{'auto_rules'} || []
$options->{'auto_rules'}
Truth table50%1/2
A
A= $options->{'auto_rules'}
Aresult
11
00
403 $config->auto_rules ($name)],
404 );
405  
406 23 ▶sub page_label {
Subroutine page_label: called CC=3 SCAR=11.1
407 23 my ($state) = @_;
408  
409 23 my ($title, $url) = @{$state}{qw(title url)};
410 23 s/[\x00-\x1f\x7f]/ /g for $title, $url;
411  
412 23 ▶ (length $title ? (substr $title, 0, 80) . " - " : "")
Branch: 1 branch
BranchTrueFalseTotal
length $title ? : 23 0 23
413 . (substr $url, 0, 160)
414 }
415  
416 24 ▶sub code_preview {
Subroutine code_preview: called CC=3 SCAR=11.0
417 24 my ($code, $limit) = @_;
418  
419 24 ▶ $limit //= 160;
Condition100%2/2
$limit //= 160
l!l
1212
MC/DC100%1/1
$limit //= 160
$limit
Truth table100%2/2
A
A= $limit
Aresult
11
00
420 24 my $short = substr $code, 0, $limit;
421 24 ▶ $short .= "..." if length $code > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if length $code > $limit 3 21 24
422 24 $json->encode ($short)
423 }
424  
425 17 ▶sub url_preview {
Subroutine url_preview: called CC=2 SCAR=7.0
426 17 my $url = shift;
427  
428 17 $url =~ s/[?#].*//s;
429 17 $url =~ s{(://)[^/]*@}{$1};
430 17 $url =~ s/[\x00-\x1f\x7f]/ /g;
431 17 my $short = substr $url, 0, 64;
432 17 ▶ $short .= "..." if length $url > 64;
Branch: 1 branch
BranchTrueFalseTotal
if length $url > 64 0 17 17
433 17 $json->encode ($short)
434 }
435  
436 14 ▶sub browser_origin {
Subroutine browser_origin: called CC=2 SCAR=7.0
437 14 my ($scheme, $authority) = $_[0] =~ m{\A(https?://)([^/?#]+)};
438 14 ▶ return "null" unless defined $authority;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $authority 0 14 14
439 14 $authority =~ s/.*@//;
440 14 $scheme . $authority
441 }
442  
443 14 ▶sub browser_state {
Subroutine browser_state: called CC=1 SCAR=0.0
444 14 my ($state) = @_;
445 14 +{ %$state, origin => browser_origin ($state->{url}) }
446 }
447  
448 50 ▶sub permission {
Subroutine permission: called CC=17 SCAR=28.5
449 50 my ($req, $action, $target, $code, $kind) = @_;
450  
451 50 my $subject = "tabs";
452 50 my @subjects = ({ label => "the list of connected browser tabs", match => $subject });
453 50 my ($origin, @origins);
454  
455 50 ▶ if (defined $target) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target) 23 27 50
456 23 ▶ 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" 23 0 23
457 23 my $label = (page_label $page->{state}) . " (tab " . (substr $target, 0, 8) . ")";
458  
459 23 my $coder = JSON::XS->new->allow_nonref;
460 23 ▶ my $operation = defined $kind && $kind eq "operation";
Condition66%2/3
defined $kind && $kind eq 'operation'
!ll&&!rl&&r
1805
MC/DC50%1/2
defined $kind && $kind eq 'operation'
defined $kind $kind eq 'operation'
Truth table66%2/3
A && B
A= defined $kindB= $kind eq 'operation'
ABresult
0X0
100
111
461 23 ▶ my $prefix = ($operation ? "operation:" : "execute:") . $coder->encode ($target) . ":";
Branch: 1 branch
BranchTrueFalseTotal
$operation ? : 5 18 23
462 23 $subject = $prefix . $coder->encode ($code);
463 23 ▶ @subjects = (
Branch: 4 branches
BranchTrueFalseTotal
$operation ? : 5 18 23
$operation ? : 5 18 23
$operation ? : 5 18 23
$operation ? : 5 18 23
464 {
465 label => ($operation ? "this exact operation" : "this exact code") . " in page '$label'",
466 match => $subject,
467 },
468 {
469 label => ($operation ? "any operation" : "any JavaScript") . " in page '$label'",
470 match => "^" . (quotemeta $prefix),
471 },
472 {
473 label => ($operation ? "any operation" : "any JavaScript")
474 . " in all browser tabs, including future connections",
475 match => "^" . (quotemeta ($operation ? "operation:" : "execute:")),
476 },
477 );
478 23 $target = $label;
479 23 $origin = $page->{state}{origin};
480 23 ▶ if ($origin =~ m{\Ahttps?://}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($origin =~ m[\Ahttps?://]u) 23 0 23
481 23 @origins = ({ label => "This origin ($origin)", match => $origin });
482 23 (my $host_origin = $origin) =~ s/:[0-9]+\z//;
483 23 push @origins, {
484 label => "Same scheme and host, any port ($host_origin)",
485 match => "^" . (quotemeta $host_origin) . "(?::[0-9]+)?\\z",
486 };
487 23 push @origins, { label => "Any origin", match => "^" };
488 }
489 }
490  
491 $req->require_permission ({
492 tool => $req->{tool}, action => $action,
493 dimensions => {
494 subject => {
495 label => "Code / tab", value => $subject, choices => \@subjects,
496 (defined $target && !(defined $kind && $kind eq "operation")
497 50 ▶ ? (default => $subjects[1]{match}) : ()),
Branch: 4 branches
BranchTrueFalseTotal
defined $target && !(defined $kind && $kind eq 'operation') ? : 18 32 50
defined $origin ? : 23 27 50
defined $target ? : 23 27 50
defined $kind && $kind eq 'operation' ? : 5 18 23
Condition77%7/9
defined $target and not defined $kind && $kind eq "operation"
!ll&&!rl&&r
27518
defined $kind && $kind eq 'operation'
!ll&&!rl&&r
1805
defined $kind and $kind eq "operation"
!ll&&!rl&&r
1805
MC/DC66%2/3
defined $target and not defined $kind && $kind eq "operation"
defined $target defined $kind $kind eq 'operation'
MC/DC50%1/2
defined $kind and $kind eq "operation"
defined $kind $kind eq "operation"
Truth table75%3/4
A and not(B && C)
A= defined $targetB= defined $kindC= $kind eq 'operation'
ABCresult
0XX0
1110
10X1
1101
Truth table66%2/3
A and B
A= defined $kindB= $kind eq "operation"
ABresult
0X0
100
111
498 },
499 (defined $origin ? (origin => { label => "Origin", value => $origin, choices => \@origins }) : ()),
500 },
501 sandbox_grants => [],
502 reason => defined $target ? (defined $kind && $kind eq "operation"
503 ? "Call browser operation in '$target'\n\n$code"
504 : "Execute JavaScript in '$target'\n\n$code")
505 : "List connected browser tabs or tab operations",
506 });
507 }
508  
509 34 ▶sub send_message {
Subroutine send_message: called CC=1 SCAR=0.0
510 34 my ($page, $message) = @_;
511  
512 34 $page->{conn}->send (AnyEvent::WebSocket::Message->new (opcode => 1, body => $json->encode ($message)));
513 }
514  
515 15 ▶sub finish_call {
Subroutine finish_call: called CC=9 SCAR=22.3
516 15 my ($page, $id, $success, $value) = @_;
517  
518 15 ▶ my $entry = delete $page->{pending}{$id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = delete $page->{"pending"}{$id} or return 14 1 15
519 14 my $req = $entry->{request};
520 14 ▶ return if $req->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $req->{"finished"} 0 14 14
521  
522 14 ▶ 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 14 14
elsif ($success) { } 9 5 14
Condition88%8/9
$success and $entry->{"result_key"} and $entry->{"result_key"} eq "operations" and ref $value ne "ARRAY"
!ll&&!rl&&r
1130
$success and $entry->{"result_key"} and $entry->{"result_key"} eq "operations"
!ll&&!rl&&r
1013
$success and $entry->{"result_key"}
!ll&&!rl&&r
554
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 table80%4/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
523 0 $req->error ("Browser operation list is not an array");
524 } elsif ($success) {
525 9 ▶ $req->result ({ ($entry->{result_key} || "value") => $value });
Condition100%2/2
$entry->{'result_key'} || 'value'
l!l
45
MC/DC100%1/1
$entry->{'result_key'} || 'value'
$entry->{'result_key'}
Truth table100%2/2
A
A= $entry->{'result_key'}
Aresult
11
00
526 } else {
527 5 $req->error ($value);
528 }
529 }
530  
531 14 ▶sub tab_operation {
Subroutine tab_operation: called CC=28 SCAR=37.1
532 14 my ($req, $args, $method) = @_;
533  
534 defined $args->{target} && !ref $args->{target} && length $args->{target}
535 14 ▶ 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" 13 1 14
Condition66%4/6
defined $args->{"target"} and not ref $args->{"target"} and length $args->{"target"}
!ll&&!rl&&r
1013
defined $args->{"target"} and not ref $args->{"target"}
!ll&&!rl&&r
0113
MC/DC33%1/3
defined $args->{"target"} and not ref $args->{"target"} and length $args->{"target"}
defined $args->{"target"} not ref $args->{"target"} length $args->{"target"}
Truth table50%2/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
536 13 my $list = $method eq "list_tab_operations";
537 13 ▶ unless ($list) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($list) 8 5 13
538 defined $args->{operation} && !ref $args->{operation} && length $args->{operation}
539 8 ▶ 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" 7 1 8
Condition50%3/6
defined $args->{"operation"} and not ref $args->{"operation"} and length $args->{"operation"}
!ll&&!rl&&r
017
defined $args->{"operation"} and not ref $args->{"operation"}
!ll&&!rl&&r
008
MC/DC33%1/3
defined $args->{"operation"} and not ref $args->{"operation"} and length $args->{"operation"}
defined $args->{"operation"} not ref $args->{"operation"} length $args->{"operation"}
Truth table50%2/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
540 7 ▶ !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" 6 1 7
Condition100%3/3
not exists $args->{"arguments"} or ref $args->{"arguments"} eq "HASH"
l!l&&r!l&&!r
511
MC/DC100%2/2
not exists $args->{"arguments"} or ref $args->{"arguments"} eq "HASH"
not exists $args->{"arguments"} ref $args->{"arguments"} eq "HASH"
Truth table100%3/3
not(A) or B
A= not exists $args->{"arguments"}B= ref $args->{"arguments"} eq "HASH"
ABresult
1X1
011
000
541 or die "Browser operation arguments must be an object\n";
542 }
543  
544 11 ▶ 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" 11 0 11
545 !defined $args->{instance_id} || $args->{instance_id} eq $page->{instance_id}
546 11 ▶ 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" 10 1 11
Condition100%3/3
not defined $args->{"instance_id"} or $args->{"instance_id"} eq $page->{"instance_id"}
l!l&&r!l&&!r
821
MC/DC100%2/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 table100%3/3
not(A) or B
A= not defined $args->{"instance_id"}B= $args->{"instance_id"} eq $page->{"instance_id"}
ABresult
1X1
011
000
547 !defined $args->{revision} || $args->{revision} == $page->{tab}{revision}
548 10 ▶ 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] 9 1 10
Condition100%3/3
not defined $args->{"revision"} or $args->{"revision"} == $page->{"tab"}{"revision"}
l!l&&r!l&&!r
721
MC/DC100%2/2
not defined $args->{"revision"} or $args->{"revision"} == $page->{"tab"}{"revision"}
not defined $args->{"revision"} $args->{"revision"} == $page->{"tab"}{"revision"}
Truth table100%3/3
not(A) or B
A= not defined $args->{"revision"}B= $args->{"revision"} == $page->{"tab"}{"revision"}
ABresult
1X1
011
000
549 9 ▶ !$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 9 9
550  
551 9 ▶ if ($list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list) { } 4 5 9
552 4 permission $req, "read";
553 } else {
554 permission $req, exec => $args->{target},
555 5 ▶ $json->encode ({ operation => $args->{operation}, arguments => $args->{arguments} || {} }), "operation";
Condition100%2/2
$args->{'arguments'} || {}
l!l
14
MC/DC100%1/1
$args->{'arguments'} || {}
$args->{'arguments'}
Truth table100%2/2
A
A= $args->{'arguments'}
Aresult
11
00
556 }
557 7 ▶ $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" 7 0 7
Condition33%1/3
$pages{$args->{"target"}} and $pages{$args->{"target"}} == $page
!ll&&!rl&&r
007
MC/DC0%0/2
$pages{$args->{"target"}} and $pages{$args->{"target"}} == $page
$pages{$args->{"target"}} $pages{$args->{"target"}} == $page
Truth table33%1/3
A and B
A= $pages{$args->{"target"}}B= $pages{$args->{"target"}} == $page
ABresult
0X0
100
111
558 or die "Browser document changed while permission was pending\n";
559  
560 7 ▶ if ($page->{minor} < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page->{"minor"} < 1) 2 5 7
561 2 ▶ $req->result ({ operations => [] }) if $list;
Branch: 1 branch
BranchTrueFalseTotal
if $list 1 1 2
562 2 ▶ $req->error ("Browser tab operations require protocol 1.1") unless $list;
Branch: 1 branch
BranchTrueFalseTotal
unless $list 1 1 2
563 2 return;
564 }
565  
566 5 my $id = ++$page->{rpcid};
567 5 ▶ my $entry = $page->{pending}{$id} = {
Branch: 1 branch
BranchTrueFalseTotal
$list ? : 3 2 5
568 request => $req, result_key => $list ? "operations" : "value",
569 };
570 5 weaken (my $weak = $page);
571 0 ▶ $entry->{timer} = AE::timer $args->{timeout} // 30, 0, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
572 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
573 0 send_message $weak, ["cancel", $id*1];
574 0 finish_call $weak, $id, 0, "Browser tab operation timed out";
575 5 ▶ };
Condition50%1/2
$args->{"timeout"} // 30
l!l
05
MC/DC0%0/1
$args->{"timeout"} // 30
$args->{"timeout"}
Truth table50%1/2
A
A= $args->{"timeout"}
Aresult
11
00
576 0 ▶ $req->{on_cancel} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
577 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
578 0 send_message $weak, ["cancel", $id*1];
579 5 };
580 5 AE::log debug => "webbrowser $method " . $json->encode ($args->{target});
581 send_message $page, ["call", $id*1, $method, {
582 ($list ? () : (operation => $args->{operation}, arguments => $args->{arguments} || {})),
583 5 ▶ max_bytes => $args->{max_bytes} // 16 * 1024,
Branch: 1 branch
BranchTrueFalseTotal
$list ? : 3 2 5
Condition100%4/4
$args->{"arguments"} or {}
l!l
11
$args->{"max_bytes"} // 16384
l!l
23
MC/DC100%1/1
$args->{"arguments"} or {}
$args->{"arguments"}
MC/DC100%1/1
$args->{"max_bytes"} // 16384
$args->{"max_bytes"}
Truth table100%2/2
A
A= $args->{"arguments"}
Aresult
11
00
Truth table100%2/2
A
A= $args->{"max_bytes"}
Aresult
11
00
584 }];
585 }
586  
587 5 ▶sub finish_tab {
Subroutine finish_tab: called CC=3 SCAR=11.8
588 5 my ($tab, $reason) = @_;
589  
590 5 ▶ return unless $tabs{$tab->{requested}} && $tabs{$tab->{requested}} == $tab;
Branch: 1 branch
BranchTrueFalseTotal
unless $tabs{$tab->{"requested"}} and $tabs{$tab->{"requested"}} == $tab 0 5 5
Condition33%1/3
$tabs{$tab->{"requested"}} and $tabs{$tab->{"requested"}} == $tab
!ll&&!rl&&r
005
MC/DC0%0/2
$tabs{$tab->{"requested"}} and $tabs{$tab->{"requested"}} == $tab
$tabs{$tab->{"requested"}} $tabs{$tab->{"requested"}} == $tab
Truth table33%1/3
A and B
A= $tabs{$tab->{"requested"}}B= $tabs{$tab->{"requested"}} == $tab
ABresult
0X0
100
111
591 5 delete $tab->{expiry};
592 5 delete $tabs{$tab->{requested}};
593 5 mcp_log "$tab->{requested}: session finished ($reason)";
594 }
595  
596 9 ▶sub expire_tab {
Subroutine expire_tab: called CC=3 SCAR=13.6
597 9 my ($tab) = @_;
598  
599 9 delete $tab->{expiry};
600 9 ▶ return if $tab->{page} || $tab->{navigation};
Branch: 1 branch
BranchTrueFalseTotal
if $tab->{"page"} or $tab->{"navigation"} 2 7 9
Condition66%2/3
$tab->{"page"} or $tab->{"navigation"}
l!l&&r!l&&!r
027
MC/DC50%1/2
$tab->{"page"} or $tab->{"navigation"}
$tab->{"page"} $tab->{"navigation"}
Truth table66%2/3
A or B
A= $tab->{"page"}B= $tab->{"navigation"}
ABresult
1X1
011
000
601  
602 0 ▶ $tab->{expiry} = AE::timer 30, 0, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
603 finish_tab $tab, "inactive for 30s"
604 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
605 7 };
606 }
607  
608 4 ▶sub finish_navigation {
Subroutine finish_navigation: called CC=6 SCAR=18.0
609 4 my ($tab, $kind, $reason) = @_;
610  
611 4 ▶ my $entry = delete $tab->{navigation} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = delete $tab->{"navigation"} or return 4 0 4
612 4 delete $entry->{timer};
613 4 my $req = $entry->{request};
614 4 ▶ return if $req->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $req->{"finished"} 0 4 4
615  
616 4 my $page = $tab->{page};
617 $req->result ({
618 navigation => $kind,
619 revision => $tab->{revision}*1,
620 4 ▶ ($page ? (%{$page->{state}}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
$page ? : 3 1 4
defined $reason ? : 2 2 4
621 (defined $reason ? (reason => $reason) : ()),
622 });
623  
624 4 ▶ expire_tab $tab unless $page;
Branch: 1 branch
BranchTrueFalseTotal
unless $page 1 3 4
625 }
626  
627 10 ▶sub close_page {
Subroutine close_page: called CC=8 SCAR=20.9
628 10 my ($page, $code, $reason) = @_;
629  
630 10 ▶ delete $connections{$page+0} or return;
Branch: 1 branch
BranchTrueFalseTotal
delete $connections{$page + 0} or return 10 0 10
631 10 ▶ 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 8 2 10
Condition66%2/3
defined $page->{"target"} and $pages{$page->{"target"}} == $page
!ll&&!rl&&r
208
MC/DC50%1/2
defined $page->{"target"} and $pages{$page->{"target"}} == $page
defined $page->{"target"} $pages{$page->{"target"}} == $page
Truth table66%2/3
A and B
A= defined $page->{"target"}B= $pages{$page->{"target"}} == $page
ABresult
0X0
100
111
632 10 delete $page->{hello_timer};
633  
634 10 ▶ if (my $tab = $page->{tab}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tab = $page->{"tab"}) 8 2 10
635 8 ▶ delete $tab->{page} if $tab->{page} == $page;
Branch: 1 branch
BranchTrueFalseTotal
if $tab->{"page"} == $page 8 0 8
636 8 expire_tab $tab;
637 }
638  
639 10 finish_call $page, $_, 0, $reason for keys %{$page->{pending}};
640 10 $page->{conn}->close ($code, $reason);
641 10 ▶ if (defined $page->{target}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $page->{'target'}) { } 8 2 10
642 8 mcp_log "$page->{target}: disconnected ($reason)";
643 } else {
644 2 AE::log trace => "Browser WebSocket disconnected before hello ($reason)";
645 }
646 }
647  
648 15 ▶sub valid_state {
Subroutine valid_state: called CC=5 SCAR=21.9
649 15 my ($state) = @_;
650  
651 ref $state eq "HASH" && defined $state->{url} && !ref $state->{url}
652 && defined $state->{title} && !ref $state->{title}
653 15 ▶}
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"} 15 0 15
Condition33%3/9
ref $state eq "HASH" and defined $state->{"url"} and not ref $state->{"url"} and defined $state->{"title"}
!ll&&!rl&&r
0015
ref $state eq "HASH" and defined $state->{"url"} and not ref $state->{"url"}
!ll&&!rl&&r
0015
ref $state eq "HASH" and defined $state->{"url"}
!ll&&!rl&&r
0015
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 table20%1/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
654  
655 37 ▶sub receive_message {
Subroutine receive_message: called CC=69 SCAR=58.2
656 37 my ($page, $packet) = @_;
657  
658 37 ▶ 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" 37 0 37
Condition33%3/9
ref $packet eq "ARRAY" and @$packet and defined $packet->[0] and not ref $packet->[0]
!ll&&!rl&&r
0037
ref $packet eq "ARRAY" and @$packet and defined $packet->[0]
!ll&&!rl&&r
0037
ref $packet eq "ARRAY" and @$packet
!ll&&!rl&&r
0037
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 table20%1/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
659 or die "Invalid browser message\n";
660  
661 37 ▶ unless (defined $page->{target}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $page->{"target"}) 10 27 37
662 10 ▶ @$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" 9 1 10
Condition66%18/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
109
@$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
109
@$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
109
@$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
109
@$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
109
@$packet == 5 and $packet->[0] eq "mcp-webbrowser" and defined $packet->[1] and not ref $packet->[1] and defined $packet->[2]
!ll&&!rl&&r
109
@$packet == 5 and $packet->[0] eq "mcp-webbrowser" and defined $packet->[1] and not ref $packet->[1]
!ll&&!rl&&r
109
@$packet == 5 and $packet->[0] eq "mcp-webbrowser" and defined $packet->[1]
!ll&&!rl&&r
109
@$packet == 5 and $packet->[0] eq "mcp-webbrowser"
!ll&&!rl&&r
109
MC/DC10%1/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 table18%2/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
663 && defined $packet->[1] && !ref $packet->[1]
664 && defined $packet->[2] && !ref $packet->[2]
665 && defined $packet->[3] && !ref $packet->[3] && length $packet->[3]
666 && valid_state ($packet->[4])
667 or die "Invalid browser hello\n";
668 9 ▶ $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" 8 1 9
Condition66%2/3
$packet->[1] eq 1 and $packet->[2] =~ /\A(?:0|[1-9][0-9]*)\z/u
!ll&&!rl&&r
108
MC/DC50%1/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 table66%2/3
A and B
A= $packet->[1] eq 1B= $packet->[2] =~ /\A(?:0|[1-9][0-9]*)\z/u
ABresult
0X0
100
111
669 or die "Unsupported browser protocol $packet->[1].$packet->[2] (server 1.1)\n";
670  
671 8 my $new = !exists $tabs{$page->{requested}};
672 my $tab = $tabs{$page->{requested}} ||= {
673 8 ▶ requested => $page->{requested}, revision => 0,
Condition100%2/2
$tabs{$page->{"requested"}} ||= {"requested", $page->{"requested"}, "revision", 0}
l!l
35
MC/DC100%1/1
$tabs{$page->{"requested"}} ||= {"requested", $page->{"requested"}, "revision", 0}
$tabs{$page->{"requested"}}
Truth table100%2/2
A
A= $tabs{$page->{"requested"}}
Aresult
11
00
674 };
675 8 ▶ my $different = !defined $tab->{instance_id} || $tab->{instance_id} ne $packet->[3];
Condition100%3/3
!defined($tab->{'instance_id'}) || $tab->{'instance_id'} ne $packet->[3]
l!l&&r!l&&!r
512
MC/DC100%2/2
!defined($tab->{'instance_id'}) || $tab->{'instance_id'} ne $packet->[3]
!defined($tab->{'instance_id'}) $tab->{'instance_id'} ne $packet->[3]
Truth table100%3/3
not(A) || B
A= !defined($tab->{'instance_id'})B= $tab->{'instance_id'} ne $packet->[3]
ABresult
1X1
011
000
676 8 ▶ close_page $tab->{page}, 1000, "Page connection replaced" if $tab->{page};
Branch: 1 branch
BranchTrueFalseTotal
if $tab->{"page"} 2 6 8
677 8 delete $tab->{expiry};
678  
679 8 ▶ ++$tab->{revision} if $different;
Branch: 1 branch
BranchTrueFalseTotal
if $different 6 2 8
680 8 $tab->{instance_id} = $packet->[3];
681 @$page{qw(target instance_id minor state tab)} =
682 8 ($page->{requested}, $packet->[3], $packet->[2], browser_state ($packet->[4]), $tab);
683 8 $tab->{page} = $page;
684 8 delete $page->{hello_timer};
685 8 $pages{$page->{target}} = $page;
686 8 send_message $page, ["welcome", $page->{target}, $page->{instance_id}, $tab->{revision}*1];
687  
688 8 ▶ if ($different && $tab->{navigation} && $tab->{navigation}{phase} eq "proceed") {
Branch: 1 branch
BranchTrueFalseTotal
if ($different and $tab->{"navigation"} and $tab->{"navigation"}{"phase"} eq "proceed") 1 7 8
Condition83%5/6
$different and $tab->{"navigation"} and $tab->{"navigation"}{"phase"} eq "proceed"
!ll&&!rl&&r
701
$different and $tab->{"navigation"}
!ll&&!rl&&r
251
MC/DC66%2/3
$different and $tab->{"navigation"} and $tab->{"navigation"}{"phase"} eq "proceed"
$different $tab->{"navigation"} $tab->{"navigation"}{"phase"} eq "proceed"
Truth table75%3/4
A and B and C
A= $differentB= $tab->{"navigation"}C= $tab->{"navigation"}{"phase"} eq "proceed"
ABCresult
0XX0
10X0
1100
1111
689 1 finish_navigation $tab, "document";
690 }
691  
692 8 ▶ mcp_log "$page->{target}: session started (" . url_preview ($page->{state}{url}) . ")" if $new;
Branch: 1 branch
BranchTrueFalseTotal
if $new 5 3 8
693 8 ▶ mcp_log "$page->{target}: connected (revision $tab->{revision})"
Branch: 1 branch
BranchTrueFalseTotal
unless $new 3 5 8
694 unless $new;
695 return
696 8 }
697  
698 27 my $type = $packet->[0];
699  
700 27 ▶ if ($type eq "reply") {
Branch: 5 branches
BranchTrueFalseTotal
if ($type eq 'reply') { } 13 14 27
elsif ($type eq 'ready') { } 4 10 14
elsif ($type eq 'navigated') { } 1 9 10
elsif ($type eq 'state') { } 5 4 9
elsif ($type eq 'quit') { } 4 0 4
701 13 ▶ @$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" 13 0 13
Condition40%11/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
0013
@$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
0013
@$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
0013
@$packet == 4 and defined $packet->[1] and not ref $packet->[1] and $packet->[1] =~ /\A[1-9][0-9]*\z/u
!ll&&!rl&&r
0013
@$packet == 4 and defined $packet->[1] and not ref $packet->[1]
!ll&&!rl&&r
0013
@$packet == 4 and defined $packet->[1]
!ll&&!rl&&r
0013
$packet->[2] eq 0 or $packet->[2] eq 1
l!l&&r!l&&!r
3100
$packet->[2] or defined $packet->[3] and not ref $packet->[3]
l!l&&r!l&&!r
1030
defined $packet->[3] and not ref $packet->[3]
!ll&&!rl&&r
003
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 table14%2/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
702 && defined $packet->[2] && ($packet->[2] eq "0" || $packet->[2] eq "1")
703 && ($packet->[2] || defined $packet->[3] && !ref $packet->[3])
704 or die "Invalid browser reply\n";
705 13 my $entry = $page->{tab}{navigation};
706 13 ▶ 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 13 13
Condition33%2/6
$entry and $entry->{"page"} == $page and $entry->{"id"} == $packet->[1]
!ll&&!rl&&r
1300
$entry and $entry->{"page"} == $page
!ll&&!rl&&r
1300
MC/DC0%0/3
$entry and $entry->{"page"} == $page and $entry->{"id"} == $packet->[1]
$entry $entry->{"page"} == $page $entry->{"id"} == $packet->[1]
Truth table25%1/4
A and B and C
A= $entryB= $entry->{"page"} == $pageC= $entry->{"id"} == $packet->[1]
ABCresult
0XX0
10X0
1100
1111
707 0 ▶ if (!$packet->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $packet->[2]) 0 0 0
708 0 delete $page->{tab}{navigation};
709 0 delete $entry->{timer};
710 0 ▶ $entry->{request}->error ($packet->[3]) unless $entry->{request}{finished};
Branch: 1 branch
BranchTrueFalseTotal
unless $entry->{"request"}{"finished"} 0 0 0
711 }
712 } else {
713 13 finish_call $page, @$packet[1, 2, 3];
714 }
715 } elsif ($type eq "ready") {
716 4 ▶ @$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" 4 0 4
Condition33%3/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
004
@$packet == 2 and defined $packet->[1] and not ref $packet->[1]
!ll&&!rl&&r
004
@$packet == 2 and defined $packet->[1]
!ll&&!rl&&r
004
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 table20%1/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
717 && $packet->[1] =~ /\A[1-9][0-9]*\z/ or die "Invalid navigation acknowledgement\n";
718  
719 4 my $entry = $page->{tab}{navigation};
720 4 ▶ 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") 4 0 4
Condition33%3/9
$entry and $entry->{"page"} == $page and $entry->{"id"} == $packet->[1] and $entry->{"phase"} eq "ready"
!ll&&!rl&&r
004
$entry and $entry->{"page"} == $page and $entry->{"id"} == $packet->[1]
!ll&&!rl&&r
004
$entry and $entry->{"page"} == $page
!ll&&!rl&&r
004
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 table20%1/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
721 && $entry->{phase} eq "ready") {
722 4 $entry->{phase} = "proceed";
723 4 AE::log trace => "webbrowser navigate $page->{target} #$entry->{id}: " . code_preview ($entry->{code});
724 4 send_message $page, ["proceed", $packet->[1]*1];
725 }
726 } elsif ($type eq "navigated") {
727 1 ▶ @$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" 1 0 1
Condition33%4/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
001
@$packet == 3 and defined $packet->[1] and not ref $packet->[1] and $packet->[1] =~ /\A[1-9][0-9]*\z/u
!ll&&!rl&&r
001
@$packet == 3 and defined $packet->[1] and not ref $packet->[1]
!ll&&!rl&&r
001
@$packet == 3 and defined $packet->[1]
!ll&&!rl&&r
001
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 table16%1/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
728 && $packet->[1] =~ /\A[1-9][0-9]*\z/ && valid_state ($packet->[2])
729 or die "Invalid navigation result\n";
730  
731 1 my $entry = $page->{tab}{navigation};
732 1 ▶ 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") 1 0 1
Condition33%3/9
$entry and $entry->{"page"} == $page and $entry->{"id"} == $packet->[1] and $entry->{"phase"} eq "proceed"
!ll&&!rl&&r
001
$entry and $entry->{"page"} == $page and $entry->{"id"} == $packet->[1]
!ll&&!rl&&r
001
$entry and $entry->{"page"} == $page
!ll&&!rl&&r
001
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 table20%1/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
733 && $entry->{phase} eq "proceed") {
734 1 $page->{state} = browser_state ($packet->[2]);
735 1 finish_navigation $page->{tab}, "same-document";
736 }
737 } elsif ($type eq "state") {
738 5 ▶ @$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" 5 0 5
Condition33%1/3
@$packet == 2 and valid_state($packet->[1])
!ll&&!rl&&r
005
MC/DC0%0/2
@$packet == 2 and valid_state($packet->[1])
@$packet == 2 valid_state($packet->[1])
Truth table33%1/3
A and B
A= @$packet == 2B= valid_state($packet->[1])
ABresult
0X0
100
111
739 5 $page->{state} = browser_state ($packet->[1]);
740 } elsif ($type eq "quit") {
741 4 ▶ @$packet == 1 or die "Invalid browser quit\n";
Branch: 1 branch
BranchTrueFalseTotal
@$packet == 1 or die "Invalid browser quit\n" 4 0 4
742 4 close_page $page, 1000, "Page disconnected";
743 finish_tab $page->{tab}, "disabled"
744 4 ▶ if $page->{tab} && !$page->{tab}{navigation};
Branch: 1 branch
BranchTrueFalseTotal
if $page->{"tab"} and not $page->{"tab"}{"navigation"} 4 0 4
Condition33%1/3
$page->{"tab"} and not $page->{"tab"}{"navigation"}
!ll&&!rl&&r
004
MC/DC0%0/2
$page->{"tab"} and not $page->{"tab"}{"navigation"}
$page->{"tab"} not $page->{"tab"}{"navigation"}
Truth table33%1/3
A and not(B)
A= $page->{"tab"}B= not $page->{"tab"}{"navigation"}
ABresult
0X0
100
111
745 } else {
746 0 die "Unknown browser message\n";
747 }
748 }
749  
750 12 ▶sub accept_browser {
Subroutine accept_browser: called CC=1 SCAR=0.1
751 12 my ($fh) = @_;
752  
753 12 my ($requested, $rejection);
754 2 ▶ my $reject = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
755 2 $rejection = shift;
756 2 die "Browser WebSocket handshake rejected\n";
757 12 };
758 my $ws = AnyEvent::WebSocket::Server->new (
759 max_payload_size => 16 * 1024 * 1024,
760 12 ▶ handshake => sub {
Subroutine __ANON__: called CC=7 SCAR=20.5
761 12 my ($req, $res) = @_;
762 12 my ($cookie, $target) = $req->{resource_name} =~ m{\A/([^/]+)/([^/?#]+)\z};
763 12 ▶ defined $cookie or $reject->("invalid path");
Branch: 1 branch
BranchTrueFalseTotal
defined $cookie or &$reject("invalid path") 12 0 12
764 12 ▶ $token{$cookie} or $reject->("invalid token");
Branch: 1 branch
BranchTrueFalseTotal
$token{$cookie} or &$reject("invalid token") 11 1 12
765 11 ▶ $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") 1 10 11
766 0 $target =~ s/%([0-9a-fA-F]{2})/chr hex $1/ge;
767 10 ▶ utf8::decode $target or $reject->("invalid target");
Branch: 1 branch
BranchTrueFalseTotal
utf8::decode($target) or &$reject("invalid target") 10 0 10
768 10 ▶ length $target && $target !~ /\0/ or $reject->("invalid target");
Branch: 1 branch
BranchTrueFalseTotal
length $target and not $target =~ /\0/u or &$reject("invalid target") 10 0 10
Condition33%1/3
length $target and not $target =~ /\0/u
!ll&&!rl&&r
0010
MC/DC0%0/2
length $target and not $target =~ /\0/u
length $target not $target =~ /\0/u
Truth table33%1/3
A and not(B)
A= length $targetB= not $target =~ /\0/u
ABresult
0X0
100
111
769 10 $requested = $target;
770  
771 10 $res
772 },
773 12 );
774 12 ▶ $ws->establish ($fh)->cb (sub {
Subroutine __ANON__: called CC=4 SCAR=14.5
775 12 my $conn = eval { $_[0]->recv };
776 12 ▶ unless ($conn) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conn) 2 10 12
777 2 ▶ AE::log warn => $rejection
Branch: 1 branch
BranchTrueFalseTotal
$rejection ? : 2 0 2
Condition0%0/2
$@ || 'no connection'
l!l
00
MC/DC0%0/1
$@ || 'no connection'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
778 ? "Browser WebSocket rejected: $rejection"
779 : "Browser WebSocket establishment failed: " . ($@ || "no connection");
780 2 return;
781 }
782  
783 10 my $page = { conn => $conn, requested => $requested, pending => {}, rpcid => 0 };
784 10 $connections{$page+0} = $page;
785 10 weaken (my $weak = $page);
786 10 ▶ $conn->on (finish => sub { close_page $weak, 1000, "Page disconnected" if $weak });
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if $weak 2 0 2
787 37 ▶ $conn->on (each_message => sub {
Subroutine __ANON__: called CC=5 SCAR=16.5
788 37 ▶ return unless $weak && exists $connections{$weak+0};
Branch: 1 branch
BranchTrueFalseTotal
unless $weak and exists $connections{$weak + 0} 0 37 37
Condition33%1/3
$weak and exists $connections{$weak + 0}
!ll&&!rl&&r
0037
MC/DC0%0/2
$weak and exists $connections{$weak + 0}
$weak exists $connections{$weak + 0}
Truth table33%1/3
A and B
A= $weakB= exists $connections{$weak + 0}
ABresult
0X0
100
111
789 37 my $ok = eval {
790 37 ▶ $_[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" 37 0 37
791 37 receive_message $weak, $json->decode ($_[1]->body);
792 35 1
793 };
794 37 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 2 35 37
795 2 my $reason = $@;
796 2 AE::log warn => "Browser protocol error: $reason";
797 2 $reason =~ s/\n.*//s;
798 2 $reason =~ s/[^\x20-\x7e]/?/g;
799 2 close_page $weak, 1002, substr $reason, 0, 123;
800 }
801 10 });
802 0 ▶ $page->{hello_timer} = AE::timer 15, 0, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
803 0 ▶ close_page $weak, 1002, "Browser hello timed out" if $weak;
Branch: 1 branch
BranchTrueFalseTotal
if $weak 0 0 0
804 10 };
805 12 });
806 }
807  
808 3 my $toolset = AnyEvent::MCP::Server::Toolset->new;
809 $toolset->tool (tabs => "List connected tabs; metadata client-reported.",
810 { type => "object", properties => {}, additionalProperties => JSON::XS::false },
811 23 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
812 23 my ($req) = @_;
813 23 permission $req, "read";
814 23 AE::log debug => "webbrowser tabs";
815 $req->result ({ tabs => [map {
816 23 my $page = $pages{$_};
817 37 +{ %{$page->{state}}, target => $_, instance_id => $page->{instance_id},
818 37 revision => $page->{tab}{revision}*1 }
819 } sort keys %pages] });
820 },
821 3 );
822 $toolset->tool (execute => "Page JS: CODE becomes body of async () => CODE; supply CODE, not arrow-function wrapper. "
823 . "Example: ({title: document.title}); NOT async () => ({title: document.title}) (returns function, rejected). "
824 . "Single expression returns implicitly; { ... } body needs explicit return; await works. "
825 . "UTF-8 JSON reply max_bytes=16384 (16 KiB) default, max 2097152 (2 MiB). Oversize errors after execution; no truncation. "
826 . "navigate=true: acknowledge before execution, "
827 . "then wait for new document or observed same-document navigation. "
828 . "Revision/instance guards reject replaced documents.",
829 {
830 type => "object",
831 properties => {
832 target => { type => "string", minLength => 1 },
833 code => { type => "string", description => "Expression or { ... } body for async () => CODE. Do not include async () => wrapper." },
834 instance_id => { type => "string", minLength => 1 },
835 revision => { type => "integer", minimum => 1 },
836 navigate => { type => "boolean", default => JSON::XS::false },
837 max_bytes => { type => "integer", minimum => 1024, maximum => 2 * 1024 * 1024, default => 16 * 1024 },
838 timeout => { type => "number", minimum => 1, maximum => 300, default => 30 },
839 },
840 required => ["target", "code"], additionalProperties => JSON::XS::false,
841 },
842 16 ▶ sub {
Subroutine __ANON__: called CC=15 SCAR=34.3
843 16 my ($req, $args) = @_;
844  
845 16 ▶ 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" 16 0 16
846 !defined $args->{instance_id} || $args->{instance_id} eq $page->{instance_id}
847 16 ▶ 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" 15 1 16
Condition100%3/3
not defined $args->{"instance_id"} or $args->{"instance_id"} eq $page->{"instance_id"}
l!l&&r!l&&!r
1411
MC/DC100%2/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 table100%3/3
not(A) or B
A= not defined $args->{"instance_id"}B= $args->{"instance_id"} eq $page->{"instance_id"}
ABresult
1X1
011
000
848 !defined $args->{revision} || $args->{revision} == $page->{tab}{revision}
849 15 ▶ 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] 14 1 15
Condition100%3/3
not defined $args->{"revision"} or $args->{"revision"} == $page->{"tab"}{"revision"}
l!l&&r!l&&!r
1311
MC/DC100%2/2
not defined $args->{"revision"} or $args->{"revision"} == $page->{"tab"}{"revision"}
not defined $args->{"revision"} $args->{"revision"} == $page->{"tab"}{"revision"}
Truth table100%3/3
not(A) or B
A= not defined $args->{"revision"}B= $args->{"revision"} == $page->{"tab"}{"revision"}
ABresult
1X1
011
000
850 14 ▶ !$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 14 14
851 14 permission $req, exec => $args->{target}, $args->{code};
852  
853 AE::log debug => "webbrowser " . ($args->{navigate} ? "navigate " : "execute ")
854 . $json->encode ($args->{target}) . " " . url_preview ($page->{state}{url})
855 12 ▶ . " " . code_preview ($args->{code}, 20);
Branch: 1 branch
BranchTrueFalseTotal
$args->{'navigate'} ? : 4 8 12
856  
857 12 ▶ $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" 12 0 12
Condition33%1/3
$pages{$args->{"target"}} and $pages{$args->{"target"}} == $page
!ll&&!rl&&r
0012
MC/DC0%0/2
$pages{$args->{"target"}} and $pages{$args->{"target"}} == $page
$pages{$args->{"target"}} $pages{$args->{"target"}} == $page
Truth table33%1/3
A and B
A= $pages{$args->{"target"}}B= $pages{$args->{"target"}} == $page
ABresult
0X0
100
111
858 or die "Browser document changed while permission was pending\n";
859  
860 12 my $id = ++$page->{rpcid};
861 12 ▶ if ($args->{navigate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"navigate"}) 4 8 12
862 4 my $tab = $page->{tab};
863 my $entry = $tab->{navigation} = {
864 request => $req, page => $page, id => $id, phase => "ready", code => $args->{code},
865 4 };
866 2 ▶ $entry->{timer} = AE::timer $args->{timeout} // 30, 0, sub {
Subroutine __ANON__: called CC=10 SCAR=35.2
867 2 ▶ return unless $tab->{navigation} && $tab->{navigation} == $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless $tab->{"navigation"} and $tab->{"navigation"} == $entry 0 2 2
Condition33%1/3
$tab->{"navigation"} and $tab->{"navigation"} == $entry
!ll&&!rl&&r
002
MC/DC0%0/2
$tab->{"navigation"} and $tab->{"navigation"} == $entry
$tab->{"navigation"} $tab->{"navigation"} == $entry
Truth table33%1/3
A and B
A= $tab->{"navigation"}B= $tab->{"navigation"} == $entry
ABresult
0X0
100
111
868 2 ▶ if ($entry->{phase} eq "proceed") {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{'phase'} eq 'proceed') { } 2 0 2
869 2 ▶ 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"} 1 1 2
Condition66%2/3
$tab->{"page"} and $tab->{"page"} == $entry->{"page"}
!ll&&!rl&&r
101
MC/DC50%1/2
$tab->{"page"} and $tab->{"page"} == $entry->{"page"}
$tab->{"page"} $tab->{"page"} == $entry->{"page"}
Truth table66%2/3
A and B
A= $tab->{"page"}B= $tab->{"page"} == $entry->{"page"}
ABresult
0X0
100
111
870 2 finish_navigation $tab, "timeout", "No completed navigation was observed before timeout";
871 } else {
872 0 delete $tab->{navigation};
873 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
874 0 ▶ $req->error ("Browser did not acknowledge navigation") unless $req->{finished};
Branch: 1 branch
BranchTrueFalseTotal
unless $req->{"finished"} 0 0 0
875 0 ▶ expire_tab $tab unless $tab->{page};
Branch: 1 branch
BranchTrueFalseTotal
unless $tab->{"page"} 0 0 0
876 }
877 4 ▶ };
Condition100%2/2
$args->{"timeout"} // 30
l!l
22
MC/DC100%1/1
$args->{"timeout"} // 30
$args->{"timeout"}
Truth table100%2/2
A
A= $args->{"timeout"}
Aresult
11
00
878 0 ▶ $req->{on_cancel} = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
879 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
880 0 delete $tab->{navigation};
881 0 delete $entry->{timer};
882 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
883 0 ▶ expire_tab $tab unless $tab->{page};
Branch: 1 branch
BranchTrueFalseTotal
unless $tab->{"page"} 0 0 0
884 4 };
885 send_message $page, ["call", $id*1, "navigate", {
886 code => $args->{code},
887 4 ▶ max_bytes => $args->{max_bytes} // 16 * 1024,
Condition50%1/2
$args->{"max_bytes"} // 16384
l!l
04
MC/DC0%0/1
$args->{"max_bytes"} // 16384
$args->{"max_bytes"}
Truth table50%1/2
A
A= $args->{"max_bytes"}
Aresult
11
00
888 }];
889 4 return;
890 }
891  
892 8 my $entry = $page->{pending}{$id} = { request => $req };
893 8 weaken (my $weak = $page);
894 1 ▶ $entry->{timer} = AE::timer $args->{timeout} // 30, 0, sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
895 1 ▶ return unless $weak && exists $weak->{pending}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $weak and exists $weak->{"pending"}{$id} 0 1 1
Condition33%1/3
$weak and exists $weak->{"pending"}{$id}
!ll&&!rl&&r
001
MC/DC0%0/2
$weak and exists $weak->{"pending"}{$id}
$weak exists $weak->{"pending"}{$id}
Truth table33%1/3
A and B
A= $weakB= exists $weak->{"pending"}{$id}
ABresult
0X0
100
111
896 1 send_message $weak, ["cancel", $id*1];
897 1 finish_call $weak, $id, 0, "Browser execution timed out";
898 8 ▶ };
Condition100%2/2
$args->{"timeout"} // 30
l!l
17
MC/DC100%1/1
$args->{"timeout"} // 30
$args->{"timeout"}
Truth table100%2/2
A
A= $args->{"timeout"}
Aresult
11
00
899 1 ▶ $req->{on_cancel} = sub {
Subroutine __ANON__: called CC=3 SCAR=13.1
900 1 ▶ return unless $weak && delete $weak->{pending}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $weak and delete $weak->{"pending"}{$id} 0 1 1
Condition33%1/3
$weak and delete $weak->{"pending"}{$id}
!ll&&!rl&&r
001
MC/DC0%0/2
$weak and delete $weak->{"pending"}{$id}
$weak delete $weak->{"pending"}{$id}
Truth table33%1/3
A and B
A= $weakB= delete $weak->{"pending"}{$id}
ABresult
0X0
100
111
901 1 send_message $weak, ["cancel", $id*1];
902 8 };
903 8 AE::log trace => "webbrowser execute $args->{target} #$id: " . code_preview ($args->{code});
904 send_message $page, ["call", $id*1, "execute", {
905 code => $args->{code},
906 8 ▶ max_bytes => $args->{max_bytes} // 16 * 1024,
Condition100%2/2
$args->{"max_bytes"} // 16384
l!l
17
MC/DC100%1/1
$args->{"max_bytes"} // 16384
$args->{"max_bytes"}
Truth table100%2/2
A
A= $args->{"max_bytes"}
Aresult
11
00
907 }];
908 },
909 3 );
910 $toolset->tool (add_element => "Insert DOM element via userscript GM_addElement; style/script bypass page CSP. "
911 . "Set HTML attributes via attributes; DOM properties (textContent/innerHTML) via properties. "
912 . "parent: CSS selector; default document.documentElement. "
913 . "Set distinctive id or data-* to find/edit/remove later via execute. "
914 . "No automatic ID/registry.",
915 {
916 type => "object",
917 properties => {
918 target => { type => "string", minLength => 1 },
919 tag => { type => "string", minLength => 1 },
920 parent => { type => "string", minLength => 1 },
921 attributes => { type => "object", additionalProperties => { type => "string" } },
922 properties => { type => "object", additionalProperties => JSON::XS::true },
923 instance_id => { type => "string", minLength => 1 },
924 revision => { type => "integer", minimum => 1 },
925 },
926 required => ["target", "tag"], additionalProperties => JSON::XS::false,
927 },
928 4 ▶ sub {
Subroutine __ANON__: called CC=10 SCAR=30.2
929 4 my ($req, $args) = @_;
930  
931 4 ▶ 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" 4 0 4
932 !defined $args->{instance_id} || $args->{instance_id} eq $page->{instance_id}
933 4 ▶ 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" 4 0 4
Condition66%2/3
not defined $args->{"instance_id"} or $args->{"instance_id"} eq $page->{"instance_id"}
l!l&&r!l&&!r
310
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 table66%2/3
not(A) or B
A= not defined $args->{"instance_id"}B= $args->{"instance_id"} eq $page->{"instance_id"}
ABresult
1X1
011
000
934 !defined $args->{revision} || $args->{revision} == $page->{tab}{revision}
935 4 ▶ 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] 4 0 4
Condition66%2/3
not defined $args->{"revision"} or $args->{"revision"} == $page->{"tab"}{"revision"}
l!l&&r!l&&!r
310
MC/DC0%0/2
not defined $args->{"revision"} or $args->{"revision"} == $page->{"tab"}{"revision"}
not defined $args->{"revision"} $args->{"revision"} == $page->{"tab"}{"revision"}
Truth table66%2/3
not(A) or B
A= not defined $args->{"revision"}B= $args->{"revision"} == $page->{"tab"}{"revision"}
ABresult
1X1
011
000
936 4 ▶ !$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 4 4
937 4 permission $req, exec => $args->{target}, "GM_addElement " . $json->encode ($args);
938 2 ▶ $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" 2 0 2
Condition33%1/3
$pages{$args->{"target"}} and $pages{$args->{"target"}} == $page
!ll&&!rl&&r
002
MC/DC0%0/2
$pages{$args->{"target"}} and $pages{$args->{"target"}} == $page
$pages{$args->{"target"}} $pages{$args->{"target"}} == $page
Truth table33%1/3
A and B
A= $pages{$args->{"target"}}B= $pages{$args->{"target"}} == $page
ABresult
0X0
100
111
939 or die "Browser document changed while permission was pending\n";
940  
941 2 my $id = ++$page->{rpcid};
942 2 my $entry = $page->{pending}{$id} = { request => $req };
943 2 weaken (my $weak = $page);
944 0 ▶ $entry->{timer} = AE::timer 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 add_element timed out";
948 2 };
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 2 };
953 2 AE::log debug => "webbrowser add_element $args->{target}: " . $json->encode ($args->{tag});
954 send_message $page, ["call", $id*1, "add_element", {
955 2 ▶ map { exists $args->{$_} ? ($_ => $args->{$_}) : () } qw(tag parent attributes properties)
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{$_} ? : 5 3 8
956 }];
957 },
958 3 );
959  
960 $toolset->tool (list_tab_operations =>
961 "List additional operations offered by a connected tab. Returns {operations: [...]} with descriptions and argument schemas; empty if none. Requires read permission.",
962 {
963 type => "object",
964 properties => {
965 target => { type => "string", minLength => 1 },
966 instance_id => { type => "string", minLength => 1 },
967 revision => { type => "integer", minimum => 1 },
968 timeout => { type => "number", minimum => 1, maximum => 300, default => 30 },
969 max_bytes => { type => "integer", minimum => 1024, maximum => 2 * 1024 * 1024, default => 16 * 1024 },
970 },
971 required => ["target"], additionalProperties => JSON::XS::false,
972 },
973 6 ▶ sub { tab_operation $_[0], $_[1], "list_tab_operations" },
Subroutine __ANON__: called CC=1 SCAR=0.0
974 3 );
975 $toolset->tool (call_tab_operation =>
976 "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.",
977 {
978 type => "object",
979 properties => {
980 target => { type => "string", minLength => 1 },
981 operation => { type => "string", minLength => 1 },
982 arguments => { type => "object", default => {} },
983 instance_id => { type => "string", minLength => 1 },
984 revision => { type => "integer", minimum => 1 },
985 timeout => { type => "number", minimum => 1, maximum => 300, default => 30 },
986 max_bytes => { type => "integer", minimum => 1024, maximum => 2 * 1024 * 1024, default => 16 * 1024 },
987 },
988 required => ["target", "operation"], additionalProperties => JSON::XS::false,
989 },
990 8 ▶ sub { tab_operation $_[0], $_[1], "call_tab_operation" },
Subroutine __ANON__: called CC=1 SCAR=0.0
991 3 );
992  
993 3 mcp_tool_help ($options->{tool_help}, $toolset);
994  
995 my $custom_instructions = $options->{custom_instructions}
996 // $config->{section}{"instance $name"}{custom_instructions}
997 2 ▶ // $config->{section}{"program " . AnyEvent::MCP::Config::program_name ()}{custom_instructions};
Condition33%2/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
002
$options->{"custom_instructions"} // $config->{"section"}{"instance $name"}{"custom_instructions"}
l!l&&r!l&&!r
002
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 table25%1/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
998 my %server = (
999 server_info => { name => "mcp-webbrowser", version => "0" },
1000 auto_instructions => 1,
1001 short_instructions => "Inspect connected tabs; run page JavaScript; discover optional per-tab operations. Discover tools.",
1002 (defined $custom_instructions ? (custom_instructions => $custom_instructions) : ()),
1003 instructions => "Tabs have stable targets/document revisions. "
1004 . "Use tabs for current revision. execute runs async () => CODE in page; "
1005 . "expression returns implicitly; { ... } body needs return; parenthesize object literals. "
1006 . "navigate=true acknowledges before evaluation; waits for new document or SPA navigation. "
1007 . "Navigation result: document, same-document, timeout. Execution may have happened even on timeout; never retried. "
1008 . "Guard later calls with revision; metadata client-reported. "
1009 . "list-tab-operations discovers browser-provided operations for one tab; "
1010 . "call-tab-operation invokes one by name and JSON arguments.",
1011 toolsets => [$toolset], permissions => $permissions,
1012 on_request => \&mcp_log_request,
1013 13 ▶ on_error => sub { AE::log info => "mcp-tool $_[1]{tool}: $_[2]" },
Subroutine __ANON__: called CC=1 SCAR=0.0
1014 2 ▶);
Branch: 1 branch
BranchTrueFalseTotal
defined $custom_instructions ? : 0 2 2
1015 2 my $http = AnyEvent::MCP::Server::HTTP->new (%server, %http, path => "/");
1016 2 my $stream;
1017  
1018 2 ▶if (defined $connect) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $connect) { } 1 1 2
elsif ($stdio) { } 1 0 1
1019 1 require AnyEvent::MCP::Server::Connect;
1020 $stream = AnyEvent::MCP::Server::Connect->new (
1021 1 ▶ %server, connect => $connect, on_close => sub { $done->send },
Subroutine __ANON__: called CC=1 SCAR=0.0
1022 1 );
1023 1 $stream->start;
1024 } elsif ($stdio) {
1025 1 binmode STDIN;
1026 1 binmode STDOUT;
1027 $stream = AnyEvent::MCP::Server::JSONL->new (
1028 1 ▶ %server, on_connection_close => sub { $done->send },
Subroutine __ANON__: called CC=1 SCAR=0.0
1029 1 );
1030 1 $stream->accept (\*STDIN, \*STDOUT);
1031 }
1032  
1033 14 ▶my $listener = AnyEvent::Socket::tcp_server $http{host}, $http{port}, sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
1034 14 ▶ my ($fh) = @_ or return;
Branch: 1 branch
BranchTrueFalseTotal
my($fh) = or return 14 0 14
1035 14 ▶ my $watcher; $watcher = AE::io $fh, 0, sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1036 14 undef $watcher;
1037 14 ▶ defined recv $fh, my $magic, 1, Socket::MSG_PEEK or return;
Branch: 1 branch
BranchTrueFalseTotal
defined recv($fh, my $magic, 1, 2) or return 14 0 14
1038 14 ▶ if ($magic eq "G") {
Branch: 1 branch
BranchTrueFalseTotal
if ($magic eq 'G') { } 12 2 14
1039 12 accept_browser $fh;
1040 } else {
1041 2 $http->accept ($fh);
1042 }
1043 14 };
1044 2 };
1045 2 ▶my @signals = map { AE::signal $_, sub { $done->send } } qw(INT TERM);
Subroutine __ANON__: called CC=1 SCAR=0.0
1046 2 $done->recv;
1047 2 undef $listener;
1048 2 close_page $_, 1000, "Browser broker stopped" for values %connections;
1049 2 finish_tab $_, "broker stopped" for values %tabs;
1050 2 ▶$stream->close if $stream;
Branch: 1 branch
BranchTrueFalseTotal
if $stream 2 0 2
1051 2 $http->close;
« blib/lib/AnyEvent/MCP/Server/Simple.pm ↑ Summary lib/AnyEvent/MCP/Worker/IMAP.pm »