Commit Graph

694 Commits

Author SHA1 Message Date
Nikolay Govorov
e5a9f8ba2e Fix ony more crash 2026-05-04 18:12:47 +01:00
Nikolay Govorov
9a312a4177 Refactor server/client/cdp structure 2026-05-04 16:41:22 +01:00
Pierre Tachoire
080e1e6415 cdp: rename Audit into Audits 2026-05-04 12:42:55 +02:00
Pierre Tachoire
e3eb8eba46 typo fix 2026-05-04 08:59:54 +02:00
Pierre Tachoire
cddabe60f5 cdp: avoid request id conflict between LID- and REQ-
Use distinct key for laoder id and request id based captured response.
2026-05-04 08:59:53 +02:00
Pierre Tachoire
11172a341a cdp: use loader_id as captured response key for documents 2026-05-04 08:59:50 +02:00
Pierre Tachoire
84246c3b57 Get the pending frame from the Runner
We want to take in account the pending frame in Runner._tick to continue
to process. If we use only the current frame, we will immediately return
in case of navigation.
2026-05-04 08:59:49 +02:00
Pierre Tachoire
c251f0c03b cdp: remove replacePage and use Session.initiateRootNavigation 2026-05-04 08:59:48 +02:00
Pierre Tachoire
f7ac258b8c dispatch frame_remove and new_frame events from sesion.replacePage
So the CDP can remove/reset context and re-create isolated world
accordingly
2026-05-04 08:59:47 +02:00
Pierre Tachoire
acdddb7ec8 keep the existing page active until the pending one is loaded
During a root navigation, we keep the existing page active until we get
the headers callback from the pending page. Then
Session.commitPendingPage makes the switch.

It delays the deinit of CPD execution context to handle JS execution in
the meantime.

Now session has an array of two pages, _active_idx points to the main
page.

Both active and pending pages share the same frame_id, it must remains
stable. So this PR adds a Request.protect_from_abort to avoid removing
the request form the pending page when deinit the previous active page.
2026-05-04 08:50:26 +02:00
Karl Seguin
e42acc5335 Merge pull request #2322 from navidemad/fix-a27-prompt-default-text
cdp: fall back to dialog defaultText when LP.handleJavaScriptDialog promptText is null
2026-04-30 12:12:33 +08:00
Halil Durak
0c3d5573f0 Cookie: don't allow JS context to mutate HttpOnly cookies
Changes function signature for `Jar.add` in order to do this, not sure if we should have separate functions for that or comptime-if sufficient.
2026-04-29 17:40:48 +03:00
Navid EMAD
71af170658 cdp: fall back to dialog defaultText when LP.handleJavaScriptDialog promptText is null
When a CDP client pre-arms `LP.handleJavaScriptDialog {accept: true}` with
no `promptText` and the page subsequently calls `window.prompt(message,
defaultText)`, Lightpanda discarded the dialog's `defaultText` argument
and returned `""`. Chrome's behavior is to surface `defaultText` as the
prompt's return value — that's the natural "user accepted without typing
anything" outcome per the HTML simple-dialog algorithm.

The fix is one line in `Window.zig`'s `prompt` JS binding: keep the
second argument named (`default_text` instead of `_`) and use it as the
fallback. Pre-armed `promptText` still wins when the client supplies it;
`accept = false` still returns null regardless. When neither
`promptText` nor `defaultText` is provided, the binding still returns
`""` per the CDP spec.

Adds four new assertions to the existing `cdp.lp` integration test
covering the matrix: defaultText fallback (no promptText), promptText
overrides defaultText, accept=false ignores defaultText, and the
existing no-defaultText case continues to return `""`.
2026-04-29 15:01:18 +02:00
Adrià Arrufat
92db1b4a5e Merge pull request #2294 from navidemad/fix-a24-ua-stylesheet-display-none
css: apply UA stylesheet display:none defaults for unrendered elements
2026-04-29 08:51:53 +02:00
Navid EMAD
7dccf59ec2 zig fmt linting 2026-04-29 03:45:09 +02:00
Navid EMAD
8cc82d1d64 lp: move handleJavaScriptDialog pre-arm from Page to LP namespace
Per upstream review (#2261): Page.handleJavaScriptDialog is a standard
CDP method that reactive Chrome-style drivers send in response to a
Page.javascriptDialogOpening event. Repurposing it for the proactive
pre-arm flow risks surprising those drivers — the next dialog they
trigger would be blindly accepted.

Move the pre-arm handler to LP.handleJavaScriptDialog so only
Lightpanda-aware clients opt in. Page.handleJavaScriptDialog reverts
to the upstream stub returning -32000 "No dialog is showing" so the
reactive surface is unchanged. The Page.javascriptDialogOpening event
listener still pops the BrowserContext stash and fills the response
output param — only the setter moved.

Tests rename cdp.page: → cdp.lp: and target LP.handleJavaScriptDialog;
behavior coverage is identical.
2026-04-29 02:04:20 +02:00
Navid EMAD
fd2f26a065 Merge remote-tracking branch 'origin/main' into fix-a3-handle-javascript-dialog 2026-04-29 00:57:03 +02:00
Navid EMAD
7c190d9e69 Merge remote-tracking branch 'origin/main' into fix-a24-ua-stylesheet-display-none 2026-04-29 00:47:56 +02:00
Muki Kiboigo
1057b9de8d toRequestId2 -> toRequestId on CDP 2026-04-28 07:01:43 -07:00
Muki Kiboigo
85a5c0f927 decrement intercepted and properly deinit on BrowserContext deinit 2026-04-28 07:01:43 -07:00
Muki Kiboigo
bb9e238f6c Requests now use arenas from the arena pool 2026-04-28 07:01:41 -07:00
Muki Kiboigo
3db3281e8e working authentication with InterceptionLayer 2026-04-28 07:01:40 -07:00
Muki Kiboigo
d0b421b085 partial auth challenge support 2026-04-28 07:01:40 -07:00
Muki Kiboigo
0d50f706db more fixing of hanging in cdp interception 2026-04-28 07:01:40 -07:00
Muki Kiboigo
9c826159a0 crude InterceptionLayer 2026-04-28 07:01:40 -07:00
Muki Kiboigo
6d41ea6fd0 move arena up to Request instead of Transfer 2026-04-28 07:01:39 -07:00
Muki Kiboigo
14ad5c9cdc move RequestStart to InterceptionLayer 2026-04-28 07:01:39 -07:00
Muki Kiboigo
46d0b34c54 add RequestParams and SyncRequest 2026-04-28 07:01:39 -07:00
Navid EMAD
0a4c2a2743 css: apply UA stylesheet display:none defaults for unrendered elements
`StyleManager.hasDisplayNone` honored only the `[hidden]` UA-stylesheet
rule; the rest of HTML Rendering §15.3.1 ("Hidden elements") was
unimplemented. As a result `getComputedStyle(headEl).display === "block"`
and `el.checkVisibility()` returned `true` for `<head>`, `<script>`,
`<style>`, `<link>`, `<meta>`, `<title>`, `<noscript>`, `<template>`,
`<param>`, `<source>`, `<track>`, `<area>`, `<datalist>`,
`<input type="hidden">`, and the non-`<summary>` direct children of a
closed `<details>`.

Add a `matchesUaDisplayNoneRule` helper consulted at the top of
`isElementHidden` so both `getComputedStyle().display` (via
`hasDisplayNone`) and `el.checkVisibility()` (via `isHidden`'s ancestor
walker) honor the same UA-stylesheet truth. The helper covers the
`[hidden]` attribute, a new `Tag.isHiddenByUaStylesheet()` predicate
mapping the spec's unrendered tag list, the
`input[type="hidden" i]` rule, and the `details:not([open]) > *:not(summary)`
parent-relationship rule. Inline `display` overrides still win per CSS
cascade.

Closes #2293
2026-04-28 06:31:14 +02:00
Navid EMAD
7237c377d3 browser: send Referer on cross-page navigation requests
Anchor click, form submit, and `location.href = ...` assignments queue a
navigation through `Frame.scheduleNavigation`, which then tears down the
originating page and rebuilds the frame in `Session.processRootQueuedNavigation`
before `Frame.navigate` issues the HTTP request. The originator's URL was
discarded with the old arena, so the request went out without a Referer
header — even though the HTML "navigate" algorithm and Fetch §4.5 require
one. `Frame.headersForRequest` (#1449) handled subresource fetches but was
never called from the navigation path.

Capture the originating frame's URL into a new `referer` field on
`NavigateOpts` at scheduling time, dup'd into the `QueuedNavigation` arena
so it survives the page tear-down. `Frame.navigate` adds it as a
`Referer:` header alongside the existing per-request headers. Iframe
initial navigation (`Frame.zig:1282`) also sets `referer = parent.url`
since the parent frame outlives that direct `navigate` call. CDP
`Page.navigate` (`.reason = .address_bar`) and `Page.reload` continue to
omit Referer — matches Chrome.

Closes #2281
2026-04-28 03:21:12 +02:00
Navid EMAD
1b9e8ad46c page: drop POST method/body on redirect so reload doesn't replay it
If a POST navigation gets redirected (302/303), the page that actually
loads is fetched with GET — but Frame._navigated_options still carries
the original POST method, body, and Content-Type. Page.reload would
then re-POST the form data to the redirect target, which is both
incorrect and dangerous (re-submission of credentials, charges, etc.).

Reset method/body/header on _navigated_options inside
frameHeaderDoneCallback whenever response.redirectCount() > 0. The full
spec-correct version would distinguish 307/308 (preserve method) from
301/302/303 (convert POST→GET), but resubmitting form data is the more
dangerous failure mode — conservative reset matches Chrome's practical
behavior on reload.

Also collapse the prev_body/prev_header extraction in doReload to a
single tuple-destructured blk: block (no behavior change).

Tests: new cdp.frame: reload after POST→redirect drops the POST drives
POST /redirect_to_echo → 302 → /echo_method, then Page.reload, asserts
the second request is GET. /redirect_to_echo route added to
testing.zig. The existing reload-replays-POST test still passes (no
redirect, POST is still replayed).
2026-04-27 22:47:55 +02:00
Navid EMAD
5b1452f162 Merge remote-tracking branch 'origin/main' into fix-a6-page-reload-replay-post
# Conflicts:
#	src/cdp/domains/page.zig
2026-04-27 22:39:54 +02:00
Navid EMAD
00c42dec4e http: inherit request URL fragment across fragment-less redirect
Per RFC 7231 §7.1.2, when a 3xx response carries a Location header
without a fragment, the user agent must process the redirect as if
the value inherited the fragment of the request URL. URL.resolve
follows RFC 3986 §5.3 which drops the base fragment, so handleRedirect
now reattaches the original fragment when the resolved target has none.

Closes #2263
2026-04-27 08:04:47 +02:00
Navid EMAD
1d806475c4 page: make handleJavaScriptDialog drive confirm/prompt return values
Page.handleJavaScriptDialog previously responded -32000 "No dialog is
showing" regardless of whether a dialog was open, leaving CDP clients
no way to influence the JS-side return value of confirm() / prompt().
PR #2085 wired up the Page.javascriptDialogOpening event but explicitly
deferred the return-value override since true Chrome semantics require
suspending V8 mid-execution.

Add a pre-arm model that fits the auto-dismiss architecture without
runtime suspension: handleJavaScriptDialog stashes {accept, promptText}
on the BrowserContext; when the next JS dialog dispatches the
javascript_dialog_opening notification, the listener pops the stash and
fills it into the dispatch's response output param so Window.confirm /
prompt return the CDP client's choice. Without a pre-arm, headless
auto-dismiss values from PR #2085 are preserved (confirm->false,
prompt->null, alert->void).

Closes #2260
2026-04-27 07:08:01 +02:00
Navid EMAD
ea6b228f9d page: replay POST method/body/header on Page.reload
doReload built a NavigateOpts with only url + kind=.reload; method/body/header
defaulted to GET/null/null, so any prior POST navigation regressed to a GET
on reload. The HTML reload navigation re-fetches the document that produced
the current entry, and Chrome replays the same HTTP request that loaded the
page (including method, body, and Content-Type) — Lightpanda dropped all
three.

Retain the prior request body and content-type header in Frame.NavigatedOpts
(duped into the frame arena), and have doReload capture them into the CDP
command's arena before replacePage() frees the old frame. The reload's
frame.navigate call carries the replayed method/body/header so the request
the page was loaded with is the request that runs again.

Closes #2258
2026-04-27 06:28:58 +02:00
Navid EMAD
53b41966fd network: send empty params test cases as raw JSON
The empty Zig anonymous struct `.{}` serializes to `[]` (tuple → JSON
array), not `{}`. The dispatch path's `InputParams.jsonParse` requires
the params field to begin with `object_begin`, so the previous test
fixtures hit `error.UnexpectedToken` → `error.InvalidJSON` instead of
exercising the production code paths.

Switch the two empty-params test cases to raw JSON string literals,
which the testing helper passes through unchanged (string literals are
pointer types and skip `std.json.Stringify.valueAlloc`). The production
code paths under test are unchanged.
2026-04-27 04:56:57 +02:00
Navid EMAD
5fba50a8d0 network: accept empty params on clearBrowserCookies, add getAllCookies
`Network.clearBrowserCookies` had an inverted-logic guard that returned
`InvalidParams` whenever the caller included a `params: {}` field — which
most CDP libraries (chrome-remote-interface, chromedp, etc.) do
unconditionally. The CDP spec defines no parameters for the method, but
JSON-RPC convention is to silently accept extra ones; Chrome and the
sibling `Storage.clearCookies` handler already do. Drop the guard.

`Network.getAllCookies` was missing from the `Network` dispatch enum and
returned `UnknownMethod`. Add a small handler that returns the entire
cookie jar via the existing `CdpStorage.CookieWriter`, mirroring
`Storage.getCookies` minus the `browserContextId` filter (Network
commands are scoped to the current browser context already).

Closes #2254
2026-04-27 04:10:20 +02:00
Karl Seguin
8509b112b8 Various small fixes
Extracted from https://github.com/lightpanda-io/browser/pull/2242
2026-04-25 13:22:41 +08:00
Nikolay Govorov
c7d004fefb Setup timeout via tcp keepalive 2026-04-24 12:40:21 +01:00
Pierre Tachoire
f63b8ae004 cdp: AXNodeId is a string per spec
AXNodeId is defined as string in CDP spec.
This this a difference with DOM.NodeId which is an int.

https://chromedevtools.github.io/devtools-protocol/tot/Accessibility/#type-AXNodeId
https://chromedevtools.github.io/devtools-protocol/tot/DOM/#type-NodeId
2026-04-24 11:24:16 +02:00
Karl Seguin
550fb58f3f Introduce Page (container)
Follow up to https://github.com/lightpanda-io/browser/pull/2200

This change is actually pretty mundane, but a bunch of files that used to
take a *Session (e.g. every WebAPI releaseRef and deinit) now take a *Page.

This aims to separate the 2 lifetimes currently managed by Session by moving
the "Page" lifetime to a dedicated container: Page. Ultimately, the goal is to
remove the 1-page-per-session limit of the current design. Not to explicitly
support multiple pages per session (though, that's more possible now), but
in order to better emulate Chrome where, during a navigation event, the old and
new page both exist.
2026-04-23 15:48:13 +08:00
Karl Seguin
73320e163d Add placeholder handlers for Audit enable/disable CDP methods
Might help with: https://github.com/lightpanda-io/browser/issues/2177

I say "might" because there are a 2 more methods in Audit which I haven't
implemented. This is just the most basic placeholder for now.
2026-04-23 09:19:49 +08:00
Adrià Arrufat
e9c93a49f0 cdp: promote <label> to checkbox/radio for CSS-hidden inputs
Chromium's accessibility tree prunes elements hidden via `display:none`,
`visibility:hidden`, the `hidden` attribute, `inert`, or `aria-hidden="true"`
and we match this behavior. It's correct for most elements, but it breaks
a common form pattern: CSS-only toggle switches and custom radio groups
hide the real `<input>` and style the `<label>` as the interactive surface.

Before, an agent walking the AX tree for a toggle switch saw:

    {role: "none", name: "Enable feature", ignored: false}

a generic element with no indication it's interactive and no exposure of
the underlying `checked` state. The input was pruned and the label had no
intrinsic role.

Now, when a `<label>` is associated (via `for=` or by wrapping) with a
hidden checkbox or radio input, the label is promoted to the input's role
and state:

    {role: "checkbox", name: "Enable feature",
     properties: [..., {checked: "true"}, {focusable: true}, ...]}

Native browsers already forward label clicks to the associated input, so
the label's `backendDOMNodeId` remains a valid click target — no action-
side changes needed.

Scope is intentionally narrow:

  - Only checkbox and radio inputs. Other labelable controls (textarea,
    select, etc.) don't have the "visible label as interactive surface"
    idiom.
  - Skipped when the label has an explicit `role=` attribute, to respect
    the page's declared semantics.
  - Skipped when the input is visible: normal AX tree flow already
    surfaces it, and promoting would double-count.

The fixture `src/browser/tests/cdp/ax_tree.html` covers:

  - `display:none` checkbox, checked, referenced by `<label for=id>`
  - `display:none` radio, checked, referenced by `<label for=id>`
  - `visibility:hidden` radio, unchecked, referenced by `<label for=id>`
  - Wrapping `<label>` containing a `display:none` checkbox
2026-04-22 16:04:29 +02:00
Karl Seguin
2275416505 Page -> Frame
This is to pave the way for introducing a new "Page" container, which will take
over the page lifecycle currently burdening Session. The ultimate goal of that
is to allow the Session to have multiple pages (mostly for better transitions
between pages), which is hard to do now since the Session has so much state.

This rename was aggressive, e.g. currentPage() -> currentFrame() so that, when
the new Page container is added, you won't see "currentPage()" and wonder:

  "Does 'currentPage' mean the new Page container, or the Frame (which
  used to be called Page)".
2026-04-22 08:42:18 +08:00
Karl Seguin
842affd83b Pre-op for Page -> Frame rename
Rename page.id -> page._loader_id and propagate the change throughout. This was
my attempt at pretending that page.id (and page._frame_id) weren't CDP-sepcific.
But they are, and it's a lot cleaner to treat them this way. Might seem
unnecessary, but without this, after page -> frame, you'd end up with:

frame.id
frame._frame_id

Which is weird? What is `frame.id` if it isn't the frame id and if that's the
case, what's frame_id? Now it'll be:

frame._loader_id
frame._frame_id

Which removes the ambiguity, makes the CDP code a bit more obvious, and doesn't
try to hide the fact that these are CDP things that, for now at least, pollute
the code a little.
2026-04-22 06:30:23 +08:00
Karl Seguin
5731429200 Merge pull request #2205 from lightpanda-io/cdp_timing_fields
Add timing fields to a few CDP messages
2026-04-22 06:28:28 +08:00
Karl Seguin
686591baed fix wrong method name, ugh 2026-04-21 17:49:59 +08:00
Karl Seguin
8294ad4921 Add timing fields to a few CDP messages
Hopefully fixes https://github.com/lightpanda-io/browser/issues/2199

Adds requestTime to responseReceived and, for completeness, adds timestamp and
wallTime to requestWillBeSent.
2026-04-21 17:40:36 +08:00
Karl Seguin
33f8905f1d Shrink screenshot.png
I asked claude to do for the png what it did for the pdf. The savings aren't as
big AND, it isn't as nice as the original.

I'm tempted to say we should keep the original, but I don't feel strongly about
it and maybe saving a few KB is worth it?
2026-04-21 17:10:26 +08:00
Pierre Tachoire
fbb93e7d53 cdp: implement a fake Page.printToPDF
Similar to Page.captureScreenshot
This is useful for tools integration calling this method, returning a
fake result instead of an error.
2026-04-20 16:52:24 +02:00