diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..f13a0147 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,8 @@ +# Text is stored and checked out with LF on every platform. The shared +# JavaScript build scripts match line ends with "\n", and the parity trees are +# compared byte for byte, so a CRLF checkout on Windows breaks both. +* text=auto eol=lf + +# Windows-only scripts that need CRLF. +*.bat text eol=crlf +*.cmd text eol=crlf diff --git a/.github/workflows/android.yml b/.github/workflows/android.yml index 705097b1..747c7eea 100644 --- a/.github/workflows/android.yml +++ b/.github/workflows/android.yml @@ -29,3 +29,45 @@ jobs: working-directory: Android # Device tests (QuickJS parity with JavaScriptCore, rendering, native APIs) # need a device or emulator: run Android/scripts/device-test.sh locally. + # The core instrumented suite (QuickJS parity with JavaScriptCore, rendering, native APIs) on an + # emulator. Started by hand only: Actions > Android > Run workflow. An emulator is not a phone; + # the suites on a real device are still run with Android/scripts/device-test.sh. + device: + name: Device tests on an emulator + if: github.event_name == 'workflow_dispatch' + runs-on: ubuntu-latest + timeout-minutes: 60 + steps: + - uses: actions/checkout@v7 + - uses: actions/setup-node@v7 + with: {node-version: 24} + - uses: actions/setup-java@v5 + with: {distribution: temurin, java-version: 21} + - name: Install the bundler used for the shared renderer + run: npm ci + working-directory: Web/studio + - name: Shared JavaScript and the parity corpus the device tests read + run: node Android/scripts/build-shared-js.mjs + - name: Let the runner use KVM + run: | + echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules + sudo udevadm control --reload-rules + sudo udevadm trigger --name-match=kvm + - uses: reactivecircus/android-emulator-runner@v2 + with: + api-level: 35 + arch: x86_64 + target: google_apis + disable-animations: false + script: | + sh Android/scripts/device-test.sh core | tee device-test.log + FLAVOR=foss sh Android/scripts/device-test.sh app | tee app-device-test.log + grep -q "^OK (" device-test.log && grep -q "^OK (" app-device-test.log + - uses: actions/upload-artifact@v7 + if: always() + with: + name: android-core-device-test + path: | + device-test.log + app-device-test.log + if-no-files-found: ignore diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml new file mode 100644 index 00000000..e47c6507 --- /dev/null +++ b/.github/workflows/windows.yml @@ -0,0 +1,128 @@ +name: Windows +on: + pull_request: + paths: ['Windows/**', 'Web/preview-engine/**', 'Android/core/src/main/js/**', 'Android/core/src/main/cpp/quickjs/**', 'Scripts/lib/shared-js.mjs', 'Shared/ScriptWidgetRuntime/Widget/Resource/support.bundle/**', '.github/workflows/windows.yml'] + push: + branches: [main] + paths: ['Windows/**', 'Web/preview-engine/**', 'Android/core/src/main/js/**', 'Android/core/src/main/cpp/quickjs/**', 'Scripts/lib/shared-js.mjs', 'Shared/ScriptWidgetRuntime/Widget/Resource/support.bundle/**', '.github/workflows/windows.yml'] + workflow_dispatch: +permissions: + contents: read +concurrency: + group: windows-${{ github.ref }} + cancel-in-progress: true +jobs: + test: + name: Build and test (x64) + runs-on: windows-2025 + timeout-minutes: 45 + steps: + - uses: actions/checkout@v7 + - uses: actions/setup-node@v7 + with: {node-version: 24} + - uses: actions/setup-dotnet@v5 + with: {dotnet-version: '10.0.x'} + - name: Preview engine tests on Windows + working-directory: Web/preview-engine + run: npm test + - name: Install esbuild for the shared JavaScript bundle + working-directory: Web/studio + run: npm ci --no-audit --no-fund + - name: Shared JavaScript + run: node Windows/scripts/build-shared-js.mjs + - name: swcore (QuickJS) + shell: pwsh + run: ./Windows/scripts/build-native.ps1 -Arch x64 + - name: Build + working-directory: Windows + run: dotnet build ScriptWidget.slnx -c Release -p:Platform=x64 --nologo + - name: Test + working-directory: Windows + run: dotnet test tests/ScriptWidget.Core.Tests -c Release --nologo --logger "console;verbosity=normal" --logger "trx;LogFileName=tests.trx" + # The engine binding (swcore.c) under AddressSanitizer. The rest of the suite is not run this + # way: compiling packages with Babel inside a sanitized QuickJS hosted by .NET does not finish. + - name: Engine binding tests with AddressSanitizer in swcore + shell: pwsh + run: | + ./Windows/scripts/build-native.ps1 -Arch x64 -Asan + cd Windows + dotnet test tests/ScriptWidget.Core.Tests -c Release --nologo -p:SwNativeSuffix=-asan --filter "FullyQualifiedName~QuickJs" --logger "console;verbosity=minimal" + # The desktop host on the build agent's desktop: window content, window order, drag, clicks, + # system events and the show-desktop self-test. Pictures of the widget windows are uploaded. + - name: Desktop check + shell: pwsh + run: | + $hostPath = Get-ChildItem Windows/src/ScriptWidget.App/bin -Recurse -Filter ScriptWidget.exe | Sort-Object LastWriteTime -Descending | Select-Object -First 1 -ExpandProperty FullName + powershell -NoProfile -ExecutionPolicy Bypass -File Windows/scripts/desktop-check.ps1 -HostPath $hostPath -Output "$env:RUNNER_TEMP\desktop-check" + - uses: actions/upload-artifact@v7 + if: always() + with: + name: windows-desktop-check + path: | + ${{ runner.temp }}/desktop-check/*.png + ${{ runner.temp }}/desktop-check/*.log + ${{ runner.temp }}/desktop-check/data/host.log + if-no-files-found: ignore + - uses: actions/upload-artifact@v7 + if: always() + with: + name: windows-test-results + path: Windows/tests/ScriptWidget.Core.Tests/TestResults + if-no-files-found: ignore + arm64: + name: Build (ARM64, cross-compiled on x64) + runs-on: windows-2025 + timeout-minutes: 30 + steps: + - uses: actions/checkout@v7 + - uses: actions/setup-node@v7 + with: {node-version: 24} + - uses: actions/setup-dotnet@v5 + with: {dotnet-version: '10.0.x'} + - name: Install esbuild for the shared JavaScript bundle + working-directory: Web/studio + run: npm ci --no-audit --no-fund + - run: node Windows/scripts/build-shared-js.mjs + - name: swcore (QuickJS) for ARM64 + shell: pwsh + run: ./Windows/scripts/build-native.ps1 -Arch arm64 + - name: Build + working-directory: Windows + run: dotnet build ScriptWidget.slnx -c Release -p:Platform=ARM64 --nologo + arm64-run: + name: Build and test (ARM64) + runs-on: windows-11-arm + timeout-minutes: 45 + steps: + - uses: actions/checkout@v7 + - uses: actions/setup-node@v7 + with: {node-version: 24} + - uses: actions/setup-dotnet@v5 + with: {dotnet-version: '10.0.x'} + - name: Install esbuild for the shared JavaScript bundle + working-directory: Web/studio + run: npm ci --no-audit --no-fund + - run: node Windows/scripts/build-shared-js.mjs + - name: swcore (QuickJS) for ARM64 + shell: pwsh + run: ./Windows/scripts/build-native.ps1 -Arch arm64 + - name: Build + working-directory: Windows + run: dotnet build ScriptWidget.slnx -c Release -p:Platform=ARM64 --nologo + - name: Test + working-directory: Windows + run: dotnet test tests/ScriptWidget.Core.Tests -c Release -p:Platform=ARM64 --nologo --logger "console;verbosity=normal" + - name: Desktop check + shell: pwsh + run: | + $hostPath = Get-ChildItem Windows/src/ScriptWidget.App/bin -Recurse -Filter ScriptWidget.exe | Sort-Object LastWriteTime -Descending | Select-Object -First 1 -ExpandProperty FullName + powershell -NoProfile -ExecutionPolicy Bypass -File Windows/scripts/desktop-check.ps1 -HostPath $hostPath -Output "$env:RUNNER_TEMP\desktop-check" + - uses: actions/upload-artifact@v7 + if: always() + with: + name: windows-arm64-desktop-check + path: | + ${{ runner.temp }}/desktop-check/*.png + ${{ runner.temp }}/desktop-check/*.log + ${{ runner.temp }}/desktop-check/data/host.log + if-no-files-found: ignore diff --git a/AGENTS.md b/AGENTS.md index 75d30769..716fa357 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -13,6 +13,7 @@ ScriptWidget lets users author native widgets with JavaScript and JSX: WidgetKit - `macOS/ScriptWidgetMac`, `ScriptWidgetMacWidget`: Studio app and macOS widget extension. Studio features live in `Editor/`, `AIGenerate/`, `Skills/`, `Plugin/`, and `Onboarding/`. - `Editor/editorfe/`: Vite frontend with CodeMirror 6. Built files are copied into both platform `StudioEditor.bundle` directories. - `Web/preview-engine/`: browser preview engine (plain JavaScript with `.d.ts` types); `Web/studio/`: Web Studio, a React + strict TypeScript + shadcn/ui app published to GitHub Pages. Its `kernel.js` and `renderer.js` are also the Android runtime, so a change there affects the browser preview and Android together. +- `Windows/`: the Windows 11 desktop app (preview). `native` builds `swcore.dll` from the QuickJS-NG sources under `Android/core/src/main/cpp/quickjs`; `src/ScriptWidget.Core` is a C# port of `Android/core` (engine wrapper, compiler, executor, packages, APIs, plugins, Win2D renderer); `src/ScriptWidget.App` is the always-running desktop host; `src/ScriptWidget.Studio` is the WinUI 3 management window; `src/ScriptWidget.Cli` is `swcli`. It runs the same shared JavaScript as Android, built by `Scripts/lib/shared-js.mjs`. `Windows/assets`, `Windows/native/out` and `Windows/artifacts` are generated; do not edit or commit them. - `Android/core`: QuickJS-NG with its JNI layer, the Kotlin host and native APIs, Canvas renderer, package store, Gallery, Skills, plugins and AI client. `Android/app`: Compose app, widget providers, tiles, shortcuts, Live Updates and push. `Android/core/src/main/assets/scriptwidget` is generated; do not edit it. `Android/app/src/main/assets/Script.android` holds the templates for what only Android has: the scrolling ``, the `material.` colors and the automation entry point, all described under "Only on Android" in `docs/android/README.md`. `Android/play`: Google Play listing kit. - `Tests/ScriptWidgetRuntimeTests/`: shared runtime, execution, performance, package/cache, iCloud-state, Gallery, AI, and security tests. - `Gallery/`: curated Gallery 1.0 index, JSON Schema, widget seeds, and Skill seeds. @@ -33,6 +34,8 @@ Widget packages live under `Scripts//`. Package 2.0 uses `widget.js - Metadata-only release checks: `node Scripts/validate-release.mjs` and `node Scripts/validate-doc-links.mjs`. - Android (not covered by `release-readiness.sh`): `node Android/scripts/build-shared-js.mjs` after changing `Web/preview-engine/src`, the support bundle or `Android/core/src/main/js`; `cd Android && ./gradlew :core:testDebugUnitTest :app:assembleDebug`; `sh Android/scripts/device-test.sh core` and `app` on a connected device or emulator; `./gradlew :app:bundlePlayRelease` for the Play bundle and `:app:assembleFossRelease` for the APK published on GitHub Releases. The app has two flavors: `play` contains Firebase Cloud Messaging, `foss` must contain no Google or other proprietary library. +- Windows (not covered by `release-readiness.sh`; needs Windows 11, Visual Studio with the C++ workload, the .NET 10 SDK and Node): `node Windows/scripts/build-shared-js.mjs` after changing `Web/preview-engine/src`, the support bundle or `Android/core/src/main/js`; `powershell -File Windows/scripts/build-native.ps1 -Arch x64`; `cd Windows && dotnet test tests/ScriptWidget.Core.Tests`; `powershell -File Windows/scripts/desktop-check.ps1` after touching the desktop host; `powershell -File Windows/scripts/package.ps1` for the MSIX (add `-FrameworkDependent` and the Partner Center identity for the Store). Record what ran, on which machine, in `docs/windows/testing.md`, and keep `docs/windows/TODO.md` current. + The app uses `iCloud.ScriptWidget` and `group.everettjf.scriptwidget`. Signed real-container tests require a trusted, signed-in device; deterministic cache/iCloud-state tests do not. The Android application ID is `com.atomsprout.scriptwidget`; the Kotlin namespace stays `com.everettjf.scriptwidget`. Take the package name from `context.packageName` and class names from the class, never from a literal. `Android/keystore.properties`, `Android/firebase.properties`, `google-services.json` and keystores are git-ignored and must stay out of the repository; a build without `firebase.properties` has push turned off. diff --git a/Android/core/src/main/assets/scriptwidget/android-apis.js b/Android/core/src/main/assets/scriptwidget/android-apis.js index 4ba86227..56acb019 100644 --- a/Android/core/src/main/assets/scriptwidget/android-apis.js +++ b/Android/core/src/main/assets/scriptwidget/android-apis.js @@ -83,7 +83,9 @@ __swRegisterAPIs(function (apis, helpers, bridge) { "use strict"; const { nativePromise } = helpers; const supported = new Set(bridge.job.supportedAPIs ?? []); - const message = (name) => `[unsupported_platform] ${name} is not supported on Android in this release`; + // The host names itself; the Android host does not pass a name. + const platform = typeof bridge.job.platformName === "string" ? bridge.job.platformName : "Android"; + const message = (name) => `[unsupported_platform] ${name} is not supported on ${platform} in this release`; const rejecting = (name) => () => nativePromise((resolve, reject) => reject(message(name))); const groups = { diff --git a/Android/core/src/main/js/android-apis.js b/Android/core/src/main/js/android-apis.js index 4ba86227..56acb019 100644 --- a/Android/core/src/main/js/android-apis.js +++ b/Android/core/src/main/js/android-apis.js @@ -83,7 +83,9 @@ __swRegisterAPIs(function (apis, helpers, bridge) { "use strict"; const { nativePromise } = helpers; const supported = new Set(bridge.job.supportedAPIs ?? []); - const message = (name) => `[unsupported_platform] ${name} is not supported on Android in this release`; + // The host names itself; the Android host does not pass a name. + const platform = typeof bridge.job.platformName === "string" ? bridge.job.platformName : "Android"; + const message = (name) => `[unsupported_platform] ${name} is not supported on ${platform} in this release`; const rejecting = (name) => () => nativePromise((resolve, reject) => reject(message(name))); const groups = { diff --git a/Android/gradlew.bat b/Android/gradlew.bat index 03754c4c..3185a43f 100644 --- a/Android/gradlew.bat +++ b/Android/gradlew.bat @@ -1,112 +1,112 @@ -@rem -@rem Copyright 2015 the original author or authors. -@rem -@rem Licensed under the Apache License, Version 2.0 (the "License"); -@rem you may not use this file except in compliance with the License. -@rem You may obtain a copy of the License at -@rem -@rem https://www.apache.org/licenses/LICENSE-2.0 -@rem -@rem Unless required by applicable law or agreed to in writing, software -@rem distributed under the License is distributed on an "AS IS" BASIS, -@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -@rem See the License for the specific language governing permissions and -@rem limitations under the License. -@rem -@rem SPDX-License-Identifier: Apache-2.0 -@rem - -@if "%DEBUG%"=="" @echo off -@rem ########################################################################## -@rem -@rem gradlew startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables, and ensure extensions are enabled -setlocal EnableExtensions - -@rem Catch executions from older scripts and ensure they exit cleanly. -@rem This can be removed once we can be reasonably confident that few people -@rem will be migrating directly to this new wrapper. -goto afterSafetyNet -:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -goto exitWithErrorLevel -:afterSafetyNet - -set DIRNAME=%~dp0 -if "%DIRNAME%"=="" set DIRNAME=. -@rem This is normally unused -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Resolve any "." and ".." in APP_HOME to make it shorter. -for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if %ERRORLEVEL% equ 0 goto execute - -1>&2 echo. -1>&2 echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -1>&2 echo. -1>&2 echo Please set the JAVA_HOME variable in your environment to match the -1>&2 echo location of your Java installation. - -"%COMSPEC%" /c exit 1 -goto exitWithErrorLevel - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto execute - -1>&2 echo. -1>&2 echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -1>&2 echo. -1>&2 echo Please set the JAVA_HOME variable in your environment to match the -1>&2 echo location of your Java installation. - -"%COMSPEC%" /c exit 1 -goto exitWithErrorLevel - -:execute -@rem Setup the command line - - - -@rem Execute gradlew -@rem endlocal doesn't take effect until after the line is parsed and variables are expanded -@rem which allows us to clear the local environment before executing the java command -endlocal & "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* & call :exitWithErrorLevel & goto exitWithErrorLevel - -@rem This label must not be changed. We rely on old scripts being able to jump to this point. -:exitWithErrorLevel -@rem Use "%COMSPEC%" /c exit to allow operators to work properly in scripts -"%COMSPEC%" /c exit %ERRORLEVEL% +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem gradlew startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables, and ensure extensions are enabled +setlocal EnableExtensions + +@rem Catch executions from older scripts and ensure they exit cleanly. +@rem This can be removed once we can be reasonably confident that few people +@rem will be migrating directly to this new wrapper. +goto afterSafetyNet +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +goto exitWithErrorLevel +:afterSafetyNet + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +1>&2 echo. +1>&2 echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +1>&2 echo. +1>&2 echo Please set the JAVA_HOME variable in your environment to match the +1>&2 echo location of your Java installation. + +"%COMSPEC%" /c exit 1 +goto exitWithErrorLevel + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +1>&2 echo. +1>&2 echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +1>&2 echo. +1>&2 echo Please set the JAVA_HOME variable in your environment to match the +1>&2 echo location of your Java installation. + +"%COMSPEC%" /c exit 1 +goto exitWithErrorLevel + +:execute +@rem Setup the command line + + + +@rem Execute gradlew +@rem endlocal doesn't take effect until after the line is parsed and variables are expanded +@rem which allows us to clear the local environment before executing the java command +endlocal & "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* & call :exitWithErrorLevel & goto exitWithErrorLevel + +@rem This label must not be changed. We rely on old scripts being able to jump to this point. +:exitWithErrorLevel +@rem Use "%COMSPEC%" /c exit to allow operators to work properly in scripts +"%COMSPEC%" /c exit %ERRORLEVEL% diff --git a/Android/scripts/build-shared-js.mjs b/Android/scripts/build-shared-js.mjs index f9165969..8c105486 100644 --- a/Android/scripts/build-shared-js.mjs +++ b/Android/scripts/build-shared-js.mjs @@ -8,70 +8,15 @@ // Output (not checked in): the parity corpus used by instrumented tests. import fs from "node:fs"; import path from "node:path"; -import { fileURLToPath } from "node:url"; -import { execFileSync } from "node:child_process"; +import { buildSharedJS, repository, hostSources } from "../../Scripts/lib/shared-js.mjs"; -const here = path.dirname(fileURLToPath(import.meta.url)); -const repository = path.resolve(here, "../.."); -const engine = path.join(repository, "Web/preview-engine"); -const support = path.join(repository, "Shared/ScriptWidgetRuntime/Widget/Resource/support.bundle"); -const hostSources = path.join(repository, "Android/core/src/main/js"); -const output = path.join(repository, "Android/core/src/main/assets/scriptwidget"); -const testOutput = path.join(repository, "Android/core/src/androidTest/assets"); - -const { scriptWidgetKernel } = await import(path.join(engine, "src/kernel.js")); -const { runtimeContract, errorCategories } = await import(path.join(engine, "src/contract.js")); - -function moduleBody(file) { - return fs.readFileSync(path.join(engine, "src", file), "utf8") - .replace(/^import[^;]+;\n/gm, "") - .replace(/^export \{[^}]*\}[^;]*;\n/gm, "") - .replace(/^export (const|function|async function|class) /gm, "$1 "); -} - -fs.mkdirSync(output, { recursive: true }); -const banner = "// Generated by Android/scripts/build-shared-js.mjs. Do not edit.\n"; - -fs.writeFileSync(path.join(output, "kernel.js"), `${banner}globalThis.__swKernel = ${scriptWidgetKernel.toString()};\n`); - -const prepareModules = ["contract.js", "packageFiles.js", "timeZones.js", "timeline.js", "package/manifest.js", "compiler.js", "prepare.js"]; -fs.writeFileSync(path.join(output, "prepare.js"), - `${banner}globalThis.__swPrepareBundle = (function () {\n${prepareModules.map(moduleBody).join("\n")}\n` + - "return { createCompiler, preparePackage, buildTimelinePlan, decodeManifest, validateManifest, normalizePackagePath };\n})();\n"); - -// The Android host is a native host: it implements the native API version. -const contract = { ...runtimeContract, apiVersion: runtimeContract.nativeAPIVersion, compilerFingerprint: "scriptwidget-android-babel-v1" }; -fs.writeFileSync(path.join(output, "contract.json"), JSON.stringify({ contract, errorCategories }, null, 2) + "\n"); - -for (const name of ["core.js", "util.js", "moment.min.js"]) fs.copyFileSync(path.join(support, name), path.join(output, name)); -for (const name of fs.readdirSync(hostSources).filter((file) => file.endsWith(".js")).sort()) { - fs.copyFileSync(path.join(hostSources, name), path.join(output, name)); -} - -// The shared web renderer, bundled for QuickJS with a recording document. -const esbuild = [path.join(repository, "Web/studio/node_modules/.bin/esbuild"), path.join(repository, "Editor/editorfe/node_modules/.bin/esbuild")].find((file) => fs.existsSync(file)); -if (!esbuild) throw new Error("esbuild not found: run npm install in Web/studio first"); -execFileSync(esbuild, [path.join(hostSources, "render/entry.mjs"), "--bundle", "--format=iife", "--global-name=__swRenderBundle", "--target=es2022", +await buildSharedJS({ + output: path.join(repository, "Android/core/src/main/assets/scriptwidget"), + banner: "// Generated by Android/scripts/build-shared-js.mjs. Do not edit.\n", + compilerFingerprint: "scriptwidget-android-babel-v1", + hostFiles: fs.readdirSync(hostSources).filter((file) => file.endsWith(".js")).sort().map((file) => [file, file]), // esbuild writes each module's path relative to the working directory into the bundle, so the // directory is fixed: the output is the same wherever the script is started from. - "--charset=utf8", "--log-level=warning", `--outfile=${path.join(output, "render.js")}`], { stdio: "inherit", cwd: path.join(repository, "Android") }); - -// Parity corpus for instrumented tests: every bundled package that needs no -// live data, with the tree JavaScriptCore produced for it. -const { corpusPackages, readNativeTrees, usesLiveData } = await import(path.join(engine, "test/parity/corpus.js")); -const { readPackageDirectory } = await import(path.join(engine, "src/node.js")); -const native = readNativeTrees(); -const corpus = corpusPackages() - .map(({ name, directory }) => { - const files = readPackageDirectory(directory); - const text = {}, binaries = []; - for (const [file, content] of Object.entries(files)) { if (typeof content === "string") text[file] = content; else binaries.push(file); } - return { name, files: text, binaries, expected: native.entries[name] ?? null }; - }) - .filter((item) => !usesLiveData(item.files["main.jsx"])); -fs.mkdirSync(testOutput, { recursive: true }); -fs.writeFileSync(path.join(testOutput, "parity-corpus.json"), - JSON.stringify({ timeZone: native.timeZone, locale: native.javaScriptLocale, prelude: fs.readFileSync(path.join(engine, "test/parity/prelude.js"), "utf8"), corpus })); - -console.log(`assets -> ${path.relative(repository, output)} (${fs.readdirSync(output).join(", ")})`); -console.log(`parity corpus: ${corpus.length} packages, time zone ${native.timeZone}`); + bundleDirectory: path.join(repository, "Android"), + corpusOutput: path.join(repository, "Android/core/src/androidTest/assets"), +}); diff --git a/CHANGELOG.md b/CHANGELOG.md index 437126f7..b47affe4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,13 @@ Notable user-visible changes are recorded here. ScriptWidget follows semantic ve - Capabilities without an Android implementation report `[unsupported_platform]` and are listed on the package page. See [ScriptWidget for Android](docs/android/README.md). - Verified on one device (Pixel 6, Android 17). Other vendors' launchers and background limits are untested; AI generation has not been run against a live provider. +### Windows (preview, `windows` branch) + +- Add a Windows 11 app that runs the same Package 2.0 widgets on the desktop: QuickJS-NG with the shared execution kernel, the shared renderer painted with Win2D, and small, medium, large and extra-large widgets in windows that sit on the desktop. +- Add a tray icon, a management window with the library, the bundled templates, the code editor with a live preview, settings, and `.swt` import and export. +- `$device`, `$system`, `$storage`, `$file`, `fetch`, `$http`, `$xml`, `$dataSource` and `$weather` work; the other data APIs report `[unsupported_platform]`. See [ScriptWidget for Windows](docs/windows/README.md). +- Automated tests and a scripted desktop check pass on one x64 PC and in CI on x64 and ARM64. The desktop behavior was first checked from window captures, the window order and posted input while the PC was locked, then on the unlocked desktop with the real Win+D key and real mouse input, which found and fixed two Show Desktop bugs and one in the bundled network templates. Signing out and in, a second display and an ARM64 PC are untried. See [the test notes](docs/windows/testing.md). + ### Compatibility fixes - Allow confirmed deletion of late-synced or recreated unregistered widget folders without adopting their old records. Preserve confirmation-time directory identity, show deletion failures, and record diagnostic outcomes. diff --git a/Scripts/lib/shared-js.mjs b/Scripts/lib/shared-js.mjs new file mode 100644 index 00000000..6471ded8 --- /dev/null +++ b/Scripts/lib/shared-js.mjs @@ -0,0 +1,89 @@ +// Builds the JavaScript assets that the QuickJS hosts (Android and Windows) +// share with the web preview engine: the execution kernel, the package +// preparation bundle, the runtime contract, the support scripts, the host +// files and the renderer bundle. Also writes the parity corpus the hosts' +// tests run. +import fs from "node:fs"; +import path from "node:path"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import { execFileSync } from "node:child_process"; + +const repository = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.."); +const engine = path.join(repository, "Web/preview-engine"); +const support = path.join(repository, "Shared/ScriptWidgetRuntime/Widget/Resource/support.bundle"); +const hostSources = path.join(repository, "Android/core/src/main/js"); +const load = (file) => import(pathToFileURL(path.join(engine, file)).href); + +function moduleBody(file) { + return fs.readFileSync(path.join(engine, "src", file), "utf8") + .replace(/^import[^;]+;\n/gm, "") + .replace(/^export \{[^}]*\}[^;]*;\n/gm, "") + .replace(/^export (const|function|async function|class) /gm, "$1 "); +} + +function findEsbuild() { + const names = process.platform === "win32" ? ["esbuild.cmd"] : ["esbuild"]; + for (const directory of ["Web/studio/node_modules/.bin", "Editor/editorfe/node_modules/.bin"]) { + for (const name of names) { + const file = path.join(repository, directory, name); + if (fs.existsSync(file)) return file; + } + } + throw new Error("esbuild not found: run npm install in Web/studio first"); +} + +/// `output`: directory for the assets. `banner`: first line of generated +/// files. `compilerFingerprint`: identifies the host's compiler in caches. +/// `hostFiles`: maps a file in Android/core/src/main/js to its output name. +/// `bundleDirectory`: working directory of esbuild; module paths in the +/// bundle are relative to it, so it is fixed per host. +/// `corpusOutput`: where to write parity-corpus.json. +export async function buildSharedJS({ output, banner, compilerFingerprint, hostFiles, bundleDirectory, corpusOutput }) { + const { scriptWidgetKernel } = await load("src/kernel.js"); + const { runtimeContract, errorCategories } = await load("src/contract.js"); + + fs.mkdirSync(output, { recursive: true }); + fs.writeFileSync(path.join(output, "kernel.js"), `${banner}globalThis.__swKernel = ${scriptWidgetKernel.toString()};\n`); + + const prepareModules = ["contract.js", "packageFiles.js", "timeZones.js", "timeline.js", "package/manifest.js", "compiler.js", "prepare.js"]; + fs.writeFileSync(path.join(output, "prepare.js"), + `${banner}globalThis.__swPrepareBundle = (function () {\n${prepareModules.map(moduleBody).join("\n")}\n` + + "return { createCompiler, preparePackage, buildTimelinePlan, decodeManifest, validateManifest, normalizePackagePath };\n})();\n"); + + // A QuickJS host is a native host: it implements the native API version. + const contract = { ...runtimeContract, apiVersion: runtimeContract.nativeAPIVersion, compilerFingerprint }; + fs.writeFileSync(path.join(output, "contract.json"), JSON.stringify({ contract, errorCategories }, null, 2) + "\n"); + + for (const name of ["core.js", "util.js", "moment.min.js"]) fs.copyFileSync(path.join(support, name), path.join(output, name)); + for (const [source, target] of hostFiles) fs.copyFileSync(path.join(hostSources, source), path.join(output, target)); + + // The shared web renderer, bundled for QuickJS with a recording document. + const esbuild = findEsbuild(); + const entry = path.join(hostSources, "render/entry.mjs"); + const outfile = path.join(output, "render.js"); + const args = [entry, "--bundle", "--format=iife", "--global-name=__swRenderBundle", "--target=es2022", "--charset=utf8", "--log-level=warning", `--outfile=${outfile}`]; + if (process.platform === "win32") execFileSync(`"${esbuild}" ${args.map((value) => `"${value}"`).join(" ")}`, { stdio: "inherit", cwd: bundleDirectory, shell: true }); + else execFileSync(esbuild, args, { stdio: "inherit", cwd: bundleDirectory }); + + // Parity corpus: every bundled package that needs no live data, with the + // tree JavaScriptCore produced for it. + const { corpusPackages, readNativeTrees, usesLiveData } = await load("test/parity/corpus.js"); + const { readPackageDirectory } = await load("src/node.js"); + const native = readNativeTrees(); + const corpus = corpusPackages() + .map(({ name, directory }) => { + const files = readPackageDirectory(directory); + const text = {}, binaries = []; + for (const [file, content] of Object.entries(files)) { if (typeof content === "string") text[file] = content; else binaries.push(file); } + return { name, files: text, binaries, expected: native.entries[name] ?? null }; + }) + .filter((item) => !usesLiveData(item.files["main.jsx"])); + fs.mkdirSync(corpusOutput, { recursive: true }); + fs.writeFileSync(path.join(corpusOutput, "parity-corpus.json"), + JSON.stringify({ timeZone: native.timeZone, locale: native.javaScriptLocale, prelude: fs.readFileSync(path.join(engine, "test/parity/prelude.js"), "utf8"), corpus })); + + console.log(`assets -> ${path.relative(repository, output)} (${fs.readdirSync(output).join(", ")})`); + console.log(`parity corpus: ${corpus.length} packages, time zone ${native.timeZone}`); +} + +export { repository, hostSources }; diff --git a/Scripts/validate-doc-links.mjs b/Scripts/validate-doc-links.mjs index 0ac6bf9f..73d1dca3 100644 --- a/Scripts/validate-doc-links.mjs +++ b/Scripts/validate-doc-links.mjs @@ -1,5 +1,5 @@ import { readFile, readdir, stat } from "node:fs/promises"; -import { dirname, join, relative, resolve } from "node:path"; +import { dirname, join, relative, resolve, sep } from "node:path"; import { fileURLToPath } from "node:url"; const root = resolve(dirname(fileURLToPath(import.meta.url)), ".."); @@ -34,7 +34,7 @@ for (const entry of roots) { try { decoded = decodeURIComponent(target); } catch { failures.push(`${relative(root, markdownPath)}: invalid URL encoding in ${target}`); continue; } const destination = resolve(dirname(markdownPath), decoded); - if (destination !== root && !destination.startsWith(`${root}/`)) { + if (destination !== root && !destination.startsWith(`${root}${sep}`)) { failures.push(`${relative(root, markdownPath)}: link escapes repository: ${target}`); continue; } diff --git a/Windows/.gitignore b/Windows/.gitignore new file mode 100644 index 00000000..208cebee --- /dev/null +++ b/Windows/.gitignore @@ -0,0 +1,13 @@ +bin/ +obj/ +.vs/ +*.user +# Built by scripts/build-native.ps1 +native/build/ +native/out/ +# Generated by scripts/build-shared-js.mjs +assets/scriptwidget/ +tests/assets/ +# Packaging output +package/out/ +artifacts/ diff --git a/Windows/Directory.Build.props b/Windows/Directory.Build.props new file mode 100644 index 00000000..68aa5c78 --- /dev/null +++ b/Windows/Directory.Build.props @@ -0,0 +1,34 @@ + + + net10.0-windows10.0.22621.0 + 10.0.22000.0 + 10.0.22000.0 + x64;ARM64 + x64 + win-arm64 + win-x64 + enable + enable + latest + true + false + $(NoWarn);NU1903;CA1416 + 0.1.0 + AtomSprout + ScriptWidget + + true + + false + + false + None + + true + arm64 + x64 + $(MSBuildThisFileDirectory)native\out\$(SwNativeArch)$(SwNativeSuffix) + $(MSBuildThisFileDirectory)assets + + diff --git a/Windows/ScriptWidget.slnx b/Windows/ScriptWidget.slnx new file mode 100644 index 00000000..5270e8e3 --- /dev/null +++ b/Windows/ScriptWidget.slnx @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Windows/native/CMakeLists.txt b/Windows/native/CMakeLists.txt new file mode 100644 index 00000000..61b80aa2 --- /dev/null +++ b/Windows/native/CMakeLists.txt @@ -0,0 +1,48 @@ +cmake_minimum_required(VERSION 3.24) +project(swcore C) + +set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD_REQUIRED ON) + +# The same QuickJS-NG sources the Android app builds. +set(QJS ${CMAKE_CURRENT_SOURCE_DIR}/../../Android/core/src/main/cpp/quickjs) + +# QuickJS on Windows computes the local-time offset of every date from the +# offset in force today. The copy built here asks swcore instead; the vendored +# source stays as upstream wrote it. The build stops if upstream changes the +# function, so the patch can be revisited. +file(READ ${QJS}/quickjs.c QUICKJS_SOURCE) +set(TZ_BEFORE "static int getTimezoneOffset(int64_t time) {\n#if defined(_WIN32)\n") +set(TZ_AFTER "int sw_timezone_offset(int64_t time);\nstatic int getTimezoneOffset(int64_t time) {\n#if defined(_WIN32)\n return sw_timezone_offset(time);\n#elif defined(SW_UPSTREAM_WINDOWS_TIMEZONE)\n") +string(FIND "${QUICKJS_SOURCE}" "${TZ_BEFORE}" TZ_POSITION) +if(TZ_POSITION EQUAL -1) + message(FATAL_ERROR "quickjs.c: getTimezoneOffset no longer matches the Windows time zone patch") +endif() +string(REPLACE "${TZ_BEFORE}" "${TZ_AFTER}" QUICKJS_SOURCE "${QUICKJS_SOURCE}") +file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/quickjs_windows.c "${QUICKJS_SOURCE}") + +add_library(swcore SHARED + swcore.c + ${CMAKE_CURRENT_BINARY_DIR}/quickjs_windows.c + ${QJS}/libregexp.c + ${QJS}/libunicode.c + ${QJS}/dtoa.c) + +target_include_directories(swcore PRIVATE ${QJS}) +target_compile_definitions(swcore PRIVATE _CRT_SECURE_NO_WARNINGS) + +if(MSVC) + # QuickJS uses C11 atomics, which MSVC still gates behind a switch. + target_compile_options(swcore PRIVATE /experimental:c11atomics /W3 /wd4018 /wd4146 /wd4244 /wd4267 /wd4334 /wd4996 /wd4133 /wd4090 /wd4200) + target_compile_options(swcore PRIVATE $<$>:/guard:cf>) + target_link_options(swcore PRIVATE /DYNAMICBASE /NXCOMPAT $<$>:/guard:cf>) + # A 64 MB script heap plus deep recursion: QuickJS checks its own stack + # limit, so the DLL does not need a larger reserve. + if(SW_ASAN) + target_compile_options(swcore PRIVATE /fsanitize=address /Zi) + endif() +endif() + +target_link_libraries(swcore PRIVATE icu) + +set_target_properties(swcore PROPERTIES PREFIX "" MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") diff --git a/Windows/native/swcore.c b/Windows/native/swcore.c new file mode 100644 index 00000000..d8394c20 --- /dev/null +++ b/Windows/native/swcore.c @@ -0,0 +1,382 @@ +// C binding between the .NET host and QuickJS-NG, the Windows counterpart of +// Android/core/src/main/cpp/scriptwidget_jni.c. One Engine owns one runtime +// and one context. All entry points must be called from the thread that +// created the engine. Strings cross the boundary as UTF-16 so astral +// characters and lone surrogates survive unchanged. + +#include +#include +#include +#include + +#define WIN32_LEAN_AND_MEAN +#include + +#include + +#include "quickjs.h" + +#define SW_EXPORT __declspec(dllexport) + +// Status of a call that produces a value. +#define SW_NULL 0 // undefined or null +#define SW_VALUE 1 // sw_result holds the string or the bytes +#define SW_EXCEPTION -1 // sw_result holds the message and stack + +typedef struct Engine Engine; + +// __swNative(op, payload). The host answers by calling sw_host_return or +// sw_host_throw before it returns; neither means null. A length of -1 is null. +typedef void (*sw_host_fn)(void *user, const uint16_t *operation, intptr_t operationLength, const uint16_t *payload, intptr_t payloadLength); + +struct Engine { + JSRuntime *rt; + JSContext *ctx; + sw_host_fn host; + void *user; + int64_t deadlineTicks; + bool interrupted; + // Answer of the host call in progress. + JSValue hostResult; + bool hostAnswered; + bool hostThrew; + // Result of the last call: UTF-16 text or raw bytes, owned by the engine. + uint8_t *result; + size_t resultLength; // UTF-16 code units for text, bytes for bytecode + size_t resultCapacity; +}; + +// MARK: - Local time +// +// QuickJS on Windows answers Date's local-time questions with the offset +// that is in force right now, whatever the date asked about, so a date on +// the other side of a daylight-saving change comes out an hour off. The +// build replaces that with sw_timezone_offset (see CMakeLists.txt), which +// asks ICU for the offset at that instant in the host's time zone. + +static SRWLOCK zoneLock = SRWLOCK_INIT; +static UChar zoneID[128]; +static int32_t zoneLength = -1; // -1: ICU's default zone +static volatile LONG zoneGeneration = 1; +static __declspec(thread) UCalendar *threadCalendar; +static __declspec(thread) LONG threadGeneration; + +// IANA zone name such as "America/Los_Angeles"; NULL or a negative length +// selects the system zone. +SW_EXPORT void sw_set_time_zone(const uint16_t *id, intptr_t length) { + AcquireSRWLockExclusive(&zoneLock); + if (id == NULL || length < 0 || length >= (intptr_t)(sizeof(zoneID) / sizeof(zoneID[0]))) zoneLength = -1; + else { memcpy(zoneID, id, (size_t)length * sizeof(UChar)); zoneLength = (int32_t)length; } + InterlockedIncrement(&zoneGeneration); + ReleaseSRWLockExclusive(&zoneLock); +} + +// Minutes to add to local time to get UTC at `time` (milliseconds since the +// epoch), like Date.prototype.getTimezoneOffset. +int sw_timezone_offset(int64_t time) { + LONG generation = zoneGeneration; + if (threadCalendar == NULL || threadGeneration != generation) { + if (threadCalendar != NULL) { ucal_close(threadCalendar); threadCalendar = NULL; } + UErrorCode status = U_ZERO_ERROR; + AcquireSRWLockShared(&zoneLock); + threadCalendar = ucal_open(zoneLength >= 0 ? zoneID : NULL, zoneLength, "en_US", UCAL_GREGORIAN, &status); + ReleaseSRWLockShared(&zoneLock); + if (U_FAILURE(status)) { threadCalendar = NULL; return 0; } + threadGeneration = generation; + } + UErrorCode status = U_ZERO_ERROR; + ucal_setMillis(threadCalendar, (UDate)time, &status); + int32_t zone = ucal_get(threadCalendar, UCAL_ZONE_OFFSET, &status); + int32_t daylight = ucal_get(threadCalendar, UCAL_DST_OFFSET, &status); + if (U_FAILURE(status)) return 0; + return -(zone + daylight) / 60000; +} + +// MARK: - Engine + +static int64_t ticksPerSecond(void) { + static int64_t frequency; + if (frequency == 0) { LARGE_INTEGER value; QueryPerformanceFrequency(&value); frequency = value.QuadPart; } + return frequency; +} + +static int64_t monotonicTicks(void) { + LARGE_INTEGER value; + QueryPerformanceCounter(&value); + return value.QuadPart; +} + +static int interruptHandler(JSContext *ctx, void *opaque) { + (void)ctx; + Engine *engine = (Engine *)opaque; + if (engine->interrupted) return 1; + if (engine->deadlineTicks > 0 && monotonicTicks() > engine->deadlineTicks) { + engine->interrupted = true; + return 1; + } + return 0; +} + +static bool reserve(Engine *engine, size_t bytes) { + if (bytes <= engine->resultCapacity) return true; + size_t capacity = bytes + bytes / 2 + 64; + uint8_t *grown = (uint8_t *)realloc(engine->result, capacity); + if (grown == NULL) return false; + engine->result = grown; + engine->resultCapacity = capacity; + return true; +} + +static void setResultBytes(Engine *engine, const void *bytes, size_t length) { + if (!reserve(engine, length)) { engine->resultLength = 0; return; } + if (length > 0) memcpy(engine->result, bytes, length); + engine->resultLength = length; +} + +static void appendResultText(Engine *engine, const uint16_t *chars, size_t length) { + if (!reserve(engine, (engine->resultLength + length) * sizeof(uint16_t))) return; + if (length > 0) memcpy(engine->result + engine->resultLength * sizeof(uint16_t), chars, length * sizeof(uint16_t)); + engine->resultLength += length; +} + +static void appendResultAscii(Engine *engine, const char *text) { + for (const char *cursor = text; *cursor; cursor += 1) { uint16_t unit = (uint16_t)(unsigned char)*cursor; appendResultText(engine, &unit, 1); } +} + +static JSValue stringFromHost(Engine *engine, const uint16_t *chars, intptr_t length) { + if (chars == NULL || length < 0) return JS_NULL; + return JS_NewStringUTF16(engine->ctx, chars, (size_t)length); +} + +// Appends value converted to a string; returns false when conversion throws +// (the exception is cleared). +static bool appendValue(Engine *engine, JSValueConst value) { + size_t length = 0; + const uint16_t *chars = JS_ToCStringLenUTF16(engine->ctx, &length, value); + if (chars == NULL) { JS_FreeValue(engine->ctx, JS_GetException(engine->ctx)); return false; } + appendResultText(engine, chars, length); + JS_FreeCStringUTF16(engine->ctx, chars); + return true; +} + +// Moves the pending JS exception into the result as "message\nstack". +static int takeException(Engine *engine) { + JSContext *ctx = engine->ctx; + JSValue error = JS_GetException(ctx); + engine->resultLength = 0; + if (!appendValue(engine, error)) appendResultAscii(engine, "unknown error"); + if (JS_IsError(error)) { + JSValue stack = JS_GetPropertyStr(ctx, error, "stack"); + if (JS_IsException(stack)) JS_FreeValue(ctx, JS_GetException(ctx)); + else if (!JS_IsUndefined(stack) && !JS_IsNull(stack)) { + size_t before = engine->resultLength; + appendResultAscii(engine, "\n"); + size_t afterSeparator = engine->resultLength; + if (!appendValue(engine, stack) || engine->resultLength == afterSeparator) engine->resultLength = before; + } + JS_FreeValue(ctx, stack); + } + JS_FreeValue(ctx, error); + return SW_EXCEPTION; +} + +static int finishValue(Engine *engine, JSValue value) { + if (JS_IsException(value)) return takeException(engine); + if (JS_IsUndefined(value) || JS_IsNull(value)) { JS_FreeValue(engine->ctx, value); engine->resultLength = 0; return SW_NULL; } + size_t length = 0; + const uint16_t *chars = JS_ToCStringLenUTF16(engine->ctx, &length, value); + JS_FreeValue(engine->ctx, value); + if (chars == NULL) return takeException(engine); + engine->resultLength = 0; + appendResultText(engine, chars, length); + JS_FreeCStringUTF16(engine->ctx, chars); + return SW_VALUE; +} + +// __swNative(op, payload) -> string | null. Synchronous call into the host. +static JSValue hostCall(JSContext *ctx, JSValueConst thisValue, int argc, JSValueConst *argv) { + (void)thisValue; + Engine *engine = (Engine *)JS_GetRuntimeOpaque(JS_GetRuntime(ctx)); + if (argc < 1) return JS_ThrowTypeError(ctx, "__swNative requires an operation"); + size_t operationLength = 0; + const uint16_t *operation = JS_ToCStringLenUTF16(ctx, &operationLength, argv[0]); + if (operation == NULL) return JS_EXCEPTION; + const uint16_t *payload = NULL; + size_t payloadLength = 0; + if (argc > 1 && !JS_IsUndefined(argv[1]) && !JS_IsNull(argv[1])) { + payload = JS_ToCStringLenUTF16(ctx, &payloadLength, argv[1]); + if (payload == NULL) { JS_FreeCStringUTF16(ctx, operation); return JS_EXCEPTION; } + } + // A host call can run JavaScript that makes another host call. + JSValue outerResult = engine->hostResult; + bool outerAnswered = engine->hostAnswered, outerThrew = engine->hostThrew; + engine->hostResult = JS_NULL; + engine->hostAnswered = false; + engine->hostThrew = false; + engine->host(engine->user, operation, (intptr_t)operationLength, payload, payload != NULL ? (intptr_t)payloadLength : -1); + JS_FreeCStringUTF16(ctx, operation); + if (payload != NULL) JS_FreeCStringUTF16(ctx, payload); + JSValue result = engine->hostResult; + bool threw = engine->hostThrew; + engine->hostResult = outerResult; + engine->hostAnswered = outerAnswered; + engine->hostThrew = outerThrew; + if (threw) { + JSValue error = JS_NewError(ctx); + JS_SetPropertyStr(ctx, error, "message", result); + return JS_Throw(ctx, error); + } + return result; +} + +// __swEvaluate(code, fileName): classic-script evaluation in the global +// lexical scope, like JSContext.evaluateScript. Indirect eval would scope +// top-level let/const/class to the call and hide them from later scripts. +static JSValue evaluateGlobal(JSContext *ctx, JSValueConst thisValue, int argc, JSValueConst *argv) { + (void)thisValue; + if (argc < 1) return JS_ThrowTypeError(ctx, "__swEvaluate requires source text"); + size_t length = 0; + const char *code = JS_ToCStringLen(ctx, &length, argv[0]); + if (code == NULL) return JS_EXCEPTION; + const char *name = argc > 1 ? JS_ToCString(ctx, argv[1]) : NULL; + JSValue result = JS_Eval(ctx, code, length, name != NULL ? name : "script.js", JS_EVAL_TYPE_GLOBAL); + if (name != NULL) JS_FreeCString(ctx, name); + JS_FreeCString(ctx, code); + return result; +} + +static void enter(Engine *engine) { JS_UpdateStackTop(engine->rt); } + +SW_EXPORT Engine *sw_create(int64_t memoryLimit, int64_t stackSize, sw_host_fn host, void *user) { + Engine *engine = (Engine *)calloc(1, sizeof(Engine)); + if (engine == NULL) return NULL; + engine->rt = JS_NewRuntime(); + if (engine->rt == NULL) { free(engine); return NULL; } + JS_SetRuntimeOpaque(engine->rt, engine); + if (memoryLimit > 0) JS_SetMemoryLimit(engine->rt, (size_t)memoryLimit); + if (stackSize > 0) JS_SetMaxStackSize(engine->rt, (size_t)stackSize); + JS_SetInterruptHandler(engine->rt, interruptHandler, engine); + engine->ctx = JS_NewContext(engine->rt); + if (engine->ctx == NULL) { JS_FreeRuntime(engine->rt); free(engine); return NULL; } + engine->host = host; + engine->user = user; + engine->hostResult = JS_NULL; + JSValue global = JS_GetGlobalObject(engine->ctx); + JS_SetPropertyStr(engine->ctx, global, "__swNative", JS_NewCFunction(engine->ctx, hostCall, "__swNative", 2)); + JS_SetPropertyStr(engine->ctx, global, "__swEvaluate", JS_NewCFunction(engine->ctx, evaluateGlobal, "__swEvaluate", 2)); + JS_FreeValue(engine->ctx, global); + return engine; +} + +SW_EXPORT void sw_destroy(Engine *engine) { + enter(engine); + JS_FreeContext(engine->ctx); + JS_FreeRuntime(engine->rt); + free(engine->result); + free(engine); +} + +SW_EXPORT void sw_set_deadline(Engine *engine, int64_t millisecondsFromNow) { + engine->interrupted = false; + engine->deadlineTicks = millisecondsFromNow > 0 ? monotonicTicks() + millisecondsFromNow * ticksPerSecond() / 1000 : 0; +} + +SW_EXPORT int sw_interrupted(Engine *engine) { return engine->interrupted ? 1 : 0; } + +// Text or bytes of the last call, valid until the next call on this engine. +// The length is in UTF-16 code units for text and in bytes for bytecode. +SW_EXPORT const void *sw_result(Engine *engine, intptr_t *length) { + *length = (intptr_t)engine->resultLength; + return engine->result; +} + +SW_EXPORT void sw_host_return(Engine *engine, const uint16_t *chars, intptr_t length) { + if (engine->hostAnswered) JS_FreeValue(engine->ctx, engine->hostResult); + engine->hostResult = stringFromHost(engine, chars, length); + engine->hostAnswered = true; + engine->hostThrew = false; +} + +SW_EXPORT void sw_host_throw(Engine *engine, const uint16_t *chars, intptr_t length) { + if (engine->hostAnswered) JS_FreeValue(engine->ctx, engine->hostResult); + engine->hostResult = chars != NULL && length >= 0 ? JS_NewStringUTF16(engine->ctx, chars, (size_t)length) : JS_NewString(engine->ctx, "native host error"); + engine->hostAnswered = true; + engine->hostThrew = true; +} + +// JS_Eval needs NUL-terminated UTF-8. Round-trip through a JS string so the +// conversion from UTF-16 is done by the engine. +static JSValue evaluateSource(Engine *engine, const uint16_t *code, intptr_t length, const char *fileName, int flags) { + JSValue source = JS_NewStringUTF16(engine->ctx, code, (size_t)length); + if (JS_IsException(source)) return source; + size_t utf8Length = 0; + const char *utf8 = JS_ToCStringLen(engine->ctx, &utf8Length, source); + JS_FreeValue(engine->ctx, source); + if (utf8 == NULL) return JS_EXCEPTION; + JSValue value = JS_Eval(engine->ctx, utf8, utf8Length, fileName, flags); + JS_FreeCString(engine->ctx, utf8); + return value; +} + +SW_EXPORT int sw_evaluate(Engine *engine, const uint16_t *code, intptr_t length, const char *fileName) { + enter(engine); + return finishValue(engine, evaluateSource(engine, code, length, fileName, JS_EVAL_TYPE_GLOBAL)); +} + +SW_EXPORT int sw_compile(Engine *engine, const uint16_t *code, intptr_t length, const char *fileName) { + enter(engine); + JSValue function = evaluateSource(engine, code, length, fileName, JS_EVAL_TYPE_GLOBAL | JS_EVAL_FLAG_COMPILE_ONLY); + if (JS_IsException(function)) return takeException(engine); + size_t size = 0; + uint8_t *bytes = JS_WriteObject(engine->ctx, &size, function, JS_WRITE_OBJ_BYTECODE); + JS_FreeValue(engine->ctx, function); + if (bytes == NULL) return takeException(engine); + setResultBytes(engine, bytes, size); + js_free(engine->ctx, bytes); + return SW_VALUE; +} + +// Bytecode must come from sw_compile in this application build; it is never +// read from packages or other untrusted input. +SW_EXPORT int sw_evaluate_bytecode(Engine *engine, const uint8_t *bytes, intptr_t length) { + enter(engine); + JSValue function = JS_ReadObject(engine->ctx, bytes, (size_t)length, JS_READ_OBJ_BYTECODE); + if (JS_IsException(function)) return takeException(engine); + return finishValue(engine, JS_EvalFunction(engine->ctx, function)); +} + +SW_EXPORT int sw_call(Engine *engine, const char *functionName, const uint16_t *const *arguments, const intptr_t *lengths, int count) { + enter(engine); + JSContext *ctx = engine->ctx; + JSValue global = JS_GetGlobalObject(ctx); + JSValue function = JS_GetPropertyStr(ctx, global, functionName); + JSValue *values = count > 0 ? (JSValue *)calloc((size_t)count, sizeof(JSValue)) : NULL; + for (int index = 0; index < count; index += 1) values[index] = stringFromHost(engine, arguments[index], lengths[index]); + JSValue result = JS_Call(ctx, function, global, count, values); + for (int index = 0; index < count; index += 1) JS_FreeValue(ctx, values[index]); + free(values); + JS_FreeValue(ctx, function); + JS_FreeValue(ctx, global); + return finishValue(engine, result); +} + +// Runs queued promise jobs. Returns the number executed, or -1 when a job +// threw (sw_result holds the message). +SW_EXPORT int sw_run_jobs(Engine *engine, int maximum) { + enter(engine); + int executed = 0; + while (executed < maximum) { + JSContext *jobContext = NULL; + int status = JS_ExecutePendingJob(engine->rt, &jobContext); + if (status == 0) break; + if (status < 0) { takeException(engine); return -1; } + executed += 1; + } + return executed; +} + +SW_EXPORT int64_t sw_memory_used(Engine *engine) { + JSMemoryUsage usage; + JS_ComputeMemoryUsage(engine->rt, &usage); + return (int64_t)usage.malloc_size; +} diff --git a/Windows/package/AppxManifest.xml b/Windows/package/AppxManifest.xml new file mode 100644 index 00000000..f6e410d8 --- /dev/null +++ b/Windows/package/AppxManifest.xml @@ -0,0 +1,67 @@ + + + + + + + + ScriptWidget + {{PublisherDisplayName}} + Assets\StoreLogo.png + Desktop widgets written in JavaScript and JSX. + + + + +{{FrameworkDependencies}} + + + + + + + + + + + + + + + + + + + + + ScriptWidget package + + .swt + + + + + + + + + + + + + + diff --git a/Windows/package/Assets/ScriptWidget.ico b/Windows/package/Assets/ScriptWidget.ico new file mode 100644 index 00000000..e3ecbe47 Binary files /dev/null and b/Windows/package/Assets/ScriptWidget.ico differ diff --git a/Windows/package/Assets/Square150x150Logo.scale-100.png b/Windows/package/Assets/Square150x150Logo.scale-100.png new file mode 100644 index 00000000..2c35b481 Binary files /dev/null and b/Windows/package/Assets/Square150x150Logo.scale-100.png differ diff --git a/Windows/package/Assets/Square150x150Logo.scale-200.png b/Windows/package/Assets/Square150x150Logo.scale-200.png new file mode 100644 index 00000000..af9f1514 Binary files /dev/null and b/Windows/package/Assets/Square150x150Logo.scale-200.png differ diff --git a/Windows/package/Assets/Square44x44Logo.scale-100.png b/Windows/package/Assets/Square44x44Logo.scale-100.png new file mode 100644 index 00000000..97c98778 Binary files /dev/null and b/Windows/package/Assets/Square44x44Logo.scale-100.png differ diff --git a/Windows/package/Assets/Square44x44Logo.scale-200.png b/Windows/package/Assets/Square44x44Logo.scale-200.png new file mode 100644 index 00000000..1be5736d Binary files /dev/null and b/Windows/package/Assets/Square44x44Logo.scale-200.png differ diff --git a/Windows/package/Assets/Square44x44Logo.targetsize-16.png b/Windows/package/Assets/Square44x44Logo.targetsize-16.png new file mode 100644 index 00000000..21ce455b Binary files /dev/null and b/Windows/package/Assets/Square44x44Logo.targetsize-16.png differ diff --git a/Windows/package/Assets/Square44x44Logo.targetsize-16_altform-unplated.png b/Windows/package/Assets/Square44x44Logo.targetsize-16_altform-unplated.png new file mode 100644 index 00000000..21ce455b Binary files /dev/null and b/Windows/package/Assets/Square44x44Logo.targetsize-16_altform-unplated.png differ diff --git a/Windows/package/Assets/Square44x44Logo.targetsize-24.png b/Windows/package/Assets/Square44x44Logo.targetsize-24.png new file mode 100644 index 00000000..0ae92e96 Binary files /dev/null and b/Windows/package/Assets/Square44x44Logo.targetsize-24.png differ diff --git a/Windows/package/Assets/Square44x44Logo.targetsize-24_altform-unplated.png b/Windows/package/Assets/Square44x44Logo.targetsize-24_altform-unplated.png new file mode 100644 index 00000000..0ae92e96 Binary files /dev/null and b/Windows/package/Assets/Square44x44Logo.targetsize-24_altform-unplated.png differ diff --git a/Windows/package/Assets/Square44x44Logo.targetsize-256.png b/Windows/package/Assets/Square44x44Logo.targetsize-256.png new file mode 100644 index 00000000..adf65150 Binary files /dev/null and b/Windows/package/Assets/Square44x44Logo.targetsize-256.png differ diff --git a/Windows/package/Assets/Square44x44Logo.targetsize-256_altform-unplated.png b/Windows/package/Assets/Square44x44Logo.targetsize-256_altform-unplated.png new file mode 100644 index 00000000..adf65150 Binary files /dev/null and b/Windows/package/Assets/Square44x44Logo.targetsize-256_altform-unplated.png differ diff --git a/Windows/package/Assets/Square44x44Logo.targetsize-32.png b/Windows/package/Assets/Square44x44Logo.targetsize-32.png new file mode 100644 index 00000000..f85d75f0 Binary files /dev/null and b/Windows/package/Assets/Square44x44Logo.targetsize-32.png differ diff --git a/Windows/package/Assets/Square44x44Logo.targetsize-32_altform-unplated.png b/Windows/package/Assets/Square44x44Logo.targetsize-32_altform-unplated.png new file mode 100644 index 00000000..f85d75f0 Binary files /dev/null and b/Windows/package/Assets/Square44x44Logo.targetsize-32_altform-unplated.png differ diff --git a/Windows/package/Assets/Square44x44Logo.targetsize-48.png b/Windows/package/Assets/Square44x44Logo.targetsize-48.png new file mode 100644 index 00000000..474e8c76 Binary files /dev/null and b/Windows/package/Assets/Square44x44Logo.targetsize-48.png differ diff --git a/Windows/package/Assets/Square44x44Logo.targetsize-48_altform-unplated.png b/Windows/package/Assets/Square44x44Logo.targetsize-48_altform-unplated.png new file mode 100644 index 00000000..474e8c76 Binary files /dev/null and b/Windows/package/Assets/Square44x44Logo.targetsize-48_altform-unplated.png differ diff --git a/Windows/package/Assets/StoreLogo.scale-100.png b/Windows/package/Assets/StoreLogo.scale-100.png new file mode 100644 index 00000000..beab86b7 Binary files /dev/null and b/Windows/package/Assets/StoreLogo.scale-100.png differ diff --git a/Windows/package/Assets/StoreLogo.scale-200.png b/Windows/package/Assets/StoreLogo.scale-200.png new file mode 100644 index 00000000..7e81807e Binary files /dev/null and b/Windows/package/Assets/StoreLogo.scale-200.png differ diff --git a/Windows/package/Assets/Wide310x150Logo.scale-100.png b/Windows/package/Assets/Wide310x150Logo.scale-100.png new file mode 100644 index 00000000..712a70a8 Binary files /dev/null and b/Windows/package/Assets/Wide310x150Logo.scale-100.png differ diff --git a/Windows/package/Assets/Wide310x150Logo.scale-200.png b/Windows/package/Assets/Wide310x150Logo.scale-200.png new file mode 100644 index 00000000..454a8266 Binary files /dev/null and b/Windows/package/Assets/Wide310x150Logo.scale-200.png differ diff --git a/Windows/scripts/build-native.ps1 b/Windows/scripts/build-native.ps1 new file mode 100644 index 00000000..3d700e95 --- /dev/null +++ b/Windows/scripts/build-native.ps1 @@ -0,0 +1,45 @@ +# Builds swcore.dll (QuickJS-NG plus the host binding) with MSVC. +# +# pwsh Windows/scripts/build-native.ps1 [-Arch x64|arm64] [-Asan] +# +# Output: Windows/native/out/[-asan]/swcore.dll +param( + [ValidateSet('x64', 'arm64')] [string] $Arch = 'x64', + [switch] $Asan +) +$ErrorActionPreference = 'Stop' +$native = Join-Path $PSScriptRoot '..\native' | Resolve-Path +$name = if ($Asan) { "$Arch-asan" } else { $Arch } +$build = Join-Path $native "build\$name" +$out = Join-Path $native "out\$name" + +$vswhere = Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio\Installer\vswhere.exe' +if (-not (Test-Path $vswhere)) { throw 'Visual Studio with the C++ workload is required (vswhere.exe not found).' } +$vs = & $vswhere -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath +if (-not $vs) { throw 'No Visual Studio installation with the MSVC tools was found.' } +$vcvars = Join-Path $vs 'VC\Auxiliary\Build\vcvarsall.bat' +$target = if ($Arch -eq 'arm64') { if ($env:PROCESSOR_ARCHITECTURE -eq 'ARM64') { 'arm64' } else { 'x64_arm64' } } else { if ($env:PROCESSOR_ARCHITECTURE -eq 'ARM64') { 'arm64_x64' } else { 'x64' } } + +# Import the MSVC environment for the target into this process. +$lines = & cmd /c "`"$vcvars`" $target >nul 2>&1 && set" +if ($LASTEXITCODE -ne 0) { throw "vcvarsall $target failed" } +foreach ($line in $lines) { if ($line -match '^([^=]+)=(.*)$') { Set-Item -Path "env:$($Matches[1])" -Value $Matches[2] } } + +if (-not (Test-Path (Join-Path $env:VCToolsInstallDir "bin\Host$($env:VSCMD_ARG_HOST_ARCH)\$Arch\cl.exe"))) { + throw "The MSVC $Arch build tools are not installed. Add them in the Visual Studio Installer (MSVC $Arch build tools)." +} + +$asanFlag = if ($Asan) { 'ON' } else { 'OFF' } +& cmake -S $native -B $build -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=cl "-DSW_ASAN=$asanFlag" | Out-Host +if ($LASTEXITCODE -ne 0) { throw 'cmake configure failed' } +& cmake --build $build | Out-Host +if ($LASTEXITCODE -ne 0) { throw 'cmake build failed' } + +New-Item -ItemType Directory -Force $out | Out-Null +Copy-Item (Join-Path $build 'swcore.dll') $out -Force +if ($Asan) { + # The sanitizer runtime must sit next to the DLL that uses it. + $runtime = Get-ChildItem (Join-Path $env:VCToolsInstallDir "bin\Host$($env:VSCMD_ARG_HOST_ARCH)\$Arch") -Filter 'clang_rt.asan_dynamic-*.dll' | Select-Object -First 1 + if ($runtime) { Copy-Item $runtime.FullName $out -Force } +} +Write-Host "swcore -> $out" diff --git a/Windows/scripts/build-shared-js.mjs b/Windows/scripts/build-shared-js.mjs new file mode 100644 index 00000000..f8eb1b2c --- /dev/null +++ b/Windows/scripts/build-shared-js.mjs @@ -0,0 +1,39 @@ +// Generates the JavaScript assets the Windows runtime shares with the web +// preview engine and the Android app. Run after changing +// Web/preview-engine/src, the support bundle, or Android/core/src/main/js: +// +// node Windows/scripts/build-shared-js.mjs +// +// Output (not checked in): Windows/assets/scriptwidget/ and the parity +// corpus in Windows/tests/assets/. +import fs from "node:fs"; +import path from "node:path"; +import { pathToFileURL } from "node:url"; +import { buildSharedJS, repository } from "../../Scripts/lib/shared-js.mjs"; + +await buildSharedJS({ + output: path.join(repository, "Windows/assets/scriptwidget"), + banner: "// Generated by Windows/scripts/build-shared-js.mjs. Do not edit.\n", + compilerFingerprint: "scriptwidget-windows-babel-v1", + // The QuickJS host files are shared with Android. API files are evaluated + // in name order after host.js; the fallbacks in apis.js come last. + hostFiles: [ + ["android-host.js", "host.js"], + ["android-apis-datasource.js", "apis-10-datasource.js"], + ["android-apis-weather.js", "apis-20-weather.js"], + ["android-apis.js", "apis-90.js"], + ["android-compiler.js", "compiler.js"], + ], + // The Android directory keeps the module paths inside render.js identical + // to the Android bundle. + bundleDirectory: path.join(repository, "Android"), + corpusOutput: path.join(repository, "Windows/tests/assets"), +}); + +// The API reference of the management window: the metadata the code editor completes from +// (Editor/editorfe/src/scriptWidgetAPI.js), as data. Generating it keeps the two from disagreeing. +const { scriptWidgetAPI } = await import(pathToFileURL(path.join(repository, "Editor/editorfe/src/scriptWidgetAPI.js")).href); +fs.writeFileSync( + path.join(repository, "Windows/assets/scriptwidget/api-reference.json"), + JSON.stringify({ functions: scriptWidgetAPI.functions, components: scriptWidgetAPI.components, globalProperties: scriptWidgetAPI.globalProperties }) + "\n", +); diff --git a/Windows/scripts/desktop-check.ps1 b/Windows/scripts/desktop-check.ps1 new file mode 100644 index 00000000..e90d3a74 --- /dev/null +++ b/Windows/scripts/desktop-check.ps1 @@ -0,0 +1,248 @@ +# Checks the desktop host on a real Windows desktop without needing to look at +# the screen: it also works while the session is locked. +# +# powershell -File Windows/scripts/desktop-check.ps1 [-HostPath ] [-Output ] +# +# It starts the host with a scratch data folder, then checks that +# - the first start places a widget and its window shows a picture, +# - widget windows sit directly above the desktop and below every other window, +# - a drag moves the widget, snaps it to the screen edge and saves the place, +# - a button click runs the declared action, +# - display, clock, sleep, lock and theme events redraw the widgets, +# - the actions of the tray menu and of a widget's right-click menu work, +# - "show desktop" raises the widgets and puts them back, using a stand-in +# for the desktop window. +# Pictures of the widget windows are saved in the output folder. +# +# Pointer input is posted to the windows as messages; it is not a real mouse. +# What this cannot check is listed in docs/windows/TODO.md (the real Win+D, +# the tray menu, how the widgets look against the wallpaper). +param( + [string] $HostPath, + [string] $Output = (Join-Path $env:TEMP 'scriptwidget-desktop-check') +) +$ErrorActionPreference = 'Stop' +$windows = Resolve-Path (Join-Path $PSScriptRoot '..') +# The newest build that has the runtime assets next to it (an intermediate native AOT output has none). +if (-not $HostPath) { $HostPath = Get-ChildItem (Join-Path $windows 'src\ScriptWidget.App\bin') -Recurse -Filter ScriptWidget.exe | Where-Object { Test-Path (Join-Path $_.DirectoryName 'assets\scriptwidget\kernel.js') } | Sort-Object LastWriteTime -Descending | Select-Object -First 1 -ExpandProperty FullName } +if (-not $HostPath -or -not (Test-Path $HostPath)) { throw 'ScriptWidget.exe was not found. Build src/ScriptWidget.App or pass -HostPath.' } +$cli = Get-ChildItem (Join-Path $windows 'src\ScriptWidget.Cli\bin') -Recurse -Filter swcli.exe | Sort-Object LastWriteTime -Descending | Select-Object -First 1 -ExpandProperty FullName +if (-not $cli) { throw 'swcli.exe was not found. Build src/ScriptWidget.Cli.' } +if (Get-Process ScriptWidget -ErrorAction SilentlyContinue) { throw 'ScriptWidget is already running. Quit it first: this check uses its own data folder.' } + +Add-Type -AssemblyName System.Drawing +Add-Type @" +using System; using System.Runtime.InteropServices; using System.Text; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; +public static class SwCheck { + public delegate bool EnumProc(IntPtr h, IntPtr l); + [DllImport("user32.dll")] public static extern bool EnumWindows(EnumProc p, IntPtr l); + [DllImport("user32.dll")] public static extern IntPtr GetTopWindow(IntPtr h); + [DllImport("user32.dll")] public static extern IntPtr GetWindow(IntPtr h, uint cmd); + [DllImport("user32.dll", CharSet=CharSet.Unicode)] public static extern int GetClassName(IntPtr h, StringBuilder s, int n); + [DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr h); + [DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr h, out RECT r); + [DllImport("user32.dll")] public static extern bool PostMessage(IntPtr h, uint m, IntPtr w, IntPtr l); + [DllImport("user32.dll", CharSet=CharSet.Unicode)] public static extern IntPtr SendMessageTimeout(IntPtr h, uint m, IntPtr w, string l, uint flags, uint timeout, out IntPtr result); + [DllImport("user32.dll")] public static extern bool PrintWindow(IntPtr h, IntPtr dc, uint flags); + [DllImport("user32.dll")] public static extern IntPtr SetThreadDpiAwarenessContext(IntPtr c); + public struct RECT { public int L, T, R, B; } + public static string Class(IntPtr h) { var s = new StringBuilder(128); GetClassName(h, s, 128); return s.ToString(); } + public static List Find(string cls) { var r = new List(); EnumWindows((h, l) => { if (Class(h) == cls) r.Add(h); return true; }, IntPtr.Zero); return r; } + // Classes of the visible top-level windows, front to back. + public static List VisibleOrder() { var r = new List(); var h = GetTopWindow(IntPtr.Zero); while (h != IntPtr.Zero) { if (IsWindowVisible(h)) r.Add(Class(h)); h = GetWindow(h, 2); } return r; } + // Saves what the window itself shows and returns the share of pixels that are not transparent or black. + public static double Capture(IntPtr h, string file) { + RECT r; GetWindowRect(h, out r); int w = r.R - r.L, ht = r.B - r.T; if (w <= 0 || ht <= 0) return 0; + using (var bmp = new Bitmap(w, ht, PixelFormat.Format32bppArgb)) { + using (var g = Graphics.FromImage(bmp)) { var dc = g.GetHdc(); PrintWindow(h, dc, 2); g.ReleaseHdc(dc); } + bmp.Save(file, ImageFormat.Png); + int drawn = 0, total = 0; for (int y = 0; y < ht; y += 4) for (int x = 0; x < w; x += 4) { total++; var c = bmp.GetPixel(x, y); if (c.R + c.G + c.B > 24) drawn++; } + return total == 0 ? 0 : (double)drawn / total; } } +} +"@ -ReferencedAssemblies System.Drawing +[SwCheck]::SetThreadDpiAwarenessContext([IntPtr](-4)) | Out-Null + +$data = Join-Path $Output 'data' +if (Test-Path $Output) { Remove-Item $Output -Recurse -Force } +New-Item -ItemType Directory -Force $Output | Out-Null +$env:SCRIPTWIDGET_DATA = $data +$env:SCRIPTWIDGET_VERBOSE = '1' +$script:failures = 0 +function Check([string] $what, [bool] $ok, [string] $detail = '') { + if ($ok) { Write-Host "PASS $what $detail" } else { Write-Host "FAIL $what $detail"; $script:failures++ } +} +function LParam([int] $x, [int] $y) { [IntPtr]((($y -band 0xFFFF) -shl 16) -bor ($x -band 0xFFFF)) } +function Rect($h) { $r = New-Object SwCheck+RECT; [SwCheck]::GetWindowRect($h, [ref] $r) | Out-Null; $r } +function LogLines { if (Test-Path "$data\host.log") { Get-Content "$data\host.log" } else { @() } } +function Wait-Log([string] $pattern, [int] $from, [int] $seconds = 8) { + for ($i = 0; $i -lt $seconds * 4; $i++) { if ((LogLines | Select-Object -Skip $from) -match $pattern) { return $true }; Start-Sleep -Milliseconds 250 } + return $false +} + +# "Show desktop", acted out with a stand-in for the desktop window (the real one is not touched). +$showDesktopLog = Join-Path $Output 'showdesktop.log' +$selfTest = Start-Process $HostPath -ArgumentList '--selftest-showdesktop', "`"$showDesktopLog`"" -PassThru -Wait +if (Test-Path $showDesktopLog) { Get-Content $showDesktopLog | Where-Object { $_ -match '^(PASS|FAIL)' } | ForEach-Object { Write-Host ($_ -replace '^(PASS|FAIL) ', '$1 show desktop stand-in: ') } } +Check 'show desktop is handled (stand-in desktop window)' ($selfTest.ExitCode -eq 0) + +$process = Start-Process $HostPath -ArgumentList '--startup' -PassThru +try { + # First start + $deadline = (Get-Date).AddSeconds(25) + while ((Get-Date) -lt $deadline -and -not ((LogLines) -match 'drawn ')) { Start-Sleep -Milliseconds 500 } + $widgets = [SwCheck]::Find('ScriptWidgetWidget') + Check 'first start places one widget' ($widgets.Count -eq 1) "($($widgets.Count) widget windows)" + Check 'the host is still running' (-not $process.HasExited) + # Windows refuses the tray icon when no taskbar is running (a build agent, for example). + $tray = (LogLines) -match 'tray icon added: True' + if ($tray) { Check 'the tray icon is added' $true } else { Write-Host 'SKIP the tray icon was not accepted: no taskbar in this session' } + + # A second widget with a button + $template = Join-Path $windows '..\Shared\ScriptWidgetRuntime\Resource\Script.bundle\template\Habit Heatmap' + & $cli pack $template (Join-Path $Output 'habit.swt') | Out-Null + & $cli install (Join-Path $Output 'habit.swt') | Out-Null + $before = (LogLines).Count + & $cli add 'Habit Heatmap' --family medium | Out-Null + Check 'a second widget is added and drawn' (Wait-Log 'drawn ' $before) + # The second window exists before its first picture is drawn; wait until both are shown. + for ($i = 0; $i -lt 40; $i++) { + $widgets = [SwCheck]::Find('ScriptWidgetWidget') + $shown = @($widgets | Where-Object { $r = Rect $_; [SwCheck]::IsWindowVisible($_) -and ($r.R - $r.L) -gt 0 }) + if ($shown.Count -eq 2) { break } + Start-Sleep -Milliseconds 250 + } + Check 'two widget windows exist' ($widgets.Count -eq 2) + + # Pictures of what the windows show + $index = 0 + foreach ($h in $widgets) { + $index++ + $share = [SwCheck]::Capture($h, (Join-Path $Output "widget$index.png")) + $r = Rect $h + Check "widget $index shows a picture" ($share -gt 0.2) ("({0}x{1} pixels, {2:P0} drawn)" -f ($r.R - $r.L), ($r.B - $r.T), $share) + } + + # Window order: widgets directly above the desktop, below everything else + $order = [SwCheck]::VisibleOrder() + $desktop = [Array]::LastIndexOf([string[]] $order, 'Progman') + $positions = 0..($order.Count - 1) | Where-Object { $order[$_] -eq 'ScriptWidgetWidget' } + Check 'the desktop window is the last visible window' ($desktop -eq $order.Count - 1) + Check 'the widgets are directly above the desktop window' (($positions | Measure-Object -Maximum).Maximum -eq $desktop - 1 -and ($positions | Measure-Object -Minimum).Minimum -eq $desktop - $widgets.Count) + + # Drag: 240 pixels left and up to 3 pixels below the top of its display + $h = $widgets | Where-Object { (Rect $_).T -gt 20 } | Select-Object -First 1 + if (-not $h) { $h = $widgets[0] } + $start = Rect $h + [SwCheck]::PostMessage($h, 0x201, [IntPtr]1, (LParam 200 100)) | Out-Null + [SwCheck]::PostMessage($h, 0x200, [IntPtr]1, (LParam (200 - 240) (100 - $start.T + 3))) | Out-Null + Start-Sleep -Milliseconds 300 + [SwCheck]::PostMessage($h, 0x202, [IntPtr]0, (LParam 200 100)) | Out-Null + Start-Sleep -Seconds 1 + $moved = Rect $h + Check 'a drag moves the widget' ($moved.L -eq $start.L - 240) "($($start.L),$($start.T) -> $($moved.L),$($moved.T))" + Check 'the widget snaps to the top edge' ($moved.T -eq 0) + $saved = (Get-Content "$data\widgets.json" -Raw | ConvertFrom-Json).widgets + Check 'the new place is saved' ([bool]($saved | Where-Object { $_.y -eq 0 })) + + # Click the "Check in" button of the Habit Heatmap widget (12..80 x 133..147 points) + $scale = ((Rect $widgets[0]).R - (Rect $widgets[0]).L) / 329.0 + $before = (LogLines).Count + foreach ($w in $widgets) { + $point = LParam ([int](40 * $scale)) ([int](140 * $scale)) + [SwCheck]::PostMessage($w, 0x201, [IntPtr]1, $point) | Out-Null + [SwCheck]::PostMessage($w, 0x202, [IntPtr]0, $point) | Out-Null + } + Check 'a click on a button runs the declared action' (Wait-Log 'action Habit Heatmap: ok' $before) + $storage = Get-ChildItem "$data\Storage" -ErrorAction SilentlyContinue | ForEach-Object { Get-Content $_.FullName -Raw } + Check 'the action wrote to the package storage' ("$storage" -match 'habitHeatmap') + + # System events + $hostWindow = [SwCheck]::Find('ScriptWidgetHost') | Select-Object -First 1 + $events = @( + @('display change', 0x7E, 32, 'display change'), + @('clock change', 0x1E, 0, 'time change'), + @('resume from sleep', 0x218, 0x12, 'resume from sleep'), + @('session lock', 0x2B1, 7, 'session locked'), + @('session unlock', 0x2B1, 8, 'session unlocked') + ) + foreach ($event in $events) { + $before = (LogLines).Count + [SwCheck]::PostMessage($hostWindow, $event[1], [IntPtr]$event[2], [IntPtr]0) | Out-Null + $seen = Wait-Log $event[3] $before 5 + $redrawn = if ($event[0] -eq 'session lock') { $true } else { Wait-Log 'drawn ' $before 8 } + Check "$($event[0]) is handled" ($seen -and $redrawn) + } + $before = (LogLines).Count + $result = [IntPtr]::Zero + [SwCheck]::SendMessageTimeout($hostWindow, 0x1A, [IntPtr]0, 'ImmersiveColorSet', 2, 2000, [ref] $result) | Out-Null + Check 'a theme change redraws the widgets' ((Wait-Log 'theme or format change' $before 5) -and (Wait-Log 'drawn ' $before 8)) + $after = Rect $h + Check 'the widget keeps its place through the events' ($after.L -eq $moved.L -and $after.T -eq $moved.T) + # Menus: every action of the tray menu and of a widget's right-click menu, run through the + # host's debugging requests (the popup itself needs a pointer and is not shown). + function Send-Host($request) { + $file = Join-Path $Output 'request.json' + # The request goes through a file name-safe argument: quotes inside JSON do not survive a command line. + $json = ($request | ConvertTo-Json -Compress -Depth 6) -replace '"', '\"' + (& $cli host $json | Out-String) | ConvertFrom-Json + } + function Widget-List { (& $cli widgets | Out-String | ConvertFrom-Json).widgets } + $tray = Send-Host @{ command = 'debug-menu'; target = 'tray' } + Check 'the tray menu has its entries' ($tray.ok -and $tray.items.Count -eq 8 -and $tray.items[1].children.Count -ge 2) "($(($tray.items | Where-Object { $_.text } | ForEach-Object { $_.text }) -join ' | '))" + $list = Widget-List + $target = $list | Select-Object -First 1 + $menu = Send-Host @{ command = 'debug-menu'; target = $target.id } + Check 'a widget menu has its entries' ($menu.ok -and $menu.items.Count -eq 7 -and $menu.items[1].children.Count -ge 1) + # Size: pick a size that is not the current one and watch the window change. + $sizes = $menu.items[1].children + $other = 0..($sizes.Count - 1) | Where-Object { -not $sizes[$_].checked } | Select-Object -First 1 + $countBefore = (LogLines).Count + $widthsBefore = ([SwCheck]::Find('ScriptWidgetWidget') | ForEach-Object { (Rect $_).R - (Rect $_).L } | Sort-Object) -join ',' + $answer = Send-Host @{ command = 'debug-invoke'; target = $target.id; path = @(1, $other) } + $drawn = Wait-Log 'drawn ' $countBefore + Start-Sleep -Milliseconds 500 + $widthsAfter = ([SwCheck]::Find('ScriptWidgetWidget') | ForEach-Object { (Rect $_).R - (Rect $_).L } | Sort-Object) -join ',' + Check 'the size menu changes the widget size' ($answer.ok -and $drawn -and $widthsAfter -ne $widthsBefore) "($widthsBefore -> $widthsAfter pixels wide)" + # Lock positions: a drag must not move a locked widget. + $answer = Send-Host @{ command = 'debug-invoke'; target = 'tray'; path = @(4) } + $locked = (Send-Host @{ command = 'debug-menu'; target = 'tray' }).items[4].checked + $h2 = [SwCheck]::Find('ScriptWidgetWidget') | Select-Object -First 1 + $place = Rect $h2 + [SwCheck]::PostMessage($h2, 0x201, [IntPtr]1, (LParam 60 60)) | Out-Null + [SwCheck]::PostMessage($h2, 0x200, [IntPtr]1, (LParam 160 160)) | Out-Null + Start-Sleep -Milliseconds 200 + [SwCheck]::PostMessage($h2, 0x202, [IntPtr]0, (LParam 160 160)) | Out-Null + Start-Sleep -Milliseconds 500 + $still = Rect $h2 + Check 'lock positions stops a drag' ($answer.ok -and $locked -and $still.L -eq $place.L -and $still.T -eq $place.T) + Send-Host @{ command = 'debug-invoke'; target = 'tray'; path = @(4) } | Out-Null + Check 'lock positions turns off again' (-not (Send-Host @{ command = 'debug-menu'; target = 'tray' }).items[4].checked) + # Add widget from the tray, refresh all, remove from the widget menu. + $before = @(Widget-List).Count + $answer = Send-Host @{ command = 'debug-invoke'; target = 'tray'; path = @(1, 0) } + Start-Sleep -Seconds 2 + Check 'the tray menu adds a widget' ($answer.ok -and @(Widget-List).Count -eq $before + 1) + $countBefore = (LogLines).Count + $answer = Send-Host @{ command = 'debug-invoke'; target = 'tray'; path = @(3) } + Check 'refresh all redraws the widgets' ($answer.ok -and (Wait-Log 'drawn ' $countBefore)) + $newest = Widget-List | Select-Object -Last 1 + $answer = Send-Host @{ command = 'debug-invoke'; target = $newest.id; path = @(6) } + Start-Sleep -Seconds 1 + Check 'the widget menu removes the widget' ($answer.ok -and @(Widget-List).Count -eq $before -and @([SwCheck]::Find('ScriptWidgetWidget')).Count -eq $before) + + Check 'the host is still running at the end' (-not $process.HasExited) + # Quit from the tray menu ends the program by itself. + $errorsBeforeQuit = LogLines | Where-Object { $_ -match 'failed|crashed|refused' } + Send-Host @{ command = 'debug-invoke'; target = 'tray'; path = @(7) } | Out-Null + $quit = $process.WaitForExit(8000) + Check 'quit from the tray menu ends the host' ($quit -and $process.ExitCode -eq 0) + Check 'the place survives a restart' ([bool]((Get-Content "$data\widgets.json" -Raw | ConvertFrom-Json).widgets | Where-Object { $_.y -eq 0 })) + $errors = LogLines | Where-Object { $_ -match 'failed|crashed|refused' } + Check 'the log holds no failures' (-not $errors) ($errors | Select-Object -First 2 | Out-String) +} +finally { + if (-not $process.HasExited) { $process | Stop-Process -Force } +} +Write-Host "pictures and log -> $Output" +if ($script:failures -gt 0) { Write-Host "$($script:failures) check(s) failed"; exit 1 } +Write-Host 'all checks passed' diff --git a/Windows/scripts/make-icons.ps1 b/Windows/scripts/make-icons.ps1 new file mode 100644 index 00000000..170f84a8 --- /dev/null +++ b/Windows/scripts/make-icons.ps1 @@ -0,0 +1,83 @@ +# Generates the Windows icon and the MSIX tile images from the shared app icon +# (Resource/AppIcon/ScriptWidget-AppIcon-2026.png). The output is checked in; +# run this again only when the app icon changes. +# +# powershell -File Windows/scripts/make-icons.ps1 +$ErrorActionPreference = 'Stop' +Add-Type -AssemblyName System.Drawing +$repository = Resolve-Path (Join-Path $PSScriptRoot '..\..') +$source = Join-Path $repository 'Resource\AppIcon\ScriptWidget-AppIcon-2026.png' +$assets = Join-Path $repository 'Windows\package\Assets' +New-Item -ItemType Directory -Force $assets | Out-Null +$original = [System.Drawing.Image]::FromFile($source) + +function New-Scaled([int] $width, [int] $height, [double] $iconFraction = 1.0, [bool] $rounded = $false) { + $bitmap = New-Object System.Drawing.Bitmap $width, $height, ([System.Drawing.Imaging.PixelFormat]::Format32bppArgb) + $graphics = [System.Drawing.Graphics]::FromImage($bitmap) + $graphics.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic + $graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias + $graphics.PixelOffsetMode = [System.Drawing.Drawing2D.PixelOffsetMode]::HighQuality + $graphics.Clear([System.Drawing.Color]::Transparent) + $side = [Math]::Round([Math]::Min($width, $height) * $iconFraction) + $x = [Math]::Round(($width - $side) / 2) + $y = [Math]::Round(($height - $side) / 2) + if ($rounded) { + # The source icon is a full square; Windows icons look right with rounded corners. + $radius = $side * 0.22 + $path = New-Object System.Drawing.Drawing2D.GraphicsPath + $d = $radius * 2 + $path.AddArc($x, $y, $d, $d, 180, 90) + $path.AddArc($x + $side - $d, $y, $d, $d, 270, 90) + $path.AddArc($x + $side - $d, $y + $side - $d, $d, $d, 0, 90) + $path.AddArc($x, $y + $side - $d, $d, $d, 90, 90) + $path.CloseFigure() + $graphics.SetClip($path) + } + $graphics.DrawImage($original, $x, $y, $side, $side) + $graphics.Dispose() + return $bitmap +} + +function Save-Png($bitmap, [string] $name) { + $bitmap.Save((Join-Path $assets $name), [System.Drawing.Imaging.ImageFormat]::Png) + $bitmap.Dispose() +} + +# MSIX tile images: the icon on a transparent tile. +foreach ($scale in 100, 200) { + $f = $scale / 100 + Save-Png (New-Scaled (44 * $f) (44 * $f) 1.0 $true) "Square44x44Logo.scale-$scale.png" + Save-Png (New-Scaled (150 * $f) (150 * $f) 0.6 $true) "Square150x150Logo.scale-$scale.png" + Save-Png (New-Scaled (310 * $f) (150 * $f) 0.6 $true) "Wide310x150Logo.scale-$scale.png" + Save-Png (New-Scaled (50 * $f) (50 * $f) 1.0 $true) "StoreLogo.scale-$scale.png" +} +foreach ($size in 16, 24, 32, 48, 256) { + Save-Png (New-Scaled $size $size 1.0 $true) "Square44x44Logo.targetsize-$size.png" + Save-Png (New-Scaled $size $size 1.0 $true) "Square44x44Logo.targetsize-${size}_altform-unplated.png" +} + +# ScriptWidget.ico: PNG-compressed entries, which Windows Vista and later read. +$sizes = 16, 20, 24, 32, 40, 48, 64, 256 +$images = foreach ($size in $sizes) { + $bitmap = New-Scaled $size $size 1.0 $true + $stream = New-Object System.IO.MemoryStream + $bitmap.Save($stream, [System.Drawing.Imaging.ImageFormat]::Png) + $bitmap.Dispose() + , $stream.ToArray() +} +$file = New-Object System.IO.MemoryStream +$writer = New-Object System.IO.BinaryWriter $file +$writer.Write([uint16]0); $writer.Write([uint16]1); $writer.Write([uint16]$sizes.Count) +$offset = 6 + 16 * $sizes.Count +for ($index = 0; $index -lt $sizes.Count; $index++) { + $size = $sizes[$index] + $writer.Write([byte]($size -band 0xFF)); $writer.Write([byte]($size -band 0xFF)) + $writer.Write([byte]0); $writer.Write([byte]0) + $writer.Write([uint16]1); $writer.Write([uint16]32) + $writer.Write([uint32]$images[$index].Length); $writer.Write([uint32]$offset) + $offset += $images[$index].Length +} +foreach ($image in $images) { $writer.Write($image) } +[System.IO.File]::WriteAllBytes((Join-Path $assets 'ScriptWidget.ico'), $file.ToArray()) +$original.Dispose() +Write-Host "icons -> $assets" diff --git a/Windows/scripts/package.ps1 b/Windows/scripts/package.ps1 new file mode 100644 index 00000000..0b23457c --- /dev/null +++ b/Windows/scripts/package.ps1 @@ -0,0 +1,86 @@ +# Builds the ScriptWidget MSIX package. +# +# powershell -File Windows/scripts/package.ps1 [-Arch x64|arm64] [-Version 0.1.0.0] +# [-IdentityName ] [-Publisher ] [-PublisherDisplayName ] +# [-FrameworkDependent] [-SkipBuild] +# +# -IdentityName, -Publisher and -PublisherDisplayName must be the values +# Partner Center shows under "Product identity" for a Store submission. The +# defaults only produce a package for local testing. +# +# By default the package carries the Windows App SDK inside it, so it installs +# on a clean machine. -FrameworkDependent declares the Windows App Runtime +# framework package instead, which the Microsoft Store installs by itself and +# which makes the package much smaller. +# +# Output: Windows/artifacts/ScriptWidget__.msix, or +# ..._-store.msix with -FrameworkDependent (not signed; the Store signs +# submissions), and the layout folder it was packed from. The Store form only +# runs inside a package: register its layout folder with +# Add-AppxPackage -Register AppxManifest.xml (Developer Mode) to try it. +param( + [ValidateSet('x64', 'arm64')] [string] $Arch = 'x64', + [string] $Version = '0.1.0.0', + [string] $IdentityName = 'AtomSprout.ScriptWidget', + [string] $Publisher = 'CN=ScriptWidget Local Test', + [string] $PublisherDisplayName = 'AtomSprout', + [switch] $FrameworkDependent, + [switch] $SkipBuild +) +$ErrorActionPreference = 'Stop' +$windows = Resolve-Path (Join-Path $PSScriptRoot '..') +$repository = Resolve-Path (Join-Path $windows '..') +$platform = if ($Arch -eq 'arm64') { 'ARM64' } else { 'x64' } +$rid = "win-$Arch" +$artifacts = Join-Path $windows 'artifacts' +# The two forms are kept apart: the Store form only runs inside a package. +$form = if ($FrameworkDependent) { "$Arch-store" } else { $Arch } +$layout = Join-Path $artifacts "layout-$form" + +function Invoke-Checked([scriptblock] $command, [string] $what) { + & $command + if ($LASTEXITCODE -ne 0) { throw "$what failed with exit code $LASTEXITCODE" } +} + +if (-not $SkipBuild) { + # vswhere must be on PATH for the native AOT link step. + $installer = Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio\Installer' + if (Test-Path $installer) { $env:PATH = "$installer;$env:PATH" } + Invoke-Checked { node (Join-Path $windows 'scripts\build-shared-js.mjs') } 'shared JavaScript' + & (Join-Path $windows 'scripts\build-native.ps1') -Arch $Arch + if (Test-Path $layout) { Remove-Item $layout -Recurse -Force } + $dependencyMode = if ($FrameworkDependent) { 'true' } else { 'false' } + $common = @('-c', 'Release', '-r', $rid, "-p:Platform=$platform", "-p:Version=$($Version -replace '\.\d+$', '')", "-p:SwFrameworkDependent=$dependencyMode", '--nologo', '-v', 'q', '-o', $layout) + # The always-running host is compiled ahead of time: no .NET runtime to load, less memory. + Invoke-Checked { dotnet publish (Join-Path $windows 'src\ScriptWidget.App') @common '-p:PublishAot=true' } 'host publish' + Invoke-Checked { dotnet publish (Join-Path $windows 'src\ScriptWidget.Studio') @common '--self-contained' 'true' } 'management window publish' +} +if (-not (Test-Path (Join-Path $layout 'ScriptWidget.exe'))) { throw "No build in $layout. Run without -SkipBuild." } + +# Debug symbols and build leftovers do not belong in the package. +Get-ChildItem $layout -Recurse -Include '*.pdb', '*.lib', '*.exp' | Remove-Item -Force + +# Tile and taskbar images belong to the management window's project: its resources.pri indexes them +# with their qualifiers (scale, target size, unplated) beside its XAML resources. Without the +# unplated images the taskbar draws the icon on a plate in the user's accent color. +if (-not (Test-Path (Join-Path $layout 'Assets\Square44x44Logo.targetsize-24_altform-unplated.png'))) { throw 'The tile images were not published into the layout.' } +if (-not (Test-Path (Join-Path $layout 'resources.pri'))) { throw 'resources.pri is missing: the management window was not published into the layout.' } + +$dependencies = '' +if ($FrameworkDependent) { + # Matches the Windows App SDK 1.6 the projects reference. + $dependencies = " `r`n" +} +$manifest = Get-Content (Join-Path $windows 'package\AppxManifest.xml') -Raw +$manifest = $manifest.Replace('{{IdentityName}}', $IdentityName).Replace('{{Publisher}}', $Publisher).Replace('{{PublisherDisplayName}}', $PublisherDisplayName) +$manifest = $manifest.Replace('{{Version}}', $Version).Replace('{{Architecture}}', $Arch).Replace('{{FrameworkDependencies}}', $dependencies) +[System.IO.File]::WriteAllText((Join-Path $layout 'AppxManifest.xml'), $manifest, (New-Object System.Text.UTF8Encoding $false)) + +$kits = Join-Path ${env:ProgramFiles(x86)} 'Windows Kits\10\bin' +$makeappx = Get-ChildItem $kits -Recurse -Filter makeappx.exe | Where-Object { $_.FullName -match '\\x64\\' } | Sort-Object FullName -Descending | Select-Object -First 1 +if (-not $makeappx) { throw 'makeappx.exe was not found. Install the Windows SDK.' } +$package = Join-Path $artifacts "ScriptWidget_${Version}_$form.msix" +if (Test-Path $package) { Remove-Item $package -Force } +Invoke-Checked { & $makeappx.FullName pack /d $layout /p $package /o | Out-Null } 'makeappx pack' +$size = (Get-Item $package).Length / 1MB +Write-Host ("package -> {0} ({1:N1} MB)" -f $package, $size) diff --git a/Windows/src/ScriptWidget.App/AppHost.cs b/Windows/src/ScriptWidget.App/AppHost.cs new file mode 100644 index 00000000..d73acb23 --- /dev/null +++ b/Windows/src/ScriptWidget.App/AppHost.cs @@ -0,0 +1,726 @@ +using System.Collections.Concurrent; +using System.Runtime.InteropServices; +using System.Text.Json; +using System.Text.Json.Nodes; +using ScriptWidget.App.Interop; +using ScriptWidget.App.Windowing; +using ScriptWidget.Core; +using ScriptWidget.Core.Api; +using ScriptWidget.Core.Hosting; +using ScriptWidget.Core.Render; +using ScriptWidget.Core.Surfaces; + +namespace ScriptWidget.App; + +/// What a click asked for. Replayed against the package on the refresh thread. +internal sealed record WidgetAction(string Type, string? ActionID, string? Function, string? StateKey, bool? Value); + +/// One widget on the desktop. Used on the UI thread only. +internal sealed class WidgetController(WidgetPlacement placement, WidgetWindow window) +{ + public WidgetPlacement Placement { get; } = placement; + public WidgetWindow Window { get; } = window; + public IReadOnlyList HitRegions { get; set; } = []; + /// Pixels per point of the bitmap on screen. + public double RenderedScale { get; set; } = 1; + public RunRequest? LastRequest { get; set; } + public RunResult? LastResult { get; set; } + /// When to run the script again (Environment.TickCount64). + public long NextRun { get; set; } = long.MaxValue; + /// When to redraw the same tree because a date in it changed; long.MaxValue when it has none. + public long NextRedraw { get; set; } = long.MaxValue; + public bool Busy { get; set; } + public bool Placed { get; set; } + /// Requests that arrived while a refresh was running. + public bool RunAgain { get; set; } + public WidgetAction? PendingAction { get; set; } +} + +/// +/// The always-running desktop host: owns the widget windows, runs packages on +/// a schedule, handles clicks and the tray menu, and answers the management +/// window over the local pipe. Windows and menus live on the UI thread; +/// scripts and rendering run on one background thread, one widget at a time. +/// +internal sealed unsafe class AppHost : NativeWindow +{ + private const nuint ScheduleTimer = 1; + private const nuint TrimTimer = 2; + /// How long after the last render the layout engine is let go when nothing needs it soon. + private const uint TrimDelayMilliseconds = 20_000; + private const uint WM_WTSSESSION_CHANGE = 0x02B1; + private const int WTS_SESSION_LOCK = 7, WTS_SESSION_UNLOCK = 8; + + [DllImport("wtsapi32.dll")] private static extern int WTSRegisterSessionNotification(nint window, uint flags); + [DllImport("wtsapi32.dll")] private static extern int WTSUnRegisterSessionNotification(nint window); + + private readonly DataPaths paths; + private readonly HostSettings settings; + private readonly WidgetPlacements placements; + private readonly Library library; + private readonly Lazy runtime; + private readonly UiDispatcher ui; + private readonly DesktopLayer layer; + private readonly TrayIcon tray; + private readonly Dictionary widgets = new(); + private readonly BlockingCollection work = new(); + private readonly CancellationTokenSource stopping = new(); + private bool sessionLocked; + private bool quitting; + + private AppHost(DataPaths paths) + { + this.paths = paths; + settings = new HostSettings(paths.Settings); + placements = new WidgetPlacements(paths.Widgets); + library = new Library(paths); + runtime = new Lazy(() => new WidgetRuntime(paths)); + ui = new UiDispatcher(); + layer = new DesktopLayer(); + // A hidden top-level window: message-only windows do not receive the system broadcasts used below. + Create("ScriptWidgetHost", Win32.WS_EX_TOOLWINDOW, Win32.WS_POPUP); + WTSRegisterSessionNotification(Handle, 0); + tray = new TrayIcon(Strings.TrayTip, Path.Combine(AppContext.BaseDirectory, "ScriptWidget.ico")) { Menu = TrayMenu }; + tray.Activated += () => Studio.Open(); + layer.TaskbarCreated += tray.Add; + var worker = new Thread(() => { foreach (var job in work.GetConsumingEnumerable()) { try { job(); } catch (Exception error) { Log.Write($"refresh job failed: {error}"); } } }) + { Name = "scriptwidget-refresh", IsBackground = true }; + worker.Start(); + } + + /// Runs the host until the user quits. Returns the process exit code. + public static int Run(string[] arguments) + { + using var single = new Mutex(true, @"Local\ScriptWidget.Host", out var first); + if (!first) + { + // Another instance owns the desktop: ask it to show the management window instead. + if (!arguments.Contains("--startup")) HostPipe.Send(new JsonObject { ["command"] = "show-studio" }); + return 0; + } + var paths = new DataPaths(); + Log.File = paths.Log; + Log.Write($"host started, version {typeof(AppHost).Assembly.GetName().Version}, packaged {Startup.IsPackaged}"); + using var host = new AppHost(paths); + host.Start(arguments); + Win32.MSG message; + while (Win32.GetMessageW(&message, 0, 0, 0) > 0) + { + Win32.TranslateMessage(&message); + Win32.DispatchMessageW(&message); + } + Log.Write("host stopped"); + return 0; + } + + private void Start(string[] arguments) + { + foreach (var placement in placements.All()) AddController(placement); + _ = Task.Run(() => HostPipe.ServeAsync(HandleRequest, stopping.Token)); + // Loading the runtime reads a few megabytes of script; do it off the UI thread. + work.Add(() => _ = runtime.Value); + foreach (var controller in widgets.Values) QueueRefresh(controller); + if (!settings.Welcomed) + { + settings.Welcomed = true; + work.Add(FirstRun); + if (!arguments.Contains("--startup")) Studio.Open(); + } + ArmTimer(); + } + + /// Puts a first widget on the desktop so a new user sees what the app does. + private void FirstRun() + { + try + { + if (library.List().Count > 0 || placements.All().Count > 0) return; + var bundled = library.Bundled(); + var starter = bundled.FirstOrDefault(item => item.Name == "Datetime Current") ?? bundled.FirstOrDefault(); + if (starter is null) return; + var installed = library.InstallBundled(starter); + if (placements.Add(installed.Installation, "medium") is { } placement) ui.Post(() => { var controller = AddController(placement); QueueRefresh(controller); }); + } + catch (Exception error) { Log.Write($"first run: {error.Message}"); } + } + + // MARK: - Widgets + + private WidgetController AddController(WidgetPlacement placement) + { + var window = new WidgetWindow { Locked = settings.Locked }; + var controller = new WidgetController(placement, window); + widgets[placement.ID] = controller; + window.Clicked += (x, y) => OnClick(controller, x, y); + window.Moved += () => OnMoved(controller); + window.MenuRequested += (x, y) => PopupMenu.Show(window.Handle, x, y, WidgetMenu(controller)); + window.ScaleChanged += () => QueueRefresh(controller, redrawOnly: true); + window.IsInteractive = (x, y) => HitTest(controller, x, y) is not null; + Position(controller); + layer.Add(window); + return controller; + } + + private void RemoveController(string id) + { + if (!widgets.Remove(id, out var controller)) return; + layer.Remove(controller.Window); + controller.Window.Dispose(); + placements.Remove(id); + } + + /// Moves the window to its saved place, or to a free spot on the primary display for a new widget. + private void Position(WidgetController controller) + { + var placement = controller.Placement; + var monitors = Monitors.All(); + var (pointWidth, pointHeight) = WidgetFamilies.Size(placement.Family); + if (placement.Monitor.Length == 0) + { + // A new widget: cascade from the top-right corner of the primary work area. + var primary = monitors.FirstOrDefault(item => item.Primary) ?? Monitors.Primary(); + var margin = (int)Math.Round(24 * primary.Scale); + var width = (int)Math.Round(pointWidth * primary.Scale); + placement.Monitor = primary.Device; + placement.Scale = primary.Scale; + // The first free step of the cascade, so a new widget never lands exactly on another one. + for (var index = 0; index < WidgetPlacements.MaximumWidgets; index++) + { + var step = (int)Math.Round(28 * primary.Scale) * index; + placement.OffsetX = primary.Work.Right - primary.Bounds.Left - width - margin - step; + placement.OffsetY = primary.Work.Top - primary.Bounds.Top + margin + step; + if (!widgets.Values.Any(other => other.Placement != placement && other.Placement.Monitor == placement.Monitor && other.Placement.OffsetX == placement.OffsetX && other.Placement.OffsetY == placement.OffsetY)) break; + } + placements.Update(placement); + } + // A display that was unplugged keeps its name in the placement, so the widget returns when it is back. + var monitor = monitors.FirstOrDefault(item => item.Device == placement.Monitor) ?? monitors.FirstOrDefault(item => item.Primary) ?? Monitors.Primary(); + var ratio = monitor.Scale / (placement.Scale <= 0 ? 1 : placement.Scale); + var x = monitor.Bounds.Left + (int)Math.Round(placement.OffsetX * ratio); + var y = monitor.Bounds.Top + (int)Math.Round(placement.OffsetY * ratio); + var pixelWidth = (int)Math.Round(pointWidth * monitor.Scale); + var pixelHeight = (int)Math.Round(pointHeight * monitor.Scale); + // Keep at least a grabbable part of the widget on the display. + var keep = (int)Math.Round(48 * monitor.Scale); + x = Math.Clamp(x, monitor.Bounds.Left - pixelWidth + keep, monitor.Bounds.Right - keep); + y = Math.Clamp(y, monitor.Bounds.Top, monitor.Bounds.Bottom - keep); + controller.Window.MoveTo(x, y); + _ = pixelHeight; + } + + private void OnMoved(WidgetController controller) + { + if (Log.Verbose) Log.Write($"moved to {controller.Window.X},{controller.Window.Y}"); + var window = controller.Window; + var monitor = Monitors.Containing(window.X + window.Width / 2, window.Y + window.Height / 2); + var placement = controller.Placement; + placement.Monitor = monitor.Device; + placement.Scale = monitor.Scale; + placement.OffsetX = window.X - monitor.Bounds.Left; + placement.OffsetY = window.Y - monitor.Bounds.Top; + placements.Update(placement); + if (Math.Abs(monitor.Scale - controller.RenderedScale) > 0.01) QueueRefresh(controller, redrawOnly: true); + } + + // MARK: - Refresh + + /// Queues a run of the widget's script (or only a redraw of its last tree). Call on the UI thread. + private void QueueRefresh(WidgetController controller, WidgetAction? action = null, bool redrawOnly = false) + { + if (controller.Busy) + { + if (action is not null) controller.PendingAction = action; + if (!redrawOnly || action is not null) controller.RunAgain = true; + return; + } + controller.Busy = true; + var placement = controller.Placement; + var family = placement.Family; + var parameter = placement.Parameter; + var package = placement.Package; + var scale = controller.Window.Scale; + var dark = DeviceInfo.IsDarkMode(); + var previousRequest = redrawOnly ? controller.LastRequest : null; + var previousResult = redrawOnly ? controller.LastResult : null; + work.Add(() => + { + RenderOutput? output = null; + RunRequest? request = previousRequest; + RunResult? result = previousResult; + try + { + var (width, height) = WidgetFamilies.Size(family); + var engine = runtime.Value; + if (request is null || result is null || request.Family != family || request.DarkMode != dark) + { + var installed = library.FindByInstallation(package); + if (installed is null) + { + request = null; + result = null; + output = engine.RenderTree(WidgetRuntime.NoticeTree(Strings.MissingTitle, Strings.MissingDetail), family, (float)width, (float)height, (float)scale, dark); + } + else + { + if (action is not null) Perform(engine, installed, family, parameter, dark, action); + request = new RunRequest { Files = installed.Files, PackageID = installed.Installation, Family = family, Parameter = parameter, DarkMode = dark, BuildDirectory = installed.BuildDirectory }; + result = engine.Run(request); + if (result.Execution.Error is { } error) Log.Write($"widget {installed.Name}: {error.Kind}"); + } + } + if (output is null && request is not null && result is not null) output = engine.Render(request, result, (float)width, (float)height, (float)scale); + } + catch (Exception error) when (error is not OutOfMemoryException) + { + Log.Write($"refresh failed: {error.GetType().Name}: {error.Message}"); + } + var finishedRequest = request; + var finishedResult = result; + ui.Post(() => Apply(controller, output, finishedRequest, finishedResult, scale)); + }); + } + + /// Shows a finished render and schedules the next one. Runs on the UI thread. + private void Apply(WidgetController controller, RenderOutput? output, RunRequest? request, RunResult? result, double scale) + { + controller.Busy = false; + if (!widgets.ContainsKey(controller.Placement.ID)) return; + if (output is not null) + { + controller.Window.SetBitmap(output.Pixels, output.PixelWidth, output.PixelHeight); + controller.HitRegions = output.HitRegions; + controller.RenderedScale = output.Scale; + if (!controller.Placed) { controller.Placed = true; layer.Reassert(); } + } + controller.LastRequest = request; + controller.LastResult = result; + if (Log.Verbose) Log.Write($"drawn {controller.Placement.Family} {output?.PixelWidth}x{output?.PixelHeight} layout {output?.LayoutMilliseconds} ms paint {output?.PaintMilliseconds} ms"); + var now = Environment.TickCount64; + var next = now + settings.RefreshMinutes * 60_000L; + if (result?.NextTimelineTransition is { } transition) + { + var wait = transition - DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + next = Math.Min(next, now + Math.Max(wait, 1000)); + } + controller.NextRun = next; + controller.NextRedraw = NextRedraw(result?.Execution.Tree, now); + if (controller.RunAgain || controller.PendingAction is not null) + { + var action = controller.PendingAction; + controller.RunAgain = false; + controller.PendingAction = null; + QueueRefresh(controller, action); + } + ArmTimer(); + } + + /// + /// When a tree with dates must be drawn again: a running timer every + /// second, a clock time at the next minute, anything else when the script + /// runs again. + /// + private static long NextRedraw(string? tree, long now) + { + if (tree is null || !tree.Contains("\"date\"", StringComparison.Ordinal)) return long.MaxValue; + var interval = long.MaxValue; + void Visit(JsonNode? node) + { + switch (node) + { + case JsonObject item: + if (item.OptString("$element") == "date") + { + var style = item.OptObject("props").OptString("style", "date"); + if (style is "timer" or "relative" or "offset") interval = Math.Min(interval, 1000); + else if (style == "time") interval = Math.Min(interval, 60_000); + } + Visit(item.OptArray("children")); + break; + case JsonArray array: + foreach (var child in array) Visit(child); + break; + } + } + try { Visit(Json.Parse(tree)); } catch (JsonException) { return long.MaxValue; } + if (interval == long.MaxValue) return long.MaxValue; + if (interval == 1000) return now + 1000; + // Align to the start of the next minute, when the displayed time changes. + var clock = DateTimeOffset.UtcNow; + return now + (60_000 - (clock.Second * 1000 + clock.Millisecond)) + 50; + } + + /// Runs the tapped action before the widget is redrawn. Runs on the refresh thread. + private static void Perform(WidgetRuntime engine, InstalledPackage installed, string family, string parameter, bool dark, WidgetAction action) + { + // A manifest that is present but unreadable fails closed: nothing is stored and nothing runs. + if (installed.ManifestPresent && installed.Manifest is null) { Log.Write("action refused: the manifest cannot be read"); return; } + var environments = new Dictionary { ["action-source"] = ActionPlanner.SourceWidget }; + if (action.Type == "toggle" && action.Value is { } value) + { + // The toggle's new state is saved first, then offered to the action as action-value. + if (SurfaceManifest.IsValidStateKey(action.StateKey) && installed.Permissions.Contains("storage")) + engine.Storage.SetString(installed.Installation, action.StateKey!, value ? "true" : "false"); + environments["action-value"] = value ? "true" : "false"; + environments["control-value"] = environments["action-value"]; + } + if (!string.IsNullOrEmpty(action.ActionID)) + { + // Only an action the manifest declares can be selected by a click. + PackageSurfaces surfaces; + try { surfaces = SurfaceManifest.Parse(installed.Manifest); } + catch (SurfaceManifestException error) { Log.Write($"action refused: {error.Code}"); return; } + if (surfaces.Action(action.ActionID) is not { } declared) { Log.Write("action refused: not declared in the manifest"); return; } + environments["action-id"] = declared.ID; + var result = engine.Run(new RunRequest { Files = installed.Files, PackageID = installed.Installation, Family = family, Parameter = parameter, DarkMode = dark, ActionID = declared.ID, ExtraEnvironments = environments, BuildDirectory = installed.BuildDirectory }); + Log.Write($"action {installed.Name}: {result.Execution.Error?.Kind ?? "ok"}"); + } + else if (SurfaceManifest.IsValidFunction(action.Function)) + { + var result = engine.Run(new RunRequest { Files = installed.Files, PackageID = installed.Installation, Family = family, Parameter = parameter, DarkMode = dark, CallFunction = action.Function, ExtraEnvironments = environments, BuildDirectory = installed.BuildDirectory }); + Log.Write($"action {installed.Name}: {result.Execution.Error?.Kind ?? "ok"}"); + } + } + + /// + /// Frees what only a render needs: the layout engine with the renderer script loaded in it. The + /// next render creates it again, which costs a fraction of a second. + /// + private void Trim() + { + runtime.Value.Renderer.Release(); + GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive, blocking: true, compacting: true); + } + + private void RefreshAll() + { + foreach (var controller in widgets.Values) QueueRefresh(controller); + } + + private void RefreshPackage(string installation) + { + foreach (var controller in widgets.Values.Where(item => item.Placement.Package == installation)) QueueRefresh(controller); + } + + // MARK: - Schedule + + private void ArmTimer() + { + if (Handle == 0) return; + var earliest = long.MaxValue; + foreach (var controller in widgets.Values) earliest = Math.Min(earliest, Math.Min(controller.NextRun, controller.NextRedraw)); + // Nothing to draw for a while: schedule the release of the layout engine and its script heap. + if (earliest - Environment.TickCount64 > 2 * 60_000) Win32.SetTimer(Handle, TrimTimer, TrimDelayMilliseconds, 0); + else Win32.KillTimer(Handle, TrimTimer); + if (earliest == long.MaxValue || sessionLocked) { Win32.KillTimer(Handle, ScheduleTimer); return; } + var delay = Math.Clamp(earliest - Environment.TickCount64, 50, 60_000); + Win32.SetTimer(Handle, ScheduleTimer, (uint)delay, 0); + } + + private void OnTimer() + { + if (sessionLocked) return; + var now = Environment.TickCount64; + // A full-screen program or game is in front: leave the processor to it. + if (settings.PauseWhenBusy && IsUserBusy()) + { + Win32.SetTimer(Handle, ScheduleTimer, 30_000, 0); + return; + } + foreach (var controller in widgets.Values.ToList()) + { + if (now >= controller.NextRun) { controller.NextRun = long.MaxValue; controller.NextRedraw = long.MaxValue; QueueRefresh(controller); } + else if (now >= controller.NextRedraw) { controller.NextRedraw = long.MaxValue; QueueRefresh(controller, redrawOnly: true); } + } + ArmTimer(); + } + + private static bool IsUserBusy() + { + int state; + if (Win32.SHQueryUserNotificationState(&state) != 0) return false; + return state is 2 /* QUNS_BUSY */ or 3 /* QUNS_RUNNING_D3D_FULL_SCREEN */ or 4 /* QUNS_PRESENTATION_MODE */; + } + + // MARK: - Clicks and menus + + private static HitRegion? HitTest(WidgetController controller, int pixelX, int pixelY) + { + var scale = controller.RenderedScale <= 0 ? 1 : controller.RenderedScale; + var x = (float)(pixelX / scale); + var y = (float)(pixelY / scale); + // The last region drawn is on top. + for (var index = controller.HitRegions.Count - 1; index >= 0; index--) + { + var bounds = controller.HitRegions[index].Bounds; + if (x >= bounds.Left && x < bounds.Right && y >= bounds.Top && y < bounds.Bottom) return controller.HitRegions[index]; + } + return null; + } + + private void OnClick(WidgetController controller, int pixelX, int pixelY) + { + if (Log.Verbose) Log.Write($"click at {pixelX},{pixelY}: {controller.HitRegions.Count} regions, scale {controller.RenderedScale}"); + if (HitTest(controller, pixelX, pixelY) is not { } region) return; + var interaction = region.Interaction; + Log.Write($"click: {interaction.OptString("type")}"); + string? Text(string name) => interaction.IsNull(name) ? null : interaction.OptString(name) is { Length: > 0 } value ? value : null; + switch (interaction.OptString("type")) + { + case "link": + Links.Open(Text("url"), _ => Studio.Open()); + break; + case "button" when "reload".Equals(Text("action"), StringComparison.OrdinalIgnoreCase): + QueueRefresh(controller); + break; + case "button": + QueueRefresh(controller, new WidgetAction("button", Text("actionID"), Text("handler"), null, null)); + break; + case "toggle": + QueueRefresh(controller, new WidgetAction("toggle", Text("actionID"), Text("handler"), Text("stateKey"), !interaction.OptBoolean("on"))); + break; + } + } + + private IReadOnlyList WidgetMenu(WidgetController controller) + { + var placement = controller.Placement; + var installed = library.FindByInstallation(placement.Package); + var families = installed?.Families ?? WidgetFamilies.All; + var sizes = families.Select(family => new MenuItem(Strings.Family(family), () => SetFamily(controller, family), Checked: family == placement.Family)).ToList(); + return + [ + new MenuItem(Strings.Refresh, () => QueueRefresh(controller)), + new MenuItem(Strings.Size, Children: sizes), + new MenuItem(Strings.Edit, () => Studio.Open(installed?.Name), Enabled: installed is not null), + MenuItem.Separator, + new MenuItem(Strings.LockPositions, ToggleLocked, Checked: settings.Locked), + MenuItem.Separator, + new MenuItem(Strings.Remove, () => RemoveController(placement.ID)), + ]; + } + + private IReadOnlyList TrayMenu() + { + var packages = library.List(); + var add = packages.Take(24).Select(item => new MenuItem(item.DisplayName, () => AddWidget(item.Installation, item.Families.Contains("medium") ? "medium" : item.Families[0], ""))).ToList(); + if (packages.Count == 0) add.Add(new MenuItem(Strings.NoPackages, Enabled: false)); + if (packages.Count > 24) add.Add(new MenuItem(Strings.MorePackages, () => Studio.Open())); + return + [ + new MenuItem(Strings.OpenStudio, () => Studio.Open()), + new MenuItem(Strings.AddWidget, Children: add), + MenuItem.Separator, + new MenuItem(Strings.RefreshAll, RefreshAll), + new MenuItem(Strings.LockPositions, ToggleLocked, Checked: settings.Locked), + new MenuItem(Strings.StartAtLogin, () => Startup.SetEnabled(!Startup.IsEnabled()), Checked: Startup.IsEnabled()), + MenuItem.Separator, + new MenuItem(Strings.Quit, Quit), + ]; + } + + private void ToggleLocked() + { + settings.Locked = !settings.Locked; + foreach (var controller in widgets.Values) controller.Window.Locked = settings.Locked; + } + + private void SetFamily(WidgetController controller, string family) + { + if (!WidgetFamilies.IsKnown(family) || controller.Placement.Family == family) return; + controller.Placement.Family = family; + placements.Update(controller.Placement); + controller.LastRequest = null; + controller.LastResult = null; + QueueRefresh(controller); + } + + private string? AddWidget(string installation, string family, string parameter) + { + if (placements.Add(installation, family, parameter) is not { } placement) return null; + var controller = AddController(placement); + QueueRefresh(controller); + return placement.ID; + } + + private void Quit() + { + if (quitting) return; + quitting = true; + stopping.Cancel(); + Win32.PostQuitMessage(0); + } + + // MARK: - System messages + + protected override nint? OnMessage(uint message, nuint wParam, nint lParam) + { + switch (message) + { + case Win32.WM_TIMER when wParam == ScheduleTimer: + OnTimer(); + return 0; + case Win32.WM_TIMER when wParam == TrimTimer: + Win32.KillTimer(Handle, TrimTimer); + if (widgets.Values.All(controller => !controller.Busy) && runtime.IsValueCreated) work.Add(Trim); + return 0; + case Win32.WM_DISPLAYCHANGE: + if (Log.Verbose) Log.Write("display change"); + // Displays were added, removed or rescaled: put every widget back where it belongs. + foreach (var controller in widgets.Values) { Position(controller); QueueRefresh(controller, redrawOnly: true); } + layer.Reassert(); + return null; + case Win32.WM_SETTINGCHANGE: + // The light/dark theme or the regional format changed. + if (lParam != 0 && Marshal.PtrToStringUni(lParam) is "ImmersiveColorSet" or "intl") { if (Log.Verbose) Log.Write("theme or format change"); RefreshAll(); } + return null; + case Win32.WM_TIMECHANGE: + if (Log.Verbose) Log.Write("time change"); + RefreshAll(); + return null; + case Win32.WM_POWERBROADCAST when (int)wParam == Win32.PBT_APMRESUMEAUTOMATIC: + // Timers do not run while the machine sleeps: everything may be stale. + if (Log.Verbose) Log.Write("resume from sleep"); + RefreshAll(); + return 1; + case WM_WTSSESSION_CHANGE: + if ((int)wParam == WTS_SESSION_LOCK) { if (Log.Verbose) Log.Write("session locked"); sessionLocked = true; ArmTimer(); } + else if ((int)wParam == WTS_SESSION_UNLOCK) { if (Log.Verbose) Log.Write("session unlocked"); sessionLocked = false; RefreshAll(); ArmTimer(); layer.Reassert(); } + return 0; + case Win32.WM_ENDSESSION when wParam != 0: + Quit(); + return 0; + } + return null; + } + + // MARK: - Requests from the management window + + /// Handles one pipe request. Runs on a thread-pool thread; work that touches windows hops to the UI thread. + private JsonObject HandleRequest(JsonObject request) + { + var command = request.OptString("command"); + if (command == "ping") return new JsonObject { ["ok"] = true, ["version"] = typeof(AppHost).Assembly.GetName().Version?.ToString() }; + var completion = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + ui.Post(() => + { + try { completion.SetResult(HandleOnUiThread(command, request)); } + catch (Exception error) { completion.SetResult(Failure(error.Message)); } + }); + return completion.Task.Wait(10_000) ? completion.Task.Result : Failure("The host did not answer in time"); + } + + private static JsonObject Failure(string message) => new() { ["ok"] = false, ["error"] = message }; + + /// The tray menu for "tray", or the right-click menu of the widget with that identifier. + private IReadOnlyList? DebugMenu(string target) => + target == "tray" ? TrayMenu() : widgets.TryGetValue(target, out var controller) ? WidgetMenu(controller) : null; + + private static JsonArray Describe(IReadOnlyList items) + { + var array = new JsonArray(); + foreach (var item in items) + { + array.Append(new JsonObject + { + ["text"] = item.Text, + ["checked"] = item.Checked, + ["enabled"] = item.Enabled, + ["children"] = item.Children is null ? null : Describe(item.Children), + }); + } + return array; + } + + private JsonObject HandleOnUiThread(string command, JsonObject request) + { + switch (command) + { + case "list": + { + var items = new JsonArray(); + foreach (var controller in widgets.Values) items.Append(controller.Placement.ToJson()); + return new JsonObject { ["ok"] = true, ["widgets"] = items }; + } + case "add": + { + var installation = request.OptString("package"); + if (library.FindByInstallation(installation) is null) return Failure("Package not found"); + var family = request.OptString("family", "medium"); + var id = AddWidget(installation, WidgetFamilies.IsKnown(family) ? family : "medium", request.OptString("parameter")); + return id is null ? Failure(Strings.TooManyWidgets) : new JsonObject { ["ok"] = true, ["id"] = id }; + } + case "remove": + RemoveController(request.OptString("id")); + return new JsonObject { ["ok"] = true }; + case "update": + { + if (!widgets.TryGetValue(request.OptString("id"), out var controller)) return Failure("Widget not found"); + if (request.Has("parameter")) + { + var parameter = request.OptString("parameter"); + controller.Placement.Parameter = parameter.Length > 2048 ? parameter[..2048] : parameter; + placements.Update(controller.Placement); + controller.LastRequest = null; + QueueRefresh(controller); + } + if (request.Has("family")) SetFamily(controller, request.OptString("family")); + return new JsonObject { ["ok"] = true }; + } + case "refresh": + if (request.OptString("package") is { Length: > 0 } package) RefreshPackage(package); else RefreshAll(); + return new JsonObject { ["ok"] = true }; + case "library-changed": + { + // Packages were edited, renamed or deleted in the management window. + foreach (var controller in widgets.Values) { controller.LastRequest = null; controller.LastResult = null; } + RefreshAll(); + return new JsonObject { ["ok"] = true }; + } + case "settings-changed": + settings.Reload(); + foreach (var controller in widgets.Values) controller.Window.Locked = settings.Locked; + ArmTimer(); + return new JsonObject { ["ok"] = true }; + case "show-studio": + Studio.Open(); + return new JsonObject { ["ok"] = true }; + case "quit": + Quit(); + return new JsonObject { ["ok"] = true }; + // For desktop-check.ps1 (SCRIPTWIDGET_VERBOSE=1 only): the menus as data, and running one + // of their items, so the menu actions are checked without a pointer. + case "debug-menu" when Log.Verbose: + return DebugMenu(request.OptString("target")) is { } menu ? new JsonObject { ["ok"] = true, ["items"] = Describe(menu) } : Failure("Widget not found"); + case "debug-invoke" when Log.Verbose: + { + if (DebugMenu(request.OptString("target")) is not { } items) return Failure("Widget not found"); + MenuItem? item = null; + foreach (var index in request.OptArray("path")?.Select(node => (int)(node?.AsDouble() ?? -1)) ?? []) + { + if (index < 0 || index >= items.Count) return Failure("No such menu item"); + item = items[index]; + items = item.Children ?? []; + } + if (item?.Invoke is null || !item.Enabled) return Failure("The menu item cannot be invoked"); + item.Invoke(); + return new JsonObject { ["ok"] = true, ["text"] = item.Text }; + } + default: + return Failure($"Unknown command {command}"); + } + } + + public override void Dispose() + { + stopping.Cancel(); + work.CompleteAdding(); + if (Handle != 0) WTSUnRegisterSessionNotification(Handle); + foreach (var controller in widgets.Values) controller.Window.Dispose(); + widgets.Clear(); + tray.Dispose(); + layer.Dispose(); + ui.Dispose(); + if (runtime.IsValueCreated) runtime.Value.Dispose(); + base.Dispose(); + } +} diff --git a/Windows/src/ScriptWidget.App/Interop/Win32.cs b/Windows/src/ScriptWidget.App/Interop/Win32.cs new file mode 100644 index 00000000..a3a93556 --- /dev/null +++ b/Windows/src/ScriptWidget.App/Interop/Win32.cs @@ -0,0 +1,245 @@ +using System.Runtime.InteropServices; + +namespace ScriptWidget.App.Interop; + +/// The Win32 calls the desktop host uses. Every signature is blittable, so it works under native AOT. +internal static unsafe class Win32 +{ + // MARK: - Constants + + public const uint WS_POPUP = 0x80000000; + public const uint WS_EX_LAYERED = 0x00080000; + public const uint WS_EX_TOOLWINDOW = 0x00000080; + public const uint WS_EX_NOACTIVATE = 0x08000000; + public const uint WS_EX_TOPMOST = 0x00000008; + + public const uint WM_DESTROY = 0x0002; + public const uint WM_CLOSE = 0x0010; + public const uint WM_QUERYENDSESSION = 0x0011; + public const uint WM_ENDSESSION = 0x0016; + public const uint WM_SETTINGCHANGE = 0x001A; + public const uint WM_TIMECHANGE = 0x001E; + public const uint WM_SETCURSOR = 0x0020; + public const uint WM_MOUSEACTIVATE = 0x0021; + public const uint WM_WINDOWPOSCHANGING = 0x0046; + public const uint WM_DISPLAYCHANGE = 0x007E; + public const uint WM_CONTEXTMENU = 0x007B; + public const uint WM_NCHITTEST = 0x0084; + public const uint WM_COMMAND = 0x0111; + public const uint WM_TIMER = 0x0113; + public const uint WM_MOUSEMOVE = 0x0200; + public const uint WM_LBUTTONDOWN = 0x0201; + public const uint WM_LBUTTONUP = 0x0202; + public const uint WM_LBUTTONDBLCLK = 0x0203; + public const uint WM_RBUTTONUP = 0x0205; + public const uint WM_CAPTURECHANGED = 0x0215; + public const uint WM_POWERBROADCAST = 0x0218; + public const uint WM_DPICHANGED = 0x02E0; + public const uint WM_APP = 0x8000; + public const uint WM_NULL = 0x0000; + + public const int PBT_APMRESUMEAUTOMATIC = 0x0012; + public const int MA_NOACTIVATE = 3; + public const int HTCLIENT = 1; + public const int HTTRANSPARENT = -1; + + public static readonly nint HWND_BOTTOM = 1; + public static readonly nint HWND_TOPMOST = -1; + public static readonly nint HWND_NOTOPMOST = -2; + public static readonly nint HWND_MESSAGE = -3; + + public const uint SWP_NOSIZE = 0x0001; + public const uint SWP_NOMOVE = 0x0002; + public const uint SWP_NOZORDER = 0x0004; + public const uint SWP_NOACTIVATE = 0x0010; + public const uint SWP_SHOWWINDOW = 0x0040; + public const uint SWP_HIDEWINDOW = 0x0080; + public const uint SWP_NOOWNERZORDER = 0x0200; + public const uint SWP_NOSENDCHANGING = 0x0400; + + public const int SW_HIDE = 0; + public const int SW_SHOWNOACTIVATE = 4; + public const int SW_SHOWNORMAL = 1; + + public const uint ULW_ALPHA = 0x00000002; + public const byte AC_SRC_OVER = 0; + public const byte AC_SRC_ALPHA = 1; + + public const uint MONITOR_DEFAULTTONULL = 0; + public const uint MONITOR_DEFAULTTOPRIMARY = 1; + public const uint MONITOR_DEFAULTTONEAREST = 2; + public const uint MONITORINFOF_PRIMARY = 1; + + public const uint MF_STRING = 0x0000; + public const uint MF_GRAYED = 0x0001; + public const uint MF_CHECKED = 0x0008; + public const uint MF_POPUP = 0x0010; + public const uint MF_SEPARATOR = 0x0800; + public const uint TPM_RIGHTBUTTON = 0x0002; + public const uint TPM_RETURNCMD = 0x0100; + public const uint TPM_NONOTIFY = 0x0080; + + public const uint NIM_ADD = 0, NIM_MODIFY = 1, NIM_DELETE = 2, NIM_SETVERSION = 4; + public const uint NIF_MESSAGE = 1, NIF_ICON = 2, NIF_TIP = 4, NIF_SHOWTIP = 0x80; + public const uint NOTIFYICON_VERSION_4 = 4; + public const uint NIN_SELECT = 0x0400; + public const uint NIN_KEYSELECT = 0x0401; + + public const uint EVENT_SYSTEM_FOREGROUND = 0x0003; + public const uint WINEVENT_OUTOFCONTEXT = 0; + public const uint WINEVENT_SKIPOWNPROCESS = 2; + + public const uint IMAGE_ICON = 1; + public const uint LR_LOADFROMFILE = 0x0010; + public const uint LR_DEFAULTSIZE = 0x0040; + public const uint LR_SHARED = 0x8000; + public static readonly nint IDC_ARROW = 32512; + public static readonly nint IDC_HAND = 32649; + public static readonly nint IDC_SIZEALL = 32646; + public static readonly nint IDI_APPLICATION = 32512; + + public static readonly nint DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 = -4; + + // MARK: - Structures + + [StructLayout(LayoutKind.Sequential)] + public struct POINT { public int X, Y; } + + [StructLayout(LayoutKind.Sequential)] + public struct SIZE { public int Width, Height; } + + [StructLayout(LayoutKind.Sequential)] + public struct RECT + { + public int Left, Top, Right, Bottom; + public readonly int Width => Right - Left; + public readonly int Height => Bottom - Top; + } + + [StructLayout(LayoutKind.Sequential)] + public struct MSG { public nint Window; public uint Message; public nuint WParam; public nint LParam; public uint Time; public POINT Point; public uint Private; } + + [StructLayout(LayoutKind.Sequential)] + public struct WNDCLASSEXW + { + public uint Size, Style; + public delegate* unmanaged[Stdcall] WindowProcedure; + public int ClassExtra, WindowExtra; + public nint Instance, Icon, Cursor, Background; + public char* MenuName, ClassName; + public nint SmallIcon; + } + + [StructLayout(LayoutKind.Sequential)] + public struct WINDOWPOS { public nint Window, InsertAfter; public int X, Y, Width, Height; public uint Flags; } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct BLENDFUNCTION { public byte BlendOp, BlendFlags, SourceConstantAlpha, AlphaFormat; } + + [StructLayout(LayoutKind.Sequential)] + public struct BITMAPINFOHEADER + { + public uint Size; public int Width, Height; public ushort Planes, BitCount; public uint Compression, SizeImage; + public int XPelsPerMeter, YPelsPerMeter; public uint ColorsUsed, ColorsImportant; + } + + [StructLayout(LayoutKind.Sequential)] + public struct MONITORINFOEXW + { + public uint Size; public RECT Monitor, Work; public uint Flags; + public fixed char Device[32]; + } + + [StructLayout(LayoutKind.Sequential)] + public struct NOTIFYICONDATAW + { + public uint Size; public nint Window; public uint ID, Flags, CallbackMessage; public nint Icon; + public fixed char Tip[128]; + public uint State, StateMask; + public fixed char Info[256]; + public uint VersionOrTimeout; + public fixed char InfoTitle[64]; + public uint InfoFlags; public Guid Item; public nint BalloonIcon; + } + + // MARK: - user32 + + private const string User = "user32.dll"; + + [DllImport(User, SetLastError = true)] public static extern ushort RegisterClassExW(WNDCLASSEXW* windowClass); + [DllImport(User, SetLastError = true)] public static extern nint CreateWindowExW(uint exStyle, char* className, char* title, uint style, int x, int y, int width, int height, nint parent, nint menu, nint instance, nint parameter); + [DllImport(User)] public static extern nint DefWindowProcW(nint window, uint message, nuint wParam, nint lParam); + [DllImport(User)] public static extern int DestroyWindow(nint window); + [DllImport(User)] public static extern int ShowWindow(nint window, int command); + [DllImport(User)] public static extern int IsWindowVisible(nint window); + [DllImport(User)] public static extern int SetWindowPos(nint window, nint insertAfter, int x, int y, int width, int height, uint flags); + [DllImport(User)] public static extern int GetWindowRect(nint window, RECT* rectangle); + [DllImport(User)] public static extern int GetMessageW(MSG* message, nint window, uint filterMinimum, uint filterMaximum); + [DllImport(User)] public static extern int TranslateMessage(MSG* message); + [DllImport(User)] public static extern nint DispatchMessageW(MSG* message); + [DllImport(User)] public static extern int PostMessageW(nint window, uint message, nuint wParam, nint lParam); + [DllImport(User)] public static extern void PostQuitMessage(int exitCode); + [DllImport(User)] public static extern int UpdateLayeredWindow(nint window, nint destination, POINT* position, SIZE* size, nint source, POINT* sourcePoint, uint colorKey, BLENDFUNCTION* blend, uint flags); + [DllImport(User)] public static extern nint GetDC(nint window); + [DllImport(User)] public static extern int ReleaseDC(nint window, nint context); + [DllImport(User)] public static extern nint SetCapture(nint window); + [DllImport(User)] public static extern int ReleaseCapture(); + [DllImport(User)] public static extern int GetCursorPos(POINT* point); + [DllImport(User)] public static extern nint SetCursor(nint cursor); + [DllImport(User)] public static extern nint LoadCursorW(nint instance, nint name); + [DllImport(User)] public static extern nint LoadIconW(nint instance, nint name); + [DllImport(User)] public static extern nint LoadImageW(nint instance, char* name, uint type, int width, int height, uint flags); + [DllImport(User)] public static extern nint MonitorFromWindow(nint window, uint flags); + [DllImport(User)] public static extern nint MonitorFromPoint(POINT point, uint flags); + [DllImport(User)] public static extern int GetMonitorInfoW(nint monitor, MONITORINFOEXW* information); + [DllImport(User)] public static extern int EnumDisplayMonitors(nint context, RECT* clip, delegate* unmanaged[Stdcall] callback, nint data); + [DllImport(User)] public static extern uint GetDpiForWindow(nint window); + [DllImport(User)] public static extern int SetProcessDpiAwarenessContext(nint context); + [DllImport(User)] public static extern uint RegisterWindowMessageW(char* name); + [DllImport(User)] public static extern nint CreatePopupMenu(); + [DllImport(User)] public static extern int AppendMenuW(nint menu, uint flags, nuint item, char* text); + [DllImport(User)] public static extern int TrackPopupMenuEx(nint menu, uint flags, int x, int y, nint window, void* parameters); + [DllImport(User)] public static extern int DestroyMenu(nint menu); + [DllImport(User)] public static extern int SetForegroundWindow(nint window); + [DllImport(User)] public static extern nint GetForegroundWindow(); + [DllImport(User)] public static extern int GetClassNameW(nint window, char* name, int capacity); + [DllImport(User)] public static extern nint SetWinEventHook(uint eventMinimum, uint eventMaximum, nint module, delegate* unmanaged[Stdcall] callback, uint process, uint thread, uint flags); + [DllImport(User)] public static extern int UnhookWinEvent(nint hook); + [DllImport(User)] public static extern nuint SetTimer(nint window, nuint id, uint milliseconds, nint callback); + [DllImport(User)] public static extern int KillTimer(nint window, nuint id); + [DllImport(User)] public static extern nint FindWindowExW(nint parent, nint after, char* className, char* title); + [DllImport(User)] public static extern nint GetWindow(nint window, uint command); + [DllImport(User)] public static extern nint WindowFromPoint(POINT point); + [DllImport(User)] public static extern int MessageBoxW(nint window, char* text, char* caption, uint type); + [DllImport(User)] public static extern nint GetShellWindow(); + + // MARK: - gdi32, shcore, shell32, kernel32 + + [DllImport("gdi32.dll")] public static extern nint CreateCompatibleDC(nint context); + [DllImport("gdi32.dll")] public static extern int DeleteDC(nint context); + [DllImport("gdi32.dll")] public static extern nint CreateDIBSection(nint context, BITMAPINFOHEADER* information, uint usage, void** bits, nint section, uint offset); + [DllImport("gdi32.dll")] public static extern nint SelectObject(nint context, nint value); + [DllImport("gdi32.dll")] public static extern int DeleteObject(nint value); + + [DllImport("shcore.dll")] public static extern int GetDpiForMonitor(nint monitor, int type, uint* dpiX, uint* dpiY); + + [DllImport("shell32.dll")] public static extern int Shell_NotifyIconW(uint message, NOTIFYICONDATAW* data); + [DllImport("shell32.dll")] public static extern int SHQueryUserNotificationState(int* state); + [DllImport("shell32.dll")] public static extern nint ShellExecuteW(nint window, char* operation, char* file, char* parameters, char* directory, int show); + + [DllImport("kernel32.dll")] public static extern nint GetModuleHandleW(char* name); + + // MARK: - Helpers + + public static int LowWord(nint value) => (short)(value & 0xFFFF); + public static int HighWord(nint value) => (short)((value >> 16) & 0xFFFF); + public static int LowWord(nuint value) => (int)(value & 0xFFFF); + public static int HighWord(nuint value) => (int)((value >> 16) & 0xFFFF); + + public static string ClassName(nint window) + { + var buffer = stackalloc char[128]; + var length = GetClassNameW(window, buffer, 128); + return length <= 0 ? "" : new string(buffer, 0, length); + } +} diff --git a/Windows/src/ScriptWidget.App/Program.cs b/Windows/src/ScriptWidget.App/Program.cs new file mode 100644 index 00000000..6f321814 --- /dev/null +++ b/Windows/src/ScriptWidget.App/Program.cs @@ -0,0 +1,20 @@ +using ScriptWidget.App.Interop; + +namespace ScriptWidget.App; + +internal static class Program +{ + [STAThread] + private static int Main(string[] arguments) + { + Win32.SetProcessDpiAwarenessContext(Win32.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); + if (arguments.Length > 0 && arguments[0] == "--selftest-window") return SelfTest.Run(arguments); + if (arguments.Length > 0 && arguments[0] == "--selftest-showdesktop") return SelfTest.RunShowDesktop(arguments); + try { return AppHost.Run(arguments); } + catch (Exception error) + { + Windowing.Log.Write($"host crashed: {error}"); + return 1; + } + } +} diff --git a/Windows/src/ScriptWidget.App/ScriptWidget.App.csproj b/Windows/src/ScriptWidget.App/ScriptWidget.App.csproj new file mode 100644 index 00000000..3893cb2e --- /dev/null +++ b/Windows/src/ScriptWidget.App/ScriptWidget.App.csproj @@ -0,0 +1,16 @@ + + + WinExe + ScriptWidget + ScriptWidget.App + app.manifest + ..\..\package\Assets\ScriptWidget.ico + true + false + en + + + + + + diff --git a/Windows/src/ScriptWidget.App/SelfTest.cs b/Windows/src/ScriptWidget.App/SelfTest.cs new file mode 100644 index 00000000..432e53d7 --- /dev/null +++ b/Windows/src/ScriptWidget.App/SelfTest.cs @@ -0,0 +1,225 @@ +using ScriptWidget.App.Interop; +using ScriptWidget.App.Windowing; + +namespace ScriptWidget.App; + +/// +/// ScriptWidget.exe --selftest-window <seconds> <log>: shows one widget window with a +/// drawn test card and writes its state to a log once a second, so the window +/// order can be checked on a real desktop while windows are opened and the +/// desktop is shown. Used for the checks recorded in docs/windows/testing.md. +/// +internal static unsafe class SelfTest +{ + public static int Run(string[] arguments) + { + var seconds = arguments.Length > 1 && int.TryParse(arguments[1], out var parsed) ? parsed : 20; + var logPath = arguments.Length > 2 ? arguments[2] : Path.Combine(Path.GetTempPath(), "scriptwidget-window-selftest.log"); + File.WriteAllText(logPath, ""); + void Note(string line) => File.AppendAllText(logPath, $"{DateTimeOffset.Now:HH:mm:ss.fff} {line}{Environment.NewLine}"); + + using var layer = new DesktopLayer(); + using var window = new WidgetWindow(); + var monitor = Monitors.Primary(); + var scale = monitor.Scale; + int width = (int)Math.Round(329 * scale), height = (int)Math.Round(170 * scale); + window.MoveTo(monitor.Work.Right - width - (int)(40 * scale), monitor.Work.Top + (int)(40 * scale)); + window.SetBitmap(Card(width, height, (int)Math.Round(22 * scale)), width, height); + layer.Add(window); + window.Clicked += (x, y) => Note($"clicked {x},{y}"); + window.Moved += () => Note($"moved to {window.X},{window.Y}"); + Note($"monitors: {string.Join("; ", Monitors.All().Select(item => $"{item.Device} {item.Bounds.Width}x{item.Bounds.Height} scale {item.Scale} primary {item.Primary}"))}"); + Note($"window {window.Handle:X} at {window.X},{window.Y} size {width}x{height}"); + + var started = Environment.TickCount64; + using var timer = new TimerWindow(1000, () => + { + var centre = new Win32.POINT { X = window.X + width / 2, Y = window.Y + height / 2 }; + var top = Win32.WindowFromPoint(centre); + var foreground = Win32.GetForegroundWindow(); + Note($"desktopShown={layer.DesktopShown} windowAtCentre={(top == window.Handle ? "widget" : Win32.ClassName(top))} foreground={Win32.ClassName(foreground)} visible={Win32.IsWindowVisible(window.Handle) != 0}"); + if (Environment.TickCount64 - started > seconds * 1000L) Win32.PostQuitMessage(0); + }); + + Win32.MSG message; + while (Win32.GetMessageW(&message, 0, 0, 0) > 0) + { + Win32.TranslateMessage(&message); + Win32.DispatchMessageW(&message); + } + Note("done"); + return 0; + } + + /// + /// ScriptWidget.exe --selftest-showdesktop <log>: acts out "show desktop" with a stand-in + /// for the desktop window and checks that a widget is raised while the desktop is shown and + /// returns to its place directly above the desktop afterwards. The real desktop window is not + /// touched, so this also runs on a locked or unattended machine. Exit code 0 when every check passes. + /// + public static int RunShowDesktop(string[] arguments) + { + var logPath = arguments.Length > 1 ? arguments[1] : Path.Combine(Path.GetTempPath(), "scriptwidget-showdesktop-selftest.log"); + File.WriteAllText(logPath, ""); + var failures = 0; + void Check(string what, bool ok) + { + if (!ok) failures++; + File.AppendAllText(logPath, $"{(ok ? "PASS" : "FAIL")} {what}{Environment.NewLine}"); + } + // True when `upper` is in front of `lower` in the window order. + static bool InFront(nint upper, nint lower) + { + for (var window = Win32.GetWindow(upper, 2 /* GW_HWNDNEXT */); window != 0; window = Win32.GetWindow(window, 2)) if (window == lower) return true; + return false; + } + static bool IsTopmost(nint window) => (GetWindowLongPtrW(window, -20) & Win32.WS_EX_TOPMOST) != 0; + + using var desktop = new PlainWindow("ScriptWidgetSelfTestDesktop"); + using var other = new PlainWindow("ScriptWidgetSelfTestOther"); + // The stand-in desktop starts behind the stand-in for an ordinary window, like the real desktop. + Win32.SetWindowPos(other.Handle, 0 /* HWND_TOP */, 60, 60, 200, 120, Win32.SWP_NOACTIVATE | Win32.SWP_SHOWWINDOW); + Win32.SetWindowPos(desktop.Handle, other.Handle, 40, 40, 400, 300, Win32.SWP_NOACTIVATE | Win32.SWP_SHOWWINDOW); + var foreground = other.Handle; + DesktopLayer.HostOverride = desktop.Handle; + DesktopLayer.ForegroundOverride = () => foreground; + try + { + using var layer = new DesktopLayer(); + using var window = new WidgetWindow(); + window.MoveTo(80, 80); + window.SetBitmap(Card(120, 80, 12), 120, 80); + layer.Add(window); + layer.Reassert(); + + Check("normally: the widget is in front of the desktop", InFront(window.Handle, desktop.Handle)); + Check("normally: an ordinary window is in front of the widget", InFront(other.Handle, window.Handle)); + Check("normally: the widget is not topmost", !IsTopmost(window.Handle)); + + // A click on the wallpaper: the desktop becomes the foreground window but is not raised. + foreground = desktop.Handle; + layer.Check(); + Check("wallpaper click: not treated as show desktop", !layer.DesktopShown); + Check("wallpaper click: the widget stays behind the ordinary window", InFront(other.Handle, window.Handle) && !IsTopmost(window.Handle)); + + // Show desktop: the desktop is raised above everything and is the foreground window. + Win32.SetWindowPos(desktop.Handle, 0 /* HWND_TOP */, 0, 0, 0, 0, Win32.SWP_NOMOVE | Win32.SWP_NOSIZE | Win32.SWP_NOACTIVATE); + layer.Check(); + Check("show desktop: detected", layer.DesktopShown); + Check("show desktop: the widget is in front of the raised desktop", InFront(window.Handle, desktop.Handle)); + Check("show desktop: the widget is topmost", IsTopmost(window.Handle)); + + // Leaving: an ordinary window is activated and the desktop goes back to the bottom. + Win32.SetWindowPos(desktop.Handle, Win32.HWND_BOTTOM, 0, 0, 0, 0, Win32.SWP_NOMOVE | Win32.SWP_NOSIZE | Win32.SWP_NOACTIVATE); + foreground = other.Handle; + layer.Check(); + Check("afterwards: no longer shown", !layer.DesktopShown); + Check("afterwards: the widget is not topmost", !IsTopmost(window.Handle)); + Check("afterwards: the widget is in front of the desktop", InFront(window.Handle, desktop.Handle)); + Check("afterwards: the ordinary window is in front of the widget", InFront(other.Handle, window.Handle)); + + // A second round must work the same way. + Win32.SetWindowPos(desktop.Handle, 0, 0, 0, 0, 0, Win32.SWP_NOMOVE | Win32.SWP_NOSIZE | Win32.SWP_NOACTIVATE); + foreground = desktop.Handle; + layer.Check(); + Check("second time: detected and raised", layer.DesktopShown && IsTopmost(window.Handle)); + Win32.SetWindowPos(desktop.Handle, Win32.HWND_BOTTOM, 0, 0, 0, 0, Win32.SWP_NOMOVE | Win32.SWP_NOSIZE | Win32.SWP_NOACTIVATE); + foreground = other.Handle; + layer.Check(); + Check("second time: back in place", !layer.DesktopShown && !IsTopmost(window.Handle) && InFront(window.Handle, desktop.Handle) && InFront(other.Handle, window.Handle)); + + // What a real Win+D does: the desktop becomes the foreground window first and is raised + // only after the other windows have minimized, and nothing reports the raise. + foreground = desktop.Handle; + layer.Check(); + Check("late raise: not shown before the desktop is raised", !layer.DesktopShown); + Pump(300); + Win32.SetWindowPos(desktop.Handle, 0, 0, 0, 0, 0, Win32.SWP_NOMOVE | Win32.SWP_NOSIZE | Win32.SWP_NOACTIVATE); + Pump(500); + Check("late raise: detected without another foreground change", layer.DesktopShown && IsTopmost(window.Handle) && InFront(window.Handle, desktop.Handle)); + + // Leaving, the other way round: the foreground window changes while the desktop is still + // raised, and the desktop goes back to the bottom afterwards. + foreground = other.Handle; + layer.Check(); + Check("late drop: no longer shown", !layer.DesktopShown && !IsTopmost(window.Handle)); + Win32.SetWindowPos(other.Handle, 0, 0, 0, 0, 0, Win32.SWP_NOMOVE | Win32.SWP_NOSIZE | Win32.SWP_NOACTIVATE); + Win32.SetWindowPos(desktop.Handle, Win32.HWND_BOTTOM, 0, 0, 0, 0, Win32.SWP_NOMOVE | Win32.SWP_NOSIZE | Win32.SWP_NOACTIVATE); + Pump(900); + Check("late drop: the widget ends in front of the desktop", InFront(window.Handle, desktop.Handle)); + Check("late drop: the ordinary window ends in front of the widget", InFront(other.Handle, window.Handle)); + } + finally + { + DesktopLayer.HostOverride = 0; + DesktopLayer.ForegroundOverride = null; + } + File.AppendAllText(logPath, failures == 0 ? "all checks passed" + Environment.NewLine : $"{failures} check(s) failed{Environment.NewLine}"); + return failures == 0 ? 0 : 1; + } + + [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern nint GetWindowLongPtrW(nint window, int index); + [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern int PeekMessageW(Win32.MSG* message, nint window, uint filterMinimum, uint filterMaximum, uint remove); + + /// Runs the message loop for a while, so timers fire. + private static void Pump(int milliseconds) + { + var end = Environment.TickCount64 + milliseconds; + Win32.MSG message; + while (Environment.TickCount64 < end) + { + while (PeekMessageW(&message, 0, 0, 0, 1 /* PM_REMOVE */) != 0) + { + Win32.TranslateMessage(&message); + Win32.DispatchMessageW(&message); + } + Thread.Sleep(10); + } + } + + /// An empty tool window used as a stand-in in the self-tests. + private sealed class PlainWindow : NativeWindow + { + public PlainWindow(string className) => Create(className, Win32.WS_EX_TOOLWINDOW | Win32.WS_EX_NOACTIVATE, Win32.WS_POPUP); + protected override nint? OnMessage(uint message, nuint wParam, nint lParam) => message == Win32.WM_MOUSEACTIVATE ? Win32.MA_NOACTIVATE : null; + } + + /// A rounded card with a gradient, as premultiplied BGRA. + private static byte[] Card(int width, int height, int radius) + { + var pixels = new byte[width * height * 4]; + for (var row = 0; row < height; row++) + { + for (var column = 0; column < width; column++) + { + var dx = column < radius ? radius - column : column >= width - radius ? column - (width - radius - 1) : 0; + var dy = row < radius ? radius - row : row >= height - radius ? row - (height - radius - 1) : 0; + if (dx * dx + dy * dy > radius * radius) continue; + var t = (double)(column + row) / (width + height); + var index = (row * width + column) * 4; + pixels[index] = (byte)(0xe5 + (0xd4 - 0xe5) * t); + pixels[index + 1] = (byte)(0x46 + (0xb6 - 0x46) * t); + pixels[index + 2] = (byte)(0x4f + (0x06 - 0x4f) * t); + pixels[index + 3] = 255; + } + } + return pixels; + } + + private sealed class TimerWindow : NativeWindow + { + private readonly Action tick; + public TimerWindow(uint milliseconds, Action tick) + { + this.tick = tick; + Create("ScriptWidgetSelfTestTimer", 0, 0, Win32.HWND_MESSAGE); + Win32.SetTimer(Handle, 1, milliseconds, 0); + } + protected override nint? OnMessage(uint message, nuint wParam, nint lParam) + { + if (message != Win32.WM_TIMER) return null; + tick(); + return 0; + } + } +} diff --git a/Windows/src/ScriptWidget.App/Strings.cs b/Windows/src/ScriptWidget.App/Strings.cs new file mode 100644 index 00000000..3ea07255 --- /dev/null +++ b/Windows/src/ScriptWidget.App/Strings.cs @@ -0,0 +1,37 @@ +using System.Globalization; + +namespace ScriptWidget.App; + +/// The few words the tray and widget menus need, in Simplified Chinese and English. +internal static class Strings +{ + private static readonly bool Chinese = (Environment.GetEnvironmentVariable("SCRIPTWIDGET_LANGUAGE") ?? CultureInfo.CurrentUICulture.TwoLetterISOLanguageName).StartsWith("zh", StringComparison.OrdinalIgnoreCase); + + private static string Pick(string english, string chinese) => Chinese ? chinese : english; + + public static string OpenStudio => Pick("Open ScriptWidget", "打开 ScriptWidget"); + public static string AddWidget => Pick("Add widget", "添加小组件"); + public static string NoPackages => Pick("No widgets yet. Open ScriptWidget to create one.", "还没有小组件,打开 ScriptWidget 创建一个"); + public static string MorePackages => Pick("More in ScriptWidget…", "更多请打开 ScriptWidget…"); + public static string RefreshAll => Pick("Refresh all", "全部刷新"); + public static string LockPositions => Pick("Lock positions", "锁定位置"); + public static string StartAtLogin => Pick("Start when I sign in", "登录时启动"); + public static string Quit => Pick("Quit", "退出"); + public static string Refresh => Pick("Refresh", "刷新"); + public static string Size => Pick("Size", "尺寸"); + public static string Edit => Pick("Edit…", "编辑…"); + public static string Remove => Pick("Remove from desktop", "从桌面移除"); + public static string TrayTip => "ScriptWidget"; + + public static string Family(string family) => family switch + { + "small" => Pick("Small", "小"), + "large" => Pick("Large", "大"), + "extraLarge" => Pick("Extra large", "特大"), + _ => Pick("Medium", "中"), + }; + + public static string MissingTitle => Pick("Widget not found", "找不到这个小组件"); + public static string MissingDetail => Pick("Its package was removed. Right-click to take this widget off the desktop.", "它的包已被删除。右键可以把它从桌面移除。"); + public static string TooManyWidgets => Pick("The desktop already holds the maximum number of widgets.", "桌面上的小组件已达上限。"); +} diff --git a/Windows/src/ScriptWidget.App/SystemIntegration.cs b/Windows/src/ScriptWidget.App/SystemIntegration.cs new file mode 100644 index 00000000..194be481 --- /dev/null +++ b/Windows/src/ScriptWidget.App/SystemIntegration.cs @@ -0,0 +1,73 @@ +using System.Diagnostics; +using System.Runtime.InteropServices; +using ScriptWidget.App.Interop; +using ScriptWidget.App.Windowing; + +namespace ScriptWidget.App; + +/// Opens links a widget asks for. Package content is untrusted, so only a few schemes are handed to Windows. +internal static unsafe class Links +{ + /// + /// Schemes that open a browser or a mail program. Everything else is + /// refused: a file path or a file: URL would run programs, and + /// many registered protocol handlers were never meant to take input from + /// untrusted content. + /// + private static readonly HashSet Allowed = new(StringComparer.OrdinalIgnoreCase) { "http", "https", "mailto" }; + + /// Opens the link, or returns false when it is not one a widget may open. + public static bool Open(string? url, Action? openInApp = null) + { + if (string.IsNullOrWhiteSpace(url) || url.Length > 4096) return false; + if (!Uri.TryCreate(url.Trim(), UriKind.Absolute, out var uri)) return false; + if (uri.Scheme.Equals("scriptwidget", StringComparison.OrdinalIgnoreCase)) { openInApp?.Invoke(uri); return openInApp is not null; } + if (!Allowed.Contains(uri.Scheme)) return false; + if (uri.Scheme is "http" or "https" && string.IsNullOrEmpty(uri.Host)) return false; + // The canonical form of the parsed URL, never the raw text. + var target = uri.AbsoluteUri; + fixed (char* operation = "open") + fixed (char* file = target) + return Win32.ShellExecuteW(0, operation, file, null, null, Win32.SW_SHOWNORMAL) > 32; + } +} + +/// Starting the host when the user signs in. +internal static class Startup +{ + public static bool IsPackaged => ScriptWidget.Core.Hosting.StartupRegistration.IsPackaged; + public static bool IsEnabled() => ScriptWidget.Core.Hosting.StartupRegistration.IsEnabled(); + public static void SetEnabled(bool enabled) => ScriptWidget.Core.Hosting.StartupRegistration.SetEnabled(enabled, Environment.ProcessPath); +} + +/// Starts the management window program. +internal static class Studio +{ + public static string? Path() + { + var directory = AppContext.BaseDirectory; + foreach (var candidate in new[] + { + System.IO.Path.Combine(directory, "ScriptWidgetStudio.exe"), + System.IO.Path.Combine(directory, "Studio", "ScriptWidgetStudio.exe"), + Environment.GetEnvironmentVariable("SCRIPTWIDGET_STUDIO") ?? "", + }) + { + if (candidate.Length > 0 && File.Exists(candidate)) return candidate; + } + return null; + } + + /// Opens the management window, optionally on a package. + public static void Open(string? packageName = null) + { + if (Path() is not { } executable) { Log.Write("management window program not found"); return; } + try + { + var start = new ProcessStartInfo(executable) { UseShellExecute = false }; + if (packageName is not null) { start.ArgumentList.Add("--open"); start.ArgumentList.Add(packageName); } + Process.Start(start)?.Dispose(); + } + catch (Exception error) when (error is System.ComponentModel.Win32Exception or InvalidOperationException) { Log.Write($"management window: {error.Message}"); } + } +} diff --git a/Windows/src/ScriptWidget.App/Windowing/DesktopLayer.cs b/Windows/src/ScriptWidget.App/Windowing/DesktopLayer.cs new file mode 100644 index 00000000..9c06313f --- /dev/null +++ b/Windows/src/ScriptWidget.App/Windowing/DesktopLayer.cs @@ -0,0 +1,213 @@ +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using ScriptWidget.App.Interop; + +namespace ScriptWidget.App.Windowing; + +/// +/// Keeps the widget windows on the desktop: below every ordinary window, and +/// still visible when the user asks Windows to show the desktop. +/// +/// The widgets are top-level windows held directly above the desktop window +/// in the window order (the approach Rainmeter uses) rather than children of +/// the shell's desktop window, which broke for other programs when Windows +/// 11 24H2 rearranged that window. +/// +/// "Show desktop" (Win+D, the taskbar corner) raises the window that hosts +/// the desktop icons above everything and makes it the foreground window, +/// which would cover the widgets, so while that state lasts the widgets are +/// raised too. Since Windows 11 24H2 that window is Progman itself; before, +/// it is a WorkerW that takes over the icons (Rainmeter's System.cpp +/// documents both layouts). The state is detected with a hidden probe window +/// kept directly above the desktop like the widgets: the desktop is being +/// shown when the icons window is the foreground window and has moved above +/// the probe. Both conditions must hold, so a click on the wallpaper, which +/// makes the desktop the foreground window without raising it, never lifts +/// widgets over ordinary windows. If a Windows version shows the desktop +/// another way, the widgets are covered until the desktop is left. +/// +internal sealed unsafe class DesktopLayer : NativeWindow +{ + private const string ProbeClass = "ScriptWidgetDesktopProbe"; + private const nuint RecheckTimer = 1; + private const nuint LowerTimer = 2; + private bool watching; + private int lowerAttempts; + private static DesktopLayer? current; + + private const uint GW_HWNDPREV = 3; + private readonly List windows = new(); + private nint hook; + private bool placingProbe; + private readonly uint taskbarCreated; + + /// For the self-test: a stand-in for the desktop window, so "show desktop" can be acted out without touching the real one. + internal static nint HostOverride; + /// For the self-test: which window counts as the foreground window. + internal static Func? ForegroundOverride; + + /// True while Windows is showing the desktop and the widgets are raised above it. + public bool DesktopShown { get; private set; } + + public DesktopLayer() + { + Create(ProbeClass, Win32.WS_EX_TOOLWINDOW | Win32.WS_EX_NOACTIVATE, Win32.WS_POPUP); + PlaceProbe(); + current = this; + hook = Win32.SetWinEventHook(Win32.EVENT_SYSTEM_FOREGROUND, Win32.EVENT_SYSTEM_FOREGROUND, 0, &ForegroundChanged, 0, 0, Win32.WINEVENT_OUTOFCONTEXT | Win32.WINEVENT_SKIPOWNPROCESS); + fixed (char* name = "TaskbarCreated") taskbarCreated = Win32.RegisterWindowMessageW(name); + } + + public void Add(WidgetWindow window) + { + windows.Add(window); + Place(window); + } + + public void Remove(WidgetWindow window) => windows.Remove(window); + + /// Re-applies the window order to every widget, for example after Explorer restarted. + public void Reassert() + { + PlaceProbe(); + Check(); + foreach (var window in windows) Place(window); + } + + private void Place(WidgetWindow window) + { + if (DesktopShown) window.SetOrder(Win32.HWND_TOPMOST); + else + { + window.SetOrder(Win32.HWND_NOTOPMOST); + window.SetOrder(AboveDesktop(window.Handle)); + } + } + + private void PlaceProbe() + { + placingProbe = true; + try { Win32.SetWindowPos(Handle, AboveDesktop(Handle), 0, 0, 0, 0, Win32.SWP_NOMOVE | Win32.SWP_NOSIZE | Win32.SWP_NOACTIVATE | Win32.SWP_NOOWNERZORDER); } + finally { placingProbe = false; } + } + + /// + /// The window to insert after so that it lands directly above the + /// desktop: below every ordinary window, above the window that draws the wallpaper and icons. + /// HWND_BOTTOM is not it. On Windows 11 that puts a window underneath the desktop window, where + /// it is neither seen nor clicked. + /// + private static nint AboveDesktop(nint window) + { + var desktop = IconsHost(); + if (desktop == 0) return Win32.HWND_BOTTOM; + // The nearest window above the desktop that is not the one being placed. + var previous = Win32.GetWindow(desktop, GW_HWNDPREV); + if (previous == window) previous = Win32.GetWindow(previous, GW_HWNDPREV); + return previous != 0 ? previous : Win32.HWND_BOTTOM; + } + + /// The top-level window that hosts the desktop icons: Progman, or a WorkerW on some configurations. + private static nint IconsHost() + { + if (HostOverride != 0) return HostOverride; + var worker = IconsWorker(); + return worker != 0 ? worker : Win32.GetShellWindow(); + } + + /// A WorkerW window that hosts the desktop icons, or 0 when the icons live in Progman. + private static nint IconsWorker() + { + fixed (char* workerClass = "WorkerW") + fixed (char* viewClass = "SHELLDLL_DefView") + { + nint worker = 0; + while ((worker = Win32.FindWindowExW(0, worker, workerClass, null)) != 0) + { + if (Win32.FindWindowExW(worker, 0, viewClass, null) != 0) return worker; + } + } + return 0; + } + + /// Compares the desktop window with the probe and raises or lowers the widgets when the state changed. + public void Check() + { + var host = IconsHost(); + var shown = false; + var foreground = ForegroundOverride?.Invoke() ?? Win32.GetForegroundWindow(); + var desktopInFront = host != 0 && Win32.IsWindowVisible(host) != 0 && foreground == host; + if (desktopInFront) + { + // FindWindowEx searches the windows below `host` in the window order: the probe is + // there only after the desktop window was raised above it. + fixed (char* probe = ProbeClass) shown = Win32.FindWindowExW(0, host, probe, null) == Handle; + } + // Windows makes the desktop the foreground window first and raises it some time later + // (on a real desktop, after the windows have finished minimizing), and no event reports + // the raise. So the order is looked at again for as long as the desktop is in front. + var watch = desktopInFront || DesktopShown; + if (watch != watching || shown != DesktopShown) + { + watching = watch; + if (watch) Win32.SetTimer(Handle, RecheckTimer, shown ? 500u : 100u, 0); + else Win32.KillTimer(Handle, RecheckTimer); + } + if (shown == DesktopShown) return; + DesktopShown = shown; + if (Log.Verbose) Log.Write(shown ? "desktop shown: widgets raised" : "desktop left: widgets lowered"); + foreach (var window in windows) Place(window); + if (!shown) + { + PlaceProbe(); + // The desktop window is still high in the window order when the foreground window + // changes and drops to the bottom a moment later. A widget placed "directly above the + // desktop" now would be left above ordinary windows, so the placing is repeated until + // the desktop window has settled. + lowerAttempts = 8; + Win32.SetTimer(Handle, LowerTimer, 250, 0); + } + else + { + lowerAttempts = 0; + Win32.KillTimer(Handle, LowerTimer); + } + } + + protected override nint? OnMessage(uint message, nuint wParam, nint lParam) + { + if (message == Win32.WM_TIMER && wParam == RecheckTimer) { Check(); return 0; } + if (message == Win32.WM_TIMER && wParam == LowerTimer) + { + lowerAttempts -= 1; + if (lowerAttempts <= 0 || DesktopShown) Win32.KillTimer(Handle, LowerTimer); + if (!DesktopShown) { PlaceProbe(); foreach (var window in windows) Place(window); } + return 0; + } + if (message == Win32.WM_WINDOWPOSCHANGING) + { + // Only this class moves the probe in the window order. + if (!placingProbe) ((Win32.WINDOWPOS*)lParam)->Flags |= Win32.SWP_NOZORDER; + return null; + } + if (taskbarCreated != 0 && message == taskbarCreated) { if (Log.Verbose) Log.Write("taskbar created"); Reassert(); TaskbarCreated?.Invoke(); return 0; } + return null; + } + + /// Explorer started (again): the tray icon must be added again. + public event Action? TaskbarCreated; + + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] + private static void ForegroundChanged(nint hook, uint eventID, nint window, int objectID, int childID, uint thread, uint time) + { + try { current?.Check(); } + catch (Exception error) { Log.Write($"foreground check failed: {error.Message}"); } + } + + public override void Dispose() + { + if (hook != 0) { Win32.UnhookWinEvent(hook); hook = 0; } + if (current == this) current = null; + base.Dispose(); + } +} diff --git a/Windows/src/ScriptWidget.App/Windowing/Monitors.cs b/Windows/src/ScriptWidget.App/Windowing/Monitors.cs new file mode 100644 index 00000000..b937593d --- /dev/null +++ b/Windows/src/ScriptWidget.App/Windowing/Monitors.cs @@ -0,0 +1,46 @@ +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using ScriptWidget.App.Interop; + +namespace ScriptWidget.App.Windowing; + +/// One display. Rectangles are in physical pixels of the virtual screen. +internal sealed record Monitor(nint Handle, string Device, Win32.RECT Bounds, Win32.RECT Work, double Scale, bool Primary); + +internal static unsafe class Monitors +{ + [ThreadStatic] private static List? collecting; + + public static IReadOnlyList All() + { + var result = new List(); + collecting = result; + try { Win32.EnumDisplayMonitors(0, null, &Collect, 0); } + finally { collecting = null; } + return result; + } + + public static Monitor? Describe(nint handle) + { + if (handle == 0) return null; + var info = new Win32.MONITORINFOEXW { Size = (uint)sizeof(Win32.MONITORINFOEXW) }; + if (Win32.GetMonitorInfoW(handle, &info) == 0) return null; + uint dpiX = 96, dpiY = 96; + if (Win32.GetDpiForMonitor(handle, 0, &dpiX, &dpiY) != 0 || dpiX == 0) dpiX = 96; + var device = new string(info.Device).TrimEnd('\0'); + var end = device.IndexOf('\0'); + if (end >= 0) device = device[..end]; + return new Monitor(handle, device, info.Monitor, info.Work, dpiX / 96.0, (info.Flags & Win32.MONITORINFOF_PRIMARY) != 0); + } + + public static Monitor Primary() => Describe(Win32.MonitorFromPoint(default, Win32.MONITOR_DEFAULTTOPRIMARY)) ?? throw new InvalidOperationException("No display is attached"); + + public static Monitor Containing(int x, int y) => Describe(Win32.MonitorFromPoint(new Win32.POINT { X = x, Y = y }, Win32.MONITOR_DEFAULTTONEAREST)) ?? Primary(); + + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] + private static int Collect(nint monitor, nint context, Win32.RECT* rectangle, nint data) + { + if (Describe(monitor) is { } described) collecting?.Add(described); + return 1; + } +} diff --git a/Windows/src/ScriptWidget.App/Windowing/NativeWindow.cs b/Windows/src/ScriptWidget.App/Windowing/NativeWindow.cs new file mode 100644 index 00000000..f18788dc --- /dev/null +++ b/Windows/src/ScriptWidget.App/Windowing/NativeWindow.cs @@ -0,0 +1,133 @@ +using System.Collections.Concurrent; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using ScriptWidget.App.Interop; + +namespace ScriptWidget.App.Windowing; + +/// A Win32 window whose messages are handled by a C# object. Windows are created and used on the UI thread. +internal abstract unsafe class NativeWindow : IDisposable +{ + private static readonly Dictionary Windows = new(); + private static readonly HashSet Classes = new(); + [ThreadStatic] private static NativeWindow? creating; + + public nint Handle { get; private set; } + + protected void Create(string className, uint exStyle, uint style, nint parent = 0) + { + var instance = Win32.GetModuleHandleW(null); + fixed (char* name = className) + { + if (Classes.Add(className)) + { + var windowClass = new Win32.WNDCLASSEXW + { + Size = (uint)sizeof(Win32.WNDCLASSEXW), + Style = 0x0008 /* CS_DBLCLKS */, + WindowProcedure = &WindowProcedure, + Instance = instance, + Cursor = Win32.LoadCursorW(0, Win32.IDC_ARROW), + ClassName = name, + }; + if (Win32.RegisterClassExW(&windowClass) == 0) throw new InvalidOperationException($"Could not register window class {className} (error {Marshal.GetLastPInvokeError()})"); + } + creating = this; + try + { + Handle = Win32.CreateWindowExW(exStyle, name, name, style, 0, 0, 0, 0, parent, 0, instance, 0); + } + finally { creating = null; } + } + if (Handle == 0) throw new InvalidOperationException($"Could not create window {className} (error {Marshal.GetLastPInvokeError()})"); + Windows[Handle] = this; + } + + /// Handles one message. Return null to let Windows handle it. + protected abstract nint? OnMessage(uint message, nuint wParam, nint lParam); + + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] + private static nint WindowProcedure(nint window, uint message, nuint wParam, nint lParam) + { + try + { + var target = Windows.TryGetValue(window, out var known) ? known : creating; + if (target is not null) + { + if (target.Handle == 0) target.Handle = window; + if (target.OnMessage(message, wParam, lParam) is { } result) return result; + } + } + catch (Exception error) + { + // An exception must not unwind into the window manager. + Log.Write($"window message {message:X}: {error}"); + } + return Win32.DefWindowProcW(window, message, wParam, lParam); + } + + public virtual void Dispose() + { + if (Handle == 0) return; + var handle = Handle; + Handle = 0; + Win32.DestroyWindow(handle); + Windows.Remove(handle); + } +} + +/// Runs work on the UI thread from any thread. +internal sealed unsafe class UiDispatcher : NativeWindow +{ + private const uint Wake = Win32.WM_APP + 1; + private readonly ConcurrentQueue queue = new(); + private readonly int thread = Environment.CurrentManagedThreadId; + + public UiDispatcher() => Create("ScriptWidgetDispatcher", 0, 0, Win32.HWND_MESSAGE); + + public bool OnUiThread => Environment.CurrentManagedThreadId == thread; + + public void Post(Action work) + { + queue.Enqueue(work); + var handle = Handle; + if (handle != 0) Win32.PostMessageW(handle, Wake, 0, 0); + } + + protected override nint? OnMessage(uint message, nuint wParam, nint lParam) + { + if (message != Wake) return null; + while (queue.TryDequeue(out var work)) + { + try { work(); } + catch (Exception error) { Log.Write($"dispatched work failed: {error}"); } + } + return 0; + } +} + +/// A small rolling log in the data folder. Never receives package output, tokens or response bodies. +internal static class Log +{ + private static readonly object Gate = new(); + public static string? File { get; set; } + /// Extra detail for diagnosing input on a real desktop; set SCRIPTWIDGET_VERBOSE=1. + public static bool Verbose { get; } = Environment.GetEnvironmentVariable("SCRIPTWIDGET_VERBOSE") == "1"; + + public static void Write(string message) + { + var line = $"{DateTimeOffset.Now:yyyy-MM-dd HH:mm:ss.fff} {message}"; + System.Diagnostics.Debug.WriteLine(line); + if (File is not { } path) return; + lock (Gate) + { + try + { + var info = new FileInfo(path); + if (info.Exists && info.Length > 512 * 1024) System.IO.File.Move(path, path + ".1", overwrite: true); + System.IO.File.AppendAllText(path, line + Environment.NewLine); + } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) { } + } + } +} diff --git a/Windows/src/ScriptWidget.App/Windowing/TrayIcon.cs b/Windows/src/ScriptWidget.App/Windowing/TrayIcon.cs new file mode 100644 index 00000000..6cbd044d --- /dev/null +++ b/Windows/src/ScriptWidget.App/Windowing/TrayIcon.cs @@ -0,0 +1,131 @@ +using ScriptWidget.App.Interop; + +namespace ScriptWidget.App.Windowing; + +/// One entry of a popup menu. A null is a separator. +internal sealed record MenuItem(string? Text, Action? Invoke = null, bool Checked = false, bool Enabled = true, IReadOnlyList? Children = null) +{ + public static readonly MenuItem Separator = new((string?)null); +} + +/// Shows a native popup menu and runs the chosen item. +internal static unsafe class PopupMenu +{ + public static void Show(nint owner, int x, int y, IReadOnlyList items) + { + var actions = new List { null }; // command 0 means "nothing chosen" + var menu = Build(items, actions); + try + { + // A menu only closes on an outside click when its owner is the foreground window. + Win32.SetForegroundWindow(owner); + var chosen = Win32.TrackPopupMenuEx(menu, Win32.TPM_RIGHTBUTTON | Win32.TPM_RETURNCMD | Win32.TPM_NONOTIFY, x, y, owner, null); + Win32.PostMessageW(owner, Win32.WM_NULL, 0, 0); + if (chosen > 0 && chosen < actions.Count) actions[chosen]?.Invoke(); + } + finally { Win32.DestroyMenu(menu); } + } + + private static nint Build(IReadOnlyList items, List actions) + { + var menu = Win32.CreatePopupMenu(); + foreach (var item in items) + { + if (item.Text is null) { Win32.AppendMenuW(menu, Win32.MF_SEPARATOR, 0, null); continue; } + var flags = Win32.MF_STRING | (item.Checked ? Win32.MF_CHECKED : 0) | (item.Enabled ? 0 : Win32.MF_GRAYED); + fixed (char* text = item.Text) + { + if (item.Children is { Count: > 0 } children) Win32.AppendMenuW(menu, flags | Win32.MF_POPUP, (nuint)Build(children, actions), text); + else + { + actions.Add(item.Invoke); + Win32.AppendMenuW(menu, flags, (nuint)(actions.Count - 1), text); + } + } + } + return menu; + } +} + +/// The notification-area icon. A left click or a right click opens the menu built by . +internal sealed unsafe class TrayIcon : NativeWindow +{ + private const uint Callback = Win32.WM_APP + 2; + private readonly nint icon; + private string tip; + private bool added; + + /// Builds the menu each time it is opened. + public Func>? Menu { get; set; } + /// Double click on the icon. + public event Action? Activated; + + public TrayIcon(string tip, string? iconPath) + { + this.tip = tip; + Create("ScriptWidgetTray", 0, 0); + nint loaded = 0; + if (iconPath is not null && File.Exists(iconPath)) + { + fixed (char* path = iconPath) loaded = Win32.LoadImageW(0, path, Win32.IMAGE_ICON, 0, 0, Win32.LR_LOADFROMFILE | Win32.LR_DEFAULTSIZE); + } + icon = loaded != 0 ? loaded : Win32.LoadIconW(0, Win32.IDI_APPLICATION); + Add(); + } + + /// Adds the icon; called again when Explorer restarts. + public void Add() + { + var data = Data(); + data.Flags = Win32.NIF_MESSAGE | Win32.NIF_ICON | Win32.NIF_TIP | Win32.NIF_SHOWTIP; + added = Win32.Shell_NotifyIconW(Win32.NIM_ADD, &data) != 0; + if (Log.Verbose) Log.Write($"tray icon added: {added}"); + data.VersionOrTimeout = Win32.NOTIFYICON_VERSION_4; + Win32.Shell_NotifyIconW(Win32.NIM_SETVERSION, &data); + } + + public void SetTip(string text) + { + tip = text; + var data = Data(); + data.Flags = Win32.NIF_TIP | Win32.NIF_SHOWTIP; + Win32.Shell_NotifyIconW(Win32.NIM_MODIFY, &data); + } + + private Win32.NOTIFYICONDATAW Data() + { + var data = new Win32.NOTIFYICONDATAW { Size = (uint)sizeof(Win32.NOTIFYICONDATAW), Window = Handle, ID = 1, CallbackMessage = Callback, Icon = icon }; + var text = tip.Length > 127 ? tip[..127] : tip; + text.AsSpan().CopyTo(new Span(data.Tip, 127)); + return data; + } + + protected override nint? OnMessage(uint message, nuint wParam, nint lParam) + { + if (message != Callback) return null; + // With NOTIFYICON_VERSION_4 the event is in the low word of lParam and the position in wParam. + var notification = (uint)Win32.LowWord((nuint)lParam); + if (notification is Win32.WM_CONTEXTMENU or Win32.NIN_SELECT or Win32.NIN_KEYSELECT) + { + if (Menu?.Invoke() is { Count: > 0 } items) + { + Win32.POINT cursor; + Win32.GetCursorPos(&cursor); + PopupMenu.Show(Handle, cursor.X, cursor.Y, items); + } + } + else if (notification == Win32.WM_LBUTTONDBLCLK) Activated?.Invoke(); + return 0; + } + + public override void Dispose() + { + if (added && Handle != 0) + { + var data = Data(); + Win32.Shell_NotifyIconW(Win32.NIM_DELETE, &data); + added = false; + } + base.Dispose(); + } +} diff --git a/Windows/src/ScriptWidget.App/Windowing/WidgetWindow.cs b/Windows/src/ScriptWidget.App/Windowing/WidgetWindow.cs new file mode 100644 index 00000000..f52c588e --- /dev/null +++ b/Windows/src/ScriptWidget.App/Windowing/WidgetWindow.cs @@ -0,0 +1,200 @@ +using ScriptWidget.App.Interop; + +namespace ScriptWidget.App.Windowing; + +/// +/// One desktop widget: a layered window with per-pixel transparency that +/// shows a bitmap and reports clicks. It never takes focus, stays out of the +/// taskbar and Alt-Tab, and its place in the window order is decided by +/// alone. Fully transparent pixels let clicks +/// through to whatever is behind. +/// +internal sealed unsafe class WidgetWindow : NativeWindow +{ + public const string ClassName = "ScriptWidgetWidget"; + private const int DragThreshold = 4; + + private int x, y, width, height; + private bool pressed, dragging, visible; + private Win32.POINT pressCursor, pressOrigin; + private bool allowOrder; + + /// When true the widget cannot be dragged. + public bool Locked { get; set; } + /// A click that was not a drag, in pixels from the window's top-left corner. + public event Action? Clicked; + /// The user finished dragging the widget. + public event Action? Moved; + /// Right click, in screen pixels. + public event Action? MenuRequested; + /// The window is now on a display with another scale. + public event Action? ScaleChanged; + /// True when the point (window pixels) is over something clickable. + public Func? IsInteractive { get; set; } + + public WidgetWindow() => Create(ClassName, Win32.WS_EX_LAYERED | Win32.WS_EX_TOOLWINDOW | Win32.WS_EX_NOACTIVATE, Win32.WS_POPUP); + + public int X => x; + public int Y => y; + public int Width => width; + public int Height => height; + + /// Scale of the display the window is on. + public double Scale => Monitors.Describe(Win32.MonitorFromWindow(Handle, Win32.MONITOR_DEFAULTTONEAREST))?.Scale ?? 1.0; + + public void MoveTo(int newX, int newY) + { + x = newX; + y = newY; + if (Handle != 0) Win32.SetWindowPos(Handle, 0, x, y, 0, 0, Win32.SWP_NOSIZE | Win32.SWP_NOZORDER | Win32.SWP_NOACTIVATE); + } + + /// Shows a premultiplied BGRA bitmap, resizing the window to it. + public void SetBitmap(ReadOnlySpan bgra, int pixelWidth, int pixelHeight) + { + if (Handle == 0 || pixelWidth <= 0 || pixelHeight <= 0 || bgra.Length < pixelWidth * pixelHeight * 4) return; + var screen = Win32.GetDC(0); + var memory = Win32.CreateCompatibleDC(screen); + var header = new Win32.BITMAPINFOHEADER + { + Size = (uint)sizeof(Win32.BITMAPINFOHEADER), + Width = pixelWidth, + Height = -pixelHeight, // top-down rows + Planes = 1, + BitCount = 32, + }; + void* bits = null; + var bitmap = Win32.CreateDIBSection(screen, &header, 0, &bits, 0, 0); + try + { + if (bitmap == 0 || bits is null) return; + bgra[..(pixelWidth * pixelHeight * 4)].CopyTo(new Span(bits, pixelWidth * pixelHeight * 4)); + var previous = Win32.SelectObject(memory, bitmap); + var position = new Win32.POINT { X = x, Y = y }; + var size = new Win32.SIZE { Width = pixelWidth, Height = pixelHeight }; + var source = new Win32.POINT(); + var blend = new Win32.BLENDFUNCTION { BlendOp = Win32.AC_SRC_OVER, SourceConstantAlpha = 255, AlphaFormat = Win32.AC_SRC_ALPHA }; + allowOrder = true; + try { Win32.UpdateLayeredWindow(Handle, screen, &position, &size, memory, &source, 0, &blend, Win32.ULW_ALPHA); } + finally { allowOrder = false; } + Win32.SelectObject(memory, previous); + width = pixelWidth; + height = pixelHeight; + } + finally + { + if (bitmap != 0) Win32.DeleteObject(bitmap); + Win32.DeleteDC(memory); + Win32.ReleaseDC(0, screen); + } + if (!visible) + { + visible = true; + Win32.ShowWindow(Handle, Win32.SW_SHOWNOACTIVATE); + } + } + + /// Places the window in the window order; every other attempt to reorder it is refused. + public void SetOrder(nint insertAfter) + { + if (Handle == 0) return; + allowOrder = true; + try { Win32.SetWindowPos(Handle, insertAfter, 0, 0, 0, 0, Win32.SWP_NOMOVE | Win32.SWP_NOSIZE | Win32.SWP_NOACTIVATE | Win32.SWP_NOOWNERZORDER); } + finally { allowOrder = false; } + } + + protected override nint? OnMessage(uint message, nuint wParam, nint lParam) + { + switch (message) + { + case Win32.WM_MOUSEACTIVATE: + return Win32.MA_NOACTIVATE; + + case Win32.WM_WINDOWPOSCHANGING: + // Clicking or another program must not bring a desktop widget forward. + if (!allowOrder) ((Win32.WINDOWPOS*)lParam)->Flags |= Win32.SWP_NOZORDER; + return null; + + case Win32.WM_LBUTTONDOWN or Win32.WM_LBUTTONDBLCLK: + { + pressed = true; + dragging = false; + // The pointer position comes from the message, not from the cursor at this instant. + pressCursor = new Win32.POINT { X = x + Win32.LowWord(lParam), Y = y + Win32.HighWord(lParam) }; + pressOrigin = new Win32.POINT { X = x, Y = y }; + Win32.SetCapture(Handle); + return 0; + } + + case Win32.WM_MOUSEMOVE: + { + if (!pressed) return null; + var cursor = new Win32.POINT { X = x + Win32.LowWord(lParam), Y = y + Win32.HighWord(lParam) }; + var deltaX = cursor.X - pressCursor.X; + var deltaY = cursor.Y - pressCursor.Y; + if (!dragging && !Locked && (Math.Abs(deltaX) > DragThreshold || Math.Abs(deltaY) > DragThreshold)) dragging = true; + if (dragging) + { + var (snappedX, snappedY) = Snap(pressOrigin.X + deltaX, pressOrigin.Y + deltaY); + MoveTo(snappedX, snappedY); + } + return 0; + } + + case Win32.WM_LBUTTONUP: + { + if (Log.Verbose) Log.Write($"button up: pressed={pressed} dragging={dragging} at {Win32.LowWord(lParam)},{Win32.HighWord(lParam)}"); + if (!pressed) return null; + var wasDragging = dragging; + pressed = false; + dragging = false; + Win32.ReleaseCapture(); + if (wasDragging) Moved?.Invoke(); + else Clicked?.Invoke(Win32.LowWord(lParam), Win32.HighWord(lParam)); + return 0; + } + + case Win32.WM_CAPTURECHANGED: + // Capture also ends when the button is released; only a drag in progress is finished here, + // so a click still arrives with the button-up message. + if (pressed && dragging) { pressed = false; dragging = false; Moved?.Invoke(); } + return 0; + + case Win32.WM_RBUTTONUP: + { + Win32.POINT cursor; + Win32.GetCursorPos(&cursor); + MenuRequested?.Invoke(cursor.X, cursor.Y); + return 0; + } + + case Win32.WM_SETCURSOR: + { + if (Win32.LowWord(lParam) != Win32.HTCLIENT) return null; + Win32.POINT cursor; + Win32.GetCursorPos(&cursor); + var interactive = IsInteractive?.Invoke(cursor.X - x, cursor.Y - y) ?? false; + Win32.SetCursor(Win32.LoadCursorW(0, dragging ? Win32.IDC_SIZEALL : interactive ? Win32.IDC_HAND : Win32.IDC_ARROW)); + return 1; + } + + case Win32.WM_DPICHANGED: + ScaleChanged?.Invoke(); + return 0; + } + return null; + } + + /// Snaps the window's edges to the edges of the work area when they come close. + private (int X, int Y) Snap(int proposedX, int proposedY) + { + var monitor = Monitors.Containing(proposedX + width / 2, proposedY + height / 2); + var distance = (int)Math.Round(10 * monitor.Scale); + var work = monitor.Work; + if (Math.Abs(proposedX - work.Left) < distance) proposedX = work.Left; + else if (Math.Abs(proposedX + width - work.Right) < distance) proposedX = work.Right - width; + if (Math.Abs(proposedY - work.Top) < distance) proposedY = work.Top; + else if (Math.Abs(proposedY + height - work.Bottom) < distance) proposedY = work.Bottom - height; + return (proposedX, proposedY); + } +} diff --git a/Windows/src/ScriptWidget.App/app.manifest b/Windows/src/ScriptWidget.App/app.manifest new file mode 100644 index 00000000..bdd0754e --- /dev/null +++ b/Windows/src/ScriptWidget.App/app.manifest @@ -0,0 +1,25 @@ + + + + + + PerMonitorV2 + true/pm + true + UTF-8 + + + + + + + + + + + + + + + + diff --git a/Windows/src/ScriptWidget.Cli/Program.cs b/Windows/src/ScriptWidget.Cli/Program.cs new file mode 100644 index 00000000..c623edb5 --- /dev/null +++ b/Windows/src/ScriptWidget.Cli/Program.cs @@ -0,0 +1,312 @@ +using System.Text; +using System.Text.Json.Nodes; +using ScriptWidget.Core; +using ScriptWidget.Core.Hosting; +using ScriptWidget.Core.Packages; +using ScriptWidget.Core.Runtime; + +namespace ScriptWidget.Cli; + +/// +/// swcli: runs ScriptWidget packages from the command line with the same +/// runtime as the desktop host. Used by CI to check packages and to draw +/// reference images, and for debugging. +/// +internal static class Program +{ + private const string Usage = """ + Usage: swcli [options] + + Package commands (take a package folder): + validate Check widget.json and compile the package. + run [options] Run the script and print the element tree, logs and errors as JSON. + render --output [opts] Run the script and draw the widget to a PNG file. + timeline Print the current timeline slot and the next transition. + pack Write the package as a .swt archive. + + Options for run and render: + --family small, medium, large or extraLarge (default medium) + --param value of $getenv("widget-param") + --dark run and draw in the dark theme + --scale pixels per point for render (default 2) + --now fixed clock for dates in render (milliseconds since 1970) + --data data folder for storage and caches (default: a temporary folder) + + Library and desktop commands (use the signed-in user's data): + list List installed packages. + install Install an archive into the library. + templates List the bundled templates that run on Windows. + widgets List the widgets on the desktop (needs the host running). + add [--family ] Put a package on the desktop (needs the host running). + refresh Refresh every desktop widget (needs the host running). + host Send one request to the host and print the answer (for scripts). + """; + + private static int Main(string[] arguments) + { + Console.OutputEncoding = new UTF8Encoding(false); + if (arguments.Length == 0 || arguments[0] is "--help" or "-h" or "help") { Console.WriteLine(Usage); return arguments.Length == 0 ? 2 : 0; } + try + { + var options = new Options(arguments.Skip(1).ToArray()); + return arguments[0] switch + { + "validate" => Validate(options), + "run" => Run(options), + "render" => Render(options), + "timeline" => Timeline(options), + "pack" => Pack(options), + "list" => List(), + "install" => Install(options), + "templates" => Templates(), + "widgets" => Host(new JsonObject { ["command"] = "list" }), + "add" => Add(options), + "refresh" => Host(new JsonObject { ["command"] = "refresh" }), + "host" => Host(Json.ParseObject(options.Positional(0, "request"))), + _ => Fail($"Unknown command {arguments[0]}\n\n{Usage}", 2), + }; + } + catch (ArgumentException error) { return Fail(error.Message, 2); } + catch (PackageException error) { return Fail($"[{error.Code}] {error.Message}", 1); } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) { return Fail(error.Message, 1); } + } + + private static int Fail(string message, int code) + { + Console.Error.WriteLine(message); + return code; + } + + private static void Print(JsonObject json) => Console.WriteLine(Json.Stringify(json, indented: true)); + + // MARK: - Package commands + + private static DataPaths ScratchData(Options options) => + new(options.Value("data") ?? Path.Combine(Path.GetTempPath(), "scriptwidget-cli", Environment.ProcessId.ToString(System.Globalization.CultureInfo.InvariantCulture))); + + private static (DiskPackageFiles Files, string Directory) Package(Options options) + { + var directory = Path.GetFullPath(options.Positional(0, "package folder")); + if (!Directory.Exists(directory)) throw new ArgumentException($"Not a package folder: {directory}"); + return (new DiskPackageFiles(directory), directory); + } + + private static int Validate(Options options) + { + var (files, directory) = Package(options); + using var compiler = new PackageCompiler(new SharedScripts()); + var report = new JsonObject { ["package"] = directory }; + var manifestText = files.ReadText(PackageManifests.FileName); + if (manifestText is not null) + { + var validation = compiler.ValidateManifest(manifestText, files.Paths()); + report["manifest"] = validation; + if (!validation.OptBoolean("ok")) { report["ok"] = false; Print(report); return 1; } + } + else report["manifest"] = null; + try + { + compiler.Prepare(files); + report["ok"] = true; + } + catch (RuntimeError error) + { + report["ok"] = false; + report["error"] = new JsonObject { ["kind"] = error.Kind, ["message"] = error.Message }; + } + Print(report); + return report.OptBoolean("ok") ? 0 : 1; + } + + private static RunRequest Request(Options options, DiskPackageFiles files, string directory) => new() + { + Files = files, + PackageID = "cli:" + directory, + Family = WidgetFamilies.IsKnown(options.Value("family") ?? "medium") ? options.Value("family") ?? "medium" : throw new ArgumentException($"Unknown family {options.Value("family")}"), + Parameter = options.Value("param") ?? "", + DarkMode = options.Flag("dark"), + }; + + private static JsonObject Describe(RunResult result) + { + var logs = new JsonArray(); + foreach (var entry in result.Execution.Logs) logs.Append(new JsonObject { ["level"] = entry.Level, ["message"] = entry.Message }); + return new JsonObject + { + ["ok"] = result.Execution.Error is null, + ["error"] = result.Execution.Error is { } error ? new JsonObject { ["kind"] = error.Kind, ["message"] = error.Message } : null, + ["elapsedMilliseconds"] = result.Execution.ElapsedMilliseconds, + ["logs"] = logs, + ["diagnostics"] = result.Execution.Diagnostics.DeepClone(), + ["nextTimelineTransition"] = result.NextTimelineTransition, + }; + } + + private static int Run(Options options) + { + var (files, directory) = Package(options); + using var runtime = new WidgetRuntime(ScratchData(options)); + var result = runtime.Run(Request(options, files, directory)); + var report = Describe(result); + report["tree"] = result.Execution.Tree is { } tree ? Json.TryParse(tree) : null; + Print(report); + return result.Execution.Error is null ? 0 : 1; + } + + private static int Render(Options options) + { + var (files, directory) = Package(options); + var output = Path.GetFullPath(options.Value("output") ?? throw new ArgumentException("render needs --output ")); + var scale = float.TryParse(options.Value("scale") ?? "2", System.Globalization.CultureInfo.InvariantCulture, out var parsed) && parsed is >= 0.5f and <= 8 ? parsed : throw new ArgumentException("--scale must be between 0.5 and 8"); + long? now = options.Value("now") is { } text ? long.Parse(text, System.Globalization.CultureInfo.InvariantCulture) : null; + using var runtime = new WidgetRuntime(ScratchData(options)); + var request = Request(options, files, directory); + var result = runtime.Run(request); + var (width, height) = WidgetFamilies.Size(request.Family); + var rendered = runtime.Render(request, result, (float)width, (float)height, scale, nowMilliseconds: now); + Directory.CreateDirectory(Path.GetDirectoryName(output)!); + File.WriteAllBytes(output, rendered.EncodePng()); + var report = Describe(result); + report["output"] = output; + report["pixelWidth"] = rendered.PixelWidth; + report["pixelHeight"] = rendered.PixelHeight; + report["layoutMilliseconds"] = rendered.LayoutMilliseconds; + report["paintMilliseconds"] = rendered.PaintMilliseconds; + report["renderDiagnostics"] = rendered.Diagnostics.DeepClone(); + var regions = new JsonArray(); + foreach (var region in rendered.HitRegions) + { + regions.Append(new JsonObject + { + ["x"] = Math.Round(region.Bounds.Left, 1), ["y"] = Math.Round(region.Bounds.Top, 1), + ["width"] = Math.Round(region.Bounds.Width, 1), ["height"] = Math.Round(region.Bounds.Height, 1), + ["interaction"] = region.Interaction.DeepClone(), + }); + } + report["hitRegions"] = regions; + Print(report); + return result.Execution.Error is null ? 0 : 1; + } + + private static int Timeline(Options options) + { + var (files, _) = Package(options); + var scripts = new SharedScripts(); + var manifest = files.ReadText(PackageManifests.FileName) is { } text ? PackageManifests.Parse(text) : null; + var state = new TimelinePlanner(scripts).State(manifest); + Print(new JsonObject + { + ["hasSchedule"] = state is not null, + ["environments"] = state is null ? null : Json.Object(state.Environments), + ["nextTransition"] = state?.NextTransitionMilliseconds, + }); + return 0; + } + + private static int Pack(Options options) + { + var (_, directory) = Package(options); + var output = Path.GetFullPath(options.Positional(1, "output file")); + // Export through a temporary library so the archive is written by the same hardened code as the app's export. + var scratch = Path.Combine(Path.GetTempPath(), "scriptwidget-cli", "pack-" + Guid.NewGuid().ToString("N")); + try + { + var store = new PackageStore(Path.Combine(scratch, "library"), Path.Combine(scratch, "staging")); + var source = new DiskPackageFiles(directory); + var main = source.ReadText(PackageManifests.LegacyEntry) ?? throw new ArgumentException("The package has no main.jsx"); + var name = store.UniqueName(Path.GetFileName(directory.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar))); + var created = store.Create(name, main, source.ReadText(PackageManifests.FileName)); + foreach (var path in source.Paths()) + { + if (path is PackageManifests.LegacyEntry or PackageManifests.FileName) continue; + if (source.ReadBytes(path) is { } bytes) store.WriteFile(created.Name, path, bytes); + } + using var stream = File.Create(output); + store.ExportArchive(created.Name, stream); + } + finally + { + try { Directory.Delete(scratch, true); } catch (Exception error) when (error is IOException or UnauthorizedAccessException) { } + } + Console.WriteLine(output); + return 0; + } + + // MARK: - Library and desktop commands + + private static int List() + { + var library = new Library(new DataPaths()); + var items = new JsonArray(); + foreach (var package in library.List()) items.Append(new JsonObject { ["name"] = package.Name, ["title"] = package.DisplayName, ["installation"] = package.Installation, ["directory"] = package.Directory }); + Print(new JsonObject { ["packages"] = items }); + return 0; + } + + private static int Templates() + { + var library = new Library(new DataPaths()); + var items = new JsonArray(); + foreach (var template in library.Bundled()) items.Append(new JsonObject { ["group"] = template.Group, ["name"] = template.Name }); + Print(new JsonObject { ["templates"] = items }); + return 0; + } + + private static int Install(Options options) + { + var file = Path.GetFullPath(options.Positional(0, "archive")); + var paths = new DataPaths(); + var library = new Library(paths); + using var runtime = new WidgetRuntime(paths); + using var stream = File.OpenRead(file); + var installed = library.ImportArchive(stream, Path.GetFileNameWithoutExtension(file), files => + { + try { runtime.ValidatePackage(files); } + catch (RuntimeError error) { throw new PackageException(PackageErrors.InvalidManifest, error.Message); } + }); + HostPipe.Send(new JsonObject { ["command"] = "library-changed" }, 300, 3000); + Print(new JsonObject { ["name"] = installed.Name, ["installation"] = installed.Installation }); + return 0; + } + + private static int Add(Options options) + { + var name = options.Positional(0, "package name"); + var library = new Library(new DataPaths()); + var package = library.Find(name) ?? library.FindByInstallation(name) ?? throw new ArgumentException($"No installed package named {name}"); + return Host(new JsonObject { ["command"] = "add", ["package"] = package.Installation, ["family"] = options.Value("family") ?? "medium", ["parameter"] = options.Value("param") ?? "" }); + } + + private static int Host(JsonObject request) + { + if (HostPipe.Send(request) is not { } response) return Fail("ScriptWidget is not running.", 1); + Print(response); + return response.OptBoolean("ok") ? 0 : 1; + } + + /// --name value options, --flag switches and positional arguments. + private sealed class Options + { + private static readonly HashSet Flags = ["dark"]; + private readonly Dictionary values = new(); + private readonly HashSet flags = new(); + private readonly List positional = new(); + + public Options(string[] arguments) + { + for (var index = 0; index < arguments.Length; index++) + { + var argument = arguments[index]; + if (!argument.StartsWith("--", StringComparison.Ordinal)) { positional.Add(argument); continue; } + var name = argument[2..]; + if (Flags.Contains(name)) { flags.Add(name); continue; } + if (index + 1 >= arguments.Length) throw new ArgumentException($"--{name} needs a value"); + values[name] = arguments[++index]; + } + } + + public string? Value(string name) => values.GetValueOrDefault(name); + public bool Flag(string name) => flags.Contains(name); + public string Positional(int index, string what) => index < positional.Count ? positional[index] : throw new ArgumentException($"Missing {what}"); + } +} diff --git a/Windows/src/ScriptWidget.Cli/ScriptWidget.Cli.csproj b/Windows/src/ScriptWidget.Cli/ScriptWidget.Cli.csproj new file mode 100644 index 00000000..de524b9f --- /dev/null +++ b/Windows/src/ScriptWidget.Cli/ScriptWidget.Cli.csproj @@ -0,0 +1,12 @@ + + + Exe + swcli + ScriptWidget.Cli + true + en + + + + + diff --git a/Windows/src/ScriptWidget.Core/Api/DeviceInfo.cs b/Windows/src/ScriptWidget.Core/Api/DeviceInfo.cs new file mode 100644 index 00000000..0294117d --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Api/DeviceInfo.cs @@ -0,0 +1,182 @@ +using System.Globalization; +using System.Runtime.InteropServices; +using System.Text.Json.Nodes; +using ScriptWidget.Core.Runtime; + +namespace ScriptWidget.Core.Api; + +/// +/// Snapshots behind $device and $system. Values are captured +/// once per execution, like the native iOS runtime. None of them needs a +/// permission. +/// +public static unsafe class DeviceInfo +{ + public static JsonObject Device() + { + var scale = PrimaryScale(); + var width = GetSystemMetrics(0) / scale; + var height = GetSystemMetrics(1) / scale; + var level = -1.0; + var state = "unknown"; + if (GetSystemPowerStatus(out var power) != 0) + { + if (power.BatteryLifePercent <= 100) level = power.BatteryLifePercent / 100.0; + var noBattery = (power.BatteryFlag & 128) != 0 || power.BatteryFlag == 255; + state = noBattery ? "full" + : (power.BatteryFlag & 8) != 0 ? "charging" + : power.ACLineStatus == 1 ? (power.BatteryLifePercent >= 100 ? "full" : "charging") + : power.ACLineStatus == 0 ? "unplugged" + : "unknown"; + if (noBattery) level = 1.0; + } + long total = 0, free = 0; + try + { + var drive = new DriveInfo(Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)) ?? "C:\\"); + total = drive.TotalSize; + free = drive.AvailableFreeSpace; + } + catch (Exception error) when (error is IOException or UnauthorizedAccessException or ArgumentException) { } + return new JsonObject + { + ["screen"] = new JsonObject { ["width"] = width, ["height"] = height, ["scale"] = scale }, + ["language"] = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName, + ["model"] = "PC", + ["name"] = Environment.MachineName, + ["battery"] = new JsonObject { ["level"] = level, ["state"] = state }, + ["systemVersion"] = Environment.OSVersion.Version.ToString(), + ["totalDiskSpace"] = total, + ["freeDiskSpace"] = free, + }; + } + + public static JsonObject System() + { + var culture = CultureInfo.CurrentCulture; + var zone = TimeZoneInfo.Local; + var now = DateTimeOffset.Now; + var memory = new MemoryStatus { Length = (uint)sizeof(MemoryStatus) }; + GlobalMemoryStatusEx(ref memory); + var saver = GetSystemPowerStatus(out var power) != 0 && power.SystemStatusFlag == 1; + int animations = 1; + SystemParametersInfoW(0x1042 /* SPI_GETCLIENTAREAANIMATION */, 0, &animations, 0); + var languages = new JsonArray(); + foreach (var language in PreferredLanguages()) languages.Append(language); + var version = typeof(DeviceInfo).Assembly.GetName().Version; + var build = Environment.OSVersion.Version.Build; + return new JsonObject + { + ["appInfo"] = new JsonObject + { + ["name"] = "ScriptWidget", + ["bundleId"] = "com.atomsprout.scriptwidget", + ["version"] = version is null ? "" : $"{version.Major}.{version.Minor}.{version.Build}", + ["build"] = version?.Revision.ToString(CultureInfo.InvariantCulture) ?? "", + }, + ["locale"] = culture.Name.Replace('-', '_'), + ["preferredLanguages"] = languages, + ["timeZone"] = new JsonObject + { + ["identifier"] = IntlBridge.DefaultTimeZone(), + ["abbreviation"] = Abbreviation(zone, now), + ["offsetSeconds"] = (int)zone.GetUtcOffset(now).TotalSeconds, + }, + ["is24HourClock"] = culture.DateTimeFormat.ShortTimePattern.Contains('H'), + ["calendarInfo"] = new JsonObject + { + ["identifier"] = culture.Calendar is GregorianCalendar ? "gregorian" : culture.Calendar.GetType().Name.Replace("Calendar", "").ToLowerInvariant(), + // Like Calendar.firstWeekday on iOS: Sunday is 1. + ["firstWeekday"] = (int)culture.DateTimeFormat.FirstDayOfWeek + 1, + ["minimumDaysInFirstWeek"] = culture.DateTimeFormat.CalendarWeekRule switch { CalendarWeekRule.FirstFourDayWeek => 4, CalendarWeekRule.FirstFullWeek => 7, _ => 1 }, + }, + ["systemUptime"] = Environment.TickCount64 / 1000.0, + ["memory"] = new JsonObject { ["physical"] = (long)memory.TotalPhysical }, + ["thermalState"] = "nominal", + ["lowPowerMode"] = saver, + ["brightness"] = 1.0, + ["reduceMotionEnabled"] = animations == 0, + ["platform"] = "windows", + ["hostName"] = Environment.MachineName, + ["processName"] = "ScriptWidget", + ["osVersionString"] = $"Windows {(build >= 22000 ? "11" : "10")} (build {build})", + ["processorCount"] = Environment.ProcessorCount, + ["activeProcessorCount"] = Environment.ProcessorCount, + }; + } + + /// True when Windows is set to the dark app theme. + public static bool IsDarkMode() + { + try + { + using var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"); + return key?.GetValue("AppsUseLightTheme") is int value && value == 0; + } + catch (Exception error) when (error is IOException or UnauthorizedAccessException or global::System.Security.SecurityException) { return false; } + } + + private static string Abbreviation(TimeZoneInfo zone, DateTimeOffset now) + { + var offset = zone.GetUtcOffset(now); + var sign = offset < TimeSpan.Zero ? "-" : "+"; + var minutes = Math.Abs(offset.Minutes); + return "GMT" + sign + Math.Abs(offset.Hours).ToString(CultureInfo.InvariantCulture) + (minutes == 0 ? "" : ":" + minutes.ToString("00", CultureInfo.InvariantCulture)); + } + + private static IEnumerable PreferredLanguages() + { + uint count = 0, length = 0; + if (GetUserPreferredUILanguages(8 /* MUI_LANGUAGE_NAME */, &count, null, &length) != 0 && length > 1) + { + var buffer = new char[length]; + fixed (char* chars = buffer) + { + if (GetUserPreferredUILanguages(8, &count, chars, &length) != 0) + { + var names = new string(buffer).Split('\0', StringSplitOptions.RemoveEmptyEntries); + if (names.Length > 0) return names; + } + } + } + return [CultureInfo.CurrentUICulture.Name]; + } + + private static double PrimaryScale() + { + var dpi = GetDpiForSystem(); + return dpi == 0 ? 1.0 : dpi / 96.0; + } + + [StructLayout(LayoutKind.Sequential)] + private struct PowerStatus + { + public byte ACLineStatus; + public byte BatteryFlag; + public byte BatteryLifePercent; + public byte SystemStatusFlag; + public uint BatteryLifeTime; + public uint BatteryFullLifeTime; + } + + [StructLayout(LayoutKind.Sequential)] + private struct MemoryStatus + { + public uint Length; + public uint MemoryLoad; + public ulong TotalPhysical; + public ulong AvailablePhysical; + public ulong TotalPageFile; + public ulong AvailablePageFile; + public ulong TotalVirtual; + public ulong AvailableVirtual; + public ulong AvailableExtendedVirtual; + } + + [DllImport("kernel32.dll")] private static extern int GetSystemPowerStatus(out PowerStatus status); + [DllImport("kernel32.dll")] private static extern int GlobalMemoryStatusEx(ref MemoryStatus status); + [DllImport("kernel32.dll")] private static extern int GetUserPreferredUILanguages(uint flags, uint* count, char* buffer, uint* length); + [DllImport("user32.dll")] private static extern int GetSystemMetrics(int index); + [DllImport("user32.dll")] private static extern uint GetDpiForSystem(); + [DllImport("user32.dll")] private static extern int SystemParametersInfoW(uint action, uint parameter, void* value, uint flags); +} diff --git a/Windows/src/ScriptWidget.Core/Api/FetchModule.cs b/Windows/src/ScriptWidget.Core/Api/FetchModule.cs new file mode 100644 index 00000000..1a670590 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Api/FetchModule.cs @@ -0,0 +1,371 @@ +using System.Diagnostics; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Text.Json.Nodes; +using ScriptWidget.Core.Runtime; + +namespace ScriptWidget.Core.Api; + +/// +/// fetch and $http. Mirrors ScriptWidgetRuntimeFetch: public HTTP(S) +/// only, the package's declared hosts only, bounded request and response +/// sizes, a per-execution request budget, and every redirect re-validated. +/// Beyond the Swift host-name rule, the resolved addresses are checked so a +/// public name cannot point at a private or loopback address. On Windows the +/// connection is opened to the very address that was checked, so a DNS answer +/// that changes between the check and the connect cannot be used. +/// +public sealed class FetchModule : INativeModule +{ + public const int MaximumResponseBytes = 2 * 1024 * 1024; + public const int MaximumRequestBodyBytes = 1024 * 1024; + public const int MaximumRequests = 32; + public const int MaximumConcurrent = 8; + public const int MaximumRedirects = 5; + public const long MaximumTotalMilliseconds = 10_000L; + public static readonly IReadOnlyList ExposedHeaders = ["content-type", "cache-control", "expires", "last-modified", "date", "retry-after"]; + + internal const string BlockedMessage = "Local and private network hosts are not allowed"; + + /// Request headers that carry credentials; they stay with the origin the script addressed. + private static readonly HashSet CredentialHeaders = new(StringComparer.OrdinalIgnoreCase) { "Authorization", "Proxy-Authorization", "Cookie" }; + + private static bool SameOrigin(Uri first, Uri second) => + first.Scheme.Equals(second.Scheme, StringComparison.OrdinalIgnoreCase) && first.Host.Equals(second.Host, StringComparison.OrdinalIgnoreCase) && first.Port == second.Port; + + /// Sent when the script sets no User-Agent of its own. + public static string DefaultUserAgent { get; } = $"ScriptWidget-Windows/{(typeof(FetchModule).Assembly.GetName().Version is { } version ? $"{version.Major}.{version.Minor}.{version.Build}" : "0")}"; + private const string TimedOutMessage = "The request timed out."; + private static readonly HashSet Methods = new(StringComparer.Ordinal) { "OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE", "PATCH", "TRACE" }; + private static readonly HashSet ContentHeaders = new(StringComparer.OrdinalIgnoreCase) + { + "Allow", "Content-Disposition", "Content-Encoding", "Content-Language", "Content-Location", "Content-MD5", "Content-Range", "Content-Type", "Expires", "Last-Modified", + }; + + public sealed class Response(int status, byte[] body, IReadOnlyDictionary headers) + { + public int Status { get; } = status; + public byte[] Body { get; } = body; + /// Response headers by lower-case name; the last value wins. + public IReadOnlyDictionary Headers { get; } = headers; + } + + public sealed class FetchException(string message) : Exception(message); + + private readonly Func> resolver; + private readonly HttpMessageInvoker client; + private readonly BoundedExecutor pool = new(MaximumConcurrent); + + /// Turns a host name (or address literal) into addresses; tests replace it to stay offline. + /// + /// Sends the request. The default connects only to addresses the resolver returned and + /// accepted. A replacement must not follow redirects itself. + /// + public FetchModule(Func>? resolver = null, HttpMessageHandler? handler = null) + { + this.resolver = resolver ?? SystemResolver; + client = new HttpMessageInvoker(handler ?? CreateHandler(this.resolver), disposeHandler: true); + } + + /// The operating system's resolver. An address literal is returned as is. + public static IReadOnlyList SystemResolver(string host) => IPAddress.TryParse(host, out var literal) ? [literal] : Dns.GetHostAddresses(host); + + /// + /// The production transport: no automatic redirects, cookies, credentials or proxy, and a + /// connect step that resolves the host itself and connects to a vetted address only. A proxy + /// is not used because the address it would reach cannot be checked here. + /// + public static SocketsHttpHandler CreateHandler(Func> resolver) => new() + { + AllowAutoRedirect = false, + UseCookies = false, + UseProxy = false, + Proxy = null, + DefaultProxyCredentials = null, + Credentials = null, + PreAuthenticate = false, + AutomaticDecompression = DecompressionMethods.All, + ConnectTimeout = TimeSpan.FromMilliseconds(MaximumTotalMilliseconds), + PooledConnectionIdleTimeout = TimeSpan.FromSeconds(30), + PooledConnectionLifetime = TimeSpan.FromMinutes(2), + MaxConnectionsPerServer = MaximumConcurrent, + MaxResponseHeadersLength = 64, + ConnectCallback = (context, cancellation) => ConnectAsync(resolver, context.DnsEndPoint, cancellation), + }; + + /// + /// Opens the TCP connection for one request. The host is refused by name first; then every + /// address it resolves to must be public, and the socket connects to one of exactly those + /// addresses. The name is never resolved a second time by the socket layer. + /// + internal static async ValueTask ConnectAsync(Func> resolver, DnsEndPoint endPoint, CancellationToken cancellation) + { + var host = Unbracket(endPoint.Host); + if (host.Length == 0 || RuntimePolicy.BlockedHostName(host)) throw new FetchException(BlockedMessage); + IReadOnlyList addresses; + try { addresses = resolver(host); } + catch (Exception) { throw new FetchException($"Could not resolve {endPoint.Host}"); } + if (addresses.Count == 0 || addresses.Any(RuntimePolicy.BlockedAddress)) throw new FetchException(BlockedMessage); + Exception? last = null; + foreach (var address in addresses) + { + var socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp) { NoDelay = true }; + try + { + await socket.ConnectAsync(new IPEndPoint(address, endPoint.Port), cancellation).ConfigureAwait(false); + return new NetworkStream(socket, ownsSocket: true); + } + catch (SocketException error) + { + socket.Dispose(); + last = error; + } + catch + { + socket.Dispose(); + throw; + } + } + throw last!; + } + + public bool Handles(string operation) => operation == "http.request"; + + public void Start(ExecutionRequest request, string operation, JsonObject? payload, NativeCompletion done) + { + var job = payload ?? new JsonObject(); + void Fail(string message) => done(false, Json.Quote(message)); + if (RuntimePolicy.IsLiveActivity(request)) { Fail("[live_activity_unavailable] Live Activity presentations cannot request network data; update their state from the app"); return; } + if (RuntimePolicy.IsTimelineProjection(request)) { Fail("[timeline_read_only] Future entries cannot make live network requests; read a saved snapshot"); return; } + var url = job.OptString("url"); + if (!Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out var uri)) { Fail($"{url} is not url"); return; } + if (ValidationError(uri) is { } invalid) { Fail(invalid); return; } + if (!RuntimePolicy.PackageAllowsHost(request, uri.Host)) { Fail("Network host is not declared in widget.json"); return; } + if (request.Session.AddNetworkRequest() > MaximumRequests) { Fail("Network request budget exceeded"); return; } + + pool.Execute(() => + { + try + { + var headers = new Dictionary(StringComparer.Ordinal); + if (job.OptObject("headers") is { } map) foreach (var (name, _) in map) headers[name] = map.OptString(name); + var body = job.Has("body") ? Encoding.UTF8.GetBytes(job.OptString("body")) : null; + if (body is not null && job.OptBoolean("jsonBody") && !headers.Keys.Any(name => name.Equals("Content-Type", StringComparison.OrdinalIgnoreCase))) headers["Content-Type"] = "application/json"; + if (body is not null && body.Length > MaximumRequestBodyBytes) throw new FetchException($"Request body exceeds the {MaximumRequestBodyBytes}-byte limit"); + var timeout = NormalizedTimeout(job.Has("timeout") ? job.OptDouble("timeout") : null); + var response = Perform(job.OptString("method", "GET"), uri, headers, body, timeout, + redirected => !request.Session.Finished && ValidationError(redirected) is null && RuntimePolicy.PackageAllowsHost(request, redirected.Host), + request.Session.Cancellation.Token); + if (request.Session.Finished) return; + var base64 = job.OptBoolean("base64"); + var text = base64 ? Convert.ToBase64String(response.Body) : DecodeUtf8(response.Body); + if (job.OptBoolean("metadata")) + { + if (text is null) throw new FetchException("Response is not HTTP or has invalid UTF-8"); + var allowed = new JsonObject(); + foreach (var name in ExposedHeaders) + { + if (response.Headers.TryGetValue(name, out var value) && Encoding.UTF8.GetByteCount(value) <= 2048) allowed[name] = value; + } + done(true, Json.Stringify(new JsonObject { ["status"] = response.Status, ["body"] = text, ["headers"] = allowed })); + } + else if (text is null) throw new FetchException($"{url} is empty"); + else done(true, Json.Quote(text)); + } + catch (FetchException error) + { + Fail(error.Message); + } + catch (Exception error) + { + Fail(string.IsNullOrEmpty(error.Message) ? error.GetType().Name : error.Message); + } + }); + } + + public string? ValidationError(Uri uri) + { + var scheme = uri.IsAbsoluteUri ? uri.Scheme.ToLowerInvariant() : null; + if (scheme != "http" && scheme != "https") return "Only HTTP and HTTPS URLs are allowed"; + var host = Unbracket(uri.Host.ToLowerInvariant()); + if (host.Length == 0) return "URL host is required"; + if (uri.UserInfo.Length > 0) return "URL credentials are not allowed"; + return RuntimePolicy.BlockedHostName(host) ? BlockedMessage : null; + } + + /// + /// Performs one request, following up to five redirects that accepts. + /// Blocks the calling thread; call it from a worker. + /// + public Response Perform(string method, Uri start, IReadOnlyDictionary headers, byte[]? body, int timeoutMilliseconds, Func allowRedirect, CancellationToken cancellation = default) => + PerformAsync(method, start, headers, body, timeoutMilliseconds, allowRedirect, cancellation).GetAwaiter().GetResult(); + + /// + /// Performs one request, following up to five redirects that accepts. + /// Every hop is resolved and its addresses checked before anything is sent. The response body is + /// counted while it is read, never trusting Content-Length, and reading stops at the limit. + /// + public async Task PerformAsync(string method, Uri start, IReadOnlyDictionary headers, byte[]? body, int timeoutMilliseconds, Func allowRedirect, CancellationToken cancellation = default) + { + var current = start; + var currentMethod = method.ToUpperInvariant(); + var currentBody = body; + if (!Methods.Contains(currentMethod)) throw new FetchException($"Unknown method '{currentMethod}'"); + var deadline = Stopwatch.GetTimestamp() + MaximumTotalMilliseconds * Stopwatch.Frequency / 1000; + for (var hop = 0; hop <= MaximumRedirects; hop++) + { + cancellation.ThrowIfCancellationRequested(); + var host = Unbracket(current.Host); + IReadOnlyList addresses; + try { addresses = resolver(host); } + catch (Exception) { throw new FetchException($"Could not resolve {current.Host}"); } + if (addresses.Count == 0 || addresses.Any(RuntimePolicy.BlockedAddress)) throw new FetchException(BlockedMessage); + using var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellation); + // Each wait (connect and response headers, then every read) gets the request timeout, inside the total deadline. + void Arm() + { + var remaining = (int)Math.Min(int.MaxValue, (deadline - Stopwatch.GetTimestamp()) * 1000 / Stopwatch.Frequency); + if (remaining <= 0) throw new FetchException(TimedOutMessage); + timeout.CancelAfter(Math.Min(timeoutMilliseconds, remaining)); + } + Arm(); + using var message = new HttpRequestMessage(new HttpMethod(currentMethod), current); + HttpContent? content = currentBody is not null && currentMethod != "GET" ? new ByteArrayContent(currentBody) : null; + var unique = new Dictionary(StringComparer.OrdinalIgnoreCase); + // A redirect to another origin does not get the credentials meant for the first one, as in + // browsers and curl. (The Android transport this is ported from sends them on.) + var otherOrigin = !SameOrigin(start, current); + foreach (var (name, value) in headers) + { + if (otherOrigin && CredentialHeaders.Contains(name)) continue; + unique[name] = value; + } + foreach (var (name, value) in unique) + { + if (name.Equals("Host", StringComparison.OrdinalIgnoreCase) || name.Equals("Content-Length", StringComparison.OrdinalIgnoreCase)) continue; + // A value can never start a new header line, whatever the transport would do with it. + if (name.Length == 0 || name.Any(character => character <= ' ' || character >= '\u007F' || character == ':') || value.Any(character => character is '\r' or '\n' or '\0')) + { + throw new FetchException($"Request header {Printable(name)} is not valid"); + } + bool added; + if (ContentHeaders.Contains(name)) + { + content ??= new ByteArrayContent([]); + added = content.Headers.TryAddWithoutValidation(name, value); + } + else added = message.Headers.TryAddWithoutValidation(name, value); + // The name is the script's own; the value is not repeated. + if (!added) throw new FetchException($"Request header {Printable(name)} is not valid"); + } + // The system network stacks on iPhone and Android name the client by themselves; HttpClient + // sends nothing, and services such as the GitHub API refuse a request without a user agent. + // A script's own User-Agent header wins. No user or device detail is included. + if (!unique.ContainsKey("User-Agent")) message.Headers.TryAddWithoutValidation("User-Agent", DefaultUserAgent); + message.Content = content; + try + { + HttpResponseMessage response; + try { response = await client.SendAsync(message, timeout.Token).ConfigureAwait(false); } + catch (HttpRequestException error) when (Refusal(error) is { } refusal) { throw refusal; } + using (response) + { + var status = (int)response.StatusCode; + if (status is >= 300 and <= 399 && status != 304) + { + var location = Last(response.Headers.NonValidated, "Location") ?? throw new FetchException("Redirect without a location"); + if (!Uri.TryCreate(current, location, out var next) || hop == MaximumRedirects || !allowRedirect(next)) throw new FetchException("Redirect target is not allowed"); + if (status == 303 || ((status == 301 || status == 302) && currentMethod == "POST")) { currentMethod = "GET"; currentBody = null; } + current = next; + continue; + } + var tooLarge = $"Response exceeds the {MaximumResponseBytes}-byte limit"; + if (response.Content.Headers.ContentLength is > MaximumResponseBytes) throw new FetchException(tooLarge); + using var buffer = new MemoryStream(); + await using (var stream = await response.Content.ReadAsStreamAsync(timeout.Token).ConfigureAwait(false)) + { + var chunk = new byte[16 * 1024]; + while (true) + { + Arm(); + var read = await stream.ReadAsync(chunk, timeout.Token).ConfigureAwait(false); + if (read <= 0) break; + if (buffer.Length + read > MaximumResponseBytes) throw new FetchException(tooLarge); + buffer.Write(chunk, 0, read); + } + } + var responseHeaders = new Dictionary(StringComparer.Ordinal); + foreach (var (name, values) in response.Headers.NonValidated) if (values.Count > 0) responseHeaders[name.ToLowerInvariant()] = values.Last(); + foreach (var (name, values) in response.Content.Headers.NonValidated) if (values.Count > 0) responseHeaders[name.ToLowerInvariant()] = values.Last(); + return new Response(status, buffer.ToArray(), responseHeaders); + } + } + catch (OperationCanceledException) when (!cancellation.IsCancellationRequested) + { + throw new FetchException(TimedOutMessage); + } + } + throw new FetchException("Too many redirects"); + } + + /// Seconds from the script, clamped to 1...10; 5 by default. + public static int NormalizedTimeout(double? seconds) + { + var value = seconds is not { } given || double.IsNaN(given) || given <= 0 ? 5.0 : Math.Clamp(given, 1.0, 10.0); + return (int)(value * 1000); + } + + /// The policy refusal of the connect step, when that is why sending failed. + private static FetchException? Refusal(Exception error) + { + for (var inner = error.InnerException; inner is not null; inner = inner.InnerException) if (inner is FetchException refusal) return refusal; + return null; + } + + private static string? Last(System.Net.Http.Headers.HttpHeadersNonValidated headers, string name) => + headers.TryGetValues(name, out var values) && values.Count > 0 ? values.Last() : null; + + private static string Printable(string name) => new(name.Where(character => character > ' ' && character < '\u007F').Take(64).ToArray()); + + private static string Unbracket(string host) => host.StartsWith('[') && host.EndsWith(']') ? host[1..^1] : host; + + private static string? DecodeUtf8(byte[] bytes) + { + try { return new UTF8Encoding(false, true).GetString(bytes); } + catch (DecoderFallbackException) { return null; } + } +} + +/// +/// Runs work on thread-pool threads, at most a fixed number at a time; the rest waits in order. +/// The counterpart of the fixed thread pools of the Android implementation. +/// +internal sealed class BoundedExecutor(int maximumConcurrent) +{ + private readonly Queue queue = new(); + private int running; + + public void Execute(Action work) + { + lock (queue) + { + if (running >= maximumConcurrent) { queue.Enqueue(work); return; } + running += 1; + } + ThreadPool.UnsafeQueueUserWorkItem(static state => state.self.Drain(state.work), (self: this, work), preferLocal: false); + } + + private void Drain(Action? work) + { + while (work is not null) + { + // Work reports its own failures; one that still throws must not end the worker. + try { work(); } catch (Exception) { } + lock (queue) + { + if (!queue.TryDequeue(out work)) { running -= 1; work = null; } + } + } + } +} diff --git a/Windows/src/ScriptWidget.Core/Api/RuntimePolicy.cs b/Windows/src/ScriptWidget.Core/Api/RuntimePolicy.cs new file mode 100644 index 00000000..2bd3ec46 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Api/RuntimePolicy.cs @@ -0,0 +1,85 @@ +using System.Net; +using System.Net.Sockets; +using System.Text.Json.Nodes; +using ScriptWidget.Core.Runtime; + +namespace ScriptWidget.Core.Api; + +/// +/// Manifest and presentation rules that every native API checks before doing +/// work. JavaScript-side checks exist only for early, readable errors. +/// +public static class RuntimePolicy +{ + public static JsonObject? Manifest(ExecutionRequest request) => request.Prepared.Manifest; + + public static bool HasPermission(ExecutionRequest request, string permission) => + Manifest(request).OptArray("permissions")?.Any(item => item?.AsText() == permission) ?? false; + + /// Future timeline entries are rendered ahead of time and must not read live data or write. + public static bool IsTimelineProjection(ExecutionRequest request) => + (request.Environments.TryGetValue("timeline-is-projection", out var value) ? value : request.Prepared.TimelineEnvironment.GetValueOrDefault("timeline-is-projection")) == "true"; + + public static bool IsLiveActivity(ExecutionRequest request) => request.Environments.GetValueOrDefault("widget-size") is "live-activity" or "dynamic-island"; + + /// True while a declared action runs; writes to system data require it. + public static bool IsAction(ExecutionRequest request) => request.Prepared.ActionID is not null; + + /// Same textual rule as ScriptWidgetFetchPolicy.validationError. + public static bool BlockedHostName(string host) + { + var value = host.ToLowerInvariant(); + var parts = value.Split('.'); + return value == "localhost" || value == "::1" || value.EndsWith(".local", StringComparison.Ordinal) || + value.StartsWith("127.", StringComparison.Ordinal) || value.StartsWith("10.", StringComparison.Ordinal) || value.StartsWith("192.168.", StringComparison.Ordinal) || + (parts.Length == 4 && parts[0] == "172" && int.TryParse(parts[1], out var second) && second is >= 16 and <= 31); + } + + /// Rejects addresses that are not on the public Internet, whatever name resolved to them. + public static bool BlockedAddress(IPAddress address) + { + if (address.IsIPv4MappedToIPv6) address = address.MapToIPv4(); + if (IPAddress.IsLoopback(address) || address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any) || address.Equals(IPAddress.IPv6None)) return true; + var bytes = address.GetAddressBytes(); + if (address.AddressFamily == AddressFamily.InterNetworkV6) + { + if (address.IsIPv6LinkLocal || address.IsIPv6SiteLocal || address.IsIPv6Multicast) return true; + if ((bytes[0] & 0xFE) == 0xFC) return true; // unique local fc00::/7 + // NAT64 and 6to4 embed an IPv4 address that the gateway would reach. + if (bytes[0] == 0x00 && bytes[1] == 0x64 && bytes[2] == 0xFF && bytes[3] == 0x9B) return BlockedAddress(new IPAddress(bytes.AsSpan(12, 4))); + if (bytes[0] == 0x20 && bytes[1] == 0x02) return BlockedAddress(new IPAddress(bytes.AsSpan(2, 4))); + return false; + } + if (bytes.Length != 4) return true; + int first = bytes[0], second = bytes[1]; + if (first == 0 || first == 10 || first == 127) return true; // this-network, private, loopback + if (first == 100 && second is >= 64 and <= 127) return true; // carrier-grade NAT + if (first == 169 && second == 254) return true; // link-local + if (first == 172 && second is >= 16 and <= 31) return true; // private + if (first == 192 && second == 168) return true; // private + if (first >= 224) return true; // multicast, reserved and broadcast + return false; + } + + public static bool HostMatches(string host, IEnumerable declarations) + { + var value = host.ToLowerInvariant(); + return declarations.Any(declaration => + { + var domain = declaration.ToLowerInvariant(); + if (!domain.StartsWith("*.", StringComparison.Ordinal)) return value == domain; + var suffix = domain[2..]; + return value != suffix && value.EndsWith("." + suffix, StringComparison.Ordinal); + }); + } + + /// Package 2.0: network permission and a matching networkDomains entry. Legacy packages without a manifest are allowed. + public static bool PackageAllowsHost(ExecutionRequest request, string host) + { + if (!request.Prepared.HasManifest) return true; + if (Manifest(request) is not { } manifest) return false; + if (!HasPermission(request, "network")) return false; + if (manifest.OptArray("networkDomains") is not { } domains) return false; + return HostMatches(host, domains.Select(item => item?.AsText() ?? "")); + } +} diff --git a/Windows/src/ScriptWidget.Core/Api/StorageModule.cs b/Windows/src/ScriptWidget.Core/Api/StorageModule.cs new file mode 100644 index 00000000..898501c0 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Api/StorageModule.cs @@ -0,0 +1,154 @@ +using System.Collections.Concurrent; +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; +using ScriptWidget.Core.Runtime; + +namespace ScriptWidget.Core.Api; + +/// +/// Package-scoped key-value storage behind $storage. Each package has +/// its own file; a package can never name or reach another package's keys. +/// Values are bounded and writes are atomic. Compare-and-set runs under the +/// same lock as plain writes, so app and widget updates do not lose records. +/// +public sealed class StorageModule(string directory) : INativeModule +{ + public const int MaximumValueBytes = 256 * 1024; + public const int MaximumKeys = 1024; + public const int MaximumTotalBytes = 8 * 1024 * 1024; + + private static readonly ConcurrentDictionary Locks = new(StringComparer.OrdinalIgnoreCase); + private static object LockFor(string path) => Locks.GetOrAdd(path, _ => new object()); + + public bool Handles(string operation) => operation.StartsWith("storage.", StringComparison.Ordinal); + + public string? Call(ExecutionRequest request, string operation, string? payload) + { + var arguments = payload is null ? new JsonObject() : Json.ParseObject(payload); + var key = arguments.OptString("key"); + var writes = operation is "storage.set" or "storage.cas" or "storage.remove" or "storage.clear"; + if (writes && RuntimePolicy.IsTimelineProjection(request)) return "false"; + var file = FileFor(request.PackageID); + lock (LockFor(file)) + { + switch (operation) + { + case "storage.get": + return !ValidKey(key) ? "null" : Read(file).OptObject(key) is { } entry ? Json.Stringify(entry) : "null"; + case "storage.keys": + return Json.Stringify(Json.Array(Read(file).Select(item => item.Key).Order(StringComparer.Ordinal))); + case "storage.set": + { + var value = arguments.OptString("v"); + var type = arguments.OptString("t"); + if (!ValidKey(key) || !ValidValue(type, value)) return "false"; + var store = Read(file); + if (!store.Has(key) && store.Count >= MaximumKeys) return "false"; + store[key] = new JsonObject { ["t"] = type, ["v"] = value }; + return Write(file, store) ? "true" : "false"; + } + case "storage.cas": + { + var value = arguments.OptString("v"); + var type = arguments.OptString("t"); + var expected = arguments.OptString("expected"); + if (!ValidKey(key) || !ValidValue(type, value) || Encoding.UTF8.GetByteCount(expected) > MaximumValueBytes) return "false"; + var store = Read(file); + var current = store.OptObject(key); + // A missing key matches an empty string or an empty object. + var matches = current is null ? expected == (type == "s" ? "" : "{}") : current.OptString("t") == type && current.OptString("v") == expected; + if (!matches) return "false"; + if (current is null && store.Count >= MaximumKeys) return "false"; + store[key] = new JsonObject { ["t"] = type, ["v"] = value }; + return Write(file, store) ? "true" : "false"; + } + case "storage.remove": + { + if (!ValidKey(key)) return "false"; + var store = Read(file); + store.Remove(key); + return Write(file, store) ? "true" : "false"; + } + case "storage.clear": + try { File.Delete(file); } catch (IOException) { return "false"; } catch (UnauthorizedAccessException) { return "false"; } + return "true"; + default: throw new ArgumentException($"Unsupported operation {operation}"); + } + } + } + + /// Reads a string value for native features such as toggle state; empty when missing. + public string GetString(string packageID, string key) + { + var file = FileFor(packageID); + lock (LockFor(file)) return Read(file).OptObject(key) is { } entry && entry.OptString("t") == "s" ? entry.OptString("v") : ""; + } + + /// Writes a string value on behalf of a native feature. Returns false when a limit rejects it. + public bool SetString(string packageID, string key, string value) + { + if (!ValidKey(key) || !ValidValue("s", value)) return false; + var file = FileFor(packageID); + lock (LockFor(file)) + { + var store = Read(file); + if (!store.Has(key) && store.Count >= MaximumKeys) return false; + store[key] = new JsonObject { ["t"] = "s", ["v"] = value }; + return Write(file, store); + } + } + + /// All stored entries of a package, for the records screen and tests. + public JsonObject Snapshot(string packageID) + { + var file = FileFor(packageID); + lock (LockFor(file)) return Read(file); + } + + public void Clear(string packageID) + { + var file = FileFor(packageID); + lock (LockFor(file)) { try { File.Delete(file); } catch (IOException) { } catch (UnauthorizedAccessException) { } } + } + + private static bool ValidKey(string key) => key.Length > 0 && Encoding.UTF8.GetByteCount(key) <= 256; + + private static bool ValidValue(string type, string value) + { + if (type != "s" && type != "j") return false; + if (Encoding.UTF8.GetByteCount(value) > MaximumValueBytes) return false; + if (type == "s") return true; + try { return Json.Parse(value) is JsonObject; } catch (JsonException) { return false; } + } + + private string FileFor(string packageID) => + Path.Combine(directory, Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(packageID))) + ".json"); + + private static JsonObject Read(string file) + { + if (!File.Exists(file)) return new JsonObject(); + try { return Json.Parse(File.ReadAllText(file, Encoding.UTF8)) as JsonObject ?? new JsonObject(); } + catch (Exception error) when (error is JsonException or IOException or UnauthorizedAccessException) { return new JsonObject(); } + } + + private bool Write(string file, JsonObject store) + { + var text = Json.Stringify(store); + if (Encoding.UTF8.GetByteCount(text) > MaximumTotalBytes) return false; + var temporary = file + ".tmp"; + try + { + Directory.CreateDirectory(directory); + File.WriteAllText(temporary, text, new UTF8Encoding(false)); + File.Move(temporary, file, overwrite: true); + return true; + } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) + { + try { File.Delete(temporary); } catch (IOException) { } catch (UnauthorizedAccessException) { } + return false; + } + } +} diff --git a/Windows/src/ScriptWidget.Core/Api/SystemSupport.cs b/Windows/src/ScriptWidget.Core/Api/SystemSupport.cs new file mode 100644 index 00000000..1051f1cf --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Api/SystemSupport.cs @@ -0,0 +1,371 @@ +using System.Collections.Concurrent; +using System.Globalization; +using System.Security.Cryptography; +using System.Text; +using System.Text.Json.Nodes; +using System.Text.RegularExpressions; +using ScriptWidget.Core.Runtime; + +namespace ScriptWidget.Core.Api; + +// The shared pieces of the Runtime 1.6 system services that $weather needs. The other +// services (grants, mutation receipts, permission prompts) are added with the APIs that use them. + +/// A rejection whose message is delivered to the script verbatim. It never contains user data. +public sealed class SystemReject(string message) : Exception(message); + +/// +/// Error codes shared by the Runtime 1.6 system services. The message is the same sentence the +/// Apple runtime uses, so scripts that match on the bracketed code behave the same on Windows. +/// +public enum SystemServiceError { InvalidOptions, PermissionDenied, Unavailable, QueryLimit, ReadOnly, ScopeChanged, LimitExceeded, OutcomeUnknown, RequestConflict } + +public static class SystemServiceErrors +{ + /// Message for a failure that is not one of the known codes. + public const string Failed = "[system_failed] System operation failed; review the result before retrying a write."; + + public static string Code(this SystemServiceError error) => error switch + { + SystemServiceError.InvalidOptions => "invalid_options", + SystemServiceError.PermissionDenied => "permission_denied", + SystemServiceError.Unavailable => "unavailable", + SystemServiceError.QueryLimit => "query_limit", + SystemServiceError.ReadOnly => "read_only", + SystemServiceError.ScopeChanged => "scope_changed", + SystemServiceError.LimitExceeded => "limit_exceeded", + SystemServiceError.OutcomeUnknown => "outcome_unknown", + _ => "request_conflict", + }; + + public static string Message(this SystemServiceError error) => $"[{error.Code()}] System operation could not complete. Existing user data is preserved."; + + public static SystemReject Reject(this SystemServiceError error) => new(error.Message()); +} + +/// One line per native call. Callers pass operation names and error codes only, never user data. +public static partial class SystemLog +{ + /// Receives each line. The host sets it to its log; tests capture lines. Nothing is written by default. + public static volatile Action Sink = _ => { }; + + public static void Call(string operation, string outcome) => Sink($"{operation} -> {outcome}"); + + [GeneratedRegex(@"\A\[([a-z_:A-Z]{1,40})]", RegexOptions.CultureInvariant)] + private static partial Regex Bracketed(); + + /// The bracketed code of a rejection message, or error when it has none. The free text is never logged. + public static string Code(string message) => Bracketed().Match(message) is { Success: true } match ? match.Groups[1].Value : "error"; +} + +/// Background workers for system queries, so the JavaScript thread never blocks on a provider or disk. +internal static class SystemExecutor +{ + public static readonly BoundedExecutor Pool = new(4); + + /// Runs off the JavaScript thread, logs one line, and completes exactly once with JSON or a rejection message. + public static void Run(string operation, NativeCompletion done, Func block) + { + void Finish(bool ok, string value) + { + SystemLog.Call(operation, ok ? "ok" : SystemLog.Code(value)); + done(ok, ok ? value : Json.Quote(value)); + } + Pool.Execute(() => + { + string result; + try { result = block(); } + catch (SystemReject error) { Finish(false, string.IsNullOrEmpty(error.Message) ? SystemServiceErrors.Failed : error.Message); return; } + catch (Exception) { Finish(false, SystemServiceErrors.Failed); return; } + Finish(true, result); + }); + } +} + +/// +/// The facts about one execution that policy decisions need. Built from the +/// in production and directly in tests, so every rule can +/// be exercised without the JavaScript engine. +/// +public sealed partial class CallContext( + string installation, + bool hasManifest, + IReadOnlySet permissions, + IReadOnlyList networkDomains, + string? minimumRuntimeVersion, + bool isAction, + bool isTimelineProjection, + bool isLiveActivity, + Func? dataQuery = null, + Func? networkRequest = null, + Func? active = null, + CancellationToken cancellation = default) +{ + public const int MaximumDataQueries = 8; + public const int MaximumNetworkRequests = 32; + + /// Installation identity; namespaces grants, receipts and caches. + public string Installation { get; } = installation; + public bool HasManifest { get; } = hasManifest; + public IReadOnlySet Permissions { get; } = permissions; + public IReadOnlyList NetworkDomains { get; } = networkDomains; + public string? MinimumRuntimeVersion { get; } = minimumRuntimeVersion; + public bool IsAction { get; } = isAction; + public bool IsTimelineProjection { get; } = isTimelineProjection; + public bool IsLiveActivity { get; } = isLiveActivity; + /// Cancelled when the execution ends; in-flight work should stop. + public CancellationToken Cancellation { get; } = cancellation; + + /// An installed owner is a canonical lowercase UUID; previews and unmanaged packages are not owners. + public bool IsInstalled => IsOwner(Installation); + + /// True while the execution is still running; results for a finished execution are dropped. + public bool IsActive => active?.Invoke() ?? true; + + /// Takes one of the eight system-data queries of this execution. False when the budget is spent. + public bool ConsumeDataQuery() => dataQuery?.Invoke() ?? true; + + /// Takes one of the 32 network requests of this execution. + public bool ConsumeNetworkRequest() => networkRequest?.Invoke() ?? true; + + /// Package 2.0 permission check: a missing manifest never declares anything. + public bool Declares(string permission) => HasManifest && Permissions.Contains(permission); + + /// network plus a matching networkDomains entry, the rule fetch applies. + public bool AllowsHost(string host) => Declares("network") && RuntimePolicy.HostMatches(host, NetworkDomains); + + [GeneratedRegex(@"\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z", RegexOptions.CultureInvariant)] + private static partial Regex Owner(); + + public static bool IsOwner(string value) => Owner().IsMatch(value); + + public static CallContext From(ExecutionRequest request) + { + var manifest = request.Prepared.Manifest; + List Strings(string key) => manifest.OptArray(key)?.Select(item => item?.AsText() ?? "").ToList() ?? []; + var runtime = manifest.OptString("minimumRuntimeVersion"); + return new CallContext( + installation: request.PackageID, + // A present manifest that could not be read still counts as present: it declares nothing and is never treated as legacy. + hasManifest: request.Prepared.HasManifest, + permissions: Strings("permissions").ToHashSet(StringComparer.Ordinal), + networkDomains: Strings("networkDomains"), + minimumRuntimeVersion: runtime.Length > 0 ? runtime : null, + isAction: RuntimePolicy.IsAction(request), + isTimelineProjection: RuntimePolicy.IsTimelineProjection(request), + isLiveActivity: RuntimePolicy.IsLiveActivity(request), + dataQuery: () => request.Session.AddDataQuery() <= MaximumDataQueries, + networkRequest: () => request.Session.AddNetworkRequest() <= MaximumNetworkRequests, + active: () => !request.Session.Finished, + cancellation: request.Session.Cancellation.Token); + } +} + +/// ISO-8601 instants in the forms the Apple runtime accepts and produces. +public static partial class IsoDates +{ + [GeneratedRegex(@"\A([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(?:\.([0-9]{1,9}))?(?:Z|([+-])([0-9]{2}):([0-9]{2}))\z", RegexOptions.CultureInvariant)] + private static partial Regex Pattern(); + + /// Epoch milliseconds of a full date-time with a time zone, or null. Dates without a zone or with impossible fields are rejected. + public static long? Parse(string text) + { + if (Encoding.UTF8.GetByteCount(text) > 64 || Pattern().Match(text) is not { Success: true } match) return null; + int Field(int group) => int.Parse(match.Groups[group].Value, NumberStyles.None, CultureInfo.InvariantCulture); + int year = Field(1), month = Field(2), day = Field(3), hour = Field(4), minute = Field(5), second = Field(6); + if (year < 1 || month is < 1 or > 12 || day < 1 || day > DateTime.DaysInMonth(year, month) || hour > 23 || minute > 59 || second > 59) return null; + long offsetMinutes = 0; + if (match.Groups[8].Success) + { + int offsetHours = Field(9), offsetRest = Field(10); + if (offsetHours > 18 || offsetRest > 59 || (offsetHours == 18 && offsetRest != 0)) return null; + offsetMinutes = (match.Groups[8].Value == "-" ? -1 : 1) * (offsetHours * 60L + offsetRest); + } + var fraction = match.Groups[7].Success ? int.Parse(match.Groups[7].Value.PadRight(3, '0')[..3], NumberStyles.None, CultureInfo.InvariantCulture) : 0; + var local = new DateTime(year, month, day, hour, minute, second, DateTimeKind.Utc); + return (local.Ticks - DateTime.UnixEpoch.Ticks) / TimeSpan.TicksPerMillisecond - offsetMinutes * 60_000L + fraction; + } + + /// 2026-11-01T12:00:00Z: whole seconds, UTC. + public static string Format(long milliseconds) + { + var seconds = milliseconds >= 0 ? milliseconds / 1000 : -((-milliseconds + 999) / 1000); + return DateTime.UnixEpoch.AddSeconds(seconds).ToString("yyyy-MM-dd'T'HH:mm:ss'Z'", CultureInfo.InvariantCulture); + } +} + +/// Strict, bounded option parsing shared by the system services. Every failure is invalid_options. +public static class SystemOptions +{ + public const int MaximumOptionBytes = 16384; + + private static SystemReject Invalid() => SystemServiceError.InvalidOptions.Reject(); + + /// Rejects unknown fields and option objects above 16 KiB. + public static void Fields(JsonObject options, IReadOnlySet allowed) + { + if (options.Any(member => !allowed.Contains(member.Key)) || Encoding.UTF8.GetByteCount(Json.Stringify(options)) > MaximumOptionBytes) throw Invalid(); + } + + public static bool HasControlCharacters(string text) + { + for (var index = 0; index < text.Length; index++) + { + // An unpaired surrogate is neither a control nor a format character. + if (!Rune.TryGetRuneAt(text, index, out var rune)) continue; + if (Rune.GetUnicodeCategory(rune) is UnicodeCategory.Control or UnicodeCategory.Format) return true; + index += rune.Utf16SequenceLength - 1; + } + return false; + } + + /// A finite JSON number inside the range; Booleans and strings are not numbers. + public static double Number(JsonNode? value, double minimum, double maximum) + { + if (JsonNumber(value) is not { } number) throw Invalid(); + if (!double.IsFinite(number) || number < minimum || number > maximum) throw Invalid(); + return number; + } + + /// The value of a JSON number, however the node was made; null for everything else. + public static double? JsonNumber(JsonNode? value) + { + if (value is null || !value.IsNumber()) return null; + if (value.AsDouble() is { } number) return number; + return double.TryParse(value.ToJsonString(), NumberStyles.Float, CultureInfo.InvariantCulture, out var parsed) ? parsed : null; + } + + public static int Integer(JsonNode? value, int minimum, int maximum) + { + var number = Number(value, minimum, maximum); + if (Math.Round(number, MidpointRounding.ToEven) != number) throw Invalid(); + return (int)number; + } +} + +/// Digests for cache identities. +public static class Canonical +{ + public static string Sha256(string text) => Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(text))); + + /// File names for per-installation records: a digest, so an identity can never become a path. + public static string OwnerFileName(string installation, string suffix) => Sha256(installation)[..40] + suffix; +} + +/// +/// A small private JSON file. Reads are bounded and refuse links and +/// non-regular files; writes replace the file atomically. A present file that +/// cannot be read is reported as so callers fail closed instead +/// of treating it as missing. +/// +public sealed class PrivateFile(string path, int maximumBytes) +{ + public sealed class Unreadable() : Exception("private file is unreadable"); + + private static readonly ConcurrentDictionary Locks = new(StringComparer.OrdinalIgnoreCase); + + public string Path { get; } = path; + + /// Lock shared by every reader and writer of this path in the process. + public object Lock => Locks.GetOrAdd(System.IO.Path.GetFullPath(Path), _ => new object()); + + public bool Exists() => Attributes() is not null; + + /// File text, or null when the file does not exist. + public string? Read() + { + if (Attributes() is not { } attributes) return null; + if (attributes.HasFlag(FileAttributes.Directory) || attributes.HasFlag(FileAttributes.ReparsePoint)) throw new Unreadable(); + try + { + using var stream = new FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.Read); + if (stream.Length > maximumBytes) throw new Unreadable(); + var bytes = new byte[maximumBytes + 1]; + var total = 0; + while (total < bytes.Length) + { + var count = stream.Read(bytes, total, bytes.Length - total); + if (count <= 0) break; + total += count; + } + if (total > maximumBytes) throw new Unreadable(); + return Encoding.UTF8.GetString(bytes, 0, total); + } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) + { + throw new Unreadable(); + } + } + + /// Writes the text through a temporary file and an atomic rename. False when it exceeds the bound or the disk refuses it. + public bool Write(string text) + { + var bytes = Encoding.UTF8.GetBytes(text); + if (bytes.Length > maximumBytes) return false; + var temporary = Path + ".tmp"; + try + { + if (System.IO.Path.GetDirectoryName(Path) is { Length: > 0 } parent) Directory.CreateDirectory(parent); + File.WriteAllBytes(temporary, bytes); + File.Move(temporary, Path, overwrite: true); + return true; + } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) + { + try { File.Delete(temporary); } catch (Exception inner) when (inner is IOException or UnauthorizedAccessException) { } + return false; + } + } + + public void Delete() + { + try { File.Delete(Path); } catch (Exception error) when (error is IOException or UnauthorizedAccessException) { } + } + + /// Attributes of the entry itself (never of a link target), or null when nothing is there. + private FileAttributes? Attributes() + { + try { return File.GetAttributes(Path); } + catch (Exception error) when (error is FileNotFoundException or DirectoryNotFoundException) { return null; } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) { throw new Unreadable(); } + } +} + +/// +/// The shared gate of the Runtime 1.6 system services. The order is the Apple +/// runtime's and decides which code a script sees: +/// 1. permission_denied: not an installed owner, no manifest, the permission +/// is not declared, or minimumRuntimeVersion is below 1.6; +/// 2. read_only: a future timeline entry, a Live Activity presentation, or +/// a write outside a user-initiated action; +/// 3. query_limit: the eight-query budget of the execution is spent. +/// Services with a per-widget native grant check it between steps 2 and 3; $weather has none. +/// +public static partial class SystemGate +{ + public static void Check(CallContext context, string permission, bool writing) + { + Validate(context, permission, writing); + if (!context.ConsumeDataQuery()) throw SystemServiceError.QueryLimit.Reject(); + } + + /// Steps 1 and 2 only; repeated before a result is delivered so a changed state stops an in-flight call. + public static void Validate(CallContext context, string permission, bool writing) + { + if (!context.IsInstalled || !context.Declares(permission) || !SupportsRuntime("1.6", context.MinimumRuntimeVersion)) throw SystemServiceError.PermissionDenied.Reject(); + if (context.IsTimelineProjection || context.IsLiveActivity || (writing && !context.IsAction)) throw SystemServiceError.ReadOnly.Reject(); + } + + [GeneratedRegex(@"\A([0-9]{1,4})\.([0-9]{1,4})\z", RegexOptions.CultureInvariant)] + private static partial Regex Version(); + + /// True when (major.minor) is at least . A missing or malformed version is 0.0. + public static bool SupportsRuntime(string required, string? declared) + { + static (int Major, int Minor)? Parts(string? text) => + Version().Match(text ?? "") is { Success: true } match ? (int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture), int.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture)) : null; + if (Parts(required) is not { } need) return false; + var have = Parts(declared) ?? (0, 0); + return have.Major > need.Major || (have.Major == need.Major && have.Minor >= need.Minor); + } +} diff --git a/Windows/src/ScriptWidget.Core/Api/WeatherModule.cs b/Windows/src/ScriptWidget.Core/Api/WeatherModule.cs new file mode 100644 index 00000000..76111ef2 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Api/WeatherModule.cs @@ -0,0 +1,285 @@ +using System.Globalization; +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; +using ScriptWidget.Core.Runtime; + +namespace ScriptWidget.Core.Api; + +/// +/// $weather.forecast(options) input. +/// +/// Rules: closed fields (latitude, longitude, hours); latitude −90…90 and longitude −180…180, +/// rounded to two decimals before anything leaves the device; hours an integer 1…48, 24 when absent. +/// +public sealed class WeatherOptions(double latitude, double longitude, int hours) +{ + public const string Host = "api.met.no"; + private static readonly IReadOnlySet AllowedFields = new HashSet { "latitude", "longitude", "hours" }; + + public double Latitude { get; } = latitude; + public double Longitude { get; } = longitude; + public int Hours { get; } = hours; + + /// The MET Norway compact forecast for the rounded coordinates. + public string Url => $"https://{Host}/weatherapi/locationforecast/2.0/compact?lat={Decimal(Latitude)}&lon={Decimal(Longitude)}"; + + /// Throws invalid_options for any violation of the rules above. + public static WeatherOptions Parse(JsonObject options) + { + SystemOptions.Fields(options, AllowedFields); + var latitude = Round2(SystemOptions.Number(options["latitude"], -90.0, 90.0)); + var longitude = Round2(SystemOptions.Number(options["longitude"], -180.0, 180.0)); + var hours = options.Has("hours") ? SystemOptions.Integer(options["hours"], 1, 48) : 24; + return new WeatherOptions(latitude, longitude, hours); + } + + /// Two decimals, halves away from zero, as rounded() does in the Apple runtime. + public static double Round2(double value) + { + var scaled = value * 100; + return (scaled < 0 ? -Math.Floor(-scaled + 0.5) : Math.Floor(scaled + 0.5)) / 100; + } + + /// Shortest decimal text with at least one decimal place (60.0), the form every platform puts in the URL. + private static string Decimal(double value) + { + var text = value.ToString("R", CultureInfo.InvariantCulture); + return text.Contains('.') || text.Contains('E') ? text : text + ".0"; + } +} + +/// Pure rules of $weather: provider normalisation and the private cache. +public static class WeatherRules +{ + public const int MaximumResponseBytes = 2 * 1024 * 1024; + public const int MaximumSeries = 300; + public const int MaximumCacheBytes = 128 * 1024; + /// A matching cache younger than this is returned without a request. + public const long FreshMilliseconds = 3_600_000L; + /// A matching cache up to this age may stand in for a failed request, marked isStale. + public const long UsableMilliseconds = 6 * 3_600_000L; + public const string Provider = "MET Norway"; + public const string AttributionText = "Weather data from MET Norway"; + public const string AttributionUrl = "https://www.met.no/en/free-meteorological-data/Download-services"; + + /// + /// Turns a MET Norway compact forecast into the documented result. + /// + /// - The body is at most 2 MiB and properties.timeseries is an array of at most 300 objects, else unavailable. + /// - Entries older than one hour before , or without a time and instant details, are skipped. + /// - air_temperature must be −150…100 and wind_speed 0…200; a present precipitation_amount + /// must be 0…1000. A violation rejects the whole payload (invalid_options, the Apple runtime's code). + /// - Missing precipitation and condition stay null; a condition is cut to 64 characters. + /// - At most options.Hours rows; no usable row is unavailable. + /// + public static JsonObject Normalize(byte[] body, WeatherOptions options, long nowMilliseconds) + { + if (body.Length > MaximumResponseBytes) throw SystemServiceError.Unavailable.Reject(); + try + { + JsonArray? series; + try { series = (Json.Parse(Encoding.UTF8.GetString(body)) as JsonObject).OptObject("properties").OptArray("timeseries"); } + catch (JsonException) { series = null; } + if (series is null || series.Count > MaximumSeries || series.Any(item => item is not JsonObject)) throw SystemServiceError.Unavailable.Reject(); + var rows = new JsonArray(); + foreach (var item in series.OfType()) + { + if (Text(item["time"]) is not { } time) continue; + var date = string.IsNullOrWhiteSpace(time) || SystemOptions.HasControlCharacters(time) ? null : IsoDates.Parse(time); + if (date is not { } instant || instant < nowMilliseconds - 3_600_000L) continue; + if (item.OptObject("data") is not { } values || values.OptObject("instant").OptObject("details") is not { } details) continue; + var temperature = SystemOptions.Number(details["air_temperature"], -150.0, 100.0); + var wind = SystemOptions.Number(details["wind_speed"], 0.0, 200.0); + var next = values.OptObject("next_1_hours"); + var nextDetails = next.OptObject("details"); + double? precipitation = nextDetails is not null && nextDetails.Has("precipitation_amount") ? SystemOptions.Number(nextDetails["precipitation_amount"], 0.0, 1000.0) : null; + var symbol = Text(next.OptObject("summary")?["symbol_code"]); + rows.Append(new JsonObject + { + ["time"] = time, + ["temperatureC"] = temperature, + ["windMetersPerSecond"] = wind, + ["precipitationMM"] = precipitation is { } amount ? JsonValue.Create(amount) : null, + ["condition"] = symbol is null ? null : JsonValue.Create(FirstCodePoints(symbol, 64)), + }); + if (rows.Count == options.Hours) break; + } + if (rows.Count == 0) throw SystemServiceError.Unavailable.Reject(); + return new JsonObject + { + ["provider"] = Provider, + ["attribution"] = new JsonObject { ["text"] = AttributionText, ["url"] = AttributionUrl }, + ["latitude"] = options.Latitude, + ["longitude"] = options.Longitude, + ["fetchedAt"] = IsoDates.Format(nowMilliseconds), + ["isStale"] = false, + ["hours"] = rows, + }; + } + catch (ArgumentException) + { + // A repeated member name: the document does not say one thing, so it says nothing. + throw SystemServiceError.Unavailable.Reject(); + } + } + + /// + /// A cached forecast that may answer , or null. + /// + /// It matches only when the rounded coordinates are equal, fetchedAt is not in the future and at + /// most six hours old, and it holds at least the requested number of hours. The result carries the + /// first options.Hours rows and isStale (true from one hour of age). + /// + public static JsonObject? FromCache(string? text, WeatherOptions options, long nowMilliseconds) + { + if (text is null || Encoding.UTF8.GetByteCount(text) > MaximumCacheBytes) return null; + try + { + if (Json.Parse(text) is not JsonObject cached) return null; + if (Coordinate(cached["latitude"]) != options.Latitude || Coordinate(cached["longitude"]) != options.Longitude) return null; + if (Text(cached["fetchedAt"]) is not { } stamp || IsoDates.Parse(stamp) is not { } fetched) return null; + var age = nowMilliseconds - fetched; + if (cached.OptArray("hours") is not { } hours) return null; + if (age < 0 || age > UsableMilliseconds || hours.Count < options.Hours) return null; + var rows = new JsonArray(); + for (var index = 0; index < options.Hours; index++) rows.Append(hours[index].Copy()); + cached["hours"] = rows; + cached["isStale"] = age >= FreshMilliseconds; + return cached; + } + catch (Exception error) when (error is JsonException or ArgumentException) + { + return null; + } + } + + private static double? Coordinate(JsonNode? node) => SystemOptions.JsonNumber(node); + + private static string? Text(JsonNode? node) => node.IsString() ? node!.GetValue() : null; + + private static string FirstCodePoints(string text, int count) + { + var index = 0; + for (var taken = 0; taken < count && index < text.Length; taken++) + { + index += char.IsHighSurrogate(text[index]) && index + 1 < text.Length && char.IsLowSurrogate(text[index + 1]) ? 2 : 1; + } + return text[..index]; + } +} + +/// A provider response: the HTTP status and the bounded body. +public sealed class WeatherResponse(int status, byte[] body) +{ + public int Status { get; } = status; + public byte[] Body { get; } = body; +} + +/// The network side of $weather, replaceable in tests. Implementations throw on any transport failure. +public interface IWeatherTransport +{ + /// One GET. decides for every redirect target; a refused redirect is a failure. + WeatherResponse Get(string url, IReadOnlyDictionary headers, int timeoutMilliseconds, Func allowRedirect, CancellationToken cancellation); +} + +/// over the bounded fetch transport: public hosts only, resolved addresses checked, at most 2 MiB. +public sealed class FetchWeatherTransport(FetchModule? fetch = null) : IWeatherTransport +{ + private readonly FetchModule fetch = fetch ?? new FetchModule(); + + public WeatherResponse Get(string url, IReadOnlyDictionary headers, int timeoutMilliseconds, Func allowRedirect, CancellationToken cancellation) + { + var uri = new Uri(url, UriKind.Absolute); + if (fetch.ValidationError(uri) is not null) throw new FetchModule.FetchException("blocked"); + var response = fetch.Perform("GET", uri, headers, null, timeoutMilliseconds, target => fetch.ValidationError(target) is null && allowRedirect(target), cancellation); + return new WeatherResponse(response.Status, response.Body); + } +} + +/// +/// $weather.forecast: MET Norway's compact forecast for coordinates the script supplies. The +/// device location is never read here. The order of checks is the Apple runtime's: +/// 1. the system gate (weather declared by an installed Runtime 1.6 package, not a future +/// timeline entry or Live Activity, the eight-query budget); +/// 2. invalid_options for the options; +/// 3. permission_denied unless the package declares network and api.met.no; +/// 4. a matching cache younger than one hour is returned without a request; +/// 5. query_limit when the 32-request network budget of the execution is spent; +/// 6. the request (3 s timeout, redirects only to declared hosts); a failure returns a matching +/// cache up to six hours old with isStale: true, else the failure. +/// +/// There is no per-widget native grant for weather: nothing is read from the device. +/// +public sealed class WeatherModule(IWeatherTransport transport, string directory, string userAgent, Func? clock = null) : INativeModule +{ + public const string Permission = "weather"; + public const int TimeoutMilliseconds = 3_000; + private static readonly IReadOnlySet SupportedApis = new HashSet { "$weather" }; + + private readonly Func clock = clock ?? (() => DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()); + + /// MET Norway asks every client to identify itself; no user or device detail is included. + public static string UserAgent(string versionName) => $"ScriptWidget-Windows/{(versionName.Length == 0 ? "0" : versionName)} https://github.com/everettjf/scriptwidget"; + + public IReadOnlySet Apis => SupportedApis; + + public bool Handles(string operation) => operation.StartsWith("weather.", StringComparison.Ordinal); + + public void Start(ExecutionRequest request, string operation, JsonObject? payload, NativeCompletion done) + { + var context = CallContext.From(request); + if (operation == "weather.forecast") SystemExecutor.Run(operation, done, () => Json.Stringify(Forecast(context, payload.OptObject("options")))); + else done(false, Json.Quote($"Unsupported operation {operation}")); + } + + public JsonObject Forecast(CallContext context, JsonObject? options) + { + SystemGate.Check(context, Permission, writing: false); + var query = WeatherOptions.Parse(options ?? new JsonObject()); + if (!context.AllowsHost(WeatherOptions.Host)) throw SystemServiceError.PermissionDenied.Reject(); + var cache = Cache(context.Installation); + JsonObject? cached; + lock (cache.Lock) + { + string? stored; + try { stored = cache.Read(); } catch (PrivateFile.Unreadable) { stored = null; } + cached = WeatherRules.FromCache(stored, query, clock()); + } + if (cached is not null && !cached.OptBoolean("isStale")) return cached; + if (!context.ConsumeNetworkRequest()) throw SystemServiceError.QueryLimit.Reject(); + try + { + WeatherResponse response; + try + { + response = transport.Get(query.Url, new Dictionary { ["User-Agent"] = userAgent }, TimeoutMilliseconds, + target => context.IsActive && context.AllowsHost(target.Host), context.Cancellation); + } + catch (Exception) { throw SystemServiceError.Unavailable.Reject(); } + SystemGate.Validate(context, Permission, writing: false); + if (!context.AllowsHost(WeatherOptions.Host) || response.Status != 200) throw SystemServiceError.Unavailable.Reject(); + var normalized = WeatherRules.Normalize(response.Body, query, clock()); + var encoded = Json.Stringify(normalized); + if (Encoding.UTF8.GetByteCount(encoded) > WeatherRules.MaximumCacheBytes) throw SystemServiceError.LimitExceeded.Reject(); + bool written; + lock (cache.Lock) written = cache.Write(encoded); + if (!written) throw SystemServiceError.Unavailable.Reject(); + return normalized; + } + catch (SystemReject) when (cached is not null) + { + return cached; + } + } + + /// Removes the forecast cache of a deleted installation. + public void Retire(string installation) + { + if (CallContext.IsOwner(installation)) Cache(installation).Delete(); + SystemLog.Call("weather.retire", "ok"); + } + + /// One forecast per installation, outside every script-reachable path. + private PrivateFile Cache(string installation) => new(Path.Combine(directory, Canonical.OwnerFileName(installation, ".json")), WeatherRules.MaximumCacheBytes); +} diff --git a/Windows/src/ScriptWidget.Core/Hosting/DataPaths.cs b/Windows/src/ScriptWidget.Core/Hosting/DataPaths.cs new file mode 100644 index 00000000..b526370c --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Hosting/DataPaths.cs @@ -0,0 +1,41 @@ +namespace ScriptWidget.Core.Hosting; + +/// +/// Where ScriptWidget keeps its data: %LOCALAPPDATA%\ScriptWidget, or +/// the package's own local folder when the program is installed from the +/// Microsoft Store, which Windows removes on uninstall. +/// +public sealed class DataPaths +{ + public DataPaths(string? root = null) + { + Root = root ?? Environment.GetEnvironmentVariable("SCRIPTWIDGET_DATA") ?? DefaultRoot(); + Directory.CreateDirectory(Root); + } + + private static string DefaultRoot() + { + if (StartupRegistration.IsPackaged) + { + // The real folder, not %LOCALAPPDATA%: Windows would redirect that for a packaged program, + // and the redirection does not support moving a directory, which a transactional + // package install does. + try { return Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "ScriptWidget"); } + catch (Exception error) when (error is not OutOfMemoryException) { } + } + return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "ScriptWidget"); + } + + public string Root { get; } + /// Installed packages, one folder each. + public string Packages => Path.Combine(Root, "Packages"); + /// Package-scoped $storage files. + public string Storage => Path.Combine(Root, "Storage"); + /// Compiled artifacts, remote images and other data that can be rebuilt. + public string Cache => Path.Combine(Root, "Cache"); + public string Plugins => Path.Combine(Root, "Plugins"); + /// The widgets placed on the desktop. + public string Widgets => Path.Combine(Root, "widgets.json"); + public string Settings => Path.Combine(Root, "settings.json"); + public string Log => Path.Combine(Root, "host.log"); +} diff --git a/Windows/src/ScriptWidget.Core/Hosting/HostPipe.cs b/Windows/src/ScriptWidget.Core/Hosting/HostPipe.cs new file mode 100644 index 00000000..a482128f --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Hosting/HostPipe.cs @@ -0,0 +1,104 @@ +using System.IO.Pipes; +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; + +namespace ScriptWidget.Core.Hosting; + +/// +/// The local channel between the always-running desktop host and the +/// programs that drive it (the management window, the command line). One +/// JSON object per line in each direction. The pipe is created for the +/// current user only, so other accounts on the machine cannot connect. +/// Requests name packages and widgets by identifier; nothing sent over the +/// pipe is ever executed as code. +/// +public static class HostPipe +{ + public const string Name = "ScriptWidget.Host.v1"; + private const int MaximumLineBytes = 1024 * 1024; + + /// Serves requests until is cancelled. runs on a thread-pool thread. + public static async Task ServeAsync(Func handle, CancellationToken cancellation) + { + while (!cancellation.IsCancellationRequested) + { + NamedPipeServerStream server; + try + { + server = new NamedPipeServerStream(Name, PipeDirection.InOut, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Byte, PipeOptions.Asynchronous | PipeOptions.CurrentUserOnly); + } + catch (IOException) { await Task.Delay(1000, cancellation).ConfigureAwait(false); continue; } + try { await server.WaitForConnectionAsync(cancellation).ConfigureAwait(false); } + catch (OperationCanceledException) { await server.DisposeAsync().ConfigureAwait(false); return; } + catch (IOException) { await server.DisposeAsync().ConfigureAwait(false); continue; } + _ = Task.Run(() => ServeConnectionAsync(server, handle, cancellation), cancellation); + } + } + + private static async Task ServeConnectionAsync(NamedPipeServerStream server, Func handle, CancellationToken cancellation) + { + await using (server.ConfigureAwait(false)) + { + try + { + while (server.IsConnected && !cancellation.IsCancellationRequested) + { + if (await ReadLineAsync(server, cancellation).ConfigureAwait(false) is not { } line) return; + JsonObject response; + try { response = handle(Json.ParseObject(line)); } + catch (Exception error) when (error is JsonException or InvalidOperationException or ArgumentException) + { + response = new JsonObject { ["ok"] = false, ["error"] = error.Message }; + } + await WriteLineAsync(server, Json.Stringify(response), cancellation).ConfigureAwait(false); + } + } + catch (Exception error) when (error is IOException or OperationCanceledException or ObjectDisposedException) { } + } + } + + /// Sends one request to the running host. Returns null when no host is running. + public static JsonObject? Send(JsonObject request, int connectTimeoutMilliseconds = 1500, int responseTimeoutMilliseconds = 30_000) + { + try + { + using var client = new NamedPipeClientStream(".", Name, PipeDirection.InOut, PipeOptions.Asynchronous | PipeOptions.CurrentUserOnly); + client.Connect(connectTimeoutMilliseconds); + using var timeout = new CancellationTokenSource(responseTimeoutMilliseconds); + WriteLineAsync(client, Json.Stringify(request), timeout.Token).GetAwaiter().GetResult(); + var line = ReadLineAsync(client, timeout.Token).GetAwaiter().GetResult(); + return line is null ? null : Json.TryParse(line) as JsonObject; + } + catch (Exception error) when (error is IOException or TimeoutException or OperationCanceledException or UnauthorizedAccessException) { return null; } + } + + /// True when a host is listening. + public static bool IsHostRunning() => Send(new JsonObject { ["command"] = "ping" }, 300, 3000) is { } response && response.OptBoolean("ok"); + + private static async Task ReadLineAsync(Stream stream, CancellationToken cancellation) + { + var bytes = new MemoryStream(); + var buffer = new byte[4096]; + while (true) + { + var read = await stream.ReadAsync(buffer, cancellation).ConfigureAwait(false); + if (read == 0) return bytes.Length == 0 ? null : Encoding.UTF8.GetString(bytes.GetBuffer(), 0, (int)bytes.Length); + var end = Array.IndexOf(buffer, (byte)'\n', 0, read); + if (end >= 0) + { + bytes.Write(buffer, 0, end); + return Encoding.UTF8.GetString(bytes.GetBuffer(), 0, (int)bytes.Length); + } + bytes.Write(buffer, 0, read); + if (bytes.Length > MaximumLineBytes) throw new IOException("Request is too large"); + } + } + + private static async Task WriteLineAsync(Stream stream, string line, CancellationToken cancellation) + { + var bytes = Encoding.UTF8.GetBytes(line.Replace("\n", "") + "\n"); + await stream.WriteAsync(bytes, cancellation).ConfigureAwait(false); + await stream.FlushAsync(cancellation).ConfigureAwait(false); + } +} diff --git a/Windows/src/ScriptWidget.Core/Hosting/Library.cs b/Windows/src/ScriptWidget.Core/Hosting/Library.cs new file mode 100644 index 00000000..477d0ac0 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Hosting/Library.cs @@ -0,0 +1,276 @@ +using System.Text; +using System.Text.Json.Nodes; +using System.Text.RegularExpressions; +using ScriptWidget.Core.Packages; +using ScriptWidget.Core.Runtime; + +namespace ScriptWidget.Core.Hosting; + +/// One installed package. stays the same when the package is renamed. +public sealed class InstalledPackage(string name, string directory, string installation, JsonObject? manifest, bool manifestPresent) +{ + public string Name { get; } = name; + public string Directory { get; } = directory; + public string Installation { get; } = installation; + public JsonObject? Manifest { get; } = manifest; + /// True when widget.json exists, readable or not. A present manifest that cannot be read fails closed. + public bool ManifestPresent { get; } = manifestPresent; + + public IPackageFiles Files => new DiskPackageFiles(Directory); + public string BuildDirectory => Path.Combine(Directory, "__Build"); + public string DisplayName => Manifest.OptString("name") is { Length: > 0 } title ? title : Name; + public string Description => Manifest.OptString("description"); + public IReadOnlyList Permissions => Manifest.OptArray("permissions")?.Select(item => item?.AsText() ?? "").ToList() ?? []; + /// Widget sizes the manifest lists that Windows can show; every size when it lists none. + public IReadOnlyList Families + { + get + { + // The manifest uses the WidgetKit names ("systemMedium"); $getenv("widget-size") uses the short ones. + var declared = Manifest.OptArray("supportedFamilies")?.Select(item => WidgetFamilies.FromManifest(item?.AsText() ?? "")).OfType().Distinct().ToList(); + return declared is { Count: > 0 } ? WidgetFamilies.All.Where(declared.Contains).ToList() : WidgetFamilies.All; + } + } +} + +/// A template or example shipped with the application. +public sealed record BundledPackage(string Group, string Name, string Directory); + +/// +/// The user's widget library under the data folder, plus the read-only +/// templates shipped with the application. All package I/O goes through the +/// hardened . +/// +public sealed partial class Library +{ + public const string InstallationFile = ".scriptwidget-installation"; + + private readonly string root; + private readonly string templates; + + /// Folder with the bundled templates; defaults to templates next to the program. + public Library(DataPaths paths, string? templatesDirectory = null) + { + root = paths.Packages; + templates = templatesDirectory ?? Path.Combine(AppContext.BaseDirectory, "templates"); + System.IO.Directory.CreateDirectory(root); + Store = new PackageStore(root, Path.Combine(paths.Cache, "package-staging")); + } + + public PackageStore Store { get; } + public string Root => root; + + /// Raised after packages were added, removed, renamed or edited. + public event Action? Changed; + + public IReadOnlyList List() => Store.List().OrderBy(item => item.Name, StringComparer.OrdinalIgnoreCase).Select(Describe).ToList(); + + public InstalledPackage? Find(string name) => Store.List().FirstOrDefault(item => item.Name.Equals(name, StringComparison.OrdinalIgnoreCase)) is { } found ? Describe(found) : null; + + public InstalledPackage? FindByInstallation(string installation) => List().FirstOrDefault(item => item.Installation == installation); + + private InstalledPackage Describe(PackageInfo info) + { + JsonObject? manifest = null; + var present = false; + try + { + var file = new FileInfo(Path.Combine(info.Directory, PackageManifests.FileName)); + present = file.Exists; + if (present && file.Length < PackageLimits.MaxMetadataBytes) manifest = PackageManifests.Parse(File.ReadAllText(file.FullName, Encoding.UTF8)); + } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) { } + return new InstalledPackage(info.Name, info.Directory, Installation(info.Directory), manifest, present); + } + + /// Stable identity kept in a dot-file, which package listings and exports skip. + private static string Installation(string directory) + { + var marker = Path.Combine(directory, InstallationFile); + try + { + if (File.Exists(marker) && new FileInfo(marker).Length <= 256) + { + var existing = File.ReadAllText(marker, Encoding.UTF8).Trim(); + if (existing.Length is >= 8 and <= 64 && existing.All(character => char.IsAsciiLetterOrDigit(character) || character == '-')) return existing; + } + } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) { } + var created = Guid.NewGuid().ToString(); + try { File.WriteAllText(marker, created, new UTF8Encoding(false)); } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) { } + return created; + } + + private static void ForgetInstallation(string directory) + { + try { File.Delete(Path.Combine(directory, InstallationFile)); } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) { } + } + + /// Creates a package; a taken name gets a numbered suffix. + public InstalledPackage Create(string name, string source, string? manifest = null) + { + var created = Describe(Store.Create(Store.UniqueName(name), source, manifest)); + Changed?.Invoke(); + return created; + } + + /// Checks the unpacked files before they are installed; throw to refuse the package. + public InstalledPackage ImportArchive(Stream input, string suggestedName, Action? validate = null) + { + var info = Store.ImportArchive(input, suggestedName, validate); + // An archive never carries another installation's identity. + ForgetInstallation(info.Directory); + var installed = Describe(info); + Changed?.Invoke(); + return installed; + } + + public void ExportArchive(string name, Stream output) => Store.ExportArchive(name, output); + + public void Delete(string name) + { + Store.Delete(name); + Changed?.Invoke(); + } + + public InstalledPackage Rename(string name, string newName) + { + var renamed = Describe(Store.Rename(name, newName)); + Changed?.Invoke(); + return renamed; + } + + public InstalledPackage Duplicate(string name) + { + var copy = Store.Duplicate(name); + ForgetInstallation(copy.Directory); + var installed = Describe(copy); + Changed?.Invoke(); + return installed; + } + + public void WriteFile(string name, string path, byte[] bytes) + { + Store.WriteFile(name, path, bytes); + ClearBuild(name); + Changed?.Invoke(); + } + + /// Removes the compiled-artifact cache of a package. + public void ClearBuild(string name) + { + if (Find(name) is not { } installed) return; + try { if (System.IO.Directory.Exists(installed.BuildDirectory)) System.IO.Directory.Delete(installed.BuildDirectory, true); } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) { } + } + + // MARK: - Bundled templates + + /// + /// Script globals Windows does not implement in this release. A template + /// that uses one would only show "not supported", so it is left out of + /// the template list. + /// + [GeneratedRegex(@"\$(health|calendar|reminders|photos|contacts|home|icloud|alarms|maps|services|music|notifications|location|documents)\b")] + private static partial Regex UnsupportedAPI(); + + /// True for a script global without the dollar sign, such as health, that Windows does not implement in this release. + public static bool IsUnsupportedApi(string name) => UnsupportedAPI().IsMatch("$" + name) && UnsupportedAPI().Match("$" + name).Length == name.Length + 1; + + /// The bundled templates that can run on Windows, grouped as the Apple apps group them. + public IReadOnlyList Bundled() + { + var result = new List(); + foreach (var group in new[] { "template", "component", "api" }) + { + var directory = Path.Combine(templates, group); + if (!System.IO.Directory.Exists(directory)) continue; + foreach (var package in System.IO.Directory.EnumerateDirectories(directory).OrderBy(Path.GetFileName, StringComparer.OrdinalIgnoreCase)) + { + var main = Path.Combine(package, PackageManifests.LegacyEntry); + if (!File.Exists(main)) continue; + try { if (UnsupportedAPI().IsMatch(File.ReadAllText(main, Encoding.UTF8))) continue; } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) { continue; } + result.Add(new BundledPackage(group, Path.GetFileName(package), package)); + } + } + return result; + } + + /// Files of a bundled package as (relative path, bytes). + public IReadOnlyDictionary BundledFiles(BundledPackage bundled) + { + var result = new Dictionary(StringComparer.Ordinal); + void Visit(string directory, string relative) + { + foreach (var entry in new DirectoryInfo(directory).EnumerateFileSystemInfos().OrderBy(item => item.Name, StringComparer.Ordinal)) + { + if (entry.Name.StartsWith('.') || entry.Name == "__Build" || entry.Attributes.HasFlag(FileAttributes.ReparsePoint)) continue; + var path = relative.Length == 0 ? entry.Name : relative + "/" + entry.Name; + if (entry is DirectoryInfo) Visit(entry.FullName, path); + else if (entry is FileInfo { Length: <= PackageLimits.MaxFileBytes }) result[path] = File.ReadAllBytes(entry.FullName); + } + } + Visit(bundled.Directory, ""); + return result; + } + + /// + /// The manifest for a bundled template that has no widget.json. A copy in the library always + /// has a manifest, and a manifest without network stops every request, so the hosts the + /// template names in its own source are declared for it. This is done only for the templates + /// shipped with the app, never for an imported package. + /// + public static JsonObject BundledManifest(string name, IReadOnlyDictionary files) + { + var metadata = files.TryGetValue(PackageManifests.LegacyMetadata, out var bytes) ? Encoding.UTF8.GetString(bytes) : null; + var manifest = PackageManifests.LegacyManifest(name, metadata); + manifest["id"] = PackageManifests.FreshId(); + var hosts = new SortedSet(StringComparer.Ordinal); + foreach (var (path, content) in files) + { + if (!path.EndsWith(".js", StringComparison.Ordinal) && !path.EndsWith(".jsx", StringComparison.Ordinal)) continue; + foreach (var line in Encoding.UTF8.GetString(content).Split('\n')) + { + if (line.TrimStart().StartsWith("//", StringComparison.Ordinal)) continue; + foreach (Match match in SourceUrl().Matches(line)) + { + var host = match.Groups[1].Value.ToLowerInvariant(); + if (DeclarableHost().IsMatch(host)) hosts.Add(host); + } + } + } + if (hosts.Count > 0) + { + manifest["permissions"] = Json.Array(["network"]); + manifest["networkDomains"] = Json.Array(hosts); + } + return manifest; + } + + [GeneratedRegex(@"https?://([A-Za-z0-9.-]+)")] + private static partial Regex SourceUrl(); + + [GeneratedRegex(@"^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*\.[a-z]{2,}$")] + private static partial Regex DeclarableHost(); + + public InstalledPackage InstallBundled(BundledPackage bundled) + { + var files = BundledFiles(bundled); + if (!files.TryGetValue(PackageManifests.LegacyEntry, out var main)) throw new PackageException(PackageErrors.MissingEntry, "Template has no main.jsx"); + var name = Store.UniqueName(bundled.Name); + var manifest = files.TryGetValue(PackageManifests.FileName, out var manifestBytes) + ? Encoding.UTF8.GetString(manifestBytes) + : Encoding.UTF8.GetString(PackageManifests.Encode(BundledManifest(name, files))); + var created = Store.Create(name, Encoding.UTF8.GetString(main), manifest); + foreach (var (path, bytes) in files) + { + if (path != PackageManifests.LegacyEntry && path != PackageManifests.FileName) Store.WriteFile(created.Name, path, bytes); + } + var installed = Describe(created); + Changed?.Invoke(); + return installed; + } +} diff --git a/Windows/src/ScriptWidget.Core/Hosting/StartupRegistration.cs b/Windows/src/ScriptWidget.Core/Hosting/StartupRegistration.cs new file mode 100644 index 00000000..02729b9a --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Hosting/StartupRegistration.cs @@ -0,0 +1,62 @@ +using System.Runtime.InteropServices; +using Microsoft.Win32; + +namespace ScriptWidget.Core.Hosting; + +/// Starting the desktop host when the user signs in. +public static class StartupRegistration +{ + private const string RunKey = @"Software\Microsoft\Windows\CurrentVersion\Run"; + private const string ValueName = "ScriptWidget"; + /// Identifier of the startup task declared in the MSIX manifest. + public const string TaskID = "ScriptWidgetStartup"; + + [DllImport("kernel32.dll")] private static extern unsafe int GetCurrentPackageFullName(uint* length, char* name); + + /// True when the program was installed as an MSIX package (from the Microsoft Store). + public static unsafe bool IsPackaged + { + get + { + uint length = 0; + return GetCurrentPackageFullName(&length, null) != 15700 /* APPMODEL_ERROR_NO_PACKAGE */; + } + } + + public static bool IsEnabled() + { + try + { + if (IsPackaged) + { + var task = Windows.ApplicationModel.StartupTask.GetAsync(TaskID).AsTask().GetAwaiter().GetResult(); + return task.State is Windows.ApplicationModel.StartupTaskState.Enabled or Windows.ApplicationModel.StartupTaskState.EnabledByPolicy; + } + using var key = Registry.CurrentUser.OpenSubKey(RunKey); + return key?.GetValue(ValueName) is string; + } + catch (Exception error) when (error is not OutOfMemoryException) { return false; } + } + + /// Turns starting at sign-in on or off. Returns the state afterwards; Windows or the user's policy may refuse. + /// Full path of ScriptWidget.exe, used when the program is not packaged. + public static bool SetEnabled(bool enabled, string? hostExecutable) + { + try + { + if (IsPackaged) + { + var task = Windows.ApplicationModel.StartupTask.GetAsync(TaskID).AsTask().GetAwaiter().GetResult(); + if (enabled) task.RequestEnableAsync().AsTask().GetAwaiter().GetResult(); else task.Disable(); + } + else + { + using var key = Registry.CurrentUser.CreateSubKey(RunKey); + if (enabled && hostExecutable is not null) key.SetValue(ValueName, $"\"{hostExecutable}\" --startup"); + else key.DeleteValue(ValueName, throwOnMissingValue: false); + } + } + catch (Exception error) when (error is not OutOfMemoryException) { } + return IsEnabled(); + } +} diff --git a/Windows/src/ScriptWidget.Core/Hosting/WidgetPlacements.cs b/Windows/src/ScriptWidget.Core/Hosting/WidgetPlacements.cs new file mode 100644 index 00000000..5d8d2492 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Hosting/WidgetPlacements.cs @@ -0,0 +1,237 @@ +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; + +namespace ScriptWidget.Core.Hosting; + +/// One widget on the desktop: which package it shows, at what size, and where. +public sealed class WidgetPlacement +{ + public required string ID { get; init; } + /// Identity of the installed package (its folder name). + public required string Package { get; set; } + public string Family { get; set; } = "medium"; + /// The value of $getenv("widget-param"). + public string Parameter { get; set; } = ""; + /// Device name of the display the widget is on, such as \\.\DISPLAY1. + public string Monitor { get; set; } = ""; + /// Offset from the display's top-left corner, in pixels at . + public int OffsetX { get; set; } + public int OffsetY { get; set; } + /// Display scale when the offset was saved. + public double Scale { get; set; } = 1.0; + + public JsonObject ToJson() => new() + { + ["id"] = ID, ["package"] = Package, ["family"] = Family, ["parameter"] = Parameter, + ["monitor"] = Monitor, ["x"] = OffsetX, ["y"] = OffsetY, ["scale"] = Scale, + }; + + public static WidgetPlacement? FromJson(JsonObject json) + { + var id = json.OptString("id"); + var package = json.OptString("package"); + if (id.Length == 0 || id.Length > 64 || package.Length == 0 || package.Length > 255) return null; + var family = json.OptString("family", "medium"); + var scale = json.OptDouble("scale", 1.0); + return new WidgetPlacement + { + ID = id, + Package = package, + Family = WidgetFamilies.IsKnown(family) ? family : "medium", + Parameter = Clip(json.OptString("parameter"), 2048), + Monitor = Clip(json.OptString("monitor"), 64), + OffsetX = Math.Clamp(json.OptInt("x"), -100_000, 100_000), + OffsetY = Math.Clamp(json.OptInt("y"), -100_000, 100_000), + Scale = scale is >= 0.5 and <= 8 ? scale : 1.0, + }; + } + + private static string Clip(string text, int length) => text.Length <= length ? text : text[..length]; +} + +/// Widget sizes on Windows, in points. They follow the macOS desktop widget sizes of the preview engine. +public static class WidgetFamilies +{ + public static readonly IReadOnlyList All = ["small", "medium", "large", "extraLarge"]; + + public static bool IsKnown(string family) => All.Contains(family); + + /// The size for a supportedFamilies entry of widget.json, or null for one Windows does not have (Lock Screen sizes). + public static string? FromManifest(string name) => name switch + { + "systemSmall" or "small" => "small", + "systemMedium" or "medium" => "medium", + "systemLarge" or "large" => "large", + "systemExtraLarge" or "extraLarge" => "extraLarge", + _ => null, + }; + + public static (double Width, double Height) Size(string family) => family switch + { + "small" => (170, 170), + "large" => (329, 345), + "extraLarge" => (639, 345), + _ => (329, 170), + }; +} + +/// The saved list of desktop widgets. Safe to use from any thread. +public sealed class WidgetPlacements(string file) +{ + public const int MaximumWidgets = 64; + private readonly object gate = new(); + private List? items; + + public IReadOnlyList All() + { + lock (gate) return Load().ToList(); + } + + public WidgetPlacement? Find(string id) + { + lock (gate) return Load().FirstOrDefault(item => item.ID == id); + } + + /// Adds a widget. Returns null when the limit is reached. + public WidgetPlacement? Add(string package, string family, string parameter = "") + { + lock (gate) + { + var list = Load(); + if (list.Count >= MaximumWidgets) return null; + var placement = new WidgetPlacement { ID = Guid.NewGuid().ToString("N"), Package = package, Family = WidgetFamilies.IsKnown(family) ? family : "medium", Parameter = parameter }; + list.Add(placement); + Save(list); + return placement; + } + } + + public void Remove(string id) + { + lock (gate) + { + var list = Load(); + if (list.RemoveAll(item => item.ID == id) > 0) Save(list); + } + } + + /// Saves a change made to a placement returned by this store. + public void Update(WidgetPlacement placement) + { + lock (gate) + { + var list = Load(); + var index = list.FindIndex(item => item.ID == placement.ID); + if (index < 0) return; + list[index] = placement; + Save(list); + } + } + + private List Load() + { + if (items is not null) return items; + var loaded = new List(); + try + { + if (File.Exists(file) && Json.Parse(File.ReadAllText(file, Encoding.UTF8)) is JsonObject root && root.OptArray("widgets") is { } array) + { + foreach (var node in array) + { + if (loaded.Count >= MaximumWidgets) break; + if (node is JsonObject item && WidgetPlacement.FromJson(item) is { } placement && loaded.All(existing => existing.ID != placement.ID)) loaded.Add(placement); + } + } + } + catch (Exception error) when (error is JsonException or IOException or UnauthorizedAccessException) { } + return items = loaded; + } + + private void Save(List list) + { + var array = new JsonArray(); + foreach (var item in list) array.Append(item.ToJson()); + var text = Json.Stringify(new JsonObject { ["version"] = 1, ["widgets"] = array }, indented: true); + var temporary = file + ".tmp"; + try + { + Directory.CreateDirectory(Path.GetDirectoryName(file)!); + File.WriteAllText(temporary, text, new UTF8Encoding(false)); + File.Move(temporary, file, overwrite: true); + } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) + { + try { File.Delete(temporary); } catch (IOException) { } catch (UnauthorizedAccessException) { } + } + } +} + +/// User settings of the desktop host. +public sealed class HostSettings(string file) +{ + private readonly object gate = new(); + private JsonObject? values; + + /// How often widgets without a schedule of their own are refreshed. + public int RefreshMinutes + { + get => Math.Clamp(Read().OptInt("refreshMinutes", 15), 5, 24 * 60); + set => Write("refreshMinutes", Math.Clamp(value, 5, 24 * 60)); + } + + /// When true, widgets cannot be dragged. + public bool Locked + { + get => Read().OptBoolean("locked"); + set => Write("locked", value); + } + + /// Hold refreshes while a full-screen program or game is in front. + public bool PauseWhenBusy + { + get => Read().OptBoolean("pauseWhenBusy", true); + set => Write("pauseWhenBusy", value); + } + + /// Set after the first-run welcome was shown. + public bool Welcomed + { + get => Read().OptBoolean("welcomed"); + set => Write("welcomed", value); + } + + private JsonObject Read() + { + lock (gate) + { + if (values is not null) return values; + try { values = File.Exists(file) ? Json.Parse(File.ReadAllText(file, Encoding.UTF8)) as JsonObject : null; } + catch (Exception error) when (error is JsonException or IOException or UnauthorizedAccessException) { values = null; } + return values ??= new JsonObject(); + } + } + + private void Write(string name, JsonNode value) + { + lock (gate) + { + var current = Read(); + current[name] = value; + var temporary = file + ".tmp"; + try + { + Directory.CreateDirectory(Path.GetDirectoryName(file)!); + File.WriteAllText(temporary, Json.Stringify(current, indented: true), new UTF8Encoding(false)); + File.Move(temporary, file, overwrite: true); + } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) { } + } + } + + /// Drops the cached values so the next read sees what another process wrote. + public void Reload() + { + lock (gate) values = null; + } +} diff --git a/Windows/src/ScriptWidget.Core/Js/QuickJs.cs b/Windows/src/ScriptWidget.Core/Js/QuickJs.cs new file mode 100644 index 00000000..0a8ce42b --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Js/QuickJs.cs @@ -0,0 +1,259 @@ +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +namespace ScriptWidget.Core.Js; + +/// Thrown when JavaScript raises an uncaught exception or is interrupted. +public sealed class JsException(string message, bool interrupted) : Exception(message) +{ + public bool Interrupted { get; } = interrupted; +} + +/// Synchronous bridge invoked by the __swNative(op, payload) JavaScript global. +public delegate string? JsHost(string operation, string? payload); + +/// +/// One QuickJS-NG runtime and context. Not thread safe: create, use and close +/// an instance on a single thread (see ). +/// +public sealed unsafe class QuickJs : IDisposable +{ + public const long DefaultMemoryLimit = 64L * 1024 * 1024; + public const long DefaultStack = 3L * 1024 * 1024; + + private readonly JsHost host; + private GCHandle self; + private nint pointer; + + public QuickJs(JsHost host, long memoryLimitBytes = DefaultMemoryLimit, long stackBytes = DefaultStack) + { + this.host = host; + self = GCHandle.Alloc(this); + pointer = Native.sw_create(memoryLimitBytes, stackBytes, &HostCall, GCHandle.ToIntPtr(self)); + if (pointer == 0) + { + self.Free(); + throw new InvalidOperationException("QuickJS runtime could not be created"); + } + } + + /// Stops running JavaScript after the given time; 0 clears the deadline. + public void SetDeadline(long milliseconds) => Native.sw_set_deadline(Handle(), milliseconds); + + public string? Evaluate(string code, string fileName = "script.js") + { + var engine = Handle(); + var name = Utf8(fileName); + int status; + fixed (char* chars = code) + fixed (byte* file = name) + status = Native.sw_evaluate(engine, chars, code.Length, file); + return Finish(engine, status); + } + + public byte[] Compile(string code, string fileName = "script.js") + { + var engine = Handle(); + var name = Utf8(fileName); + int status; + fixed (char* chars = code) + fixed (byte* file = name) + status = Native.sw_compile(engine, chars, code.Length, file); + if (status < 0) throw Failure(engine); + var bytes = Native.sw_result(engine, out var length); + return new ReadOnlySpan(bytes, (int)length).ToArray(); + } + + /// Bytecode must come from in this application build. + public string? EvaluateBytecode(byte[] bytecode) + { + var engine = Handle(); + int status; + fixed (byte* bytes = bytecode) + status = Native.sw_evaluate_bytecode(engine, bytes, bytecode.Length); + return Finish(engine, status); + } + + public string? Call(string function, params string?[] arguments) + { + var engine = Handle(); + var name = Utf8(function); + var count = arguments.Length; + var handles = new GCHandle[count]; + var pointers = stackalloc char*[Math.Max(count, 1)]; + var lengths = stackalloc nint[Math.Max(count, 1)]; + try + { + for (var index = 0; index < count; index++) + { + var argument = arguments[index]; + if (argument is null) { pointers[index] = null; lengths[index] = -1; continue; } + handles[index] = GCHandle.Alloc(argument, GCHandleType.Pinned); + pointers[index] = (char*)handles[index].AddrOfPinnedObject(); + lengths[index] = argument.Length; + } + int status; + fixed (byte* file = name) + status = Native.sw_call(engine, file, pointers, lengths, count); + return Finish(engine, status); + } + finally + { + foreach (var handle in handles) if (handle.IsAllocated) handle.Free(); + } + } + + /// Runs queued promise jobs; returns how many ran. + public int RunJobs(int maximum = 10_000) + { + var engine = Handle(); + var executed = Native.sw_run_jobs(engine, maximum); + if (executed < 0) throw Failure(engine); + return executed; + } + + public long MemoryUsed() => Native.sw_memory_used(Handle()); + + public void Dispose() + { + if (pointer == 0) return; + Native.sw_destroy(pointer); + pointer = 0; + self.Free(); + } + + /// + /// Time zone of JavaScript's local time for every engine in the process: + /// an IANA name such as "America/Los_Angeles", or null for the system zone. + /// + public static void SetTimeZone(string? ianaName) + { + if (ianaName is null) { Native.sw_set_time_zone(null, -1); return; } + fixed (char* chars = ianaName) Native.sw_set_time_zone(chars, ianaName.Length); + } + + private nint Handle() => pointer != 0 ? pointer : throw new ObjectDisposedException(nameof(QuickJs), "QuickJS runtime is closed"); + + private static string? Finish(nint engine, int status) + { + if (status < 0) throw Failure(engine); + return status == 0 ? null : ResultText(engine); + } + + private static string ResultText(nint engine) + { + var chars = (char*)Native.sw_result(engine, out var length); + return length == 0 ? "" : new string(chars, 0, (int)length); + } + + private static JsException Failure(nint engine) => new(ResultText(engine), Native.sw_interrupted(engine) != 0); + + private static byte[] Utf8(string text) + { + var bytes = new byte[System.Text.Encoding.UTF8.GetByteCount(text) + 1]; + System.Text.Encoding.UTF8.GetBytes(text, bytes); + return bytes; + } + + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])] + private static void HostCall(nint user, char* operation, nint operationLength, char* payload, nint payloadLength) + { + var engine = (QuickJs)GCHandle.FromIntPtr(user).Target!; + try + { + var result = engine.host(new string(operation, 0, (int)operationLength), payload is null || payloadLength < 0 ? null : new string(payload, 0, (int)payloadLength)); + if (result is null) return; + fixed (char* chars = result) Native.sw_host_return(engine.pointer, chars, result.Length); + } + catch (Exception error) + { + // Exceptions must not unwind through native frames: hand the message to JavaScript. + var message = string.IsNullOrEmpty(error.Message) ? "native host error" : error.Message; + fixed (char* chars = message) Native.sw_host_throw(engine.pointer, chars, message.Length); + } + } + + private static partial class Native + { + private const string Library = "swcore"; + + [DllImport(Library, CallingConvention = CallingConvention.Cdecl)] + public static extern nint sw_create(long memoryLimit, long stackSize, delegate* unmanaged[Cdecl] host, nint user); + [DllImport(Library, CallingConvention = CallingConvention.Cdecl)] + public static extern void sw_destroy(nint engine); + [DllImport(Library, CallingConvention = CallingConvention.Cdecl)] + public static extern void sw_set_deadline(nint engine, long millisecondsFromNow); + [DllImport(Library, CallingConvention = CallingConvention.Cdecl)] + public static extern int sw_interrupted(nint engine); + [DllImport(Library, CallingConvention = CallingConvention.Cdecl)] + public static extern void* sw_result(nint engine, out nint length); + [DllImport(Library, CallingConvention = CallingConvention.Cdecl)] + public static extern void sw_host_return(nint engine, char* chars, nint length); + [DllImport(Library, CallingConvention = CallingConvention.Cdecl)] + public static extern void sw_host_throw(nint engine, char* chars, nint length); + [DllImport(Library, CallingConvention = CallingConvention.Cdecl)] + public static extern int sw_evaluate(nint engine, char* code, nint length, byte* fileName); + [DllImport(Library, CallingConvention = CallingConvention.Cdecl)] + public static extern int sw_compile(nint engine, char* code, nint length, byte* fileName); + [DllImport(Library, CallingConvention = CallingConvention.Cdecl)] + public static extern int sw_evaluate_bytecode(nint engine, byte* bytes, nint length); + [DllImport(Library, CallingConvention = CallingConvention.Cdecl)] + public static extern int sw_call(nint engine, byte* functionName, char** arguments, nint* lengths, int count); + [DllImport(Library, CallingConvention = CallingConvention.Cdecl)] + public static extern int sw_run_jobs(nint engine, int maximum); + [DllImport(Library, CallingConvention = CallingConvention.Cdecl)] + public static extern long sw_memory_used(nint engine); + [DllImport(Library, CallingConvention = CallingConvention.Cdecl)] + public static extern void sw_set_time_zone(char* id, nint length); + } +} + +/// Runs work on a fresh thread with a stack large enough for deep JavaScript recursion. +public static class JsThread +{ + private const int StackBytes = 8 * 1024 * 1024; + + public static T Run(Func block, string name = "scriptwidget-js", int stackBytes = StackBytes) + { + T result = default!; + System.Runtime.ExceptionServices.ExceptionDispatchInfo? failure = null; + var thread = new Thread(() => + { + try { result = block(); } + catch (Exception error) { failure = System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(error); } + }, stackBytes) { Name = name, IsBackground = true }; + thread.Start(); + thread.Join(); + failure?.Throw(); + return result; + } +} + +/// A dedicated large-stack thread that runs submitted work in order. +public sealed class JsWorker : IDisposable +{ + private readonly System.Collections.Concurrent.BlockingCollection queue = new(); + private readonly Thread thread; + + public JsWorker(string name, int stackBytes) + { + thread = new Thread(() => { foreach (var work in queue.GetConsumingEnumerable()) work(); }, stackBytes) { Name = name, IsBackground = true }; + thread.Start(); + } + + public T Run(Func block) + { + var completion = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + queue.Add(() => + { + try { completion.SetResult(block()); } + catch (Exception error) { completion.SetException(error); } + }); + try { return completion.Task.GetAwaiter().GetResult(); } + catch (AggregateException error) when (error.InnerException is not null) { throw error.InnerException; } + } + + public void Post(Action work) => queue.Add(work); + + public void Dispose() => queue.CompleteAdding(); +} diff --git a/Windows/src/ScriptWidget.Core/Json.cs b/Windows/src/ScriptWidget.Core/Json.cs new file mode 100644 index 00000000..195dd6b8 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Json.cs @@ -0,0 +1,139 @@ +using System.Text.Encodings.Web; +using System.Text.Json; +using System.Text.Json.Nodes; + +namespace ScriptWidget.Core; + +/// +/// JSON helpers over with the lenient accessors of +/// org.json, which the Android implementation this code is ported from uses. +/// Objects keep insertion order. Nothing here uses reflection, so it works +/// under native AOT. +/// +public static class Json +{ + private static readonly JsonSerializerOptions Compact = new() { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping }; + private static readonly JsonSerializerOptions Indented = new() { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, WriteIndented = true }; + private static readonly JsonDocumentOptions Strict = new() { AllowTrailingCommas = false, CommentHandling = JsonCommentHandling.Disallow, MaxDepth = 256 }; + + /// Parses JSON text; throws when it is malformed. + public static JsonNode? Parse(string text) => JsonNode.Parse(text, null, Strict); + + public static JsonObject ParseObject(string text) => Parse(text) as JsonObject ?? throw new JsonException("Expected a JSON object"); + + public static JsonNode? TryParse(string? text) + { + if (text is null) return null; + try { return Parse(text); } catch (JsonException) { return null; } + } + + public static string Stringify(JsonNode? node, bool indented = false) => node is null ? "null" : node.ToJsonString(indented ? Indented : Compact); + + /// A JSON string literal, quotes included. + public static string Quote(string? text) => text is null ? "null" : Stringify(JsonValue.Create(text)); + + public static JsonArray Array(IEnumerable items) + { + var array = new JsonArray(); + foreach (var item in items) array.Add((JsonNode)JsonValue.Create(item)); + return array; + } + + public static JsonObject Object(IEnumerable> items) + { + var result = new JsonObject(); + foreach (var (key, value) in items) result[key] = value; + return result; + } + + /// + /// Adds a node to an array. Use this instead of JsonArray.Add, whose + /// generic overload wins for JsonObject and JsonValue arguments and is not + /// safe under native AOT. + /// + public static JsonArray Append(this JsonArray array, JsonNode? node) { array.Add(node); return array; } + + public static JsonArray Append(this JsonArray array, string? text) { array.Add((JsonNode?)(text is null ? null : JsonValue.Create(text))); return array; } + + // MARK: - Lenient accessors + + public static bool Has(this JsonObject json, string name) => json.ContainsKey(name); + + /// True when the member is missing or null. + public static bool IsNull(this JsonObject json, string name) => !json.TryGetPropertyValue(name, out var value) || value is null; + + public static JsonObject? OptObject(this JsonObject? json, string name) => json is not null && json.TryGetPropertyValue(name, out var value) ? value as JsonObject : null; + + public static JsonArray? OptArray(this JsonObject? json, string name) => json is not null && json.TryGetPropertyValue(name, out var value) ? value as JsonArray : null; + + /// The member as text; numbers and booleans are converted, like org.json's optString. + public static string OptString(this JsonObject? json, string name, string fallback = "") + { + if (json is null || !json.TryGetPropertyValue(name, out var value) || value is null) return fallback; + return value.AsText() ?? fallback; + } + + public static string? OptStringOrNull(this JsonObject? json, string name) + { + if (json is null || !json.TryGetPropertyValue(name, out var value) || value is null) return null; + return value.AsText(); + } + + public static bool OptBoolean(this JsonObject? json, string name, bool fallback = false) + { + if (json is null || !json.TryGetPropertyValue(name, out var value) || value is not JsonValue item) return fallback; + if (item.TryGetValue(out var flag)) return flag; + if (item.TryGetValue(out var text)) return text.Equals("true", StringComparison.OrdinalIgnoreCase) || (!text.Equals("false", StringComparison.OrdinalIgnoreCase) && fallback); + return fallback; + } + + public static double OptDouble(this JsonObject? json, string name, double fallback = double.NaN) + { + if (json is null || !json.TryGetPropertyValue(name, out var value) || value is null) return fallback; + return value.AsDouble() ?? fallback; + } + + public static int OptInt(this JsonObject? json, string name, int fallback = 0) + { + var number = json.OptDouble(name); + return double.IsNaN(number) ? fallback : (int)Math.Clamp(number, int.MinValue, int.MaxValue); + } + + public static long OptLong(this JsonObject? json, string name, long fallback = 0) + { + var number = json.OptDouble(name); + return double.IsNaN(number) ? fallback : (long)Math.Clamp(number, long.MinValue, long.MaxValue); + } + + /// Text of a scalar node; null for objects and arrays. + public static string? AsText(this JsonNode node) + { + if (node is not JsonValue value) return null; + if (value.TryGetValue(out var text)) return text; + if (value.TryGetValue(out var flag)) return flag ? "true" : "false"; + return value.ToJsonString(); + } + + public static double? AsDouble(this JsonNode node) + { + if (node is not JsonValue value) return null; + // A value built in memory holds its CLR type (int, float, decimal…), a parsed one a JSON + // element; the JSON text of a number is the one form every case shares. + var text = value.GetValueKind() switch + { + JsonValueKind.Number => value.ToJsonString(), + JsonValueKind.String => value.TryGetValue(out var content) ? content : null, + _ => null, + }; + return text is not null && double.TryParse(text, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out var parsed) ? parsed : null; + } + + public static bool IsString(this JsonNode? node) => node is JsonValue value && value.TryGetValue(out _); + + public static bool IsNumber(this JsonNode? node) => node is JsonValue value && value.GetValueKind() == JsonValueKind.Number; + + public static bool IsBoolean(this JsonNode? node) => node is JsonValue value && value.GetValueKind() is JsonValueKind.True or JsonValueKind.False; + + /// A detached copy that can be added to another object or array. + public static JsonNode? Copy(this JsonNode? node) => node?.DeepClone(); +} diff --git a/Windows/src/ScriptWidget.Core/Packages/ArchivePreflight.cs b/Windows/src/ScriptWidget.Core/Packages/ArchivePreflight.cs new file mode 100644 index 00000000..00b1e364 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Packages/ArchivePreflight.cs @@ -0,0 +1,226 @@ +namespace ScriptWidget.Core.Packages; + +/// +/// Central-directory validation of an untrusted .swt archive, ported from +/// ScriptPackageArchivePreflight.validate in Swift (and preflightArchive in +/// archive.js). It inspects bytes only and never inflates or writes anything. +/// +internal static class ArchivePreflight +{ + /// + /// One central-directory entry that passed validation. are the non-empty + /// path components; is the position in the central directory. + /// + public sealed class Entry(int index, string name, IReadOnlyList parts, bool isDirectory, long uncompressedSize, long crc) + { + public int Index { get; } = index; + public string Name { get; } = name; + public IReadOnlyList Parts { get; } = parts; + public bool IsDirectory { get; } = isDirectory; + public long UncompressedSize { get; } = uncompressedSize; + public long Crc { get; } = crc; + } + + private const long EndSignature = 0x06054B50L; + private const long CentralSignature = 0x02014B50L; + private const long UInt32Max = 0xFFFFFFFFL; + + /// + /// Validates the whole archive and returns its entries, or throws. + /// + /// Rejected: empty or oversized archives; a missing, multi-disk, trailing-garbage or + /// out-of-bounds central directory; more entries than allowed; encrypted entries; compression + /// other than stored/deflate; malformed ZIP64 sizes; entry names that are not UTF-8, are + /// absolute, contain a backslash, colon, NUL, control character, . or .. + /// component, nest too deeply, or hold a component Windows cannot store safely (see + /// ); duplicate, case-colliding or + /// file-versus-directory clashing paths; symbolic links and other special files; a declared + /// file size or declared total above the limits; and a large file that claims zero compressed + /// bytes. + /// + public static List Validate(byte[] data, ArchiveLimits limits) + { + if (data.Length == 0 || data.Length > limits.MaximumArchiveBytes) throw Fail(PackageErrors.ArchiveSize, "Archive must be between 1 byte and 32 MiB."); + var end = EndOfCentralDirectory(data); + if (end < 0) throw Fail(PackageErrors.ArchiveMalformed, "Archive has no valid ZIP central directory."); + if (U16(data, end + 4) != 0 || U16(data, end + 6) != 0 || U16(data, end + 8) != U16(data, end + 10)) + { + throw Fail(PackageErrors.ArchiveMultiDisk, "Multi-disk ZIP archives are not supported."); + } + if (end + 22 + U16(data, end + 20) != data.Length) throw Fail(PackageErrors.ArchiveMalformed, "Archive has a malformed ZIP footer."); + var count = U16(data, end + 10); + var centralSize = U32(data, end + 12); + var centralOffset = U32(data, end + 16); + if (count <= 0 || count > limits.MaximumFileCount) + { + throw Fail(PackageErrors.ArchiveTooManyEntries, $"Archive must contain 1–{limits.MaximumFileCount} entries."); + } + if (centralOffset + centralSize > data.Length) throw Fail(PackageErrors.ArchiveMalformed, "Archive central directory is out of bounds."); + + var cursor = (int)centralOffset; + var expanded = 0L; + var collisions = new Collisions(); + var entries = new List(count); + for (var index = 0; index < count; index++) + { + if (cursor + 46 > data.Length || U32(data, cursor) != CentralSignature) + { + throw Fail(PackageErrors.ArchiveMalformed, "Archive central directory contains an invalid entry."); + } + var flags = U16(data, cursor + 8); + var method = U16(data, cursor + 10); + var crc = U32(data, cursor + 16); + var nameLength = U16(data, cursor + 28); + var extraLength = U16(data, cursor + 30); + var commentLength = U16(data, cursor + 32); + var external = U32(data, cursor + 38); + var entryEnd = cursor + 46 + nameLength + extraLength + commentLength; + if (nameLength == 0 || entryEnd > data.Length) throw Fail(PackageErrors.ArchiveMalformed, "Archive contains a truncated filename."); + var extraStart = cursor + 46 + nameLength; + if (ResolvedSizes(U32(data, cursor + 20), U32(data, cursor + 24), data, extraStart, extraStart + extraLength) is not var (compressed, uncompressed)) + { + throw Fail(PackageErrors.ArchiveMalformed, "Archive contains malformed ZIP64 size metadata."); + } + if ((flags & 0x1) != 0) throw Fail(PackageErrors.ArchiveEncrypted, "Encrypted package entries are not supported."); + if (method != 0 && method != 8) throw Fail(PackageErrors.ArchiveUnsupportedCompression, $"Unsupported ZIP compression method {method}."); + var name = SafeFiles.DecodeUtf8(data, cursor + 46, nameLength); + var parts = name is null ? null : EntryParts(name, limits.MaximumPathDepth); + if (name is null || parts is null) throw Fail(PackageErrors.ArchiveUnsafePath, "Archive contains an unsafe or non-UTF-8 path."); + var isDirectory = name.EndsWith('/'); + if (!collisions.Add(name, parts, isDirectory)) + { + throw Fail(PackageErrors.ArchivePathCollision, "Archive contains duplicate or case-colliding paths."); + } + // Unix mode in the high half: only regular files (0x8000), directories (0x4000) or + // an absent type are accepted; 0xA000 is a symbolic link. + var type = (int)((external >>> 16) & 0xF000L); + if (type == 0xA000) throw Fail(PackageErrors.ArchiveSymlink, "Symbolic links are not allowed in ScriptWidget packages."); + if (type != 0 && type != 0x8000 && type != 0x4000) + { + throw Fail(PackageErrors.ArchiveSymlink, "Only regular files and directories are allowed in ScriptWidget packages."); + } + if (!isDirectory) + { + // Sizes are unsigned 64-bit; a negative long is far above any limit. + if (uncompressed < 0 || uncompressed > limits.MaximumFileBytes) + { + throw Fail(PackageErrors.ArchiveFileTooLarge, $"Package file {name} exceeds 25 MiB."); + } + expanded += uncompressed; + if (expanded > limits.MaximumExpandedBytes) throw Fail(PackageErrors.ArchiveExpandedTooLarge, "Expanded package exceeds 64 MiB."); + if (uncompressed > 1024 * 1024 && compressed == 0L) + { + throw Fail(PackageErrors.ArchiveInvalidRatio, $"Archive entry {name} has an invalid compression ratio."); + } + } + entries.Add(new Entry(index, name, parts, isDirectory, uncompressed, crc)); + cursor = entryEnd; + } + if (cursor != centralOffset + centralSize) + { + throw Fail(PackageErrors.ArchiveMalformed, "Archive entries do not match the declared central directory."); + } + return entries; + } + + /// + /// Components of an archive entry name, or null when the name is unsafe: a leading slash, + /// any backslash, colon (drive or stream syntax) or NUL, a ./.. component, a + /// component that is too long, holds control characters or is not safe on Windows, no + /// components, more than , or more than 1024 UTF-16 units in all. + /// + public static IReadOnlyList? EntryParts(string name, int maximumDepth) + { + if (name.StartsWith('/') || name.Contains('\\') || name.Contains(':') || name.Contains('\0')) return null; + if (name.Length > PackageLimits.MaxRelativePathUnits) return null; + var parts = name.Split('/', StringSplitOptions.RemoveEmptyEntries); + if (parts.Length == 0 || parts.Length > maximumDepth || !parts.All(PackagePathRules.ValidComponent)) return null; + return parts; + } + + /// + /// Tracks every file and directory prefix seen so far. Two spellings of one identity + /// (case or Unicode form), a repeated path, and a path used as both file and directory would + /// overwrite or shadow each other on extraction, so each is a collision. + /// + private sealed class Collisions + { + private readonly HashSet fullPaths = new(StringComparer.Ordinal); + private readonly Dictionary spellings = new(StringComparer.Ordinal); + private readonly Dictionary directories = new(StringComparer.Ordinal); + + public bool Add(string name, IReadOnlyList parts, bool isDirectory) + { + if (!fullPaths.Add(PackageNames.Identity(name).Trim('/'))) return false; + var prefix = new System.Text.StringBuilder(); + for (var index = 0; index < parts.Count; index++) + { + if (index > 0) prefix.Append('/'); + prefix.Append(parts[index]); + var path = prefix.ToString(); + var identity = PackageNames.Identity(path); + if (!spellings.TryGetValue(identity, out var spelling)) spellings[identity] = spelling = path; + if (spelling != path) return false; + var directory = index < parts.Count - 1 || isDirectory; + if (!directories.TryGetValue(path, out var known)) directories[path] = known = directory; + if (known != directory) return false; + } + return true; + } + } + + private static (long Compressed, long Uncompressed)? ResolvedSizes(long compressed32, long uncompressed32, byte[] data, int extraStart, int extraEnd) + { + if (compressed32 != UInt32Max && uncompressed32 != UInt32Max) return (compressed32, uncompressed32); + var compressed = compressed32; + var uncompressed = uncompressed32; + var cursor = extraStart; + while (cursor + 4 <= extraEnd) + { + var identifier = U16(data, cursor); + var end = cursor + 4 + U16(data, cursor + 2); + if (end > extraEnd) return null; + if (identifier == 0x0001) + { + var value = cursor + 4; + if (uncompressed32 == UInt32Max) + { + if (value + 8 > end) return null; + uncompressed = U64(data, value); + value += 8; + } + if (compressed32 == UInt32Max) + { + if (value + 8 > end) return null; + compressed = U64(data, value); + } + return (compressed, uncompressed); + } + cursor = end; + } + return null; + } + + /// Offset of the last end-of-central-directory record, or -1. + private static int EndOfCentralDirectory(byte[] data) + { + if (data.Length < 22) return -1; + var lowerBound = Math.Max(0, data.Length - 65_557); + for (var index = data.Length - 22; index >= lowerBound; index--) + { + if (U32(data, index) == EndSignature) return index; + } + return -1; + } + + private static int U16(byte[] data, int index) => + index < 0 || index + 2 > data.Length ? 0 : data[index] | (data[index + 1] << 8); + + private static long U32(byte[] data, int index) => + index < 0 || index + 4 > data.Length ? 0 : (long)U16(data, index) | ((long)U16(data, index + 2) << 16); + + private static long U64(byte[] data, int index) => + index < 0 || index + 8 > data.Length ? 0 : U32(data, index) | (U32(data, index + 4) << 32); + + private static PackageException Fail(string code, string message) => new(code, message); +} diff --git a/Windows/src/ScriptWidget.Core/Packages/DiskPackageFiles.cs b/Windows/src/ScriptWidget.Core/Packages/DiskPackageFiles.cs new file mode 100644 index 00000000..33637124 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Packages/DiskPackageFiles.cs @@ -0,0 +1,87 @@ +using System.Text.RegularExpressions; +using ScriptWidget.Core.Runtime; + +namespace ScriptWidget.Core.Packages; + +/// +/// Read-only view of one package directory on disk. +/// +/// Every path is package-relative and must already be normalized. A path is refused (reported as +/// missing) when it is absolute, contains a backslash, NUL, ./.. or empty component, +/// names a dot-file or the generated __Build cache, is not safe on Windows (drive, UNC, +/// stream or device syntax, a trailing dot or space), crosses a symbolic link, junction or other +/// reparse point at any component, has a case or Unicode-normalization alias in its directory, is +/// a hard-linked or special file, or resolves canonically outside the root. Reads are +/// size-bounded while reading. +/// +public sealed partial class DiskPackageFiles(string root) : IPackageFiles +{ + private readonly string root = SafeFiles.Root(root); + + [GeneratedRegex(@"\.(jsx?|mjs)$", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)] + private static partial Regex ScriptFile(); + + /// + /// Regular files of the package as sorted, normalized relative paths. Dot-files and + /// dot-directories, __Build, links, special files, hard-linked files, ambiguous + /// spellings, names that are not safe on Windows and anything deeper than 16 components are + /// left out. + /// + /// too_many_entries when the package holds more than 500 files. + public IReadOnlyList Paths() + { + var result = new List(); + if (SafeFiles.RealDirectory(root) is { } directory) Collect(directory, "", 1, result); + result.Sort(StringComparer.Ordinal); + return result; + } + + /// True only for a regular file inside the package that passes every rule in the class description. + public bool Exists(string path) => Resolve(path) is not null; + + /// + /// UTF-8 text of a text-like file (per ), or null when + /// the file is missing, refused, binary, not valid UTF-8, or larger than 512 KiB for scripts + /// and 2 MiB for other text. + /// + public string? ReadText(string path) + { + if (!PackageCompiler.IsTextFile(path)) return null; + var limit = ScriptFile().IsMatch(path) ? PackageLimits.MaxScriptBytes : PackageLimits.MaxTextBytes; + return ReadBytes(path, limit) is { } bytes ? SafeFiles.DecodeUtf8(bytes) : null; + } + + /// Bytes of a file, or null when it is missing, refused, or larger than 16 MiB. + public byte[]? ReadBytes(string path) => ReadBytes(path, PackageLimits.MaxBinaryReadBytes); + + /// with an explicit bound, for callers with their own limit (manifest, export). + internal byte[]? ReadBytes(string path, long maximumBytes) => Resolve(path) is { } file ? SafeFiles.ReadBounded(file, maximumBytes) : null; + + private string? Resolve(string path) => + PackagePathRules.VisibleComponents(path) is { } parts ? SafeFiles.ExistingFile(root, parts) : null; + + private static void Collect(string directory, string prefix, int depth, List result) + { + if (depth > PackageLimits.MaxPathDepth) return; + var names = SafeFiles.Names(directory); + if (names is null) return; + foreach (var name in names) + { + if (PackagePathRules.IsExcluded(name) || !PackagePathRules.ValidComponent(name) || !SafeFiles.Unambiguous(names, name)) continue; + var child = SafeFiles.Child(directory, name); + if (SafeFiles.Attributes(child) is not { } info || info.IsSymbolicLink) continue; + if (info.IsDirectory) + { + Collect(child, $"{prefix}{name}/", depth + 1, result); + } + else if (info.IsRegularFile && SafeFiles.SinglyLinked(child)) + { + result.Add(prefix + name); + if (result.Count > PackageLimits.MaxFileCount) + { + throw new PackageException(PackageErrors.TooManyEntries, "Package contains too many entries."); + } + } + } + } +} diff --git a/Windows/src/ScriptWidget.Core/Packages/PackageArchive.cs b/Windows/src/ScriptWidget.Core/Packages/PackageArchive.cs new file mode 100644 index 00000000..06cbcde1 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Packages/PackageArchive.cs @@ -0,0 +1,343 @@ +using System.IO.Compression; +using System.Text; + +namespace ScriptWidget.Core.Packages; + +/// +/// A package extracted from an archive into a private staging directory. Nothing is visible in +/// the library until succeeds; removes whatever is +/// still in staging, so a failed or abandoned import leaves nothing behind. +/// +public sealed class ExtractedPackage : IDisposable +{ + private readonly string workDirectory; + + internal ExtractedPackage(string directory, string? rootName, string workDirectory) + { + Directory = directory; + RootName = rootName; + this.workDirectory = workDirectory; + } + + /// The extracted package root (inside the staging directory until moved). + public string Directory { get; } + + /// Name of the single top-level archive directory, or null when files sat at the archive root. + public string? RootName { get; } + + /// + /// Publishes the package with one atomic rename. must not exist + /// and must be on the same volume as the staging directory. + /// + public void MoveTo(string destination) => SafeFiles.Publish(Directory, SafeFiles.Root(destination)); + + public void Dispose() => SafeFiles.DeleteTree(workDirectory); +} + +/// +/// .swt (ZIP) import and export with the hardening of the Swift implementation +/// (ScriptPackageArchivePreflight, ScriptManager.importScriptPackage). +/// +/// Archives are untrusted. The whole central directory is validated before anything is written; +/// extraction happens in a unique directory under a caller-supplied staging directory; and the +/// sizes an archive declares are never trusted, only the bytes actually inflated. +/// +public static class PackageArchive +{ + private const string MacosResourceDirectory = "__MACOSX"; + private static readonly DateTimeOffset FixedEntryTime = new(1980, 1, 1, 0, 0, 0, TimeSpan.Zero); + + /// + /// Validates without extracting it and returns its entry names. + /// + /// With an archive_* code for every rule listed on the class. + public static IReadOnlyList Preflight(byte[] archive, ArchiveLimits? limits = null) => + ArchivePreflight.Validate(archive, limits ?? new ArchiveLimits()).Select(entry => entry.Name).ToList(); + + /// + /// Reads, validates and extracts one package from . + /// + /// Steps, each failing closed: read at most the archive byte limit; preflight the complete + /// central directory; pick the package root (files at the archive root, or exactly one + /// top-level directory); extract only that root into a fresh directory under + /// ; verify the extracted tree. Dot-files, __MACOSX + /// and __Build (generated cache that must never be accepted from an archive) are not + /// extracted. While inflating, bytes are counted and extraction stops as soon as an entry + /// exceeds its declared size or a limit, so a zip bomb with false size headers cannot expand; + /// the CRC-32 must match as well. Every target is checked to be canonically inside the fresh + /// directory before it is created. + /// + /// The caller must dispose the result. On failure the staging directory is already clean. + /// + public static ExtractedPackage Extract(Stream input, string stagingDirectory, ArchiveLimits? limits = null) + { + limits ??= new ArchiveLimits(); + var bytes = ReadArchive(input, limits); + var entries = ArchivePreflight.Validate(bytes, limits); + var selection = Select(entries); + var work = SafeFiles.NewWorkDirectory(SafeFiles.Root(stagingDirectory), "import"); + try + { + var tree = SafeFiles.Child(work, "package"); + Directory.CreateDirectory(tree); + var total = 0L; + using (var zip = new ZipArchive(new MemoryStream(bytes, writable: false), ZipArchiveMode.Read, leaveOpen: false, Encoding.UTF8)) + { + foreach (var (parts, entry) in selection.Files) + { + var zipEntry = entry.Index < zip.Entries.Count ? zip.Entries[entry.Index] : null; + if (zipEntry is null || zipEntry.FullName != entry.Name) throw Corrupt(entry.Name, null); + // Preflight refused these already; the reader's own view must agree. + if (zipEntry.IsEncrypted) throw new PackageException(PackageErrors.ArchiveEncrypted, "Encrypted package entries are not supported."); + if (((zipEntry.ExternalAttributes >>> 16) & 0xF000) == 0xA000) + { + throw new PackageException(PackageErrors.ArchiveSymlink, "Symbolic links are not allowed in ScriptWidget packages."); + } + var target = parts.Aggregate(tree, SafeFiles.Child); + if (!SafeFiles.Inside(tree, target)) throw new PackageException(PackageErrors.ArchiveUnsafePath, "Archive contains an unsafe or non-UTF-8 path."); + Directory.CreateDirectory(Path.GetDirectoryName(target)!); + using var stream = zipEntry.Open(); + total += CopyEntry(stream, target, entry, total, limits); + } + } + PackageTree.Validate(tree, limits); + return new ExtractedPackage(tree, selection.RootName, work); + } + catch (Exception error) + { + SafeFiles.DeleteTree(work); + if (error is not PackageException && (SafeFiles.IsFileError(error) || error is InvalidDataException or ArgumentException)) + { + throw new PackageException(PackageErrors.ArchiveCorrupt, "The package could not be extracted.", error); + } + throw; + } + } + + /// + /// followed by an atomic move to ; the + /// staging directory is cleaned whether or not the import succeeds. Returns the archive's + /// top-level directory name, if any. + /// + public static string? ImportTo(Stream input, string stagingDirectory, string destination, ArchiveLimits? limits = null) + { + using var extracted = Extract(input, stagingDirectory, limits); + extracted.MoveTo(destination); + return extracted.RootName; + } + + /// + /// Writes the package in to as a + /// .swt archive whose entries live under one rootName/ directory. + /// + /// Dot-files, __Build, links, hard-linked and special files are skipped (they are not + /// part of the authored package and must not leak through an export). Entries are sorted and + /// carry a fixed timestamp, so the same package always yields the same bytes. The export + /// obeys the import limits, so every exported archive can be imported again. + /// adds generated files (for example a migrated + /// widget.json) that are absent on disk. Nothing is written to + /// unless the whole archive was produced. + /// + public static void Export( + string packageDirectory, + string rootName, + Stream output, + IReadOnlyDictionary? extraFiles = null, + ArchiveLimits? limits = null) + { + limits ??= new ArchiveLimits(); + var root = PackageNames.ArchiveRootName(rootName); + var files = new DiskPackageFiles(packageDirectory); + var contents = new SortedDictionary(StringComparer.Ordinal); + foreach (var path in files.Paths()) contents[path] = null; + foreach (var (path, bytes) in extraFiles ?? new Dictionary()) + { + if (PackagePathRules.VisibleComponents(path) is null) throw new PackageException(PackageErrors.InvalidPath, "Invalid package path."); + // As in the Kotlin (putIfAbsent over a null value): an extra file takes the place of a disk file of the same path. + if (!contents.TryGetValue(path, out var present) || present is null) contents[path] = bytes; + } + if (contents.Count == 0) throw new PackageException(PackageErrors.ArchiveNoPackage, $"A package must contain 1–{limits.MaximumFileCount} files."); + if (contents.Count > limits.MaximumFileCount) throw new PackageException(PackageErrors.TooManyEntries, $"A package must contain 1–{limits.MaximumFileCount} files."); + + var buffer = new BoundedOutput(limits.MaximumArchiveBytes); + var total = 0L; + // Disposed only on success: closing a failed archive would write a central directory and could mask the failure. + var zip = new ZipArchive(buffer, ZipArchiveMode.Create, leaveOpen: true, Encoding.UTF8); + foreach (var (path, extra) in contents) + { + var name = $"{root}/{path}"; + // The importer would refuse this name, so exporting it would produce a dead archive. + if (ArchivePreflight.EntryParts(name, limits.MaximumPathDepth) is null) + { + throw new PackageException(PackageErrors.InvalidPath, $"Package path {path} cannot be exported."); + } + var bytes = extra ?? files.ReadBytes(path, limits.MaximumFileBytes) + ?? throw new PackageException(PackageErrors.FileTooLarge, $"Package file {path} is unreadable or exceeds 25 MiB."); + if (bytes.Length > limits.MaximumFileBytes) throw new PackageException(PackageErrors.FileTooLarge, $"Package file {path} exceeds 25 MiB."); + total += bytes.Length; + if (total > limits.MaximumExpandedBytes) throw new PackageException(PackageErrors.PackageTooLarge, "Expanded package exceeds 64 MiB."); + var entry = zip.CreateEntry(name, CompressionLevel.Optimal); + entry.LastWriteTime = FixedEntryTime; + using var stream = entry.Open(); + stream.Write(bytes, 0, bytes.Length); + } + zip.Dispose(); + buffer.WriteTo(output); + } + + private sealed record Selection(IReadOnlyList<(IReadOnlyList Parts, ArchivePreflight.Entry Entry)> Files, string? RootName); + + /// + /// Chooses the package root like ScriptManager.extractedPackageRoot: the archive root + /// when main.jsx or widget.json sits there, otherwise the only top-level directory. + /// + private static Selection Select(List entries) + { + var files = entries.Where(entry => !entry.IsDirectory && entry.Parts[0] != MacosResourceDirectory && !entry.Parts.Any(PackagePathRules.IsExcluded)).ToList(); + if (files.Any(entry => entry.Parts.Count == 1 && entry.Parts[0] is "main.jsx" or "widget.json")) + { + return new Selection(files.Select(entry => (entry.Parts, entry)).ToList(), null); + } + var roots = files.Where(entry => entry.Parts.Count > 1).Select(entry => entry.Parts[0]).ToHashSet(StringComparer.Ordinal); + if (roots.Count != 1) throw new PackageException(PackageErrors.ArchiveNoPackage, "The archive must contain one widget package."); + var root = roots.First(); + return new Selection( + files.Where(entry => entry.Parts.Count > 1 && entry.Parts[0] == root).Select(entry => ((IReadOnlyList)entry.Parts.Skip(1).ToList(), entry)).ToList(), + root); + } + + private static byte[] ReadArchive(Stream input, ArchiveLimits limits) + { + using var output = new MemoryStream(); + var buffer = new byte[64 * 1024]; + try + { + while (true) + { + var count = input.Read(buffer, 0, buffer.Length); + if (count <= 0) break; + output.Write(buffer, 0, count); + if (output.Length > limits.MaximumArchiveBytes) break; + } + } + catch (Exception error) when (SafeFiles.IsFileError(error)) + { + throw new PackageException(PackageErrors.ArchiveCorrupt, "Archive could not be read.", error); + } + if (output.Length == 0 || output.Length > limits.MaximumArchiveBytes) + { + throw new PackageException(PackageErrors.ArchiveSize, "Archive must be between 1 byte and 32 MiB."); + } + return output.ToArray(); + } + + /// + /// Inflates one entry into a new file, counting real bytes; returns the number written. The + /// length the reader reports is never used: only bytes that were actually produced count. + /// + private static long CopyEntry(Stream input, string target, ArchivePreflight.Entry entry, long totalBefore, ArchiveLimits limits) + { + var crc = new Crc32(); + var count = 0L; + using (var output = new FileStream(target, FileMode.CreateNew, FileAccess.Write, FileShare.None)) + { + var buffer = new byte[16 * 1024]; + while (true) + { + int read; + try + { + read = input.Read(buffer, 0, buffer.Length); + } + catch (Exception error) when (SafeFiles.IsFileError(error) || error is InvalidDataException) + { + throw Corrupt(entry.Name, error); + } + if (read <= 0) break; + count += read; + if (count > entry.UncompressedSize || count > limits.MaximumFileBytes) + { + throw new PackageException(PackageErrors.ArchiveSizeMismatch, $"Archive entry {entry.Name} does not match its declared size."); + } + if (totalBefore + count > limits.MaximumExpandedBytes) + { + throw new PackageException(PackageErrors.ArchiveExpandedTooLarge, "Expanded package exceeds 64 MiB."); + } + crc.Update(buffer, 0, read); + output.Write(buffer, 0, read); + } + } + if (count != entry.UncompressedSize) + { + throw new PackageException(PackageErrors.ArchiveSizeMismatch, $"Archive entry {entry.Name} does not match its declared size."); + } + if (crc.Value != entry.Crc) throw Corrupt(entry.Name, null); + return count; + } + + private static PackageException Corrupt(string name, Exception? cause) => + new(PackageErrors.ArchiveCorrupt, $"Archive entry {name} is corrupt.", cause); + + /// In-memory sink that refuses to grow past the archive byte limit. + private sealed class BoundedOutput(int limit) : MemoryStream + { + public override void WriteByte(byte value) + { + Ensure(1); + base.WriteByte(value); + } + + public override void Write(byte[] buffer, int offset, int count) + { + Ensure(count); + base.Write(buffer, offset, count); + } + + public override void Write(ReadOnlySpan buffer) + { + Ensure(buffer.Length); + base.Write(buffer); + } + + private void Ensure(int extra) + { + if (Position + extra > limit) throw new PackageException(PackageErrors.ArchiveSize, "Archive exceeds 32 MiB."); + } + } +} + +/// CRC-32 (IEEE 802.3, as used by ZIP) over the bytes fed to . +internal sealed class Crc32 +{ + private static readonly uint[] Table = BuildTable(); + private uint state = 0xFFFFFFFF; + + /// The checksum of everything fed so far, as an unsigned 32-bit value. + public long Value => state ^ 0xFFFFFFFF; + + public void Update(byte[] buffer, int offset, int count) + { + var current = state; + for (var index = offset; index < offset + count; index++) current = Table[(current ^ buffer[index]) & 0xFF] ^ (current >> 8); + state = current; + } + + public static long Of(byte[] bytes) + { + var crc = new Crc32(); + crc.Update(bytes, 0, bytes.Length); + return crc.Value; + } + + private static uint[] BuildTable() + { + var table = new uint[256]; + for (uint index = 0; index < 256; index++) + { + var value = index; + for (var bit = 0; bit < 8; bit++) value = (value & 1) != 0 ? 0xEDB88320 ^ (value >> 1) : value >> 1; + table[index] = value; + } + return table; + } +} diff --git a/Windows/src/ScriptWidget.Core/Packages/PackageException.cs b/Windows/src/ScriptWidget.Core/Packages/PackageException.cs new file mode 100644 index 00000000..35561bb0 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Packages/PackageException.cs @@ -0,0 +1,63 @@ +namespace ScriptWidget.Core.Packages; + +/// +/// The single failure type of the package layer. is a stable machine-readable +/// identifier (see ); the message is the human-readable explanation, +/// reusing the Swift wording where the Swift implementation has one. +/// +public sealed class PackageException(string code, string message, Exception? cause = null) : Exception(message, cause) +{ + public string Code { get; } = code; +} + +/// +/// Stable error codes. The manifest codes (invalid_name, invalid_manifest_case, +/// missing_entry) are the ones WidgetPackageManifestValidator and manifest.js +/// emit. The Swift archive preflight reports messages only, so the archive_* codes are +/// defined here. +/// +public static class PackageErrors +{ + /// Archive is empty or larger than the archive byte limit. + public const string ArchiveSize = "archive_size"; + /// Missing or garbled central directory, footer, entry header, or ZIP64 metadata. + public const string ArchiveMalformed = "archive_malformed"; + public const string ArchiveMultiDisk = "archive_multi_disk"; + public const string ArchiveTooManyEntries = "archive_too_many_entries"; + public const string ArchiveEncrypted = "archive_encrypted"; + public const string ArchiveUnsupportedCompression = "archive_unsupported_compression"; + /// + /// Absolute, traversing, backslash, drive-style, NUL, non-UTF-8, or too deeply nested entry + /// path, or one that Windows cannot store safely (see ). + /// + public const string ArchiveUnsafePath = "archive_unsafe_path"; + /// Two entries that are equal after NFC normalization and case folding, or a file/directory clash. + public const string ArchivePathCollision = "archive_path_collision"; + public const string ArchiveSymlink = "archive_symlink"; + public const string ArchiveFileTooLarge = "archive_file_too_large"; + public const string ArchiveExpandedTooLarge = "archive_expanded_too_large"; + public const string ArchiveInvalidRatio = "archive_invalid_ratio"; + /// The inflated byte count differs from the size the central directory declared (zip bomb guard). + public const string ArchiveSizeMismatch = "archive_size_mismatch"; + /// The entry could not be inflated or its CRC-32 does not match. + public const string ArchiveCorrupt = "archive_corrupt"; + /// The archive does not contain exactly one widget package. + public const string ArchiveNoPackage = "archive_no_package"; + + public const string InvalidName = "invalid_name"; + public const string InvalidManifest = "invalid_manifest"; + public const string InvalidManifestCase = "invalid_manifest_case"; + public const string MissingEntry = "missing_entry"; + public const string InvalidEntry = "invalid_entry"; + public const string InvalidPath = "invalid_path"; + public const string PackageExists = "package_exists"; + public const string PackageNotFound = "package_not_found"; + public const string FileNotFound = "file_not_found"; + public const string FileTooLarge = "file_too_large"; + public const string TooManyEntries = "too_many_entries"; + public const string PackageTooLarge = "package_too_large"; + /// Symbolic link, junction or other reparse point, hard link, special file, or case alias inside a package tree. + public const string UnsafeFile = "unsafe_file"; + /// The library or staging directory is unusable (missing, a link, or on another volume). + public const string StorageUnavailable = "storage_unavailable"; +} diff --git a/Windows/src/ScriptWidget.Core/Packages/PackageFileWriter.cs b/Windows/src/ScriptWidget.Core/Packages/PackageFileWriter.cs new file mode 100644 index 00000000..826330b7 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Packages/PackageFileWriter.cs @@ -0,0 +1,94 @@ +namespace ScriptWidget.Core.Packages; + +/// Atomic, path-validated file writes inside one package, ported from ScriptWidgetPackageFiles.write. +internal static class PackageFileWriter +{ + /// + /// Writes to below the package + /// . + /// + /// The path must pass the package path rules (no traversal, absolute path, backslash, NUL, + /// dot-file or __Build component, and nothing Windows would resolve to a drive, UNC + /// share, device or alternate data stream). Every existing directory on the way must be a real + /// directory, never a link or junction, and no component may have a case alias, so a write + /// cannot be redirected outside the package. An existing target must be a singly linked + /// regular file. Content is limited to 25 MiB (256 KiB for widget.json, whose name must + /// be spelled exactly). The bytes go to a hidden temporary file in the target directory, are + /// flushed, and replace the target with one atomic rename: readers see the old or the new + /// content, never a partial file, and a failed write leaves the old content in place. + /// + public static void Write(string root, string relativePath, byte[] bytes) + { + var parts = PackagePathRules.VisibleComponents(relativePath) ?? throw Fail(PackageErrors.InvalidPath, "Invalid package path."); + if (bytes.Length > PackageLimits.MaxFileBytes) throw Fail(PackageErrors.FileTooLarge, "Package file exceeds 25 MiB."); + if (parts.Count == 1 && parts[0].ToLowerInvariant() == PackageManifests.FileName) + { + if (parts[0] != PackageManifests.FileName) throw Fail(PackageErrors.InvalidManifestCase, "Use the exact file name widget.json."); + if (bytes.Length > PackageLimits.MaxMetadataBytes) throw Fail(PackageErrors.FileTooLarge, "widget.json exceeds 256 KiB."); + } + var directory = SafeFiles.RealDirectory(root) ?? throw Fail(PackageErrors.PackageNotFound, "Package directory is unavailable."); + foreach (var name in parts.Take(parts.Count - 1)) + { + RequireUnambiguous(directory, name); + var child = SafeFiles.Child(directory, name); + var info = SafeFiles.Attributes(child); + if (info is null) + { + try + { + // The canonical boundary is checked before anything is created. + if (!SafeFiles.Inside(root, child)) throw Fail(PackageErrors.UnsafeFile, "Package path leaves the package."); + Directory.CreateDirectory(child); + } + catch (Exception error) when (SafeFiles.IsFileError(error)) + { + throw new PackageException(PackageErrors.UnsafeFile, "Package directory could not be created.", error); + } + } + else if (info.Value.IsSymbolicLink || !info.Value.IsDirectory) + { + throw Fail(PackageErrors.UnsafeFile, "Package path crosses a link or a file."); + } + directory = child; + } + var fileName = parts[^1]; + RequireUnambiguous(directory, fileName); + var target = SafeFiles.Child(directory, fileName); + var existing = SafeFiles.Attributes(target); + if (existing is { } found && (found.IsSymbolicLink || !found.IsRegularFile || !SafeFiles.SinglyLinked(target))) + { + throw Fail(PackageErrors.UnsafeFile, "Only regular package files can be replaced."); + } + var temporary = SafeFiles.Child(directory, $".write-{Guid.NewGuid()}"); + try + { + // Canonical boundary check on the file that will be written and the directory that receives it. + if (!SafeFiles.Inside(root, target) || !SafeFiles.Inside(root, temporary)) throw Fail(PackageErrors.UnsafeFile, "Package path leaves the package."); + SafeFiles.WriteNew(temporary, bytes); + File.Move(temporary, target, overwrite: true); + } + catch (Exception error) when (SafeFiles.IsFileError(error)) + { + throw new PackageException(PackageErrors.StorageUnavailable, "Package file could not be saved.", error); + } + finally + { + try + { + File.Delete(temporary); + } + catch (Exception error) when (SafeFiles.IsFileError(error)) + { + // Hidden temporaries are never listed, read or exported. + } + } + } + + private static void RequireUnambiguous(string directory, string name) + { + var siblings = SafeFiles.Names(directory) ?? throw Fail(PackageErrors.UnsafeFile, "Package directory could not be verified."); + if (!SafeFiles.Unambiguous(siblings, name)) throw Fail(PackageErrors.UnsafeFile, "Case-colliding package paths are not allowed."); + } + + private static PackageException Fail(string code, string message) => new(code, message); +} diff --git a/Windows/src/ScriptWidget.Core/Packages/PackageLimits.cs b/Windows/src/ScriptWidget.Core/Packages/PackageLimits.cs new file mode 100644 index 00000000..eba287a3 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Packages/PackageLimits.cs @@ -0,0 +1,52 @@ +namespace ScriptWidget.Core.Packages; + +/// Size and shape limits of the package layer, each taken from the Swift implementation. +public static class PackageLimits +{ + /// ScriptPackageArchivePolicy.maximumArchiveBytes. + public const int MaxArchiveBytes = 32 * 1024 * 1024; + /// ScriptPackageArchivePolicy.maximumExpandedBytes. + public const long MaxExpandedBytes = 64L * 1024 * 1024; + /// ScriptPackageArchivePolicy.maximumFileBytes; also the ScriptWidgetPackageFiles.read/write bound. + public const long MaxFileBytes = 25L * 1024 * 1024; + /// ScriptPackageArchivePolicy.maximumFileCount (files and directories). + public const int MaxFileCount = 500; + /// ScriptPackageArchivePolicy.maximumPathDepth. + public const int MaxPathDepth = 16; + /// Longest path component in UTF-8 bytes (ScriptWidgetPackageFiles.components). + public const int MaxComponentBytes = 255; + /// Longest path component in UTF-16 units, the NTFS file name limit (Windows-specific). + public const int MaxComponentUnits = 255; + /// Longest package-relative path in UTF-16 units, separators included (Windows-specific). + public const int MaxRelativePathUnits = 1024; + /// + /// Longest absolute path this layer reads, writes or publishes, in UTF-16 units + /// (Windows-specific). A package path joined to its install or staging root must fit. + /// + public const int MaxFullPathUnits = 2048; + /// ScriptWidgetMetadataFile.maximumBytes: bound for widget.json and meta.json. + public const int MaxMetadataBytes = 256 * 1024; + /// ScriptWidgetRuntimeContract.maximumSourceBytes: bound for one script source file. + public const int MaxScriptBytes = 512 * 1024; + /// Bound for text reads of non-script files through (as on Android). + public const int MaxTextBytes = 2 * 1024 * 1024; + /// Bound for (as on Android, below the 25 MiB file limit). + public const int MaxBinaryReadBytes = 16 * 1024 * 1024; + /// Package names: at most 80 grapheme clusters and 240 UTF-8 bytes (ScriptWidgetPackageName). + public const int MaxNameGraphemes = 80; + public const int MaxNameBytes = 240; + /// Most directory entries listed before a listing fails closed (ScriptWidgetPackageFiles.names). + public const int MaxDirectoryEntries = 100_000; + /// getValidPackageName tries the suffixes " (1)" through " (10000)". + public const int MaxNameAttempts = 10_000; +} + +/// Archive limits; the defaults are the Swift ScriptPackageArchivePolicy. Tests may shrink them. +public sealed record ArchiveLimits +{ + public int MaximumArchiveBytes { get; init; } = PackageLimits.MaxArchiveBytes; + public long MaximumExpandedBytes { get; init; } = PackageLimits.MaxExpandedBytes; + public long MaximumFileBytes { get; init; } = PackageLimits.MaxFileBytes; + public int MaximumFileCount { get; init; } = PackageLimits.MaxFileCount; + public int MaximumPathDepth { get; init; } = PackageLimits.MaxPathDepth; +} diff --git a/Windows/src/ScriptWidget.Core/Packages/PackageManifests.cs b/Windows/src/ScriptWidget.Core/Packages/PackageManifests.cs new file mode 100644 index 00000000..90d38bc8 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Packages/PackageManifests.cs @@ -0,0 +1,87 @@ +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; + +namespace ScriptWidget.Core.Packages; + +/// +/// Minimal widget.json handling for the library: creating default manifests and reading the +/// display fields. Semantic validation (unknown keys, permissions, entry, versions) is the shared +/// manifest validator's job () +/// and is not repeated here. +/// +public static class PackageManifests +{ + public const string FileName = "widget.json"; + public const string LegacyEntry = "main.jsx"; + public const string LegacyMetadata = "meta.json"; + + private static readonly string[] DiscoveryKeys = ["description", "category", "icon", "preview"]; + + /// Display fields of a manifest; each is null when absent or not a string. + public sealed record Summary(string? Id, string? Name, string? Version); + + /// Parses manifest text as a JSON object, or null. A present manifest that fails here is invalid, never legacy. + public static JsonObject? Parse(string? text) + { + if (text is null) return null; + try + { + if (Json.Parse(text) is not JsonObject manifest) return null; + // Members are materialized lazily; a duplicate key surfaces here rather than later. + _ = manifest.Count; + return manifest; + } + catch (Exception error) when (error is JsonException or ArgumentException or InvalidOperationException) + { + return null; + } + } + + public static Summary Summarize(JsonObject manifest) => new(StringValue(manifest, "id"), StringValue(manifest, "name"), StringValue(manifest, "version")); + + public static string? StringValue(JsonObject manifest, string key) => + manifest.TryGetPropertyValue(key, out var node) && node is JsonValue value && value.TryGetValue(out var text) ? text : null; + + /// A fresh local identifier, as WidgetPackageManifest.newProject generates. + public static string FreshId() => "local." + Guid.NewGuid().ToString("D").ToLowerInvariant(); + + /// WidgetPackageManifest.newProject / newManifest in manifest.js. + public static JsonObject NewManifest(string name) => new() + { + ["formatVersion"] = 2, + ["id"] = FreshId(), + ["name"] = name, + ["version"] = "1.0.0", + ["runtimeVersion"] = "1.0", + ["entry"] = LegacyEntry, + ["supportedFamilies"] = Json.Array(["systemSmall", "systemMedium", "systemLarge"]), + ["permissions"] = new JsonArray(), + ["networkDomains"] = new JsonArray(), + ["plugins"] = new JsonArray(), + ["actions"] = new JsonArray(), + ["controls"] = new JsonArray(), + }; + + /// + /// WidgetPackageManifest.legacy: the manifest given to a package that has main.jsx + /// and no widget.json. Discovery fields are carried over from meta.json when it is + /// well formed; a malformed meta.json is ignored, as in Swift. + /// + public static JsonObject LegacyManifest(string name, string? metadataText) + { + var manifest = NewManifest(name); + manifest["id"] = PackageNames.LegacyId(name); + if (Parse(metadataText) is not { } metadata) return manifest; + foreach (var key in DiscoveryKeys) + { + if (StringValue(metadata, key) is { } text) manifest[key] = text; + } + if (metadata.OptArray("tags") is { } tags && tags.All(tag => tag.IsString())) manifest["tags"] = tags.DeepClone(); + return manifest; + } + + /// The manifest as indented UTF-8 JSON with LF line ends and a final newline. + public static byte[] Encode(JsonObject manifest) => + Encoding.UTF8.GetBytes(Json.Stringify(manifest, indented: true).Replace("\r\n", "\n", StringComparison.Ordinal) + "\n"); +} diff --git a/Windows/src/ScriptWidget.Core/Packages/PackageNames.cs b/Windows/src/ScriptWidget.Core/Packages/PackageNames.cs new file mode 100644 index 00000000..35a523c6 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Packages/PackageNames.cs @@ -0,0 +1,137 @@ +using System.Globalization; +using System.Text; +using System.Text.RegularExpressions; + +namespace ScriptWidget.Core.Packages; + +/// +/// Package (library directory) names, ported from validPackageName in manifest.js and +/// ScriptWidgetPackageName.isValid in Swift. +/// +public static partial class PackageNames +{ + [GeneratedRegex("^[A-Za-z]:", RegexOptions.CultureInvariant)] + private static partial Regex DrivePrefix(); + + [GeneratedRegex("[\\\\/:*?\"<>|\\x00]", RegexOptions.CultureInvariant)] + private static partial Regex ArchiveUnsafe(); + + [GeneratedRegex("[^a-z0-9]+", RegexOptions.CultureInvariant)] + private static partial Regex NotSlug(); + + /// + /// The identity two names share when a case-insensitive or normalizing filesystem would treat + /// them as one entry: NFC, then culture-independent case folding + /// (ScriptWidgetPackageFiles.identity). The fold goes through upper case first, as NTFS + /// compares names by upper-casing them, so no pair Windows treats as one entry gets two + /// identities. Text that is not valid Unicode cannot be normalized and is only case-folded. + /// + public static string Identity(string name) + { + string normalized; + try + { + normalized = name.Normalize(NormalizationForm.FormC); + } + catch (ArgumentException) + { + normalized = name; + } + return normalized.ToUpperInvariant().ToLowerInvariant(); + } + + /// + /// True when may be used as a package name and directory name. + /// + /// Rejects blank names, a leading dot (hidden files, ./..), the reserved + /// __Build cache name in any case, path separators, Windows drive prefixes, and + /// control/format characters other than ZWNJ/ZWJ, so a display name can never become a + /// traversal or a cache path. Length is bounded to 80 grapheme clusters and 240 UTF-8 bytes. + /// Because the name is a directory name in the library, it must also be one Windows stores + /// verbatim (): no colon or other reserved + /// character, no trailing dot or space, no device name. + /// + public static bool IsValid(string? name) + { + if (name is null || string.IsNullOrWhiteSpace(name) || name.StartsWith('.')) return false; + if (Identity(name) == "__build" || name.Contains('/') || name.Contains('\\') || DrivePrefix().IsMatch(name)) return false; + if (HasControlCharacters(name) || !PackagePathRules.WindowsSafe(name)) return false; + return GraphemeCount(name) <= PackageLimits.MaxNameGraphemes && Utf8Length(name) <= PackageLimits.MaxNameBytes; + } + + /// Control, format (except ZWNJ/ZWJ, which joined emoji and some scripts need) or unpaired surrogate characters. + internal static bool HasControlCharacters(string text) + { + for (var index = 0; index < text.Length; index++) + { + var character = text[index]; + if (character is '‌' or '‍') continue; + UnicodeCategory category; + if (char.IsHighSurrogate(character) && index + 1 < text.Length && char.IsLowSurrogate(text[index + 1])) + { + category = CharUnicodeInfo.GetUnicodeCategory(char.ConvertToUtf32(character, text[index + 1])); + index += 1; + } + else + { + category = CharUnicodeInfo.GetUnicodeCategory(character); + } + if (category is UnicodeCategory.Control or UnicodeCategory.Format or UnicodeCategory.Surrogate) return true; + } + return false; + } + + internal static int Utf8Length(string text) => Encoding.UTF8.GetByteCount(text); + + internal static int GraphemeCount(string text) => GraphemeBoundaries(text).Length - 1; + + /// + /// followed by , with the stem shortened by + /// whole grapheme clusters until the result fits the name limits. This is the truncation + /// getValidPackageName and duplicateScript apply in Swift. + /// + internal static string WithSuffix(string name, string suffix) + { + var bounds = GraphemeBoundaries(name); + var keep = Math.Min(bounds.Length - 1, Math.Max(0, PackageLimits.MaxNameGraphemes - GraphemeCount(suffix))); + while (keep > 0 && Utf8Length(name[..bounds[keep]] + suffix) > PackageLimits.MaxNameBytes) keep -= 1; + return name[..bounds[keep]] + suffix; + } + + /// WidgetPackageManifest.legacy: the identifier of a package that had no widget.json. + internal static string LegacyId(string name) + { + var slug = NotSlug().Replace(name.ToLowerInvariant(), "-").Trim('-'); + return "legacy." + (slug.Length == 0 ? "widget" : slug); + } + + /// packageDirectoryName in archive.js: a root directory name every platform's importer accepts. + internal static string ArchiveRootName(string name) + { + var cleaned = ArchiveUnsafe().Replace(name, "-").TrimStart('.').Trim(); + if (cleaned.Length > 80) cleaned = cleaned[..80]; + return cleaned.Length == 0 ? "Widget" : cleaned; + } + + /// + /// The library directory name for a package whose manifest name Windows cannot store as a + /// directory, such as "Ratio 16:9" or "Notes?". Characters Windows reserves become a hyphen + /// and trailing dots and spaces are dropped; the manifest keeps its own name, which is what + /// the user sees. A name that is still not valid (a device name such as "CON", a hidden or + /// blank name) is returned unchanged so the caller refuses it. + /// + public static string DirectoryName(string name) + { + if (IsValid(name)) return name; + // Path separators and leading dots are refused on every platform; they are not repaired here. + if (name.Contains('/') || name.Contains('\\') || name.StartsWith('.')) return name; + var cleaned = WindowsReserved().Replace(name, "-").TrimEnd('.', ' '); + return IsValid(cleaned) ? cleaned : name; + } + + [GeneratedRegex("[:*?\"<>|]", RegexOptions.CultureInvariant)] + private static partial Regex WindowsReserved(); + + /// Start index of every grapheme cluster, followed by the text length. + private static int[] GraphemeBoundaries(string text) => [.. StringInfo.ParseCombiningCharacters(text), text.Length]; +} diff --git a/Windows/src/ScriptWidget.Core/Packages/PackagePathRules.cs b/Windows/src/ScriptWidget.Core/Packages/PackagePathRules.cs new file mode 100644 index 00000000..f8212eea --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Packages/PackagePathRules.cs @@ -0,0 +1,79 @@ +using System.Text.RegularExpressions; +using ScriptWidget.Core.Runtime; + +namespace ScriptWidget.Core.Packages; + +/// +/// Package-relative path rules, ported from ScriptWidgetPackageFiles.components in Swift, +/// plus the names Windows cannot store safely (). +/// +internal static partial class PackagePathRules +{ + [GeneratedRegex("^[A-Za-z]:", RegexOptions.CultureInvariant)] + private static partial Regex DrivePrefix(); + + private static readonly HashSet ReservedDeviceNames = new(StringComparer.Ordinal) { "CON", "PRN", "AUX", "NUL", "CONIN$", "CONOUT$" }; + + /// + /// Splits a package-relative path into components, or returns null when it is unsafe. + /// + /// The path must already be in normal form: no leading slash, no empty, . or .. + /// component, no backslash and no NUL (so nothing can name a location outside the package), no + /// Windows drive prefix, at most 16 components of at most 255 UTF-8 bytes each, and no control + /// characters. On Windows every component must also pass and the + /// whole path is limited to 1024 UTF-16 units, so UNC, drive, device and stream syntax can + /// never reach the filesystem. + /// + public static IReadOnlyList? Components(string? relative) + { + var normalized = PackagePaths.Normalize(relative); + if (normalized is null || normalized != relative || DrivePrefix().IsMatch(normalized)) return null; + if (normalized.Length > PackageLimits.MaxRelativePathUnits) return null; + var parts = normalized.Split('/'); + if (parts.Length > PackageLimits.MaxPathDepth || !parts.All(ValidComponent)) return null; + return parts; + } + + /// + /// , additionally refusing dot-files and the generated __Build + /// cache at any depth. Those are never part of the authored package, so they are neither + /// listed, read nor written. + /// + public static IReadOnlyList? VisibleComponents(string? relative) => + Components(relative) is { } parts && !parts.Any(IsExcluded) ? parts : null; + + public static bool ValidComponent(string part) => + part.Length > 0 && part != "." && part != ".." && !part.Contains('/') && !part.Contains('\\') && + PackageNames.Utf8Length(part) <= PackageLimits.MaxComponentBytes && !PackageNames.HasControlCharacters(part) && WindowsSafe(part); + + public static bool IsExcluded(string part) => part.StartsWith('.') || PackageNames.Identity(part) == "__build"; + + /// + /// True when Windows stores as a file or directory of exactly that + /// name. Refused: more than 255 UTF-16 units; a trailing dot or space (Win32 strips them, so + /// the name would alias another file); a colon (alternate data stream or drive syntax); the + /// characters < > " | ? *, either separator and U+0000–U+001F; and the reserved + /// device names CON, PRN, AUX, NUL, COM0–COM9, LPT0–LPT9 (also with a superscript digit), + /// CONIN$ and CONOUT$ in any case, with or without an extension. + /// + public static bool WindowsSafe(string part) + { + if (part.Length == 0 || part.Length > PackageLimits.MaxComponentUnits) return false; + if (part[^1] is '.' or ' ') return false; + foreach (var character in part) + { + if (character < 0x20 || character is '<' or '>' or ':' or '"' or '|' or '?' or '*' or '/' or '\\') return false; + } + return !IsReservedDeviceName(part); + } + + /// Win32 resolves these names to devices whatever directory or extension they carry. + private static bool IsReservedDeviceName(string part) + { + var dot = part.IndexOf('.'); + var stem = (dot < 0 ? part : part[..dot]).TrimEnd(' ').ToUpperInvariant(); + if (ReservedDeviceNames.Contains(stem)) return true; + if (stem.Length != 4 || (!stem.StartsWith("COM", StringComparison.Ordinal) && !stem.StartsWith("LPT", StringComparison.Ordinal))) return false; + return stem[3] is (>= '0' and <= '9') or '¹' or '²' or '³'; + } +} diff --git a/Windows/src/ScriptWidget.Core/Packages/PackageStore.cs b/Windows/src/ScriptWidget.Core/Packages/PackageStore.cs new file mode 100644 index 00000000..b97650d7 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Packages/PackageStore.cs @@ -0,0 +1,401 @@ +using System.Text; +using System.Text.Json.Nodes; +using System.Text.RegularExpressions; +using ScriptWidget.Core.Runtime; + +namespace ScriptWidget.Core.Packages; + +/// +/// One installed package as shown in the library. The manifest fields are display values read +/// leniently from widget.json; they are null when the manifest is absent, unreadable or the +/// field is not a string. They are not validated. +/// +/// The package (directory) name. +/// Absolute path of the package directory. +/// True when any spelling of widget.json is present; such a package is never treated as legacy. +public sealed record PackageInfo(string Name, string Directory, bool HasManifest, string? ManifestId, string? ManifestName, string? ManifestVersion); + +/// +/// The library of installed packages, one directory per package under +/// libraryRoot/<PackageName>/. Ported from ScriptManager in Swift. +/// +/// Everything that enters the library is prepared in a unique directory under the staging root +/// and published with one atomic rename, so a failed create, import or duplicate leaves no +/// partial package. The staging root must be on the same volume as the library root and must not +/// be inside it. Package names follow , and a name is usable +/// only when no other library entry has the same identity after case folding and NFC +/// normalization. +/// +/// Mutations are serialized per store instance; use one instance per library. +/// +public sealed partial class PackageStore(string libraryRoot, string stagingRoot, ArchiveLimits? limits = null) +{ + private readonly string library = SafeFiles.Root(libraryRoot); + private readonly string staging = SafeFiles.Root(stagingRoot); + private readonly ArchiveLimits limits = limits ?? new ArchiveLimits(); + private readonly Lock gate = new(); + + [GeneratedRegex(@"\.(js|jsx)$", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)] + private static partial Regex EntryFile(); + + /// Installed packages sorted by name. Entries that are links, files, invalid or ambiguous names are not packages. + public IReadOnlyList List() + { + var names = LibraryNames(); + var result = new List(); + foreach (var name in names.Order(StringComparer.Ordinal)) + { + if (!PackageNames.IsValid(name) || !SafeFiles.Unambiguous(names, name)) continue; + if (SafeFiles.RealDirectory(SafeFiles.Child(library, name)) is { } directory) result.Add(Info(name, directory)); + } + return result; + } + + /// Read-only, boundary-checked view of an installed package. + public DiskPackageFiles Files(string name) => new(Existing(name)); + + /// + /// if it is free, otherwise the first free "<stem> (n)" + /// for n in 1…10000, with the stem shortened to keep the name limits + /// (ScriptManager.getValidPackageName). + /// + /// invalid_name when is invalid or no name is free. + public string UniqueName(string recommended) + { + lock (gate) + { + RequireValid(recommended); + var identities = LibraryNames().Select(PackageNames.Identity).ToHashSet(StringComparer.Ordinal); + bool Available(string name) => PackageNames.IsValid(name) && !identities.Contains(PackageNames.Identity(name)); + if (Available(recommended)) return recommended; + for (var index = 1; index <= PackageLimits.MaxNameAttempts; index++) + { + var candidate = PackageNames.WithSuffix(recommended, $" ({index})"); + if (Available(candidate)) return candidate; + } + throw new PackageException(PackageErrors.InvalidName, "No free package name is available."); + } + } + + /// + /// Creates a package named exactly from + /// and a manifest. Without a default Package 2.0 manifest is + /// generated. The source is written to the manifest's entry (default main.jsx). + /// The package becomes visible only after source and manifest are fully written. + /// + /// must be a JSON object of at most 256 KiB with a safe + /// .js/.jsx entry path; its semantic validation is the caller's responsibility. + /// + /// invalid_name, package_exists, invalid_manifest, invalid_entry, file_too_large. + public PackageInfo Create(string name, string mainSource, string? manifestJson = null) + { + RequireValid(name); + var source = Encoding.UTF8.GetBytes(mainSource); + if (source.Length > PackageLimits.MaxScriptBytes) throw new PackageException(PackageErrors.FileTooLarge, "Script source exceeds 512 KiB."); + byte[] manifestBytes; + var entry = PackageManifests.LegacyEntry; + if (manifestJson is null) + { + manifestBytes = PackageManifests.Encode(PackageManifests.NewManifest(name)); + } + else + { + manifestBytes = Encoding.UTF8.GetBytes(manifestJson); + if (manifestBytes.Length > PackageLimits.MaxMetadataBytes) throw new PackageException(PackageErrors.FileTooLarge, "widget.json exceeds 256 KiB."); + var manifest = PackageManifests.Parse(manifestJson) ?? throw new PackageException(PackageErrors.InvalidManifest, "widget.json is invalid."); + entry = PackageManifests.StringValue(manifest, "entry") ?? throw new PackageException(PackageErrors.InvalidManifest, "widget.json is invalid."); + if (PackagePathRules.VisibleComponents(entry) is null || entry == PackageManifests.FileName || !EntryFile().IsMatch(entry)) + { + throw new PackageException(PackageErrors.InvalidEntry, "entry must be a package-relative .js or .jsx file."); + } + } + return Staged("create", prepared => + { + PackageFileWriter.Write(prepared, entry, source); + PackageFileWriter.Write(prepared, PackageManifests.FileName, manifestBytes); + PackageTree.Validate(prepared, limits); + lock (gate) + { + if (!Free(name)) throw new PackageException(PackageErrors.PackageExists, "A package with this name already exists."); + SafeFiles.Publish(prepared, SafeFiles.Child(library, name)); + } + return Info(name, SafeFiles.Child(library, name)); + }); + } + + /// + /// Imports a .swt archive and returns the installed package. + /// + /// The archive is preflighted and extracted by . The + /// installed directory name comes from the manifest's name, never from the archive file + /// name; on a collision it gets the next free " (n)" suffix (). + /// A package without widget.json is legacy: it must contain main.jsx and receives + /// a generated manifest named after the archive's top-level directory, or + /// when the files sit at the archive root. A present manifest + /// is authoritative: a wrongly cased, non-UTF-8, non-object or nameless widget.json + /// fails the import instead of being reinterpreted as legacy. + /// + /// runs on the staged package before publication (plug the shared + /// manifest validator, , in here); if it throws, + /// nothing is installed. A rejected import leaves nothing in the library or the staging + /// directory. + /// + public PackageInfo ImportArchive(Stream input, string? suggestedName, Action? validate = null) + { + using var extracted = PackageArchive.Extract(input, staging, limits); + var prepared = extracted.Directory; + var files = new DiskPackageFiles(prepared); + string name; + if (ManifestPresent(prepared)) + { + name = PackageManifests.StringValue(ReadManifest(files), "name") ?? throw new PackageException(PackageErrors.InvalidManifest, "widget.json is invalid."); + // A package made on another platform may be named with characters Windows reserves. + name = PackageNames.DirectoryName(name); + } + else + { + if (!files.Exists(PackageManifests.LegacyEntry)) throw new PackageException(PackageErrors.MissingEntry, "Entry file main.jsx does not exist."); + name = extracted.RootName ?? (PackageNames.IsValid(suggestedName) ? suggestedName! : "Imported Widget"); + RequireValid(name); + PackageFileWriter.Write(prepared, PackageManifests.FileName, PackageManifests.Encode(LegacyManifest(name, files))); + } + RequireValid(name); + validate?.Invoke(files); + lock (gate) + { + var installed = UniqueName(name); + extracted.MoveTo(SafeFiles.Child(library, installed)); + return Info(installed, SafeFiles.Child(library, installed)); + } + } + + /// + /// Exports an installed package as a .swt archive under one top-level directory named + /// after the package. __Build, dot-files and links are not exported. A legacy package is + /// exported with a generated widget.json (the installed package is not modified); a + /// package whose present manifest is unreadable is not exported. + /// + public void ExportArchive(string name, Stream output) + { + var directory = Existing(name); + var files = new DiskPackageFiles(directory); + var extras = new Dictionary(); + if (ManifestPresent(directory)) + { + ReadManifest(files); + } + else + { + if (!files.Exists(PackageManifests.LegacyEntry)) throw new PackageException(PackageErrors.MissingEntry, "Entry file main.jsx does not exist."); + extras[PackageManifests.FileName] = PackageManifests.Encode(LegacyManifest(name, files)); + } + PackageArchive.Export(directory, name, output, extras, limits); + } + + /// + /// Copies a package to a new "<name> Remix" package (made unique like + /// ) and returns it. The source tree must be free of links and special + /// files. __Build and dot-files are not copied. A Package 2.0 copy gets a fresh + /// id and its new name; a legacy package stays legacy + /// (ScriptManager.duplicateScript). + /// + public PackageInfo Duplicate(string name) + { + var source = Existing(name); + PackageTree.Validate(source, limits); + var files = new DiskPackageFiles(source); + var manifest = ManifestPresent(source) ? ReadManifest(files) : null; + return Staged("duplicate", prepared => + { + foreach (var path in files.Paths()) + { + var bytes = files.ReadBytes(path, limits.MaximumFileBytes) ?? throw new PackageException(PackageErrors.UnsafeFile, "Package file could not be verified."); + PackageFileWriter.Write(prepared, path, bytes); + } + lock (gate) + { + var copy = UniqueName(PackageNames.WithSuffix(name, " Remix")); + if (manifest is not null) + { + manifest["id"] = PackageManifests.FreshId(); + manifest["name"] = copy; + PackageFileWriter.Write(prepared, PackageManifests.FileName, PackageManifests.Encode(manifest)); + } + PackageTree.Validate(prepared, limits); + SafeFiles.Publish(prepared, SafeFiles.Child(library, copy)); + return Info(copy, SafeFiles.Child(library, copy)); + } + }); + } + + /// + /// Renames a package directory atomically. The manifest is left unchanged, as in Swift. + /// must be valid and free; a change of case only is refused as ambiguous. + /// + public PackageInfo Rename(string name, string newName) + { + lock (gate) + { + var source = Existing(name); + RequireValid(newName); + if (name != newName) + { + if (!Free(newName)) throw new PackageException(PackageErrors.PackageExists, "A package with this name already exists."); + SafeFiles.Publish(source, SafeFiles.Child(library, newName)); + } + return Info(newName, SafeFiles.Child(library, newName)); + } + } + + /// + /// Deletes a package. The directory first leaves the library with one atomic rename into + /// staging, so it disappears at once; its contents are then removed without following links. + /// + public void Delete(string name) + { + lock (gate) + { + var directory = Existing(name); + try + { + var work = SafeFiles.NewWorkDirectory(staging, "delete"); + try + { + Directory.Move(directory, SafeFiles.Child(work, "package")); + } + finally + { + SafeFiles.DeleteTree(work); + } + } + catch (Exception error) when (SafeFiles.IsFileError(error) || error is PackageException) + { + SafeFiles.DeleteTree(directory); + } + if (SafeFiles.Occupied(directory)) throw new PackageException(PackageErrors.StorageUnavailable, "The widget could not be deleted."); + } + } + + /// + /// Atomically writes one file of an installed package. See + /// for the path, link and size rules. A new file is refused once the package holds 500 files. + /// + public void WriteFile(string name, string relativePath, byte[] bytes) + { + lock (gate) + { + var directory = Existing(name); + var files = new DiskPackageFiles(directory); + if (!files.Exists(relativePath) && files.Paths().Count >= limits.MaximumFileCount) + { + throw new PackageException(PackageErrors.TooManyEntries, "Package contains too many entries."); + } + PackageFileWriter.Write(directory, relativePath, bytes); + } + } + + /// + /// Bytes of one file of an installed package, at most 25 MiB, under the rules of . + /// + /// invalid_path for an unsafe path, file_not_found when the file is missing, refused or too large. + public byte[] ReadFile(string name, string relativePath) + { + var directory = Existing(name); + if (PackagePathRules.VisibleComponents(relativePath) is null) throw new PackageException(PackageErrors.InvalidPath, "Invalid package path."); + return new DiskPackageFiles(directory).ReadBytes(relativePath, PackageLimits.MaxFileBytes) + ?? throw new PackageException(PackageErrors.FileNotFound, $"File not found: {relativePath}"); + } + + /// Runs on a fresh staging directory that is removed afterwards unless it was published. + private T Staged(string prefix, Func build) + { + var work = SafeFiles.NewWorkDirectory(staging, prefix); + try + { + var prepared = SafeFiles.Child(work, "package"); + Directory.CreateDirectory(prepared); + return build(prepared); + } + catch (Exception error) when (SafeFiles.IsFileError(error)) + { + throw new PackageException(PackageErrors.StorageUnavailable, "The package could not be prepared.", error); + } + finally + { + SafeFiles.DeleteTree(work); + } + } + + private List LibraryNames() + { + List? names = null; + try + { + Directory.CreateDirectory(library); + if (SafeFiles.RealDirectory(library) is { } directory) names = SafeFiles.Names(directory); + } + catch (Exception error) when (SafeFiles.IsFileError(error)) + { + names = null; + } + return names ?? throw new PackageException(PackageErrors.StorageUnavailable, "The widget library is unavailable."); + } + + private static void RequireValid(string name) + { + if (!PackageNames.IsValid(name)) throw new PackageException(PackageErrors.InvalidName, "Invalid package name."); + } + + /// True when no library entry shares the identity of . + private bool Free(string name) + { + var identity = PackageNames.Identity(name); + return !LibraryNames().Any(other => PackageNames.Identity(other) == identity); + } + + /// Directory of an installed package: a valid, exactly spelled, unambiguous name that is a real directory. + private string Existing(string name) + { + RequireValid(name); + var names = LibraryNames(); + var directory = names.Contains(name) && SafeFiles.Unambiguous(names, name) ? SafeFiles.RealDirectory(SafeFiles.Child(library, name)) : null; + return directory ?? throw new PackageException(PackageErrors.PackageNotFound, "Package not found."); + } + + /// + /// True when the package has a manifest. Any other spelling of widget.json is an error: + /// on a case-insensitive platform it would be the manifest, so it must not be skipped as legacy. + /// + private static bool ManifestPresent(string directory) + { + var names = SafeFiles.Names(directory) ?? throw new PackageException(PackageErrors.UnsafeFile, "Package directory could not be verified."); + var spellings = names.Where(name => name.ToLowerInvariant() == PackageManifests.FileName).ToList(); + if (spellings.Any(name => name != PackageManifests.FileName)) throw new PackageException(PackageErrors.InvalidManifestCase, "Use the exact file name widget.json."); + return spellings.Count > 0; + } + + private static JsonObject ReadManifest(DiskPackageFiles files) + { + var text = files.ReadBytes(PackageManifests.FileName, PackageLimits.MaxMetadataBytes) is { } bytes ? SafeFiles.DecodeUtf8(bytes) : null; + return PackageManifests.Parse(text) ?? throw new PackageException(PackageErrors.InvalidManifest, "widget.json is invalid."); + } + + private static JsonObject LegacyManifest(string name, DiskPackageFiles files) + { + var metadata = files.ReadBytes(PackageManifests.LegacyMetadata, PackageLimits.MaxMetadataBytes) is { } bytes ? SafeFiles.DecodeUtf8(bytes) : null; + return PackageManifests.LegacyManifest(name, metadata); + } + + private static PackageInfo Info(string name, string directory) + { + var names = SafeFiles.Names(directory) ?? []; + var hasManifest = names.Any(entry => entry.ToLowerInvariant() == PackageManifests.FileName); + PackageManifests.Summary? summary = null; + if (names.Contains(PackageManifests.FileName)) + { + var text = new DiskPackageFiles(directory).ReadBytes(PackageManifests.FileName, PackageLimits.MaxMetadataBytes) is { } bytes ? SafeFiles.DecodeUtf8(bytes) : null; + if (PackageManifests.Parse(text) is { } manifest) summary = PackageManifests.Summarize(manifest); + } + return new PackageInfo(name, directory, hasManifest, summary?.Id, summary?.Name, summary?.Version); + } +} diff --git a/Windows/src/ScriptWidget.Core/Packages/PackageTree.cs b/Windows/src/ScriptWidget.Core/Packages/PackageTree.cs new file mode 100644 index 00000000..dff0af75 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Packages/PackageTree.cs @@ -0,0 +1,56 @@ +namespace ScriptWidget.Core.Packages; + +/// Whole-tree verification, ported from ScriptManager.validateExtractedTree in Swift. +internal static class PackageTree +{ + /// + /// Verifies a package directory before it is published or copied. + /// + /// The tree may hold only directories and singly linked regular files: a symbolic link, + /// junction, hard link or special file could reach outside the package. Names must be valid + /// components with no case or normalization collisions, nesting is limited to 16 components, + /// and the entry count, per-file size and total size limits are those of the archive policy. + /// Sizes are the ones the filesystem reports for files this layer wrote or is about to copy + /// with a bound. + /// + public static void Validate(string root, ArchiveLimits? limits = null) + { + if (SafeFiles.RealDirectory(root) is null) throw Fail(PackageErrors.UnsafeFile, "Package directory could not be verified."); + Visit(root, 1, new State(limits ?? new ArchiveLimits())); + } + + private sealed class State(ArchiveLimits limits) + { + public ArchiveLimits Limits { get; } = limits; + public int Count; + public long Total; + } + + private static void Visit(string directory, int depth, State state) + { + var names = SafeFiles.Names(directory) ?? throw Fail(PackageErrors.UnsafeFile, "Package file could not be verified."); + foreach (var name in names) + { + if (depth > state.Limits.MaximumPathDepth || !PackagePathRules.ValidComponent(name)) throw Fail(PackageErrors.InvalidPath, "Invalid package path."); + if (!SafeFiles.Unambiguous(names, name)) throw Fail(PackageErrors.UnsafeFile, "Case-colliding package paths are not allowed."); + var child = SafeFiles.Child(directory, name); + var info = SafeFiles.Attributes(child) ?? throw Fail(PackageErrors.UnsafeFile, "Package file could not be verified."); + if (info.IsSymbolicLink) throw Fail(PackageErrors.UnsafeFile, "Symbolic links are not allowed."); + if (!info.IsDirectory && !info.IsRegularFile) throw Fail(PackageErrors.UnsafeFile, "Only directories and regular files are allowed."); + state.Count += 1; + if (state.Count > state.Limits.MaximumFileCount) throw Fail(PackageErrors.TooManyEntries, "Package contains too many entries."); + if (info.IsDirectory) + { + Visit(child, depth + 1, state); + continue; + } + if (!SafeFiles.SinglyLinked(child)) throw Fail(PackageErrors.UnsafeFile, "Hard-linked files are not allowed."); + var size = info.Size; + if (size < 0 || size > state.Limits.MaximumFileBytes) throw Fail(PackageErrors.FileTooLarge, "Package file exceeds 25 MiB."); + if (state.Total > state.Limits.MaximumExpandedBytes - size) throw Fail(PackageErrors.PackageTooLarge, "Expanded package exceeds 64 MiB."); + state.Total += size; + } + } + + private static PackageException Fail(string code, string message) => new(code, message); +} diff --git a/Windows/src/ScriptWidget.Core/Packages/SafeFiles.cs b/Windows/src/ScriptWidget.Core/Packages/SafeFiles.cs new file mode 100644 index 00000000..b939dfe6 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Packages/SafeFiles.cs @@ -0,0 +1,377 @@ +using System.Runtime.InteropServices; +using System.Text; +using Microsoft.Win32.SafeHandles; + +namespace ScriptWidget.Core.Packages; + +/// What reports about a path itself, never about a link target. +/// A directory that is not a reparse point. +/// Any reparse point: a symbolic link, junction or mount point. +/// A plain file: not a directory, reparse point or device. +/// File size in bytes as the filesystem reports it. +internal readonly record struct FileEntry(bool IsDirectory, bool IsSymbolicLink, bool IsRegularFile, long Size); + +/// +/// Link-refusing filesystem primitives shared by the package layer. Every lookup reads the +/// attributes of the path itself, so a reparse point (symbolic link, junction, mount point) is +/// seen as a link and never as its target; files are opened with +/// FILE_FLAG_OPEN_REPARSE_POINT and checked again on the open handle. +/// +/// Unlike the Swift implementation these cannot hold directory handles (openat), so a check +/// and the following open are separate system calls. Package directories live in app-private +/// storage. Paths are joined by plain concatenation (), never with +/// Path.Combine, which would let a rooted name replace the directory. +/// +internal static partial class SafeFiles +{ + private static readonly EnumerationOptions EverythingInDirectory = new() + { + AttributesToSkip = 0, + IgnoreInaccessible = false, + RecurseSubdirectories = false, + ReturnSpecialDirectories = false, + MatchType = MatchType.Simple, + }; + + private static readonly UTF8Encoding StrictUtf8 = new(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true); + + /// The absolute form of a caller-supplied root directory, without a trailing separator. + public static string Root(string path) => Path.TrimEndingDirectorySeparator(Path.GetFullPath(path)); + + /// directly below . The name must be a validated component. + public static string Child(string directory, string name) => + directory.EndsWith(Path.DirectorySeparatorChar) ? directory + name : directory + Path.DirectorySeparatorChar + name; + + /// Attributes of itself (never of a link target), or null when it is absent or unreadable. + public static FileEntry? Attributes(string path) + { + try + { + var attributes = File.GetAttributes(path); + var link = attributes.HasFlag(FileAttributes.ReparsePoint); + var directory = attributes.HasFlag(FileAttributes.Directory); + var regular = !link && !directory && !attributes.HasFlag(FileAttributes.Device); + return new FileEntry(directory && !link, link, regular, regular ? new FileInfo(path).Length : 0); + } + catch (Exception error) when (IsFileError(error)) + { + return null; + } + } + + /// True when something, including a dangling link, occupies . + public static bool Occupied(string path) + { + try + { + File.GetAttributes(path); + return true; + } + catch (Exception error) when (error is FileNotFoundException or DirectoryNotFoundException) + { + return false; + } + catch (Exception error) when (IsFileError(error)) + { + // Present but unreadable still occupies the name. + return true; + } + } + + /// when it is a real directory rather than a link to one, else null. + public static string? RealDirectory(string path) => Attributes(path) is { IsDirectory: true, IsSymbolicLink: false } ? path : null; + + /// Entry names of , or null when it cannot be listed or is implausibly large. + public static List? Names(string directory) + { + try + { + var result = new List(); + foreach (var entry in Directory.EnumerateFileSystemEntries(directory, "*", EverythingInDirectory)) + { + result.Add(Path.GetFileName(entry)); + if (result.Count > PackageLimits.MaxDirectoryEntries) return null; + } + return result; + } + catch (Exception error) when (IsFileError(error)) + { + return null; + } + } + + /// + /// False when holds a different spelling of + /// (another case or Unicode form). Such aliases resolve to one file on some filesystems and + /// two on others, so they are refused. + /// + public static bool Unambiguous(IReadOnlyList names, string name) + { + var identity = PackageNames.Identity(name); + return !names.Any(other => other != name && PackageNames.Identity(other) == identity); + } + + /// + /// False for a regular file with more than one hard link: a second link could expose or alter + /// a file outside the package. Also false when the link count cannot be read. + /// + public static bool SinglyLinked(string path) + { + using var handle = Open(path, access: 0); + return handle is not null && GetFileInformationByHandle(handle, out var information) && information.NumberOfLinks <= 1; + } + + /// + /// Resolves below to an existing regular file, or null. + /// + /// Each component must be spelled exactly as stored, must not be a link, and must not have a + /// case alias; intermediate components must be directories. The canonical path of the result + /// must still be inside the canonical root, which catches anything the walk could have missed. + /// + public static string? ExistingFile(string root, IReadOnlyList parts) + { + var directory = RealDirectory(root); + if (directory is null) return null; + for (var index = 0; index < parts.Count; index++) + { + var name = parts[index]; + var siblings = Names(directory); + if (siblings is null || !siblings.Contains(name) || !Unambiguous(siblings, name)) return null; + var child = Child(directory, name); + if (Attributes(child) is not { } info || info.IsSymbolicLink) return null; + if (index < parts.Count - 1) + { + if (!info.IsDirectory) return null; + directory = child; + } + else + { + if (!info.IsRegularFile || !SinglyLinked(child) || !Inside(root, child)) return null; + return child; + } + } + return null; + } + + /// + /// True when is in canonical form and strictly below the canonical + /// form of . The canonical form is Path.GetFullPath; a path that + /// Win32 would rewrite (a .. component, a trailing dot or space, a device name) is not + /// its own canonical form and is refused, as is one longer than 2048 UTF-16 units. The prefix + /// test includes the separator, so root2 is never inside root. Links are not + /// resolved here: callers refuse every reparse point between root and path. + /// + public static bool Inside(string root, string path) + { + try + { + var canonicalRoot = Root(root); + var canonical = Path.GetFullPath(path); + if (canonical != path || canonical.Length > PackageLimits.MaxFullPathUnits) return false; + var prefix = canonicalRoot.EndsWith(Path.DirectorySeparatorChar) ? canonicalRoot : canonicalRoot + Path.DirectorySeparatorChar; + return canonical.Length > prefix.Length && canonical.StartsWith(prefix, StringComparison.OrdinalIgnoreCase); + } + catch (Exception error) when (IsFileError(error) || error is ArgumentException) + { + return false; + } + } + + /// + /// Reads without following a link, or returns null when it holds more + /// than or is not a plain file. + /// + public static byte[]? ReadBounded(string file, long maximumBytes) + { + try + { + using var handle = Open(file, GenericRead); + if (handle is null || !GetFileInformationByHandle(handle, out var information)) return null; + if ((information.FileAttributes & (uint)(FileAttributes.ReparsePoint | FileAttributes.Directory | FileAttributes.Device)) != 0) return null; + using var input = new FileStream(handle, FileAccess.Read, bufferSize: 0, isAsync: false); + using var output = new MemoryStream(); + var buffer = new byte[16 * 1024]; + var total = 0L; + while (true) + { + var count = input.Read(buffer, 0, buffer.Length); + if (count <= 0) break; + total += count; + // The size is counted while reading, never taken from metadata that may change. + if (total > maximumBytes) return null; + output.Write(buffer, 0, count); + } + return output.ToArray(); + } + catch (Exception error) when (IsFileError(error)) + { + return null; + } + } + + /// Strict UTF-8 decoding: malformed input yields null instead of replacement characters. + public static string? DecodeUtf8(byte[] bytes) => DecodeUtf8(bytes, 0, bytes.Length); + + public static string? DecodeUtf8(byte[] bytes, int offset, int length) + { + try + { + return StrictUtf8.GetString(bytes, offset, length); + } + catch (ArgumentException) + { + return null; + } + } + + /// Creates exclusively, writes and flushes them to storage. + public static void WriteNew(string file, byte[] bytes) + { + using var stream = new FileStream(file, FileMode.CreateNew, FileAccess.Write, FileShare.None, bufferSize: 0); + stream.Write(bytes, 0, bytes.Length); + stream.Flush(flushToDisk: true); + } + + /// Creates a uniquely named private working directory below . + public static string NewWorkDirectory(string stagingRoot, string prefix) + { + try + { + Directory.CreateDirectory(stagingRoot); + if (RealDirectory(stagingRoot) is null) throw new IOException("Staging directory is a link"); + var work = Child(stagingRoot, $"{prefix}-{Guid.NewGuid()}"); + if (Occupied(work)) throw new IOException("Staging work directory already exists"); + Directory.CreateDirectory(work); + return work; + } + catch (Exception error) when (IsFileError(error)) + { + throw new PackageException(PackageErrors.StorageUnavailable, "The staging directory is unavailable.", error); + } + } + + /// + /// Publishes a prepared directory with one atomic rename, so observers see either nothing or + /// the complete package. must be free; the rename itself also + /// refuses an existing destination, and callers hold the store lock. + /// + public static void Publish(string prepared, string destination) + { + if (Occupied(destination)) throw new PackageException(PackageErrors.PackageExists, "A package with this name already exists."); + if (destination.Length > PackageLimits.MaxFullPathUnits) throw new PackageException(PackageErrors.StorageUnavailable, "The package could not be published."); + if (!string.Equals(Path.GetPathRoot(prepared), Path.GetPathRoot(destination), StringComparison.OrdinalIgnoreCase)) + { + throw new PackageException(PackageErrors.StorageUnavailable, "Staging and library directories must share a filesystem."); + } + try + { + Directory.Move(prepared, destination); + } + catch (Exception error) when (IsFileError(error)) + { + throw new PackageException(PackageErrors.StorageUnavailable, "The package could not be published.", error); + } + } + + /// + /// Removes and everything below it. Links are removed, never followed. + /// Best effort. Names are passed to Win32 verbatim, so entries it would otherwise rewrite + /// (a trailing dot or space) are removed too. + /// + public static void DeleteTree(string path) + { + if (!Occupied(path)) return; + try + { + Delete(Verbatim(Path.GetFullPath(path))); + } + catch (Exception error) when (IsFileError(error)) + { + // Leftovers stay inside the private staging directory and are retried by later cleanups. + } + } + + private static void Delete(string path) + { + var attributes = File.GetAttributes(path); + if (attributes.HasFlag(FileAttributes.ReadOnly)) File.SetAttributes(path, attributes & ~FileAttributes.ReadOnly); + if (!attributes.HasFlag(FileAttributes.Directory)) + { + File.Delete(path); + return; + } + if (!attributes.HasFlag(FileAttributes.ReparsePoint)) + { + foreach (var entry in Directory.EnumerateFileSystemEntries(path, "*", EverythingInDirectory)) + { + try + { + Delete(entry); + } + catch (Exception error) when (IsFileError(error)) + { + // Keep going; the directory removal below reports what is left. + } + } + } + // Non-recursive: removes an emptied directory, or a junction or directory link itself. + Directory.Delete(path, recursive: false); + } + + /// The I/O failures of the filesystem, which this layer turns into null or a . + public static bool IsFileError(Exception error) => + error is IOException or UnauthorizedAccessException or NotSupportedException or System.Security.SecurityException; + + /// The \\?\ form of an absolute path, which Win32 uses without rewriting or a length limit. + private static string Verbatim(string fullPath) + { + if (fullPath.StartsWith(@"\\?\", StringComparison.Ordinal)) return fullPath; + return fullPath.StartsWith(@"\\", StringComparison.Ordinal) ? @"\\?\UNC\" + fullPath[2..] : @"\\?\" + fullPath; + } + + /// + /// Opens itself: a reparse point is opened rather than followed, so the + /// caller can inspect and refuse it. Returns null when the path cannot be opened. + /// + private static SafeFileHandle? Open(string path, uint access) + { + string full; + try + { + full = Path.GetFullPath(path); + } + catch (Exception error) when (IsFileError(error) || error is ArgumentException) + { + return null; + } + var handle = CreateFile(Verbatim(full), access, FileShare.ReadWrite | FileShare.Delete, 0, FileMode.Open, OpenReparsePoint | BackupSemantics, 0); + if (!handle.IsInvalid) return handle; + handle.Dispose(); + return null; + } + + private const uint GenericRead = 0x80000000; + private const uint OpenReparsePoint = 0x00200000; + private const uint BackupSemantics = 0x02000000; + + /// BY_HANDLE_FILE_INFORMATION. + [StructLayout(LayoutKind.Sequential)] + private struct FileInformation + { + public uint FileAttributes; + public uint CreationTimeLow, CreationTimeHigh; + public uint LastAccessTimeLow, LastAccessTimeHigh; + public uint LastWriteTimeLow, LastWriteTimeHigh; + public uint VolumeSerialNumber; + public uint FileSizeHigh, FileSizeLow; + public uint NumberOfLinks; + public uint FileIndexHigh, FileIndexLow; + } + + [LibraryImport("kernel32.dll", EntryPoint = "CreateFileW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)] + private static partial SafeFileHandle CreateFile(string fileName, uint desiredAccess, FileShare shareMode, nint securityAttributes, FileMode creationDisposition, uint flagsAndAttributes, nint templateFile); + + [LibraryImport("kernel32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + private static partial bool GetFileInformationByHandle(SafeFileHandle file, out FileInformation information); +} diff --git a/Windows/src/ScriptWidget.Core/Plugins/DataSourceModule.cs b/Windows/src/ScriptWidget.Core/Plugins/DataSourceModule.cs new file mode 100644 index 00000000..3dc1271d --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Plugins/DataSourceModule.cs @@ -0,0 +1,172 @@ +using System.Text.Json.Nodes; +using ScriptWidget.Core.Api; +using ScriptWidget.Core.Runtime; + +namespace ScriptWidget.Core.Plugins; + +/// +/// Performs one HTTP exchange for a plugin request. Production uses , which +/// resolves every hop and refuses private addresses; tests inject a fake so they stay offline. +/// must be consulted for every redirect target before it is followed, and +/// work must stop when is cancelled. +/// +public delegate FetchModule.Response DataSourceTransport(string method, Uri uri, IReadOnlyDictionary headers, byte[]? body, int timeoutMilliseconds, Func allowRedirect, CancellationToken cancellation); + +/// +/// The rules of $dataSource.request, as pure functions of the execution and the plugin. Each +/// returns the rejection message (the Swift wording) or null when the rule is satisfied. They are +/// evaluated natively for every request; the JavaScript installer is not a security boundary. +/// +public static class DataSourcePolicy +{ + public const string SessionEnded = "Execution session is no longer active"; + public const string LiveActivity = "[live_activity_unavailable] Live Activity presentations cannot request service data; update their state from the app"; + public const string Timeline = "[timeline_read_only] Future entries cannot make live service requests"; + public const string NetworkPermission = "Plugin requests require the network permission"; + public const string HttpsOnly = "Data source requests require HTTPS"; + public const string PluginHost = "Plugin request escaped its declared hosts."; + public const string PackageHost = "Data source host is not declared in widget.json networkDomains."; + public const string Budget = "Network request budget exceeded"; + + /// + /// Presentation rules: a finished execution gets nothing; Live Activity presentations and + /// future timeline entries are rendered ahead of time and must not make live requests. + /// + public static string? PresentationError(ExecutionRequest request) + { + if (request.Session.Finished) return SessionEnded; + if (RuntimePolicy.IsLiveActivity(request)) return LiveActivity; + if (RuntimePolicy.IsTimelineProjection(request)) return Timeline; + return null; + } + + /// + /// Declaration rules: the package's validated widget.json must list in plugins + /// and hold the network permission. A package without a manifest declares nothing, so it is + /// refused: installing a plugin never grants a widget anything by itself. + /// + public static string? DeclarationError(ExecutionRequest request, string pluginID) + { + var manifest = request.Prepared.HasManifest ? request.Prepared.Manifest : null; + var plugins = manifest.OptArray("plugins"); + var declared = plugins is not null && plugins.Any(item => item.IsString() && item!.GetValue() == pluginID); + if (!declared) return $"Plugin {pluginID} is not declared in widget.json"; + return RuntimePolicy.HasPermission(request, "network") ? null : NetworkPermission; + } + + /// + /// Destination rules for the initial URL and for every redirect target: HTTPS only, a public + /// host name without credentials, a host the plugin declares, and a host the package allows + /// through network + networkDomains. A plugin therefore never enlarges what the package + /// may reach, and a package never reaches a host the plugin did not declare. + /// + public static string? DestinationError(ExecutionRequest request, PluginManifest plugin, Uri uri) + { + if (!uri.IsAbsoluteUri || uri.Scheme.ToLowerInvariant() != "https") return HttpsOnly; + if (ValidationError(uri) is { } invalid) return invalid; + var host = uri.Host.ToLowerInvariant(); + if (!RuntimePolicy.HostMatches(host, plugin.Hosts)) return PluginHost; + if (!request.Prepared.HasManifest || !RuntimePolicy.PackageAllowsHost(request, host)) return PackageHost; + return null; + } + + /// True when a redirect to may be followed; the execution must still be running. + public static bool AllowsRedirect(ExecutionRequest request, PluginManifest plugin, Uri uri) => + !request.Session.Finished && DestinationError(request, plugin, uri) is null; + + /// Same textual rule as FetchModule.ValidationError: HTTP(S), a host, no credentials, not local or private. + private static string? ValidationError(Uri uri) + { + var host = PluginUrl.Unbracket(uri.Host.ToLowerInvariant()); + if (host.Length == 0) return "URL host is required"; + if (uri.UserInfo.Length > 0) return "URL credentials are not allowed"; + return RuntimePolicy.BlockedHostName(host) ? "Local and private network hosts are not allowed" : null; + } +} + +/// +/// $dataSource.request(pluginID, operationID, parameters), ported from +/// ScriptWidgetRuntimeDataSource. Handles the asynchronous operation datasource.request with +/// payload {pluginID, operationID, parameters} and resolves with the mapped response. +/// +/// Every rule of is checked here, in this order, with the Swift error strings: +/// execution still active, not a Live Activity, not a timeline projection, plugin declared in +/// widget.json, network permission, plugin installed and valid, request built only from +/// declared parameters, HTTPS destination inside both the plugin's hosts and the package's +/// networkDomains, and the per-execution network budget shared with fetch. Redirects are +/// re-validated against the same destination rules. Errors never contain response content, +/// header values or parameter values. +/// +public sealed class DataSourceModule : INativeModule +{ + public const string Operation = "datasource.request"; + /// The Swift fetch session's per-request timeout (5 s); the 10 s total is enforced by the transport. + public const int TimeoutMilliseconds = 5_000; + + private static readonly Lazy SharedFetch = new(() => new FetchModule()); + private static readonly Lazy SharedPool = new(() => new BoundedExecutor(FetchModule.MaximumConcurrent)); + private static readonly IReadOnlySet SupportedApis = new HashSet { "$dataSource" }; + + private readonly PluginStore store; + private readonly DataSourceTransport transport; + private readonly Action executor; + + /// Installed and built-in plugins. + /// The HTTP exchange; the bounded fetch transport by default. + /// Runs the request off the JavaScript thread; a bounded worker pool by default. + public DataSourceModule(PluginStore store, DataSourceTransport? transport = null, Action? executor = null) + { + this.store = store; + this.transport = transport ?? ((method, uri, headers, body, timeout, allowRedirect, cancellation) => SharedFetch.Value.Perform(method, uri, headers, body, timeout, allowRedirect, cancellation)); + this.executor = executor ?? (work => SharedPool.Value.Execute(work)); + } + + public IReadOnlySet Apis => SupportedApis; + + public bool Handles(string operation) => operation == Operation; + + public void Start(ExecutionRequest request, string operation, JsonObject? payload, NativeCompletion done) + { + void Fail(string message) => done(false, Json.Quote(message)); + if (operation != Operation) { Fail($"Unsupported operation {operation}"); return; } + if (DataSourcePolicy.PresentationError(request) is { } presentation) { Fail(presentation); return; } + var job = payload ?? new JsonObject(); + var pluginID = job["pluginID"] is { } pluginNode && pluginNode.IsString() ? pluginNode.GetValue() : null; + var operationID = job["operationID"] is { } operationNode && operationNode.IsString() ? operationNode.GetValue() : null; + if (pluginID is null || operationID is null) { Fail("Data source requests need a plugin id and an operation id"); return; } + if (DataSourcePolicy.DeclarationError(request, pluginID) is { } declaration) { Fail(declaration); return; } + var values = new Dictionary(StringComparer.Ordinal); + if (job.OptObject("parameters") is { } parameters) foreach (var (name, value) in parameters) values[name] = value; + + executor(() => + { + try + { + var plugin = store.Manifest(pluginID) ?? throw new PluginException(PluginErrorKind.NotFound, $"Plugin {pluginID} is not installed"); + var prepared = DataSourceRequestBuilder.Prepare(plugin, operationID, values); + if (DataSourcePolicy.DestinationError(request, plugin, prepared.Uri) is { } destination) throw new PluginException(PluginErrorKind.Permission, destination); + if (request.Session.Finished || request.Session.AddNetworkRequest() > FetchModule.MaximumRequests) + { + throw new PluginException(PluginErrorKind.Network, DataSourcePolicy.Budget); + } + var response = transport(prepared.Method, prepared.Uri, prepared.Headers, prepared.Body, TimeoutMilliseconds, + redirected => DataSourcePolicy.AllowsRedirect(request, plugin, redirected), request.Session.Cancellation.Token); + if (request.Session.Finished) return; + done(true, DataSourceResponseMapper.Map(prepared.Operation, response.Status, response.Body)); + } + catch (PluginException error) + { + Fail(error.Message); + } + catch (FetchModule.FetchException error) + { + Fail(string.IsNullOrEmpty(error.Message) ? "Data source request failed" : error.Message); + } + catch (Exception) + { + // Exception text from the network stack can quote the URL, which may carry parameter values. + Fail("Data source request failed"); + } + }); + } +} diff --git a/Windows/src/ScriptWidget.Core/Plugins/DataSourceRequests.cs b/Windows/src/ScriptWidget.Core/Plugins/DataSourceRequests.cs new file mode 100644 index 00000000..b18eb517 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Plugins/DataSourceRequests.cs @@ -0,0 +1,235 @@ +using System.Globalization; +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; +using ScriptWidget.Core.Api; + +namespace ScriptWidget.Core.Plugins; + +/// A fully resolved plugin request: nothing in it is taken from script input without validation. +public sealed class PreparedDataSourceRequest(PluginManifest plugin, PluginOperation operation, string method, Uri uri, IReadOnlyDictionary headers, byte[]? body) +{ + public PluginManifest Plugin { get; } = plugin; + public PluginOperation Operation { get; } = operation; + public string Method { get; } = method; + public Uri Uri { get; } = uri; + public IReadOnlyDictionary Headers { get; } = headers; + /// JSON body built from jsonBody parameters, or null when there are none. + public byte[]? Body { get; } = body; +} + +/// Maps an operation and script-supplied values to one request, ported from DataSourceRequestBuilder.prepare. +public static class DataSourceRequestBuilder +{ + /// Sent with every plugin request, replacing any User-Agent header parameter. + public const string UserAgent = "ScriptWidget-DataSource/1"; + + // CharacterSet.urlPathAllowed without "/?#{}": a path value can never add a segment, a query or a fragment. + private const string PathAllowed = "-._~!$&'()*+,=:@"; + // What URLComponents.queryItems leaves unescaped: "&" and "=" are always encoded, so a value cannot add a query item. + private const string QueryAllowed = "-._~!$'()*+,;:@/?"; + + /// + /// Builds the request for of . + /// + /// come from package code and are untrusted. Accepted value types are string, + /// bool, the numeric types, a holding one of those, and (for jsonBody + /// parameters only) /; null means "not supplied". + /// Booleans are sent as 1/0 in path, query and header positions, which is what the Swift + /// runtime sends for a JavaScript boolean. + /// + /// Security rules: + /// - only declared parameters are used; anything else in is ignored, so a script + /// cannot add headers, query items or body fields the plugin did not declare; + /// - path values are percent-encoded including /, ?, #, and ./.. values are refused, + /// so a value cannot change the path structure; + /// - query names and values are percent-encoded including & and =; + /// - header values with control characters are refused (no header injection); + /// - the final URL must be HTTPS and its host must match the plugin's declared hosts. + /// + /// NotFound for an unknown operation, InvalidParameters for a + /// missing or unusable value, Permission when the URL leaves the declared hosts. + public static PreparedDataSourceRequest Prepare(PluginManifest plugin, string operationID, IReadOnlyDictionary values) + { + var operation = plugin.Operations.FirstOrDefault(item => item.Id == operationID) + ?? throw new PluginException(PluginErrorKind.NotFound, $"Operation {operationID} was not found in {plugin.Id}."); + var path = operation.Path; + var query = new List<(string Name, string Value)>(); + var headers = new Dictionary(StringComparer.Ordinal); + var body = new JsonObject(); + foreach (var parameter in operation.Parameters) + { + var supplied = Unwrap(values.GetValueOrDefault(parameter.Id)); + var raw = supplied ?? parameter.DefaultValue; + var structured = raw is JsonObject or JsonArray; + var text = structured || raw is null ? "" : Describe(raw) ?? throw InvalidParameters($"Could not encode {parameter.Id}."); + if (raw is null || (!structured && text.Length == 0)) + { + if (parameter.Required) throw InvalidParameters($"Missing required parameter {parameter.Id}."); + continue; + } + if (structured && parameter.Location != PluginParameterLocation.JsonBody) throw InvalidParameters($"Could not encode {parameter.Id}."); + switch (parameter.Location) + { + case PluginParameterLocation.Path: + if (text is "." or "..") throw InvalidParameters($"Could not encode {parameter.Id}."); + path = path.Replace("{" + parameter.Id + "}", PercentEncode(text, PathAllowed), StringComparison.Ordinal); + break; + case PluginParameterLocation.Query: + query.Add((parameter.Id, text)); + break; + case PluginParameterLocation.Header: + if (text.Any(character => character < ' ' || character == '\u007F')) throw InvalidParameters($"Could not encode {parameter.Id}."); + headers[parameter.Id] = text; + break; + default: + body[parameter.Id] = raw switch + { + JsonNode node => node.DeepClone(), + string value => JsonValue.Create(value), + bool value => JsonValue.Create(value), + double value => JsonValue.Create(value), + float or decimal => JsonValue.Create(Convert.ToDouble(raw, CultureInfo.InvariantCulture)), + _ => JsonValue.Create(Convert.ToInt64(raw, CultureInfo.InvariantCulture)), + }; + break; + } + } + if (path.Contains('{') || path.Contains('}')) throw InvalidParameters("A path parameter is unresolved."); + var address = PluginUrl.Parse(plugin.BaseURL); + var baseHost = address?.Host; + if (address?.Scheme is null || string.IsNullOrEmpty(baseHost)) throw new PluginException(PluginErrorKind.InvalidManifest, "Invalid plugin baseURL."); + var basePath = address.RawPath; + var assembled = new StringBuilder() + .Append(address.Scheme.ToLowerInvariant()).Append("://").Append(baseHost) + .Append(address.Port >= 0 ? ":" + address.Port.ToString(CultureInfo.InvariantCulture) : "") + .Append(basePath == "/" ? "" : basePath).Append(path); + if (query.Count > 0) assembled.Append('?').Append(string.Join("&", query.Select(item => PercentEncode(item.Name, QueryAllowed) + "=" + PercentEncode(item.Value, QueryAllowed)))); + var url = assembled.ToString(); + var parsed = PluginUrl.Parse(url) ?? throw new PluginException(PluginErrorKind.InvalidManifest, "Invalid plugin baseURL."); + var host = parsed.Host?.ToLowerInvariant(); + if (parsed.Scheme != "https" || host is null || !RuntimePolicy.HostMatches(host, plugin.Hosts)) + { + throw new PluginException(PluginErrorKind.Permission, "Plugin request escaped its declared hosts."); + } + // The transport sends what System.Uri understood, so the same rule is applied to its reading of the URL. + if (!Uri.TryCreate(url, UriKind.Absolute, out var uri)) throw new PluginException(PluginErrorKind.InvalidManifest, "Invalid plugin baseURL."); + if (uri.Scheme != "https" || uri.UserInfo.Length > 0 || !RuntimePolicy.HostMatches(uri.Host, plugin.Hosts)) + { + throw new PluginException(PluginErrorKind.Permission, "Plugin request escaped its declared hosts."); + } + foreach (var name in headers.Keys.Where(name => name.Equals("User-Agent", StringComparison.OrdinalIgnoreCase) || (name.Equals("Content-Type", StringComparison.OrdinalIgnoreCase) && body.Count > 0)).ToList()) + { + headers.Remove(name); + } + headers["User-Agent"] = UserAgent; + var bodyBytes = body.Count > 0 ? Encoding.UTF8.GetBytes(Json.Stringify(body)) : null; + if (bodyBytes is not null) headers["Content-Type"] = "application/json"; + return new PreparedDataSourceRequest(plugin, operation, operation.Method, uri, headers, bodyBytes); + } + + /// A JSON scalar becomes its CLR value; JSON null and null both mean "not supplied". + private static object? Unwrap(object? value) + { + if (value is not JsonValue scalar) return value; + switch (scalar.GetValueKind()) + { + case JsonValueKind.String: return scalar.GetValue(); + case JsonValueKind.True: return true; + case JsonValueKind.False: return false; + case JsonValueKind.Null: return null; + case JsonValueKind.Number: + // The text of the number decides, so a node made from any numeric type reads the same. + var literal = scalar.ToJsonString(); + if (long.TryParse(literal, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var whole)) return whole; + return double.TryParse(literal, NumberStyles.Float, CultureInfo.InvariantCulture, out var number) ? number : double.NaN; + default: return scalar; + } + } + + /// Text form of a scalar value, or null when it has none (non-finite numbers, unsupported types). + private static string? Describe(object raw) + { + switch (raw) + { + case string value: return value; + case bool value: return value ? "1" : "0"; + case int or long or short or sbyte or byte or ushort or uint or ulong or System.Numerics.BigInteger: + return Convert.ToString(raw, CultureInfo.InvariantCulture); + case float or double or decimal: + { + var value = Convert.ToDouble(raw, CultureInfo.InvariantCulture); + if (double.IsNaN(value) || double.IsInfinity(value)) return null; + if (value == Math.Round(value, MidpointRounding.ToEven) && Math.Abs(value) < 1e15) return ((long)value).ToString(CultureInfo.InvariantCulture); + return Plain(value); + } + default: return null; + } + } + + /// The shortest decimal text that reads back as , without an exponent. + private static string Plain(double value) + { + var text = value.ToString("R", CultureInfo.InvariantCulture); + var exponentAt = text.IndexOf('E'); + if (exponentAt < 0) return text; + var exponent = int.Parse(text[(exponentAt + 1)..], NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture); + var mantissa = text[..exponentAt]; + var negative = mantissa.StartsWith('-'); + if (negative) mantissa = mantissa[1..]; + var point = mantissa.IndexOf('.'); + var digits = mantissa.Replace(".", "", StringComparison.Ordinal); + var position = (point < 0 ? mantissa.Length : point) + exponent; + string plain; + if (position <= 0) plain = "0." + new string('0', -position) + digits; + else if (position >= digits.Length) plain = digits + new string('0', position - digits.Length); + else plain = digits[..position] + "." + digits[position..]; + return (negative ? "-" : "") + plain; + } + + /// Percent-encodes every UTF-8 byte that is not an ASCII letter, digit or in . + internal static string PercentEncode(string value, string allowed) + { + var output = new StringBuilder(); + foreach (var item in Encoding.UTF8.GetBytes(value)) + { + var character = (char)item; + if (char.IsAsciiLetterOrDigit(character) || allowed.Contains(character)) output.Append(character); + else output.Append('%').Append(item.ToString("X2", CultureInfo.InvariantCulture)); + } + return output.ToString(); + } + + private static PluginException InvalidParameters(string message) => new(PluginErrorKind.InvalidParameters, message); +} + +/// Turns an HTTP response into the value a widget receives, as the Swift runtime bridge does. +public static class DataSourceResponseMapper +{ + /// ScriptWidgetFetchManager.maximumResponseBytes: plugin responses share the 2 MiB fetch limit. + public const int MaximumResponseBytes = FetchModule.MaximumResponseBytes; + + /// + /// Returns the JSON text delivered to the script: the response document itself for a json + /// operation, or a JSON string literal for a text operation. + /// + /// Fails closed with : a non-2xx status, more than 2 MiB, a + /// json body that is not one well-formed UTF-8 JSON object or array, or a text body that + /// is not valid UTF-8. Response content is never included in the error message. + /// + public static string Map(PluginOperation operation, int status, byte[] body) + { + if (status is < 200 or > 299) throw Network("Data source request failed"); + if (body.Length > MaximumResponseBytes) throw Network("Data source response exceeds 2 MiB."); + var text = PluginValidator.DecodeUtf8(body); + if (operation.ResponseType == PluginResponseType.Text) return StrictJson.Quote(text ?? throw Network("Data source returned non-UTF-8 text")); + StrictJson.Kind kind; + try { kind = StrictJson.Validate(text ?? throw Network("Data source returned invalid JSON")); } + catch (StrictJson.JsonException) { throw Network("Data source returned invalid JSON"); } + // JSONSerialization without .fragmentsAllowed accepts only containers at the top level. + if (kind != StrictJson.Kind.Object && kind != StrictJson.Kind.Array) throw Network("Data source returned invalid JSON"); + return text; + } + + private static PluginException Network(string message) => new(PluginErrorKind.Network, message); +} diff --git a/Windows/src/ScriptWidget.Core/Plugins/PluginFiles.cs b/Windows/src/ScriptWidget.Core/Plugins/PluginFiles.cs new file mode 100644 index 00000000..1f5c4a1e --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Plugins/PluginFiles.cs @@ -0,0 +1,446 @@ +using System.Globalization; +using System.IO.Compression; +using System.Runtime.InteropServices; +using System.Text; +using Microsoft.Win32.SafeHandles; + +namespace ScriptWidget.Core.Plugins; + +/// +/// Link-refusing filesystem primitives of the plugin store: the part of the Android package +/// layer's SafeFiles that plugins use. Every lookup looks at the entry itself, so a symbolic +/// link or junction is seen as a link and never as its target. +/// +/// A check and the following open are separate system calls; the opened handle is checked again +/// before anything is read. Plugin directories live in app-private storage. +/// +internal static class PluginFiles +{ + /// Most directory entries listed before a listing fails closed. + private const int MaximumDirectoryEntries = 100_000; + + /// Raised when a directory cannot be staged or published; the message is safe to show. + public sealed class StorageException(string message, Exception? cause = null) : Exception(message, cause); + + /// True when something, including a dangling link, occupies . + public static bool Occupied(string path) => Attributes(path) is not null; + + /// Attributes of itself (never of a link target), or null when it is absent or unreadable. + private static FileAttributes? Attributes(string path) + { + try { return File.GetAttributes(path); } + catch (Exception error) when (error is IOException or UnauthorizedAccessException or ArgumentException or NotSupportedException) { return null; } + } + + /// when it is a real directory rather than a link to one, else null. + public static string? RealDirectory(string path) => + Attributes(path) is { } attributes && attributes.HasFlag(FileAttributes.Directory) && !attributes.HasFlag(FileAttributes.ReparsePoint) ? path : null; + + /// Entry names of , or null when it cannot be listed or is implausibly large. + public static List? Names(string directory) + { + try + { + var result = new List(); + foreach (var entry in Directory.EnumerateFileSystemEntries(directory)) + { + result.Add(Path.GetFileName(entry)); + if (result.Count > MaximumDirectoryEntries) return null; + } + return result; + } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) + { + return null; + } + } + + /// + /// The identity two names share when a case-insensitive or normalizing filesystem would treat + /// them as one entry: NFC, then locale-independent lower case. + /// + public static string Identity(string name) + { + try { return name.Normalize(NormalizationForm.FormC).ToLowerInvariant(); } + catch (ArgumentException) { return name.ToLowerInvariant(); } + } + + /// + /// Resolves directly below to an existing regular file, or null. + /// The name must be spelled exactly as stored, must not be a link, must not have a case or + /// Unicode-form alias, and must still be inside the directory. + /// + public static string? ExistingFile(string directory, string name) + { + if (RealDirectory(directory) is null || Names(directory) is not { } siblings) return null; + var identity = Identity(name); + if (!siblings.Contains(name, StringComparer.Ordinal) || siblings.Any(sibling => sibling != name && Identity(sibling) == identity)) return null; + var child = Path.Combine(directory, name); + if (Attributes(child) is not { } attributes || attributes.HasFlag(FileAttributes.Directory) || attributes.HasFlag(FileAttributes.ReparsePoint)) return null; + var root = Path.GetFullPath(directory).TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar; + return Path.GetFullPath(child).StartsWith(root, StringComparison.Ordinal) ? child : null; + } + + /// + /// Reads , or returns null when it holds more than , is not a + /// regular file, or has more than one hard link: a second link could expose or alter a file + /// outside the plugin. + /// + public static byte[]? ReadBounded(string file, long maximumBytes) + { + try + { + using var stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, 1, FileOptions.SequentialScan); + if (!GetFileInformationByHandle(stream.SafeFileHandle, out var information)) return null; + if ((information.FileAttributes & (uint)(FileAttributes.Directory | FileAttributes.ReparsePoint)) != 0 || information.NumberOfLinks > 1) return null; + using var output = new MemoryStream(); + var buffer = new byte[16 * 1024]; + long total = 0; + while (true) + { + var count = stream.Read(buffer, 0, buffer.Length); + if (count <= 0) break; + total += count; + // The size is counted while reading, never taken from metadata that may change. + if (total > maximumBytes) return null; + output.Write(buffer, 0, count); + } + return output.ToArray(); + } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) + { + return null; + } + } + + /// Creates exclusively, writes and flushes them to storage. + public static void WriteNew(string file, byte[] bytes) + { + using var stream = new FileStream(file, FileMode.CreateNew, FileAccess.Write, FileShare.None); + stream.Write(bytes, 0, bytes.Length); + stream.Flush(flushToDisk: true); + } + + /// Creates a uniquely named private working directory below . + public static string NewWorkDirectory(string stagingRoot, string prefix) + { + try + { + Directory.CreateDirectory(stagingRoot); + if (RealDirectory(stagingRoot) is null) throw new IOException("Staging directory is a link"); + var path = Path.Combine(stagingRoot, $"{prefix}-{Guid.NewGuid():D}"); + if (Occupied(path)) throw new IOException("Staging directory already exists"); + Directory.CreateDirectory(path); + return path; + } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) + { + throw new StorageException("The staging directory is unavailable.", error); + } + } + + /// + /// Publishes a prepared directory with one rename, so observers see either nothing or the + /// complete plugin. must be free; callers hold the store lock. + /// + public static void Publish(string prepared, string destination) + { + if (Occupied(destination)) throw new StorageException("A package with this name already exists."); + try { Directory.Move(prepared, destination); } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) { throw new StorageException("The package could not be published.", error); } + } + + /// Removes and everything below it. Links are removed, never followed. Best effort. + public static void DeleteTree(string path) + { + if (Attributes(path) is not { } attributes) return; + try + { + if (!attributes.HasFlag(FileAttributes.Directory)) File.Delete(path); + else if (attributes.HasFlag(FileAttributes.ReparsePoint)) Directory.Delete(path, recursive: false); + else Directory.Delete(path, recursive: true); + } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) + { + // Leftovers stay inside the private staging directory and are retried by later cleanups. + } + } + + [StructLayout(LayoutKind.Sequential)] + private struct ByHandleFileInformation + { + public uint FileAttributes; + public uint CreationTimeLow; + public uint CreationTimeHigh; + public uint LastAccessTimeLow; + public uint LastAccessTimeHigh; + public uint LastWriteTimeLow; + public uint LastWriteTimeHigh; + public uint VolumeSerialNumber; + public uint FileSizeHigh; + public uint FileSizeLow; + public uint NumberOfLinks; + public uint FileIndexHigh; + public uint FileIndexLow; + } + + [DllImport("kernel32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + private static extern bool GetFileInformationByHandle(SafeFileHandle file, out ByHandleFileInformation information); +} + +/// +/// Central-directory validation and bounded inflation of an untrusted .swplugin archive: the +/// archive preflight of the package layer (ScriptPackageArchivePreflight.validate in Swift), +/// kept here so the plugin store does not depend on the package store. Validation inspects bytes +/// only and never inflates or writes anything. +/// +internal static class PluginArchive +{ + /// One central-directory entry that passed validation. are the non-empty path components. + public sealed record Entry(string Name, IReadOnlyList Parts, bool IsDirectory, long UncompressedSize, long Crc); + + // ScriptPackageArchivePolicy. + private const int MaximumArchiveBytes = 32 * 1024 * 1024; + private const long MaximumExpandedBytes = 64L * 1024 * 1024; + private const long MaximumFileBytes = 25L * 1024 * 1024; + private const int MaximumFileCount = 500; + private const int MaximumPathDepth = 16; + private const int MaximumComponentBytes = 255; + + private const long EndSignature = 0x06054B50L; + private const long CentralSignature = 0x02014B50L; + private const long UInt32Max = 0xFFFFFFFFL; + + /// + /// Validates the whole archive and returns its entries, or throws. + /// + /// Rejected: empty or oversized archives; a missing, multi-disk, trailing-garbage or + /// out-of-bounds central directory; more entries than allowed; encrypted entries; compression + /// other than stored/deflate; malformed ZIP64 sizes; entry names that are not UTF-8, are + /// absolute, contain a backslash, colon, NUL, control character, . or .. component, or + /// nest too deeply; duplicate, case-colliding or file-versus-directory clashing paths; symbolic + /// links and other special files; a declared file size or declared total above the limits; + /// and a large file that claims zero compressed bytes. + /// + public static List Validate(byte[] data) + { + if (data.Length == 0 || data.Length > MaximumArchiveBytes) throw Fail("Archive must be between 1 byte and 32 MiB."); + var end = EndOfCentralDirectory(data); + if (end < 0) throw Fail("Archive has no valid ZIP central directory."); + if (U16(data, end + 4) != 0 || U16(data, end + 6) != 0 || U16(data, end + 8) != U16(data, end + 10)) throw Fail("Multi-disk ZIP archives are not supported."); + if (end + 22 + U16(data, end + 20) != data.Length) throw Fail("Archive has a malformed ZIP footer."); + var count = U16(data, end + 10); + var centralSize = U32(data, end + 12); + var centralOffset = U32(data, end + 16); + if (count <= 0 || count > MaximumFileCount) throw Fail($"Archive must contain 1–{MaximumFileCount} entries."); + if (centralOffset + centralSize > data.Length) throw Fail("Archive central directory is out of bounds."); + + var cursor = (int)centralOffset; + long expanded = 0; + var collisions = new Collisions(); + var entries = new List(count); + for (var index = 0; index < count; index++) + { + if (cursor + 46 > data.Length || U32(data, cursor) != CentralSignature) throw Fail("Archive central directory contains an invalid entry."); + var flags = U16(data, cursor + 8); + var method = U16(data, cursor + 10); + var crc = U32(data, cursor + 16); + var nameLength = U16(data, cursor + 28); + var extraLength = U16(data, cursor + 30); + var commentLength = U16(data, cursor + 32); + var external = U32(data, cursor + 38); + var entryEnd = cursor + 46 + nameLength + extraLength + commentLength; + if (nameLength == 0 || entryEnd > data.Length) throw Fail("Archive contains a truncated filename."); + var extraStart = cursor + 46 + nameLength; + if (ResolvedSizes(U32(data, cursor + 20), U32(data, cursor + 24), data, extraStart, extraStart + extraLength) is not { } sizes) + { + throw Fail("Archive contains malformed ZIP64 size metadata."); + } + var (compressed, uncompressed) = sizes; + if ((flags & 0x1) != 0) throw Fail("Encrypted package entries are not supported."); + if (method != 0 && method != 8) throw Fail($"Unsupported ZIP compression method {method.ToString(CultureInfo.InvariantCulture)}."); + var name = DecodeUtf8(data, cursor + 46, nameLength); + var parts = name is null ? null : EntryParts(name, MaximumPathDepth); + if (name is null || parts is null) throw Fail("Archive contains an unsafe or non-UTF-8 path."); + var isDirectory = name.EndsWith('/'); + if (!collisions.Add(name, parts, isDirectory)) throw Fail("Archive contains duplicate or case-colliding paths."); + // Unix mode in the high half: only regular files (0x8000), directories (0x4000) or + // an absent type are accepted; 0xA000 is a symbolic link. + var type = (int)((external >> 16) & 0xF000L); + if (type == 0xA000) throw Fail("Symbolic links are not allowed in ScriptWidget packages."); + if (type != 0 && type != 0x8000 && type != 0x4000) throw Fail("Only regular files and directories are allowed in ScriptWidget packages."); + if (!isDirectory) + { + // Sizes are unsigned 64-bit; a negative value is far above any limit. + if (uncompressed < 0 || uncompressed > MaximumFileBytes) throw Fail($"Package file {name} exceeds 25 MiB."); + expanded += uncompressed; + if (expanded > MaximumExpandedBytes) throw Fail("Expanded package exceeds 64 MiB."); + if (uncompressed > 1024 * 1024 && compressed == 0L) throw Fail($"Archive entry {name} has an invalid compression ratio."); + } + entries.Add(new Entry(name, parts, isDirectory, uncompressed, crc)); + cursor = entryEnd; + } + if (cursor != centralOffset + centralSize) throw Fail("Archive entries do not match the declared central directory."); + return entries; + } + + /// + /// Inflates one validated entry in memory, counting real bytes so false size headers cannot + /// expand, and checks its CRC-32. Throws when the entry is + /// missing, does not match its declared size or checksum, or exceeds . + /// + public static byte[] Inflate(ZipArchive zip, Entry entry, int maximumBytes) + { + var zipEntry = zip.GetEntry(entry.Name); + if (zipEntry is null || zipEntry.FullName.EndsWith('/')) throw new InvalidDataException(); + using var output = new MemoryStream(); + var crc = 0xFFFFFFFFu; + using (var input = zipEntry.Open()) + { + var buffer = new byte[16 * 1024]; + while (true) + { + var count = input.Read(buffer, 0, buffer.Length); + if (count <= 0) break; + if (output.Length + count > entry.UncompressedSize || output.Length + count > maximumBytes) throw new InvalidDataException(); + for (var index = 0; index < count; index++) crc = CrcTable[(crc ^ buffer[index]) & 0xFF] ^ (crc >> 8); + output.Write(buffer, 0, count); + } + } + if (output.Length != entry.UncompressedSize || (crc ^ 0xFFFFFFFFu) != entry.Crc) throw new InvalidDataException(); + return output.ToArray(); + } + + /// + /// Components of an archive entry name, or null when the name is unsafe: a leading slash, + /// any backslash, colon (drive or stream syntax) or NUL, a ./.. component, a component + /// that is too long or holds control characters, no components, or more than . + /// + public static IReadOnlyList? EntryParts(string name, int maximumDepth) + { + if (name.StartsWith('/') || name.Contains('\\') || name.Contains(':') || name.Contains('\0')) return null; + var parts = name.Split('/', StringSplitOptions.RemoveEmptyEntries); + if (parts.Length == 0 || parts.Length > maximumDepth || !parts.All(ValidComponent)) return null; + return parts; + } + + private static bool ValidComponent(string part) => + part.Length > 0 && part != "." && part != ".." && !part.Contains('/') && !part.Contains('\\') && + Encoding.UTF8.GetByteCount(part) <= MaximumComponentBytes && !HasControlCharacters(part); + + /// Control, format (except ZWNJ/ZWJ, which joined emoji and some scripts need) or unpaired surrogate characters. + private static bool HasControlCharacters(string text) + { + for (var index = 0; index < text.Length; index++) + { + if (!Rune.TryGetRuneAt(text, index, out var rune)) return true; + if (rune.Value is 0x200C or 0x200D) continue; + if (Rune.GetUnicodeCategory(rune) is UnicodeCategory.Control or UnicodeCategory.Format) return true; + index += rune.Utf16SequenceLength - 1; + } + return false; + } + + /// + /// Tracks every file and directory prefix seen so far. Two spellings of one identity + /// (case or Unicode form), a repeated path, and a path used as both file and directory would + /// overwrite or shadow each other on extraction, so each is a collision. + /// + private sealed class Collisions + { + private readonly HashSet fullPaths = new(StringComparer.Ordinal); + private readonly Dictionary spellings = new(StringComparer.Ordinal); + private readonly Dictionary directories = new(StringComparer.Ordinal); + + public bool Add(string name, IReadOnlyList parts, bool isDirectory) + { + if (!fullPaths.Add(PluginFiles.Identity(name).Trim('/'))) return false; + var prefix = new StringBuilder(); + for (var index = 0; index < parts.Count; index++) + { + if (index > 0) prefix.Append('/'); + prefix.Append(parts[index]); + var path = prefix.ToString(); + var identity = PluginFiles.Identity(path); + if (!spellings.TryGetValue(identity, out var spelling)) spellings[identity] = spelling = path; + if (spelling != path) return false; + var directory = index < parts.Count - 1 || isDirectory; + if (!directories.TryGetValue(path, out var known)) directories[path] = known = directory; + if (known != directory) return false; + } + return true; + } + } + + private static (long Compressed, long Uncompressed)? ResolvedSizes(long compressed32, long uncompressed32, byte[] data, int extraStart, int extraEnd) + { + if (compressed32 != UInt32Max && uncompressed32 != UInt32Max) return (compressed32, uncompressed32); + var compressed = compressed32; + var uncompressed = uncompressed32; + var cursor = extraStart; + while (cursor + 4 <= extraEnd) + { + var identifier = U16(data, cursor); + var end = cursor + 4 + U16(data, cursor + 2); + if (end > extraEnd) return null; + if (identifier == 0x0001) + { + var value = cursor + 4; + if (uncompressed32 == UInt32Max) + { + if (value + 8 > end) return null; + uncompressed = U64(data, value); + value += 8; + } + if (compressed32 == UInt32Max) + { + if (value + 8 > end) return null; + compressed = U64(data, value); + } + return (compressed, uncompressed); + } + cursor = end; + } + return null; + } + + private static int EndOfCentralDirectory(byte[] data) + { + if (data.Length < 22) return -1; + var lowerBound = Math.Max(0, data.Length - 65_557); + for (var index = data.Length - 22; index >= lowerBound; index--) + { + if (U32(data, index) == EndSignature) return index; + } + return -1; + } + + private static string? DecodeUtf8(byte[] data, int offset, int length) + { + try { return new UTF8Encoding(false, true).GetString(data, offset, length); } + catch (DecoderFallbackException) { return null; } + } + + private static int U16(byte[] data, int index) => index < 0 || index + 2 > data.Length ? 0 : data[index] | (data[index + 1] << 8); + + private static long U32(byte[] data, int index) => index < 0 || index + 4 > data.Length ? 0 : (long)U16(data, index) | ((long)U16(data, index + 2) << 16); + + private static long U64(byte[] data, int index) => index < 0 || index + 8 > data.Length ? 0 : U32(data, index) | (U32(data, index + 4) << 32); + + private static readonly uint[] CrcTable = BuildCrcTable(); + + private static uint[] BuildCrcTable() + { + var table = new uint[256]; + for (uint index = 0; index < 256; index++) + { + var value = index; + for (var bit = 0; bit < 8; bit++) value = (value & 1) != 0 ? 0xEDB88320u ^ (value >> 1) : value >> 1; + table[index] = value; + } + return table; + } + + private static PluginException Fail(string message) => new(PluginErrorKind.InvalidManifest, message); +} diff --git a/Windows/src/ScriptWidget.Core/Plugins/PluginManifest.cs b/Windows/src/ScriptWidget.Core/Plugins/PluginManifest.cs new file mode 100644 index 00000000..7221ff04 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Plugins/PluginManifest.cs @@ -0,0 +1,512 @@ +using System.Globalization; +using System.Net; +using System.Text; +using System.Text.RegularExpressions; +using ScriptWidget.Core.Api; + +namespace ScriptWidget.Core.Plugins; + +/// Where a parameter value goes in the request (DataSourceParameterLocation in Swift). +public enum PluginParameterLocation { Path, Query, Header, JsonBody } + +/// How a response body is handed to the widget (DataSourceResponseType in Swift). +public enum PluginResponseType { Json, Text } + +/// The names the enumerations have in plugin.json. +public static class PluginWireNames +{ + public static string WireName(this PluginParameterLocation location) => location switch + { + PluginParameterLocation.Path => "path", + PluginParameterLocation.Query => "query", + PluginParameterLocation.Header => "header", + _ => "jsonBody", + }; + + public static string WireName(this PluginResponseType type) => type == PluginResponseType.Json ? "json" : "text"; + + public static PluginParameterLocation? ParameterLocation(string name) => name switch + { + "path" => PluginParameterLocation.Path, + "query" => PluginParameterLocation.Query, + "header" => PluginParameterLocation.Header, + "jsonBody" => PluginParameterLocation.JsonBody, + _ => null, + }; + + public static PluginResponseType? ResponseType(string name) => name switch + { + "json" => PluginResponseType.Json, + "text" => PluginResponseType.Text, + _ => null, + }; +} + +/// One declared operation parameter. A plugin can only place values where it declared them. +public sealed record PluginParameter(string Id, string Label, PluginParameterLocation Location, bool Required, string? DefaultValue = null); + +/// One declared HTTPS request template. +public sealed record PluginOperation(string Id, string Title, string Method, string Path, PluginResponseType ResponseType, IReadOnlyList Parameters) +{ + public bool Equals(PluginOperation? other) => + other is not null && Id == other.Id && Title == other.Title && Method == other.Method && Path == other.Path && ResponseType == other.ResponseType && Parameters.SequenceEqual(other.Parameters); + + public override int GetHashCode() => HashCode.Combine(Id, Title, Method, Path, ResponseType, Parameters.Count); +} + +/// +/// A Data Source Plugin 1.0 manifest (plugin.json). It is data: a mapping from parameters to +/// HTTPS requests against the hosts it declares. It carries no code and no permissions. +/// +public sealed record PluginManifest( + int FormatVersion, + string Id, + string Name, + string Version, + string Summary, + string Symbol, + string Author, + string License, + string MinimumRuntimeVersion, + string BaseURL, + IReadOnlyList Hosts, + IReadOnlyList Operations) +{ + /// The only supported formatVersion; any other value fails closed. + public const int CurrentFormatVersion = 1; + + public bool Equals(PluginManifest? other) => + other is not null && FormatVersion == other.FormatVersion && Id == other.Id && Name == other.Name && Version == other.Version && Summary == other.Summary && + Symbol == other.Symbol && Author == other.Author && License == other.License && MinimumRuntimeVersion == other.MinimumRuntimeVersion && BaseURL == other.BaseURL && + Hosts.SequenceEqual(other.Hosts) && Operations.SequenceEqual(other.Operations); + + public override int GetHashCode() => HashCode.Combine(FormatVersion, Id, Name, Version, BaseURL, Hosts.Count, Operations.Count); + + /// + /// plugin.json text with sorted keys and two-space indentation, the form Swift writes + /// (.prettyPrinted, .sortedKeys, .withoutEscapingSlashes). An absent defaultValue is omitted. + /// + public string ToJson() + { + static string Q(string value) => StrictJson.Quote(value); + var output = new StringBuilder("{\n"); + output.Append($" \"author\": {Q(Author)},\n"); + output.Append($" \"baseURL\": {Q(BaseURL)},\n"); + output.Append($" \"formatVersion\": {FormatVersion.ToString(CultureInfo.InvariantCulture)},\n"); + output.Append($" \"hosts\": [{string.Join(", ", Hosts.Select(Q))}],\n"); + output.Append($" \"id\": {Q(Id)},\n"); + output.Append($" \"license\": {Q(License)},\n"); + output.Append($" \"minimumRuntimeVersion\": {Q(MinimumRuntimeVersion)},\n"); + output.Append($" \"name\": {Q(Name)},\n"); + output.Append(" \"operations\": ["); + for (var index = 0; index < Operations.Count; index++) + { + var operation = Operations[index]; + output.Append(index == 0 ? "\n" : ",\n"); + output.Append(" {\n"); + output.Append($" \"id\": {Q(operation.Id)},\n"); + output.Append($" \"method\": {Q(operation.Method)},\n"); + output.Append(" \"parameters\": ["); + for (var parameterIndex = 0; parameterIndex < operation.Parameters.Count; parameterIndex++) + { + var parameter = operation.Parameters[parameterIndex]; + output.Append(parameterIndex == 0 ? "\n" : ",\n"); + output.Append(" {"); + if (parameter.DefaultValue is not null) output.Append($"\"defaultValue\": {Q(parameter.DefaultValue)}, "); + output.Append($"\"id\": {Q(parameter.Id)}, \"label\": {Q(parameter.Label)}, "); + output.Append($"\"location\": {Q(parameter.Location.WireName())}, \"required\": {(parameter.Required ? "true" : "false")}}}"); + } + output.Append(operation.Parameters.Count == 0 ? "],\n" : "\n ],\n"); + output.Append($" \"path\": {Q(operation.Path)},\n"); + output.Append($" \"responseType\": {Q(operation.ResponseType.WireName())},\n"); + output.Append($" \"title\": {Q(operation.Title)}\n"); + output.Append(" }"); + } + output.Append(Operations.Count == 0 ? "],\n" : "\n ],\n"); + output.Append($" \"summary\": {Q(Summary)},\n"); + output.Append($" \"symbol\": {Q(Symbol)},\n"); + output.Append($" \"version\": {Q(Version)}\n"); + output.Append("}\n"); + return output.ToString(); + } +} + +/// What went wrong in the plugin layer (the cases of DataSourcePluginError in Swift). +public enum PluginErrorKind { InvalidManifest, NotFound, Permission, InvalidParameters, Network, Storage } + +/// +/// The single failure type of the plugin layer, mirroring DataSourcePluginError in Swift. +/// is the text shown to the user or passed to the script's rejection handler. +/// +public sealed class PluginException(PluginErrorKind kind, string message, Exception? cause = null) : Exception(message, cause) +{ + public PluginErrorKind Kind { get; } = kind; +} + +/// One reason a manifest is refused. is the stable Swift issue identifier. +public sealed record PluginValidationIssue(string Id, string Message); + +/// +/// Strict decoder and validator for plugin.json, ported from DataSourcePluginValidator. +/// Plugin manifests are untrusted (imported ZIPs, shared files); everything not explicitly +/// allowed is refused. +/// +public static partial class PluginValidator +{ + /// DataSourcePluginValidator.maximumManifestBytes: bounds parsing work for an untrusted file. + public const int MaximumManifestBytes = 256 * 1024; + /// DataSourcePluginValidator.maximumOperations. + public const int MaximumOperations = 50; + /// DataSourcePluginValidator.maximumParameters (per operation). + public const int MaximumParameters = 30; + /// Most host declarations a plugin may carry. + public const int MaximumHosts = 20; + /// Longest plugin name, in user-perceived characters. + public const int MaximumNameCharacters = 80; + /// ScriptWidgetBuildContract.runtimeAPIVersion: the only minimumRuntimeVersion accepted. + public const string RuntimeApiVersion = "1.0"; + + private static readonly HashSet RootKeys = ["formatVersion", "id", "name", "version", "summary", "symbol", "author", "license", "minimumRuntimeVersion", "baseURL", "hosts", "operations"]; + private static readonly HashSet OperationKeys = ["id", "title", "method", "path", "responseType", "parameters"]; + private static readonly HashSet ParameterKeys = ["id", "label", "location", "required", "defaultValue"]; + private static readonly HashSet Methods = ["GET", "POST", "PUT", "PATCH", "DELETE"]; + + [GeneratedRegex(@"\A[A-Za-z0-9][A-Za-z0-9.-]{2,127}\z", RegexOptions.CultureInvariant)] + private static partial Regex Identifier(); + [GeneratedRegex(@"\A[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z.-]+)?\z", RegexOptions.CultureInvariant)] + private static partial Regex SemanticVersion(); + [GeneratedRegex(@"\A[A-Za-z0-9!#$%&'*+.^_`|~-]+\z", RegexOptions.CultureInvariant)] + private static partial Regex HeaderName(); + [GeneratedRegex(@"\A[a-z0-9*.-]+\z", RegexOptions.CultureInvariant)] + private static partial Regex HostDeclaration(); + [GeneratedRegex(@"\{([A-Za-z0-9.-]+)\}", RegexOptions.CultureInvariant)] + private static partial Regex Placeholder(); + + /// + /// Decodes and validates plugin.json bytes. + /// + /// Security rules, each failing closed with : + /// - at most 256 KiB, strict UTF-8, strict JSON with no duplicate keys; + /// - unknown keys at any level are rejected, so a future or misspelled capability is never + /// silently ignored; + /// - every field must be present with its exact JSON type (no coercion); + /// - then every rule of . + /// + public static PluginManifest Decode(byte[] data) + { + if (data.Length > MaximumManifestBytes) throw Invalid("plugin.json exceeds 256 KiB."); + var text = DecodeUtf8(data) ?? throw Invalid("plugin.json must be UTF-8 JSON."); + object? parsed; + try { parsed = StrictJson.Parse(text); } + catch (StrictJson.JsonException) { throw Invalid("plugin.json is not valid JSON."); } + catch (Exception error) when (error is FormatException or OverflowException) { throw Invalid("plugin.json is not valid JSON."); } + if (parsed is not Dictionary root) throw Invalid("plugin.json must be an object."); + var operations = Objects(root.GetValueOrDefault("operations")); + if (!root.Keys.All(RootKeys.Contains) || operations is null) throw Invalid("plugin.json contains unknown fields."); + foreach (var operation in operations) + { + var parameters = Objects(operation.GetValueOrDefault("parameters")); + if (!operation.Keys.All(OperationKeys.Contains) || parameters is null || parameters.Any(parameter => !parameter.Keys.All(ParameterKeys.Contains))) + { + throw Invalid("Plugin operation contains unknown fields."); + } + } + var manifest = new PluginManifest( + FormatVersion: Integer(root, "formatVersion"), + Id: Text(root, "id"), + Name: Text(root, "name"), + Version: Text(root, "version"), + Summary: Text(root, "summary"), + Symbol: Text(root, "symbol"), + Author: Text(root, "author"), + License: Text(root, "license"), + MinimumRuntimeVersion: Text(root, "minimumRuntimeVersion"), + BaseURL: Text(root, "baseURL"), + Hosts: (root.GetValueOrDefault("hosts") as List ?? throw Malformed("hosts")).Select(host => host as string ?? throw Malformed("hosts")).ToList(), + Operations: operations.Select(operation => new PluginOperation( + Id: Text(operation, "id"), + Title: Text(operation, "title"), + Method: Text(operation, "method"), + Path: Text(operation, "path"), + ResponseType: PluginWireNames.ResponseType(Text(operation, "responseType")) ?? throw Malformed("responseType"), + Parameters: Objects(operation.GetValueOrDefault("parameters"))!.Select(parameter => new PluginParameter( + Id: Text(parameter, "id"), + Label: Text(parameter, "label"), + Location: PluginWireNames.ParameterLocation(Text(parameter, "location")) ?? throw Malformed("location"), + Required: parameter.GetValueOrDefault("required") as bool? ?? throw Malformed("required"), + DefaultValue: parameter.GetValueOrDefault("defaultValue") switch + { + null => null, + string value => value, + _ => throw Malformed("defaultValue"), + })).ToList())).ToList()); + var issues = Validate(manifest); + if (issues.Count > 0) throw Invalid(string.Join("\n", issues.Select(issue => issue.Message))); + return manifest; + } + + /// + /// Semantic rules of Data Source Plugin 1.0. An empty list means the manifest is valid. + /// + /// - formatVersion must be 1 and minimumRuntimeVersion the supported runtime: unsupported + /// versions fail closed instead of being run with guessed semantics. + /// - baseURL must be HTTPS without credentials, query or fragment: plugins never send + /// cleartext requests and never embed secrets in the manifest. + /// - 1–20 unique, well-formed hosts that include the baseURL host and are public Internet + /// names: a plugin can only reach hosts the user can review, never the device or local network. + /// (The public-host rule is enforced by Swift when the request is made; here it is also + /// enforced at import.) + /// - 1–50 operations with unique ids, an upper-case standard method, and an absolute path with + /// no .., query or fragment: an operation cannot leave its declared path space. + /// - At most 30 parameters per operation, unique ids, every {placeholder} declared as a path + /// parameter and every path parameter present in the path; header parameter ids must be + /// valid header names. + /// + public static IReadOnlyList Validate(PluginManifest manifest) + { + var issues = new List(); + void Reject(string id, string message) => issues.Add(new PluginValidationIssue(id, message)); + if (manifest.FormatVersion != PluginManifest.CurrentFormatVersion) Reject("format", $"Unsupported Data Source Plugin format {manifest.FormatVersion.ToString(CultureInfo.InvariantCulture)}."); + if (!Identifier().IsMatch(manifest.Id)) Reject("id", "Plugin id must be a stable 3–128 character identifier."); + if (string.IsNullOrWhiteSpace(manifest.Name) || new StringInfo(manifest.Name).LengthInTextElements > MaximumNameCharacters) Reject("name", "Plugin name must contain 1–80 characters."); + if (!SemanticVersion().IsMatch(manifest.Version)) Reject("version", "Plugin version must use semantic versioning."); + if (manifest.MinimumRuntimeVersion != RuntimeApiVersion) Reject("runtime", $"Plugin requires unsupported runtime {manifest.MinimumRuntimeVersion}."); + var address = PluginUrl.Parse(manifest.BaseURL); + var baseHost = address?.Host?.ToLowerInvariant(); + if (address is null || address.Scheme?.ToLowerInvariant() != "https" || string.IsNullOrEmpty(baseHost) || address.HasUserInfo || address.HasQuery || address.HasFragment) + { + Reject("base_url", "baseURL must be an HTTPS URL without credentials, query, or fragment."); + return issues; + } + if (manifest.Hosts.Count == 0 || manifest.Hosts.Count > MaximumHosts) Reject("hosts", "A plugin must declare 1–20 HTTPS hosts."); + if (manifest.Hosts.Distinct(StringComparer.Ordinal).Count() != manifest.Hosts.Count) Reject("hosts", "Plugin hosts must be unique."); + foreach (var host in manifest.Hosts) + { + if (!IsValidDomainDeclaration(host)) Reject("host", $"Invalid plugin host declaration: {host}."); + // "*.local" covers only private names; test it as a name it would match. + else if (RuntimePolicy.BlockedHostName(host.StartsWith("*.", StringComparison.Ordinal) ? "x" + host[1..] : host)) Reject("host", $"Plugin host must be a public Internet host: {host}."); + } + if (RuntimePolicy.BlockedHostName(PluginUrl.Unbracket(baseHost))) Reject("base_host", "baseURL must use a public Internet host."); + if (!RuntimePolicy.HostMatches(baseHost, manifest.Hosts)) Reject("base_host", "baseURL host must be included in hosts."); + if (manifest.Operations.Count == 0 || manifest.Operations.Count > MaximumOperations) Reject("operations", $"A plugin must contain 1–{MaximumOperations} operations."); + if (manifest.Operations.Select(operation => operation.Id.ToLowerInvariant()).Distinct(StringComparer.Ordinal).Count() != manifest.Operations.Count) Reject("operation_ids", "Operation ids must be unique."); + foreach (var operation in manifest.Operations) + { + var prefix = $"operation.{operation.Id}"; + if (!Identifier().IsMatch(operation.Id)) Reject(prefix, $"Operation id is invalid: {operation.Id}."); + if (!Methods.Contains(operation.Method)) Reject(prefix, $"Operation {operation.Id} has an unsupported HTTP method."); + if (operation.Path.Length == 0 || !operation.Path.StartsWith('/') || operation.Path.Contains("..", StringComparison.Ordinal) || operation.Path.Contains('?') || operation.Path.Contains('#')) + { + Reject(prefix, "Operation path must be an absolute URL path without traversal, query, or fragment."); + } + if (operation.Parameters.Count > MaximumParameters) Reject(prefix, $"Operation {operation.Id} has too many parameters."); + if (operation.Parameters.Select(parameter => parameter.Id.ToLowerInvariant()).Distinct(StringComparer.Ordinal).Count() != operation.Parameters.Count) Reject(prefix, "Operation parameter ids must be unique."); + var parameterIDs = operation.Parameters.Select(parameter => parameter.Id).ToHashSet(StringComparer.Ordinal); + foreach (var parameter in operation.Parameters) + { + var placeholder = "{" + parameter.Id + "}"; + if (!Identifier().IsMatch(parameter.Id)) Reject(prefix, $"Invalid parameter id: {parameter.Id}."); + if (parameter.Location == PluginParameterLocation.Path && !operation.Path.Contains(placeholder, StringComparison.Ordinal)) Reject(prefix, $"Path parameter {parameter.Id} has no matching placeholder."); + if (parameter.Location != PluginParameterLocation.Path && operation.Path.Contains(placeholder, StringComparison.Ordinal)) Reject(prefix, $"Placeholder {parameter.Id} must be a path parameter."); + if (parameter.Location == PluginParameterLocation.Header && !HeaderName().IsMatch(parameter.Id)) Reject(prefix, $"Header parameter {parameter.Id} is invalid."); + } + foreach (Match match in Placeholder().Matches(operation.Path)) + { + var placeholder = match.Groups[1].Value; + if (!parameterIDs.Contains(placeholder)) Reject(prefix, $"Path placeholder {placeholder} has no declared parameter."); + } + } + return issues; + } + + /// + /// ScriptWidgetNetworkPolicy.isValidDomainDeclaration: a lower-case host name, optionally + /// *.-prefixed, with no scheme, path or port, so a declaration cannot smuggle a URL. + /// + public static bool IsValidDomainDeclaration(string value) + { + var normalized = value.ToLowerInvariant(); + return value == normalized && !normalized.Contains("://", StringComparison.Ordinal) && !normalized.Contains('/') && !normalized.Contains(':') && + !normalized.StartsWith('.') && !normalized.EndsWith('.') && HostDeclaration().IsMatch(normalized) && + (!normalized.Contains('*') || (normalized.StartsWith("*.", StringComparison.Ordinal) && normalized.IndexOf('*', 1) < 0)); + } + + private static List>? Objects(object? value) + { + if (value is not List list) return null; + var result = new List>(list.Count); + foreach (var item in list) + { + if (item is not Dictionary map) return null; + result.Add(map); + } + return result; + } + + private static string Text(Dictionary map, string key) => map.GetValueOrDefault(key) as string ?? throw Malformed(key); + + /// Whole JSON numbers only; 1.0 is accepted like Swift's JSONDecoder, 1.5 and "1" are not. + private static int Integer(Dictionary map, string key) => map.GetValueOrDefault(key) switch + { + long value when value is >= int.MinValue and <= int.MaxValue => (int)value, + double value when value == Math.Round(value, MidpointRounding.ToEven) && Math.Abs(value) <= int.MaxValue => (int)value, + _ => throw Malformed(key), + }; + + /// Strict UTF-8 text without a byte order mark, or null when the bytes are malformed. + internal static string? DecodeUtf8(byte[] bytes) + { + try + { + var offset = bytes.Length >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF ? 3 : 0; + return new UTF8Encoding(false, true).GetString(bytes, offset, bytes.Length - offset); + } + catch (DecoderFallbackException) + { + return null; + } + } + + private static PluginException Malformed(string key) => Invalid($"plugin.json field \"{key}\" is missing or has the wrong type."); + + private static PluginException Invalid(string message) => new(PluginErrorKind.InvalidManifest, message); +} + +/// +/// A URL split the way java.net.URI splits it, which is what the Android validator and +/// request builder rely on. It is deliberately stricter than : no spaces, no +/// backslashes, no characters outside the URI grammar, and a host only when the authority is a +/// plain server name, an IPv4 address or a bracketed IPv6 address. +/// +internal sealed class PluginUrl +{ + public string? Scheme { get; private init; } + /// The host as written (brackets included for IPv6), or null when there is none. + public string? Host { get; private init; } + public int Port { get; private init; } = -1; + public bool HasUserInfo { get; private init; } + public string RawPath { get; private init; } = ""; + public bool HasQuery { get; private init; } + public bool HasFragment { get; private init; } + + public static string Unbracket(string host) => host.StartsWith('[') && host.EndsWith(']') ? host[1..^1] : host; + + /// Null when is not a URI at all. + public static PluginUrl? Parse(string text) + { + if (text.Length == 0) return new PluginUrl(); + var hash = text.IndexOf('#'); + var hasFragment = hash >= 0; + var fragment = hasFragment ? text[(hash + 1)..] : ""; + var rest = hasFragment ? text[..hash] : text; + if (!Legal(fragment, brackets: false) || fragment.Contains('#')) return null; + + string? scheme = null; + var stop = rest.IndexOfAny(['/', '?', ':']); + if (stop >= 0 && rest[stop] == ':') + { + scheme = rest[..stop]; + if (scheme.Length == 0 || !char.IsAsciiLetter(scheme[0]) || !scheme.All(character => char.IsAsciiLetterOrDigit(character) || character is '+' or '-' or '.')) return null; + rest = rest[(stop + 1)..]; + if (rest.Length == 0) return null; + // An opaque URI such as "mailto:someone" has no host, path or query. + if (rest[0] != '/') return Legal(rest, brackets: false) ? new PluginUrl { Scheme = scheme, HasFragment = hasFragment } : null; + } + + string? authority = null; + if (rest.StartsWith("//", StringComparison.Ordinal)) + { + var end = rest.IndexOfAny(['/', '?'], 2); + authority = end < 0 ? rest[2..] : rest[2..end]; + rest = end < 0 ? "" : rest[end..]; + if (!Legal(authority, brackets: true)) return null; + } + var mark = rest.IndexOf('?'); + var hasQuery = mark >= 0; + var path = hasQuery ? rest[..mark] : rest; + if (!Legal(path, brackets: false) || (hasQuery && !Legal(rest[(mark + 1)..], brackets: false))) return null; + + string? host = null; + var port = -1; + var hasUserInfo = false; + if (!string.IsNullOrEmpty(authority)) + { + var server = authority; + var at = server.IndexOf('@'); + if (at >= 0) server = server[(at + 1)..]; + string name; + var portText = ""; + if (server.StartsWith('[')) + { + var close = server.IndexOf(']'); + name = close < 0 ? "" : server[..(close + 1)]; + var after = close < 0 ? "" : server[(close + 1)..]; + if (after.Length > 0 && after[0] != ':') name = ""; + else if (after.Length > 0) portText = after[1..]; + if (name.Length > 0 && !(IPAddress.TryParse(name[1..^1], out var literal) && literal.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)) name = ""; + } + else + { + var colon = server.IndexOf(':'); + name = colon < 0 ? server : server[..colon]; + if (colon >= 0) portText = server[(colon + 1)..]; + if (!ServerName(name)) name = ""; + } + // Anything else is a registry-based authority: it has no host, port or user information. + if (name.Length > 0 && portText.All(char.IsAsciiDigit) && (portText.Length == 0 || int.TryParse(portText, NumberStyles.None, CultureInfo.InvariantCulture, out port))) + { + host = name; + hasUserInfo = at >= 0; + if (portText.Length == 0) port = -1; + } + else port = -1; + } + return new PluginUrl { Scheme = scheme, Host = host, Port = port, HasUserInfo = hasUserInfo, RawPath = path, HasQuery = hasQuery, HasFragment = hasFragment }; + } + + /// An IPv4 address, or a host name of letter-digit-hyphen labels whose last label starts with a letter. + private static bool ServerName(string name) + { + if (name.Length == 0) return false; + var labels = name.Split('.'); + if (labels.Length == 4 && labels.All(label => label.Length is >= 1 and <= 3 && label.All(char.IsAsciiDigit) && int.Parse(label, CultureInfo.InvariantCulture) <= 255)) return true; + if (labels[^1].Length == 0) labels = labels[..^1]; + if (labels.Length == 0) return false; + foreach (var label in labels) + { + if (label.Length == 0 || !label.All(character => char.IsAsciiLetterOrDigit(character) || character == '-') || label[0] == '-' || label[^1] == '-') return false; + } + return char.IsAsciiLetter(labels[^1][0]); + } + + /// True when every character may appear in a URI and every % starts a two-digit escape. + private static bool Legal(string part, bool brackets) + { + for (var index = 0; index < part.Length; index++) + { + var character = part[index]; + if (character == '%') + { + if (index + 2 >= part.Length) return false; + if (!char.IsAsciiHexDigit(part[index + 1]) || !char.IsAsciiHexDigit(part[index + 2])) return false; + index += 2; + } + else if (character is '[' or ']') + { + if (!brackets) return false; + } + else if (character < '\u0080') + { + if (!char.IsAsciiLetterOrDigit(character) && !"-_.!~*'();/?:@&=+$,".Contains(character)) return false; + } + else if (char.IsControl(character) || char.IsWhiteSpace(character) || char.IsSurrogate(character) && !Paired(part, index)) + { + return false; + } + } + return true; + } + + private static bool Paired(string text, int index) => + char.IsHighSurrogate(text[index]) ? index + 1 < text.Length && char.IsLowSurrogate(text[index + 1]) : index > 0 && char.IsHighSurrogate(text[index - 1]); +} diff --git a/Windows/src/ScriptWidget.Core/Plugins/PluginStore.cs b/Windows/src/ScriptWidget.Core/Plugins/PluginStore.cs new file mode 100644 index 00000000..0ce7e244 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Plugins/PluginStore.cs @@ -0,0 +1,343 @@ +using System.IO.Compression; +using System.Text; +using System.Text.RegularExpressions; + +namespace ScriptWidget.Core.Plugins; + +/// A plugin the store can serve. Built-in plugins ship with the app and have no directory. +public sealed class InstalledPlugin(PluginManifest manifest, bool builtIn, string? readme) +{ + public PluginManifest Manifest { get; } = manifest; + public bool BuiltIn { get; } = builtIn; + public string? Readme { get; } = readme; +} + +/// +/// Installed Data Source Plugins, ported from DataSourcePluginManager. +/// +/// Each plugin is a directory below the root holding plugin.json and an optional README.md, +/// nothing else. Imported files are untrusted: they are bounded, validated completely before +/// anything is written, staged in a private directory and published with one rename, so +/// a failed import leaves nothing behind and readers never see a partial plugin. +/// +public sealed partial class PluginStore +{ + /// DataSourcePluginManager.maximumPackageBytes: the largest archive or manifest accepted for import. + public const int MaximumPackageBytes = 512 * 1024; + /// Largest README.md stored or shown (Swift bounds it only by the archive limits). + public const int MaximumReadmeBytes = 512 * 1024; + public const string ManifestFile = "plugin.json"; + public const string ReadmeFile = "README.md"; + private const string StagingDirectory = ".staging"; + private const string NotAPackage = "Archive must contain one Data Source Plugin 1.0 package."; + private static readonly HashSet AllowedFiles = new(StringComparer.Ordinal) { ManifestFile, ReadmeFile }; + private static readonly DateTimeOffset FixedEntryTime = new(1980, 1, 1, 0, 0, 0, TimeSpan.Zero); + + private static PluginParameter Parameter(string id, string label, PluginParameterLocation location, bool required, string defaultValue) => + new(id, label, location, required, defaultValue); + + /// The plugins every ScriptWidget build ships, identical to DataSourcePluginManager.builtIns. + public static readonly IReadOnlyList BuiltIns = + [ + new PluginManifest( + FormatVersion: 1, Id: "app.scriptwidget.datasource.open-meteo", Name: "Open-Meteo", Version: "1.0.0", + Summary: "Weather forecasts without an API key.", Symbol: "cloud.sun", Author: "Open-Meteo", License: "CC BY 4.0", + MinimumRuntimeVersion: "1.0", BaseURL: "https://api.open-meteo.com", Hosts: ["api.open-meteo.com"], + Operations: [new PluginOperation("forecast", "Forecast", "GET", "/v1/forecast", PluginResponseType.Json, + [ + Parameter("latitude", "Latitude", PluginParameterLocation.Query, true, "37.7749"), + Parameter("longitude", "Longitude", PluginParameterLocation.Query, true, "-122.4194"), + Parameter("current", "Current fields", PluginParameterLocation.Query, false, "temperature_2m,weather_code"), + Parameter("timezone", "Timezone", PluginParameterLocation.Query, false, "auto"), + ])]), + new PluginManifest( + FormatVersion: 1, Id: "app.scriptwidget.datasource.github", Name: "GitHub Public API", Version: "1.0.0", + Summary: "Public repository metadata from GitHub.", Symbol: "chevron.left.forwardslash.chevron.right", Author: "GitHub", License: "API Terms", + MinimumRuntimeVersion: "1.0", BaseURL: "https://api.github.com", Hosts: ["api.github.com"], + Operations: [new PluginOperation("repository", "Repository", "GET", "/repos/{owner}/{repo}", PluginResponseType.Json, + [ + Parameter("owner", "Owner", PluginParameterLocation.Path, true, "everettjf"), + Parameter("repo", "Repository", PluginParameterLocation.Path, true, "scriptwidget"), + ])]), + ]; + + private readonly string root; + private readonly string staging; + private readonly object gate = new(); + + public PluginStore(string root) + { + this.root = Path.GetFullPath(root); + staging = Path.Combine(this.root, StagingDirectory); + } + + [GeneratedRegex("[^a-z0-9]+", RegexOptions.CultureInvariant)] + private static partial Regex NotSlug(); + [GeneratedRegex(@"\A(?:con|prn|aux|nul|com[0-9]|lpt[0-9])\z", RegexOptions.CultureInvariant)] + private static partial Regex DeviceName(); + + /// Built-in plugins not replaced by an installed one, plus installed plugins, sorted by name. + public IReadOnlyList List() + { + var custom = new List(); + var customIDs = new HashSet(StringComparer.Ordinal); + foreach (var directory in Directories()) + { + if (Load(directory) is { } manifest && customIDs.Add(manifest.Id)) custom.Add(new InstalledPlugin(manifest, false, ReadText(directory, ReadmeFile, MaximumReadmeBytes))); + } + var builtIn = BuiltIns.Where(manifest => !customIDs.Contains(manifest.Id)).Select(manifest => new InstalledPlugin(manifest, true, BuiltInReadme(manifest))); + return builtIn.Concat(custom).OrderBy(plugin => plugin.Manifest.Name, StringComparer.OrdinalIgnoreCase).ToList(); + } + + /// + /// The validated manifest for , or null. A stored manifest is validated again on every + /// read, so a file altered on disk after import is never used unless it still passes. + /// + public PluginManifest? Manifest(string id) + { + foreach (var directory in Directories()) + { + if (Load(directory) is { } manifest && manifest.Id == id) return manifest; + } + return BuiltIns.FirstOrDefault(manifest => manifest.Id == id); + } + + /// + /// Installs a plugin from a .swplugin ZIP archive or a bare plugin.json. + /// + /// Security rules, each failing closed with : + /// - at most 512 KiB of input (DataSourcePluginManager.maximumPackageBytes); + /// - an archive passes the package archive preflight (no traversal, absolute paths, + /// backslashes, symlinks, encrypted entries, case collisions or forged directories); + /// - the package root (archive root or its single directory) holds only plugin.json and an + /// optional README.md, both regular files: a plugin cannot carry code or other payloads; + /// - entries are inflated with their real sizes counted, never trusting declared sizes, and + /// their CRC-32 checked; plugin.json is at most 256 KiB and README.md 512 KiB of UTF-8; + /// - the manifest passes ; + /// - an id that is already installed or built in is refused rather than replaced. + /// + public PluginManifest Install(Stream input) + { + var bytes = ReadBounded(input); + var (manifestBytes, readme) = IsZip(bytes) ? Unpack(bytes) : (bytes, null); + var manifest = PluginValidator.Decode(manifestBytes); + lock (gate) + { + if (Manifest(manifest.Id) is not null) throw new PluginException(PluginErrorKind.InvalidManifest, $"Plugin {manifest.Id} is already installed."); + var work = WorkDirectory("install"); + try + { + var prepared = Path.Combine(work, "plugin"); + Directory.CreateDirectory(prepared); + PluginFiles.WriteNew(Path.Combine(prepared, ManifestFile), manifestBytes); + if (readme is not null) PluginFiles.WriteNew(Path.Combine(prepared, ReadmeFile), readme); + PluginFiles.Publish(prepared, UniqueDirectory(manifest.Name)); + } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) + { + throw Storage("The plugin could not be installed.", error); + } + catch (PluginFiles.StorageException error) + { + throw Storage(error.Message, error); + } + finally + { + PluginFiles.DeleteTree(work); + } + } + return manifest; + } + + /// + /// Removes the installed plugin . The directory is first renamed out of the store, so the + /// plugin disappears atomically. Built-in plugins cannot be deleted. + /// + public void Delete(string id) + { + lock (gate) + { + var directory = DirectoryOf(id) + ?? throw new PluginException(PluginErrorKind.NotFound, BuiltIns.Any(manifest => manifest.Id == id) ? "Built-in plugins cannot be deleted." : "Plugin not found."); + var work = WorkDirectory("delete"); + try + { + Directory.Move(directory, Path.Combine(work, "plugin")); + } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) + { + throw Storage("The plugin could not be deleted.", error); + } + finally + { + PluginFiles.DeleteTree(work); + } + } + } + + /// + /// Writes plugin to as a .swplugin ZIP with plugin.json and, when present, + /// README.md at the archive root. Only those two files are ever exported, so nothing else + /// placed in the plugin directory can leak. Nothing is written unless the archive is complete. + /// + public void Export(string id, Stream output) + { + var files = new List<(string Name, byte[] Bytes)>(); + var directory = DirectoryOf(id); + if (directory is not null) + { + files.Add((ManifestFile, ReadBytes(directory, ManifestFile, PluginValidator.MaximumManifestBytes) ?? throw new PluginException(PluginErrorKind.NotFound, "Plugin not found."))); + if (ReadBytes(directory, ReadmeFile, MaximumReadmeBytes) is { } readme) files.Add((ReadmeFile, readme)); + } + else + { + var builtIn = BuiltIns.FirstOrDefault(manifest => manifest.Id == id) ?? throw new PluginException(PluginErrorKind.NotFound, "Plugin not found."); + files.Add((ManifestFile, Encoding.UTF8.GetBytes(builtIn.ToJson()))); + files.Add((ReadmeFile, Encoding.UTF8.GetBytes(BuiltInReadme(builtIn)))); + } + using var buffer = new MemoryStream(); + using (var zip = new ZipArchive(buffer, ZipArchiveMode.Create, leaveOpen: true, entryNameEncoding: Encoding.UTF8)) + { + foreach (var (name, bytes) in files) + { + var entry = zip.CreateEntry(name, CompressionLevel.Optimal); + entry.LastWriteTime = FixedEntryTime; + using var stream = entry.Open(); + stream.Write(bytes, 0, bytes.Length); + } + } + try + { + buffer.Position = 0; + buffer.CopyTo(output); + } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) + { + throw Storage("Could not create the .swplugin archive.", error); + } + } + + // MARK: - Import + + private static byte[] ReadBounded(Stream input) + { + using var output = new MemoryStream(); + var buffer = new byte[16 * 1024]; + try + { + while (output.Length <= MaximumPackageBytes) + { + var count = input.Read(buffer, 0, buffer.Length); + if (count <= 0) break; + output.Write(buffer, 0, count); + } + } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) + { + throw Storage("The plugin file could not be read.", error); + } + if (output.Length > MaximumPackageBytes) throw Invalid("A plugin package may not exceed 512 KiB."); + if (output.Length == 0) throw Invalid(NotAPackage); + return output.ToArray(); + } + + private static bool IsZip(byte[] bytes) => bytes.Length >= 4 && bytes[0] == (byte)'P' && bytes[1] == (byte)'K'; + + /// Returns the bytes of plugin.json and README.md (or null) from a validated archive. + private static (byte[] Manifest, byte[]? Readme) Unpack(byte[] archive) + { + var entries = PluginArchive.Validate(archive); + // Hidden files and the macOS resource directory are ignored, as in the Swift importer. + var visible = entries.Where(entry => entry.Parts[0] != "__MACOSX" && !entry.Parts.Any(part => part.StartsWith('.'))).ToList(); + var atRoot = visible.Any(entry => entry.Parts.Count == 1 && !entry.IsDirectory && entry.Parts[0] == ManifestFile); + var prefix = 0; + if (!atRoot) + { + var roots = visible.Where(entry => entry.Parts.Count > 1 || entry.IsDirectory).Select(entry => entry.Parts[0]).ToHashSet(StringComparer.Ordinal); + if (roots.Count != 1) throw Invalid(NotAPackage); + prefix = 1; + } + var members = visible.Where(entry => entry.Parts.Count > prefix).ToList(); + if (members.Any(entry => entry.IsDirectory || entry.Parts.Count != prefix + 1 || !AllowedFiles.Contains(entry.Parts[prefix])) || !members.Any(entry => entry.Parts[prefix] == ManifestFile)) + { + throw Invalid("A plugin package allows only plugin.json and optional README.md."); + } + try + { + using var zip = new ZipArchive(new MemoryStream(archive, writable: false), ZipArchiveMode.Read, leaveOpen: false, entryNameEncoding: Encoding.UTF8); + var manifest = Inflate(zip, members.First(entry => entry.Parts[prefix] == ManifestFile), PluginValidator.MaximumManifestBytes, "plugin.json exceeds 256 KiB."); + var readmeEntry = members.FirstOrDefault(entry => entry.Parts[prefix] == ReadmeFile); + var readme = readmeEntry is null ? null : Inflate(zip, readmeEntry, MaximumReadmeBytes, "README.md exceeds 512 KiB."); + if (readme is not null && PluginValidator.DecodeUtf8(readme) is null) throw Invalid("README.md must be UTF-8 text."); + return (manifest, readme); + } + catch (Exception error) when (error is IOException or InvalidDataException or NotSupportedException or ArgumentException) + { + throw Invalid("The plugin archive is corrupt."); + } + } + + /// Inflates one entry in memory, counting real bytes so false size headers cannot expand. + private static byte[] Inflate(ZipArchive zip, PluginArchive.Entry entry, int maximumBytes, string tooLarge) + { + if (entry.UncompressedSize > maximumBytes) throw Invalid(tooLarge); + return PluginArchive.Inflate(zip, entry, maximumBytes); + } + + // MARK: - Storage + + /// Plugin directories: real directories (never links) whose names are not hidden. + private List Directories() + { + if (PluginFiles.RealDirectory(root) is null) return []; + return (PluginFiles.Names(root) ?? []).Where(name => !name.StartsWith('.')).Order(StringComparer.Ordinal) + .Select(name => PluginFiles.RealDirectory(Path.Combine(root, name))).OfType().ToList(); + } + + private string? DirectoryOf(string id) => Directories().FirstOrDefault(directory => Load(directory)?.Id == id); + + private static PluginManifest? Load(string directory) + { + if (ReadBytes(directory, ManifestFile, PluginValidator.MaximumManifestBytes) is not { } bytes) return null; + try { return PluginValidator.Decode(bytes); } + catch (PluginException) { return null; } + } + + /// Reads a regular, singly linked file that is not a symlink and stays inside . + private static byte[]? ReadBytes(string directory, string name, int maximumBytes) => + PluginFiles.ExistingFile(directory, name) is { } file ? PluginFiles.ReadBounded(file, maximumBytes) : null; + + private static string? ReadText(string directory, string name, int maximumBytes) => + ReadBytes(directory, name, maximumBytes) is { } bytes ? PluginValidator.DecodeUtf8(bytes) : null; + + private string WorkDirectory(string prefix) + { + try + { + Directory.CreateDirectory(root); + return PluginFiles.NewWorkDirectory(staging, prefix); + } + catch (Exception error) when (error is IOException or UnauthorizedAccessException or PluginFiles.StorageException) + { + throw Storage("The plugin directory is unavailable.", error); + } + } + + /// Directory names are derived from the plugin name but reduced to [a-z0-9-], so they cannot traverse. + private string UniqueDirectory(string name) + { + var slug = NotSlug().Replace(name.ToLowerInvariant(), "-").Trim('-'); + // Windows only: a device name such as "con" or "com1" is not usable as a directory name. + var baseName = slug.Length == 0 ? "plugin" : DeviceName().IsMatch(slug) ? "plugin-" + slug : slug; + var path = Path.Combine(root, baseName); + var index = 2; + while (PluginFiles.Occupied(path)) { path = Path.Combine(root, $"{baseName}-{index}"); index += 1; } + return path; + } + + private static string BuiltInReadme(PluginManifest manifest) => $"# {manifest.Name}\n\n{manifest.Summary}\n"; + + private static PluginException Invalid(string message) => new(PluginErrorKind.InvalidManifest, message); + + private static PluginException Storage(string message, Exception cause) => new(PluginErrorKind.Storage, message, cause); +} diff --git a/Windows/src/ScriptWidget.Core/Plugins/StrictJson.cs b/Windows/src/ScriptWidget.Core/Plugins/StrictJson.cs new file mode 100644 index 00000000..3bc40a4d --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Plugins/StrictJson.cs @@ -0,0 +1,256 @@ +using System.Globalization; +using System.Text; + +namespace ScriptWidget.Core.Plugins; + +/// +/// RFC 8259 JSON reader and writer for untrusted plugin manifests and data source responses. +/// +/// Lenient readers accept unquoted strings, single quotes, comments and trailing text, and +/// platforms disagree on number types and duplicate keys. A manifest that one platform reads and +/// another refuses is not a contract, so manifests are parsed here: exactly one JSON value, +/// nothing after it, no raw control characters, bounded nesting. +/// +internal static class StrictJson +{ + /// Deepest container nesting accepted; JSONSerialization on Apple platforms stops at 512 as well. + public const int MaximumDepth = 512; + + /// What a document's top-level value is, for callers that only validate. + public enum Kind { Object, Array, String, Number, Boolean, Null } + + public sealed class JsonException(string message) : Exception(message); + + /// + /// Parses into Dictionary<string, object?>, List<object?>, + /// string, long, double, bool and null. + /// + /// A repeated object key is rejected unless : two values for one key let + /// a validator and a later reader disagree about what the document says. + /// + public static object? Parse(string text, bool allowDuplicateKeys = false) => new Reader(text, true, allowDuplicateKeys).Document(); + + /// Checks that is one well-formed JSON value without building it, and returns its kind. + public static Kind Validate(string text) + { + var reader = new Reader(text, false, true); + reader.Document(); + return reader.TopLevel; + } + + /// JSON string literal for . Slashes are not escaped, like .withoutEscapingSlashes in Swift. + public static string Quote(string value) + { + var output = new StringBuilder(value.Length + 2).Append('"'); + foreach (var character in value) + { + switch (character) + { + case '"': output.Append("\\\""); break; + case '\\': output.Append("\\\\"); break; + case '\n': output.Append("\\n"); break; + case '\r': output.Append("\\r"); break; + case '\t': output.Append("\\t"); break; + case '\b': output.Append("\\b"); break; + case '\f': output.Append("\\f"); break; + default: + if (character < ' ' || character == '\u2028' || character == '\u2029') output.Append("\\u").Append(((int)character).ToString("x4", CultureInfo.InvariantCulture)); + else output.Append(character); + break; + } + } + return output.Append('"').ToString(); + } + + private sealed class Reader(string text, bool build, bool allowDuplicateKeys) + { + private int index; + public Kind TopLevel { get; private set; } + + public object? Document() + { + SkipWhitespace(); + TopLevel = KindAt(); + var result = Value(0); + SkipWhitespace(); + if (index != text.Length) throw Fail("Unexpected text after the JSON value"); + return result; + } + + private Kind KindAt() => Peek() switch + { + '{' => Kind.Object, + '[' => Kind.Array, + '"' => Kind.String, + 't' or 'f' => Kind.Boolean, + 'n' => Kind.Null, + _ => Kind.Number, + }; + + private object? Value(int depth) + { + if (depth > MaximumDepth) throw Fail("JSON is nested too deeply"); + return Peek() switch + { + '{' => ObjectValue(depth), + '[' => ArrayValue(depth), + '"' => String(), + 't' => Literal("true", true), + 'f' => Literal("false", false), + 'n' => Literal("null", null), + _ => Number(), + }; + } + + private object? ObjectValue(int depth) + { + index += 1; + var result = build ? new Dictionary(StringComparer.Ordinal) : null; + var seen = !allowDuplicateKeys ? new HashSet(StringComparer.Ordinal) : null; + SkipWhitespace(); + if (Peek() == '}') { index += 1; return result; } + while (true) + { + SkipWhitespace(); + if (Peek() != '"') throw Fail("Expected an object key"); + var key = String(); + if (seen is not null && !seen.Add(key)) throw Fail($"Duplicate object key {key}"); + SkipWhitespace(); + if (Peek() != ':') throw Fail("Expected ':' after an object key"); + index += 1; + SkipWhitespace(); + var member = Value(depth + 1); + if (result is not null) result[key] = member; + SkipWhitespace(); + switch (Peek()) + { + case ',': index += 1; break; + case '}': index += 1; return result; + default: throw Fail("Expected ',' or '}' in an object"); + } + } + } + + private object? ArrayValue(int depth) + { + index += 1; + var result = build ? new List() : null; + SkipWhitespace(); + if (Peek() == ']') { index += 1; return result; } + while (true) + { + SkipWhitespace(); + var member = Value(depth + 1); + result?.Add(member); + SkipWhitespace(); + switch (Peek()) + { + case ',': index += 1; break; + case ']': index += 1; return result; + default: throw Fail("Expected ',' or ']' in an array"); + } + } + } + + private string String() + { + index += 1; + var output = new StringBuilder(); + while (true) + { + if (index >= text.Length) throw Fail("Unterminated string"); + var character = text[index]; + index += 1; + if (character == '"') return output.ToString(); + if (character < ' ') throw Fail("Control character in a string"); + if (character != '\\') { output.Append(character); continue; } + if (index >= text.Length) throw Fail("Unterminated escape"); + var escape = text[index]; + index += 1; + switch (escape) + { + case '"' or '\\' or '/': output.Append(escape); break; + case 'b': output.Append('\b'); break; + case 'f': output.Append('\f'); break; + case 'n': output.Append('\n'); break; + case 'r': output.Append('\r'); break; + case 't': output.Append('\t'); break; + case 'u': + { + if (index + 4 > text.Length) throw Fail("Truncated unicode escape"); + var code = 0; + for (var count = 0; count < 4; count++) + { + var digit = HexDigit(text[index]); + if (digit < 0) throw Fail("Invalid unicode escape"); + code = code * 16 + digit; + index += 1; + } + output.Append((char)code); + break; + } + default: throw Fail("Invalid escape"); + } + } + } + + /// + /// Character.digit(c, 16) of the Android implementation: besides ASCII it accepts every + /// Unicode decimal digit and the full-width Latin letters A to F. + /// + private static int HexDigit(char character) + { + if (character is >= '0' and <= '9') return character - '0'; + if (character is >= 'a' and <= 'f') return character - 'a' + 10; + if (character is >= 'A' and <= 'F') return character - 'A' + 10; + if (character is >= '\uFF21' and <= '\uFF26') return character - '\uFF21' + 10; + if (character is >= '\uFF41' and <= '\uFF46') return character - '\uFF41' + 10; + return character > '\u007F' ? CharUnicodeInfo.GetDecimalDigitValue(character) : -1; + } + + private object Number() + { + var start = index; + if (Peek() == '-') index += 1; + if (Peek() == '0') index += 1; + else if (Peek() is >= '1' and <= '9') Digits(); + else throw Fail("Unexpected character"); + var integral = true; + if (Peek() == '.') { integral = false; index += 1; Digits(); } + if (Peek() is 'e' or 'E') + { + integral = false; + index += 1; + if (Peek() is '+' or '-') index += 1; + Digits(); + } + var literal = text[start..index]; + if (integral && long.TryParse(literal, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var whole)) return whole; + return double.Parse(literal, NumberStyles.Float, CultureInfo.InvariantCulture); + } + + private void Digits() + { + var start = index; + while (Peek() is >= '0' and <= '9') index += 1; + if (index == start) throw Fail("Expected a digit"); + } + + private object? Literal(string word, object? value) + { + if (string.CompareOrdinal(text, index, word, 0, word.Length) != 0) throw Fail("Unexpected character"); + index += word.Length; + return value; + } + + private void SkipWhitespace() + { + while (index < text.Length && text[index] is ' ' or '\n' or '\r' or '\t') index += 1; + } + + /// The current character, or NUL at the end (NUL is never valid outside a string). + private char Peek() => index < text.Length ? text[index] : '\0'; + + private JsonException Fail(string message) => new($"{message} at offset {index}"); + } +} diff --git a/Windows/src/ScriptWidget.Core/Render/Css.cs b/Windows/src/ScriptWidget.Core/Render/Css.cs new file mode 100644 index 00000000..5af92706 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Render/Css.cs @@ -0,0 +1,340 @@ +using System.Globalization; +using System.Numerics; +using System.Text.RegularExpressions; +using Microsoft.Graphics.Canvas; +using Microsoft.Graphics.Canvas.Brushes; +using Windows.Graphics.DirectX; +using Windows.UI; + +namespace ScriptWidget.Core.Render; + +/// A gradient brush together with the bitmap it may be drawn from. +public sealed class GradientBrush(ICanvasBrush brush, CanvasBitmap? bitmap = null) : IDisposable +{ + public ICanvasBrush Brush { get; } = brush; + + public void Dispose() + { + (Brush as IDisposable)?.Dispose(); + bitmap?.Dispose(); + } +} + +/// Parsers for the small CSS subset the shared renderer writes. +public static partial class Css +{ + public static readonly Color Black = Windows.UI.Color.FromArgb(255, 0, 0, 0); + public static readonly Color White = Windows.UI.Color.FromArgb(255, 255, 255, 255); + public static readonly Color Transparent = Windows.UI.Color.FromArgb(0, 0, 0, 0); + public static readonly Color Gray = Windows.UI.Color.FromArgb(255, 136, 136, 136); + + private static readonly Dictionary Named = new() + { + ["white"] = White, ["black"] = Black, ["transparent"] = Transparent, ["none"] = Transparent, + ["red"] = Windows.UI.Color.FromArgb(255, 255, 0, 0), ["green"] = Windows.UI.Color.FromArgb(255, 0, 128, 0), ["blue"] = Windows.UI.Color.FromArgb(255, 0, 0, 255), ["gray"] = Gray, ["grey"] = Gray, + }; + + /// A number as Kotlin's toFloatOrNull reads it: null when the text is not one. + public static float? Number(string? text) + { + if (string.IsNullOrEmpty(text)) return null; + return float.TryParse(text, NumberStyles.Float, CultureInfo.InvariantCulture, out var value) ? value : null; + } + + private static string RemoveSuffix(string text, string suffix) => text.EndsWith(suffix, StringComparison.Ordinal) ? text[..^suffix.Length] : text; + + /// Parses rgba()/rgb()/#hex/named colors. Returns null for none, empty or unknown values. + public static Color? Color(string? value) + { + var text = value?.Trim().ToLowerInvariant(); + if (text is null) return null; + if (text.Length == 0 || text == "none") return null; + if (Named.TryGetValue(text, out var named)) return named; + if (text.StartsWith("rgb", StringComparison.Ordinal)) + { + var open = text.IndexOf('('); + var inner = open < 0 ? text : text[(open + 1)..]; + var close = inner.IndexOf(')'); + if (close >= 0) inner = inner[..close]; + var parts = inner.Split(',', '/').Select(part => part.Trim()).ToArray(); + if (parts.Length < 3) return null; + if (Number(parts[0]) is not { } red || Number(parts[1]) is not { } green || Number(parts[2]) is not { } blue) return null; + var alpha = (parts.Length > 3 ? Number(parts[3]) : null) ?? 1f; + return Windows.UI.Color.FromArgb((byte)(Math.Clamp(alpha, 0f, 1f) * 255f + 0.5f), Channel(red), Channel(green), Channel(blue)); + } + if (text.StartsWith('#')) + { + var hex = text[1..]; + if (!hex.All(Uri.IsHexDigit)) return null; + switch (hex.Length) + { + case 3: + return Windows.UI.Color.FromArgb(255, (byte)(Convert.ToInt32(hex[..1], 16) * 17), (byte)(Convert.ToInt32(hex[1..2], 16) * 17), (byte)(Convert.ToInt32(hex[2..3], 16) * 17)); + case 6: + { + var rgb = Convert.ToUInt32(hex, 16); + return Windows.UI.Color.FromArgb(255, (byte)(rgb >> 16), (byte)(rgb >> 8), (byte)rgb); + } + case 8: + { + var rgba = Convert.ToUInt32(hex, 16); + return Windows.UI.Color.FromArgb((byte)rgba, (byte)(rgba >> 24), (byte)(rgba >> 16), (byte)(rgba >> 8)); + } + default: return null; + } + } + return null; + } + + private static byte Channel(float value) => (byte)Math.Clamp((int)value, 0, 255); + + /// 12px, 12 or a bare number. Returns null for auto, percentages and keywords. + public static float? Pixels(string? value) + { + var text = value?.Trim(); + if (text is null) return null; + if (text.Length == 0 || text.EndsWith('%')) return null; + return Number(RemoveSuffix(text, "px")); + } + + /// A length that may be a percentage of . + public static float? Length(string? value, float basis) + { + var text = value?.Trim(); + if (text is null) return null; + if (text.EndsWith('%')) return Number(text[..^1]) is { } percent ? percent / 100f * basis : null; + return Pixels(text); + } + + /// Splits on commas that are not inside parentheses. + public static List SplitTopLevel(string value, char separator = ',') + { + var parts = new List(); + var depth = 0; + var start = 0; + for (var index = 0; index < value.Length; index++) + { + var character = value[index]; + if (character == '(') depth += 1; + else if (character == ')') depth -= 1; + else if (character == separator && depth == 0) { parts.Add(value[start..index].Trim()); start = index + 1; } + } + parts.Add(value[start..].Trim()); + return parts.Where(part => part.Length > 0).ToList(); + } + + private sealed record Stop(Color Color, float Position); + + /// <color> <position> where position is in px or deg. + private static Stop? ParseStop(string text) + { + text = text.Trim(); + var split = text.LastIndexOf(' '); + if (split <= 0) return null; + if (Color(text[..split]) is not { } color) return null; + if (Number(RemoveSuffix(RemoveSuffix(text[(split + 1)..], "px"), "deg")) is not { } position) return null; + return new Stop(color, position); + } + + /// + /// Brush for a CSS gradient painted in a x box, or null + /// when is not a gradient this renderer emits. moves the + /// box; is the resolution a conic gradient is drawn at. + /// + public static GradientBrush? Gradient(ICanvasResourceCreator device, string? value, float width, float height, Vector2 offset = default, float pixelScale = 1f) + { + var text = value?.Trim(); + if (text is null) return null; + var open = text.IndexOf('('); + if (open < 0 || !text.EndsWith(')')) return null; + var kind = text[..open]; + var arguments = SplitTopLevel(text[(open + 1)..^1]); + if (arguments.Count == 0) return null; + var move = Matrix3x2.CreateTranslation(offset); + switch (kind) + { + case "linear-gradient": + { + var angled = arguments[0].EndsWith("deg", StringComparison.Ordinal); + var degrees = angled ? Number(RemoveSuffix(arguments[0], "deg")) ?? 180f : 180f; + var stopTexts = angled ? arguments.Skip(1).ToList() : arguments; + var parsed = stopTexts.Select(ParseStop).ToList(); + var radians = degrees * Math.PI / 180.0; + var directionX = (float)Math.Sin(radians); + var directionY = -(float)Math.Cos(radians); + var lineLength = Math.Abs(width * directionX) + Math.Abs(height * directionY); + // Stops without positions (a solid two-stop fallback) span the gradient line. + List stops; + if (parsed.All(stop => stop is not null)) stops = parsed.OfType().ToList(); + else + { + stops = []; + for (var index = 0; index < stopTexts.Count; index++) + if (Color(stopTexts[index]) is { } color) stops.Add(new Stop(color, lineLength * index / Math.Max(1, stopTexts.Count - 1))); + } + if (stops.Count < 2) return null; + var first = stops[0].Position; + var last = stops[^1].Position; + if (last - first <= 0f) return null; + Vector2 Point(float distance) => new(width / 2f + (distance - lineLength / 2f) * directionX, height / 2f + (distance - lineLength / 2f) * directionY); + return new GradientBrush(new CanvasLinearGradientBrush(device, GradientStops(stops, Monotonic(stops.Select(stop => (stop.Position - first) / (last - first))))) + { + StartPoint = Point(first), EndPoint = Point(last), Transform = move, + }); + } + case "radial-gradient": + { + if (CenterOf(arguments[0]) is not { } center) return null; + var stops = arguments.Skip(1).Select(ParseStop).OfType().ToList(); + if (stops.Count < 2) return null; + var radius = stops[^1].Position; + if (radius <= 0f) return null; + return new GradientBrush(new CanvasRadialGradientBrush(device, GradientStops(stops, Monotonic(stops.Select(stop => Math.Clamp(stop.Position / radius, 0f, 1f))))) + { + Center = center, RadiusX = radius, RadiusY = radius, Transform = move, + }); + } + case "conic-gradient": + { + if (CenterOf(arguments[0]) is not { } center) return null; + var match = ConicFrom().Match(arguments[0]); + var from = (match.Success ? Number(match.Groups[1].Value) : null) ?? 0f; + var stops = arguments.Skip(1).Select(ParseStop).OfType().ToList(); + if (stops.Count < 2) return null; + return Conic(device, center, from, stops, Monotonic(stops.Select(stop => Math.Clamp(stop.Position / 360f, 0f, 1f))), width, height, move, pixelScale); + } + } + return null; + } + + private static CanvasGradientStop[] GradientStops(List stops, float[] positions) + { + var result = new CanvasGradientStop[stops.Count]; + for (var index = 0; index < result.Length; index++) result[index] = new CanvasGradientStop { Color = stops[index].Color, Position = positions[index] }; + return result; + } + + /// + /// Direct2D has no sweep gradient, so the box is filled pixel by pixel and used as an image brush. + /// CSS measures conic angles clockwise from 12 o'clock. + /// + private static GradientBrush? Conic(ICanvasResourceCreator device, Vector2 center, float from, List stops, float[] positions, float width, float height, Matrix3x2 move, float pixelScale) + { + if (!(width > 0f) || !(height > 0f)) return null; + var scale = Math.Max(0.01f, pixelScale); + // Bounded like every other image this renderer makes. + var largest = Math.Max(width, height) * scale; + if (largest > MaximumConicPixels) scale *= MaximumConicPixels / largest; + var pixelWidth = Math.Max(1, (int)Math.Ceiling(width * scale)); + var pixelHeight = Math.Max(1, (int)Math.Ceiling(height * scale)); + var pixels = new byte[pixelWidth * pixelHeight * 4]; + var at = 0; + for (var row = 0; row < pixelHeight; row++) + { + var dy = (row + 0.5f) / scale - center.Y; + for (var column = 0; column < pixelWidth; column++) + { + var dx = (column + 0.5f) / scale - center.X; + var degrees = MathF.Atan2(dx, -dy) * 180f / MathF.PI - from; + degrees %= 360f; + if (degrees < 0f) degrees += 360f; + var position = degrees / 360f; + var index = 0; + while (index + 1 < positions.Length && positions[index + 1] <= position) index += 1; + float alpha, red, green, blue; + var low = stops[index].Color; + if (position <= positions[0] || index + 1 >= positions.Length) { low = position <= positions[0] ? stops[0].Color : stops[^1].Color; alpha = low.A; red = low.R; green = low.G; blue = low.B; } + else + { + var high = stops[index + 1].Color; + var span = positions[index + 1] - positions[index]; + var amount = span <= 0f ? 0f : (position - positions[index]) / span; + alpha = low.A + (high.A - low.A) * amount; + red = low.R + (high.R - low.R) * amount; + green = low.G + (high.G - low.G) * amount; + blue = low.B + (high.B - low.B) * amount; + } + var coverage = alpha / 255f; + pixels[at++] = (byte)(blue * coverage + 0.5f); + pixels[at++] = (byte)(green * coverage + 0.5f); + pixels[at++] = (byte)(red * coverage + 0.5f); + pixels[at++] = (byte)(alpha + 0.5f); + } + } + var bitmap = CanvasBitmap.CreateFromBytes(device, pixels, pixelWidth, pixelHeight, DirectXPixelFormat.B8G8R8A8UIntNormalized, 96f, CanvasAlphaMode.Premultiplied); + var brush = new CanvasImageBrush(device, bitmap) + { + Transform = Matrix3x2.CreateScale(1f / scale) * move, + ExtendX = CanvasEdgeBehavior.Clamp, ExtendY = CanvasEdgeBehavior.Clamp, Interpolation = CanvasImageInterpolation.Linear, + }; + return new GradientBrush(brush, bitmap); + } + + private const float MaximumConicPixels = 1024f; + + private static Vector2? CenterOf(string argument) + { + var match = GradientCenter().Match(argument); + if (!match.Success) return null; + if (Number(match.Groups[1].Value) is not { } x || Number(match.Groups[2].Value) is not { } y) return null; + return new Vector2(x, y); + } + + /// Direct2D, like Android, wants non-decreasing positions. + private static float[] Monotonic(IEnumerable positions) + { + var result = positions.ToArray(); + var previous = 0f; + for (var index = 0; index < result.Length; index++) + { + var current = float.IsNaN(result[index]) ? previous : Math.Max(previous, result[index]); + result[index] = current; + previous = current; + } + return result; + } + + public sealed record Shadow(float Dx, float Dy, float Blur, Color Color); + + /// drop-shadow(Xpx Ypx Rpx color) from a filter list. + public static Shadow? DropShadow(string? filter) + { + if (filter is null) return null; + var match = DropShadowPattern().Match(filter); + if (!match.Success) return null; + if (Color(match.Groups[4].Value) is not { } color) return null; + if (Number(match.Groups[1].Value) is not { } dx || Number(match.Groups[2].Value) is not { } dy || Number(match.Groups[3].Value) is not { } blur) return null; + return new Shadow(dx, dy, blur, color); + } + + public sealed record BoxShadowValue(bool Inset, float Dx, float Dy, float Blur, float Spread, Color Color); + + /// [inset] X Y blur [spread] color. + public static BoxShadowValue? BoxShadow(string? value) + { + var text = value?.Trim(); + if (text is null) return null; + var inset = text.StartsWith("inset", StringComparison.Ordinal); + if (inset) text = text["inset".Length..].Trim(); + var colorStart = -1; + for (var index = 0; index < text.Length; index++) + if (char.IsLetter(text[index]) && text[index] != 'p' && text[index] != 'x') { colorStart = index; break; } + if (colorStart < 0) return null; + var numbers = Whitespace().Split(text[..colorStart].Trim()).Select(Pixels).OfType().ToList(); + if (Color(text[colorStart..]) is not { } color) return null; + if (numbers.Count < 2) return null; + return new BoxShadowValue(inset, numbers[0], numbers[1], numbers.Count > 2 ? numbers[2] : 0f, numbers.Count > 3 ? numbers[3] : 0f, color); + } + + [GeneratedRegex(@"from\s+(-?[0-9.]+)deg")] + private static partial Regex ConicFrom(); + + [GeneratedRegex(@"at\s+(-?[0-9.eE+-]+)px\s+(-?[0-9.eE+-]+)px")] + private static partial Regex GradientCenter(); + + [GeneratedRegex(@"drop-shadow\(\s*(-?[0-9.]+)px\s+(-?[0-9.]+)px\s+(-?[0-9.]+)px\s+(.+?\))\s*\)")] + private static partial Regex DropShadowPattern(); + + [GeneratedRegex(@"\s+")] + internal static partial Regex Whitespace(); +} diff --git a/Windows/src/ScriptWidget.Core/Render/DomPainter.cs b/Windows/src/ScriptWidget.Core/Render/DomPainter.cs new file mode 100644 index 00000000..c7599a04 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Render/DomPainter.cs @@ -0,0 +1,988 @@ +using System.Numerics; +using System.Security.Cryptography; +using System.Text.Json.Nodes; +using System.Text.RegularExpressions; +using Microsoft.Graphics.Canvas; +using Microsoft.Graphics.Canvas.Effects; +using Microsoft.Graphics.Canvas.Geometry; +using Windows.Foundation; +using Windows.UI; + +namespace ScriptWidget.Core.Render; + +/// A rectangle in points, like Android's RectF. +public readonly record struct RectF(float Left, float Top, float Right, float Bottom) +{ + public float Width => Right - Left; + public float Height => Bottom - Top; + public float CenterX => (Left + Right) / 2f; + public float CenterY => (Top + Bottom) / 2f; + + /// The part shared with , or null when the two do not overlap. + public RectF? Intersect(RectF other) + { + if (!(Left < other.Right && other.Left < Right && Top < other.Bottom && other.Top < Bottom)) return null; + return new RectF(Math.Max(Left, other.Left), Math.Max(Top, other.Top), Math.Min(Right, other.Right), Math.Min(Bottom, other.Bottom)); + } + + internal Rect ToRect() => new(Left, Top, Math.Max(0f, Width), Math.Max(0f, Height)); +} + +public readonly record struct PointF(float X, float Y); + +/// +/// A 3x3 matrix with the conventions of Android's Matrix: points are columns, +/// and PreConcat(m) applies m before this matrix. +/// +public sealed class Matrix +{ + private readonly float[] values = [1f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 1f]; + + public Matrix() { } + + public Matrix(Matrix other) { other.values.CopyTo(values, 0); } + + private Matrix(float a, float b, float c, float d, float e, float f, float g, float h, float i) { values = [a, b, c, d, e, f, g, h, i]; } + + public static Matrix Translation(float x, float y) => new(1f, 0f, x, 0f, 1f, y, 0f, 0f, 1f); + + public static Matrix Scaling(float x, float y) => new(x, 0f, 0f, 0f, y, 0f, 0f, 0f, 1f); + + /// Clockwise on screen for positive degrees, as in CSS and SVG. + public static Matrix Rotation(float degrees) + { + var radians = degrees * MathF.PI / 180f; + var cos = MathF.Cos(radians); + var sin = MathF.Sin(radians); + return new Matrix(cos, -sin, 0f, sin, cos, 0f, 0f, 0f, 1f); + } + + public static Matrix Rotation(float degrees, float centerX, float centerY) + { + var matrix = Translation(centerX, centerY); + matrix.PreConcat(Rotation(degrees)); + matrix.PreTranslate(-centerX, -centerY); + return matrix; + } + + /// Rows: x, y and w of the mapped point. + public static Matrix Projective(float a, float b, float c, float d, float e, float f, float g, float h, float i) => new(a, b, c, d, e, f, g, h, i); + + private static void Multiply(float[] left, float[] right, float[] into) + { + Span result = stackalloc float[9]; + for (var row = 0; row < 3; row++) + for (var column = 0; column < 3; column++) + result[row * 3 + column] = left[row * 3] * right[column] + left[row * 3 + 1] * right[3 + column] + left[row * 3 + 2] * right[6 + column]; + result.CopyTo(into); + } + + public void PreConcat(Matrix other) => Multiply(values, other.values, values); + + public void PostConcat(Matrix other) => Multiply(other.values, values, values); + + public void PreTranslate(float x, float y) => PreConcat(Translation(x, y)); + + public void PostTranslate(float x, float y) => PostConcat(Translation(x, y)); + + public PointF MapPoint(float x, float y) + { + var mappedX = values[0] * x + values[1] * y + values[2]; + var mappedY = values[3] * x + values[4] * y + values[5]; + var w = values[6] * x + values[7] * y + values[8]; + if (w != 1f && w != 0f) { mappedX /= w; mappedY /= w; } + return new PointF(mappedX, mappedY); + } + + /// The bounding box of the mapped corners. + public RectF MapRect(RectF rect) + { + Span corners = [MapPoint(rect.Left, rect.Top), MapPoint(rect.Right, rect.Top), MapPoint(rect.Right, rect.Bottom), MapPoint(rect.Left, rect.Bottom)]; + float left = corners[0].X, right = corners[0].X, top = corners[0].Y, bottom = corners[0].Y; + foreach (var corner in corners) + { + left = Math.Min(left, corner.X); right = Math.Max(right, corner.X); + top = Math.Min(top, corner.Y); bottom = Math.Max(bottom, corner.Y); + } + return new RectF(left, top, right, bottom); + } + + public bool IsAffine => values[6] == 0f && values[7] == 0f && values[8] == 1f; + + /// True when the matrix only moves things. + public bool IsTranslation => values[0] == 1f && values[4] == 1f && values[1] == 0f && values[3] == 0f && values[6] == 0f && values[7] == 0f; + + internal Matrix3x2 ToMatrix3x2() => new(values[0], values[3], values[1], values[4], values[2], values[5]); + + internal Matrix4x4 ToMatrix4x4() => new( + values[0], values[3], 0f, values[6], + values[1], values[4], 0f, values[7], + 0f, 0f, 1f, 0f, + values[2], values[5], 0f, values[8]); +} + +/// +/// The part of android.graphics.Canvas the painters use, over a Win2D drawing +/// session: a save/restore stack of transforms, clips and opacity layers, +/// plus offscreen recordings for shadows, masks and perspective. +/// Lengths are in pixels of the target unless a transform says otherwise. +/// +public sealed class PaintCanvas : IDisposable +{ + private sealed class Perspective(CanvasCommandList list, Matrix4x4 matrix, Matrix3x2 outer) + { + public CanvasCommandList List { get; } = list; + public Matrix4x4 Matrix { get; } = matrix; + public Matrix3x2 Outer { get; } = outer; + } + + private sealed class SaveEntry(Matrix3x2 transform, int layers) + { + public Matrix3x2 Transform { get; } = transform; + public int Layers { get; } = layers; + public Perspective? Perspective { get; set; } + } + + private readonly List sessions = []; + private readonly List<(CanvasCommandList List, int Layers)> recordings = []; + private readonly List layers = []; + private readonly List saves = []; + + /// Takes ownership of . + public PaintCanvas(ICanvasResourceCreator device, CanvasDrawingSession session) + { + Device = device; + Configure(session); + sessions.Add(session); + } + + public ICanvasResourceCreator Device { get; } + + /// The session drawing goes to right now. + public CanvasDrawingSession Session => sessions[^1]; + + public Matrix3x2 Transform { get => Session.Transform; set => Session.Transform = value; } + + private static void Configure(CanvasDrawingSession session) + { + session.Antialiasing = CanvasAntialiasing.Antialiased; + // ClearType needs an opaque background; a widget bitmap is transparent. + session.TextAntialiasing = Microsoft.Graphics.Canvas.Text.CanvasTextAntialiasing.Grayscale; + // Glyphs keep their measured shapes and positions at every scale: no fitting to the pixel grid. + session.TextRenderingParameters = TextRendering; + } + + private static readonly Microsoft.Graphics.Canvas.Text.CanvasTextRenderingParameters TextRendering = + new(Microsoft.Graphics.Canvas.Text.CanvasTextRenderingMode.NaturalSymmetric, Microsoft.Graphics.Canvas.Text.CanvasTextGridFit.Disable); + + public void Save() => saves.Add(new SaveEntry(Transform, layers.Count)); + + public void Restore() + { + if (saves.Count == 0) return; + var entry = saves[^1]; + saves.RemoveAt(saves.Count - 1); + if (entry.Perspective is { } perspective) + { + var list = EndRecord(); + using var effect = new Transform3DEffect { Source = list, TransformMatrix = perspective.Matrix, InterpolationMode = CanvasImageInterpolation.Cubic }; + Session.Transform = perspective.Outer; + Session.DrawImage(effect); + list.Dispose(); + } + PopLayers(entry.Layers); + Session.Transform = entry.Transform; + } + + private void PopLayers(int count) + { + while (layers.Count > count) + { + layers[^1].Dispose(); + layers.RemoveAt(layers.Count - 1); + } + } + + public void Translate(float x, float y) => Transform = Matrix3x2.CreateTranslation(x, y) * Transform; + + public void Scale(float x, float y) => Transform = Matrix3x2.CreateScale(x, y) * Transform; + + public void Rotate(float degrees, float centerX, float centerY) => Transform = Matrix3x2.CreateRotation(degrees * MathF.PI / 180f, new Vector2(centerX, centerY)) * Transform; + + /// + /// Applies before the current transform. A matrix with perspective cannot be a + /// Direct2D transform: what is drawn until the matching is recorded and + /// drawn through a 3D transform effect. + /// + public void Concat(Matrix matrix) + { + if (matrix.IsAffine || saves.Count == 0 || saves[^1].Perspective is not null) + { + Transform = matrix.ToMatrix3x2() * Transform; + return; + } + var outer = Transform; + // Record at the resolution of the target so the effect does not enlarge a low-resolution image. + var scale = DeviceScale(); + BeginRecord(Matrix3x2.CreateScale(scale)); + saves[^1].Perspective = new Perspective(recordings[^1].List, Matrix4x4.CreateScale(1f / scale, 1f / scale, 1f) * matrix.ToMatrix4x4(), outer); + } + + /// Pixels per unit of the current transform, at least 1. + public float DeviceScale() + { + var transform = Transform; + return Math.Max(1f, Math.Max(MathF.Sqrt(transform.M11 * transform.M11 + transform.M12 * transform.M12), MathF.Sqrt(transform.M21 * transform.M21 + transform.M22 * transform.M22))); + } + + /// Clips to until the enclosing is restored. + public void ClipPath(CanvasGeometry geometry) => layers.Add(Session.CreateLayer(1f, geometry)); + + public void ClipRect(RectF bounds) => layers.Add(Session.CreateLayer(1f, bounds.ToRect())); + + /// Like saveLayerAlpha: what is drawn until is blended as one group. + public void SaveLayerAlpha(float opacity) + { + Save(); + layers.Add(Session.CreateLayer(opacity)); + } + + /// Sends drawing to a command list with the given transform (the current one by default). + public void BeginRecord(Matrix3x2? transform = null) + { + var start = transform ?? Transform; + var list = new CanvasCommandList(Device); + var session = list.CreateDrawingSession(); + Configure(session); + session.Transform = start; + recordings.Add((list, layers.Count)); + sessions.Add(session); + } + + /// Ends the innermost recording; the caller owns the list. + public CanvasCommandList EndRecord() + { + var (list, count) = recordings[^1]; + recordings.RemoveAt(recordings.Count - 1); + PopLayers(count); + sessions[^1].Dispose(); + sessions.RemoveAt(sessions.Count - 1); + return list; + } + + /// Draws an image that is already in the coordinates of the session, ignoring the transform. + public void DrawDeviceImage(ICanvasImage image, Vector2 offset = default) + { + var saved = Transform; + Session.Transform = Matrix3x2.Identity; + Session.DrawImage(image, offset); + Session.Transform = saved; + } + + public void Dispose() + { + while (recordings.Count > 0) EndRecord().Dispose(); + PopLayers(0); + saves.Clear(); + foreach (var session in sessions) session.Dispose(); + sessions.Clear(); + } +} + +/// One element recorded by the shared renderer (see render/entry.mjs serialize). +public sealed class DomNode +{ + private static readonly IReadOnlyDictionary Empty = new Dictionary(); + + public DomNode(JsonObject json) + { + Tag = json.OptString("t"); + Svg = json.OptInt("g") == 1; + Style = StringMap(json.OptObject("s")); + Attributes = StringMap(json.OptObject("a")); + Dataset = StringMap(json.OptObject("d")); + Text = json.Has("x") ? json.OptString("x") : null; + var children = json.OptArray("c"); + if (children is null) Children = []; + else + { + var list = new List(children.Count); + foreach (var child in children) list.Add(new DomNode(child as JsonObject ?? throw new FormatException("A recorded element is not an object"))); + Children = list; + } + } + + public string Tag { get; } + public bool Svg { get; } + public IReadOnlyDictionary Style { get; } + public IReadOnlyDictionary Attributes { get; } + public IReadOnlyDictionary Dataset { get; } + public string? Text { get; } + public IReadOnlyList Children { get; } + + private static IReadOnlyDictionary StringMap(JsonObject? json) + { + if (json is null) return Empty; + var result = new Dictionary(json.Count); + foreach (var (key, _) in json) result[key] = json.OptString(key); + return result; + } +} + +/// A tappable area of the widget, in points from the widget's top-left corner. +public sealed record HitRegion(RectF Bounds, JsonObject Interaction); + +/// A <date> text the host may replace with a self-updating view. +public sealed record DateSlot(RectF Bounds, JsonObject Description, TextStyle? Style, Color Color, string Align); + +/// A package GIF the host plays with its own views; it is left out of the bitmap. +public sealed record GifSlot(RectF Bounds, string Source); + +/// +/// A view with a clock or swing animation, drawn at rest into its own bitmap +/// so the host can move it. is the rotation centre (clock) or the +/// resting centre (swing) in widget points; the bitmap is centred on it. +/// are premultiplied BGRA, on a side. +/// +public sealed record MotionSlot(JsonObject Description, PointF Anchor, byte[] Pixels, int PixelSize, float SizePoints); + +/// +/// A stretch of a scrolling list as one image: one or more whole rows, drawn at +/// the list's width. are in points from the piece's top-left corner. +/// +public sealed record ListPiece(byte[] Png, float HeightPoints, IReadOnlyList HitRegions); + +/// +/// A <list> the host scrolls with its own views. is the visible +/// area in widget points; the rows are left out of the widget bitmap. +/// is true when rows were dropped to stay inside the image budget. +/// +public sealed record ListSlot(RectF Bounds, IReadOnlyList Pieces, bool Truncated); + +internal sealed record PendingPiece(Task Png, float Height, IReadOnlyList HitRegions); + +/// +/// Turns list pieces into PNG data. Encoding is the slow part of drawing a +/// long list, so it runs on several threads, and a piece whose pixels did not +/// change since an earlier drawing reuses the data made then. +/// +internal static class PieceEncoder +{ + private static readonly int Threads = Math.Clamp(Environment.ProcessorCount, 2, 4); + /// Pieces waiting to be encoded hold their pixels; the painter waits rather than queue more than this. + private static readonly SemaphoreSlim Waiting = new(Threads * 2); + private const int MaximumCacheBytes = 12 * 1024 * 1024; + private static readonly Dictionary> Cache = new(); + private static readonly LinkedList<(string Key, byte[] Png)> Recent = new(); + private static int cacheBytes; + + /// are premultiplied BGRA. + public static Task Encode(byte[] pixels, int width, int height) + { + Waiting.Wait(); + return Task.Run(() => + { + try + { + var key = $"{width}x{height}:" + Convert.ToHexStringLower(SHA256.HashData(pixels)); + lock (Cache) + { + if (Cache.TryGetValue(key, out var found)) + { + Recent.Remove(found); + Recent.AddFirst(found); + return found.Value.Png; + } + } + var png = PngEncoder.Encode(pixels, width, height); + lock (Cache) + { + if (!Cache.ContainsKey(key) && png.Length <= MaximumCacheBytes) + { + Cache[key] = Recent.AddFirst((key, png)); + cacheBytes += png.Length; + while (cacheBytes > MaximumCacheBytes && Recent.Last is { } oldest) + { + Recent.RemoveLast(); + Cache.Remove(oldest.Value.Key); + cacheBytes -= oldest.Value.Png.Length; + } + } + } + return png; + } + finally + { + Waiting.Release(); + } + }); + } +} + +/// Supplies decoded images for src values: res:<path>, data URLs and fetched remote URLs. +public interface IImageSource +{ + CanvasBitmap? Bitmap(string source, int maximumPixels); +} + +/// +/// Paints a recorded element tree on a canvas. It implements exactly the +/// HTML/CSS and SVG subset that Web/preview-engine/src/render/renderer.js +/// writes: absolutely positioned boxes, text, images and inline SVG. +/// +public sealed partial class DomPainter +{ + private readonly TextEngine text; + private readonly IImageSource images; + /// <date> styles the host draws itself with self-updating views; their text is left out of the bitmap. + private readonly IReadOnlySet liveDateStyles; + /// When true, GIF images are recorded as instead of being drawn as a still frame. + private readonly bool liveGifs; + private readonly SvgPainter svg; + + public DomPainter(TextEngine text, IImageSource images, IReadOnlySet? liveDateStyles = null, bool liveGifs = false) + { + this.text = text; + this.images = images; + this.liveDateStyles = liveDateStyles ?? new HashSet(); + this.liveGifs = liveGifs; + svg = new SvgPainter(text); + } + + public List GifSlots { get; } = []; + /// Filled when is on. + public List MotionSlots { get; } = []; + /// Draw animated views into instead of the widget bitmap. + public bool LiveMotion { get; set; } + /// Filled when is on. + public List ListSlots { get; } = []; + /// Draw the rows of the first <list> into instead of the widget bitmap. + public bool LiveLists { get; set; } + public List HitRegions { get; } = []; + public List DateSlots { get; } = []; + + /// Inside a list row or a moving view everything is drawn into the bitmap; nothing is handed to the host. + private int staticDepth; + private float pixelScale = 1f; + private JsonObject? activeDate; + + private sealed record TextContext( + string Family = "sans-serif", float Size = 17f, int Weight = 400, float? LineHeight = null, + string Align = "left", bool Wrap = true, Color Color = default) + { + public TextStyle Style() => new(Family, Size, Weight, LineHeight ?? Size * 1.2f); + } + + private static string? Get(IReadOnlyDictionary map, string key) => map.TryGetValue(key, out var value) ? value : null; + + private static JsonObject? ParseObject(string? text) => Json.TryParse(text) as JsonObject; + + private static float Opacity(IReadOnlyDictionary style) => Css.Number(Get(style, "opacity")) is { } value ? Math.Clamp(value, 0f, 1f) : 1f; + + /// Paints (the widget element). converts points to pixels. + public void Paint(PaintCanvas canvas, DomNode root, float scale) + { + HitRegions.Clear(); + DateSlots.Clear(); + GifSlots.Clear(); + MotionSlots.Clear(); + ListSlots.Clear(); + staticDepth = 0; + pixelScale = scale; + canvas.Save(); + canvas.Scale(scale, scale); + var width = Css.Pixels(Get(root.Style, "width")) ?? 0f; + var height = Css.Pixels(Get(root.Style, "height")) ?? 0f; + Element(canvas, root, width, height, new TextContext(Color: Css.Black), new Matrix()); + canvas.Restore(); + } + + private void Element(PaintCanvas canvas, DomNode node, float parentWidth, float parentHeight, TextContext inherited, Matrix toWidget) + { + if (node.Tag == "style" || node.Tag.Length == 0) return; + var style = node.Style; + if (node.Svg) + { + var left = Css.Pixels(Get(style, "left")) ?? 0f; + var top = Css.Pixels(Get(style, "top")) ?? 0f; + canvas.Save(); + canvas.Translate(left, top); + svg.Paint(canvas, node, (Get(style, "color") is { } color ? Css.Color(color) : null) ?? inherited.Color); + canvas.Restore(); + return; + } + + if (LiveMotion && staticDepth == 0 && MotionSlots.Count < MaximumMotionSlots) + { + var motion = ParseObject(Get(node.Dataset, "swAnimation")); + if (motion is not null && IsSupportedMotion(motion) && RecordMotion(canvas, node, motion, parentWidth, parentHeight, inherited, toWidget)) return; + } + var absolute = Get(style, "position") == "absolute"; + var x = absolute ? Css.Length(Get(style, "left"), parentWidth) ?? 0f : 0f; + var y = (absolute ? Css.Length(Get(style, "top"), parentHeight) ?? 0f : 0f) + (Css.Pixels(Get(style, "marginTop")) ?? 0f); + var width = Css.Length(Get(style, "width"), parentWidth) ?? parentWidth; + var height = Css.Length(Get(style, "height"), parentHeight) ?? parentHeight; + var bounds = new RectF(0f, 0f, width, height); + + var local = new Matrix(toWidget); + local.PreTranslate(x, y); + canvas.Save(); + canvas.Translate(x, y); + + if (Transform(style, width, height) is { } matrix) { canvas.Concat(matrix); local.PreConcat(matrix); } + + var shadow = Css.DropShadow(Get(style, "filter")); + var opacity = Opacity(style); + if (shadow is not null) + { + PaintWithDropShadow(canvas, node, width, height, inherited, local, shadow, opacity); + canvas.Restore(); + return; + } + var layered = opacity < 1f; + if (layered) canvas.SaveLayerAlpha(opacity); + Content(canvas, node, bounds, inherited, local); + if (layered) canvas.Restore(); + canvas.Restore(); + } + + /// + /// The standard deviation, in the units of the radius, of Android's BlurMaskFilter, + /// so shadows are as soft as in the Android bitmap. + /// + private static float BlurSigma(float radius) => radius > 0f ? 0.57735f * radius + 0.5f : 0f; + + /// Records the element offscreen so its silhouette can cast a blurred shadow. + private void PaintWithDropShadow(PaintCanvas canvas, DomNode node, float width, float height, TextContext inherited, Matrix local, Css.Shadow shadow, float opacity) + { + var deviceScale = canvas.DeviceScale(); + canvas.BeginRecord(); + CanvasCommandList layer; + // Hit regions inside are collected in widget coordinates through `local`. + try { Content(canvas, node, new RectF(0f, 0f, width, height), inherited, local); } + finally { layer = canvas.EndRecord(); } + + using (layer) + using (var blurred = new ShadowEffect { Source = layer, BlurAmount = Math.Min(250f, shadow.Blur > 0f ? Math.Max(0.1f, BlurSigma(shadow.Blur * deviceScale)) : 0f), ShadowColor = shadow.Color }) + { + var layered = opacity < 1f; + if (layered) canvas.SaveLayerAlpha(opacity); + // The offset is given in the element's own units; the recording is in pixels. + var offset = Vector2.TransformNormal(new Vector2(shadow.Dx, shadow.Dy), canvas.Transform); + canvas.DrawDeviceImage(blurred, offset); + canvas.DrawDeviceImage(layer); + if (layered) canvas.Restore(); + } + } + + /// Standard clock hands map to a system clock view; a custom interval is played as rotated frames. + private static bool IsSupportedMotion(JsonObject motion) => motion.OptString("kind") switch + { + "swing" => true, + "clock" => motion.OptString("type") is "clockSecond" or "clockMinute" or "clockMiniute" or "clockHour" or "clock", + _ => false, + }; + + /// Draws into a new transparent bitmap and returns its premultiplied BGRA pixels. + private static byte[] DrawBitmap(ICanvasResourceCreator device, int pixelWidth, int pixelHeight, Action draw) + { + using var target = new CanvasRenderTarget(device, pixelWidth, pixelHeight, 96f); + var session = target.CreateDrawingSession(); + session.Clear(Css.Transparent); + using (var layer = new PaintCanvas(device, session)) draw(layer); + return target.GetPixelBytes(); + } + + /// Draws at rest (no animation transform) into a square bitmap centred on its anchor. + private bool RecordMotion(PaintCanvas canvas, DomNode node, JsonObject motion, float parentWidth, float parentHeight, TextContext inherited, Matrix toWidget) + { + var style = node.Style; + var x = Css.Length(Get(style, "left"), parentWidth) ?? 0f; + var y = Css.Length(Get(style, "top"), parentHeight) ?? 0f; + var width = Css.Length(Get(style, "width"), parentWidth) ?? parentWidth; + var height = Css.Length(Get(style, "height"), parentHeight) ?? parentHeight; + if (width <= 0f || height <= 0f) return false; + var origin = Get(style, "transformOrigin")?.Split(' '); + var clock = motion.OptString("kind") == "clock"; + var anchorX = clock ? Css.Length(origin?.ElementAtOrDefault(0), width) ?? width / 2f : width / 2f; + var anchorY = clock ? Css.Length(origin?.ElementAtOrDefault(1), height) ?? height / 2f : height / 2f; + // A rotating view needs room for every angle; a swinging one only for itself. + (float X, float Y)[] corners = [(0f, 0f), (width, 0f), (0f, height), (width, height)]; + var reach = clock ? corners.Max(corner => MathF.Sqrt((corner.X - anchorX) * (corner.X - anchorX) + (corner.Y - anchorY) * (corner.Y - anchorY))) : Math.Max(width, height) / 2f; + var side = reach * 2f + 4f; + var pixels = (int)MathF.Ceiling(side * pixelScale); + if (pixels <= 0 || pixels > 1024) return false; + // Tap targets inside a moving view are not tracked; the view is drawn only. + var saved = new List(HitRegions); + var opacity = Opacity(style); + byte[] bitmap; + staticDepth += 1; + try + { + bitmap = DrawBitmap(canvas.Device, pixels, pixels, layer => + { + layer.Scale(pixelScale, pixelScale); + layer.Translate(side / 2f - anchorX, side / 2f - anchorY); + if (opacity < 1f) layer.SaveLayerAlpha(opacity); + Content(layer, node, new RectF(0f, 0f, width, height), inherited, new Matrix()); + if (opacity < 1f) layer.Restore(); + }); + } + finally { staticDepth -= 1; } + HitRegions.Clear(); HitRegions.AddRange(saved); + var point = toWidget.MapPoint(x + anchorX, y + anchorY); + MotionSlots.Add(new MotionSlot(motion, point, bitmap, pixels, side)); + return true; + } + + private void Content(PaintCanvas canvas, DomNode node, RectF bounds, TextContext inherited, Matrix toWidget) + { + var style = node.Style; + var width = bounds.Width; + var height = bounds.Height; + var radius = Radius(Get(style, "borderRadius"), width, height); + using var outline = Outline(canvas.Device, style, bounds, radius); + + // An outer shadow lies under the box; an inset one is drawn over its background. + var boxShadow = Css.BoxShadow(Get(style, "boxShadow")); + if (boxShadow is not null && !boxShadow.Inset) BoxShadow(canvas, boxShadow, bounds, radius); + Background(canvas, style, bounds, outline); + if (boxShadow is not null && boxShadow.Inset) BoxShadow(canvas, boxShadow, bounds, radius); + if (Get(style, "border") is { } border && border.Contains("dashed")) + { + var color = Css.Color(border[(border.IndexOf("dashed", StringComparison.Ordinal) + "dashed".Length)..].Trim()) ?? Css.Gray; + using var dashes = new CanvasStrokeStyle { CustomDashStyle = [3f, 3f] }; + canvas.Session.DrawRectangle(new Rect(0.5f, 0.5f, Math.Max(0f, width - 1f), Math.Max(0f, height - 1f)), color, 1f, dashes); + } + + if (ParseObject(Get(node.Dataset, "swInteraction")) is { } interaction) HitRegions.Add(new HitRegion(toWidget.MapRect(bounds), interaction)); + + var clips = Get(style, "overflow") == "hidden" || Get(style, "clipPath") is not null; + if (clips) { canvas.Save(); canvas.ClipPath(outline); } + + var context = MakeTextContext(style, inherited); + var previousDate = activeDate; + if (Get(node.Dataset, "swDate") is { } description) + { + var parsed = ParseObject(description); + var dateStyle = parsed?.OptString("style"); + // A clock view shows the current time, so only a of "now" may use it. + if (parsed is not null && staticDepth == 0 && dateStyle is not null && liveDateStyles.Contains(dateStyle) && (dateStyle != "time" || parsed.OptBoolean("live"))) activeDate = parsed; + } + var date = activeDate; + if (date is not null && style.ContainsKey("fontSize")) + { + // The text element of a live : the host places a clock or timer here. + var textChild = node.Children.FirstOrDefault(child => child.Text is not null); + var color = (textChild is not null && Get(textChild.Style, "color") is { } childColor ? Css.Color(childColor) : null) ?? context.Color; + DateSlots.Add(new DateSlot(toWidget.MapRect(bounds), date, context.Style(), color, context.Align)); + activeDate = previousDate; + if (clips) canvas.Restore(); + return; + } + if (node.Tag == "img") + { + var source = Get(node.Attributes, "src") ?? ""; + if (liveGifs && staticDepth == 0 && source.StartsWith("res:", StringComparison.Ordinal) && source.EndsWith(".gif", StringComparison.OrdinalIgnoreCase) && Get(node.Style, "objectFit") == "contain") + GifSlots.Add(new GifSlot(toWidget.MapRect(bounds), source)); + else Image(canvas, node, bounds); + } + else if (node.Text is not null && node.Children.Count == 0) DrawText(canvas, node.Text, style, bounds, context); + var list = Get(node.Dataset, "swList"); + if (list is null) + { + foreach (var child in node.Children) Element(canvas, child, width, height, context, toWidget); + } + else if (LiveLists && staticDepth == 0 && ListSlots.Count == 0 && toWidget.IsTranslation && width >= 1f && height >= 1f) + { + RecordList(canvas, node, list, bounds, context, toWidget); + } + else + { + // Drawn in place: the rows that fit are visible, the rest are cut off, and so are their tap targets. + var firstHit = HitRegions.Count; + var visible = toWidget.MapRect(bounds); + canvas.Save(); + canvas.ClipRect(bounds); + foreach (var child in node.Children) Element(canvas, child, width, height, context, toWidget); + canvas.Restore(); + var inside = new List(); + for (var index = firstHit; index < HitRegions.Count; index++) + if (HitRegions[index].Bounds.Intersect(visible) is { } shown) inside.Add(HitRegions[index] with { Bounds = shown }); + HitRegions.RemoveRange(firstHit, HitRegions.Count - firstHit); + HitRegions.AddRange(inside); + } + activeDate = previousDate; + if (clips) canvas.Restore(); + } + + /// + /// Draws the rows of as a few images, each holding consecutive whole + /// rows. A row reaches down to the top of the next one, so spacing is part + /// of it. Several rows share an image because a widget update has room for + /// a limited number of small images but passes larger ones by reference. + /// + private void RecordList(PaintCanvas canvas, DomNode node, string description, RectF bounds, TextContext context, Matrix toWidget) + { + var mapped = toWidget.MapRect(bounds); + var rows = node.Children.Where(child => child.Dataset.ContainsKey("swListRow")).ToList(); + var tops = rows.Select(row => Css.Pixels(Get(row.Style, "top")) ?? 0f).ToList(); + var contentHeight = ParseObject(description) is { } parsed ? (float)parsed.OptDouble("contentHeight", 0.0) : 0f; + var bottoms = new List(rows.Count); + for (var index = 0; index < rows.Count; index++) + bottoms.Add(index + 1 < rows.Count ? tops[index + 1] : Math.Max(contentHeight, tops[index] + (Css.Pixels(Get(rows[index].Style, "height")) ?? 0f))); + var width = bounds.Width; + var pixelWidth = Math.Max(1, (int)MathF.Ceiling(width * pixelScale)); + var saved = new List(HitRegions); + var result = new List(); + var pending = new List(); + var bytes = 0L; + var truncated = false; + staticDepth += 1; + try + { + var first = 0; + while (first < rows.Count) + { + // Take rows until the piece is tall enough, without passing the tallest image a view can show. + var last = first; + while (last + 1 < rows.Count && bottoms[last] - tops[first] < MinimumPiecePoints && + MathF.Ceiling((bottoms[last + 1] - tops[first]) * pixelScale) <= MaximumPiecePixels) last += 1; + var top = tops[first]; + var height = Math.Max(1f, bottoms[last] - top); + var pixelHeight = (int)MathF.Ceiling(height * pixelScale); + if (pixelHeight > MaximumPiecePixels) { truncated = true; break; } + HitRegions.Clear(); + var from = first; + var pixels = DrawBitmap(canvas.Device, pixelWidth, pixelHeight, layer => + { + layer.Scale(pixelScale, pixelScale); + layer.Translate(0f, -top); + for (var index = from; index <= last; index++) Element(layer, rows[index], width, Math.Max(contentHeight, bounds.Height), context, Matrix.Translation(0f, -top)); + }); + pending.Add(new PendingPiece(PieceEncoder.Encode(pixels, pixelWidth, pixelHeight), height, HitRegions.ToList())); + first = last + 1; + } + foreach (var piece in pending) + { + var encoded = piece.Png.GetAwaiter().GetResult(); + if (truncated || bytes + encoded.Length > MaximumListBytes) { truncated = true; continue; } + bytes += encoded.Length; + result.Add(new ListPiece(encoded, piece.Height, piece.HitRegions)); + } + } + finally + { + staticDepth -= 1; + HitRegions.Clear(); HitRegions.AddRange(saved); + } + ListSlots.Add(new ListSlot(mapped, result, truncated)); + } + + private static TextContext MakeTextContext(IReadOnlyDictionary style, TextContext inherited) + { + if (!style.ContainsKey("fontFamily") && !style.ContainsKey("fontSize") && !style.ContainsKey("fontWeight") && !style.ContainsKey("lineHeight") && + !style.ContainsKey("textAlign") && !style.ContainsKey("whiteSpace") && !style.ContainsKey("color")) return inherited; + return new TextContext( + Family: Get(style, "fontFamily") ?? inherited.Family, + Size: Css.Pixels(Get(style, "fontSize")) ?? inherited.Size, + Weight: int.TryParse(Get(style, "fontWeight"), out var weight) ? weight : inherited.Weight, + LineHeight: Css.Pixels(Get(style, "lineHeight")) ?? inherited.LineHeight, + Align: Get(style, "textAlign") ?? inherited.Align, + Wrap: Get(style, "whiteSpace") is { } whiteSpace ? whiteSpace != "pre" : inherited.Wrap, + Color: (Get(style, "color") is { } color ? color == "transparent" ? Css.Transparent : Css.Color(color) : null) ?? inherited.Color); + } + + private void DrawText(PaintCanvas canvas, string content, IReadOnlyDictionary style, RectF bounds, TextContext context) + { + if (content.Length == 0) return; + var textStyle = context.Style(); + using var gradient = Get(style, "backgroundClip") == "text" || Get(style, "webkitBackgroundClip") == "text" + ? Css.Gradient(canvas.Device, Get(style, "backgroundImage"), bounds.Width, bounds.Height, default, pixelScale) : null; + if (gradient is null && context.Color.A == 0) return; + using var solid = gradient is null ? new Microsoft.Graphics.Canvas.Brushes.CanvasSolidColorBrush(canvas.Device, context.Color) : null; + var brush = gradient?.Brush ?? solid!; + var lines = text.Lines(content, textStyle, context.Wrap ? bounds.Width : null); + var baseline = text.Baseline(textStyle); + for (var index = 0; index < lines.Count; index++) + { + var line = lines[index]; + var x = context.Align switch + { + "center" => (bounds.Width - line.Width) / 2f, + "right" => bounds.Width - line.Width, + _ => 0f, + }; + text.Draw(canvas.Session, line.Text, textStyle, x, baseline + index * textStyle.Leading, brush); + } + } + + private void Image(PaintCanvas canvas, DomNode node, RectF bounds) + { + if (Get(node.Attributes, "src") is not { } source) return; + var scale = canvas.Transform.M11 is > 0f and var value ? value : 1f; + var pixels = Math.Max(1, (int)Math.Min(int.MaxValue, (double)bounds.Width * scale * bounds.Height * scale)); + var bitmap = images.Bitmap(source, pixels); + if (bitmap is null) return; + var destination = bounds; + var fit = Get(node.Style, "objectFit") ?? "fill"; + var size = bitmap.SizeInPixels; + if (fit is "contain" or "cover") + { + var ratio = (float)size.Width / Math.Max(1u, size.Height); + var boxRatio = bounds.Width / Math.Max(0.0001f, bounds.Height); + var fitWidth = (fit == "contain") == (ratio > boxRatio) ? bounds.Width : bounds.Height * ratio; + var fitHeight = fitWidth / ratio; + destination = new RectF((bounds.Width - fitWidth) / 2f, (bounds.Height - fitHeight) / 2f, (bounds.Width + fitWidth) / 2f, (bounds.Height + fitHeight) / 2f); + } + canvas.Save(); + canvas.ClipRect(bounds); + canvas.Session.DrawImage(bitmap, destination.ToRect(), new Rect(0, 0, size.Width, size.Height), 1f, CanvasImageInterpolation.HighQualityCubic); + canvas.Restore(); + } + + private void Background(PaintCanvas canvas, IReadOnlyDictionary style, RectF bounds, CanvasGeometry outline) + { + var color = Css.Color(Get(style, "backgroundColor")); + if (color is { A: not 0 } solidColor) canvas.Session.FillGeometry(outline, solidColor); + var image = Get(style, "backgroundImage"); + if (image is not null && image != "none" && Get(style, "backgroundClip") != "text" && Get(style, "webkitBackgroundClip") != "text") + { + // Shapes paint a gradient sized for the whole view behind a smaller outline. + var size = Get(style, "backgroundSize")?.Split(' ').Select(Css.Pixels).OfType().ToList(); + var position = Get(style, "backgroundPosition")?.Split(' ').Select(Css.Pixels).OfType().ToList(); + var width = size is { Count: > 0 } ? size[0] : bounds.Width; + var height = size is { Count: > 1 } ? size[1] : bounds.Height; + var offset = position is { Count: 2 } ? new Vector2(position[0], position[1]) : default; + using var gradient = Css.Gradient(canvas.Device, image, width, height, offset, pixelScale); + if (gradient is not null) canvas.Session.FillGeometry(outline, gradient.Brush); + else if (Css.Color(image) is { } solid) canvas.Session.FillGeometry(outline, solid); + } + } + + private static void BoxShadow(PaintCanvas canvas, Css.BoxShadowValue shadow, RectF bounds, float radius) + { + if (shadow.Inset) + { + // `inset 0 0 0 color` is the renderer's one-point inner border. + var width = shadow.Spread > 0f ? shadow.Spread : 1f; + var inset = width / 2f; + var corner = Math.Max(0f, radius - inset); + canvas.Session.DrawRoundedRectangle(new Rect(inset, inset, Math.Max(0f, bounds.Width - width), Math.Max(0f, bounds.Height - width)), corner, corner, shadow.Color, width); + return; + } + // Outer shadow under an opaque shape: draw the blurred shape first. + canvas.Save(); + canvas.Translate(shadow.Dx, shadow.Dy); + if (shadow.Blur > 0f) + { + var sigma = BlurSigma(shadow.Blur / 2f + 0.1f) * canvas.DeviceScale(); + canvas.BeginRecord(); + canvas.Session.FillRoundedRectangle(bounds.ToRect(), radius, radius, shadow.Color); + using var shape = canvas.EndRecord(); + using var blurred = new GaussianBlurEffect { Source = shape, BlurAmount = Math.Min(250f, sigma), BorderMode = EffectBorderMode.Soft }; + canvas.DrawDeviceImage(blurred); + } + else canvas.Session.FillRoundedRectangle(bounds.ToRect(), radius, radius, shadow.Color); + canvas.Restore(); + } + + private static float Radius(string? value, float width, float height) + { + var trimmed = value?.Trim(); + if (trimmed is null) return 0f; + if (trimmed.EndsWith('%')) return Math.Min(width, height) * (Css.Number(trimmed[..^1]) ?? 0f) / 100f; + return Math.Max(0f, Math.Min(Css.Pixels(trimmed) ?? 0f, Math.Min(width, height) / 2f)); + } + + private static CanvasGeometry Outline(ICanvasResourceCreator device, IReadOnlyDictionary style, RectF bounds, float radius) + { + var clip = Get(style, "clipPath"); + var width = Math.Max(0f, bounds.Width); + var height = Math.Max(0f, bounds.Height); + if (clip is not null && clip.StartsWith("circle", StringComparison.Ordinal)) return CanvasGeometry.CreateCircle(device, bounds.CenterX, bounds.CenterY, Math.Min(width, height) / 2f); + if (clip is not null && clip.StartsWith("ellipse", StringComparison.Ordinal)) return CanvasGeometry.CreateEllipse(device, bounds.CenterX, bounds.CenterY, width / 2f, height / 2f); + if (clip is not null && clip.StartsWith("inset", StringComparison.Ordinal)) + { + var round = Math.Min(width, height) / 2f; + return CanvasGeometry.CreateRoundedRectangle(device, bounds.Left, bounds.Top, width, height, round, round); + } + if (Get(style, "borderRadius")?.Trim().EndsWith('%') == true) return CanvasGeometry.CreateEllipse(device, bounds.CenterX, bounds.CenterY, width / 2f, height / 2f); + if (radius > 0f) return CanvasGeometry.CreateRoundedRectangle(device, bounds.Left, bounds.Top, width, height, radius, radius); + return CanvasGeometry.CreateRectangle(device, bounds.Left, bounds.Top, width, height); + } + + /// CSS transform list with its transform-origin, as a matrix in element coordinates. + private static Matrix? Transform(IReadOnlyDictionary style, float width, float height) + { + if (Get(style, "transform") is not { } value) return null; + var origin = Get(style, "transformOrigin")?.Split(' '); + var originX = Css.Length(origin?.ElementAtOrDefault(0), width) ?? width / 2f; + var originY = Css.Length(origin?.ElementAtOrDefault(1), height) ?? height / 2f; + var matrix = new Matrix(); + var perspective = 0f; + foreach (Match match in TransformFunction().Matches(value)) + { + var arguments = match.Groups[2].Value.Split(',').Select(argument => argument.Trim()).ToArray(); + Matrix step; + switch (match.Groups[1].Value) + { + case "rotate": step = Matrix.Rotation(Css.Number(Degrees(arguments[0])) ?? 0f); break; + case "translateX": step = Matrix.Translation(Css.Pixels(arguments[0]) ?? 0f, 0f); break; + case "translateY": step = Matrix.Translation(0f, Css.Pixels(arguments[0]) ?? 0f); break; + case "translate": step = Matrix.Translation(Css.Pixels(arguments[0]) ?? 0f, Css.Pixels(arguments.ElementAtOrDefault(1)) ?? 0f); break; + case "scale": + { + var sx = Css.Number(arguments[0]) ?? 1f; + step = Matrix.Scaling(sx, Css.Number(arguments.ElementAtOrDefault(1)) ?? sx); + break; + } + case "perspective": perspective = Css.Pixels(arguments[0]) ?? 0f; continue; + case "rotate3d": + { + if (arguments.Length < 4) continue; + var axisX = Css.Number(arguments[0]) ?? 0f; + var axisY = Css.Number(arguments[1]) ?? 0f; + var axisZ = Css.Number(arguments[2]) ?? 0f; + var degrees = Css.Number(Degrees(arguments[3])) ?? 0f; + var length = MathF.Sqrt(axisX * axisX + axisY * axisY + axisZ * axisZ); + if (length == 0f) continue; + // Android approximates this with a Camera turned about fixed axes. Here it is the + // CSS rotate3d() matrix itself, applied to the element's plane (z = 0) and + // followed by the perspective division w = 1 - z / distance. + float ux = axisX / length, uy = axisY / length, uz = axisZ / length; + var half = degrees * MathF.PI / 360f; + var sc = MathF.Sin(half) * MathF.Cos(half); + var sq = MathF.Sin(half) * MathF.Sin(half); + var r00 = 1f - 2f * (uy * uy + uz * uz) * sq; + var r01 = 2f * (ux * uy * sq - uz * sc); + var r10 = 2f * (ux * uy * sq + uz * sc); + var r11 = 1f - 2f * (ux * ux + uz * uz) * sq; + var r20 = 2f * (ux * uz * sq - uy * sc); + var r21 = 2f * (uy * uz * sq + ux * sc); + var depth = perspective > 0f ? 1f / perspective : 0f; + step = Matrix.Projective(r00, r01, 0f, r10, r11, 0f, -r20 * depth, -r21 * depth, 1f); + break; + } + default: continue; + } + matrix.PreConcat(step); + } + matrix.PreTranslate(-originX, -originY); + matrix.PostTranslate(originX, originY); + return matrix; + } + + private static string Degrees(string text) => text.EndsWith("deg", StringComparison.Ordinal) ? text[..^3] : text; + + [GeneratedRegex(@"([a-zA-Z0-9]+)\(([^)]*)\)")] + private static partial Regex TransformFunction(); + + private const int MaximumMotionSlots = 8; + /// A piece holds whole rows until it is at least this tall. + private const float MinimumPiecePoints = 160f; + /// One piece is one image; taller than this it could not be uploaded as a texture. + private const int MaximumPiecePixels = 2048; + /// All encoded pieces of one list together. + private const long MaximumListBytes = 1536L * 1024; +} diff --git a/Windows/src/ScriptWidget.Core/Render/FontRegistry.cs b/Windows/src/ScriptWidget.Core/Render/FontRegistry.cs new file mode 100644 index 00000000..32eaf805 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Render/FontRegistry.cs @@ -0,0 +1,120 @@ +using System.Collections.Concurrent; +using Microsoft.Graphics.Canvas.Text; +using Windows.UI.Text; + +namespace ScriptWidget.Core.Render; + +/// +/// One resolved face: the DirectWrite family string, the weight, and the +/// ascent and descent of the font as fractions of its size. +/// +public sealed record Typeface(string Family, int Weight, float Ascent, float Descent); + +/// +/// Resolves the CSS font-family strings the shared renderer emits to +/// DirectWrite families. The default face is the bundled Inter, so layout is +/// the same on every device; Chinese text and emoji fall back to the system +/// fonts through DirectWrite (Noto Sans SC is not shipped on Windows). +/// switches the default face to Segoe UI. +/// +public sealed class FontRegistry +{ + public const string SystemFamily = "Segoe UI"; + private const string MonospaceFamily = "Consolas"; + private const string SerifFamily = "Georgia"; + + private readonly string directory; + private readonly ConcurrentDictionary cache = new(); + private volatile bool useSystemFont; + + /// Folder with Inter.ttf and the display fonts; defaults to assets/fonts next to the program. + public FontRegistry(string? directory = null) + { + this.directory = directory ?? Path.Combine(AppContext.BaseDirectory, "assets", "fonts"); + } + + /// When true, default-design text uses the device's own sans-serif face. + public bool UseSystemFont { get => useSystemFont; set => useSystemFont = value; } + + /// Bundled display fonts selectable with font="custom,<name>,<size>": file and the family name inside it. + private static readonly Dictionary CustomFonts = new() + { + ["apple ]["] = ("Apple ][.ttf", "Apple ]["), ["apple"] = ("Apple ][.ttf", "Apple ]["), + ["larabiefont free"] = ("Larabiefont Free.ttf", "Larabiefont Free"), ["larabiefontfree"] = ("Larabiefont Free.ttf", "Larabiefont Free"), + ["monofonto"] = ("Monofonto.ttf", "Monofonto"), + ["white rabbit"] = ("New.ttf", "White Rabbit"), ["whiterabbit"] = ("New.ttf", "White Rabbit"), ["new"] = ("New.ttf", "White Rabbit"), + ["oneslot"] = ("OneSlot.ttf", "OneSlot"), + ["unispace"] = ("Unispace.ttf", "Unispace"), + }; + + public Typeface Typeface(string family, int weight) + { + var clamped = Math.Clamp(weight, 100, 900); + var key = $"{useSystemFont}|{family}|{clamped}"; + return cache.GetOrAdd(key, _ => Resolve(family, clamped)); + } + + private Typeface Resolve(string family, int weight) + { + var comma = family.IndexOf(','); + var first = (comma < 0 ? family : family[..comma]).Trim().Trim('"'); + var lower = first.ToLowerInvariant(); + if (CustomFonts.TryGetValue(lower, out var custom)) + { + // A display font has one face; its weight is not synthesized, as on Android. + return FontFile(custom.File, custom.Family, 400) ?? Bundled(weight); + } + if (lower == "ui-monospace" || lower.Contains("mono") || lower == "consolas") return SystemFace(MonospaceFamily, weight); + if (lower == "new york" || lower == "ui-serif") return SystemFace(SerifFamily, weight); + if (useSystemFont) return SystemFace(SystemFamily, weight); + return Bundled(weight); + } + + /// Inter is a variable font; DirectWrite picks its named instance nearest to the weight. + private Typeface Bundled(int weight) => FontFile("Inter.ttf", "Inter", weight) ?? SystemFace(SystemFamily, weight); + + /// + /// A font file next to the program. Win2D loads it from the family string + /// <absolute path>#<family name>, which works in an unpackaged process. + /// + private Typeface? FontFile(string file, string family, int weight) + { + var path = Path.Combine(directory, file); + if (!System.IO.File.Exists(path)) return null; + try + { + using var fonts = new CanvasFontSet(new Uri(path)); + var faces = fonts.Fonts; + if (faces.Count == 0) return null; + var face = faces.OrderBy(item => Math.Abs(item.Weight.Weight - weight)).First(); + var result = new Typeface($"{path}#{family}", weight, face.Ascent, face.Descent); + foreach (var item in faces) item.Dispose(); + return result; + } + catch (Exception) + { + return null; + } + } + + private static Typeface SystemFace(string family, int weight) + { + try + { + using var fonts = CanvasFontSet.GetSystemFontSet(); + using var matching = fonts.GetMatchingFonts(family, new FontWeight((ushort)weight), FontStretch.Normal, FontStyle.Normal); + var faces = matching.Fonts; + if (faces.Count > 0) + { + var result = new Typeface(family, weight, faces[0].Ascent, faces[0].Descent); + foreach (var item in faces) item.Dispose(); + return result; + } + } + catch (Exception) + { + // Fall through to typical metrics; the text is still measured and drawn with the same layout. + } + return new Typeface(family, weight, 1.079f, 0.251f); + } +} diff --git a/Windows/src/ScriptWidget.Core/Render/GifFrames.cs b/Windows/src/ScriptWidget.Core/Render/GifFrames.cs new file mode 100644 index 00000000..8d982b5e --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Render/GifFrames.cs @@ -0,0 +1,169 @@ +using Windows.Graphics.Imaging; + +namespace ScriptWidget.Core.Render; + +/// +/// Evenly spaced frames of a GIF and the delay between them. Each frame is +/// x premultiplied BGRA pixels. +/// +public sealed record GifFrames(IReadOnlyList Frames, int Width, int Height, int IntervalMilliseconds); + +/// +/// Samples a GIF into a bounded set of bitmaps for a widget. A widget cannot +/// run a decoder, so the host flips through prepared frames; their total +/// size counts against the widget bitmap budget and is capped here. +/// +public static class GifSampler +{ + public const int MaximumInputBytes = 8 * 1024 * 1024; + public const int MaximumFrames = 24; + private const int MinimumInterval = 80; + + private sealed record Frame(byte[] Pixels, int Left, int Top, int Width, int Height, int Delay, int Disposal); + + /// + /// Returns null when the data is not an animated GIF or cannot be decoded. + /// and are the box in pixels the GIF is fitted into; + /// bounds all frames together. + /// + public static GifFrames? Sample(byte[] data, int width, int height, long maximumBytes) + { + if (data.Length > MaximumInputBytes || width <= 0 || height <= 0) return null; + try { return Decode(data, width, height, maximumBytes); } + catch (Exception) { return null; } + } + + private static T Wait(Windows.Foundation.IAsyncOperation operation) => operation.AsTask().GetAwaiter().GetResult(); + + private static int Property(BitmapPropertySet properties, string name) => + properties.TryGetValue(name, out var value) && value.Value is { } item ? Convert.ToInt32(item, System.Globalization.CultureInfo.InvariantCulture) : 0; + + private static GifFrames? Decode(byte[] data, int width, int height, long maximumBytes) + { + using var stream = new MemoryStream(data, false).AsRandomAccessStream(); + var decoder = Wait(BitmapDecoder.CreateAsync(BitmapDecoder.GifDecoderId, stream)); + var total = (int)decoder.FrameCount; + if (total < 2) return null; + var movieWidth = 0; + var movieHeight = 0; + try + { + var screen = Wait(decoder.BitmapContainerProperties.GetPropertiesAsync(new List { "/logscr/Width", "/logscr/Height" })); + movieWidth = Property(screen, "/logscr/Width"); + movieHeight = Property(screen, "/logscr/Height"); + } + catch (Exception) + { + // Some decoders do not report the logical screen; the frames below give its size. + } + + var frames = new List(total); + var duration = 0; + for (var index = 0; index < total; index++) + { + var frame = Wait(decoder.GetFrameAsync((uint)index)); + var properties = Wait(frame.BitmapProperties.GetPropertiesAsync(new List { "/grctlext/Delay", "/grctlext/Disposal", "/imgdesc/Left", "/imgdesc/Top" })); + var pixels = Wait(frame.GetPixelDataAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, new BitmapTransform(), ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage)).DetachPixelData(); + // GIF delays are hundredths of a second. + var delay = Property(properties, "/grctlext/Delay") * 10; + frames.Add(new Frame(pixels, Property(properties, "/imgdesc/Left"), Property(properties, "/imgdesc/Top"), (int)frame.PixelWidth, (int)frame.PixelHeight, delay, Property(properties, "/grctlext/Disposal"))); + duration += delay; + } + if (duration <= 0) return null; + if (movieWidth <= 0) movieWidth = frames.Max(frame => frame.Left + frame.Width); + if (movieHeight <= 0) movieHeight = frames.Max(frame => frame.Top + frame.Height); + if (movieWidth <= 0 || movieHeight <= 0 || (long)movieWidth * movieHeight > 16L * 1024 * 1024) return null; + + var count = Math.Clamp(duration / MinimumInterval, 2, MaximumFrames); + // Fit inside the box, then shrink further until every frame together fits the budget. + var scale = Math.Min((float)width / movieWidth, (float)height / movieHeight); + long Bytes(float s) => (long)(movieWidth * s) * (long)(movieHeight * s) * 4L * count; + if (Bytes(scale) > maximumBytes) scale *= (float)Math.Sqrt((double)maximumBytes / Bytes(scale)); + var frameWidth = Math.Max(1, (int)(movieWidth * scale)); + var frameHeight = Math.Max(1, (int)(movieHeight * scale)); + + // Compose the frames in order; a sample shows the frame on screen at its time. + var canvas = new byte[movieWidth * movieHeight * 4]; + byte[]? beforeFrame = null; + var result = new List(count); + var composed = -1; + var frameEnd = 0; + for (var sample = 0; sample < count; sample++) + { + var time = (int)((long)duration * sample / count); + while (composed < 0 || (time >= frameEnd && composed + 1 < frames.Count)) + { + if (composed >= 0) Dispose(canvas, frames[composed], beforeFrame, movieWidth, movieHeight); + composed += 1; + var next = frames[composed]; + beforeFrame = next.Disposal == 3 ? (byte[])canvas.Clone() : null; + Compose(canvas, next, movieWidth, movieHeight); + frameEnd += next.Delay; + } + result.Add(Resize(canvas, movieWidth, movieHeight, frameWidth, frameHeight)); + } + return new GifFrames(result, frameWidth, frameHeight, Math.Max(MinimumInterval, duration / count)); + } + + /// What a frame leaves behind: 2 clears its area, 3 puts back what was there before it. + private static void Dispose(byte[] canvas, Frame frame, byte[]? before, int width, int height) + { + if (frame.Disposal == 3 && before is not null) { before.CopyTo(canvas, 0); return; } + if (frame.Disposal != 2) return; + for (var row = Math.Max(0, frame.Top); row < Math.Min(height, frame.Top + frame.Height); row++) + { + var from = Math.Max(0, frame.Left); + var to = Math.Min(width, frame.Left + frame.Width); + if (to > from) Array.Clear(canvas, (row * width + from) * 4, (to - from) * 4); + } + } + + private static void Compose(byte[] canvas, Frame frame, int width, int height) + { + for (var row = 0; row < frame.Height; row++) + { + var targetRow = frame.Top + row; + if (targetRow < 0 || targetRow >= height) continue; + for (var column = 0; column < frame.Width; column++) + { + var targetColumn = frame.Left + column; + if (targetColumn < 0 || targetColumn >= width) continue; + var source = (row * frame.Width + column) * 4; + // GIF pixels are opaque or fully transparent. + if (source + 3 >= frame.Pixels.Length || frame.Pixels[source + 3] == 0) continue; + Buffer.BlockCopy(frame.Pixels, source, canvas, (targetRow * width + targetColumn) * 4, 4); + } + } + } + + /// Averages the source pixels each target pixel covers. + private static byte[] Resize(byte[] source, int width, int height, int targetWidth, int targetHeight) + { + if (width == targetWidth && height == targetHeight) return (byte[])source.Clone(); + var result = new byte[targetWidth * targetHeight * 4]; + Span sums = stackalloc int[4]; + for (var row = 0; row < targetHeight; row++) + { + var top = (int)((long)row * height / targetHeight); + var bottom = Math.Max(top + 1, (int)((long)(row + 1) * height / targetHeight)); + for (var column = 0; column < targetWidth; column++) + { + var left = (int)((long)column * width / targetWidth); + var right = Math.Max(left + 1, (int)((long)(column + 1) * width / targetWidth)); + sums.Clear(); + var samples = 0; + for (var y = top; y < bottom && y < height; y++) + for (var x = left; x < right && x < width; x++) + { + var at = (y * width + x) * 4; + sums[0] += source[at]; sums[1] += source[at + 1]; sums[2] += source[at + 2]; sums[3] += source[at + 3]; + samples += 1; + } + if (samples == 0) continue; + var into = (row * targetWidth + column) * 4; + for (var channel = 0; channel < 4; channel++) result[into + channel] = (byte)((sums[channel] + samples / 2) / samples); + } + } + return result; + } +} diff --git a/Windows/src/ScriptWidget.Core/Render/RoundedCorners.cs b/Windows/src/ScriptWidget.Core/Render/RoundedCorners.cs new file mode 100644 index 00000000..67d2b92c --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Render/RoundedCorners.cs @@ -0,0 +1,53 @@ +namespace ScriptWidget.Core.Render; + +/// +/// Rounds the corners of a rendered widget. The renderer draws the widget as +/// a full rectangle, as on Android, where the launcher clips it; on Windows +/// the host owns the window, so it cuts the corners itself. +/// +public static class RoundedCorners +{ + /// Corner radius of a desktop widget, in points. + public const float DefaultRadius = 20; + + /// + /// Makes the corners of a premultiplied BGRA bitmap transparent, with an + /// antialiased edge. The bitmap is changed in place. + /// + /// Corner radius in pixels. + public static void Apply(byte[] pixels, int width, int height, float radius) + { + if (radius <= 0 || width <= 0 || height <= 0 || pixels.Length < width * height * 4) return; + radius = Math.Min(radius, Math.Min(width, height) / 2f); + var reach = (int)Math.Ceiling(radius); + for (var row = 0; row < reach; row++) + { + for (var column = 0; column < reach; column++) + { + // Distance of the pixel centre from the corner circle's centre. + var dx = radius - (column + 0.5f); + var dy = radius - (row + 0.5f); + if (dx <= 0 || dy <= 0) continue; + var distance = MathF.Sqrt(dx * dx + dy * dy); + var coverage = Math.Clamp(radius - distance + 0.5f, 0f, 1f); + if (coverage >= 1f) continue; + var right = width - 1 - column; + var bottom = height - 1 - row; + Scale(pixels, (row * width + column) * 4, coverage); + if (right != column) Scale(pixels, (row * width + right) * 4, coverage); + if (bottom == row) continue; + Scale(pixels, (bottom * width + column) * 4, coverage); + if (right != column) Scale(pixels, (bottom * width + right) * 4, coverage); + } + } + } + + private static void Scale(byte[] pixels, int index, float coverage) + { + // Premultiplied color: every channel scales with the coverage. + pixels[index] = (byte)(pixels[index] * coverage + 0.5f); + pixels[index + 1] = (byte)(pixels[index + 1] * coverage + 0.5f); + pixels[index + 2] = (byte)(pixels[index + 2] * coverage + 0.5f); + pixels[index + 3] = (byte)(pixels[index + 3] * coverage + 0.5f); + } +} diff --git a/Windows/src/ScriptWidget.Core/Render/SvgPainter.cs b/Windows/src/ScriptWidget.Core/Render/SvgPainter.cs new file mode 100644 index 00000000..f8ea16ee --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Render/SvgPainter.cs @@ -0,0 +1,473 @@ +using System.Numerics; +using System.Text.RegularExpressions; +using Microsoft.Graphics.Canvas; +using Microsoft.Graphics.Canvas.Effects; +using Microsoft.Graphics.Canvas.Geometry; +using Windows.UI; + +namespace ScriptWidget.Core.Render; + +/// +/// Paints the inline SVG the shared renderer emits: icons, rings, gauges, +/// charts and shape strokes. Presentation attributes inherit from ancestors +/// as in SVG; currentColor resolves to the element's CSS color. +/// +public sealed partial class SvgPainter(TextEngine text) +{ + private sealed record Attributes( + string Fill = "black", string Stroke = "none", float StrokeWidth = 1f, + string LineCap = "butt", string LineJoin = "miter", string FillRule = "nonzero", + float FontSize = 16f, string FontFamily = "sans-serif", int FontWeight = 400, string TextAnchor = "start") + { + public Attributes Merged(DomNode node) + { + var attributes = node.Attributes; + if (attributes.Count == 0) return this; + return new Attributes( + Fill: Get(attributes, "fill") ?? Fill, + Stroke: Get(attributes, "stroke") ?? Stroke, + StrokeWidth: Css.Number(Get(attributes, "stroke-width")) ?? StrokeWidth, + LineCap: Get(attributes, "stroke-linecap") ?? LineCap, + LineJoin: Get(attributes, "stroke-linejoin") ?? LineJoin, + FillRule: Get(attributes, "fill-rule") ?? FillRule, + FontSize: Css.Pixels(Get(attributes, "font-size")) ?? FontSize, + FontFamily: Get(attributes, "font-family") ?? FontFamily, + FontWeight: int.TryParse(Get(attributes, "font-weight"), out var weight) ? weight : FontWeight, + TextAnchor: Get(attributes, "text-anchor") ?? TextAnchor); + } + } + + private Color currentColor = Css.Black; + private Dictionary masks = new(); + + private static string? Get(IReadOnlyDictionary map, string key) => map.TryGetValue(key, out var value) ? value : null; + + private static List Numbers(string? value) => + value is null ? [] : Css.Whitespace().Replace(value.Trim(), " ").Split([' ', ','], StringSplitOptions.RemoveEmptyEntries).Select(Css.Number).OfType().ToList(); + + /// Paints an <svg> element with its top-left corner at the canvas origin. + public void Paint(PaintCanvas canvas, DomNode root, Color cssColor) + { + currentColor = cssColor; + masks = new Dictionary(); + CollectMasks(root, masks); + if (Css.Number(Get(root.Attributes, "width")) is not { } width) return; + if (Css.Number(Get(root.Attributes, "height")) is not { } height) return; + canvas.Save(); + if (Get(root.Style, "transform") is { } value && RootRotation().Match(value) is { Success: true } rotation && Css.Number(rotation.Groups[1].Value) is { } degrees) + canvas.Rotate(degrees, width / 2f, height / 2f); + var viewBox = Get(root.Attributes, "viewBox") is { } box ? Numbers(box) : null; + if (viewBox is { Count: 4 } && viewBox[2] > 0f && viewBox[3] > 0f) + { + var scale = Math.Min(width / viewBox[2], height / viewBox[3]); + canvas.Translate((width - viewBox[2] * scale) / 2f, (height - viewBox[3] * scale) / 2f); + canvas.Scale(scale, scale); + canvas.Translate(-viewBox[0], -viewBox[1]); + } + var attributes = new Attributes().Merged(root); + foreach (var child in root.Children) Draw(canvas, child, attributes); + canvas.Restore(); + } + + private static void CollectMasks(DomNode node, Dictionary into) + { + if (node.Tag == "mask" && Get(node.Attributes, "id") is { } id) into[id] = node; + foreach (var child in node.Children) CollectMasks(child, into); + } + + private void Draw(PaintCanvas canvas, DomNode node, Attributes inherited) + { + if (node.Tag == "defs" || node.Tag == "mask" || node.Tag.Length == 0) return; + var attributes = inherited.Merged(node); + DomNode? mask = null; + if (Get(node.Attributes, "mask") is { } reference && MaskReference().Match(reference) is { Success: true } found) masks.TryGetValue(found.Groups[1].Value, out mask); + var opacity = Css.Number(Get(node.Attributes, "opacity")) is { } value ? Math.Clamp(value, 0f, 1f) : 1f; + + canvas.Save(); + if (Get(node.Attributes, "transform") is { } transform) canvas.Concat(Transform(transform)); + // A masked drawing is recorded so the mask can be applied to all of it at once. + if (mask is not null) canvas.BeginRecord(); + else if (opacity < 1f) canvas.SaveLayerAlpha(opacity); + + try + { + if (node.Tag == "g") foreach (var child in node.Children) Draw(canvas, child, attributes); + else if (node.Tag == "text") DrawText(canvas, node, attributes); + else DrawPath(canvas, node, attributes); + } + catch when (mask is not null) + { + canvas.EndRecord().Dispose(); + throw; + } + + if (mask is not null) + { + // An SVG mask keeps the drawing where the mask is light: the mask's + // luminance becomes the alpha of the drawing. + using var drawing = canvas.EndRecord(); + canvas.BeginRecord(); + CanvasCommandList shape; + try + { + var maskAttributes = new Attributes().Merged(mask); + foreach (var child in mask.Children) Draw(canvas, child, maskAttributes); + } + finally { shape = canvas.EndRecord(); } + using (shape) + using (var luminance = new LuminanceToAlphaEffect { Source = shape }) + using (var masked = new AlphaMaskEffect { Source = drawing, AlphaMask = luminance }) + { + if (opacity < 1f) + { + using var faded = new OpacityEffect { Source = masked, Opacity = opacity }; + canvas.DrawDeviceImage(faded); + } + else canvas.DrawDeviceImage(masked); + } + } + else if (opacity < 1f) canvas.Restore(); + canvas.Restore(); + } + + private static float Number(DomNode node, string name, float fallback = 0f) => Css.Number(Get(node.Attributes, name)) ?? fallback; + + private static CanvasFilledRegionDetermination Winding(Attributes attributes) => + attributes.FillRule == "evenodd" ? CanvasFilledRegionDetermination.Alternate : CanvasFilledRegionDetermination.Winding; + + private static CanvasGeometry? Shape(ICanvasResourceCreator device, DomNode node, Attributes attributes) + { + switch (node.Tag) + { + case "path": + if (Get(node.Attributes, "d") is not { } data) return null; + try { return SvgPath.Parse(device, data, Winding(attributes)); } + catch (Exception error) when (error is FormatException or ArgumentException) { return null; } + case "rect": + { + var x = Number(node, "x"); var y = Number(node, "y"); + var width = Number(node, "width"); var height = Number(node, "height"); + var rx = Math.Min(Css.Number(Get(node.Attributes, "rx")) ?? Css.Number(Get(node.Attributes, "ry")) ?? 0f, width / 2f); + var ry = Math.Min(Css.Number(Get(node.Attributes, "ry")) ?? rx, height / 2f); + return width <= 0f || height <= 0f ? null : RoundedRect(device, x, y, width, height, rx, ry); + } + case "circle": + { + var r = Number(node, "r"); + return Oval(device, Number(node, "cx"), Number(node, "cy"), r, r); + } + case "ellipse": return Oval(device, Number(node, "cx"), Number(node, "cy"), Number(node, "rx"), Number(node, "ry")); + case "line": + { + using var builder = new CanvasPathBuilder(device); + builder.BeginFigure(Number(node, "x1"), Number(node, "y1")); + builder.AddLine(Number(node, "x2"), Number(node, "y2")); + builder.EndFigure(CanvasFigureLoop.Open); + return CanvasGeometry.CreatePath(builder); + } + case "polyline" or "polygon": + { + var values = Numbers(Get(node.Attributes, "points")); + if (values.Count < 4) return null; + using var builder = new CanvasPathBuilder(device); + builder.SetFilledRegionDetermination(Winding(attributes)); + builder.BeginFigure(values[0], values[1]); + for (var index = 2; index < values.Count - 1; index += 2) builder.AddLine(values[index], values[index + 1]); + builder.EndFigure(node.Tag == "polygon" ? CanvasFigureLoop.Closed : CanvasFigureLoop.Open); + return CanvasGeometry.CreatePath(builder); + } + default: return null; + } + } + + /// An oval that starts at 3 o'clock and runs clockwise, as SVG circles do; dash offsets depend on it. + private static CanvasGeometry Oval(ICanvasResourceCreator device, float cx, float cy, float rx, float ry) + { + using var builder = new CanvasPathBuilder(device); + builder.BeginFigure(cx + rx, cy); + if (rx > 0f && ry > 0f) + { + builder.AddArc(new Vector2(cx - rx, cy), rx, ry, 0f, CanvasSweepDirection.Clockwise, CanvasArcSize.Small); + builder.AddArc(new Vector2(cx + rx, cy), rx, ry, 0f, CanvasSweepDirection.Clockwise, CanvasArcSize.Small); + } + builder.EndFigure(CanvasFigureLoop.Closed); + return CanvasGeometry.CreatePath(builder); + } + + /// SVG rectangles start at (x + rx, y) and run clockwise; dash offsets depend on it. + private static CanvasGeometry RoundedRect(ICanvasResourceCreator device, float x, float y, float width, float height, float rx, float ry) + { + using var builder = new CanvasPathBuilder(device); + if (rx <= 0f || ry <= 0f) + { + builder.BeginFigure(x, y); + builder.AddLine(x + width, y); builder.AddLine(x + width, y + height); builder.AddLine(x, y + height); + builder.EndFigure(CanvasFigureLoop.Closed); + return CanvasGeometry.CreatePath(builder); + } + void Corner(float toX, float toY) => builder.AddArc(new Vector2(toX, toY), rx, ry, 0f, CanvasSweepDirection.Clockwise, CanvasArcSize.Small); + builder.BeginFigure(x + rx, y); + builder.AddLine(x + width - rx, y); + Corner(x + width, y + ry); + builder.AddLine(x + width, y + height - ry); + Corner(x + width - rx, y + height); + builder.AddLine(x + rx, y + height); + Corner(x, y + height - ry); + builder.AddLine(x, y + ry); + Corner(x + rx, y); + builder.EndFigure(CanvasFigureLoop.Closed); + return CanvasGeometry.CreatePath(builder); + } + + private Color? ResolveColor(string value) => value == "currentColor" ? currentColor : Css.Color(value); + + private void DrawPath(PaintCanvas canvas, DomNode node, Attributes attributes) + { + using var path = Shape(canvas.Device, node, attributes); + if (path is null) return; + var fillOpacity = Css.Number(Get(node.Attributes, "fill-opacity")) is { } fillValue ? Math.Clamp(fillValue, 0f, 1f) : 1f; + var strokeOpacity = Css.Number(Get(node.Attributes, "stroke-opacity")) is { } strokeValue ? Math.Clamp(strokeValue, 0f, 1f) : 1f; + if (node.Tag != "line" && ResolveColor(attributes.Fill) is { A: not 0 } fill) canvas.Session.FillGeometry(path, WithOpacity(fill, fillOpacity)); + if (ResolveColor(attributes.Stroke) is { A: not 0 } stroke && attributes.StrokeWidth > 0f) + { + using var strokeStyle = new CanvasStrokeStyle + { + StartCap = Cap(attributes.LineCap), EndCap = Cap(attributes.LineCap), DashCap = Cap(attributes.LineCap), + LineJoin = attributes.LineJoin switch { "round" => CanvasLineJoin.Round, "bevel" => CanvasLineJoin.Bevel, _ => CanvasLineJoin.MiterOrBevel }, + MiterLimit = 4f, + }; + if (Get(node.Attributes, "stroke-dasharray") is { } dashes) + { + var values = Numbers(dashes); + if (values.Count > 0 && values.Any(item => item > 0f)) + { + // pathLength rescales dash lengths to the real outline length. + var unit = Css.Number(Get(node.Attributes, "pathLength")) is { } pathLength and > 0f ? path.ComputePathLength() / pathLength : 1f; + var even = values.Count % 2 == 0 ? values : values.Concat(values).ToList(); + var intervals = new float[even.Count]; + for (var index = 0; index < intervals.Length; index++) intervals[index] = Math.Max(even[index] * unit, index % 2 == 0 ? 0.0001f : 0f); + var total = intervals.Sum(); + var offset = (Css.Number(Get(node.Attributes, "stroke-dashoffset")) ?? 0f) * unit; + var phase = total > 0f ? (offset % total + total) % total : 0f; + // A zero-length dash with round caps would still draw a dot. + if (even[0] <= 0f) return; + // Direct2D measures dashes in stroke widths. + for (var index = 0; index < intervals.Length; index++) intervals[index] /= attributes.StrokeWidth; + strokeStyle.CustomDashStyle = intervals; + strokeStyle.DashOffset = phase / attributes.StrokeWidth; + } + } + canvas.Session.DrawGeometry(path, WithOpacity(stroke, strokeOpacity), attributes.StrokeWidth, strokeStyle); + } + } + + private static CanvasCapStyle Cap(string name) => name switch { "round" => CanvasCapStyle.Round, "square" => CanvasCapStyle.Square, _ => CanvasCapStyle.Flat }; + + private static Color WithOpacity(Color color, float opacity) => + opacity >= 1f ? color : Windows.UI.Color.FromArgb((byte)(color.A * opacity + 0.5f), color.R, color.G, color.B); + + private void DrawText(PaintCanvas canvas, DomNode node, Attributes attributes) + { + if (node.Text is not { } content) return; + if (ResolveColor(attributes.Fill) is not { } fill) return; + var style = new TextStyle(attributes.FontFamily, attributes.FontSize, attributes.FontWeight, attributes.FontSize * 1.2f); + var x = Number(node, "x"); + if (attributes.TextAnchor == "middle") x -= text.Measure(content, style) / 2f; + else if (attributes.TextAnchor == "end") x -= text.Measure(content, style); + using var brush = new Microsoft.Graphics.Canvas.Brushes.CanvasSolidColorBrush(canvas.Device, fill); + text.Draw(canvas.Session, content, style, x, Number(node, "y"), brush); + } + + /// SVG transform list: translate, scale, rotate. + private static Matrix Transform(string value) + { + var matrix = new Matrix(); + foreach (Match match in TransformFunction().Matches(value)) + { + var arguments = Numbers(match.Groups[2].Value); + float Argument(int index, float fallback) => index < arguments.Count ? arguments[index] : fallback; + Matrix step; + switch (match.Groups[1].Value) + { + case "translate": step = Matrix.Translation(Argument(0, 0f), Argument(1, 0f)); break; + case "scale": step = Matrix.Scaling(Argument(0, 1f), Argument(1, Argument(0, 1f))); break; + case "rotate": step = arguments.Count >= 3 ? Matrix.Rotation(arguments[0], arguments[1], arguments[2]) : Matrix.Rotation(Argument(0, 0f)); break; + default: continue; + } + matrix.PreConcat(step); + } + return matrix; + } + + [GeneratedRegex(@"rotate\((-?[0-9.]+)deg\)")] + private static partial Regex RootRotation(); + + [GeneratedRegex(@"url\(#(.+?)\)")] + private static partial Regex MaskReference(); + + [GeneratedRegex(@"([a-zA-Z]+)\(([^)]*)\)")] + private static partial Regex TransformFunction(); +} + +/// +/// SVG path data to a Direct2D geometry: the Windows counterpart of +/// androidx PathParser. All commands are supported, absolute and relative. +/// +internal static class SvgPath +{ + public static CanvasGeometry Parse(ICanvasResourceCreator device, string data, CanvasFilledRegionDetermination winding) + { + using var builder = new CanvasPathBuilder(device); + builder.SetFilledRegionDetermination(winding); + var at = 0; + var open = false; + float x = 0f, y = 0f, startX = 0f, startY = 0f, controlX = 0f, controlY = 0f; + var previous = ' '; + var command = ' '; + + void SkipSeparators() + { + while (at < data.Length && (char.IsWhiteSpace(data[at]) || data[at] == ',')) at += 1; + } + bool MoreNumbers() + { + SkipSeparators(); + return at < data.Length && (char.IsDigit(data[at]) || data[at] is '-' or '+' or '.'); + } + float Next() + { + SkipSeparators(); + var start = at; + if (at < data.Length && data[at] is '-' or '+') at += 1; + var dot = false; + while (at < data.Length && (char.IsDigit(data[at]) || (data[at] == '.' && !dot))) { if (data[at] == '.') dot = true; at += 1; } + if (at < data.Length && data[at] is 'e' or 'E') + { + var exponent = at + 1; + if (exponent < data.Length && data[exponent] is '-' or '+') exponent += 1; + if (exponent < data.Length && char.IsDigit(data[exponent])) + { + at = exponent; + while (at < data.Length && char.IsDigit(data[at])) at += 1; + } + } + return Css.Number(data[start..at]) ?? throw new FormatException("Malformed SVG path number"); + } + // Arc flags are single digits that may be written without separators. + bool Flag() + { + SkipSeparators(); + if (at >= data.Length || data[at] is not ('0' or '1')) throw new FormatException("Malformed SVG arc flag"); + return data[at++] == '1'; + } + void Begin() + { + if (open) return; + builder.BeginFigure(x, y); + startX = x; startY = y; + open = true; + } + void End(CanvasFigureLoop loop) + { + if (!open) return; + builder.EndFigure(loop); + open = false; + } + + while (true) + { + SkipSeparators(); + if (at >= data.Length) break; + if (char.IsLetter(data[at])) command = data[at++]; + else if (command == ' ') throw new FormatException("SVG path data must start with a command"); + // More coordinates after a move are lines. + else if (command == 'M') command = 'L'; + else if (command == 'm') command = 'l'; + var relative = char.IsLower(command); + var baseX = relative ? x : 0f; + var baseY = relative ? y : 0f; + switch (char.ToUpperInvariant(command)) + { + case 'M': + End(CanvasFigureLoop.Open); + x = baseX + Next(); y = baseY + Next(); + Begin(); + break; + case 'Z': + End(CanvasFigureLoop.Closed); + x = startX; y = startY; + break; + case 'L': + Begin(); + x = baseX + Next(); y = baseY + Next(); + builder.AddLine(x, y); + break; + case 'H': + Begin(); + x = baseX + Next(); + builder.AddLine(x, y); + break; + case 'V': + Begin(); + y = baseY + Next(); + builder.AddLine(x, y); + break; + case 'C': + { + Begin(); + float x1 = baseX + Next(), y1 = baseY + Next(), x2 = baseX + Next(), y2 = baseY + Next(); + x = baseX + Next(); y = baseY + Next(); + builder.AddCubicBezier(new Vector2(x1, y1), new Vector2(x2, y2), new Vector2(x, y)); + controlX = x2; controlY = y2; + break; + } + case 'S': + { + Begin(); + var smooth = previous is 'C' or 'S'; + float x1 = smooth ? 2f * x - controlX : x, y1 = smooth ? 2f * y - controlY : y; + float x2 = baseX + Next(), y2 = baseY + Next(); + x = baseX + Next(); y = baseY + Next(); + builder.AddCubicBezier(new Vector2(x1, y1), new Vector2(x2, y2), new Vector2(x, y)); + controlX = x2; controlY = y2; + break; + } + case 'Q': + { + Begin(); + controlX = baseX + Next(); controlY = baseY + Next(); + x = baseX + Next(); y = baseY + Next(); + builder.AddQuadraticBezier(new Vector2(controlX, controlY), new Vector2(x, y)); + break; + } + case 'T': + { + Begin(); + var smooth = previous is 'Q' or 'T'; + controlX = smooth ? 2f * x - controlX : x; controlY = smooth ? 2f * y - controlY : y; + x = baseX + Next(); y = baseY + Next(); + builder.AddQuadraticBezier(new Vector2(controlX, controlY), new Vector2(x, y)); + break; + } + case 'A': + { + Begin(); + float rx = Math.Abs(Next()), ry = Math.Abs(Next()), rotation = Next(); + var large = Flag(); + var sweep = Flag(); + var fromX = x; var fromY = y; + x = baseX + Next(); y = baseY + Next(); + if (fromX == x && fromY == y) break; + if (rx == 0f || ry == 0f) { builder.AddLine(x, y); break; } + builder.AddArc(new Vector2(x, y), rx, ry, rotation * MathF.PI / 180f, sweep ? CanvasSweepDirection.Clockwise : CanvasSweepDirection.CounterClockwise, large ? CanvasArcSize.Large : CanvasArcSize.Small); + break; + } + default: throw new FormatException($"Unknown SVG path command {command}"); + } + previous = char.ToUpperInvariant(command); + if (previous == 'Z' && MoreNumbers()) throw new FormatException("Numbers after a close command"); + } + End(CanvasFigureLoop.Open); + return CanvasGeometry.CreatePath(builder); + } +} diff --git a/Windows/src/ScriptWidget.Core/Render/TextEngine.cs b/Windows/src/ScriptWidget.Core/Render/TextEngine.cs new file mode 100644 index 00000000..3149e42d --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Render/TextEngine.cs @@ -0,0 +1,148 @@ +using Microsoft.Graphics.Canvas; +using Microsoft.Graphics.Canvas.Brushes; +using Microsoft.Graphics.Canvas.Text; +using Windows.UI.Text; + +namespace ScriptWidget.Core.Render; + +/// Font description shared by measurement and drawing. Sizes are in points. +public readonly record struct TextStyle(string Family, float Size, int Weight, float Leading); + +public sealed record TextLine(string Text, float Width); + +/// +/// Text measurement and line breaking. The shared layout asks for sizes +/// through this class and the painter draws with it, so what was measured is +/// what gets drawn: both go through the same DirectWrite text format, with +/// natural (unhinted) glyph advances, no trimming and no pixel snapping. +/// All values are in points; the painter scales the canvas. +/// Use an instance on one thread at a time. +/// +public sealed class TextEngine(FontRegistry fonts) +{ + private readonly Dictionary<(string Family, int Weight, float Size), CanvasTextFormat> formats = new(); + /// Text layouts are device independent; any device can create them. Asked for each time, so a lost device is replaced. + private static CanvasDevice Device => CanvasDevice.GetSharedDevice(); + + /// The DirectWrite format of a style: the Windows counterpart of Android's TextPaint. + public CanvasTextFormat Paint(TextStyle style) + { + var typeface = fonts.Typeface(style.Family, style.Weight); + var size = style.Size > 0f && float.IsFinite(style.Size) ? style.Size : 0.01f; + var key = (typeface.Family, typeface.Weight, size); + if (formats.TryGetValue(key, out var existing)) return existing; + if (formats.Count > MaximumFormats) + { + foreach (var format in formats.Values) format.Dispose(); + formats.Clear(); + } + var created = new CanvasTextFormat + { + FontFamily = typeface.Family, + FontSize = size, + FontWeight = new FontWeight((ushort)typeface.Weight), + FontStyle = FontStyle.Normal, + FontStretch = FontStretch.Normal, + WordWrapping = CanvasWordWrapping.NoWrap, + HorizontalAlignment = CanvasHorizontalAlignment.Left, + VerticalAlignment = CanvasVerticalAlignment.Top, + TrimmingGranularity = CanvasTextTrimmingGranularity.None, + // Color emoji, and glyphs placed where they were measured rather than on whole pixels. + Options = DrawOptions, + }; + formats[key] = created; + return created; + } + + /// Width of on one line, trailing spaces included. + public float Measure(string text, TextStyle style) + { + if (text.Length == 0) return 0f; + using var layout = new CanvasTextLayout(Device, text, Paint(style), 0f, 0f); + return (float)layout.LayoutBoundsIncludingTrailingWhitespace.Width; + } + + /// + /// Lines of in a box points wide. Explicit newlines always + /// break. With a width, text wraps like CSS white-space: pre-wrap with + /// overflow-wrap: break-word; without one it does not wrap. + /// + public List Lines(string text, TextStyle style, float? width) + { + var result = new List(); + foreach (var paragraph in text.Split('\n')) + { + if (paragraph.Length == 0) { result.Add(new TextLine("", 0f)); continue; } + var whole = Measure(paragraph, style); + if (width is not { } limit || whole <= limit + 0.01f) + { + result.Add(new TextLine(paragraph, whole)); + continue; + } + // Break at word boundaries, and inside a word only when the word alone does not fit. + // Every line gets the same height, so the line a point falls on is known without asking for + // line metrics: the WinRT array of line metrics cannot be marshalled under native AOT. + var lineHeight = Math.Max(style.Size * 1.5f, 1f); + using var layout = new CanvasTextLayout(Device, paragraph, Paint(style), Math.Max(limit, 0.01f), 0f) + { + WordWrapping = CanvasWordWrapping.EmergencyBreak, + LineSpacingMode = CanvasLineSpacingMode.Uniform, + LineSpacing = lineHeight, + LineSpacingBaseline = lineHeight * 0.8f, + }; + var count = layout.LineCount; + var start = 0; + for (var index = 1; index <= count; index++) + { + // The first character of the next line; the end of the text after the last line. + var end = paragraph.Length; + if (index < count && layout.HitTest(0f, (index + 0.5f) * lineHeight, out var region)) end = Math.Clamp(region.CharacterIndex, start, paragraph.Length); + if (end <= start) continue; + var line = paragraph[start..end].TrimEnd(' '); + result.Add(new TextLine(line, Measure(line, style))); + start = end; + } + if (start < paragraph.Length) + { + var rest = paragraph[start..].TrimEnd(' '); + result.Add(new TextLine(rest, Measure(rest, style))); + } + } + return result; + } + + /// First baseline below the top of a line box of the style's leading (CSS half-leading). + public float Baseline(TextStyle style) + { + var typeface = fonts.Typeface(style.Family, style.Weight); + var ascent = typeface.Ascent * style.Size; + var descent = typeface.Descent * style.Size; + return (style.Leading - (ascent + descent)) / 2f + ascent; + } + + /// + /// Draws one line with its left edge at and its baseline at , + /// using the layout that uses. + /// + public void Draw(CanvasDrawingSession session, string text, TextStyle style, float x, float baseline, ICanvasBrush brush) + { + if (text.Length == 0) return; + // A fallback font may be taller than the style's font and would move the line. A fixed line box + // puts the baseline where this code says, whatever fonts the line ends up using. (Reading the + // line's own baseline from its metrics is not possible under native AOT.) + var ascent = fonts.Typeface(style.Family, style.Weight).Ascent * style.Size; + var height = Math.Max(style.Size * 2f, 1f); + var offset = Math.Min(Math.Max(ascent, 0f), height); + using var layout = new CanvasTextLayout(Device, text, Paint(style), 0f, 0f) + { + Options = DrawOptions, + LineSpacingMode = CanvasLineSpacingMode.Uniform, + LineSpacing = height, + LineSpacingBaseline = offset, + }; + session.DrawTextLayout(layout, x, baseline - offset, brush); + } + + private const int MaximumFormats = 512; + private const CanvasDrawTextOptions DrawOptions = CanvasDrawTextOptions.EnableColorFont | CanvasDrawTextOptions.NoPixelSnap; +} diff --git a/Windows/src/ScriptWidget.Core/Render/WidgetRenderer.cs b/Windows/src/ScriptWidget.Core/Render/WidgetRenderer.cs new file mode 100644 index 00000000..e55da543 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Render/WidgetRenderer.cs @@ -0,0 +1,441 @@ +using System.Buffers.Binary; +using System.Globalization; +using System.IO.Compression; +using System.Text.Json.Nodes; +using System.Text.RegularExpressions; +using Microsoft.Graphics.Canvas; +using ScriptWidget.Core.Js; +using ScriptWidget.Core.Runtime; + +namespace ScriptWidget.Core.Render; + +/// What to draw and at what size. Lengths are in points (device-independent pixels). +public sealed class RenderRequest +{ + public required string Tree { get; init; } + public required float Width { get; init; } + public required float Height { get; init; } + /// Pixels per point. + public required float Density { get; init; } + public string Family { get; init; } = "medium"; + public bool DarkMode { get; init; } + public IPackageFiles? Files { get; init; } + /// Remote image URLs that were fetched under the package's network policy, with the paths of their files. + public IReadOnlyDictionary RemoteImages { get; init; } = new Dictionary(); + /// Fixed clock for deterministic output; null uses the real clock. + public long? NowMilliseconds { get; init; } + /// Bitmap budget in bytes for this render; the output is scaled down to fit. + public long MaximumBitmapBytes { get; init; } = long.MaxValue; + /// <date> styles left out of the bitmap because the host shows them with self-updating views. + public IReadOnlySet LiveDateStyles { get; init; } = new HashSet(); + /// Leave <gif> out of the bitmap; the host plays it (see ). + public bool LiveGifs { get; init; } + /// Leave clock and swing animations out of the bitmap; the host moves them (see ). + public bool LiveMotion { get; init; } + /// Leave the rows of the first <list> out of the bitmap; the host scrolls them (see ). Android only; Windows hosts leave it off. + public bool LiveLists { get; init; } + /// System theme colors behind the material.<role> names; null leaves them unknown. Android only; Windows hosts leave it off. + public JsonObject? Colors { get; init; } +} + +public sealed class RenderOutput +{ + /// Premultiplied BGRA pixels, x , rows from the top, no padding. + public required byte[] Pixels { get; init; } + public required int PixelWidth { get; init; } + public required int PixelHeight { get; init; } + /// Pixels per point actually used (lower than requested when the budget forced a smaller bitmap). + public required float Scale { get; init; } + public required IReadOnlyList HitRegions { get; init; } + public required IReadOnlyList DateSlots { get; init; } + public required IReadOnlyList GifSlots { get; init; } + public required IReadOnlyList MotionSlots { get; init; } + public required IReadOnlyList ListSlots { get; init; } + public required JsonArray Diagnostics { get; init; } + public required long LayoutMilliseconds { get; init; } + public required long PaintMilliseconds { get; init; } + + /// The bitmap as PNG data. + public byte[] EncodePng() => PngEncoder.Encode(Pixels, PixelWidth, PixelHeight); +} + +/// Writes premultiplied BGRA pixels as an RGBA PNG, without a graphics device. +public static class PngEncoder +{ + private static readonly uint[] CrcTable = MakeCrcTable(); + + public static byte[] Encode(byte[] premultipliedBgra, int width, int height) + { + if (width <= 0 || height <= 0 || premultipliedBgra.Length < (long)width * height * 4) throw new ArgumentException("Pixel data does not match the size"); + var stride = width * 4 + 1; + var raw = new byte[stride * height]; + for (var row = 0; row < height; row++) + { + var from = row * width * 4; + var into = row * stride + 1; + for (var column = 0; column < width; column++, from += 4, into += 4) + { + var alpha = premultipliedBgra[from + 3]; + if (alpha == 0) continue; + if (alpha == 255) + { + raw[into] = premultipliedBgra[from + 2]; raw[into + 1] = premultipliedBgra[from + 1]; raw[into + 2] = premultipliedBgra[from]; + } + else + { + raw[into] = Straight(premultipliedBgra[from + 2], alpha); raw[into + 1] = Straight(premultipliedBgra[from + 1], alpha); raw[into + 2] = Straight(premultipliedBgra[from], alpha); + } + raw[into + 3] = alpha; + } + } + using var output = new MemoryStream(raw.Length / 4 + 128); + output.Write([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]); + Span header = stackalloc byte[13]; + BinaryPrimitives.WriteInt32BigEndian(header, width); + BinaryPrimitives.WriteInt32BigEndian(header[4..], height); + header[8] = 8; header[9] = 6; header[10] = 0; header[11] = 0; header[12] = 0; + Chunk(output, "IHDR", header); + using (var compressed = new MemoryStream(raw.Length / 4 + 64)) + { + using (var zlib = new ZLibStream(compressed, CompressionLevel.Optimal, leaveOpen: true)) zlib.Write(raw); + Chunk(output, "IDAT", compressed.GetBuffer().AsSpan(0, (int)compressed.Length)); + } + Chunk(output, "IEND", []); + return output.ToArray(); + } + + private static byte Straight(byte value, byte alpha) => (byte)Math.Min(255, (value * 255 + alpha / 2) / alpha); + + private static void Chunk(Stream output, string type, ReadOnlySpan data) + { + Span word = stackalloc byte[4]; + BinaryPrimitives.WriteInt32BigEndian(word, data.Length); + output.Write(word); + Span name = [(byte)type[0], (byte)type[1], (byte)type[2], (byte)type[3]]; + output.Write(name); + output.Write(data); + BinaryPrimitives.WriteUInt32BigEndian(word, ~Crc(data, Crc(name, 0xFFFFFFFF))); + output.Write(word); + } + + private static uint Crc(ReadOnlySpan data, uint crc) + { + foreach (var item in data) crc = CrcTable[(crc ^ item) & 0xFF] ^ (crc >> 8); + return crc; + } + + private static uint[] MakeCrcTable() + { + var table = new uint[256]; + for (uint index = 0; index < 256; index++) + { + var value = index; + for (var bit = 0; bit < 8; bit++) value = (value & 1) != 0 ? 0xEDB88320 ^ (value >> 1) : value >> 1; + table[index] = value; + } + return table; + } +} + +/// Decodes package, inline and fetched images for one render. Use it on the render thread only. +public sealed class PackageImageSource(IPackageFiles? files, IReadOnlyDictionary remote, CanvasDevice device) : IImageSource, IDisposable +{ + private readonly Dictionary decoded = new(); + private readonly Dictionary cache = new(); + + private byte[]? Bytes(string source) + { + if (source.StartsWith("res:", StringComparison.Ordinal)) return files?.ReadBytes(source["res:".Length..]); + if (source.StartsWith("data:", StringComparison.Ordinal)) + { + var marker = source.IndexOf("base64,", StringComparison.Ordinal); + try { return Convert.FromBase64String(marker < 0 ? source : source[(marker + "base64,".Length)..]); } + catch (FormatException) { return null; } + } + if (!remote.TryGetValue(source, out var path)) return null; + try + { + var file = new FileInfo(path); + return file.Exists && file.Length <= MaximumImageBytes ? File.ReadAllBytes(path) : null; + } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) { return null; } + } + + /// The whole image, decoded once per render; null when the source cannot be decoded. + private CanvasBitmap? Decode(string source) + { + if (decoded.TryGetValue(source, out var existing)) return existing; + CanvasBitmap? bitmap = null; + var data = Bytes(source); + if (data is { Length: > 0 }) + { + try + { + using var stream = new MemoryStream(data, false).AsRandomAccessStream(); + // 96 DPI: one unit of the bitmap is one pixel. + bitmap = CanvasBitmap.LoadAsync(device, stream, 96f).AsTask().GetAwaiter().GetResult(); + var size = bitmap.SizeInPixels; + if (size.Width == 0 || size.Height == 0) { bitmap.Dispose(); bitmap = null; } + } + catch (Exception) + { + // Not an image the system can decode. + bitmap = null; + } + } + decoded[source] = bitmap; + return bitmap; + } + + /// Natural size in pixels, or null when the source cannot be decoded. + public int[]? Size(string source) + { + var bitmap = Decode(source); + if (bitmap is null) return null; + var size = bitmap.SizeInPixels; + return [(int)size.Width, (int)size.Height]; + } + + public CanvasBitmap? Bitmap(string source, int maximumPixels) + { + var key = $"{source}@{maximumPixels}"; + if (cache.TryGetValue(key, out var existing)) return existing; + var result = Reduce(Decode(source), maximumPixels); + cache[key] = result; + return result; + } + + private CanvasBitmap? Reduce(CanvasBitmap? bitmap, int maximumPixels) + { + if (bitmap is null) return null; + var size = bitmap.SizeInPixels; + var sample = 1; + // Keep no more than about four times the pixels that will be shown. + while ((size.Width / (sample * 2)) * (long)(size.Height / (sample * 2)) >= maximumPixels) sample *= 2; + if (sample == 1) return bitmap; + try + { + var width = Math.Max(1u, (uint)(size.Width / sample)); + var height = Math.Max(1u, (uint)(size.Height / sample)); + var target = new CanvasRenderTarget(device, width, height, 96f); + using (var session = target.CreateDrawingSession()) + { + session.Clear(Css.Transparent); + session.DrawImage(bitmap, new Windows.Foundation.Rect(0, 0, width, height), new Windows.Foundation.Rect(0, 0, size.Width, size.Height), 1f, CanvasImageInterpolation.HighQualityCubic); + } + return target; + } + catch (Exception) + { + return bitmap; + } + } + + public void Dispose() + { + foreach (var bitmap in cache.Values.Concat(decoded.Values).Distinct()) bitmap?.Dispose(); + cache.Clear(); + decoded.Clear(); + } + + private const long MaximumImageBytes = 16L * 1024 * 1024; +} + +/// +/// Turns a canonical element tree into a bitmap. Layout runs in the shared +/// web renderer inside a long-lived QuickJS context (trusted code only); the +/// recorded elements are painted with . Layout, +/// text measurement and painting all happen on one thread with a large stack. +/// +public sealed partial class WidgetRenderer : IDisposable +{ + private readonly SharedScripts scripts; + private readonly TextEngine text; + private readonly JsWorker worker = new("scriptwidget-render", 8 * 1024 * 1024); + private QuickJs? engine; + private PackageImageSource? images; + + public WidgetRenderer(SharedScripts scripts, FontRegistry? fonts = null) + { + this.scripts = scripts; + Fonts = fonts ?? new FontRegistry(); + text = new TextEngine(Fonts); + } + + public FontRegistry Fonts { get; } + + private string? Host(string operation, string? payload) => operation switch + { + "text.measure" => Measure(Json.ParseObject(payload!)), + "text.baseline" => Number(text.Baseline(Style(Json.ParseObject(payload!)))), + "image.size" => images?.Size(payload ?? "") is { } size ? $"[{size[0]},{size[1]}]" : "null", + _ => IntlBridge.Call(operation, payload), + }; + + private static string Number(float value) => float.IsFinite(value) ? value.ToString("R", CultureInfo.InvariantCulture) : "0"; + + private static TextStyle Style(JsonObject json) => new(json.OptString("family"), (float)json.OptDouble("size", 17.0), json.OptInt("weight", 400), (float)json.OptDouble("leading", 20.0)); + + private string Measure(JsonObject json) + { + float? width = json.IsNull("width") ? null : (float)json.OptDouble("width"); + var lines = text.Lines(json.OptString("text"), Style(json), width); + return $"{{\"width\":{Number(lines.Count == 0 ? 0f : lines.Max(line => line.Width))},\"lines\":{Math.Max(1, lines.Count)}}}"; + } + + private QuickJs Engine() + { + if (engine is not null) return engine; + var created = new QuickJs(Host, memoryLimitBytes: 96L * 1024 * 1024, stackBytes: 6L * 1024 * 1024); + try + { + // Keep the native entry point for the trusted render bundle before the host file removes it. + created.Evaluate("globalThis.__swRenderNative = globalThis.__swNative;", "render-prelude.js"); + created.Evaluate(scripts.Text("host.js"), "host.js"); + created.Evaluate(scripts.Text("render.js"), "render.js"); + created.Evaluate( + "globalThis.__swRenderStart = function (request) { globalThis.__swRenderResult = null; globalThis.__swRenderError = null;" + + " __swRenderBundle.render(globalThis.__swRenderNative, request).then(function (value) { globalThis.__swRenderResult = value; }," + + " function (error) { globalThis.__swRenderError = (error && error.stack ? String(error.message || error) + '\\n' + error.stack : String(error)); }); };" + + " globalThis.__swRenderTake = function (name) { var value = globalThis[name]; globalThis[name] = null; return value; };", + "render-driver.js"); + engine = created; + return created; + } + catch + { + created.Dispose(); + throw; + } + } + + public RenderOutput Render(RenderRequest request) => worker.Run(() => + { + try { return RenderOnThread(request); } + // A lost graphics device (driver update, remote session) is replaced by Win2D; draw once more on the new one. + catch (Exception error) when (DeviceWasLost(error)) { return RenderOnThread(request); } + }); + + private static bool DeviceWasLost(Exception error) + { + try + { + var device = CanvasDevice.GetSharedDevice(); + if (!device.IsDeviceLost(error.HResult)) return false; + device.RaiseDeviceLost(); + return true; + } + catch (Exception) + { + return false; + } + } + + /// The element tree the layout records for a request, as JSON text; for tests and diagnosis. + internal string Layout(RenderRequest request) => worker.Run(() => + { + using var source = new PackageImageSource(request.Files, request.RemoteImages, CanvasDevice.GetSharedDevice()); + images = source; + try { return Json.Stringify(LayoutOnThread(request)); } + finally { images = null; } + }); + + /// Runs the shared layout; must be set. + private JsonObject LayoutOnThread(RenderRequest request) + { + var resources = new JsonObject(); + if (request.Files is not null) + foreach (var path in request.Files.Paths()) + if (ImageFile().IsMatch(path)) resources[path] = "res:" + path; + var job = new JsonObject + { + ["tree"] = request.Tree, ["width"] = (double)request.Width, ["height"] = (double)request.Height, + ["family"] = request.Family, ["dark"] = request.DarkMode, ["resources"] = resources, + ["images"] = Json.Array(request.RemoteImages.Keys), + }; + if (request.NowMilliseconds is { } now) job["now"] = now; + if (request.Colors is not null) job["colors"] = request.Colors.Copy(); + + var js = Engine(); + try + { + js.SetDeadline(LayoutTimeoutMilliseconds); + js.Call("__swRenderStart", Json.Stringify(job)); + js.RunJobs(); + var failure = js.Call("__swRenderTake", "__swRenderError"); + if (failure is not null) throw new InvalidOperationException($"Layout failed: {failure}"); + return Json.ParseObject(js.Call("__swRenderTake", "__swRenderResult") ?? throw new InvalidOperationException("Layout did not finish")); + } + catch + { + // A failed or interrupted context may hold partial state; start fresh next time. + engine?.Dispose(); + engine = null; + throw; + } + finally + { + engine?.SetDeadline(0); + } + } + + private RenderOutput RenderOnThread(RenderRequest request) + { + var layoutStart = System.Diagnostics.Stopwatch.GetTimestamp(); + var device = CanvasDevice.GetSharedDevice(); + using var source = new PackageImageSource(request.Files, request.RemoteImages, device); + images = source; + try + { + var rendered = LayoutOnThread(request); + var layoutMilliseconds = (long)System.Diagnostics.Stopwatch.GetElapsedTime(layoutStart).TotalMilliseconds; + + var paintStart = System.Diagnostics.Stopwatch.GetTimestamp(); + var scale = request.Density; + var bytesAtRequestedScale = (long)MathF.Ceiling(request.Width * scale) * (long)MathF.Ceiling(request.Height * scale) * 4L; + if (bytesAtRequestedScale > request.MaximumBitmapBytes) scale *= (float)Math.Sqrt((double)request.MaximumBitmapBytes / bytesAtRequestedScale); + var pixelWidth = Math.Clamp((int)MathF.Ceiling(request.Width * scale), 1, MaximumBitmapSide); + var pixelHeight = Math.Clamp((int)MathF.Ceiling(request.Height * scale), 1, MaximumBitmapSide); + var root = new DomNode(rendered.OptObject("root") ?? throw new InvalidOperationException("Layout returned no element tree")); + var painter = new DomPainter(text, source, request.LiveDateStyles, request.LiveGifs) { LiveMotion = request.LiveMotion, LiveLists = request.LiveLists }; + byte[] pixels; + using (var target = new CanvasRenderTarget(device, pixelWidth, pixelHeight, 96f)) + { + var session = target.CreateDrawingSession(); + session.Clear(Css.Transparent); + using (var canvas = new PaintCanvas(device, session)) painter.Paint(canvas, root, scale); + pixels = target.GetPixelBytes(); + } + return new RenderOutput + { + Pixels = pixels, PixelWidth = pixelWidth, PixelHeight = pixelHeight, Scale = scale, + HitRegions = painter.HitRegions.ToList(), DateSlots = painter.DateSlots.ToList(), GifSlots = painter.GifSlots.ToList(), + MotionSlots = painter.MotionSlots.ToList(), ListSlots = painter.ListSlots.ToList(), + Diagnostics = rendered.OptArray("diagnostics") ?? new JsonArray(), + LayoutMilliseconds = layoutMilliseconds, + PaintMilliseconds = (long)System.Diagnostics.Stopwatch.GetElapsedTime(paintStart).TotalMilliseconds, + }; + } + finally + { + images = null; + } + } + + /// Frees the layout context; the next render recreates it. + public void Release() => worker.Post(() => { engine?.Dispose(); engine = null; }); + + public void Dispose() + { + try { worker.Run(() => { engine?.Dispose(); engine = null; return true; }); } + catch (InvalidOperationException) { /* already disposed */ } + worker.Dispose(); + } + + private const long LayoutTimeoutMilliseconds = 8000L; + /// The largest texture Direct3D feature level 11 can hold. + private const int MaximumBitmapSide = 16384; + + [GeneratedRegex(@"\.(png|jpe?g|webp|gif|heic|bmp)$", RegexOptions.IgnoreCase)] + private static partial Regex ImageFile(); +} diff --git a/Windows/src/ScriptWidget.Core/Runtime/IntlBridge.cs b/Windows/src/ScriptWidget.Core/Runtime/IntlBridge.cs new file mode 100644 index 00000000..a9dda647 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Runtime/IntlBridge.cs @@ -0,0 +1,571 @@ +using System.Globalization; +using System.Runtime.InteropServices; +using System.Text; +using System.Text.Json.Nodes; + +namespace ScriptWidget.Core.Runtime; + +/// +/// ICU-backed implementation of the Intl subset installed by the QuickJS host +/// file. QuickJS has no Intl; JavaScriptCore on iPhone and the Android host +/// format with ICU as well, so the same skeletons produce matching text for +/// the same locale data. This calls the ICU that ships with Windows (icu.dll) +/// rather than .NET's own formatting, which words dates and numbers differently. +/// +public static unsafe class IntlBridge +{ + /// Overrides the system locale and time zone; used by deterministic tests. + public static volatile string? LocaleOverride; + private static volatile string? timeZoneOverride; + + public static string? TimeZoneOverride + { + get => timeZoneOverride; + set { timeZoneOverride = value; Js.QuickJs.SetTimeZone(value); } + } + + public static string DefaultLocaleTag() => LocaleOverride ?? (CultureInfo.CurrentCulture.Name is { Length: > 0 } name ? name : "en-US"); + + /// IANA name of the zone local time is shown in. + public static string DefaultTimeZone() + { + if (timeZoneOverride is { } zone) return zone; + var local = TimeZoneInfo.Local.Id; + return TimeZoneInfo.TryConvertWindowsIdToIanaId(local, out var iana) ? iana : local; + } + + public static bool Handles(string operation) => operation.StartsWith("intl.", StringComparison.Ordinal); + + private static readonly object Gate = new(); + + /// Returns null when is not an Intl operation. + public static string? Call(string operation, string? payload) + { + switch (operation) + { + case "intl.locale": return DefaultLocaleTag(); + case "intl.timeZone": return DefaultTimeZone(); + case "intl.validZone": return IsValidZone(payload ?? "") ? "1" : "0"; + } + if (!Handles(operation)) return null; + var request = Json.ParseObject(payload ?? throw new ArgumentException("Missing Intl request")); + lock (Gate) + { + return operation switch + { + "intl.date" => FormatDate(request), + "intl.dateParts" => Json.Stringify(DateParts(request)), + "intl.number" => FormatNumber(request), + "intl.plural" => Plural(request), + "intl.relative" => Relative(request), + "intl.compare" => Compare(request), + "intl.list" => List(request), + _ => null, + }; + } + } + + // MARK: - Locales and zones + + /// ICU locale ID ("en_US") as NUL-terminated bytes for a BCP 47 tag ("en-US"). + private static byte[] Locale(string tag) + { + var input = Encoding.ASCII.GetBytes(tag.Replace('_', '-') + "\0"); + var output = new byte[160]; + var status = 0; + int length; + fixed (byte* source = input) + fixed (byte* target = output) + length = Icu.uloc_forLanguageTag(source, target, output.Length - 1, null, &status); + if (status > 0 || length <= 0) return "en_US\0"u8.ToArray(); + return output; + } + + private static string RequiredLocale(JsonObject request) => request.OptStringOrNull("locale") ?? throw new ArgumentException("Missing locale"); + + private static bool IsValidZone(string id) + { + if (id.Equals("UTC", StringComparison.OrdinalIgnoreCase) || id.Equals("GMT", StringComparison.OrdinalIgnoreCase)) return true; + if (id.Length == 0 || id.Length > 100) return false; + var buffer = stackalloc char[128]; + var status = 0; + byte system = 0; + fixed (char* chars = id) Icu.ucal_getCanonicalTimeZoneID(chars, id.Length, buffer, 128, &system, &status); + return status <= 0 && system != 0; + } + + private static void Check(int status, string what) + { + if (status > 0) throw new ArgumentException($"{what} failed (ICU error {status})"); + } + + private delegate int Fill(char* buffer, int capacity, int* status); + + /// Runs an ICU call that writes UTF-16 into a buffer, growing the buffer once when it is too small. + private static string Text(Fill fill, string what) + { + var capacity = 256; + while (true) + { + var buffer = new char[capacity]; + var status = 0; + int length; + fixed (char* chars = buffer) length = fill(chars, capacity, &status); + if (status == Icu.BufferOverflow && capacity < 1 << 20) { capacity = Math.Max(length + 1, capacity * 2); continue; } + Check(status, what); + return new string(buffer, 0, Math.Min(length, capacity)); + } + } + + // MARK: - Dates + + /// Building an ICU formatter is far more expensive than using one; planners format thousands of dates. + private static readonly Dictionary FormatCache = new(); + private static readonly LinkedList FormatOrder = new(); + + private static nint DateFormat(JsonObject request) + { + var options = request.OptObject("options") ?? new JsonObject(); + var key = RequiredLocale(request) + "|" + DefaultTimeZone() + "|" + Json.Stringify(options); + if (FormatCache.TryGetValue(key, out var cached)) + { + FormatOrder.Remove(key); + FormatOrder.AddLast(key); + return cached; + } + var created = CreateDateFormat(request, options); + FormatCache[key] = created; + FormatOrder.AddLast(key); + if (FormatCache.Count > 32) + { + var eldest = FormatOrder.First!.Value; + FormatOrder.RemoveFirst(); + Icu.udat_close(FormatCache[eldest]); + FormatCache.Remove(eldest); + } + return created; + } + + private static nint CreateDateFormat(JsonObject request, JsonObject options) + { + var locale = Locale(RequiredLocale(request)); + var zone = options.OptString("timeZone") is { Length: > 0 } requested ? requested : DefaultTimeZone(); + var dateStyle = Style(options.OptString("dateStyle")); + var timeStyle = Style(options.OptString("timeStyle")); + var status = 0; + nint format; + fixed (byte* localeID = locale) + fixed (char* zoneID = zone) + { + if (dateStyle is not null || timeStyle is not null) + { + format = Icu.udat_open(timeStyle ?? Icu.StyleNone, dateStyle ?? Icu.StyleNone, localeID, zoneID, zone.Length, null, 0, &status); + } + else + { + var skeleton = Skeleton(options); + var generator = Icu.udatpg_open(localeID, &status); + Check(status, "Date pattern generator"); + string pattern; + try + { + pattern = Text((buffer, capacity, error) => { fixed (char* chars = skeleton) return Icu.udatpg_getBestPattern(generator, chars, skeleton.Length, buffer, capacity, error); }, "Date pattern"); + } + finally { Icu.udatpg_close(generator); } + pattern = ApplyHourWidth(pattern, options.OptString("hour")); + fixed (char* chars = pattern) format = Icu.udat_open(Icu.StylePattern, Icu.StylePattern, localeID, zoneID, zone.Length, chars, pattern.Length, &status); + } + } + Check(status, "Date format"); + return format; + } + + /// + /// ECMA-402 keeps the requested hour width even when the locale pattern + /// for the skeleton uses another one ("2-digit" gives "08:00 PM" in en-US). + /// + private static string ApplyHourWidth(string pattern, string hour) + { + int width; + switch (hour) { case "2-digit": width = 2; break; case "numeric": width = 1; break; default: return pattern; } + var result = new StringBuilder(); + var index = 0; + var quoted = false; + while (index < pattern.Length) + { + var character = pattern[index]; + if (character == '\'') { quoted = !quoted; result.Append(character); index += 1; continue; } + if (!quoted && "hHkK".Contains(character)) + { + var end = index; + while (end < pattern.Length && pattern[end] == character) end += 1; + result.Append(character, width); + index = end; + continue; + } + result.Append(character); + index += 1; + } + return result.ToString(); + } + + private static int? Style(string name) => name switch { "full" => 0, "long" => 1, "medium" => 2, "short" => 3, _ => null }; + + /// Maps ECMA-402 date options to an ICU skeleton. + private static string Skeleton(JsonObject options) + { + var text = new StringBuilder(); + void Width(string name, char letter, int narrow = 5) + { + switch (options.OptString(name)) + { + case "narrow": text.Append(letter, narrow); break; + case "short": text.Append(letter, 3); break; + case "long": text.Append(letter, 4); break; + case "numeric": text.Append(letter); break; + case "2-digit": text.Append(letter, 2); break; + } + } + switch (options.OptString("era")) { case "narrow": text.Append("GGGGG"); break; case "short": text.Append('G'); break; case "long": text.Append("GGGG"); break; } + Width("year", 'y'); + Width("month", 'M'); + switch (options.OptString("weekday")) { case "narrow": text.Append("EEEEE"); break; case "short": text.Append("EEE"); break; case "long": text.Append("EEEE"); break; } + Width("day", 'd'); + var hour = options.OptString("hour"); + if (hour.Length > 0) + { + var letter = options.Has("hourCycle") ? options.OptString("hourCycle") switch { "h11" => 'K', "h12" => 'h', "h23" => 'H', "h24" => 'k', _ => 'j' } + : options.Has("hour12") ? (options.OptBoolean("hour12") ? 'h' : 'H') + : 'j'; + text.Append(letter, hour == "2-digit" ? 2 : 1); + } + switch (options.OptString("dayPeriod")) { case "narrow": text.Append("BBBBB"); break; case "short": text.Append('B'); break; case "long": text.Append("BBBB"); break; } + if (options.OptString("minute").Length > 0) text.Append(options.OptString("minute") == "2-digit" ? "mm" : "m"); + if (options.OptString("second").Length > 0) text.Append(options.OptString("second") == "2-digit" ? "ss" : "s"); + var fraction = options.OptInt("fractionalSecondDigits"); + if (fraction is >= 1 and <= 3) text.Append('S', fraction); + switch (options.OptString("timeZoneName")) + { + case "short": text.Append('z'); break; + case "long": text.Append("zzzz"); break; + case "shortOffset": text.Append('O'); break; + case "longOffset": text.Append("OOOO"); break; + case "shortGeneric": text.Append('v'); break; + case "longGeneric": text.Append("vvvv"); break; + } + return text.ToString(); + } + + private static double Time(JsonObject request) + { + var time = request.OptDouble("time"); + if (double.IsNaN(time)) throw new ArgumentException("Invalid time value"); + return Math.Truncate(time); + } + + private static string FormatDate(JsonObject request) + { + var format = DateFormat(request); + var time = Time(request); + return NormalizeSpaces(Text((buffer, capacity, status) => Icu.udat_format(format, time, buffer, capacity, null, status), "Date format")); + } + + /// ICU 72+ separates the day period with U+202F; JavaScriptCore emits a plain space for compatibility. + private static string NormalizeSpaces(string text) => text.Replace(' ', ' '); + + private static string PartName(int field) => field switch + { + 0 => "era", + 1 or 18 or 20 or 30 or 34 => "year", + 2 or 26 => "month", + 3 => "day", + 9 or 19 or 25 => "weekday", + 4 or 5 or 15 or 16 => "hour", + 6 => "minute", + 7 => "second", + 8 => "fractionalSecond", + 14 or 35 or 36 => "dayPeriod", + 17 or 23 or 24 or 29 or 31 or 32 or 33 => "timeZoneName", + _ => "literal", + }; + + private static JsonArray DateParts(JsonObject request) + { + var format = DateFormat(request); + var time = Time(request); + var status = 0; + var iterator = Icu.ufieldpositer_open(&status); + Check(status, "Date parts"); + var fields = new List<(int Start, int End, int Field)>(); + string text; + try + { + text = Text((buffer, capacity, error) => Icu.udat_formatForFields(format, time, buffer, capacity, iterator, error), "Date parts"); + // The iterator holds the fields of the last successful call. + int start, end, field; + while ((field = Icu.ufieldpositer_next(iterator, &start, &end)) >= 0) fields.Add((start, end, field)); + } + finally { Icu.ufieldpositer_close(iterator); } + fields.Sort((left, right) => left.Start != right.Start ? left.Start.CompareTo(right.Start) : right.End.CompareTo(left.End)); + var parts = new JsonArray(); + void Add(string type, int start, int end) + { + if (end > start) parts.Append(new JsonObject { ["type"] = type, ["value"] = NormalizeSpaces(text[start..end]) }); + } + var index = 0; + foreach (var (start, end, field) in fields) + { + if (start < index || end > text.Length) continue; + Add("literal", index, start); + Add(PartName(field), start, end); + index = end; + } + Add("literal", index, text.Length); + return parts; + } + + // MARK: - Numbers + + private static string FormatNumber(JsonObject request) + { + var locale = Locale(RequiredLocale(request)); + var options = request.OptObject("options") ?? new JsonObject(); + var raw = request.OptString("value"); + var value = raw switch + { + "Infinity" => double.PositiveInfinity, + "-Infinity" => double.NegativeInfinity, + _ => double.TryParse(raw, NumberStyles.Float, CultureInfo.InvariantCulture, out var parsed) ? parsed : double.NaN, + }; + if (double.IsNaN(value)) return "NaN"; + if (double.IsInfinity(value)) return value > 0 ? "∞" : "-∞"; + + var style = options.OptString("style") is { Length: > 0 } requested ? requested : "decimal"; + var skeleton = new StringBuilder(); + var defaultMinimum = 0; + var defaultMaximum = 3; + switch (style) + { + case "percent": + skeleton.Append("percent scale/100 "); + defaultMaximum = 0; + break; + case "currency": + { + var currency = (options.OptString("currency") is { Length: > 0 } code ? code : "USD").ToUpperInvariant(); + if (currency.Length != 3 || !currency.All(char.IsAsciiLetter)) throw new ArgumentException($"Invalid currency code {currency}"); + skeleton.Append("currency/").Append(currency).Append(' '); + var status = 0; + int digits; + fixed (char* chars = currency) digits = Icu.ucurr_getDefaultFractionDigits(chars, &status); + if (status > 0) digits = 2; + defaultMinimum = digits; + defaultMaximum = digits; + switch (options.OptString("currencyDisplay")) + { + case "code": skeleton.Append("unit-width-iso-code "); break; + case "name": skeleton.Append("unit-width-full-name "); break; + case "narrowSymbol": skeleton.Append("unit-width-narrow "); break; + } + break; + } + } + switch (options.OptString("notation")) + { + case "compact": skeleton.Append(options.OptString("compactDisplay") == "long" ? "compact-long " : "compact-short "); break; + case "scientific": skeleton.Append("scientific "); break; + case "engineering": skeleton.Append("engineering "); break; + } + var compact = options.OptString("notation") == "compact"; + if (options.Has("minimumSignificantDigits") || options.Has("maximumSignificantDigits")) + { + var minimum = Math.Clamp(options.OptInt("minimumSignificantDigits", 1), 1, 21); + var maximum = Math.Clamp(options.OptInt("maximumSignificantDigits", 21), minimum, 21); + skeleton.Append('@', minimum).Append('#', maximum - minimum).Append(' '); + } + else if (options.Has("minimumFractionDigits") || options.Has("maximumFractionDigits") || !compact) + { + var minimum = Math.Clamp(options.OptInt("minimumFractionDigits", Math.Min(defaultMinimum, options.OptInt("maximumFractionDigits", defaultMinimum))), 0, 100); + var maximum = Math.Clamp(options.OptInt("maximumFractionDigits", Math.Max(defaultMaximum, minimum)), minimum, 100); + if (maximum == 0) skeleton.Append("precision-integer "); + else skeleton.Append('.').Append('0', minimum).Append('#', maximum - minimum).Append(' '); + } + if (options.Has("minimumIntegerDigits")) skeleton.Append("integer-width/*").Append('0', Math.Clamp(options.OptInt("minimumIntegerDigits", 1), 1, 21)).Append(' '); + if (options.Has("useGrouping") && !options.OptBoolean("useGrouping", true)) skeleton.Append("group-off "); + switch (options.OptString("signDisplay")) + { + case "always": skeleton.Append("sign-always "); break; + case "never": skeleton.Append("sign-never "); break; + case "exceptZero": skeleton.Append("sign-except-zero "); break; + } + + var skeletonText = skeleton.ToString().TrimEnd(); + var error = 0; + nint formatter; + fixed (char* chars = skeletonText) + fixed (byte* localeID = locale) + formatter = Icu.unumf_openForSkeletonAndLocale(chars, skeletonText.Length, localeID, &error); + Check(error, "Number format"); + var result = Icu.unumf_openResult(&error); + try + { + Check(error, "Number format"); + // The exact decimal expansion of the double, like BigDecimal(value) on Android. + var digits = Encoding.ASCII.GetBytes(value.ToString("E60", CultureInfo.InvariantCulture)); + fixed (byte* decimalText = digits) Icu.unumf_formatDecimal(formatter, decimalText, digits.Length, result, &error); + Check(error, "Number format"); + return Text((buffer, capacity, status) => Icu.unumf_resultToString(result, buffer, capacity, status), "Number format"); + } + finally + { + if (result != 0) Icu.unumf_closeResult(result); + Icu.unumf_close(formatter); + } + } + + private static string Plural(JsonObject request) + { + var locale = Locale(RequiredLocale(request)); + var status = 0; + nint rules; + fixed (byte* localeID = locale) rules = Icu.uplrules_openForType(localeID, request.OptString("type") == "ordinal" ? 1 : 0, &status); + Check(status, "Plural rules"); + try + { + var value = request.OptDouble("value"); + return Text((buffer, capacity, error) => Icu.uplrules_select(rules, value, buffer, capacity, error), "Plural rules"); + } + finally { Icu.uplrules_close(rules); } + } + + private static string Relative(JsonObject request) + { + var options = request.OptObject("options") ?? new JsonObject(); + var style = options.OptString("style") switch { "short" => 1, "narrow" => 2, _ => 0 }; + var name = request.OptString("unit"); + var unit = (name.EndsWith('s') ? name[..^1] : name) switch + { + "year" => 0, "quarter" => 1, "month" => 2, "week" => 3, "day" => 4, "hour" => 5, "minute" => 6, "second" => 7, + _ => throw new ArgumentException("Invalid unit argument for format()"), + }; + var locale = Locale(RequiredLocale(request)); + var status = 0; + nint formatter; + fixed (byte* localeID = locale) formatter = Icu.ureldatefmt_open(localeID, 0, style, Icu.CapitalizationNone, &status); + Check(status, "Relative time format"); + try + { + var value = request.OptDouble("value"); + return options.OptString("numeric") == "auto" + ? Text((buffer, capacity, error) => Icu.ureldatefmt_format(formatter, value, unit, buffer, capacity, error), "Relative time format") + : Text((buffer, capacity, error) => Icu.ureldatefmt_formatNumeric(formatter, value, unit, buffer, capacity, error), "Relative time format"); + } + finally { Icu.ureldatefmt_close(formatter); } + } + + private static string Compare(JsonObject request) + { + var locale = Locale(RequiredLocale(request)); + var left = request.OptString("left"); + var right = request.OptString("right"); + var status = 0; + nint collator; + fixed (byte* localeID = locale) collator = Icu.ucol_open(localeID, &status); + Check(status, "Collator"); + try + { + int result; + fixed (char* first = left) + fixed (char* second = right) + result = Icu.ucol_strcoll(collator, first, left.Length, second, right.Length); + return Math.Clamp(result, -1, 1).ToString(CultureInfo.InvariantCulture); + } + finally { Icu.ucol_close(collator); } + } + + private static string List(JsonObject request) + { + var items = (request.OptArray("items") ?? new JsonArray()).Select(item => item?.AsText() ?? "").ToArray(); + var options = request.OptObject("options") ?? new JsonObject(); + var type = options.OptString("type") switch { "disjunction" => 1, "unit" => 2, _ => 0 }; + var width = options.OptString("style") switch { "short" => 1, "narrow" => 2, _ => 0 }; + if (items.Length == 0) return ""; + var locale = Locale(RequiredLocale(request)); + var status = 0; + nint formatter; + fixed (byte* localeID = locale) formatter = Icu.ulistfmt_openForType(localeID, type, width, &status); + Check(status, "List format"); + var handles = new GCHandle[items.Length]; + try + { + var pointers = new nint[items.Length]; + var lengths = new int[items.Length]; + for (var index = 0; index < items.Length; index++) + { + handles[index] = GCHandle.Alloc(items[index], GCHandleType.Pinned); + pointers[index] = handles[index].AddrOfPinnedObject(); + lengths[index] = items[index].Length; + } + return Text((buffer, capacity, error) => + { + fixed (nint* strings = pointers) + fixed (int* sizes = lengths) + return Icu.ulistfmt_format(formatter, (char**)strings, sizes, items.Length, buffer, capacity, error); + }, "List format"); + } + finally + { + foreach (var handle in handles) if (handle.IsAllocated) handle.Free(); + Icu.ulistfmt_close(formatter); + } + } + + /// The ICU C API that ships with Windows 10 1903 and later. + private static class Icu + { + private const string Library = "icu.dll"; + public const int BufferOverflow = 15; + public const int StyleNone = -1; + public const int StylePattern = -2; + public const int CapitalizationNone = 256; + + [DllImport(Library)] public static extern int uloc_forLanguageTag(byte* tag, byte* localeID, int capacity, int* parsedLength, int* status); + [DllImport(Library)] public static extern int ucal_getCanonicalTimeZoneID(char* id, int length, char* result, int capacity, byte* isSystemID, int* status); + + [DllImport(Library)] public static extern nint udat_open(int timeStyle, int dateStyle, byte* locale, char* zone, int zoneLength, char* pattern, int patternLength, int* status); + [DllImport(Library)] public static extern void udat_close(nint format); + [DllImport(Library)] public static extern int udat_format(nint format, double date, char* result, int capacity, void* position, int* status); + [DllImport(Library)] public static extern int udat_formatForFields(nint format, double date, char* result, int capacity, nint iterator, int* status); + [DllImport(Library)] public static extern nint ufieldpositer_open(int* status); + [DllImport(Library)] public static extern void ufieldpositer_close(nint iterator); + [DllImport(Library)] public static extern int ufieldpositer_next(nint iterator, int* begin, int* end); + [DllImport(Library)] public static extern nint udatpg_open(byte* locale, int* status); + [DllImport(Library)] public static extern void udatpg_close(nint generator); + [DllImport(Library)] public static extern int udatpg_getBestPattern(nint generator, char* skeleton, int length, char* pattern, int capacity, int* status); + + [DllImport(Library)] public static extern nint unumf_openForSkeletonAndLocale(char* skeleton, int length, byte* locale, int* status); + [DllImport(Library)] public static extern void unumf_close(nint formatter); + [DllImport(Library)] public static extern nint unumf_openResult(int* status); + [DllImport(Library)] public static extern void unumf_closeResult(nint result); + [DllImport(Library)] public static extern void unumf_formatDecimal(nint formatter, byte* value, int length, nint result, int* status); + [DllImport(Library)] public static extern int unumf_resultToString(nint result, char* buffer, int capacity, int* status); + [DllImport(Library)] public static extern int ucurr_getDefaultFractionDigits(char* currency, int* status); + + [DllImport(Library)] public static extern nint uplrules_openForType(byte* locale, int type, int* status); + [DllImport(Library)] public static extern void uplrules_close(nint rules); + [DllImport(Library)] public static extern int uplrules_select(nint rules, double number, char* keyword, int capacity, int* status); + + [DllImport(Library)] public static extern nint ureldatefmt_open(byte* locale, nint numberFormat, int width, int capitalization, int* status); + [DllImport(Library)] public static extern void ureldatefmt_close(nint formatter); + [DllImport(Library)] public static extern int ureldatefmt_format(nint formatter, double offset, int unit, char* result, int capacity, int* status); + [DllImport(Library)] public static extern int ureldatefmt_formatNumeric(nint formatter, double offset, int unit, char* result, int capacity, int* status); + + [DllImport(Library)] public static extern nint ucol_open(byte* locale, int* status); + [DllImport(Library)] public static extern void ucol_close(nint collator); + [DllImport(Library)] public static extern int ucol_strcoll(nint collator, char* source, int sourceLength, char* target, int targetLength); + + [DllImport(Library)] public static extern nint ulistfmt_openForType(byte* locale, int type, int width, int* status); + [DllImport(Library)] public static extern void ulistfmt_close(nint formatter); + [DllImport(Library)] public static extern int ulistfmt_format(nint formatter, char** strings, int* lengths, int count, char* result, int capacity, int* status); + } +} diff --git a/Windows/src/ScriptWidget.Core/Runtime/PackageCompiler.cs b/Windows/src/ScriptWidget.Core/Runtime/PackageCompiler.cs new file mode 100644 index 00000000..99b51fa1 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Runtime/PackageCompiler.cs @@ -0,0 +1,217 @@ +using System.Text.Json.Nodes; +using System.Text.RegularExpressions; +using ScriptWidget.Core.Js; + +namespace ScriptWidget.Core.Runtime; + +public sealed class RuntimeError(string kind, string message) : Exception(message) +{ + public string Kind { get; } = kind; +} + +/// Output of preparing a package: transpiled entry, transpiled imports and the validated manifest. +public sealed class PreparedPackage +{ + public required string Code { get; init; } + public IReadOnlyDictionary Imports { get; init; } = new Dictionary(); + public JsonObject? Manifest { get; init; } + public bool HasManifest { get; init; } + public string? ActionID { get; init; } + public IReadOnlyDictionary TimelineEnvironment { get; init; } = new Dictionary(); + + public JsonObject ToJson() => new() + { + ["code"] = Code, + ["imports"] = Json.Object(Imports), + ["manifest"] = Manifest?.DeepClone(), + ["hasManifest"] = HasManifest, + ["actionID"] = ActionID, + ["timelineEnvironment"] = Json.Object(TimelineEnvironment), + }; + + public static PreparedPackage FromJson(JsonObject json) + { + var imports = new Dictionary(); + if (json.OptObject("imports") is { } map) foreach (var (key, value) in map) imports[key] = value?.AsText() ?? ""; + var environment = new Dictionary(); + if (json.OptObject("timelineEnvironment") is { } values) foreach (var (key, value) in values) environment[key] = value?.AsText() ?? ""; + return new PreparedPackage + { + Code = json.OptStringOrNull("code") ?? throw new RuntimeError("internalError", "Compiler returned no code"), + Imports = imports, + Manifest = json.OptObject("manifest")?.DeepClone() as JsonObject, + HasManifest = json.OptBoolean("hasManifest"), + ActionID = json.IsNull("actionID") ? null : json.OptString("actionID"), + TimelineEnvironment = environment, + }; + } +} + +/// +/// Validates manifests and transpiles JSX with the same Babel bundle and +/// prepare code as the web preview engine. The Babel context is expensive, so +/// it lives on one dedicated thread and is released after a period of disuse. +/// Package code never executes in this context. +/// +public sealed partial class PackageCompiler : IDisposable +{ + private const int IdleSeconds = 45; + private const long CompileTimeoutMilliseconds = 30_000; + + private readonly SharedScripts scripts; + private readonly JsWorker worker = new("scriptwidget-compiler", 16 * 1024 * 1024); + private readonly Timer release; + private QuickJs? engine; + + public PackageCompiler(SharedScripts scripts) + { + this.scripts = scripts; + release = new Timer(_ => worker.Post(() => { engine?.Dispose(); engine = null; }), null, Timeout.Infinite, Timeout.Infinite); + } + + private T OnCompilerThread(Func block) => worker.Run(() => + { + release.Change(Timeout.Infinite, Timeout.Infinite); + var current = engine ??= CreateEngine(); + try { return block(current); } + finally + { + current.SetDeadline(0); + release.Change(TimeSpan.FromSeconds(IdleSeconds), Timeout.InfiniteTimeSpan); + } + }); + + private QuickJs CreateEngine() + { + var created = new QuickJs((operation, payload) => operation == "url.parse" ? ParseURL(payload ?? "") : IntlBridge.Call(operation, payload), memoryLimitBytes: 384L * 1024 * 1024, stackBytes: 12L * 1024 * 1024); + try + { + // The prelude captures the native entry point before the host file removes it. + created.Evaluate(CompilerPrelude, "compiler-prelude.js"); + created.Evaluate(scripts.Text("host.js"), "host.js"); + foreach (var name in new[] { "core.js", "prepare.js", "compiler.js" }) created.Evaluate(scripts.Text(name), name); + } + catch + { + created.Dispose(); + throw; + } + return created; + } + + /// Validates and transpiles a package. Throws with the engine's error kind. + public PreparedPackage Prepare(IPackageFiles files, string? entry = null, string prelude = "", string? actionID = null, string? callFunction = null) + { + var text = new JsonObject(); + var binaries = new JsonArray(); + foreach (var path in files.Paths()) + { + var content = IsTextFile(path) ? files.ReadText(path) : null; + if (content is not null) text[path] = content; else binaries.Append(path); + } + var job = new JsonObject { ["files"] = text, ["binaries"] = binaries, ["prelude"] = prelude }; + if (entry is not null) job["entry"] = entry; + if (actionID is not null) job["actionID"] = actionID; + if (callFunction is not null) job["callFunction"] = callFunction; + var jobText = Json.Stringify(job); + string? output; + try + { + output = OnCompilerThread(current => { current.SetDeadline(CompileTimeoutMilliseconds); return current.Call("__swPrepare", jobText); }); + } + catch (JsException error) + { + throw new RuntimeError(error.Interrupted ? "resourceLimit" : "internalError", string.IsNullOrEmpty(error.Message) ? "Compiler failed" : error.Message); + } + var prepared = Json.ParseObject(output ?? throw new RuntimeError("internalError", "Compiler returned nothing")); + if (!prepared.OptBoolean("ok")) + { + var error = prepared.OptObject("error"); + throw new RuntimeError(error?.OptStringOrNull("kind") ?? "internalError", error?.OptStringOrNull("message") ?? "Package could not be prepared"); + } + return PreparedPackage.FromJson(prepared); + } + + /// Returns {ok, manifest?, error?, issues[]} from the shared manifest validator. + public JsonObject ValidateManifest(string manifestText, IEnumerable paths) + { + var pathsText = Json.Stringify(Json.Array(paths)); + return Json.ParseObject(OnCompilerThread(current => { current.SetDeadline(CompileTimeoutMilliseconds); return current.Call("__swValidateManifest", manifestText, pathsText); })!); + } + + /// Timeline plan for a timeline manifest object: {entries:[{date, environments}], reloadDate}. + public JsonObject TimelinePlan(JsonObject schedule, long nowMilliseconds, string systemTimeZone) + { + var scheduleText = Json.Stringify(schedule); + return Json.ParseObject(OnCompilerThread(current => { current.SetDeadline(CompileTimeoutMilliseconds); return current.Call("__swTimeline", scheduleText, nowMilliseconds.ToString(System.Globalization.CultureInfo.InvariantCulture), systemTimeZone); })!); + } + + /// WHATWG-style components for the absolute URLs the manifest validator inspects, or null. + internal static string? ParseURL(string text) + { + if (!Uri.TryCreate(text.Trim(), UriKind.Absolute, out var uri)) return null; + var scheme = uri.Scheme.ToLowerInvariant(); + if (string.IsNullOrEmpty(uri.Host) || scheme is "mailto" or "urn" or "data" or "javascript" or "file") return null; + var userInfo = uri.UserInfo; + var separator = userInfo.IndexOf(':'); + return Json.Stringify(new JsonObject + { + ["href"] = uri.OriginalString.Trim(), + ["protocol"] = scheme + ":", + ["hostname"] = uri.Host.ToLowerInvariant().TrimStart('[').TrimEnd(']'), + ["port"] = uri.IsDefaultPort ? "" : uri.Port.ToString(System.Globalization.CultureInfo.InvariantCulture), + ["pathname"] = string.IsNullOrEmpty(uri.AbsolutePath) ? "/" : uri.AbsolutePath, + ["search"] = uri.Query, + ["hash"] = uri.Fragment, + ["username"] = separator < 0 ? userInfo : userInfo[..separator], + ["password"] = separator < 0 ? "" : userInfo[(separator + 1)..], + }); + } + + public void Dispose() + { + release.Dispose(); + worker.Post(() => { engine?.Dispose(); engine = null; }); + worker.Dispose(); + } + + /// + /// Globals the trusted compiler code expects. JavaScriptCore contexts have + /// a console object, and the shared manifest validator measures UTF-8 + /// lengths with TextEncoder. Package code never runs here. + /// + private const string CompilerPrelude = """ + globalThis.console = { log() {}, info() {}, warn() {}, error() {}, debug() {}, trace() {} }; + { + const native = globalThis.__swNative; + globalThis.URL = class URL { + constructor(text) { + const parsed = native("url.parse", String(text)); + if (parsed === null) throw new TypeError("Invalid URL"); + Object.assign(this, JSON.parse(parsed)); + } + toString() { return this.href; } + }; + } + globalThis.TextEncoder = class TextEncoder { + encode(text) { + const bytes = []; + for (const character of String(text ?? "")) { + const code = character.codePointAt(0); + if (code < 0x80) bytes.push(code); + else if (code < 0x800) bytes.push(0xc0 | (code >> 6), 0x80 | (code & 63)); + else if (code < 0x10000) { + const safe = code >= 0xd800 && code <= 0xdfff ? 0xfffd : code; + bytes.push(0xe0 | (safe >> 12), 0x80 | ((safe >> 6) & 63), 0x80 | (safe & 63)); + } else bytes.push(0xf0 | (code >> 18), 0x80 | ((code >> 12) & 63), 0x80 | ((code >> 6) & 63), 0x80 | (code & 63)); + } + return new Uint8Array(bytes); + } + }; + """; + + [GeneratedRegex(@"\.(jsx?|mjs|json|txt|md|csv|svg|xml|html?|css|ya?ml)$", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)] + private static partial Regex TextFile(); + + public static bool IsTextFile(string path) => TextFile().IsMatch(path); +} diff --git a/Windows/src/ScriptWidget.Core/Runtime/PackagePaths.cs b/Windows/src/ScriptWidget.Core/Runtime/PackagePaths.cs new file mode 100644 index 00000000..9555e2ca --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Runtime/PackagePaths.cs @@ -0,0 +1,48 @@ +using System.Text; + +namespace ScriptWidget.Core.Runtime; + +/// Package-relative path handling, mirroring normalizePackagePath in the shared engine. +public static class PackagePaths +{ + /// Returns the normalized relative path, or null when it could leave the package. + public static string? Normalize(string? relativePath) + { + if (string.IsNullOrEmpty(relativePath) || relativePath.StartsWith('/')) return null; + if (relativePath.Contains('\\') || relativePath.Contains('\0')) return null; + var parts = new List(); + foreach (var part in relativePath.Split('/')) + { + switch (part) + { + case "" or ".": continue; + case "..": + if (parts.Count == 0) return null; + parts.RemoveAt(parts.Count - 1); + break; + default: parts.Add(part); break; + } + } + return parts.Count == 0 ? null : string.Join('/', parts); + } +} + +/// Read-only view of one installed package. Paths are already normalized. +public interface IPackageFiles +{ + IReadOnlyList Paths(); + bool Exists(string path); + /// UTF-8 text of a script or data file, or null for missing and binary files. + string? ReadText(string path); + byte[]? ReadBytes(string path); +} + +public sealed class MemoryPackageFiles(IReadOnlyDictionary text, IReadOnlyDictionary? binary = null) : IPackageFiles +{ + private readonly IReadOnlyDictionary binary = binary ?? new Dictionary(); + + public IReadOnlyList Paths() => text.Keys.Concat(binary.Keys).Order(StringComparer.Ordinal).ToList(); + public bool Exists(string path) => text.ContainsKey(path) || binary.ContainsKey(path); + public string? ReadText(string path) => text.GetValueOrDefault(path); + public byte[]? ReadBytes(string path) => binary.TryGetValue(path, out var bytes) ? bytes : text.TryGetValue(path, out var value) ? Encoding.UTF8.GetBytes(value) : null; +} diff --git a/Windows/src/ScriptWidget.Core/Runtime/ScriptExecutor.cs b/Windows/src/ScriptWidget.Core/Runtime/ScriptExecutor.cs new file mode 100644 index 00000000..87c9180f --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Runtime/ScriptExecutor.cs @@ -0,0 +1,260 @@ +using System.Collections.Concurrent; +using System.Diagnostics; +using System.Security.Cryptography; +using System.Text.Json.Nodes; +using ScriptWidget.Core.Js; + +namespace ScriptWidget.Core.Runtime; + +/// Everything one execution may touch. +public sealed class ExecutionRequest +{ + public required PreparedPackage Prepared { get; init; } + public required IPackageFiles Files { get; init; } + public IReadOnlyDictionary Environments { get; init; } = new Dictionary { ["widget-size"] = "medium", ["widget-param"] = "" }; + public bool DarkMode { get; init; } + public JsonObject? Device { get; init; } + public JsonObject? System { get; init; } + /// Preview simulations for the shared kernel; production executions leave this empty. + public JsonObject Fixtures { get; init; } = new(); + public IReadOnlyList Modules { get; init; } = []; + public long TimeoutMilliseconds { get; init; } = 5000; + /// Package identity for storage namespaces and diagnostics. + public string PackageID { get; init; } = ""; + + /// Budgets and cancellation shared by every native call of this execution. + public ExecutionSession Session { get; } = new(); +} + +public sealed class ExecutionSession +{ + private int networkRequests; + private int dataQueries; + + /// Counts one more network request and returns the new total. + public int AddNetworkRequest() => Interlocked.Increment(ref networkRequests); + public int NetworkRequests => Volatile.Read(ref networkRequests); + public int AddDataQuery() => Interlocked.Increment(ref dataQueries); + public int DataQueries => Volatile.Read(ref dataQueries); + + /// True once the script asked whether dark mode is on: its output may differ between themes. + public volatile bool ReadAppearance; + /// Set when the execution ended; in-flight native work should stop. + public volatile bool Finished; + /// Cancelled when the execution ends. + public CancellationTokenSource Cancellation { get; } = new(); +} + +public sealed record ExecutionError(string Kind, string Message); + +public sealed record LogEntry(string Level, string Message); + +public sealed class ExecutionResult +{ + /// Canonical tree JSON (see encodeElement in the shared kernel), or null on error. + public string? Tree { get; init; } + public ExecutionError? Error { get; init; } + public IReadOnlyList Logs { get; init; } = []; + public JsonArray Diagnostics { get; init; } = new(); + public long ElapsedMilliseconds { get; init; } +} + +/// Completes an asynchronous native operation. Call exactly once, from any thread. +public delegate void NativeCompletion(bool ok, string? json); + +/// +/// A group of native operations exposed to package code through the host +/// bridge. Policy (manifest permissions, budgets) is enforced here and in the +/// JavaScript installers, never by trusting package code. +/// +public interface INativeModule +{ + bool Handles(string operation); + /// Script globals this module implements, such as $weather; others report "not supported on Windows". + IReadOnlySet Apis => EmptyApis; + /// Synchronous operation. Returns JSON text or null. + string? Call(ExecutionRequest request, string operation, string? payload) => throw new ArgumentException($"Unsupported operation {operation}"); + /// Asynchronous operation. Call exactly once from any thread. + void Start(ExecutionRequest request, string operation, JsonObject? payload, NativeCompletion done) => done(false, Json.Quote($"Unsupported operation {operation}")); + + private static readonly IReadOnlySet EmptyApis = new HashSet(); +} + +/// Runs one prepared package in a fresh QuickJS context and returns its element tree. +public sealed class ScriptExecutor(SharedScripts scripts) +{ + private const long InterruptGraceMilliseconds = 400; + /// Name the "not supported" messages use for this host. + public const string PlatformName = "Windows"; + + private sealed record Completion(int ID, bool OK, string? Json); + + public ExecutionResult Execute(ExecutionRequest request) + { + var started = Stopwatch.GetTimestamp(); + var result = JsThread.Run(() => Run(request)); + return new ExecutionResult { Tree = result.Tree, Error = result.Error, Logs = result.Logs, Diagnostics = result.Diagnostics, ElapsedMilliseconds = (long)Stopwatch.GetElapsedTime(started).TotalMilliseconds }; + } + + private ExecutionResult Run(ExecutionRequest request) + { + using var completions = new BlockingCollection(); + var gate = new object(); + var nativeLogs = new List(); + string? finished = null; + var cancelled = false; + + string? Host(string operation, string? payload) + { + switch (operation) + { + case "finish": finished = payload; return null; + case "package.import": return CompiledImport(request, payload); + case "package.read": return PackagePaths.Normalize(payload) is { } readPath ? request.Files.ReadText(readPath) : null; + case "package.exists": return PackagePaths.Normalize(payload) is { } existsPath && request.Files.Exists(existsPath) ? "1" : "0"; + case "support.script": return scripts.SupportScript(payload ?? ""); + case "xml.parse": return XmlBridge.Parse(payload ?? ""); + case "session.appearance": request.Session.ReadAppearance = true; return null; + case "log.log" or "log.error": + if (nativeLogs.Count < 500) nativeLogs.Add(new LogEntry(operation["log.".Length..], payload ?? "")); + return null; + case "async.start": + { + var job = Json.ParseObject(payload!); + var id = job.OptInt("id"); + var name = job.OptString("operation"); + var module = request.Modules.FirstOrDefault(item => item.Handles(name)); + if (module is null) completions.Add(new Completion(id, false, Json.Quote($"Unsupported operation {name}"))); + else + { + var delivered = false; + module.Start(request, name, job.OptObject("payload"), (ok, json) => + { + lock (gate) + { + if (delivered || cancelled) return; + delivered = true; + completions.Add(new Completion(id, ok, json)); + } + }); + } + return null; + } + } + if (IntlBridge.Handles(operation)) return IntlBridge.Call(operation, payload); + var handler = request.Modules.FirstOrDefault(item => item.Handles(operation)) ?? throw new ArgumentException($"Unsupported operation {operation}"); + return handler.Call(request, operation, payload); + } + + var token = "__sw_" + Convert.ToHexStringLower(RandomNumberGenerator.GetBytes(16)); + var deadline = Stopwatch.GetTimestamp() + request.TimeoutMilliseconds * Stopwatch.Frequency / 1000; + var engine = new QuickJs(Host); + try + { + // The interrupt deadline is a backstop for synchronous loops; the + // kernel's own timeout reports unresolved asynchronous work first. + engine.SetDeadline(request.TimeoutMilliseconds + InterruptGraceMilliseconds); + foreach (var name in scripts.HostFiles) engine.Evaluate(scripts.Text(name), name); + engine.Evaluate(scripts.Text("kernel.js"), "kernel.js"); + var job = new JsonObject + { + ["config"] = Json.Stringify(Configuration(request)), + ["code"] = request.Prepared.Code, + ["timeoutMilliseconds"] = request.TimeoutMilliseconds, + ["supportedAPIs"] = Json.Array(request.Modules.SelectMany(module => module.Apis).Distinct()), + ["platformName"] = PlatformName, + }; + engine.Call("__swBoot", token, Json.Stringify(job)); + + while (finished is null) + { + engine.RunJobs(); + if (finished is not null) break; + if (engine.Call(token, "pump") == "1") continue; + var remaining = Stopwatch.GetElapsedTime(Stopwatch.GetTimestamp(), deadline); + Completion? completion = null; + if (remaining > TimeSpan.Zero) completions.TryTake(out completion, remaining); + if (completion is null) + { + engine.Call(token, "timeout"); + engine.RunJobs(); + finished ??= Failure("resourceLimit", "Script exceeded the 5-second execution limit"); + } + else engine.Call(token, "resolve", completion.ID.ToString(System.Globalization.CultureInfo.InvariantCulture), completion.OK ? "1" : "0", completion.Json); + } + } + catch (JsException error) + { + finished ??= error.Interrupted ? Failure("resourceLimit", "Script exceeded the 5-second execution limit") + : error.Message.Contains("out of memory", StringComparison.OrdinalIgnoreCase) ? Failure("resourceLimit", "Script exceeded the runtime memory budget") + : Failure("internalError", string.IsNullOrEmpty(error.Message) ? "Execution failed" : error.Message); + } + finally + { + lock (gate) cancelled = true; + request.Session.Finished = true; + request.Session.Cancellation.Cancel(); + engine.Dispose(); + } + return Decode(finished!, nativeLogs); + } + + private static string? CompiledImport(ExecutionRequest request, string? relativePath) + { + if (PackagePaths.Normalize(relativePath) is not { } path) return null; + if (!request.Files.Exists(path) || request.Files.ReadText(path) is null) return null; + return request.Prepared.Imports.TryGetValue(path, out var code) ? code : """{"ok":false,"kind":"transformError","message":"Not a script file"}"""; + } + + private JsonObject Configuration(ExecutionRequest request) + { + var prepared = request.Prepared; + var environments = new JsonObject(); + foreach (var (key, value) in prepared.TimelineEnvironment) environments[key] = value; + foreach (var (key, value) in request.Environments) environments[key] = value; + if (prepared.ActionID is not null) + { + if (!request.Environments.ContainsKey("action-id")) environments["action-id"] = prepared.ActionID; + environments["widget-size"] = "action"; + } + request.Environments.TryGetValue("widget-size", out var size); + var config = new JsonObject + { + ["contract"] = scripts.Contract.DeepClone(), + ["environments"] = environments, + ["actionID"] = prepared.ActionID, + ["liveActivityPresentation"] = size is "live-activity" or "dynamic-island", + ["preview"] = new JsonObject { ["appearance"] = request.DarkMode ? "dark" : "light" }, + ["fixtures"] = request.Fixtures.DeepClone(), + ["manifest"] = prepared.Manifest?.DeepClone(), + ["hasManifest"] = prepared.HasManifest, + ["packageID"] = request.PackageID, + }; + if (request.Device is not null) config["device"] = request.Device.DeepClone(); + if (request.System is not null) config["system"] = request.System.DeepClone(); + return config; + } + + private static string Failure(string kind, string message) => Json.Stringify(new JsonObject + { + ["tree"] = null, + ["error"] = new JsonObject { ["kind"] = kind, ["message"] = message }, + ["logs"] = new JsonArray(), + ["diagnostics"] = new JsonArray(), + }); + + private static ExecutionResult Decode(string resultText, List nativeLogs) + { + var json = Json.ParseObject(resultText); + var errorJson = json.OptObject("error"); + var error = errorJson is null ? null : new ExecutionError(errorJson.OptString("kind", "internalError"), errorJson.OptString("message")); + var logs = json.OptArray("logs")?.OfType().Select(item => new LogEntry(item.OptString("level"), item.OptString("message"))).ToList(); + return new ExecutionResult + { + Tree = json.IsNull("tree") ? null : json.OptString("tree"), + Error = error, + Logs = logs is { Count: > 0 } ? logs : nativeLogs, + Diagnostics = (json.OptArray("diagnostics")?.DeepClone() as JsonArray) ?? new JsonArray(), + }; + } +} diff --git a/Windows/src/ScriptWidget.Core/Runtime/SharedScripts.cs b/Windows/src/ScriptWidget.Core/Runtime/SharedScripts.cs new file mode 100644 index 00000000..8cc4812c --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Runtime/SharedScripts.cs @@ -0,0 +1,34 @@ +using System.Collections.Concurrent; +using System.Text; +using System.Text.Json.Nodes; + +namespace ScriptWidget.Core.Runtime; + +/// JavaScript assets generated by Windows/scripts/build-shared-js.mjs. +public sealed class SharedScripts +{ + private readonly string directory; + private readonly ConcurrentDictionary cache = new(); + private readonly Lazy> hostFiles; + private readonly Lazy contractFile; + + /// Folder with kernel.js and the other assets; defaults to assets/scriptwidget next to the program. + public SharedScripts(string? directory = null) + { + this.directory = directory ?? Path.Combine(AppContext.BaseDirectory, "assets", "scriptwidget"); + if (!File.Exists(Path.Combine(this.directory, "kernel.js"))) throw new FileNotFoundException("ScriptWidget runtime assets are missing. Run node Windows/scripts/build-shared-js.mjs.", Path.Combine(this.directory, "kernel.js")); + // host.js defines the registry the API files add themselves to. + hostFiles = new(() => new[] { "host.js" }.Concat(Directory.EnumerateFiles(this.directory, "apis-*.js").Select(Path.GetFileName).OfType().Order(StringComparer.Ordinal)).ToList()); + contractFile = new(() => Json.ParseObject(Text("contract.json"))); + } + + public string Text(string name) => cache.GetOrAdd(name, file => File.ReadAllText(Path.Combine(directory, file), Encoding.UTF8)); + + /// Host files evaluated before the kernel, in order. + public IReadOnlyList HostFiles => hostFiles.Value; + + public JsonObject Contract => (JsonObject)contractFile.Value["contract"]!; + public JsonObject ErrorCategories => (JsonObject)contractFile.Value["errorCategories"]!; + + public string? SupportScript(string name) => name is "util.js" or "moment.min.js" ? Text(name) : null; +} diff --git a/Windows/src/ScriptWidget.Core/Runtime/TimelinePlanner.cs b/Windows/src/ScriptWidget.Core/Runtime/TimelinePlanner.cs new file mode 100644 index 00000000..ef6bedd7 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Runtime/TimelinePlanner.cs @@ -0,0 +1,78 @@ +using System.Globalization; +using System.Text.Json.Nodes; +using ScriptWidget.Core.Js; + +namespace ScriptWidget.Core.Runtime; + +/// The current timeline entry of a package and when the next one starts. +public sealed record TimelineState(IReadOnlyDictionary Environments, long? NextTransitionMilliseconds); + +/// +/// Evaluates a package's timeline.dailyTimes schedule with the planner +/// shared with iOS and the web preview (Web/preview-engine/src/timeline.js), +/// so slots, time zones and daylight-saving rules agree on every platform. +/// +/// iOS hands WidgetKit pre-rendered future entries. Windows, like Android, +/// wakes at the next transition and runs the script again as the current entry. +/// +public sealed class TimelinePlanner(SharedScripts scripts) +{ + private sealed record Cached(TimelineState State, long From, long Until); + private readonly Dictionary cache = new(); + + /// Returns null when the manifest declares no schedule or the schedule is invalid. + public TimelineState? State(JsonObject? manifest, long? nowMilliseconds = null) + { + if (manifest.OptObject("timeline") is not { } schedule) return null; + var now = nowMilliseconds ?? DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + // A plan stays correct until its next transition, so widgets that refresh often reuse it. + var key = Json.Stringify(schedule) + "|" + IntlBridge.DefaultTimeZone(); + lock (cache) + { + if (cache.TryGetValue(key, out var hit) && now >= hit.From && now < hit.Until) return hit.State; + } + if (Evaluate(schedule, now) is not { } state) return null; + var until = Math.Min(state.NextTransitionMilliseconds ?? long.MaxValue, now + 3_600_000); + lock (cache) + { + if (cache.Count >= 32) cache.Clear(); + cache[key] = new Cached(state, now, until); + } + return state; + } + + private TimelineState? Evaluate(JsonObject schedule, long nowMilliseconds) + { + JsonObject plan; + try + { + plan = JsThread.Run(() => + { + // A small context: the planner needs Intl and the prepare bundle, not Babel. + using var engine = new QuickJs((operation, payload) => IntlBridge.Call(operation, payload), memoryLimitBytes: 32L * 1024 * 1024); + engine.SetDeadline(6000); + engine.Evaluate(scripts.Text("host.js"), "host.js"); + engine.Evaluate("globalThis.TextEncoder = class { encode(text) { return new Uint8Array(unescape(encodeURIComponent(String(text))).split('').map(function (c) { return c.charCodeAt(0); })); } };", "prelude.js"); + engine.Evaluate(scripts.Text("prepare.js"), "prepare.js"); + engine.Evaluate( + "globalThis.__swPlan = function (schedule, now, zone) { var time = Number(now);" + + " return JSON.stringify(__swPrepareBundle.buildTimelinePlan(JSON.parse(schedule), new Date(time), new Date(time + 3600000), zone)); };", + "timeline-driver.js"); + return Json.ParseObject(engine.Call("__swPlan", Json.Stringify(schedule), nowMilliseconds.ToString(CultureInfo.InvariantCulture), IntlBridge.DefaultTimeZone())!); + }, "scriptwidget-timeline"); + } + catch (Exception error) when (error is JsException or System.Text.Json.JsonException or NullReferenceException) + { + System.Diagnostics.Trace.WriteLine($"Timeline schedule could not be evaluated: {error.Message[..Math.Min(300, error.Message.Length)]}"); + return null; + } + if (plan.OptArray("entries") is not { } entries || entries.Count == 0) return null; + if ((entries[0] as JsonObject).OptObject("environments") is not { } current) return null; + var environments = new Dictionary(); + foreach (var (key, value) in current) environments[key] = value?.AsText() ?? ""; + long? next = null; + if (entries.Count > 1 && (entries[1] as JsonObject).OptString("date") is { Length: > 0 } date + && DateTimeOffset.TryParse(date, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var parsed)) next = parsed.ToUnixTimeMilliseconds(); + return new TimelineState(environments, next); + } +} diff --git a/Windows/src/ScriptWidget.Core/Runtime/XmlBridge.cs b/Windows/src/ScriptWidget.Core/Runtime/XmlBridge.cs new file mode 100644 index 00000000..d47e8bb7 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Runtime/XmlBridge.cs @@ -0,0 +1,107 @@ +using System.Text; +using System.Text.Json.Nodes; +using System.Text.RegularExpressions; +using System.Xml; + +namespace ScriptWidget.Core.Runtime; + +/// +/// Bounded XML parser behind $xml.parse, with the limits and output +/// shape of ScriptWidgetXMLParser: {name, attributes, text, children}. DTDs +/// and entity declarations are rejected and external entities are never +/// resolved. +/// +public static partial class XmlBridge +{ + private const int MaximumBytes = 256 * 1024; + private const int MaximumElements = 2048; + private const int MaximumDepth = 32; + + private sealed class Node(string name, JsonObject attributes) + { + public readonly StringBuilder Text = new(); + public readonly JsonArray Children = new(); + public JsonObject ToJson() => new() { ["name"] = name, ["attributes"] = attributes, ["text"] = Text.ToString(), ["children"] = Children }; + } + + public static string Parse(string source) + { + try + { + return Json.Stringify(new JsonObject { ["ok"] = true, ["value"] = ParseDocument(source) }); + } + catch (Exception error) when (error is ArgumentException or XmlException) + { + return Json.Stringify(new JsonObject { ["ok"] = false, ["error"] = string.IsNullOrEmpty(error.Message) ? "Invalid XML" : error.Message }); + } + } + + private static JsonObject ParseDocument(string source) + { + if (Encoding.UTF8.GetByteCount(source) > MaximumBytes) throw new ArgumentException($"XML exceeds the {MaximumBytes}-byte limit"); + if (Declaration().IsMatch(source)) throw new ArgumentException("XML document type and entity declarations are not allowed"); + var settings = new XmlReaderSettings + { + DtdProcessing = DtdProcessing.Prohibit, + XmlResolver = null, + IgnoreComments = true, + IgnoreProcessingInstructions = true, + MaxCharactersFromEntities = 0, + MaxCharactersInDocument = MaximumBytes * 2L, + }; + using var reader = XmlReader.Create(new StringReader(source), settings); + var stack = new Stack(); + Node? root = null; + var elements = 0; + var textBytes = 0; + void Close() + { + var node = stack.Pop(); + if (stack.TryPeek(out var parent)) parent.Children.Append(node.ToJson()); + } + while (reader.Read()) + { + switch (reader.NodeType) + { + case XmlNodeType.Element: + { + elements += 1; + if (elements > MaximumElements) throw new ArgumentException($"XML exceeds the {MaximumElements}-element limit"); + if (stack.Count >= MaximumDepth) throw new ArgumentException($"XML exceeds the {MaximumDepth}-level depth limit"); + var attributes = new JsonObject(); + var empty = reader.IsEmptyElement; + var name = reader.Name; + if (reader.MoveToFirstAttribute()) + { + do { attributes[reader.Name] = reader.Value; } while (reader.MoveToNextAttribute()); + reader.MoveToElement(); + } + var node = new Node(name, attributes); + if (stack.Count == 0) + { + if (root is not null) throw new ArgumentException("XML has more than one root element"); + root = node; + } + stack.Push(node); + if (empty) Close(); + break; + } + case XmlNodeType.Text or XmlNodeType.CDATA or XmlNodeType.Whitespace or XmlNodeType.SignificantWhitespace: + { + var text = reader.Value; + textBytes += Encoding.UTF8.GetByteCount(text); + if (textBytes > MaximumBytes) throw new ArgumentException($"XML text exceeds the {MaximumBytes}-byte limit"); + if (stack.TryPeek(out var current)) current.Text.Append(text); + break; + } + case XmlNodeType.EntityReference: throw new ArgumentException("XML entity references are not allowed"); + case XmlNodeType.DocumentType: throw new ArgumentException("XML document type declarations are not allowed"); + case XmlNodeType.EndElement: Close(); break; + } + } + return (root ?? throw new ArgumentException("XML has no root element")).ToJson(); + } + + [GeneratedRegex(@" + + ScriptWidget.Core + true + + false + + + + + + + + + + + + + + + diff --git a/Windows/src/ScriptWidget.Core/Surfaces/ActionPlanner.cs b/Windows/src/ScriptWidget.Core/Surfaces/ActionPlanner.cs new file mode 100644 index 00000000..632c5665 --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Surfaces/ActionPlanner.cs @@ -0,0 +1,113 @@ +namespace ScriptWidget.Core.Surfaces; + +/// Why an action request was refused. The messages match the iOS action errors. +public enum ActionFailure +{ + InvalidRequest, + ActionNotFound, + StorageDenied, + InvalidStateKey, + StorageUnavailable, +} + +public static class ActionFailureMessages +{ + public static string Message(this ActionFailure failure) => failure switch + { + ActionFailure.InvalidRequest => "This widget action is invalid or exceeds its limits. Reload the widget and try again.", + ActionFailure.ActionNotFound => "The ScriptWidget action is no longer available.", + ActionFailure.StorageDenied => "This action did not declare the storage permission.", + ActionFailure.InvalidStateKey => "The action uses an invalid storage key.", + _ => "Widget storage is busy or unavailable. Please try again.", + }; +} + +/// Thrown by ; nothing has run or been stored when it is thrown. +public sealed class ActionRejected(ActionFailure failure) : Exception(failure.Message()) +{ + public ActionFailure Failure { get; } = failure; +} + +/// +/// What to run for one accepted request. Exactly one of +/// (a declared action) and (a control's legacy +/// function) is set. +/// +/// Values the script reads with $getenv. +/// Package storage key to write to before the action runs, or null. +public sealed record ActionPlan(string? ActionID, string? CallFunction, IReadOnlyDictionary Environments, string? StateKey, bool? StateValue); + +/// +/// Decides what a system surface may run. Only actions and controls the +/// manifest declares are accepted; a serialized request can select one of them +/// but can never name an arbitrary function. +/// +public static class ActionPlanner +{ + public const string SourceWidget = "widget"; + public const string SourceControl = "control"; + public const string SourceShortcut = "shortcut"; + private static readonly HashSet Sources = [SourceWidget, SourceControl, SourceShortcut]; + + /// The declared action; for it may be null and is then taken from the control. + /// A toggle's new value, or null for a button. + /// The declared control; required for, and only allowed with, . + /// Storage key of a widget <toggle>; a control's own stateKey always wins. + /// The request does not match the manifest. + public static ActionPlan Plan(PackageSurfaces surfaces, string? actionID, string source, bool? value, string? controlID, string? stateKey = null) + { + if (!Sources.Contains(source)) throw new ActionRejected(ActionFailure.InvalidRequest); + var environments = new Dictionary + { + ["widget-size"] = "action", + ["widget-param"] = "", + ["action-source"] = source, + }; + + if (source == SourceControl) + { + var control = surfaces.Control(controlID) ?? throw new ActionRejected(ActionFailure.ActionNotFound); + var declaredAction = control.ActionID is null ? null : surfaces.Action(control.ActionID) ?? throw new ActionRejected(ActionFailure.ActionNotFound); + // A caller that names an action must name the one this control is declared with. + if (!string.IsNullOrEmpty(actionID) && !actionID.Equals(declaredAction?.ID ?? control.ID, StringComparison.OrdinalIgnoreCase)) throw new ActionRejected(ActionFailure.ActionNotFound); + if (control.IsToggle != (value is not null)) throw new ActionRejected(ActionFailure.InvalidRequest); + string? controlKey = null; + if (control.IsToggle) + { + if (!surfaces.HasStoragePermission) throw new ActionRejected(ActionFailure.StorageDenied); + if (!SurfaceManifest.IsValidStateKey(control.StateKey)) throw new ActionRejected(ActionFailure.InvalidStateKey); + controlKey = control.StateKey; + } + // A legacy control behaves like an action named after the control. + environments["action-id"] = declaredAction?.ID ?? control.ID; + environments["control-id"] = control.ID; + Value(environments, value); + return new ActionPlan(declaredAction?.ID, declaredAction is null ? control.Function : null, environments, controlKey, value); + } + + if (controlID is not null) throw new ActionRejected(ActionFailure.InvalidRequest); + var declared = surfaces.Action(actionID) ?? throw new ActionRejected(ActionFailure.ActionNotFound); + string? key = null; + if (stateKey is not null) + { + if (value is null) throw new ActionRejected(ActionFailure.InvalidRequest); + if (!surfaces.HasStoragePermission) throw new ActionRejected(ActionFailure.StorageDenied); + if (!SurfaceManifest.IsValidStateKey(stateKey)) throw new ActionRejected(ActionFailure.InvalidStateKey); + key = stateKey; + } + environments["action-id"] = declared.ID; + Value(environments, value); + return new ActionPlan(declared.ID, null, environments, key, value); + } + + /// A stored toggle state: true and 1 are on, everything else (including a missing value) is off. + public static bool IsOn(string? stored) => stored is "true" or "1"; + + private static void Value(Dictionary environments, bool? value) + { + if (value is null) return; + environments["action-value"] = value.Value ? "true" : "false"; + // Compatibility name from the first Control Widget release. + environments["control-value"] = environments["action-value"]; + } +} diff --git a/Windows/src/ScriptWidget.Core/Surfaces/SurfaceManifest.cs b/Windows/src/ScriptWidget.Core/Surfaces/SurfaceManifest.cs new file mode 100644 index 00000000..19bb2fca --- /dev/null +++ b/Windows/src/ScriptWidget.Core/Surfaces/SurfaceManifest.cs @@ -0,0 +1,177 @@ +using System.Globalization; +using System.Text.Json.Nodes; +using System.Text.RegularExpressions; + +namespace ScriptWidget.Core.Surfaces; + +/// One entry of a package's actions: a JavaScript function the system may run by its . +public sealed record DeclaredAction(string ID, string Title, string? Description, string SystemImage, string Function); + +/// +/// One entry of a package's controls. Exactly one of +/// (a declared action) and (the legacy direct +/// action field) is set. A toggle always has a ; +/// a button never has one. +/// +public sealed record DeclaredControl(string ID, string Type, string Title, string? Subtitle, string SystemImage, string? Function, string? ActionID, string? StateKey) +{ + public const string TypeButton = "button"; + public const string TypeToggle = "toggle"; + public bool IsToggle => Type == TypeToggle; +} + +/// The validated actions and controls of one package manifest. +public sealed class PackageSurfaces(IReadOnlyList actions, IReadOnlyList controls, bool hasStoragePermission) +{ + public IReadOnlyList Actions { get; } = actions; + public IReadOnlyList Controls { get; } = controls; + public bool HasStoragePermission { get; } = hasStoragePermission; + + /// The declared action with this identifier (compared without case, as on iOS), or null. + public DeclaredAction? Action(string? id) + { + if (string.IsNullOrEmpty(id)) return null; + var matches = Actions.Where(item => item.ID.Equals(id, StringComparison.OrdinalIgnoreCase)).ToList(); + return matches.Count == 1 ? matches[0] : null; + } + + /// The declared control with this identifier, or null. + public DeclaredControl? Control(string? id) + { + if (string.IsNullOrEmpty(id)) return null; + var matches = Controls.Where(item => item.ID.Equals(id, StringComparison.OrdinalIgnoreCase)).ToList(); + return matches.Count == 1 ? matches[0] : null; + } + + /// What a package without a manifest declares: nothing. + public static readonly PackageSurfaces Empty = new([], [], false); +} + +/// A manifest whose actions or controls break the Package 2.0 contract. matches the shared validator's issue codes. +public sealed class SurfaceManifestException(string code, string message) : Exception(message) +{ + public string Code { get; } = code; +} + +/// +/// Reads actions and controls from a decoded widget.json +/// with the same limits as the shared manifest validator. Anything malformed +/// throws: a system surface never runs code from a manifest it does not fully +/// accept. +/// +public static partial class SurfaceManifest +{ + public const int MaximumActions = 16; + public const int MaximumControls = 8; + + [GeneratedRegex(@"^[A-Za-z0-9][A-Za-z0-9.-]{0,63}\z")] private static partial Regex ShortID(); + [GeneratedRegex(@"^[A-Za-z0-9][A-Za-z0-9.-]{0,79}\z")] private static partial Regex Symbol(); + [GeneratedRegex(@"^[A-Za-z_$][A-Za-z0-9_$]{0,63}\z")] private static partial Regex Function(); + [GeneratedRegex(@"^[A-Za-z0-9][A-Za-z0-9._-]{0,127}\z")] private static partial Regex StateKey(); + + private static readonly HashSet ActionKeys = ["id", "title", "description", "systemImage", "function"]; + private static readonly HashSet ControlKeys = ["id", "type", "title", "subtitle", "systemImage", "action", "actionID", "stateKey"]; + + /// True when may hold a toggle's state in package storage. + public static bool IsValidStateKey(string? key) => key is not null && StateKey().IsMatch(key); + + /// True when is a JavaScript function identifier a manifest may name. + public static bool IsValidFunction(string? name) => name is not null && Function().IsMatch(name); + + /// + /// Validates and returns the declared surfaces. A null manifest (a legacy + /// package) declares none. + /// + /// A declaration is malformed or over a limit. + public static PackageSurfaces Parse(JsonObject? manifest) + { + if (manifest is null) return PackageSurfaces.Empty; + var storage = manifest.OptArray("permissions")?.Any(item => item.IsString() && item!.AsText() == "storage") ?? false; + var actions = Actions(manifest); + return new PackageSurfaces(actions, Controls(manifest, actions, storage), storage); + } + + private static List Actions(JsonObject manifest) + { + var items = Objects(manifest, "actions", ActionKeys); + if (items.Count > MaximumActions) throw Fail("too_many_actions", "A package may declare at most 16 actions."); + var result = items.Select(item => + { + var id = Required(item, "id", "actions"); + if (!ShortID().IsMatch(id)) throw Fail("invalid_action_id", "Invalid action identifier."); + var title = Required(item, "title", "actions"); + if (string.IsNullOrWhiteSpace(title) || Characters(title) > 40) throw Fail("invalid_action_title", "Action titles must contain 1–40 characters."); + var description = Optional(item, "description", "actions"); + if (description is not null && Characters(description) > 160) throw Fail("invalid_action_description", "Action descriptions may contain at most 160 characters."); + var image = Required(item, "systemImage", "actions"); + if (!Symbol().IsMatch(image)) throw Fail("invalid_action_image", "Action systemImage must be an SF Symbols name."); + var name = Required(item, "function", "actions"); + if (!Function().IsMatch(name)) throw Fail("invalid_action_function", "Action function must be a JavaScript function identifier."); + return new DeclaredAction(id, title, description, image, name); + }).ToList(); + if (result.Select(item => item.ID.ToLowerInvariant()).Distinct().Count() != result.Count) throw Fail("duplicate_actions", "Action identifiers must be unique."); + return result; + } + + private static List Controls(JsonObject manifest, List actions, bool storage) + { + var items = Objects(manifest, "controls", ControlKeys); + if (items.Count > MaximumControls) throw Fail("too_many_controls", "A package may declare at most 8 controls."); + var result = items.Select(item => + { + var id = Required(item, "id", "controls"); + if (!ShortID().IsMatch(id)) throw Fail("invalid_control_id", "Invalid control identifier."); + var type = Required(item, "type", "controls"); + if (type != DeclaredControl.TypeButton && type != DeclaredControl.TypeToggle) throw Fail("malformed_controls", "controls are malformed."); + var title = Required(item, "title", "controls"); + if (string.IsNullOrWhiteSpace(title) || Characters(title) > 40) throw Fail("invalid_control_title", "Control titles must contain 1–40 characters."); + var subtitle = Optional(item, "subtitle", "controls"); + if (subtitle is not null && Characters(subtitle) > 80) throw Fail("invalid_control_subtitle", "Control subtitles may contain at most 80 characters."); + var image = Required(item, "systemImage", "controls"); + if (!Symbol().IsMatch(image)) throw Fail("invalid_control_image", "Control systemImage must be an SF Symbols name."); + var legacy = Optional(item, "action", "controls"); + var actionID = Optional(item, "actionID", "controls"); + if (legacy is not null && actionID is not null) throw Fail("ambiguous_control_action", "Controls must declare either action or actionID, not both."); + if (legacy is not null) { if (!Function().IsMatch(legacy)) throw Fail("invalid_control_action", "Control action must be a JavaScript function identifier."); } + else if (actionID is not null) { if (!actions.Any(action => action.ID.Equals(actionID, StringComparison.OrdinalIgnoreCase))) throw Fail("missing_control_action", "A control references an unknown action."); } + else throw Fail("missing_control_action", "Controls must declare actionID or the legacy action field."); + var key = Optional(item, "stateKey", "controls"); + if (type == DeclaredControl.TypeToggle) + { + if (!IsValidStateKey(key)) throw Fail("invalid_control_state", "Toggle controls require a valid stateKey."); + if (!storage) throw Fail("control_storage", "Toggle controls require the storage permission."); + } + else if (key is not null) throw Fail("button_control_state", "Button controls must not declare stateKey."); + return new DeclaredControl(id, type, title, subtitle, image, legacy, actionID, key); + }).ToList(); + if (result.Select(item => item.ID.ToLowerInvariant()).Distinct().Count() != result.Count) throw Fail("duplicate_controls", "Control identifiers must be unique."); + return result; + } + + /// The objects of an optional array field; unknown keys and other shapes are rejected. + private static List Objects(JsonObject manifest, string field, HashSet allowed) + { + if (!manifest.TryGetPropertyValue(field, out var value) || value is null) return []; + if (value is not JsonArray array) throw Fail($"malformed_{field}", $"{field} are malformed."); + return array.Select(node => + { + if (node is not JsonObject item) throw Fail($"malformed_{field}", $"{field} are malformed."); + if (item.Any(member => !allowed.Contains(member.Key))) throw Fail($"unknown_{field}_fields", $"{field} have unknown fields."); + return item; + }).ToList(); + } + + private static string Required(JsonObject item, string key, string field) => + item.TryGetPropertyValue(key, out var value) && value.IsString() ? value!.AsText()! : throw Fail($"malformed_{field}", $"{field} are malformed."); + + private static string? Optional(JsonObject item, string key, string field) + { + if (!item.TryGetPropertyValue(key, out var value) || value is null) return null; + return value.IsString() ? value.AsText() : throw Fail($"malformed_{field}", $"{field} are malformed."); + } + + /// User-perceived characters, the unit Swift's String.count uses. + internal static int Characters(string text) => new StringInfo(text).LengthInTextElements; + + private static SurfaceManifestException Fail(string code, string message) => new(code, message); +} diff --git a/Windows/src/ScriptWidget.Core/WidgetRuntime.cs b/Windows/src/ScriptWidget.Core/WidgetRuntime.cs new file mode 100644 index 00000000..8cc6210d --- /dev/null +++ b/Windows/src/ScriptWidget.Core/WidgetRuntime.cs @@ -0,0 +1,294 @@ +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; +using ScriptWidget.Core.Api; +using ScriptWidget.Core.Hosting; +using ScriptWidget.Core.Plugins; +using ScriptWidget.Core.Render; +using ScriptWidget.Core.Runtime; + +namespace ScriptWidget.Core; + +/// Everything needed to run one package once. +public sealed class RunRequest +{ + public required IPackageFiles Files { get; init; } + /// Stable identity of the installed package; namespaces storage and caches. + public required string PackageID { get; init; } + public string Family { get; init; } = "medium"; + public string Parameter { get; init; } = ""; + public bool DarkMode { get; init; } + /// Declared action to run (actions[].id), or null to render. + public string? ActionID { get; init; } + /// Legacy onClick="name" function to call after the entry. + public string? CallFunction { get; init; } + public IReadOnlyDictionary ExtraEnvironments { get; init; } = new Dictionary(); + /// Directory for the compiled-artifact cache; null disables caching. + public string? BuildDirectory { get; init; } +} + +public sealed class RunResult(ExecutionResult execution, PreparedPackage? prepared, ExecutionRequest? request, long? nextTimelineTransition = null) +{ + public ExecutionResult Execution { get; } = execution; + public PreparedPackage? Prepared { get; } = prepared; + public ExecutionRequest? Request { get; } = request; + /// When the package's daily schedule next changes slot; the host should run the package again then. + public long? NextTimelineTransition { get; } = nextTimelineTransition; + /// True when the script's output may depend on the light/dark theme it ran under. + public bool DependsOnAppearance => Request?.Session.ReadAppearance == true; +} + +/// +/// The Windows runtime: compiles a package, executes it, and renders its +/// tree. One instance per process; all methods are safe to call from any +/// background thread. +/// +public sealed class WidgetRuntime : IDisposable +{ + private const int MaximumCachedArtifacts = 6; + private const int MaximumRemoteImages = 8; + private const long ImageBudgetMilliseconds = 6000; + private const long ImageCacheMilliseconds = 15 * 60 * 1000; + + private readonly IReadOnlyList modules; + private readonly string imageCache; + + public SharedScripts Scripts { get; } + public PackageCompiler Compiler { get; } + public ScriptExecutor Executor { get; } + public WidgetRenderer Renderer { get; } + public TimelinePlanner Timeline { get; } + public StorageModule Storage { get; } + public FetchModule Fetch { get; } + public PluginStore Plugins { get; } + + public WidgetRuntime(DataPaths paths, IEnumerable? extraModules = null, string? assetsDirectory = null) + { + Scripts = new SharedScripts(assetsDirectory); + Compiler = new PackageCompiler(Scripts); + Executor = new ScriptExecutor(Scripts); + Renderer = new WidgetRenderer(Scripts); + Timeline = new TimelinePlanner(Scripts); + Storage = new StorageModule(paths.Storage); + Fetch = new FetchModule(); + Plugins = new PluginStore(paths.Plugins); + var version = typeof(WidgetRuntime).Assembly.GetName().Version; + var weather = new WeatherModule(new FetchWeatherTransport(Fetch), Path.Combine(paths.Cache, "weather"), WeatherModule.UserAgent(version is null ? "" : $"{version.Major}.{version.Minor}.{version.Build}")); + modules = [Storage, Fetch, new DataSourceModule(Plugins), weather, .. extraModules ?? []]; + imageCache = Path.Combine(paths.Cache, "remote-images"); + } + + /// Compiles a package, reusing the cached artifact when no source changed. + public PreparedPackage Prepare(RunRequest request) + { + var cacheFile = request.BuildDirectory is null ? null : Path.Combine(request.BuildDirectory, $"prepared-{CacheKey(request)}.json"); + if (cacheFile is not null && File.Exists(cacheFile)) + { + try { return PreparedPackage.FromJson(Json.ParseObject(File.ReadAllText(cacheFile, Encoding.UTF8))); } + catch (Exception error) when (error is JsonException or IOException or UnauthorizedAccessException or RuntimeError) { } + } + var prepared = Compiler.Prepare(request.Files, actionID: request.ActionID, callFunction: request.CallFunction); + if (cacheFile is not null) + { + try + { + var directory = new DirectoryInfo(request.BuildDirectory!); + directory.Create(); + // Keep the cache small: one artifact per (source, action) pair, older ones removed. + foreach (var old in directory.EnumerateFiles("prepared-*.json").OrderByDescending(file => file.LastWriteTimeUtc).Skip(MaximumCachedArtifacts - 1)) old.Delete(); + var temporary = cacheFile + ".tmp"; + File.WriteAllText(temporary, Json.Stringify(prepared.ToJson()), new UTF8Encoding(false)); + File.Move(temporary, cacheFile, overwrite: true); + } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) { } + } + return prepared; + } + + private string CacheKey(RunRequest request) + { + using var digest = IncrementalHash.CreateHash(HashAlgorithmName.SHA256); + void Add(string text) { digest.AppendData(Encoding.UTF8.GetBytes(text)); digest.AppendData([0]); } + digest.AppendData(Encoding.UTF8.GetBytes(Scripts.Contract.OptString("compilerFingerprint"))); + Add(request.ActionID ?? ""); + Add(request.CallFunction ?? ""); + foreach (var path in request.Files.Paths()) + { + Add(path); + Add(PackageCompiler.IsTextFile(path) ? request.Files.ReadText(path) ?? "" : ""); + } + return Convert.ToHexStringLower(digest.GetHashAndReset())[..32]; + } + + /// Compiles the package the way an installation would; throws when it is invalid. + public void ValidatePackage(IPackageFiles files) => Compiler.Prepare(files); + + /// Compiles and executes. Compile failures are returned as an execution error, never thrown. + public RunResult Run(RunRequest request) + { + PreparedPackage prepared; + try { prepared = Prepare(request); } + catch (RuntimeError error) + { + return new RunResult(new ExecutionResult { Error = new ExecutionError(error.Kind, error.Message) }, null, null); + } + var environments = new Dictionary + { + ["widget-size"] = request.Family, + ["widget-param"] = request.Parameter, + ["widget-rendering-mode"] = "fullColor", + }; + // The cached artifact carries the slot of the moment it was compiled; use the slot of now. + var schedule = Timeline.State(prepared.Manifest); + if (schedule is not null) foreach (var (key, value) in schedule.Environments) environments[key] = value; + foreach (var (key, value) in request.ExtraEnvironments) environments[key] = value; + var execution = new ExecutionRequest + { + Prepared = prepared, + Files = request.Files, + Environments = environments, + DarkMode = request.DarkMode, + Device = DeviceInfo.Device(), + System = DeviceInfo.System(), + Modules = modules, + PackageID = request.PackageID, + }; + return new RunResult(Executor.Execute(execution), prepared, execution, schedule?.NextTransitionMilliseconds); + } + + /// + /// Renders the result of . Errors become a visible card so + /// a widget is never blank. Remote images are fetched first, under the + /// same network policy as fetch. + /// + /// Width in points. + /// Pixels per point of the display the widget is on. + /// Corner radius in points; 0 leaves the widget square. + public RenderOutput Render(RunRequest request, RunResult result, float width, float height, float density, bool? darkMode = null, long? nowMilliseconds = null, long maximumBitmapBytes = long.MaxValue, float cornerRadius = RoundedCorners.DefaultRadius) + { + var output = RenderSquare(request, result, width, height, density, darkMode, nowMilliseconds, maximumBitmapBytes); + RoundedCorners.Apply(output.Pixels, output.PixelWidth, output.PixelHeight, cornerRadius * output.Scale); + return output; + } + + private RenderOutput RenderSquare(RunRequest request, RunResult result, float width, float height, float density, bool? darkMode, long? nowMilliseconds, long maximumBitmapBytes) + { + var dark = darkMode ?? request.DarkMode; + var tree = result.Execution.Tree ?? ErrorTree(result.Execution.Error); + var remote = result.Execution.Tree is not null && result.Request is not null ? FetchRemoteImages(tree, result.Request) : new Dictionary(); + try + { + return Renderer.Render(new RenderRequest + { + Tree = tree, Width = width, Height = height, Density = density, Family = request.Family, DarkMode = dark, + Files = request.Files, RemoteImages = remote, NowMilliseconds = nowMilliseconds, MaximumBitmapBytes = maximumBitmapBytes, + }); + } + catch (Exception error) when (error is not OutOfMemoryException) + { + return Renderer.Render(new RenderRequest + { + Tree = ErrorTree(new ExecutionError("internalError", string.IsNullOrEmpty(error.Message) ? "Rendering failed" : error.Message)), + Width = width, Height = height, Density = density, Family = request.Family, DarkMode = dark, MaximumBitmapBytes = maximumBitmapBytes, + }); + } + } + + /// Renders a canonical tree that is not the output of a package, such as a message card. + public RenderOutput RenderTree(string tree, string family, float width, float height, float density, bool darkMode, float cornerRadius = RoundedCorners.DefaultRadius) + { + var output = Renderer.Render(new RenderRequest { Tree = tree, Width = width, Height = height, Density = density, Family = family, DarkMode = darkMode }); + RoundedCorners.Apply(output.Pixels, output.PixelWidth, output.PixelHeight, cornerRadius * output.Scale); + return output; + } + + /// A canonical tree that explains with its category and recovery hint. + public string ErrorTree(ExecutionError? error) + { + var kind = error?.Kind ?? "internalError"; + var category = Scripts.ErrorCategories.OptObject(kind) ?? Scripts.ErrorCategories.OptObject("internalError"); + var title = category?.OptStringOrNull("category") ?? "Internal"; + var recovery = category.OptString("recovery"); + var message = error?.Message ?? "Unknown error"; + if (message.Length > 400) message = message[..400]; + // Fixed colors: the card keeps its light background in dark mode. + return MessageTree( + ($"{title} error", """{"color":"#b91c1c","font":"caption","fontWeight":"bold"}"""), + (message, """{"color":"#1f2937","font":"caption"}"""), + (recovery, """{"color":"#6b7280","font":"caption2"}""")); + } + + /// A card with a title and a line of explanation, for widgets that have nothing to run. + public static string NoticeTree(string title, string detail) => MessageTree( + (title, """{"color":"#1f2937","font":"caption","fontWeight":"bold"}"""), + (detail, """{"color":"#6b7280","font":"caption2"}""")); + + private static string MessageTree(params (string Text, string Props)[] lines) + { + var children = string.Join(",", lines.Select(line => $$"""{"$element":"text","props":{{line.Props}},"children":[{{Json.Quote(line.Text)}}]}""")); + return $$"""{"$element":"vstack","props":{"alignment":"leading","background":"#fef2f2","frame":"max,topLeading","padding":"12","spacing":4},"children":[{{children}}]}"""; + } + + /// Downloads http(s) <image url> sources referenced by the tree, bounded in count, size and time. + private Dictionary FetchRemoteImages(string tree, ExecutionRequest execution) + { + var result = new Dictionary(); + if (RuntimePolicy.IsTimelineProjection(execution) || RuntimePolicy.IsLiveActivity(execution)) return result; + var urls = new List(); + void Visit(JsonNode? node) + { + switch (node) + { + case JsonObject item: + if (item.OptString("$element") == "image") + { + var props = item.OptObject("props"); + var url = props.OptString("url") is { Length: > 0 } direct ? direct : props.OptString("src"); + if (url.Length > 0 && (url.StartsWith("http://", StringComparison.Ordinal) || url.StartsWith("https://", StringComparison.Ordinal)) && !urls.Contains(url)) urls.Add(url); + } + Visit(item.OptArray("children")); + break; + case JsonArray array: + foreach (var child in array) Visit(child); + break; + } + } + try { Visit(Json.Parse(tree)); } catch (JsonException) { return result; } + if (urls.Count == 0) return result; + try { Directory.CreateDirectory(imageCache); } catch (Exception error) when (error is IOException or UnauthorizedAccessException) { return result; } + var deadline = Environment.TickCount64 + ImageBudgetMilliseconds; + foreach (var url in urls.Take(MaximumRemoteImages)) + { + if (!Uri.TryCreate(url, UriKind.Absolute, out var uri)) continue; + if (Fetch.ValidationError(uri) is not null || !RuntimePolicy.PackageAllowsHost(execution, uri.Host)) continue; + var name = Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(execution.PackageID + "\n" + url))); + var file = Path.Combine(imageCache, name); + var fresh = File.Exists(file) && (DateTime.UtcNow - File.GetLastWriteTimeUtc(file)).TotalMilliseconds < ImageCacheMilliseconds; + if (!fresh && Environment.TickCount64 < deadline && execution.Session.AddNetworkRequest() <= FetchModule.MaximumRequests) + { + try + { + var response = Fetch.Perform("GET", uri, new Dictionary(), null, 4000, + next => Fetch.ValidationError(next) is null && RuntimePolicy.PackageAllowsHost(execution, next.Host)); + if (response.Status is >= 200 and <= 299 && response.Body.Length > 0) + { + var temporary = file + ".tmp"; + File.WriteAllBytes(temporary, response.Body); + File.Move(temporary, file, overwrite: true); + } + } + catch (Exception error) when (error is not OutOfMemoryException) { /* a stale copy or the placeholder is shown */ } + } + // A stale copy is better than a placeholder when the network is unavailable. + if (File.Exists(file)) result[url] = file; + } + return result; + } + + public void Dispose() + { + Compiler.Dispose(); + Renderer.Dispose(); + } +} diff --git a/Windows/src/ScriptWidget.Studio/App.xaml b/Windows/src/ScriptWidget.Studio/App.xaml new file mode 100644 index 00000000..40b23a99 --- /dev/null +++ b/Windows/src/ScriptWidget.Studio/App.xaml @@ -0,0 +1,11 @@ + + + + + + + + + diff --git a/Windows/src/ScriptWidget.Studio/App.xaml.cs b/Windows/src/ScriptWidget.Studio/App.xaml.cs new file mode 100644 index 00000000..5cbec33b --- /dev/null +++ b/Windows/src/ScriptWidget.Studio/App.xaml.cs @@ -0,0 +1,69 @@ +using Microsoft.UI.Xaml; + +namespace ScriptWidget.Studio; + +public partial class App : Application +{ + private readonly Request initial; + private MainWindow? window; + + public App(Request initial) + { + this.initial = initial; + InitializeComponent(); + } + + protected override void OnLaunched(LaunchActivatedEventArgs args) + { + window = new MainWindow(); + window.Activate(); + Handle(initial); + } + + /// Another start of the program handed over its request. Called on a background thread. + public void OnRedirected(Request request) => window?.DispatcherQueue.TryEnqueue(() => + { + if (window is null) return; + window.Activate(); + SetForegroundWindow(window.WindowHandle); + Handle(request); + }); + + private void Handle(Request request) + { + if (window is null) return; + if (request.ImportFile is { } file) _ = window.ImportArchiveAsync(file); + else if (request.OpenPackage is { } package) window.OpenEditor(package); + else if (request.Page == "templates") window.ShowTemplates(); + else if (request.Page == "components") window.ShowComponents(); + else if (request.Page == "apis") window.ShowApis(); + else if (request.Page is { } page && page.StartsWith("template:", StringComparison.Ordinal)) window.ShowDetail(page["template:".Length..]); + else if (request.Page == "settings") window.ShowSettings(); + if (request.Screenshot is { } picture) _ = CaptureAsync(picture); + } + + /// Saves a picture of the window content and exits. The code editor (a WebView2) is not part of the picture. + private async Task CaptureAsync(string file) + { + try + { + await Task.Delay(9000); + var bitmap = new Microsoft.UI.Xaml.Media.Imaging.RenderTargetBitmap(); + await bitmap.RenderAsync(window!.Content); + var pixels = await bitmap.GetPixelsAsync(); + using var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream(); + var encoder = await Windows.Graphics.Imaging.BitmapEncoder.CreateAsync(Windows.Graphics.Imaging.BitmapEncoder.PngEncoderId, stream); + encoder.SetPixelData(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, Windows.Graphics.Imaging.BitmapAlphaMode.Premultiplied, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, 96, 96, System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(pixels)); + await encoder.FlushAsync(); + var bytes = new byte[stream.Size]; + stream.Seek(0); + await stream.ReadAsync(System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.AsBuffer(bytes), (uint)bytes.Length, Windows.Storage.Streams.InputStreamOptions.None); + await File.WriteAllBytesAsync(file, bytes); + if (window.EditorPage is { } editorPage) await editorPage.CaptureEditorAsync(Path.ChangeExtension(file, ".editor.png")); + } + catch (Exception error) { await File.WriteAllTextAsync(file + ".error.txt", error.ToString()); } + Exit(); + } + + [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern int SetForegroundWindow(nint window); +} diff --git a/Windows/src/ScriptWidget.Studio/Editor/StudioEditor.cs b/Windows/src/ScriptWidget.Studio/Editor/StudioEditor.cs new file mode 100644 index 00000000..6eb10c69 --- /dev/null +++ b/Windows/src/ScriptWidget.Studio/Editor/StudioEditor.cs @@ -0,0 +1,167 @@ +using System.Collections.Concurrent; +using System.Text.Json.Nodes; +using Microsoft.UI.Xaml.Controls; +using Microsoft.Web.WebView2.Core; +using ScriptWidget.Core; + +namespace ScriptWidget.Studio.Editor; + +/// +/// The CodeMirror-based Studio editor (the same bundle the Apple and Android +/// apps embed) in a WebView2. The page is served only from the program's own +/// folder under a private host name; it cannot navigate elsewhere, open +/// windows or reach the network. This is the only web content in the +/// management window; widgets are never drawn by it. +/// +public sealed class StudioEditor +{ + private const string Host = "studio.scriptwidget.invalid"; + + private readonly WebView2 view; + private readonly ConcurrentDictionary> replies = new(); + private string documentID = ""; + private string sessionID = ""; + private (string ID, string Content, bool ReadOnly)? pendingDocument; + private bool? pendingDark; + + public bool IsReady { get; private set; } + /// The user asked to save (Ctrl+S). Return true when the text was saved. + public Func? Save { get; set; } + public event Action? Ready; + /// The editor could not start (for example the WebView2 runtime is missing). + public event Action? Failed; + + public StudioEditor(WebView2 view) + { + this.view = view; + _ = InitializeAsync(); + } + + private async Task InitializeAsync() + { + try + { + await view.EnsureCoreWebView2Async(); + var core = view.CoreWebView2; + core.Settings.AreDevToolsEnabled = false; + core.Settings.IsStatusBarEnabled = false; + core.Settings.AreDefaultContextMenusEnabled = true; + core.Settings.IsZoomControlEnabled = false; + core.Settings.IsPasswordAutosaveEnabled = false; + core.Settings.IsGeneralAutofillEnabled = false; + core.Settings.AreBrowserAcceleratorKeysEnabled = false; + core.SetVirtualHostNameToFolderMapping(Host, Path.Combine(AppContext.BaseDirectory, "StudioEditor"), CoreWebView2HostResourceAccessKind.DenyCors); + // Anything that is not the editor's own files is refused. + core.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All); + core.WebResourceRequested += (_, arguments) => + { + if (Uri.TryCreate(arguments.Request.Uri, UriKind.Absolute, out var uri) && uri.Host == Host) return; + arguments.Response = core.Environment.CreateWebResourceResponse(null, 403, "Forbidden", ""); + }; + core.NavigationStarting += (_, arguments) => + { + if (!Uri.TryCreate(arguments.Uri, UriKind.Absolute, out var uri) || uri.Host != Host) arguments.Cancel = true; + }; + core.NewWindowRequested += (_, arguments) => arguments.Handled = true; + core.PermissionRequested += (_, arguments) => arguments.State = CoreWebView2PermissionState.Deny; + core.DownloadStarting += (_, arguments) => arguments.Cancel = true; + core.WebMessageReceived += (_, arguments) => + { + string text; + try { text = arguments.TryGetWebMessageAsString(); } catch (ArgumentException) { return; } + if (Json.TryParse(text) is JsonObject message) OnMessage(message); + }; + await core.AddScriptToExecuteOnDocumentCreatedAsync(await File.ReadAllTextAsync(Path.Combine(AppContext.BaseDirectory, "StudioEditor", "windows-bridge.js"))); + core.Navigate($"https://{Host}/index.html"); + } + catch (Exception error) + { + Failed?.Invoke(error.Message); + } + } + + /// Shows as the document being edited. + public void Open(string id, string content, bool readOnly = false) + { + documentID = id; + sessionID = Guid.NewGuid().ToString(); + if (!IsReady) { pendingDocument = (id, content, readOnly); return; } + Invoke("document.open", new JsonObject { ["content"] = content, ["version"] = 0, ["readOnly"] = readOnly, ["resources"] = new JsonArray() }, _ => { }); + } + + /// Reads the current text from the editor; null when it is not available. + public Task ContentAsync() + { + var completion = new TaskCompletionSource(); + if (!IsReady) { completion.SetResult(null); return completion.Task; } + Invoke("editor.getState", new JsonObject(), response => completion.TrySetResult(response.OptString("result") == "ok" ? response.OptString("content") : null)); + _ = Task.Delay(3000).ContinueWith(_ => completion.TrySetResult(null), TaskScheduler.Default); + return completion.Task; + } + + public void Format() { if (IsReady) Invoke("editor.format", new JsonObject(), _ => { }); } + + public void SetTheme(bool dark) + { + if (!IsReady) { pendingDark = dark; return; } + Invoke("editor.setTheme", new JsonObject { ["theme"] = dark ? "dark" : "light" }, _ => { }); + } + + public void PublishDiagnostics(string? message) + { + if (!IsReady) return; + var items = new JsonArray(); + if (message is not null) items.Append(new JsonObject { ["message"] = message, ["severity"] = "error" }); + Invoke("diagnostics.publish", new JsonObject { ["items"] = items }, _ => { }); + } + + private void Invoke(string name, JsonObject payload, Action reply) + { + var replyID = Guid.NewGuid().ToString(); + replies[replyID] = reply; + var envelope = new JsonObject { ["protocolVersion"] = 1, ["type"] = name, ["documentID"] = documentID, ["sessionID"] = sessionID, ["payload"] = payload }; + _ = view.ExecuteScriptAsync($"window.WKWebViewJavascriptBridge._invoke({Json.Quote(name)}, {Json.Stringify(envelope)}, {Json.Quote(replyID)});"); + } + + private void Respond(string? id, JsonObject response) + { + if (id is null) return; + _ = view.ExecuteScriptAsync($"window.WKWebViewJavascriptBridge._respond({Json.Quote(id)}, {Json.Stringify(response)});"); + } + + /// A message from the page. Runs on the UI thread. + private void OnMessage(JsonObject message) + { + if (message.OptString("kind") == "reply") + { + if (replies.TryRemove(message.OptString("replyID"), out var callback)) callback(message.OptObject("response") ?? new JsonObject { ["result"] = "failed" }); + return; + } + var id = message.IsNull("id") ? null : message.OptString("id"); + var data = message.OptObject("data") ?? new JsonObject(); + switch (message.OptString("name")) + { + case "studio.ready": + IsReady = true; + if (pendingDocument is { } pending) { pendingDocument = null; Open(pending.ID, pending.Content, pending.ReadOnly); } + if (pendingDark is { } dark) { pendingDark = null; SetTheme(dark); } + Ready?.Invoke(); + Respond(id, new JsonObject { ["result"] = "ok" }); + break; + case "document.save": + { + var current = data.OptString("documentID") == documentID && (data.IsNull("sessionID") || data.OptString("sessionID") == sessionID); + var content = data.OptObject("payload")?.OptStringOrNull("content"); + var saved = current && content is not null && (Save?.Invoke(content) ?? false); + Respond(id, new JsonObject { ["result"] = saved ? "ok" : "failed" }); + break; + } + case "document.resources": + Respond(id, new JsonObject { ["result"] = "ok", ["documentID"] = documentID, ["resources"] = new JsonArray() }); + break; + default: + Respond(id, new JsonObject { ["result"] = "ok" }); + break; + } + } +} diff --git a/Windows/src/ScriptWidget.Studio/Editor/windows-bridge.js b/Windows/src/ScriptWidget.Studio/Editor/windows-bridge.js new file mode 100644 index 00000000..8766d05e --- /dev/null +++ b/Windows/src/ScriptWidget.Studio/Editor/windows-bridge.js @@ -0,0 +1,39 @@ +// Windows side of the Studio editor bridge. The editor bundle talks to +// WKWebViewJavascriptBridge on Apple platforms; this provides the same two +// methods on top of WebView2 web messages. It is injected before any script +// of the page runs. +(function () { + "use strict"; + var host = window.chrome && window.chrome.webview; + if (!host || window.WKWebViewJavascriptBridge) return; + var handlers = {}; + var callbacks = {}; + var sequence = 0; + var send = function (message) { host.postMessage(JSON.stringify(message)); }; + window.webkit = window.webkit || {}; + // The bundle is served from an https origin here, as in the Android app. + // It tells an embedding app from the public Web Studio site by this object + // (see isWebStudio in Editor/editorfe/src/main.js); without it the editor + // would ask to pair with a phone. + window.ScriptWidgetAndroid = window.ScriptWidgetAndroid || { host: "windows" }; + window.WKWebViewJavascriptBridge = { + registerHandler: function (name, handler) { handlers[name] = handler; }, + callHandler: function (name, data, callback) { + var id = null; + if (typeof callback === "function") { sequence += 1; id = "c" + sequence; callbacks[id] = callback; } + send({ kind: "call", name: name, data: data === undefined ? null : data, id: id }); + }, + // Called by the host with the reply to a callHandler message. + _respond: function (id, response) { + var callback = callbacks[id]; + delete callbacks[id]; + if (callback) callback(response); + }, + // Called by the host to run a handler the editor registered. + _invoke: function (name, data, replyID) { + var handler = handlers[name]; + if (!handler) { send({ kind: "reply", replyID: replyID, response: { result: "failed", message: "No handler for " + name } }); return; } + handler(data, function (response) { send({ kind: "reply", replyID: replyID, response: response === undefined ? {} : response }); }); + }, + }; +})(); diff --git a/Windows/src/ScriptWidget.Studio/MainWindow.xaml b/Windows/src/ScriptWidget.Studio/MainWindow.xaml new file mode 100644 index 00000000..ce4daa17 --- /dev/null +++ b/Windows/src/ScriptWidget.Studio/MainWindow.xaml @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Windows/src/ScriptWidget.Studio/MainWindow.xaml.cs b/Windows/src/ScriptWidget.Studio/MainWindow.xaml.cs new file mode 100644 index 00000000..2a20ad5e --- /dev/null +++ b/Windows/src/ScriptWidget.Studio/MainWindow.xaml.cs @@ -0,0 +1,174 @@ +using Microsoft.UI.Windowing; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using ScriptWidget.Core.Packages; +using ScriptWidget.Studio.Pages; +using ScriptWidget.Studio.Services; + +namespace ScriptWidget.Studio; + +/// The management window: the user's widgets, the templates, the editor and the settings. +public sealed partial class MainWindow : Window +{ + public static MainWindow Current { get; private set; } = null!; + private readonly DispatcherTimer toastTimer = new() { Interval = TimeSpan.FromSeconds(4) }; + + public MainWindow() + { + InitializeComponent(); + Current = this; + Title = "ScriptWidget"; + LibraryItem.Content = Loc.T("nav.library"); + TemplatesItem.Content = Loc.T("nav.templates"); + ComponentsItem.Content = Loc.T("nav.components"); + ApisItem.Content = Loc.T("nav.apis"); + SettingsItem.Content = Loc.T("nav.settings"); + var icon = Path.Combine(AppContext.BaseDirectory, "ScriptWidget.ico"); + if (File.Exists(icon)) AppWindow.SetIcon(icon); + AppWindow.Resize(new Windows.Graphics.SizeInt32(Scaled(1240), Scaled(820))); + toastTimer.Tick += (_, _) => { toastTimer.Stop(); Toast.IsOpen = false; }; + Closed += (_, _) => (ContentFrame.Content as EditorPage)?.SaveNow(); + Navigation.SelectedItem = LibraryItem; + // The widgets on the desktop belong to the host; start it when the window opens first. + _ = DesktopHost.EnsureRunningAsync(); + } + + private int Scaled(int points) + { + var dpi = GetDpiForWindow(WinRT.Interop.WindowNative.GetWindowHandle(this)); + return (int)Math.Round(points * (dpi == 0 ? 1.0 : dpi / 96.0)); + } + + [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern uint GetDpiForWindow(nint window); + + public nint WindowHandle => WinRT.Interop.WindowNative.GetWindowHandle(this); + + public double RasterizationScale => Root.XamlRoot?.RasterizationScale ?? 1.0; + + private void OnSelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs arguments) + { + if (arguments.SelectedItem is not NavigationViewItem item) return; + (ContentFrame.Content as EditorPage)?.SaveNow(); + switch (item.Tag as string) + { + case "library": ContentFrame.Navigate(typeof(LibraryPage)); break; + case "templates": ContentFrame.Navigate(typeof(TemplatesPage), TemplatesPage.Templates); break; + case "components": ContentFrame.Navigate(typeof(TemplatesPage), TemplatesPage.Components); break; + case "apis": ContentFrame.Navigate(typeof(ApisPage)); break; + case "settings": ContentFrame.Navigate(typeof(SettingsPage)); break; + } + } + + /// Opens the editor on an installed package. + public void OpenEditor(string packageName) + { + Navigation.SelectedItem = null; + ContentFrame.Navigate(typeof(EditorPage), packageName); + } + + public void ShowLibrary() + { + if (Navigation.SelectedItem == LibraryItem) ContentFrame.Navigate(typeof(LibraryPage)); + else Navigation.SelectedItem = LibraryItem; + } + + public void ShowTemplates() => Navigation.SelectedItem = TemplatesItem; + + public void ShowComponents() => Navigation.SelectedItem = ComponentsItem; + + public void ShowApis() => Navigation.SelectedItem = ApisItem; + + private NavigationViewItem ListItem(string mode) => mode switch { TemplatesPage.Components => ComponentsItem, TemplatesPage.Apis => ApisItem, _ => TemplatesItem }; + + /// Opens one bundled template or example. The list it came from stays selected in the sidebar. + public void ShowDetail(ScriptWidget.Core.Hosting.BundledPackage bundled) + { + var item = ListItem(TemplatesPage.ModeOf(bundled.Group)); + if (!ReferenceEquals(Navigation.SelectedItem, item)) Navigation.SelectedItem = item; + ContentFrame.Navigate(typeof(TemplateDetailPage), bundled); + } + + /// Opens a bundled template or example by name; false when there is none. + public bool ShowDetail(string name) + { + var all = AppServices.Library.Bundled(); + var found = all.FirstOrDefault(item => item.Group == "template" && item.Name.Equals(name, StringComparison.OrdinalIgnoreCase)) + ?? all.FirstOrDefault(item => item.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); + if (found is null) return false; + ShowDetail(found); + return true; + } + + /// Leaves the detail page for the list it was opened from. + public void BackFromDetail() + { + if (ContentFrame.Content is not TemplateDetailPage) return; + var list = ContentFrame.CanGoBack ? ContentFrame.BackStack[^1].SourcePageType : null; + if (list == typeof(TemplatesPage) || list == typeof(ApisPage)) ContentFrame.GoBack(); + else if (ReferenceEquals(Navigation.SelectedItem, ApisItem)) ContentFrame.Navigate(typeof(ApisPage)); + else ContentFrame.Navigate(typeof(TemplatesPage), (Navigation.SelectedItem as NavigationViewItem)?.Tag as string ?? TemplatesPage.Templates); + } + + /// Clicking the selected sidebar entry while one of its templates is open goes back to the list. + private void OnItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs arguments) + { + if (ContentFrame.Content is TemplateDetailPage && ReferenceEquals(arguments.InvokedItemContainer, Navigation.SelectedItem)) BackFromDetail(); + } + + /// The editor page when it is showing. + public EditorPage? EditorPage => ContentFrame.Content as EditorPage; + + public void ShowSettings() => Navigation.SelectedItem = SettingsItem; + + /// A short message at the bottom of the window. + public void Notify(string message, InfoBarSeverity severity = InfoBarSeverity.Success) + { + Toast.Severity = severity; + Toast.Message = message; + Toast.IsOpen = true; + toastTimer.Stop(); + toastTimer.Start(); + } + + public async Task ShowErrorAsync(string message) + { + var dialog = new ContentDialog { XamlRoot = Root.XamlRoot, Title = Loc.T("dialog.error.title"), Content = new TextBlock { Text = message, TextWrapping = TextWrapping.Wrap }, CloseButtonText = Loc.T("action.ok") }; + await dialog.ShowAsync(); + } + + /// Asks for one line of text. Returns null when cancelled. + public async Task PromptAsync(string title, string placeholder, string initial = "") + { + var box = new TextBox { Text = initial, PlaceholderText = placeholder, MaxLength = 120 }; + box.SelectAll(); + var dialog = new ContentDialog { XamlRoot = Root.XamlRoot, Title = title, Content = box, PrimaryButtonText = Loc.T("action.ok"), CloseButtonText = Loc.T("action.cancel"), DefaultButton = ContentDialogButton.Primary }; + return await dialog.ShowAsync() == ContentDialogResult.Primary && box.Text.Trim().Length > 0 ? box.Text.Trim() : null; + } + + public async Task ConfirmAsync(string title, string detail, string confirm) + { + var dialog = new ContentDialog { XamlRoot = Root.XamlRoot, Title = title, Content = new TextBlock { Text = detail, TextWrapping = TextWrapping.Wrap }, PrimaryButtonText = confirm, CloseButtonText = Loc.T("action.cancel"), DefaultButton = ContentDialogButton.Close }; + return await dialog.ShowAsync() == ContentDialogResult.Primary; + } + + /// Imports a .swt archive after the user confirmed, then opens it in the editor. + public async Task ImportArchiveAsync(string file) + { + try + { + var name = Path.GetFileNameWithoutExtension(file); + if (!await ConfirmAsync(Loc.T("dialog.import.title"), Loc.T("dialog.import.detail", name), Loc.T("dialog.import.confirm"))) return; + var installed = await Task.Run(() => + { + using var stream = File.OpenRead(file); + return AppServices.Library.ImportArchive(stream, name, AppServices.ValidatePackage); + }); + DesktopHost.LibraryChanged(); + OpenEditor(installed.Name); + } + catch (Exception error) when (error is PackageException or IOException or UnauthorizedAccessException) + { + await ShowErrorAsync(error.Message); + } + } +} diff --git a/Windows/src/ScriptWidget.Studio/Pages/ApisPage.xaml b/Windows/src/ScriptWidget.Studio/Pages/ApisPage.xaml new file mode 100644 index 00000000..85c62519 --- /dev/null +++ b/Windows/src/ScriptWidget.Studio/Pages/ApisPage.xaml @@ -0,0 +1,78 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Windows/src/ScriptWidget.Studio/Pages/LibraryPage.xaml.cs b/Windows/src/ScriptWidget.Studio/Pages/LibraryPage.xaml.cs new file mode 100644 index 00000000..1277301a --- /dev/null +++ b/Windows/src/ScriptWidget.Studio/Pages/LibraryPage.xaml.cs @@ -0,0 +1,166 @@ +using System.Collections.ObjectModel; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Navigation; +using ScriptWidget.Core.Hosting; +using ScriptWidget.Core.Packages; +using ScriptWidget.Studio.Services; +using Windows.Storage.Pickers; + +namespace ScriptWidget.Studio.Pages; + +/// The user's installed widgets. +public sealed partial class LibraryPage : Page +{ + private const string StarterSource = """ + // A widget is a script that returns JSX. Change the text and save to see it update. + $render( + + Hello, widget + Edit me in ScriptWidget + + ); + """; + + private readonly ObservableCollection cards = new(); + + public LibraryPage() + { + InitializeComponent(); + Cards.ItemsSource = cards; + } + + protected override void OnNavigatedTo(NavigationEventArgs e) => _ = ReloadAsync(); + + private async Task ReloadAsync() + { + var packages = await Task.Run(() => AppServices.Library.List()); + cards.Clear(); + foreach (var package in packages) cards.Add(new PackageCard { Title = package.DisplayName, Subtitle = package.Description, Installed = package }); + Empty.Visibility = cards.Count == 0 ? Visibility.Visible : Visibility.Collapsed; + Cards.Visibility = cards.Count == 0 ? Visibility.Collapsed : Visibility.Visible; + var counts = await DesktopHost.WidgetCountsAsync(); + foreach (var card in cards) + { + if (card.Installed is { } installed && counts.TryGetValue(installed.Installation, out var count)) card.Detail = Loc.T("library.onDesktop", count); + } + } + + private void OnContainerChanging(ListViewBase sender, ContainerContentChangingEventArgs arguments) + { + if (!arguments.InRecycleQueue && arguments.Item is PackageCard card) _ = card.RenderAsync(MainWindow.Current.RasterizationScale); + } + + private static PackageCard? Card(object sender) => (sender as FrameworkElement)?.DataContext as PackageCard; + + private void OnCardClick(object sender, ItemClickEventArgs arguments) + { + if (arguments.ClickedItem is PackageCard { Installed: { } installed }) MainWindow.Current.OpenEditor(installed.Name); + } + + private void OnEdit(object sender, RoutedEventArgs arguments) + { + if (Card(sender)?.Installed is { } installed) MainWindow.Current.OpenEditor(installed.Name); + } + + private void OnAdd(object sender, RoutedEventArgs arguments) + { + if (Card(sender)?.Installed is not { } installed) return; + var menu = new MenuFlyout(); + foreach (var family in installed.Families) + { + var item = new MenuFlyoutItem { Text = Loc.Family(family) }; + item.Click += async (_, _) => await AddToDesktopAsync(installed, family); + menu.Items.Add(item); + } + menu.ShowAt((FrameworkElement)sender); + } + + public static async Task AddToDesktopAsync(InstalledPackage installed, string family) + { + if (await DesktopHost.AddWidgetAsync(installed.Installation, family)) MainWindow.Current.Notify(Loc.T("toast.added")); + else MainWindow.Current.Notify(Loc.T("toast.hostMissing"), InfoBarSeverity.Error); + } + + private void OnMore(object sender, RoutedEventArgs arguments) + { + if (Card(sender)?.Installed is not { } installed) return; + var menu = new MenuFlyout(); + void Add(string key, Func action) + { + var item = new MenuFlyoutItem { Text = Loc.T(key) }; + item.Click += async (_, _) => await Guarded(action); + menu.Items.Add(item); + } + Add("action.duplicate", async () => { await Task.Run(() => AppServices.Library.Duplicate(installed.Name)); await ReloadAsync(); }); + Add("action.rename", async () => + { + if (await MainWindow.Current.PromptAsync(Loc.T("dialog.rename.title"), Loc.T("dialog.new.hint"), installed.Name) is not { } name || name == installed.Name) return; + await Task.Run(() => AppServices.Library.Rename(installed.Name, name)); + DesktopHost.LibraryChanged(); + await ReloadAsync(); + }); + Add("action.export", () => ExportAsync(installed)); + Add("action.openFolder", () => { OpenFolder(installed.Directory); return Task.CompletedTask; }); + menu.Items.Add(new MenuFlyoutSeparator()); + Add("action.delete", async () => + { + if (!await MainWindow.Current.ConfirmAsync(Loc.T("dialog.delete.title", installed.DisplayName), Loc.T("dialog.delete.detail"), Loc.T("action.delete"))) return; + await Task.Run(() => AppServices.Library.Delete(installed.Name)); + // The host shows a "not found" card for widgets of a removed package; take them off the desktop. + var widgets = await DesktopHost.SendAsync(new System.Text.Json.Nodes.JsonObject { ["command"] = "list" }); + foreach (var widget in ScriptWidget.Core.Json.OptArray(widgets, "widgets") ?? new System.Text.Json.Nodes.JsonArray()) + { + if (widget is System.Text.Json.Nodes.JsonObject item && ScriptWidget.Core.Json.OptString(item, "package") == installed.Installation) + await DesktopHost.SendAsync(new System.Text.Json.Nodes.JsonObject { ["command"] = "remove", ["id"] = ScriptWidget.Core.Json.OptString(item, "id") }); + } + await ReloadAsync(); + }); + menu.ShowAt((FrameworkElement)sender); + } + + public static void OpenFolder(string directory) + { + try { System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("explorer.exe") { ArgumentList = { directory }, UseShellExecute = false })?.Dispose(); } + catch (Exception error) when (error is System.ComponentModel.Win32Exception or InvalidOperationException) { } + } + + /// Runs an action and shows a dialog when the package store refuses it. + private static async Task Guarded(Func action) + { + try { await action(); } + catch (Exception error) when (error is PackageException or IOException or UnauthorizedAccessException) { await MainWindow.Current.ShowErrorAsync(error.Message); } + } + + public static async Task ExportAsync(InstalledPackage installed) + { + var picker = new FileSavePicker { SuggestedFileName = installed.Name, SuggestedStartLocation = PickerLocationId.DocumentsLibrary }; + picker.FileTypeChoices.Add("ScriptWidget", [".swt"]); + WinRT.Interop.InitializeWithWindow.Initialize(picker, MainWindow.Current.WindowHandle); + if (await picker.PickSaveFileAsync() is not { } file) return; + await Task.Run(() => + { + using var stream = File.Create(file.Path); + AppServices.Library.ExportArchive(installed.Name, stream); + }); + MainWindow.Current.Notify(Loc.T("toast.exported", file.Name)); + } + + private async void OnNew(object sender, RoutedEventArgs arguments) => await Guarded(async () => + { + if (await MainWindow.Current.PromptAsync(Loc.T("dialog.new.title"), Loc.T("dialog.new.hint"), Loc.T("dialog.new.default")) is not { } name) return; + var created = await Task.Run(() => AppServices.Library.Create(name, StarterSource)); + MainWindow.Current.OpenEditor(created.Name); + }); + + private async void OnImport(object sender, RoutedEventArgs arguments) + { + var picker = new FileOpenPicker { SuggestedStartLocation = PickerLocationId.Downloads }; + picker.FileTypeFilter.Add(".swt"); + picker.FileTypeFilter.Add(".zip"); + WinRT.Interop.InitializeWithWindow.Initialize(picker, MainWindow.Current.WindowHandle); + if (await picker.PickSingleFileAsync() is { } file) await MainWindow.Current.ImportArchiveAsync(file.Path); + } + + private void OnBrowseTemplates(object sender, RoutedEventArgs arguments) => MainWindow.Current.ShowTemplates(); +} diff --git a/Windows/src/ScriptWidget.Studio/Pages/PackageCard.cs b/Windows/src/ScriptWidget.Studio/Pages/PackageCard.cs new file mode 100644 index 00000000..8b3852ad --- /dev/null +++ b/Windows/src/ScriptWidget.Studio/Pages/PackageCard.cs @@ -0,0 +1,88 @@ +using System.ComponentModel; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Media; +using ScriptWidget.Core.Hosting; +using ScriptWidget.Core.Packages; +using ScriptWidget.Core.Runtime; +using ScriptWidget.Studio.Services; + +namespace ScriptWidget.Studio.Pages; + +/// One widget in a grid: an installed package or a bundled template, with its preview. +public sealed class PackageCard : INotifyPropertyChanged +{ + private ImageSource? image; + private string detail = ""; + private string output = ""; + private bool rendering; + + public required string Title { get; init; } + public string Subtitle { get; init; } = ""; + /// Set for an installed package. + public InstalledPackage? Installed { get; init; } + /// Set for a bundled template. + public BundledPackage? Bundled { get; init; } + + public ImageSource? Image + { + get => image; + private set { image = value; Changed(nameof(Image)); Changed(nameof(PlaceholderVisibility)); } + } + + /// A line under the title, such as how many widgets of it are on the desktop. + public string Detail + { + get => detail; + set { detail = value; Changed(nameof(Detail)); Changed(nameof(DetailVisibility)); } + } + + public Visibility DetailVisibility => detail.Length > 0 ? Visibility.Visible : Visibility.Collapsed; + public Visibility SubtitleVisibility => Subtitle.Length > 0 ? Visibility.Visible : Visibility.Collapsed; + /// What an example that draws nothing printed; shown in place of a picture. + public string Output + { + get => output; + private set { output = value; Changed(nameof(Output)); Changed(nameof(OutputVisibility)); Changed(nameof(PlaceholderVisibility)); } + } + + public string OutputLabel => Loc.T("editor.logs"); + public Visibility OutputVisibility => output.Length > 0 ? Visibility.Visible : Visibility.Collapsed; + public Visibility PlaceholderVisibility => image is null && output.Length == 0 ? Visibility.Visible : Visibility.Collapsed; + public Visibility InstalledVisibility => Installed is not null ? Visibility.Visible : Visibility.Collapsed; + public Visibility BundledVisibility => Bundled is not null ? Visibility.Visible : Visibility.Collapsed; + + public string EditLabel => Loc.T("action.edit"); + public string AddLabel => Loc.T("action.addToDesktop"); + public string UseLabel => Loc.T("action.use"); + public string MoreLabel => Loc.T("action.more"); + + public bool HasPreview => image is not null || output.Length > 0 || rendering; + + /// Draws the medium-size preview once. Safe to call again; later calls do nothing. + public async Task RenderAsync(double scale) + { + if (HasPreview) return; + rendering = true; + try + { + IPackageFiles files; + string id; + string? build = null; + if (Installed is { } installed) { files = installed.Files; id = installed.Installation; build = installed.BuildDirectory; } + else if (Bundled is { } bundled) { files = new DiskPackageFiles(bundled.Directory); id = "template:" + bundled.Group + "/" + bundled.Name; } + else return; + var result = await Preview.RenderAsync(files, id, "medium", Preview.SystemIsDark, "", scale, build); + if (Preview.DrawsNothing(result.Execution)) + { + var lines = Preview.LogLines(result.Execution); + Output = lines.Count > 0 ? string.Join("\n", lines) : Loc.T("detail.noOutput"); + } + else Image = result.Image; + } + catch (Exception error) when (error is not OutOfMemoryException) { /* the placeholder stays */ } + finally { rendering = false; } + } + + public event PropertyChangedEventHandler? PropertyChanged; + private void Changed(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); +} diff --git a/Windows/src/ScriptWidget.Studio/Pages/SettingsPage.xaml b/Windows/src/ScriptWidget.Studio/Pages/SettingsPage.xaml new file mode 100644 index 00000000..cde861c5 --- /dev/null +++ b/Windows/src/ScriptWidget.Studio/Pages/SettingsPage.xaml @@ -0,0 +1,76 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +