Skip to content

feat(webview): Web Share API with files in the Android web container (slock #15772) - #5120

Closed
stamp-build[bot] wants to merge 6 commits into
mainfrom
agent/lagoon/webview-share
Closed

stamp-build[bot] wants to merge 6 commits into
mainfrom
agent/lagoon/webview-share

Conversation

@stamp-build

@stamp-build stamp-build Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Lagoon from raft opened this pull request through Stamp
View in Stamp →


What / why

Builds on #5108 (its port and download-name hook). For slock #15772 (#proj-workspace:#346): the Artifact viewer's ⋯ › Share appears only when navigator.canShare({ files: [file] }) is true and calls navigator.share({ files, title }) synchronously in the click. Android's WebView has no Web Share API, so Share never showed in the app.

  • Android: a document-start shim on the page's own origin gives the page navigator.canShare and navigator.share; the app receives the files and opens the system share sheet.
  • iOS: no change. WKWebView already has Web Share level 2 with files (WebShareEnabled / WebShareFileAPIEnabled are on for Cocoa; the app's target is iOS 15.0). The shim also steps aside wherever the page already has share and canShare.
  • Desktop: unchanged.

Design

Shim (raftWebViewShareShimScript, RaftWebViewShare.kt)

  • Installed with fix(webview): a hosted page's downloads stay in the app, as a browser keeps them (#202) #5108's port, for a host that takes downloads or any origin-locked page; top frame only (window.top !== window → not installed), because the app takes the port's messages from the main frame alone.
  • canShare is synchronous: files must be Blobs, at most 64 files and 100 MB together; or a url / text / title string.
  • share returns a Promise and rejects in the Web Share order: InvalidStateError while an earlier share of this page is in flight, NotAllowedError when navigator.userActivation.isActive is false, TypeError for data it cannot share.
  • Files go as base64 chunks (384 KB) to the origin-bound raftDownloadPort, one chunk at a time: the next is read only after the app acknowledges the last (__raftShareAck), so neither side holds more than a chunk.
  • Every message is serialised with its kind first ({"k":"share-…); the app tells a share message from a download read by that exact prefix.

Receiver (RaftWebViewShareReceiver, common code)

  • Validates each share: id shape ([A-Za-z0-9_]{1,64}), file count, each size non-negative and within the cap, a checked running sum, chunk order, announced sizes.
  • Each decoded chunk goes straight to a sink; no file is held in memory.
  • File names go through RaftShareFileName.sanitize (one path component, 200 bytes) and are made unique within the share.
  • Every started share ends in exactly one of: ready, failed (DataError), or aborted (AbortError: evicted by a fifth concurrent share, silent for 2 minutes, or the port removed).

Android (RaftSandboxWebView.android.kt)

  • The UI-thread listener only checks the message prefix and hands the message on. Parsing, base64 decoding and file writes run on one background thread, in posting order.
  • Each share is staged in its own cache/slock-share-files/<uuid>/<name> entry, the same root and naming as attachment shares (existing FileProvider path). There is no web-share/ parent.
  • Before each share the staging root is swept by the attachment share's rules: entries older than 24 h and all but the newest 16 go. The attachment sweep lives in androidApp and cannot be called from the shared module, so the rule is duplicated as raftWebViewShareStaleStagingEntries; the two must be kept in step (noted on sweepAndroidAttachmentShareStaging).
  • A failed or abandoned share's entry is deleted at once. removeDownloadSupport closes the session: pending shares are aborted, their partial files deleted, and later messages for that session are ignored.
  • The expiry check is re-armed while any share is still being received.
  • The chooser is ACTION_SEND / ACTION_SEND_MULTIPLE with read grants; text-only is text/plain. The intent type is the files' normalised common type (RaftShareFileName.resolveMimeType), else major/*, else */*.
  • The Promise resolves once the chooser is shown and rejects with AbortError if it could not open.

Checks

  • RaftWebViewShareTest (common): shim strings (canShare, port, caps, top-frame / in-flight / activation guards, ack, kind-first messages, last-comma split); settle and ack scripts; streaming two files with duplicate names; chunk order (skipped, repeated, wrong file, too many bytes, bad base64, empty); negative and overflowing sizes; file-count cap; hostile names (../x, 300-byte, empty, .., case clash); invalid and empty ids; eviction, expiry, abort, restart, write failure; intent type; staging sweep rule.
  • Hosted CI: Android compile & tests.
  • Not tested: the Android side has no unit tests — the background thread, file writes, FileProvider URIs, the ack round trip, the expiry timer and the chooser intent. iOS compile is skipped by the path plan for this diff. Nothing has been run on a device.

Device checks needed

Android, with slock #15772 on staging, in the Artifact viewer ⋯ › Share:

  • one PDF: the chooser opens, the file carries its File.name, and a target app receives it intact;
  • several files: all arrive, with a sensible type;
  • a file over 100 MB: Share is refused (canShare false) and nothing is staged;
  • cancel the chooser: the page is not left stuck and a second Share works.

iOS: ⋯ › Share shows and the native share sheet opens from WKWebView in the viewer (confirms native level 2 in our WKWebView configuration).

Known limits

  • User activation is enforced in the shim only; the app does not check it natively.
  • Chunk pacing by acknowledgement is the shim's behaviour; the app does not enforce it against a page that posts chunks by hand (the size, count and order checks still apply).
  • Cancelling the chooser is not detectable on Android without a broadcast receiver, so the Promise resolves once the sheet is shown.
  • Staged files stay up to 24 h (or until 16 newer shares) so a receiver can keep reading the granted URI.

Requirement Challenge

  • Cut: an iOS polyfill (WebKit has it); detecting chooser cancellation.

🤖 Generated with Claude Code

stamp-build Bot pushed a commit that referenced this pull request Nov 20, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
stamp-build Bot pushed a commit that referenced this pull request Nov 20, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@stamp-build
stamp-build Bot force-pushed the agent/lagoon/webview-share branch from 4dd68e1 to 6ada248 Compare November 20, 2026 05:59
@stamp-build stamp-build Bot mentioned this pull request Nov 20, 2026
26 tasks
Lagoon and others added 5 commits November 20, 2026 10:00
…(slock #15772)

Android's WebView has no navigator.share. A document-start shim (the page's own origin) gives the
page a synchronous navigator.canShare and a Promise-returning navigator.share that posts the files
in base64 chunks to the origin-bound download port; the app writes them under its FileProvider
share folder and opens the system share sheet (ACTION_SEND / SEND_MULTIPLE; text-only shares send
text), then resolves the Promise once the sheet is shown or rejects it (AbortError / DataError).
iOS WKWebView already has Web Share level 2 (WebKit WebShareEnabled / WebShareFileAPIEnabled on for
Cocoa), so the shim steps aside wherever the page has the API.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review findings on #5120:
- each chunk is parsed, decoded and appended to its file on one background
  thread, acknowledged before the page reads the next; no file is held in
  memory, and pending shares end with the port, on eviction, or after two
  silent minutes (AbortError)
- a share is staged in its own <uuid> entry of slock-share-files, under the
  attachment share's age and count rules, not in a web-share/ parent
- the shim rejects without user activation (NotAllowedError), while a share
  is in flight (InvalidStateError), and is not installed in subframes
- sizes are bounded one by one with a checked sum; ids and file names are
  validated (RaftShareFileName.sanitize); the intent type is normalised

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@stamp-build
stamp-build Bot force-pushed the agent/lagoon/webview-share branch from 6ada248 to 93740c3 Compare November 20, 2026 10:08
Second review of #5120:
- the expiry check comes back while a share is still being received, so one
  whose last message was handled late still ends
- a session whose port is gone takes no more messages
- a share message is told by its leading kind, so a download read can never
  be detoured past its own chunks
- the data URL is split at its last comma
- tests assert the acknowledged chunk rather than any non-null result

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@stamp-build

stamp-build Bot commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Merged by Stamp in batch 894d75681ac8d24d90255bd7f50b16c5: 99f4e64 . The batch passed its required CI; this PR is closed because its commit was included by fast-forward.

Review in stamp.build

@stamp-build stamp-build Bot closed this Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants