diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..adcc2f6 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +# The image needs only the SDL build script from this checkout. +* +!Scripts/build-sdl3.sh diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml index 318cf6d..e12d884 100644 --- a/.github/workflows/swift.yml +++ b/.github/workflows/swift.yml @@ -18,18 +18,26 @@ jobs: - arch: arm64 runner: ubuntu-24.04-arm runs-on: ${{ matrix.runner }} - container: swift:6.4.2 + # Debian 13, which packages WPE WebKit; Ubuntu 26.04 doesn't. + container: swift:6.4.2-trixie env: # SDL's headless driver, for the window tests. SDL_VIDEO_DRIVER: offscreen + # WebKit's web processes run without their bubblewrap sandbox, which + # can't make namespaces in a container; the tests' pages are their own. + WEBKIT_DISABLE_SANDBOX_THIS_IS_DANGEROUS: 1 steps: - uses: actions/checkout@v4 - - name: Install SDL3, Cairo, FreeType, fontconfig and Wayland for Cacao, and Mesa for GL + - name: Install Cairo, FreeType, fontconfig and Wayland for Cacao, Mesa for GL, and WPE WebKit run: | apt-get update - apt-get install -y --no-install-recommends libsdl3-dev libcairo2-dev libfreetype-dev \ + apt-get install -y --no-install-recommends cmake ninja-build git libcairo2-dev libfreetype-dev \ libfontconfig-dev libwayland-dev wayland-protocols fonts-dejavu-core \ - libegl1 libegl-mesa0 libgles-dev libgl1-mesa-dri + libegl1 libegl-mesa0 libgles-dev libgl1-mesa-dri \ + libwpewebkit-2.0-1 libwpebackend-fdo-1.0-1 libwayland-server0 + # Debian's SDL is 3.2; PureSwift/SDL needs 3.4. + - name: Build SDL3 + run: Scripts/build-sdl3.sh /usr -DCMAKE_INSTALL_LIBDIR=lib/$(uname -m)-linux-gnu - name: Build run: swift build --build-tests - name: Test diff --git a/Dockerfile b/Dockerfile index c5d0b3f..ef03e69 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,18 +1,28 @@ -# Linux environment for building and testing the host: Swift 6.4.2, SDL3, -# Cairo, FreeType, fontconfig and Wayland for Cacao, a font, Mesa's EGL and -# GLES 2.0 for GL surfaces (llvmpipe without a GPU), Python for AppRuntime's -# scripts, and WABT for the packager's wasm2c engine. -FROM swift:6.4.2 +# Linux environment for building and testing the host: Swift 6.4.2 on +# Debian 13, SDL3, Cairo, FreeType, fontconfig and Wayland for Cacao, a font, +# Mesa's EGL and GLES 2.0 for GL surfaces (llvmpipe without a GPU), WPE WebKit +# and its FreeDesktop.org backend for web views, which the host loads at run +# time, Python for AppRuntime's scripts, and WABT for the packager's wasm2c +# engine. +# +# Debian rather than Ubuntu: Ubuntu 26.04 doesn't package WPE WebKit. SDL is +# built from source: Debian's is 3.2, and PureSwift/SDL needs 3.4. +FROM swift:6.4.2-trixie RUN apt-get update \ && apt-get install -y --no-install-recommends \ - libsdl3-dev libsdl3-ttf-dev libfontconfig-dev fonts-dejavu-core \ + cmake ninja-build git libfontconfig-dev fonts-dejavu-core \ libcairo2-dev libfreetype-dev libwayland-dev wayland-protocols \ libegl1 libegl-mesa0 libgles-dev libgl1-mesa-dri \ + libwpewebkit-2.0-1 libwpebackend-fdo-1.0-1 libwayland-server0 \ pkgconf python3 wabt curl ca-certificates \ && rm -rf /var/lib/apt/lists/* -# Ubuntu's wabt leaves out two files its wasm2c runtime includes; these are +COPY Scripts/build-sdl3.sh /tmp/ +RUN /tmp/build-sdl3.sh /usr -DCMAKE_INSTALL_LIBDIR=lib/$(uname -m)-linux-gnu \ + && rm /tmp/build-sdl3.sh + +# Debian's wabt leaves out two files its wasm2c runtime includes; these are # from the same release. RUN cd /usr/share/wabt/wasm2c \ && curl -fsSLO https://raw.githubusercontent.com/WebAssembly/wabt/1.0.36/wasm2c/wasm-rt-impl-tableops.inc \ diff --git a/Documentation/ExampleWeb.png b/Documentation/ExampleWeb.png new file mode 100644 index 0000000..38f0ccd Binary files /dev/null and b/Documentation/ExampleWeb.png differ diff --git a/Documentation/Plan.md b/Documentation/Plan.md index be994ec..f355360 100644 --- a/Documentation/Plan.md +++ b/Documentation/Plan.md @@ -29,6 +29,7 @@ into a Linux program: frontend's render IR, on Cacao's SwiftUI for Linux; - **Bluetooth** over BlueZ's D-Bus API; - **GL surfaces**, OpenGL ES 2.0 over EGL, in the player's views; +- **web views**, WPE WebKit pages in the player's views; - the **packager**, `linux-app` ([Packager.md](Packager.md)). References like §7.6 are to `Documentation/DynamicRuntime.md` in AppRuntime; @@ -45,7 +46,7 @@ AppRuntime's contracts. | arm64 and x86_64 Linux with glibc (Debian 13, Ubuntu 24.04+) | armv7, musl | | Native slices linked into the host | wasm2c (AppRuntime's shim, as on Darwin); WasmKit for development | | Wayland and X11 through SDL3; a headless driver for tests | Multiple windows | -| `Renderer`, Bluetooth `Central` and friends, GL surfaces, the built-ins | Location (GeoClue), Device (UPower, sysfs), Networking (libcurl), Formatting (ICU), Motion (iio-sensor-proxy), NFC (neard) | +| `Renderer`, Bluetooth `Central` and friends, GL surfaces, web views, the built-ins | Location (GeoClue), Device (UPower, sysfs), Networking (libcurl), Formatting (ICU), Motion (iio-sensor-proxy), NFC (neard) | ## 2. Process model @@ -56,6 +57,7 @@ AppRuntime's contracts. │ ├── MainLoop: timers, jobs from other threads │ │ ├── LinuxBluetooth: BlueZ over D-Bus (NIO threads → MainLoop) │ │ ├── LinuxGraphics: GL surfaces, EGL pbuffers read back │ +│ ├── LinuxWebKit: WPE WebKit pages, frames in shared memory │ │ ├── LinuxUI: DarwinUI, render IR → SwiftUI views → events │ │ │ └── Cacao: SwiftUI, Silica/Cairo, SDL3 windows and input │ │ └── libapp.a: the app's sealed linux-arm64 / linux-x86_64 slice│ @@ -89,8 +91,9 @@ timers for `schedule`. | `LinuxUI` | The SwiftUI player: DarwinAppRuntime's `DarwinUI`, copied by `Scripts/sync-darwinui.sh`, over Cacao's `SwiftUI` and `Combine` modules; `Host.swift` gives it the logger and bundle it takes from its host | | `LinuxBluetooth` | `Central`, `Peripheral`, `Service`, `Characteristic`, `Descriptor`, `Advertisement` over BlueZ | | `LinuxGraphics` | `Surface` over EGL and GLES 2.0, loaded at run time; the player's `GLSurface` view shows it (§7) | +| `LinuxWebKit` | `WebContent` over WPE WebKit, loaded at run time; the player's `WebView` view shows it (§8) | | `LinuxHostApp` | `main`: reads the app's keys, starts the host, and runs Cacao's SDL loop with one window showing the guest's first renderer | -| `LinuxPackager`, `linux-app` | The packager (§9, [Packager.md](Packager.md)) | +| `LinuxPackager`, `linux-app` | The packager (§10, [Packager.md](Packager.md)) | The package depends on AppRuntime without its `GuestExports` trait, like DarwinAppRuntime, so the slice's `rt_dispatch` and `rt_finalize` are the @@ -107,7 +110,7 @@ only ones linked (`HostRuntime.md` §8 in DarwinAppRuntime). 3. `NativeRuntime.shared.bindDirectly()`; set capabilities; set the clock. 4. Register the built-ins (`Permissions`, `Bundle`, `Application`) and each module the app uses (`LinuxUI` for `Renderer`, `LinuxBluetooth`, - `LinuxGraphics`). + `LinuxGraphics`, `LinuxWebKit`). 5. Call `rt_main`. When the guest calls `Renderer.make`, the player adds its scene; the first tree is there before `rt_main` returns. 6. Run Cacao's loop until the window closes, the window showing the first @@ -207,20 +210,90 @@ image: window is in the background. Reading back each frame costs a copy; a GL backend for Cacao's compositor could show the pbuffer as a texture. -## 8. Testing +## 8. Web views + +`LinuxWebKit` implements `WebContent` (AppRuntime's `WebKit.md`) with WPE +WebKit, and the player draws `WebView` nodes. It follows Perry's +(`Sources/PerryUIKit/WPE.swift` there): + +- **No WebKit at build time.** The host opens libwpe, WPEBackend-fdo, + libwayland-server, `libWPEWebKit-2.0.so.1`, GLib and GObject when a page + is first made, locally, so their symbols stand in for no others. Without + them a page loads nothing, and fails each load saying so. Debian 13 + packages WPE 2.48 (`libwpewebkit-2.0-1`, `libwpebackend-fdo-1.0-1`); + Ubuntu 26.04 doesn't, so the development image and CI moved to Debian 13. +- **Frames in shared memory.** WPEBackend-fdo runs in its SHM mode + (`wpe_fdo_initialize_shm`), and each page's exportable view backend hands + over every frame the web process draws as a `wl_shm` buffer: premultiplied + BGRA, turned into RGBA for Cacao's `Image(pixels:)`. WebKit draws on the + CPU; there's no GL. +- **GLib's context** runs on the main loop every 8 ms while there are pages: + their signals, script results and frames all come through it. +- **The contract** maps onto `WebKitWebView`: + - `load-changed`, `load-failed` and `load-failed-with-tls-errors` give + loading, finishing and failing; + - `notify::` on `uri`, `title` and `estimated-load-progress`, and the + history after each commit, give the rest; + - `uri` changes as soon as a load is asked for, and before a history + navigation is decided, so it's reported only once the navigation + commits or follows a redirect, as the contract wants; + - cancelled and replaced navigations aren't failures. +- **Errors** come as GErrors and map to `WebError.Code`: WebKit's network + and policy errors, GIO's (a timeout is `timedOut`, the rest `network`), + the resolver's, and TLS (`security`). +- **`decide-policy`** asks `shouldNavigate` for each navigation and new + window, except the guest's own `load`, `loadHTML` and `loadResource`. A + new window loads in the same page. WPE doesn't say which frame a + navigation is in, so `isMainFrame` is always true. +- **Without `network`**: + - a remote `load`, or a navigation to a remote URL, fails with + `notPermitted` before it starts; + - every other request goes through a proxy on a `.invalid` host (RFC + 2606), which never resolves, so remote subresources don't load. +- **`loadResource`** loads a `file://` URL in the bundle's `resources/`, + refusing paths that leave it. +- **The view** (`Sources/LinuxUI/HostWebView.swift`): + - It gives the page its size in points and the display's scale, and + makes it visible while it's on screen. The page stays loaded when the + view goes. + - A drag is the primary button, and hovering moves the pointer. + - A click focuses the page. Keys go to it as XKB keysyms, which WPE turns + into text. +- **The sandbox.** WebKit starts its web and network processes under + bubblewrap, which can't make namespaces in a container, so the + development image and CI set `WEBKIT_DISABLE_SANDBOX_THIS_IS_DANGEROUS=1` + for their test pages. The host leaves the sandbox on. Inside a launcher's + own sandbox (AppRuntime's §7.8) it's still the open question `WebKit.md` + names. +- **Not yet**: + - The wheel: Cacao sends it only to its scroll views. Pages scroll with + the arrow keys, Page Up and Page Down, and Space. + - `alert`, `confirm` and `prompt`: WebKit's defaults apply, which show + nothing. + - Permission requests: WebKit denies them by default. + - Touches, and buttons other than the primary. + - Reading files beside a bundled page only: WebKit's file-URL rules + apply. + +## 9. Testing - `LinuxUI`: DarwinUI's tests, on Cacao (§5). - `LinuxBluetooth`: the contract against a fake `BlueZBus` that plays BlueZ's objects and signals. Runs on macOS and Linux. - `LinuxGraphics`: a surface's input order, focus and size, and on Linux frames drawn and read back with Mesa's llvmpipe, and a lost context. +- `LinuxWebKit`: on Linux, in WPE: loading HTML and the events' order, + scripts' JSON, navigations asked about and refused, back, bundle + resources, a refused connection, remote loads without `network`, frames + and their pixels, a click, typing, and a resize. Every WebKit call is made + on one thread, which WebKit takes for its main thread. - End to end: AppRuntime's `ExampleUI` built as a `linux-arm64` slice, linked into `linux-app-host` and run headless with SDL's `offscreen` driver, its first frame saved with `CACAO_SCREENSHOT`. - Real BlueZ needs hardware: manual runs on a Raspberry Pi or a laptop with `bluetoothd`. -## 9. Phases +## 10. Phases 1. **Host runtime and player**: `LinuxHost`, `LinuxUI`, `LinuxBluetooth`, `LinuxHostApp`; `ExampleUI` runs in Docker. @@ -235,7 +308,7 @@ image: 4. **Player**: what DarwinUI and Cacao add: Cacao's AT-SPI bridge, input replay for end-to-end tests. -## 10. Open questions +## 11. Open questions - **Permissions.** Desktop Linux has no prompts for Bluetooth; Flatpak's portals cover location and the camera. `Permissions.request` answers from diff --git a/Package.swift b/Package.swift index cf8ca1e..6c88ced 100644 --- a/Package.swift +++ b/Package.swift @@ -71,6 +71,7 @@ let package = Package( .library(name: "LinuxUI", targets: ["LinuxUI"]), .library(name: "LinuxBluetooth", targets: ["LinuxBluetooth"]), .library(name: "LinuxGraphics", targets: ["LinuxGraphics"]), + .library(name: "LinuxWebKit", targets: ["LinuxWebKit"]), .library(name: "LinuxHostApp", targets: ["LinuxHostApp"]), .executable(name: "linux-app-host", targets: ["LinuxAppHost"]), .executable(name: "linux-app", targets: ["LinuxAppTool"]), @@ -101,7 +102,7 @@ let package = Package( // over Cacao's SwiftUI. .target( name: "LinuxUI", - dependencies: ["LinuxHost", "LinuxGraphics", runtime("DynamicUI"), runtime("DynamicRuntime"), runtime("RuntimeNative")] + swiftUI, + dependencies: ["LinuxHost", "LinuxGraphics", "LinuxWebKit", runtime("DynamicUI"), runtime("DynamicRuntime"), runtime("RuntimeNative")] + swiftUI, exclude: ["DARWINUI_REVISION"], swiftSettings: host ), @@ -127,13 +128,22 @@ let package = Package( swiftSettings: host ), + // MARK: - Web views + + // WPE WebKit, loaded at run time: the host links no WebKit. + .target( + name: "LinuxWebKit", + dependencies: ["LinuxHost", runtime("DynamicWebKit"), runtime("DynamicRuntime"), runtime("RuntimeABI")], + swiftSettings: host + ), + // MARK: - App // libc functions app slices need that older glibc lacks. .target(name: "CLinuxHost"), .target( name: "LinuxHostApp", - dependencies: ["LinuxHost", "LinuxUI", "LinuxBluetooth", "LinuxGraphics", "CLinuxHost", runtime("CRuntime"), + dependencies: ["LinuxHost", "LinuxUI", "LinuxBluetooth", "LinuxGraphics", "LinuxWebKit", "CLinuxHost", runtime("CRuntime"), .product(name: "CacaoSDL", package: "Cacao")], swiftSettings: host ), @@ -155,7 +165,8 @@ let package = Package( // doesn't build Cacao. .target( name: "LinuxPackager", - dependencies: ["LinuxHost", "LinuxBluetooth", "LinuxGraphics", runtime("DynamicUI"), .product(name: "Crypto", package: "swift-crypto")] + dependencies: ["LinuxHost", "LinuxBluetooth", "LinuxGraphics", "LinuxWebKit", runtime("DynamicUI"), + .product(name: "Crypto", package: "swift-crypto")] ), .executableTarget( name: "LinuxAppTool", @@ -172,6 +183,7 @@ let package = Package( ), .testTarget(name: "LinuxBluetoothTests", dependencies: ["LinuxBluetooth"], swiftSettings: host), .testTarget(name: "LinuxGraphicsTests", dependencies: ["LinuxGraphics", runtime("DynamicGraphics")], swiftSettings: host), + .testTarget(name: "LinuxWebKitTests", dependencies: ["LinuxWebKit", runtime("DynamicWebKit")], swiftSettings: host), .testTarget(name: "LinuxPackagerTests", dependencies: ["LinuxPackager", "LinuxHostApp"]), ] ) diff --git a/README.md b/README.md index d853c97..2cbc7aa 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ Linux program: SwiftUI for Linux; - **Bluetooth** LE central over BlueZ's D-Bus API; - **GL surfaces**: OpenGL ES 2.0 in a SwiftUI view, over EGL; +- **web views**: WPE WebKit pages in a SwiftUI view; - the **packager**, `linux-app`, which turns a universal bundle into an installable app. @@ -31,6 +32,10 @@ llvmpipe: ![ExampleGL on Linux](Documentation/ExampleGL.png) +`ExampleWeb` shows a page in a `WebView` with WPE WebKit: + +![ExampleWeb on Linux](Documentation/ExampleWeb.png) + ## Modules | Module | | @@ -39,23 +44,32 @@ llvmpipe: | `LinuxUI` | The SwiftUI player: DarwinAppRuntime's `DarwinUI`, copied by `Scripts/sync-darwinui.sh`, over Cacao's `SwiftUI` | | `LinuxBluetooth` | `Central`, `Peripheral`, `Service`, `Characteristic`, `Descriptor` and `Advertisement` over BlueZ, through `wendylabsinc/dbus` | | `LinuxGraphics` | `Surface`: GLES 2.0 drawn in EGL pbuffers, loaded at run time, and read back into the player's `GLSurface` view ([plan](Documentation/Plan.md) §7) | +| `LinuxWebKit` | `WebContent`: WPE WebKit pages, loaded at run time, drawn into shared memory and shown by the player's `WebView` view ([plan](Documentation/Plan.md) §8) | | `LinuxHostApp`, `linux-app-host` | The app's `main`: the host, and Cacao's window and loop | | `LinuxPackager`, `linux-app` | The packager: validation, the host build, wasm2c, the install tree, PortMaster ports | ## Building -Swift 6.4.2 (`.swift-version`); SDL3, Cairo, FreeType, fontconfig and -Wayland for Cacao; and EGL and GLES 2.0, which GL surfaces load at run time -and apps that draw link (Mesa's draw without a GPU): +Swift 6.4.2 (`.swift-version`) on Debian 13; SDL 3.4, Cairo, FreeType, +fontconfig and Wayland for Cacao; EGL and GLES 2.0, which GL surfaces load +at run time and apps that draw link (Mesa's draw without a GPU); and WPE +WebKit, which web views load at run time: ```sh -apt-get install libsdl3-dev libcairo2-dev libfreetype-dev libfontconfig-dev \ +apt-get install cmake ninja-build git libcairo2-dev libfreetype-dev libfontconfig-dev \ libwayland-dev wayland-protocols fonts-dejavu-core \ - libegl1 libegl-mesa0 libgles-dev libgl1-mesa-dri + libegl1 libegl-mesa0 libgles-dev libgl1-mesa-dri \ + libwpewebkit-2.0-1 libwpebackend-fdo-1.0-1 libwayland-server0 +Scripts/build-sdl3.sh /usr -DCMAKE_INSTALL_LIBDIR=lib/$(uname -m)-linux-gnu swift build swift test ``` +Debian's SDL is 3.2, and PureSwift/SDL needs 3.4, so `build-sdl3.sh` +builds it. Ubuntu 26.04 doesn't package WPE WebKit. In a container, WebKit's +web processes need `WEBKIT_DISABLE_SANDBOX_THIS_IS_DANGEROUS=1`: its +bubblewrap sandbox can't make namespaces there. + The `Dockerfile` has all of it; `Scripts/docker.sh` runs a command there, with this checkout at `/src`: diff --git a/Scripts/build-sdl3.sh b/Scripts/build-sdl3.sh new file mode 100755 index 0000000..4c95d91 --- /dev/null +++ b/Scripts/build-sdl3.sh @@ -0,0 +1,22 @@ +#!/bin/sh +# Builds SDL3 from source for the development image and CI, which run on +# Debian 13: its SDL is 3.2, and PureSwift/SDL needs 3.4. Only the offscreen +# driver, for tests and screenshots without a display. +# +# Scripts/build-sdl3.sh PREFIX [cmake arguments …] +# +# SDL3_TAG picks the release, as Docker/build-sdl.sh's does for ports. +set -e +prefix=${1:?usage: Scripts/build-sdl3.sh PREFIX [cmake arguments …]} +shift +SDL3_TAG=${SDL3_TAG:-release-3.4.20} +work=$(mktemp -d) +git clone -q --depth 1 --branch "$SDL3_TAG" https://github.com/libsdl-org/SDL.git "$work/SDL" +cmake -S "$work/SDL" -B "$work/build" -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$prefix" \ + -DSDL_SHARED=ON -DSDL_STATIC=OFF -DSDL_TESTS=OFF -DSDL_INSTALL_TESTS=OFF -DSDL_EXAMPLES=OFF \ + -DSDL_UNIX_CONSOLE_BUILD=ON -DSDL_X11=OFF -DSDL_WAYLAND=OFF -DSDL_KMSDRM=OFF -DSDL_OFFSCREEN=ON -DSDL_VULKAN=OFF \ + -DSDL_PULSEAUDIO=OFF -DSDL_PIPEWIRE=OFF -DSDL_JACK=OFF -DSDL_SNDIO=OFF -DSDL_ALSA=OFF \ + "$@" +cmake --build "$work/build" +cmake --install "$work/build" +rm -rf "$work" diff --git a/Scripts/docker.sh b/Scripts/docker.sh index c571092..719a0dd 100755 --- a/Scripts/docker.sh +++ b/Scripts/docker.sh @@ -8,7 +8,9 @@ # APPRUNTIME_PATH=~/Developer/AppRuntime Scripts/docker.sh swift build --scratch-path .build/linux # # LINUXAPPRUNTIME_APP_LIBRARY, a path inside the container, and -# CACAO_SCREENSHOT_DELAY are passed through. +# CACAO_SCREENSHOT_DELAY are passed through. WebKit's web processes run +# without their bubblewrap sandbox, which can't make namespaces in a +# container. set -e root=$(cd "$(dirname "$0")/.." && pwd) image=linuxappruntime-dev @@ -29,4 +31,5 @@ exec docker run --rm \ -v "$root:/src" -w /src \ -v linuxappruntime-swiftpm:/root/.cache \ -e LINUXAPPRUNTIME_APP_LIBRARY -e CACAO_SCREENSHOT_DELAY \ + -e WEBKIT_DISABLE_SANDBOX_THIS_IS_DANGEROUS=1 \ "$@" "$image" bash -c "$command" diff --git a/Scripts/sync-darwinui.sh b/Scripts/sync-darwinui.sh index 742e56d..742f7ef 100755 --- a/Scripts/sync-darwinui.sh +++ b/Scripts/sync-darwinui.sh @@ -7,8 +7,9 @@ # supplies the logger and bundle DarwinUI uses); # - `#if os(macOS)` code, which is for any platform but iOS: Linux is # neither; -# - NodeView, which asks `HostNodeViews` (HostGLSurface.swift) for a type -# it doesn't know before giving up on it: `GLSurface` is this host's. +# - NodeView, which asks `HostNodeViews` (HostNodeViews.swift) for a type +# it doesn't know before giving up on it: `GLSurface` and `WebView` are +# this host's. # # Files named Host*.swift are this host's own, and stay. # diff --git a/Sources/LinuxHostApp/HostApp.swift b/Sources/LinuxHostApp/HostApp.swift index 4e2dfb8..fbd15c6 100644 --- a/Sources/LinuxHostApp/HostApp.swift +++ b/Sources/LinuxHostApp/HostApp.swift @@ -5,6 +5,7 @@ import CacaoSDL import Foundation import LinuxBluetooth import LinuxGraphics +import LinuxWebKit import LinuxUI /// The app's `main` (`Plan.md` §4): finds the app's bundle, checks and @@ -24,8 +25,8 @@ import LinuxUI public enum HostApp { /// Every module this host has: the SwiftUI player (DarwinUI, on Cacao), - /// Bluetooth, and GL surfaces, which the player shows. - public static let modules: [any HostModule.Type] = [DarwinUI.self, LinuxBluetooth.self, LinuxGraphics.self] + /// Bluetooth, and GL surfaces and web pages, which the player shows. + public static let modules: [any HostModule.Type] = [DarwinUI.self, LinuxBluetooth.self, LinuxGraphics.self, LinuxWebKit.self] /// The window's title: the app's name. nonisolated(unsafe) static var title = "" diff --git a/Sources/LinuxPackager/Validator.swift b/Sources/LinuxPackager/Validator.swift index f3d6d51..2547090 100644 --- a/Sources/LinuxPackager/Validator.swift +++ b/Sources/LinuxPackager/Validator.swift @@ -2,6 +2,7 @@ import DynamicRuntime import Foundation import LinuxBluetooth import LinuxGraphics +import LinuxWebKit import LinuxHost import DynamicUI import RuntimeABI @@ -10,16 +11,19 @@ import RuntimeABI public enum HostContracts { /// The modules `linux-app-host` registers (`HostApp.modules`): the - /// player's contracts, without building the player, Bluetooth and - /// graphics. - nonisolated(unsafe) public static let modules: [any HostModule.Type] = [PlayerContracts.self, LinuxBluetooth.self, LinuxGraphics.self] + /// player's contracts, without building the player, Bluetooth, + /// graphics and web views. + nonisolated(unsafe) public static let modules: [any HostModule.Type] = [ + PlayerContracts.self, LinuxBluetooth.self, LinuxGraphics.self, LinuxWebKit.self, + ] public static var provided: [String: ContractDescriptor] { AppCompatibility.providedContracts(modules) } - /// Capabilities some contract here serves. - public static let capabilities: Set = [.bluetooth, .graphics] + /// Capabilities some contract here serves: `network` is what lets a + /// web view's page reach the network. + public static let capabilities: Set = [.bluetooth, .graphics, .network] } /// How the guest's code gets into the app (`Packager.md` §2). diff --git a/Sources/LinuxUI/HostGLSurface.swift b/Sources/LinuxUI/HostGLSurface.swift index 66ea87f..5a4dab7 100644 --- a/Sources/LinuxUI/HostGLSurface.swift +++ b/Sources/LinuxUI/HostGLSurface.swift @@ -1,7 +1,5 @@ -// Node types this host draws itself, ahead of DarwinUI: `GLSurface`, a -// `LinuxSurface`'s frames and input (`Graphics.md`). Scripts/sync-darwinui.sh -// keeps this file and has NodeView ask `HostNodeViews` before it gives up -// on a type. +// `GLSurface` nodes: a `LinuxSurface`'s frames and input (`Graphics.md`). +// Scripts/sync-darwinui.sh keeps this file. import DynamicGraphics import DynamicUI import Foundation @@ -9,26 +7,6 @@ import LinuxGraphics import LinuxHost import SwiftUI -/// The views of node types DarwinUI doesn't know. -enum HostNodeViews { - - @MainActor - static func view(for node: RenderNode, store: RenderStore) -> AnyView? { - switch node.type { - case "GLSurface": - #if os(Linux) - let identifier = Int32(clamping: node.props["surface"]?.int ?? 0) - return AnyView(GLSurfaceView(identifier: identifier)) - #else - // Apple's SwiftUI, for development: no GL here. - return AnyView(Color.black) - #endif - default: - return nil - } - } -} - #if os(Linux) /// A surface's frames, with its pointer and key input going to it. diff --git a/Sources/LinuxUI/HostNodeViews.swift b/Sources/LinuxUI/HostNodeViews.swift new file mode 100644 index 0000000..f7d291b --- /dev/null +++ b/Sources/LinuxUI/HostNodeViews.swift @@ -0,0 +1,31 @@ +// Node types this host draws itself, ahead of DarwinUI: `GLSurface` +// (HostGLSurface.swift) and `WebView` (HostWebView.swift). +// Scripts/sync-darwinui.sh keeps this file and has NodeView ask +// `HostNodeViews` before it gives up on a type. +import DynamicUI +import SwiftUI + +/// The views of node types DarwinUI doesn't know. +enum HostNodeViews { + + @MainActor + static func view(for node: RenderNode, store: RenderStore) -> AnyView? { + switch node.type { + case "GLSurface": + #if os(Linux) + return AnyView(GLSurfaceView(identifier: Int32(clamping: node.props["surface"]?.int ?? 0))) + #else + // Apple's SwiftUI, for development: no GL here. + return AnyView(Color.black) + #endif + case "WebView": + #if os(Linux) + return AnyView(HostWebView(identifier: Int32(clamping: node.props["page"]?.int ?? 0))) + #else + return AnyView(Color.white) + #endif + default: + return nil + } + } +} diff --git a/Sources/LinuxUI/HostWebView.swift b/Sources/LinuxUI/HostWebView.swift new file mode 100644 index 0000000..bc1355a --- /dev/null +++ b/Sources/LinuxUI/HostWebView.swift @@ -0,0 +1,181 @@ +// `WebView` nodes: a `LinuxWebContent`'s frames, with the view's pointer and +// key input going to its page (`WebKit.md`). Scripts/sync-darwinui.sh keeps +// this file. +#if os(Linux) +import DynamicUI +import Foundation +import LinuxHost +import LinuxWebKit +import SwiftUI + +/// A page's frames, at the size the view is offered. +struct HostWebView: View { + + let identifier: Int32 + + @StateObject private var model = HostWebViewModel() + + @FocusState private var focused: Bool + + @Environment(\.displayScale) private var displayScale + + var body: some View { + GeometryReader { proxy in + frame + .frame(width: proxy.size.width, height: proxy.size.height) + .contentShape(Rectangle()) + .gesture( + DragGesture(minimumDistance: 0) + .onChanged { value in + focused = true + model.drag(to: value.location) + } + .onEnded { value in + model.endDrag(at: value.location) + } + ) + .onContinuousHover { phase in + if case let .active(location) = phase { + model.hover(at: location) + } + } + .focusable() + .focused($focused) + .onKeyPress(phases: .all) { press in + model.key(press) + } + .onChange(of: focused) { focused in + model.page?.focus(focused) + } + .onAppear { + model.attach(identifier, size: proxy.size, scale: displayScale) + } + .onChange(of: proxy.size) { size in + model.resize(size, scale: displayScale) + } + .onDisappear { + model.detach() + } + } + } + + @ViewBuilder + private var frame: some View { + if let frame = model.frame { + Image(pixels: frame.data, width: frame.width, height: frame.height, bytesPerRow: frame.width * 4, + scale: CGFloat(frame.scale)) + .resizable() + } else { + Color.white + } + } +} + +/// The view's page and its latest frame. +final class HostWebViewModel: ObservableObject { + + @Published private(set) var frame: WebFrame? + + private(set) var page: LinuxWebContent? + + private var isDragging = false + + func attach(_ identifier: Int32, size: CGSize, scale: CGFloat) { + guard page == nil else { return } + guard let page = LinuxWebContent.page(identifier: identifier) else { + HostLog.warning("web: no page \(identifier) for a WebView") + return + } + self.page = page + page.display = { [weak self] frame in + self?.frame = frame + } + page.attach(width: UInt32(max(0, size.width.rounded())), height: UInt32(max(0, size.height.rounded())), scale: Float(scale)) + } + + func resize(_ size: CGSize, scale: CGFloat) { + page?.resize(width: UInt32(max(0, size.width.rounded())), height: UInt32(max(0, size.height.rounded())), scale: Float(scale)) + } + + func detach() { + page?.detach() + page?.display = nil + page = nil + isDragging = false + } + + // MARK: Pointer + + func drag(to location: CGPoint) { + guard let page else { return } + page.pointerMoved(x: Float(location.x), y: Float(location.y)) + if !isDragging { + isDragging = true + page.pointerButton(x: Float(location.x), y: Float(location.y), pressed: true) + } + } + + func endDrag(at location: CGPoint) { + guard isDragging, let page else { return } + isDragging = false + page.pointerMoved(x: Float(location.x), y: Float(location.y)) + page.pointerButton(x: Float(location.x), y: Float(location.y), pressed: false) + } + + func hover(at location: CGPoint) { + guard !isDragging else { return } + page?.pointerMoved(x: Float(location.x), y: Float(location.y)) + } + + // MARK: Keys + + func key(_ press: KeyPress) -> KeyPress.Result { + guard let page, let keysym = Keysyms.keysym(for: press.key.character) else { + return .ignored + } + let modifiers = press.modifiers + page.key(keysym: keysym, pressed: press.phase != .up, control: modifiers.contains(.control), + shift: modifiers.contains(.shift), alt: modifiers.contains(.option), meta: modifiers.contains(.command)) + return .handled + } +} + +/// XKB keysyms, which libwpe's key events carry, for the keys Cacao +/// reports as characters. +enum Keysyms { + + static func keysym(for character: Character) -> UInt32? { + switch character { + case KeyEquivalent.return.character, "\n": return 0xFF0D + case KeyEquivalent.escape.character: return 0xFF1B + case KeyEquivalent.delete.character, "\u{7F}": return 0xFF08 // Backspace + case KeyEquivalent.deleteForward.character: return 0xFFFF + case KeyEquivalent.tab.character: return 0xFF09 + case KeyEquivalent.home.character: return 0xFF50 + case KeyEquivalent.leftArrow.character: return 0xFF51 + case KeyEquivalent.upArrow.character: return 0xFF52 + case KeyEquivalent.rightArrow.character: return 0xFF53 + case KeyEquivalent.downArrow.character: return 0xFF54 + case KeyEquivalent.pageUp.character: return 0xFF55 + case KeyEquivalent.pageDown.character: return 0xFF56 + case KeyEquivalent.end.character: return 0xFF57 + default: break + } + guard character.unicodeScalars.count == 1, let scalar = character.unicodeScalars.first else { + return nil + } + let value = scalar.value + switch value { + case 0x20...0x7E, 0xA0...0xFF: + // Latin-1's keysyms are its code points. + return value + case 0x100...0x10FFFF where !(0xE000...0xF8FF).contains(value): + // Other characters are Unicode keysyms; the private-use area + // is where Cacao's function keys are. + return 0x0100_0000 | value + default: + return nil + } + } +} +#endif diff --git a/Sources/LinuxWebKit/LinuxWebContent.swift b/Sources/LinuxWebKit/LinuxWebContent.swift new file mode 100644 index 0000000..10f3104 --- /dev/null +++ b/Sources/LinuxWebKit/LinuxWebContent.swift @@ -0,0 +1,663 @@ +import DynamicWebKit +import Foundation +import LinuxHost + +/// A frame of a page: 8-bit RGBA, premultiplied, rows top to bottom, in +/// pixels, and the pixels per point it was drawn at. +public struct WebFrame: Sendable { + + public var data: Data + + public var width: Int + + public var height: Int + + public var scale: Float +} + +/// The `WebContent` contract (`WebKit.md`) over a WPE `WebKitWebView`. +/// +/// The page loads whether or not it's shown. The player's `WebView` view +/// attaches it with its size in points, gets its frames through `display`, +/// and forwards its input. Main thread only. +public final class LinuxWebContent: WebContent { + + public typealias Pointer = UnsafeMutableRawPointer + + public let delegate: any WebContentDelegate + + private let id: Int32 + + private let wpe: WPE? + + private var exportable: Pointer? + + private var backend: Pointer? + + private var view: Pointer? + + /// The exportable's client data: this page, until it goes. + private let slot: UnsafeMutablePointer + + private var handlers: [UInt] = [] + + // MARK: What the delegate was last told + + private var reportedURL: String? + + private var reportedTitle = "" + + private var reportedHistory = (canGoBack: false, canGoForward: false) + + private var isLoading = false + + /// A navigation started and hasn't committed: WebKit's `uri` is what + /// was asked for, not where the page is yet. + private var isProvisional = false + + /// The navigation in progress failed, so it doesn't finish. + private var failed = false + + /// The guest asked for the next navigation itself (`load`, `loadHTML`, + /// `loadResource`, or a new window it agreed to), so it isn't asked + /// about it. + private var expectsOwnNavigation = false + + // MARK: The view + + /// Called with each frame while the page is attached, for the view to + /// show. + public var display: ((WebFrame) -> Void)? + + /// The view's size in points and its scale, while attached. + public private(set) var size: (width: UInt32, height: UInt32, scale: Float)? + + public var isAttached: Bool { size != nil } + + private var buttonsHeld: UInt32 = 0 + + private init(delegate: any WebContentDelegate, id: Int32) { + self.delegate = delegate + self.id = id + self.wpe = WPE.shared + slot = .allocate(capacity: 1) + slot.initialize(to: nil) + guard let wpe else { return } + LinuxWebKit.applyNetworkPolicy() + // WebKit destroys the exportable when it's done with the backend. + guard let exportable = wpe.exportableCreate(Self.client, slot, 800, 600), + let backend = wpe.exportableGetViewBackend(exportable), + let webBackend = wpe.webViewBackendNew(backend, unsafeBitCast(Self.onBackendDestroyed, to: Pointer.self), exportable), + let view = wpe.webViewNew(webBackend) else { + HostLog.warning("web: can't make a WPE web view") + return + } + self.exportable = exportable + self.backend = backend + self.view = view + slot.pointee = Unmanaged.passUnretained(self).toOpaque() + wpe.viewBackendAddActivityState(backend, WPE.inWindow) + let data = Unmanaged.passUnretained(self).toOpaque() + func connect(_ signal: String, _ callback: UnsafeRawPointer) { + handlers.append(wpe.signalConnectData(view, signal, callback, data, nil, 0)) + } + connect("load-changed", unsafeBitCast(Self.onLoadChanged, to: UnsafeRawPointer.self)) + connect("load-failed", unsafeBitCast(Self.onLoadFailed, to: UnsafeRawPointer.self)) + connect("load-failed-with-tls-errors", unsafeBitCast(Self.onTLSErrors, to: UnsafeRawPointer.self)) + connect("decide-policy", unsafeBitCast(Self.onDecidePolicy, to: UnsafeRawPointer.self)) + for property in ["title", "uri", "estimated-load-progress"] { + connect("notify::\(property)", unsafeBitCast(Self.onNotify, to: UnsafeRawPointer.self)) + } + } + + deinit { + if let wpe, let view { + for handler in handlers { + wpe.signalHandlerDisconnect(view, handler) + } + wpe.objectUnref(view) + } + // The slot stays, empty, for a frame still on its way. + slot.pointee = nil + let id = self.id + Self.pages[id] = nil + LinuxWebKit.pagesChanged() + } + + // MARK: WebContent + + public static func make(_ delegate: any WebContentDelegate) -> LinuxWebContent { + let page = LinuxWebContent(delegate: delegate, id: nextID) + nextID &+= 1 + pages[page.id] = Weak(page) + LinuxWebKit.pagesChanged() + return page + } + + public func identifier() -> Int32 { + id + } + + public func load(_ url: String) { + guard let scheme = Self.scheme(of: url) else { + delegate.didFailNavigation(self, error: WebError.Code.badURL.rawValue, message: "\(url) isn't a URL") + return + } + if Self.remoteSchemes.contains(scheme), !LinuxWebKit.hasNetwork { + delegate.didFailNavigation(self, error: WebError.Code.notPermitted.rawValue, message: Self.noNetwork) + return + } + guard let view, let wpe else { + return unavailable() + } + LinuxWebKit.applyNetworkPolicy() + startOwnNavigation() + wpe.webViewLoadURI(view, url) + } + + public func loadHTML(_ html: String, baseURL: String?) { + guard let view, let wpe else { + return unavailable() + } + startOwnNavigation() + if let baseURL { + wpe.webViewLoadHTML(view, html, baseURL) + } else { + wpe.webViewLoadHTML(view, html, nil) + } + } + + public func loadResource(_ path: String) { + guard let view, let wpe else { + return unavailable() + } + guard let file = Self.resourceFile(path) else { + delegate.didFailNavigation(self, error: WebError.Code.badURL.rawValue, message: "no resource \(path) in the app's bundle") + return + } + startOwnNavigation() + wpe.webViewLoadURI(view, URL(fileURLWithPath: file).absoluteString) + } + + public func reload() { + guard let wpe, let view else { return } + isProvisional = true + wpe.webViewReload(view) + } + + public func stopLoading() { + guard let wpe, let view else { return } + wpe.webViewStopLoading(view) + } + + /// WebKit moves its `uri` to the history item before asking about the + /// navigation, so it's provisional from here. + public func goBack() { + guard let wpe, let view, wpe.webViewCanGoBack(view) != 0 else { return } + isProvisional = true + wpe.webViewGoBack(view) + } + + public func goForward() { + guard let wpe, let view, wpe.webViewCanGoForward(view) != 0 else { return } + isProvisional = true + wpe.webViewGoForward(view) + } + + public func evaluateJavaScript(_ script: String, request: Int32) { + guard let wpe, let view else { + delegate.didEvaluateJavaScript(self, request: request, result: nil, error: Self.notInstalled) + return + } + let box = Unmanaged.passRetained(Evaluation(page: self, request: request)).toOpaque() + wpe.webViewEvaluateJavaScript(view, script, -1, nil, nil, nil, unsafeBitCast(Self.onEvaluated, to: Pointer.self), box) + } + + /// The guest's own load: it isn't asked about, and WebKit's `uri`, + /// which changes at once, isn't where the page is until it commits. + private func startOwnNavigation() { + expectsOwnNavigation = true + isProvisional = true + } + + private func unavailable() { + delegate.didFailNavigation(self, error: WebError.Code.unknown.rawValue, message: Self.notInstalled) + } + + static let notInstalled = "WPE WebKit isn't installed" + + static let noNetwork = "the app doesn't have the network capability" + + // MARK: Pages + + private struct Weak { + weak var page: LinuxWebContent? + init(_ page: LinuxWebContent) { self.page = page } + } + + nonisolated(unsafe) private static var pages: [Int32: Weak] = [:] + + nonisolated(unsafe) private static var nextID: Int32 = 1 + + /// The live page whose `identifier()` a `WebView` node carries. + public static func page(identifier: Int32) -> LinuxWebContent? { + pages[identifier]?.page + } + + static var count: Int { + pages.values.filter { $0.page != nil }.count + } + + // MARK: URLs + + /// Schemes that reach the network. + static let remoteSchemes: Set = ["http", "https", "ws", "wss", "ftp"] + + /// A URL's scheme, lowercased: letters, digits, `+`, `-` and `.` + /// before the first colon, starting with a letter. + static func scheme(of url: String) -> String? { + guard let colon = url.firstIndex(of: ":") else { return nil } + let scheme = url[.. String? { + guard let resources = LinuxBundle.resources else { return nil } + let root = URL(fileURLWithPath: resources).standardizedFileURL.path + let file = URL(fileURLWithPath: root).appendingPathComponent(path).standardizedFileURL.path + guard file.hasPrefix(root + "/") else { return nil } + var isDirectory: ObjCBool = false + guard FileManager.default.fileExists(atPath: file, isDirectory: &isDirectory), !isDirectory.boolValue else { + return nil + } + return file + } + + // MARK: Reporting + + /// Where the page is: not while a navigation is provisional, nor an + /// empty URI WebKit has after a failed first load. + private func reportURL() { + guard let wpe, let view, !isProvisional else { return } + let uri = wpe.webViewGetURI(view).map { String(cString: $0) } + let url = uri?.isEmpty == false ? uri : nil + guard url != reportedURL else { return } + reportedURL = url + delegate.didChangeURL(self, url: url) + reportHistory() + } + + private func reportTitle() { + guard let wpe, let view else { return } + let title = wpe.webViewGetTitle(view).map { String(cString: $0) } ?? "" + guard title != reportedTitle else { return } + reportedTitle = title + delegate.didChangeTitle(self, title: title) + } + + private func reportProgress() { + guard let wpe, let view, isLoading else { return } + delegate.didChangeLoading(self, isLoading: true, progress: wpe.webViewGetEstimatedLoadProgress(view)) + } + + private func reportHistory() { + guard let wpe, let view else { return } + let history = (canGoBack: wpe.webViewCanGoBack(view) != 0, canGoForward: wpe.webViewCanGoForward(view) != 0) + guard history != reportedHistory else { return } + reportedHistory = history + delegate.didChangeHistory(self, canGoBack: history.canGoBack, canGoForward: history.canGoForward) + } + + fileprivate func loadChanged(_ event: Int32) { + switch event { + case WPE.loadStarted: + failed = false + isLoading = true + isProvisional = true + reportProgress() + case WPE.loadRedirected: + // The redirect is followed: the page is going there. + isProvisional = false + reportURL() + isProvisional = true + case WPE.loadCommitted: + isProvisional = false + reportURL() + reportHistory() + case WPE.loadFinished: + isLoading = false + isProvisional = false + reportURL() + reportHistory() + delegate.didChangeLoading(self, isLoading: false, progress: 1) + if !failed { + delegate.didFinishNavigation(self) + } + failed = false + default: + break + } + } + + fileprivate func loadFailed(_ error: GError) { + HostLog.debug("web: page \(id) failed: \(error.domain) \(error.code) \(error.message)") + guard let code = Self.errorCode(error) else { + // Stopped, or replaced by another navigation: not a failure. + return + } + fail(code, error.message) + } + + fileprivate func fail(_ code: WebError.Code, _ message: String) { + failed = true + delegate.didFailNavigation(self, error: code.rawValue, message: message) + } + + /// `WebError.Code` for a load's GError; nil when it isn't a failure. + static func errorCode(_ error: GError) -> WebError.Code? { + switch error.domain { + case "WebKitNetworkError": + switch error.code { + case 302: return nil // cancelled + case 300: return .network // transport + case 301, 303: return .badURL // unknown protocol, no such file + default: return .unknown + } + case "WebKitPolicyError": + switch error.code { + case 102: return nil // interrupted by a policy change + case 101: return .badURL // can't show the URI + default: return .unknown + } + case "g-resolver-error-quark", "soup-session-error-quark": + return .network + case "g-io-error-quark": + return error.code == 24 ? .timedOut : .network + case "g-tls-error-quark": + return .security + default: + return .unknown + } + } + + /// Asks the delegate about a navigation the page or the user started, + /// and answers WebKit. + fileprivate func decide(_ decision: Pointer, type: Int32) -> Bool { + guard let wpe, type == WPE.navigationAction || type == WPE.newWindowAction, + let action = wpe.navigationPolicyDecisionGetNavigationAction(decision) else { + return false + } + let url = wpe.navigationActionGetRequest(action).flatMap(wpe.uriRequestGetURI).map { String(cString: $0) } ?? "" + let kind = Self.navigationKind(wpe.navigationActionGetNavigationType(action)) + if type == WPE.navigationAction, expectsOwnNavigation { + expectsOwnNavigation = false + wpe.policyDecisionUse(decision) + return true + } + if let scheme = Self.scheme(of: url), Self.remoteSchemes.contains(scheme), !LinuxWebKit.hasNetwork { + wpe.policyDecisionIgnore(decision) + delegate.didFailNavigation(self, error: WebError.Code.notPermitted.rawValue, message: Self.noNetwork) + return true + } + let allowed = delegate.shouldNavigate(self, url: url, kind: kind.rawValue, isMainFrame: true) + if type == WPE.newWindowAction { + // New windows load in this page. + wpe.policyDecisionIgnore(decision) + if allowed, let view { + startOwnNavigation() + wpe.webViewLoadURI(view, url) + } + } else if allowed { + isProvisional = true + wpe.policyDecisionUse(decision) + } else { + wpe.policyDecisionIgnore(decision) + } + return true + } + + /// `WebKitNavigationType` as `WebNavigationKind`. + static func navigationKind(_ type: Int32) -> WebNavigationKind { + switch type { + case 0: .linkActivated + case 1, 4: .formSubmitted // submitted, resubmitted + case 2: .backForward + case 3: .reload + default: .other + } + } + + // MARK: The view + + /// Puts the page on screen at a size in points: it's visible, draws at + /// that size, and sends its frames to `display`. + public func attach(width: UInt32, height: UInt32, scale: Float) { + guard let wpe, let backend else { return } + size = (max(width, 1), max(height, 1), scale) + wpe.viewBackendDispatchSetDeviceScaleFactor(backend, scale) + wpe.viewBackendDispatchSetSize(backend, max(width, 1), max(height, 1)) + wpe.viewBackendAddActivityState(backend, WPE.visible) + } + + public func resize(width: UInt32, height: UInt32, scale: Float) { + guard let wpe, let backend, let size else { return } + let width = max(width, 1), height = max(height, 1) + if scale != size.scale { + wpe.viewBackendDispatchSetDeviceScaleFactor(backend, scale) + } + if width != size.width || height != size.height { + wpe.viewBackendDispatchSetSize(backend, width, height) + } + self.size = (width, height, scale) + } + + /// The view is gone: the page stays loaded, hidden and unfocused. + public func detach() { + guard let wpe, let backend, isAttached else { return } + focus(false) + buttonsHeld = 0 + wpe.viewBackendRemoveActivityState(backend, WPE.visible) + size = nil + } + + fileprivate func receive(_ buffer: Pointer) { + guard let wpe, let exportable else { return } + defer { + wpe.exportableReleaseShmBuffer(exportable, buffer) + wpe.exportableFrameComplete(exportable) + } + guard let size, let display, let shm = wpe.shmExportedBufferGetShmBuffer(buffer) else { return } + wpe.shmBufferBeginAccess(shm) + defer { wpe.shmBufferEndAccess(shm) } + let width = Int(wpe.shmBufferGetWidth(shm)), height = Int(wpe.shmBufferGetHeight(shm)) + let stride = Int(wpe.shmBufferGetStride(shm)) + guard width > 0, height > 0, stride >= width * 4, let pixels = wpe.shmBufferGetData(shm) else { return } + let data = Self.rgba(fromBGRA: UnsafeRawPointer(pixels), width: width, height: height, stride: stride) + display(WebFrame(data: data, width: width, height: height, scale: size.scale)) + } + + /// Wayland's ARGB8888, premultiplied, blue first in memory, as RGBA. + static func rgba(fromBGRA pixels: UnsafeRawPointer, width: Int, height: Int, stride: Int) -> Data { + var data = Data(count: width * height * 4) + data.withUnsafeMutableBytes { (output: UnsafeMutableRawBufferPointer) in + let output = output.bindMemory(to: UInt8.self) + let input = pixels.assumingMemoryBound(to: UInt8.self) + for row in 0.. UInt32 { + UInt32(truncatingIfNeeded: MainLoop.now() / 1_000_000) + } + + // MARK: Callbacks from C + + /// `wpe_view_backend_exportable_fdo_client`: frames in shared memory. + nonisolated(unsafe) private static let client: UnsafeRawPointer = { + let client = UnsafeMutablePointer.allocate(capacity: 5) + client.initialize(repeating: nil, count: 5) + client[2] = unsafeBitCast(LinuxWebContent.onShmBuffer, to: UnsafeRawPointer.self) + return UnsafeRawPointer(client) + }() + + private static func page(_ data: Pointer?) -> LinuxWebContent? { + data.map { Unmanaged.fromOpaque($0).takeUnretainedValue() } + } + + private static let onShmBuffer: @convention(c) (Pointer?, Pointer?) -> Void = { data, buffer in + guard let buffer else { return } + guard let pointer = data?.assumingMemoryBound(to: Pointer?.self).pointee else { + return + } + page(pointer)?.receive(buffer) + } + + private static let onBackendDestroyed: @convention(c) (Pointer?) -> Void = { exportable in + if let exportable { + WPE.shared?.exportableDestroy(exportable) + } + } + + private static let onLoadChanged: @convention(c) (Pointer?, Int32, Pointer?) -> Void = { _, event, data in + page(data)?.loadChanged(event) + } + + /// load-failed: the view, the load event, the URI and the GError; + /// true, as handled. + private static let onLoadFailed: @convention(c) (Pointer?, Int32, UnsafePointer?, Pointer?, Pointer?) -> Int32 = { + _, _, _, error, data in + guard let page = page(data), let wpe = page.wpe else { return 1 } + page.loadFailed(error.map(wpe.error) ?? GError(domain: "", code: 0, message: "")) + return 1 + } + + /// load-failed-with-tls-errors: the view, the URI, the certificate and + /// its errors; true, as handled, so load-failed doesn't follow. + private static let onTLSErrors: @convention(c) (Pointer?, UnsafePointer?, Pointer?, UInt32, Pointer?) -> Int32 = { + _, uri, _, _, data in + let url = uri.map { String(cString: $0) } ?? "" + page(data)?.fail(.security, "the certificate for \(url) isn't trusted") + return 1 + } + + private static let onDecidePolicy: @convention(c) (Pointer?, Pointer?, Int32, Pointer?) -> Int32 = { _, decision, type, data in + guard let page = page(data), let decision else { return 0 } + return page.decide(decision, type: type) ? 1 : 0 + } + + private static let onNotify: @convention(c) (Pointer?, Pointer?, Pointer?) -> Void = { _, spec, data in + guard let page = page(data), let spec, let name = page.wpe?.paramSpecGetName(spec) else { return } + switch String(cString: name) { + case "title": page.reportTitle() + case "uri": page.reportURL() + case "estimated-load-progress": page.reportProgress() + default: break + } + } + + private final class Evaluation { + let page: LinuxWebContent + let request: Int32 + init(page: LinuxWebContent, request: Int32) { + self.page = page + self.request = request + } + } + + /// A script's value as JSON (nil for undefined and what JSON can't + /// hold), or the exception's message. + private static let onEvaluated: @convention(c) (Pointer?, Pointer?, Pointer?) -> Void = { source, result, data in + guard let data else { return } + let evaluation = Unmanaged.fromOpaque(data).takeRetainedValue() + let page = evaluation.page + guard let wpe = page.wpe else { return } + var error: Pointer? + guard let source, let result, let value = wpe.webViewEvaluateJavaScriptFinish(source, result, &error) else { + let message = error.map(wpe.error)?.message ?? "the script failed" + if let error { + wpe.errorFree(error) + } + page.delegate.didEvaluateJavaScript(page, request: evaluation.request, result: nil, error: message) + return + } + defer { wpe.objectUnref(value) } + var json: String? + if wpe.valueIsUndefined(value) == 0, let text = wpe.valueToJSON(value, 0) { + json = String(cString: text) + wpe.free(text) + } + page.delegate.didEvaluateJavaScript(page, request: evaluation.request, result: json, error: nil) + } +} diff --git a/Sources/LinuxWebKit/LinuxWebKit.swift b/Sources/LinuxWebKit/LinuxWebKit.swift new file mode 100644 index 0000000..c6c8847 --- /dev/null +++ b/Sources/LinuxWebKit/LinuxWebKit.swift @@ -0,0 +1,85 @@ +import DynamicRuntime +import DynamicWebKit +import LinuxHost +import RuntimeABI + +/// The web view contracts (`WebKit.md`, `Plan.md` §8): pages in WPE WebKit, +/// shown by the player's `WebView` view. Main loop only. +public enum LinuxWebKit: HostModule { + + public static var contracts: [ContractDescriptor] { + [WebContentProxy.descriptor, WebContentDelegateProxy.descriptor] + } + + public static func register(in registrar: some ContractRegistrar) { + WebContentProxy.register(LinuxWebContent.self, in: registrar) + WebContentDelegateProxy.registerInterface(in: registrar) + } + + /// Whether WPE WebKit loads. Without it a page loads nothing, and says + /// so. + public static var isAvailable: Bool { + WPE.shared != nil + } + + // MARK: The network + + /// In place of the app's capabilities, for tests: whether pages may + /// reach the network. + nonisolated(unsafe) public static var networkOverride: Bool? = nil { + didSet { appliedNetwork = nil } + } + + /// Whether pages may reach the network: the app declares `network`. + /// Without it, remote loads are refused, and the rest go through a + /// proxy that refuses them. + static var hasNetwork: Bool { + networkOverride ?? HostPermissions.declared.contains(.network) + } + + nonisolated(unsafe) private static var appliedNetwork: Bool? + + /// Sets WebKit's proxy to match `hasNetwork`, once per change. + static func applyNetworkPolicy() { + guard let wpe = WPE.shared else { return } + let allowed: Bool = hasNetwork + guard appliedNetwork != allowed else { return } + appliedNetwork = allowed + if allowed { + if let session = wpe.networkSessionGetDefault() { + wpe.networkSessionSetProxySettings(session, 0, nil) // the system's proxy + } + } else { + wpe.blockNetwork() + } + } + + // MARK: GLib's main context + + /// How often the main loop runs GLib's context while there are pages. + static let interval: Int64 = 8_000_000 + + nonisolated(unsafe) private static var timer: MainLoop.TimerToken? + + /// Runs GLib's context on the main loop while any page is alive. + static func pagesChanged() { + if LinuxWebContent.count == 0 { + if let timer { + MainLoop.shared.cancel(timer) + self.timer = nil + } + } else if timer == nil { + schedule() + } + } + + private static func schedule() { + timer = MainLoop.shared.schedule(after: interval) { + timer = nil + WPE.shared?.dispatch() + if LinuxWebContent.count > 0 { + schedule() + } + } + } +} diff --git a/Sources/LinuxWebKit/WPE.swift b/Sources/LinuxWebKit/WPE.swift new file mode 100644 index 0000000..64b180d --- /dev/null +++ b/Sources/LinuxWebKit/WPE.swift @@ -0,0 +1,294 @@ +#if canImport(Glibc) +import Glibc +#elseif canImport(Darwin) +import Darwin +#endif +import LinuxHost + +/// WPE WebKit, WebKit's port for embedding, opened at run time +/// (`Plan.md` §8), as Perry does. +/// +/// Each page is a `WebKitWebView` over a view backend from WPEBackend-fdo +/// in its shared-memory mode, which hands every frame the web process draws +/// to the host as a `wl_shm` buffer: no GL. Pages run in WPE's web and +/// network processes, and their signals and replies come through GLib's +/// main context, which the main loop runs while there are pages. Without +/// WPE installed, `WebContent` is unavailable. +final class WPE { + + typealias Pointer = UnsafeMutableRawPointer + + // MARK: libwpe and WPEBackend-fdo + + let viewBackendAddActivityState: @convention(c) (Pointer, UInt32) -> Void + let viewBackendRemoveActivityState: @convention(c) (Pointer, UInt32) -> Void + let viewBackendDispatchSetSize: @convention(c) (Pointer, UInt32, UInt32) -> Void + let viewBackendDispatchSetDeviceScaleFactor: @convention(c) (Pointer, Float) -> Void + let viewBackendDispatchPointerEvent: @convention(c) (Pointer, UnsafeMutableRawPointer) -> Void + let viewBackendDispatchAxisEvent: @convention(c) (Pointer, UnsafeMutableRawPointer) -> Void + let viewBackendDispatchKeyboardEvent: @convention(c) (Pointer, UnsafeMutableRawPointer) -> Void + let exportableCreate: @convention(c) (UnsafeRawPointer, Pointer?, UInt32, UInt32) -> Pointer? + let exportableDestroy: @convention(c) (Pointer) -> Void + let exportableGetViewBackend: @convention(c) (Pointer) -> Pointer? + let exportableFrameComplete: @convention(c) (Pointer) -> Void + let exportableReleaseShmBuffer: @convention(c) (Pointer, Pointer) -> Void + let shmExportedBufferGetShmBuffer: @convention(c) (Pointer) -> Pointer? + + // MARK: libwayland-server's shared-memory buffers + + let shmBufferGetData: @convention(c) (Pointer) -> Pointer? + let shmBufferGetWidth: @convention(c) (Pointer) -> Int32 + let shmBufferGetHeight: @convention(c) (Pointer) -> Int32 + let shmBufferGetStride: @convention(c) (Pointer) -> Int32 + let shmBufferBeginAccess: @convention(c) (Pointer) -> Void + let shmBufferEndAccess: @convention(c) (Pointer) -> Void + + // MARK: WPE WebKit + + let webViewBackendNew: @convention(c) (Pointer, Pointer?, Pointer?) -> Pointer? + let webViewNew: @convention(c) (Pointer) -> Pointer? + let webViewLoadHTML: @convention(c) (Pointer, UnsafePointer, UnsafePointer?) -> Void + let webViewLoadURI: @convention(c) (Pointer, UnsafePointer) -> Void + let webViewGetTitle: @convention(c) (Pointer) -> UnsafePointer? + let webViewGetURI: @convention(c) (Pointer) -> UnsafePointer? + let webViewIsLoading: @convention(c) (Pointer) -> Int32 + let webViewGetEstimatedLoadProgress: @convention(c) (Pointer) -> Double + let webViewCanGoBack: @convention(c) (Pointer) -> Int32 + let webViewCanGoForward: @convention(c) (Pointer) -> Int32 + let webViewGoBack: @convention(c) (Pointer) -> Void + let webViewGoForward: @convention(c) (Pointer) -> Void + let webViewReload: @convention(c) (Pointer) -> Void + let webViewStopLoading: @convention(c) (Pointer) -> Void + let webViewGetSettings: @convention(c) (Pointer) -> Pointer? + let settingsSetAllowFileAccessFromFileURLs: @convention(c) (Pointer, Int32) -> Void + let webViewEvaluateJavaScript: @convention(c) (Pointer, UnsafePointer, Int, UnsafePointer?, + UnsafePointer?, Pointer?, Pointer?, Pointer?) -> Void + let webViewEvaluateJavaScriptFinish: @convention(c) (Pointer, Pointer, UnsafeMutablePointer) -> Pointer? + let valueIsUndefined: @convention(c) (Pointer) -> Int32 + let valueToJSON: @convention(c) (Pointer, UInt32) -> UnsafeMutablePointer? + let navigationPolicyDecisionGetNavigationAction: @convention(c) (Pointer) -> Pointer? + let navigationActionGetRequest: @convention(c) (Pointer) -> Pointer? + let navigationActionGetNavigationType: @convention(c) (Pointer) -> Int32 + let uriRequestGetURI: @convention(c) (Pointer) -> UnsafePointer? + let policyDecisionUse: @convention(c) (Pointer) -> Void + let policyDecisionIgnore: @convention(c) (Pointer) -> Void + let networkSessionGetDefault: @convention(c) () -> Pointer? + let networkSessionSetProxySettings: @convention(c) (Pointer, Int32, Pointer?) -> Void + let networkProxySettingsNew: @convention(c) (UnsafePointer?, UnsafePointer?>?) -> Pointer? + let networkProxySettingsFree: @convention(c) (Pointer) -> Void + + // MARK: GLib and GObject + + let mainContextIteration: @convention(c) (Pointer?, Int32) -> Int32 + let signalConnectData: @convention(c) (Pointer, UnsafePointer, UnsafeRawPointer, Pointer?, Pointer?, Int32) -> UInt + let signalHandlerDisconnect: @convention(c) (Pointer, UInt) -> Void + let objectUnref: @convention(c) (Pointer) -> Void + let free: @convention(c) (Pointer?) -> Void + let errorFree: @convention(c) (Pointer) -> Void + let quarkToString: @convention(c) (UInt32) -> UnsafePointer? + let paramSpecGetName: @convention(c) (Pointer) -> UnsafePointer? + + // MARK: Constants + + // WebKitLoadEvent. + static let loadStarted: Int32 = 0, loadRedirected: Int32 = 1, loadCommitted: Int32 = 2, loadFinished: Int32 = 3 + // WebKitPolicyDecisionType. + static let navigationAction: Int32 = 0, newWindowAction: Int32 = 1 + /// wpe_view_activity_state. + static let visible: UInt32 = 1, focused: UInt32 = 2, inWindow: UInt32 = 4 + /// WEBKIT_NETWORK_PROXY_MODE_CUSTOM. + static let proxyModeCustom: Int32 = 2 + + /// WPE, initialized; nil when it isn't installed. + static let shared: WPE? = WPE() + + private init?() { + let names = ["libwpe-1.0.so.1", "libWPEBackend-fdo-1.0.so.1", "libwayland-server.so.0", "libWPEWebKit-2.0.so.1", + "libglib-2.0.so.0", "libgobject-2.0.so.0"] + // Local, so WPE's libraries never stand in for others of the same + // names in the process. + let libraries = names.map { dlopen($0, RTLD_NOW | RTLD_LOCAL) } + guard let wpe = libraries[0], let fdo = libraries[1], let wayland = libraries[2], let webkit = libraries[3], + let glib = libraries[4], let gobject = libraries[5] else { + HostLog.debug("web: WPE WebKit isn't installed") + return nil + } + var missing: [String] = [] + func bind(_ library: Pointer, _ name: String, _: T.Type) -> T { + guard let symbol = dlsym(library, name) else { + missing.append(name) + return unsafeBitCast(WPE.missingFunction, to: T.self) + } + return unsafeBitCast(symbol, to: T.self) + } + let loaderInit = bind(wpe, "wpe_loader_init", (@convention(c) (UnsafePointer) -> Int32).self) + let initializeShm = bind(fdo, "wpe_fdo_initialize_shm", (@convention(c) () -> Int32).self) + viewBackendAddActivityState = bind(wpe, "wpe_view_backend_add_activity_state", (@convention(c) (Pointer, UInt32) -> Void).self) + viewBackendRemoveActivityState = bind(wpe, "wpe_view_backend_remove_activity_state", (@convention(c) (Pointer, UInt32) -> Void).self) + viewBackendDispatchSetSize = bind(wpe, "wpe_view_backend_dispatch_set_size", (@convention(c) (Pointer, UInt32, UInt32) -> Void).self) + viewBackendDispatchSetDeviceScaleFactor = bind(wpe, "wpe_view_backend_dispatch_set_device_scale_factor", + (@convention(c) (Pointer, Float) -> Void).self) + viewBackendDispatchPointerEvent = bind(wpe, "wpe_view_backend_dispatch_pointer_event", + (@convention(c) (Pointer, UnsafeMutableRawPointer) -> Void).self) + viewBackendDispatchAxisEvent = bind(wpe, "wpe_view_backend_dispatch_axis_event", + (@convention(c) (Pointer, UnsafeMutableRawPointer) -> Void).self) + viewBackendDispatchKeyboardEvent = bind(wpe, "wpe_view_backend_dispatch_keyboard_event", + (@convention(c) (Pointer, UnsafeMutableRawPointer) -> Void).self) + exportableCreate = bind(fdo, "wpe_view_backend_exportable_fdo_create", + (@convention(c) (UnsafeRawPointer, Pointer?, UInt32, UInt32) -> Pointer?).self) + exportableDestroy = bind(fdo, "wpe_view_backend_exportable_fdo_destroy", (@convention(c) (Pointer) -> Void).self) + exportableGetViewBackend = bind(fdo, "wpe_view_backend_exportable_fdo_get_view_backend", (@convention(c) (Pointer) -> Pointer?).self) + exportableFrameComplete = bind(fdo, "wpe_view_backend_exportable_fdo_dispatch_frame_complete", (@convention(c) (Pointer) -> Void).self) + exportableReleaseShmBuffer = bind(fdo, "wpe_view_backend_exportable_fdo_dispatch_release_shm_exported_buffer", + (@convention(c) (Pointer, Pointer) -> Void).self) + shmExportedBufferGetShmBuffer = bind(fdo, "wpe_fdo_shm_exported_buffer_get_shm_buffer", (@convention(c) (Pointer) -> Pointer?).self) + shmBufferGetData = bind(wayland, "wl_shm_buffer_get_data", (@convention(c) (Pointer) -> Pointer?).self) + shmBufferGetWidth = bind(wayland, "wl_shm_buffer_get_width", (@convention(c) (Pointer) -> Int32).self) + shmBufferGetHeight = bind(wayland, "wl_shm_buffer_get_height", (@convention(c) (Pointer) -> Int32).self) + shmBufferGetStride = bind(wayland, "wl_shm_buffer_get_stride", (@convention(c) (Pointer) -> Int32).self) + shmBufferBeginAccess = bind(wayland, "wl_shm_buffer_begin_access", (@convention(c) (Pointer) -> Void).self) + shmBufferEndAccess = bind(wayland, "wl_shm_buffer_end_access", (@convention(c) (Pointer) -> Void).self) + webViewBackendNew = bind(webkit, "webkit_web_view_backend_new", (@convention(c) (Pointer, Pointer?, Pointer?) -> Pointer?).self) + webViewNew = bind(webkit, "webkit_web_view_new", (@convention(c) (Pointer) -> Pointer?).self) + webViewLoadHTML = bind(webkit, "webkit_web_view_load_html", + (@convention(c) (Pointer, UnsafePointer, UnsafePointer?) -> Void).self) + webViewLoadURI = bind(webkit, "webkit_web_view_load_uri", (@convention(c) (Pointer, UnsafePointer) -> Void).self) + webViewGetTitle = bind(webkit, "webkit_web_view_get_title", (@convention(c) (Pointer) -> UnsafePointer?).self) + webViewGetURI = bind(webkit, "webkit_web_view_get_uri", (@convention(c) (Pointer) -> UnsafePointer?).self) + webViewIsLoading = bind(webkit, "webkit_web_view_is_loading", (@convention(c) (Pointer) -> Int32).self) + webViewGetEstimatedLoadProgress = bind(webkit, "webkit_web_view_get_estimated_load_progress", (@convention(c) (Pointer) -> Double).self) + webViewCanGoBack = bind(webkit, "webkit_web_view_can_go_back", (@convention(c) (Pointer) -> Int32).self) + webViewCanGoForward = bind(webkit, "webkit_web_view_can_go_forward", (@convention(c) (Pointer) -> Int32).self) + webViewGoBack = bind(webkit, "webkit_web_view_go_back", (@convention(c) (Pointer) -> Void).self) + webViewGoForward = bind(webkit, "webkit_web_view_go_forward", (@convention(c) (Pointer) -> Void).self) + webViewReload = bind(webkit, "webkit_web_view_reload", (@convention(c) (Pointer) -> Void).self) + webViewStopLoading = bind(webkit, "webkit_web_view_stop_loading", (@convention(c) (Pointer) -> Void).self) + webViewGetSettings = bind(webkit, "webkit_web_view_get_settings", (@convention(c) (Pointer) -> Pointer?).self) + settingsSetAllowFileAccessFromFileURLs = bind(webkit, "webkit_settings_set_allow_file_access_from_file_urls", + (@convention(c) (Pointer, Int32) -> Void).self) + webViewEvaluateJavaScript = bind(webkit, "webkit_web_view_evaluate_javascript", + (@convention(c) (Pointer, UnsafePointer, Int, UnsafePointer?, + UnsafePointer?, Pointer?, Pointer?, Pointer?) -> Void).self) + webViewEvaluateJavaScriptFinish = bind(webkit, "webkit_web_view_evaluate_javascript_finish", + (@convention(c) (Pointer, Pointer, UnsafeMutablePointer) -> Pointer?).self) + valueIsUndefined = bind(webkit, "jsc_value_is_undefined", (@convention(c) (Pointer) -> Int32).self) + valueToJSON = bind(webkit, "jsc_value_to_json", (@convention(c) (Pointer, UInt32) -> UnsafeMutablePointer?).self) + navigationPolicyDecisionGetNavigationAction = bind(webkit, "webkit_navigation_policy_decision_get_navigation_action", + (@convention(c) (Pointer) -> Pointer?).self) + navigationActionGetRequest = bind(webkit, "webkit_navigation_action_get_request", (@convention(c) (Pointer) -> Pointer?).self) + navigationActionGetNavigationType = bind(webkit, "webkit_navigation_action_get_navigation_type", + (@convention(c) (Pointer) -> Int32).self) + uriRequestGetURI = bind(webkit, "webkit_uri_request_get_uri", (@convention(c) (Pointer) -> UnsafePointer?).self) + policyDecisionUse = bind(webkit, "webkit_policy_decision_use", (@convention(c) (Pointer) -> Void).self) + policyDecisionIgnore = bind(webkit, "webkit_policy_decision_ignore", (@convention(c) (Pointer) -> Void).self) + networkSessionGetDefault = bind(webkit, "webkit_network_session_get_default", (@convention(c) () -> Pointer?).self) + networkSessionSetProxySettings = bind(webkit, "webkit_network_session_set_proxy_settings", + (@convention(c) (Pointer, Int32, Pointer?) -> Void).self) + networkProxySettingsNew = bind(webkit, "webkit_network_proxy_settings_new", + (@convention(c) (UnsafePointer?, UnsafePointer?>?) -> Pointer?).self) + networkProxySettingsFree = bind(webkit, "webkit_network_proxy_settings_free", (@convention(c) (Pointer) -> Void).self) + mainContextIteration = bind(glib, "g_main_context_iteration", (@convention(c) (Pointer?, Int32) -> Int32).self) + signalConnectData = bind(gobject, "g_signal_connect_data", + (@convention(c) (Pointer, UnsafePointer, UnsafeRawPointer, Pointer?, Pointer?, Int32) -> UInt).self) + signalHandlerDisconnect = bind(gobject, "g_signal_handler_disconnect", (@convention(c) (Pointer, UInt) -> Void).self) + objectUnref = bind(gobject, "g_object_unref", (@convention(c) (Pointer) -> Void).self) + free = bind(glib, "g_free", (@convention(c) (Pointer?) -> Void).self) + errorFree = bind(glib, "g_error_free", (@convention(c) (Pointer) -> Void).self) + quarkToString = bind(glib, "g_quark_to_string", (@convention(c) (UInt32) -> UnsafePointer?).self) + paramSpecGetName = bind(gobject, "g_param_spec_get_name", (@convention(c) (Pointer) -> UnsafePointer?).self) + guard missing.isEmpty else { + HostLog.warning("web: WPE WebKit is incomplete (no \(missing.joined(separator: ", ")))") + return nil + } + guard loaderInit("libWPEBackend-fdo-1.0.so.1") != 0, initializeShm() != 0 else { + HostLog.warning("web: WPEBackend-fdo didn't initialize") + return nil + } + } + + private static let missingFunction: @convention(c) () -> Void = {} + + /// Runs what GLib's main context has ready: WPE's signals, replies and + /// frames. + func dispatch() { + var budget = 1000 + while budget > 0, mainContextIteration(nil, 0) != 0 { + budget -= 1 + } + } + + /// A GError's domain, code and message. + func error(_ error: Pointer) -> GError { + let domain = quarkToString(error.load(as: UInt32.self)).map { String(cString: $0) } ?? "" + let code = error.load(fromByteOffset: 4, as: Int32.self) + let message = error.load(fromByteOffset: 8, as: UnsafePointer?.self).map { String(cString: $0) } ?? "" + return GError(domain: domain, code: code, message: message) + } + + /// Sends every page's remote loads to a proxy that refuses them: the + /// app has no `network` capability (`WebKit.md`, "Without network"). + func blockNetwork() { + guard let session = networkSessionGetDefault(), + let settings = networkProxySettingsNew(WPE.refusingProxy, nil) else { return } + networkSessionSetProxySettings(session, WPE.proxyModeCustom, settings) + networkProxySettingsFree(settings) + } + + /// A proxy on a host that never resolves (`.invalid`, RFC 2606), so + /// nothing can answer for it. + static let refusingProxy = "http://no-network.invalid:8080" +} + +/// A GError's fields. +struct GError: Equatable { + var domain: String + var code: Int32 + var message: String +} + +// MARK: - libwpe's input events + +/// `struct wpe_input_pointer_event`. The event structs are laid out as +/// libwpe's, and passed to it by address. +struct PointerEvent { + /// `wpe_input_pointer_event_type`: 1 motion, 2 button. + var type: UInt32 + var time: UInt32 + var x: Int32 + var y: Int32 + var button: UInt32 + var state: UInt32 + var modifiers: UInt32 +} + +/// `struct wpe_input_axis_event`. +struct AxisEvent { + /// `wpe_input_axis_event_type`: 1 motion. + var type: UInt32 + var time: UInt32 + var x: Int32 + var y: Int32 + /// 0 vertical, 1 horizontal. + var axis: UInt32 + var value: Int32 + var modifiers: UInt32 +} + +/// `struct wpe_input_keyboard_event`. +struct KeyboardEvent { + var time: UInt32 + /// An XKB keysym. + var keyCode: UInt32 + var hardwareKeyCode: UInt32 + var pressed: Bool + var modifiers: UInt32 +} + +/// `wpe_input_modifier`. +enum WPEModifier { + static let control: UInt32 = 1 << 0 + static let shift: UInt32 = 1 << 1 + static let alt: UInt32 = 1 << 2 + static let meta: UInt32 = 1 << 3 + static let button1: UInt32 = 1 << 20 +} diff --git a/Tests/LinuxWebKitTests/WebContentTests.swift b/Tests/LinuxWebKitTests/WebContentTests.swift new file mode 100644 index 0000000..1e248af --- /dev/null +++ b/Tests/LinuxWebKitTests/WebContentTests.swift @@ -0,0 +1,363 @@ +import DynamicWebKit +import Foundation +import LinuxHost +import Testing +@testable import LinuxWebKit + +/// Records what a page tells its delegate, and answers `shouldNavigate`. +final class RecordingDelegate: WebContentDelegate { + + var events: [String] = [] + + var allows = true + + var scripts: [Int32: (result: String?, error: String?)] = [:] + + func shouldNavigate(_ content: WebContent, url: String, kind: Int32, isMainFrame: Bool) -> Bool { + events.append("should \(kind) \(url)") + return allows + } + + func didChangeURL(_ content: WebContent, url: String?) { + events.append("url \(url ?? "nil")") + } + + func didChangeTitle(_ content: WebContent, title: String) { + events.append("title \(title)") + } + + func didChangeLoading(_ content: WebContent, isLoading: Bool, progress: Double) { + events.append(isLoading ? "loading" : "loaded \(progress)") + } + + func didChangeHistory(_ content: WebContent, canGoBack: Bool, canGoForward: Bool) { + events.append("history \(canGoBack) \(canGoForward)") + } + + func didFinishNavigation(_ content: WebContent) { + events.append("finish") + } + + func didFailNavigation(_ content: WebContent, error: Int32, message: String) { + events.append("fail \(error)") + } + + func didEvaluateJavaScript(_ content: WebContent, request: Int32, result: String?, error: String?) { + scripts[request] = (result, error) + } + + var finished: Bool { events.contains("finish") } + + var title: String? { + events.last { $0.hasPrefix("title ") }.map { String($0.dropFirst(6)) } + } +} + +/// WebKit's main thread is the first that calls it, so every test does its +/// WebKit work on this one thread. +final class WebThread: @unchecked Sendable { + + static let shared = WebThread() + + private let condition = NSCondition() + + private var jobs: [() -> Void] = [] + + private init() { + let thread = Thread { [unowned self] in + while true { + condition.lock() + while jobs.isEmpty { + condition.wait() + } + let job = jobs.removeFirst() + condition.unlock() + job() + } + } + thread.start() + } + + /// Runs `body` on the thread and waits for it. + func run(_ body: @escaping () throws -> T) throws -> T { + let done = DispatchSemaphore(value: 0) + nonisolated(unsafe) var outcome: Result! + condition.lock() + jobs.append { + outcome = Result { try body() } + done.signal() + } + condition.signal() + condition.unlock() + done.wait() + return try outcome.get() + } +} + +/// Runs GLib's context until `condition` holds, or fails after `seconds`. +func wait(_ seconds: Double = 20, until condition: () -> Bool) -> Bool { + let deadline = Date().addingTimeInterval(seconds) + while !condition() { + if Date() > deadline { + return false + } + WPE.shared?.dispatch() + usleep(5_000) + } + return true +} + +/// Pages share WebKit's process and its network session: one at a time. +@Suite(.serialized) struct WebContentTests { + + static var hasWPE: Bool { + (try? WebThread.shared.run { WPE.shared != nil }) ?? false + } + + // MARK: Without WPE + + @Test func schemes() { + #expect(LinuxWebContent.scheme(of: "https://example.com") == "https") + #expect(LinuxWebContent.scheme(of: "HTTP://x") == "http") + #expect(LinuxWebContent.scheme(of: "data:text/html,hi") == "data") + #expect(LinuxWebContent.scheme(of: "about:blank") == "about") + #expect(LinuxWebContent.scheme(of: "example.com") == nil) + #expect(LinuxWebContent.scheme(of: "1http://x") == nil) + #expect(LinuxWebContent.scheme(of: "/path:with/colon") == nil) + } + + @Test func errors() { + #expect(LinuxWebContent.errorCode(GError(domain: "WebKitNetworkError", code: 302, message: "")) == nil) + #expect(LinuxWebContent.errorCode(GError(domain: "WebKitPolicyError", code: 102, message: "")) == nil) + #expect(LinuxWebContent.errorCode(GError(domain: "WebKitNetworkError", code: 300, message: "")) == .network) + #expect(LinuxWebContent.errorCode(GError(domain: "WebKitNetworkError", code: 303, message: "")) == .badURL) + #expect(LinuxWebContent.errorCode(GError(domain: "g-resolver-error-quark", code: 1, message: "")) == .network) + #expect(LinuxWebContent.errorCode(GError(domain: "g-io-error-quark", code: 24, message: "")) == .timedOut) + #expect(LinuxWebContent.errorCode(GError(domain: "g-io-error-quark", code: 39, message: "")) == .network) + #expect(LinuxWebContent.errorCode(GError(domain: "g-tls-error-quark", code: 3, message: "")) == .security) + #expect(LinuxWebContent.errorCode(GError(domain: "other", code: 1, message: "")) == .unknown) + } + + @Test func navigationKinds() { + #expect(LinuxWebContent.navigationKind(0) == .linkActivated) + #expect(LinuxWebContent.navigationKind(1) == .formSubmitted) + #expect(LinuxWebContent.navigationKind(4) == .formSubmitted) + #expect(LinuxWebContent.navigationKind(2) == .backForward) + #expect(LinuxWebContent.navigationKind(3) == .reload) + #expect(LinuxWebContent.navigationKind(5) == .other) + } + + @Test func pixelsTurnRGBA() { + let bgra: [UInt8] = [10, 20, 30, 255, 1, 2, 3, 4, 99, 99, 99, 99] + let data = bgra.withUnsafeBytes { LinuxWebContent.rgba(fromBGRA: $0.baseAddress!, width: 2, height: 1, stride: 12) } + #expect(Array(data) == [30, 20, 10, 255, 3, 2, 1, 4]) + } + + @Test func resourcesStayInTheBundle() throws { + let root = FileManager.default.temporaryDirectory.appendingPathComponent("web-resources-\(UUID())") + try FileManager.default.createDirectory(at: root.appendingPathComponent("help"), withIntermediateDirectories: true) + try Data("Help".utf8).write(to: root.appendingPathComponent("help/index.html")) + defer { try? FileManager.default.removeItem(at: root) } + LinuxBundle.resources = root.path + defer { LinuxBundle.resources = nil } + #expect(LinuxWebContent.resourceFile("help/index.html")?.hasSuffix("/help/index.html") == true) + #expect(LinuxWebContent.resourceFile("help") == nil) + #expect(LinuxWebContent.resourceFile("missing.html") == nil) + #expect(LinuxWebContent.resourceFile("../etc/passwd") == nil) + } + + @Test func remoteLoadsNeedTheNetworkCapability() throws { + try WebThread.shared.run { + LinuxWebKit.networkOverride = false + defer { LinuxWebKit.networkOverride = nil } + let delegate = RecordingDelegate() + let page = LinuxWebContent.make(delegate) + page.load("https://example.com/") + // Refused before anything starts, with WPE or without. + #expect(delegate.events == ["fail \(WebError.Code.notPermitted.rawValue)"]) + page.load("example.com") + #expect(delegate.events.last == "fail \(WebError.Code.badURL.rawValue)") + } + } + + // MARK: In WPE + + @Test(.enabled(if: WebContentTests.hasWPE)) func loadsHTMLAndReports() throws { + try WebThread.shared.run { + let delegate = RecordingDelegate() + let page = LinuxWebContent.make(delegate) + page.loadHTML("HelloHi", baseURL: "https://example.com/") + #expect(wait { delegate.finished && delegate.title == "Hello" }) + #expect(delegate.events.first == "loading") + // The URL comes when the page commits, after loading starts. + #expect(delegate.events.contains("url https://example.com/")) + let loading = delegate.events.filter { $0 == "loading" || $0.hasPrefix("loaded") || $0 == "finish" } + #expect(Array(loading.suffix(2)) == ["loaded 1.0", "finish"]) + // The guest's own load isn't asked about. + #expect(!delegate.events.contains { $0.hasPrefix("should") }) + withExtendedLifetime(page) { } + } + } + + @Test(.enabled(if: WebContentTests.hasWPE)) func evaluatesJavaScript() throws { + try WebThread.shared.run { + let delegate = RecordingDelegate() + let page = LinuxWebContent.make(delegate) + page.loadHTML("Example", baseURL: nil) + #expect(wait { delegate.finished }) + page.evaluateJavaScript("1 + 1", request: 1) + page.evaluateJavaScript("document.title", request: 2) + page.evaluateJavaScript("[1, 'two']", request: 3) + page.evaluateJavaScript("undefined", request: 4) + page.evaluateJavaScript("throw new Error('nope')", request: 5) + #expect(wait { delegate.scripts.count == 5 }) + #expect(delegate.scripts[1]?.result == "2") + #expect(delegate.scripts[2]?.result == "\"Example\"") + #expect(delegate.scripts[3]?.result == "[1,\"two\"]") + #expect(delegate.scripts[4].map { $0.result == nil && $0.error == nil } == true) + #expect(delegate.scripts[5]?.result == nil) + #expect(delegate.scripts[5]?.error?.contains("nope") == true) + withExtendedLifetime(page) { } + } + } + + @Test(.enabled(if: WebContentTests.hasWPE)) func thePagesNavigationsAskTheDelegate() throws { + let root = try Self.bundle(["one.html": "One", "two.html": "Two"]) + defer { try? FileManager.default.removeItem(at: root) } + try WebThread.shared.run { + LinuxBundle.resources = root.path + defer { LinuxBundle.resources = nil } + let delegate = RecordingDelegate() + let page = LinuxWebContent.make(delegate) + page.loadResource("one.html") + #expect(wait { delegate.finished && delegate.title == "One" }) + delegate.events.removeAll() + + delegate.allows = false + page.evaluateJavaScript("location.href = 'two.html'", request: 1) + #expect(wait { delegate.events.contains { $0.hasPrefix("should") } }) + let asked = try #require(delegate.events.first) + #expect(asked.hasPrefix("should \(WebNavigationKind.other.rawValue) file://") && asked.hasSuffix("/two.html")) + #expect(!wait(1) { delegate.title == "Two" }) + #expect(!delegate.events.contains { $0.hasPrefix("fail") }) + + delegate.allows = true + delegate.events.removeAll() + page.evaluateJavaScript("location.href = 'two.html'", request: 2) + #expect(wait { delegate.finished && delegate.title == "Two" }) + #expect(delegate.events.contains("history true false")) + + // Back is asked about too, and goes back. + delegate.events.removeAll() + page.goBack() + #expect(wait { delegate.finished && delegate.title == "One" }) + let back = try #require(delegate.events.first, "\(delegate.events)") + #expect(back.hasPrefix("should \(WebNavigationKind.backForward.rawValue) file://") && back.hasSuffix("/one.html"), + "\(delegate.events)") + #expect(delegate.events.contains("history false true")) + withExtendedLifetime(page) { } + } + } + + /// A temporary bundle's resources: file names and contents. + static func bundle(_ files: [String: String]) throws -> URL { + let root = FileManager.default.temporaryDirectory.appendingPathComponent("web-bundle-\(UUID())") + try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true) + for (name, contents) in files { + try Data(contents.utf8).write(to: root.appendingPathComponent(name)) + } + return root + } + + @Test(.enabled(if: WebContentTests.hasWPE)) func aRefusedConnectionIsANetworkError() throws { + try WebThread.shared.run { + LinuxWebKit.networkOverride = true + defer { LinuxWebKit.networkOverride = nil } + let delegate = RecordingDelegate() + let page = LinuxWebContent.make(delegate) + // A closed port (9, discard, is one WebKit won't use at all). + page.load("http://127.0.0.1:54321/") + #expect(wait { delegate.events.contains { $0.hasPrefix("fail") } && delegate.events.last == "loaded 1.0" }) + #expect(delegate.events.contains("fail \(WebError.Code.network.rawValue)")) + #expect(!delegate.finished) + withExtendedLifetime(page) { } + } + } + + @Test(.enabled(if: WebContentTests.hasWPE)) func loadsBundleResources() throws { + let root = FileManager.default.temporaryDirectory.appendingPathComponent("web-bundle-\(UUID())") + try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true) + try Data("Bundled".utf8).write(to: root.appendingPathComponent("index.html")) + defer { try? FileManager.default.removeItem(at: root) } + try WebThread.shared.run { + LinuxBundle.resources = root.path + defer { LinuxBundle.resources = nil } + let delegate = RecordingDelegate() + let page = LinuxWebContent.make(delegate) + page.loadResource("index.html") + #expect(wait { delegate.finished }) + #expect(delegate.title == "Bundled") + #expect(delegate.events.contains { $0.hasPrefix("url file://") && $0.hasSuffix("/index.html") }) + page.loadResource("missing.html") + #expect(delegate.events.last == "fail \(WebError.Code.badURL.rawValue)") + withExtendedLifetime(page) { } + } + } + + @Test(.enabled(if: WebContentTests.hasWPE)) func drawsFramesAndTakesInput() throws { + try WebThread.shared.run { + let delegate = RecordingDelegate() + let page = LinuxWebContent.make(delegate) + var frame: WebFrame? + page.display = { frame = $0 } + page.attach(width: 160, height: 120, scale: 1) + page.loadHTML(""" + Start + + + + """, baseURL: nil) + // The background, below the button, once drawn: #22aa66. + let offset = (100 * 160 + 140) * 4 + func green() -> Bool { + guard let frame, frame.data.count == 160 * 120 * 4 else { return false } + return Array(frame.data[offset...size == 28) + #expect(MemoryLayout.size == 28) + #expect(MemoryLayout.stride == 20) + #expect(MemoryLayout.offset(of: \.pressed) == 12) + #expect(MemoryLayout.offset(of: \.modifiers) == 16) +}