PageSession

class PageSession(val context: Context, val viewport: Viewport?, val config: BrowserConfig, parentJob: Job? = null, val registry: SessionRegistry = SessionRegistry())

Manages the lifecycle, thread safety, state transitions, and ordered teardown of a single page session.

Rules:

  • Every path ends in close, including cancellation and exceptions.

  • Calling any method on a closed session throws ErrorCode.DETACHED.

  • Destroying the view is NEVER performed inside a WebView client callback.

  • Main thread operations are marshalled cleanly to the Looper.Main.

Constructors

Link copied to clipboard
constructor(context: Context, viewport: Viewport?, config: BrowserConfig, parentJob: Job? = null, registry: SessionRegistry = SessionRegistry())

Properties

Link copied to clipboard

The BrowserConfig this session was created with.

Link copied to clipboard

The Android context this session runs in.

Link copied to clipboard

The live WebView, or throws ErrorCode.DETACHED if initialize has not run yet.

Link copied to clipboard

Where this session's counters are kept.

Link copied to clipboard
var scope: CoroutineScope

Scope tied to sessionJob; every session-owned coroutine should launch on this.

Link copied to clipboard
var sessionJob: Job

Coroutine scope owned by this session. Cancelling parentJob or calling close cancels this scope and triggers teardown.

Link copied to clipboard

Current lifecycle state.

Link copied to clipboard

The size the WebView was created at; null means the default 1x1 offscreen size.

Functions

Link copied to clipboard
suspend fun capabilities(): Capabilities

Probes and returns actual capabilities for this session.

Link copied to clipboard

Throws ErrorCode.TARGET_CRASHED or ErrorCode.DETACHED if the session has been closed or destroyed.

Link copied to clipboard
suspend fun close()

Synchronously/Suspendably closes the session and tears down resources. Safe to call multiple times (idempotent). Teardown itself is non-cancellable.

Link copied to clipboard

Handles renderer termination (crash or OOM kill) from WebView callbacks. Discards the dead view, closes session state, and prevents host app crash.

Link copied to clipboard
suspend fun initialize(): HostedWebView

Initializes the session on the main thread and creates the underlying WebView.

Link copied to clipboard

Returns true if the WebView render process for this session died or was killed by OS.

Link copied to clipboard

Returns snapshot of locally recorded session metrics.

Link copied to clipboard

Called by engines when a request is blocked, to tally the bytes that were saved.

Link copied to clipboard
fun recordJsEvaluation(durationMs: Long)

Called by engines after a script runs, to accumulate evaluation count and time.

Link copied to clipboard

Called by engines after a navigation completes, to bump the local navigation counter.

Link copied to clipboard

Records a navigation this session refused under the SSRF rules.

Link copied to clipboard
suspend fun recover(): HostedWebView

Recovers a session after a renderer crash or termination. Recreates the underlying WebView and resets session health state.

Link copied to clipboard
suspend fun <T> runInState(targetState: SessionState, block: suspend CoroutineScope.() -> T): T

Guarantees that an operation runs within the active session state.

Link copied to clipboard

Schedules ordered teardown off the current stack trace if triggered from a callback. Always posts to mainHandler so teardown never runs inside a WebView callback stack frame.