From 91049abe601dceda19943f70e66d5ec5e30be517 Mon Sep 17 00:00:00 2001
From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com>
Date: Mon, 2 Nov 2026 15:33:46 +0100
Subject: [PATCH 01/18] Make and downloads work in the
embedded frontends (#451)
WebKitGTK saves downloads to the user's download folder and answers the file
chooser through an injection point; WKWebView implements the open panel and
WKDownload; CEF gets the same injection points. A blob: URL of an admitted
origin now passes the navigation policy. Scenarios capture their final state
and CI assembles a per-platform qualification report.
---
.github/workflows/BuildAndTest.yml | 54 ++++++
cmake/WebKitGTKSupport.cmake | 2 +-
docs/CHANGELOG.md | 7 +
docs/FRONTENDS.md | 28 +++
docs/adr/ADR-020-services-systeme.md | 4 +
include/webfront/ContentPolicy.hpp | 18 ++
include/webfront/frontend/CEF.hpp | 55 +++++-
include/webfront/frontend/Downloads.hpp | 26 +++
include/webfront/frontend/WKWebView.hpp | 142 ++++++++++++++-
include/webfront/frontend/WebKitGTK.hpp | 115 ++++++++++++
include/webfront/frontend/WebKitGTKPolicy.hpp | 2 +-
test/tools/Qualification.py | 158 ++++++++++++++++
test/unit/ContentPolicyTests.cpp | 8 +
test/web/CEFFileServices.cpp | 169 ++++++++++++++++++
test/web/CMakeLists.txt | 16 ++
test/web/QualificationEvidence.hpp | 46 +++++
test/web/WebKitGTKLifecycle.cpp | 159 +++++++++++++++-
test/web/file-services.html | 30 ++++
.../jasmine-consumer/file-services.spec.js | 55 ++++++
19 files changed, 1089 insertions(+), 5 deletions(-)
create mode 100644 include/webfront/frontend/Downloads.hpp
create mode 100644 test/tools/Qualification.py
create mode 100644 test/web/CEFFileServices.cpp
create mode 100644 test/web/QualificationEvidence.hpp
create mode 100644 test/web/file-services.html
create mode 100644 test/web/jasmine-consumer/file-services.spec.js
diff --git a/.github/workflows/BuildAndTest.yml b/.github/workflows/BuildAndTest.yml
index 73981f0b..a9276b3b 100644
--- a/.github/workflows/BuildAndTest.yml
+++ b/.github/workflows/BuildAndTest.yml
@@ -181,6 +181,10 @@ jobs:
Write-Host "WebView2 runtime $version at $($executable.FullName)"
}
# The fast tier (docs/TEST_TIERS.md): unit tests only, so a plain regression is reported before the browser suite starts.
+ # The file-service scenarios write their captures here (#451); see test/tools/Qualification.py.
+ - name: Choose the qualification evidence folder
+ shell: bash
+ run: echo "WEBFRONT_QUALIFICATION_DIR=$GITHUB_WORKSPACE/qualification-evidence" >> "$GITHUB_ENV"
- name: Fast tests
shell: bash
run: ctest --test-dir build -C ${{ matrix.build_type }} -L fast --parallel 4 --output-on-failure --no-tests=error
@@ -196,6 +200,17 @@ jobs:
-DREPEAT=${{ (github.event_name == 'schedule' || (github.event_name == 'pull_request' && github.base_ref == 'master') || (github.event_name == 'workflow_dispatch' && inputs.repeat_desktop_tests)) && '3' || '1' }}
-DLABEL_EXCLUDE=${{ (github.event_name == 'schedule' || (github.event_name == 'pull_request' && github.base_ref == 'master') || (github.event_name == 'workflow_dispatch' && inputs.repeat_desktop_tests)) && 'none' || 'portability' }}
-P test/cmake/RunTestSuite.cmake
+ - name: Collect the qualification evidence
+ if: always()
+ shell: bash
+ run: python3 test/tools/Qualification.py collect --build build --evidence qualification-evidence --platform ${{ matrix.platform }} --output qualification/${{ matrix.platform }}
+ - name: Retain the qualification evidence
+ if: always()
+ uses: actions/upload-artifact@v4
+ with:
+ name: qualification-${{ matrix.platform }}
+ path: qualification/${{ matrix.platform }}/
+ if-no-files-found: ignore
- name: Analyze CTest timings
if: always()
shell: bash
@@ -496,6 +511,8 @@ jobs:
sudo ip6tables -A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo ip6tables -A OUTPUT ! -o lo -j REJECT
- name: Run Jasmine bridge test
+ env:
+ WEBFRONT_QUALIFICATION_DIR: ${{ github.workspace }}/qualification-evidence
run: >-
ctest --test-dir ${{github.workspace}}/build-cef -L web-integration --output-on-failure --no-tests=error --parallel 4
--output-junit "${{ github.workspace }}/build-cef/test-results.xml"
@@ -504,6 +521,16 @@ jobs:
run: |
sudo iptables -F OUTPUT
sudo ip6tables -F OUTPUT
+ - name: Collect the qualification evidence
+ if: always()
+ run: python3 test/tools/Qualification.py collect --build build-cef --evidence qualification-evidence --platform linux-cef-${{ matrix.compiler }} --output qualification/linux-cef-${{ matrix.compiler }}
+ - name: Retain the qualification evidence
+ if: always()
+ uses: actions/upload-artifact@v4
+ with:
+ name: qualification-linux-cef-${{ matrix.compiler }}
+ path: qualification/linux-cef-${{ matrix.compiler }}/
+ if-no-files-found: ignore
- name: Compiler cache statistics
if: always()
shell: bash
@@ -517,3 +544,30 @@ jobs:
build-cef/test-results.xml
build-cef/Testing/Temporary/LastTest.log
if-no-files-found: ignore
+
+ # One document for every platform, built from the captures the jobs above retained (#451). It runs even when a job failed:
+ # a failing or missing platform is then written into the report instead of being left out.
+ qualification-report:
+ needs: [desktop, desktop-native, web-integration]
+ if: ${{ !cancelled() }}
+ runs-on: ubuntu-24.04
+ timeout-minutes: 10
+ steps:
+ - uses: actions/checkout@v7
+ - uses: actions/download-artifact@v4
+ with:
+ pattern: qualification-*
+ path: qualification-input
+ - name: Assemble the qualification document
+ shell: bash
+ run: |
+ # Each artifact holds one platform's folder; give it that platform's name.
+ for directory in qualification-input/qualification-*; do
+ [ -d "$directory" ] && mv "$directory" "qualification-input/${directory#qualification-input/qualification-}"
+ done
+ python3 test/tools/Qualification.py report --input qualification-input --output qualification-report/QUALIFICATION.md
+ cat qualification-report/QUALIFICATION.md >> "$GITHUB_STEP_SUMMARY"
+ - uses: actions/upload-artifact@v4
+ with:
+ name: qualification-report
+ path: qualification-report/
diff --git a/cmake/WebKitGTKSupport.cmake b/cmake/WebKitGTKSupport.cmake
index 0d0d7385..c73c604c 100644
--- a/cmake/WebKitGTKSupport.cmake
+++ b/cmake/WebKitGTKSupport.cmake
@@ -25,7 +25,7 @@ function(webfront_add_webkitgtk_tests)
target_link_libraries(webkitgtk_lifecycle PRIVATE WebFront_test_hardening WebFront WebFront_warnings WebFront_options)
add_dependencies(webtest webkitgtk_lifecycle)
webfront_find_xvfb(display_runner)
- foreach(mode no-display worker-thread native-close repeated-show http-error pre-run-failure profile-permissions window-title)
+ foreach(mode no-display worker-thread native-close repeated-show http-error pre-run-failure profile-permissions window-title download choose-file cancel-choose)
if(mode MATCHES "^(no-display|worker-thread|pre-run-failure|profile-permissions)$")
add_test(NAME WebKitGTKAdapter-${mode} COMMAND webkitgtk_lifecycle ${mode})
elseif(display_runner)
diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index 44cb6d35..13641d83 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -2,6 +2,13 @@
## Unreleased
+- Embedded frontends: `` and downloads (#451, ADR-020 rule 6). WebKitGTK saves downloads to the user's
+ download folder (name reduced to its last component, never overwritten); WKWebView gets `runOpenPanelWithParameters` and
+ `WKDownload` (not yet run on a Mac); CEF and WebView2 use the engine's own handling. A `blob:` URL of an admitted origin
+ now passes the navigation policy, so a generated Blob can be downloaded. Test injection points answer the chooser and the
+ download without a dialog; CI builds a per-platform qualification report with window captures
+ (`test/tools/Qualification.py`, `docs/FRONTENDS.md`).
+
- Developer environment: the distribution is served from `https://dist.webfront.fr/` (#478, ADR-021). The sources advance to 0.6.5, the
first version published under that address; `site.mjs` writes the `CNAME` file of the Pages branch and `wf run pin`
checks the pinned archive where it is served now. Libraries and environments up to 0.6.4 pin the former
diff --git a/docs/FRONTENDS.md b/docs/FRONTENDS.md
index 718e1135..77a3e550 100644
--- a/docs/FRONTENDS.md
+++ b/docs/FRONTENDS.md
@@ -45,6 +45,34 @@ startup failures before any window is created. Coordination waits have a 30-seco
`test/unit/WebFrontTests.cpp` also checks initialization once per frontend type, retry after failed
initialization, frontend selection and unavailable-engine diagnostics. These tests need no GUI.
+## File chooser and downloads
+
+`` (single, multiple, `accept`, `webkitdirectory`) and downloads (``, generated `blob:` URLs)
+work in every embedded frontend with the engine's own dialog, without a WebFront API (ADR-020, rule 6). A `blob:` URL of an
+admitted origin passes the navigation policy (`webfront::detail::blobOriginUrl`); a `blob:` URL of any other origin does not.
+
+| Frontend | Chooser | Download | Test injection |
+| --- | --- | --- | --- |
+| WebKitGTK | default `run-file-chooser` dialog (portal under Flatpak) | `download-started` / `decide-destination`: the user's XDG download folder, the page's name reduced to its last component, never overwriting (`name (1).ext`) | `Session::chooseFiles`, `downloadDirectory`, `downloadFinished` |
+| CEF (Chrome runtime) | the runtime's dialog | the runtime's download handling | `SimpleCEFClient::chooseFiles` (`CefDialogHandler::OnFileDialog`), `downloadDirectory`, `downloadFinished`. The runtime itself turns a suggested name `../x` into `_x` |
+| WKWebView | `runOpenPanelWithParameters` shows an `NSOpenPanel` sheet of the window; only frames admitted by the policy | `WKDownload` (macOS 11.3+): a save panel proposes the name | `WKWebViewOptions::chooseFiles`, `downloadDirectory`, `downloadFinished` |
+| WebView2 | the engine's native chooser | the engine's native download UI | none yet |
+
+WKWebView is written but has not been run on a Mac by its author: the qualification report below is the first check. The
+engines require a user gesture to open a chooser, so the native scenarios click with a real pointer event (XTest under
+Xvfb for WebKitGTK) or a `Runtime.evaluate` with `userGesture` (CEF).
+
+### Qualification report
+
+`ConsumerJasmine-file-services` (all engines) checks that files reach the page as `File` objects, that `accept` and
+`webkitdirectory` are honoured, and that offering a generated Blob leaves the page and the bridge working.
+`WebKitGTKAdapter-download|choose-file|cancel-choose` and `CEFFileServices-download|choose|cancel` drive the dialog and
+the download through the injection points. Every scenario captures its final state with WebFront's own window capture
+(`QualificationEvidence.hpp`, `ConsumerJasmine` captures). In CI each platform job retains a `qualification-`
+artifact (`test/tools/Qualification.py collect`) and the `qualification-report` job assembles them into
+`QUALIFICATION.md` with the images, for Linux (WebKitGTK; CEF on pull requests into `master` and on the weekly run),
+Windows (WebView2) and macOS (WKWebView). A platform that did not report is written as missing.
+
## Adding a frontend
1. Implement the type in `include/webfront/frontend/` following the contract above and include it from `Frontend.hpp`. Keep platform headers behind the option's macro.
diff --git a/docs/adr/ADR-020-services-systeme.md b/docs/adr/ADR-020-services-systeme.md
index 745ebcd3..125a6d5d 100644
--- a/docs/adr/ADR-020-services-systeme.md
+++ b/docs/adr/ADR-020-services-systeme.md
@@ -352,6 +352,10 @@ Le code vérifié le 2026-10-31 ne permet pas encore d'implémenter ces règles.
| 4 | [#449](https://github.com/ambroise-leclerc/WebFront/issues/449) Stockage modifiable et livraison des services | [#460](https://github.com/ambroise-leclerc/WebFront/issues/460) stockage sous racines déclarées, confiné pendant la résolution (révision de l'ADR-002) ; [#461](https://github.com/ambroise-leclerc/WebFront/issues/461) paquet `@webfront/services` | sélecteur HTML et écritures (règles 2 à 4) |
| 5 | [#450](https://github.com/ambroise-leclerc/WebFront/issues/450) Intégration native des frontends | [#462](https://github.com/ambroise-leclerc/WebFront/issues/462) exécuteur du thread d'interface, y compris le navigateur système ; [#463](https://github.com/ambroise-leclerc/WebFront/issues/463) fenêtre native et correspondance avec les connexions | backend natif seulement |
+État de l'étape 1 (#451) : sélecteur et téléchargements qualifiés sous WebKitGTK et CEF (Linux), implémentés sous
+WKWebView, laissés au comportement natif sous WebView2 ; le rapport de qualification de la CI (`docs/FRONTENDS.md`) documente
+chaque moteur. #452 reste à faire.
+
Après l'étape 4, le premier service (sélecteur HTML, poignées, import et export côté client) fera l'objet de sa propre
épique ; les dialogues natifs suivront l'étape 5. `PUT`/`POST` ne sont pas des préalables : les écritures passent par
des tableaux typés en morceaux ; leur ajout au serveur, utile aux très gros envois en flux et aux formulaires sans
diff --git a/include/webfront/ContentPolicy.hpp b/include/webfront/ContentPolicy.hpp
index c9822843..edf263c2 100644
--- a/include/webfront/ContentPolicy.hpp
+++ b/include/webfront/ContentPolicy.hpp
@@ -1,6 +1,7 @@
#pragma once
#include
+#include
#include
#include
#include
@@ -130,6 +131,21 @@ inline std::optional contentOrigin(std::string_view url, bool origi
return serializedContentOrigin(parts->scheme, authority->host, *port, defaultPort);
}
+/**
+ * @brief The URL a `blob:` URL was minted for (`blob:http://host:port/id` gives `http://host:port/id`), or empty.
+ *
+ * A page downloads a generated `Blob` by navigating to such a URL; its content always belongs to the page's own origin,
+ * so the origin it names is the one the navigation policy must judge.
+ */
+inline std::string_view blobOriginUrl(std::string_view url) {
+ constexpr std::string_view scheme = "blob:";
+ if (url.size() <= scheme.size() || !std::ranges::equal(url.substr(0, scheme.size()), scheme, [](char a, char b) {
+ return std::tolower(static_cast(a)) == b;
+ }))
+ return {};
+ return url.substr(scheme.size());
+}
+
/** @brief Validated snapshot shared by the HTTP and embedded-frontend policy hooks. */
class BoundContentPolicy {
public:
@@ -147,6 +163,8 @@ class BoundContentPolicy {
}
bool allowsNavigation(std::string_view url) const {
+ if (const auto inner = blobOriginUrl(url); !inner.empty())
+ url = inner; // A Blob of an admitted origin (a generated download) is that origin's own content.
const auto origin = contentOrigin(url);
return origin && (policy.allowAnyNavigation || *origin == applicationOrigin || contains(policy.navigationAllowedOrigins, *origin));
}
diff --git a/include/webfront/frontend/CEF.hpp b/include/webfront/frontend/CEF.hpp
index ad5f4ed0..817843e7 100644
--- a/include/webfront/frontend/CEF.hpp
+++ b/include/webfront/frontend/CEF.hpp
@@ -6,12 +6,14 @@
#include "CEFPaths.hpp"
#include "Capture.hpp"
#include "DefaultBrowser.hpp"
+#include "Downloads.hpp"
#include "Url.hpp"
#include "Window.hpp"
#include
#include
#include
+#include
#include
#include
#include
@@ -493,7 +495,9 @@ class SimpleCEFClient : public CefClient,
public CefLoadHandler,
public CefRequestHandler,
public CefContextMenuHandler,
- public CefCommandHandler {
+ public CefCommandHandler,
+ public CefDownloadHandler,
+ public CefDialogHandler {
public:
explicit SimpleCEFClient(webfront::detail::BoundContentPolicy policy = webfront::detail::BoundContentPolicy{}, browser::Launcher launch = browser::launch)
: contentPolicy(std::move(policy)), popupLauncher(launch) { }
@@ -512,6 +516,55 @@ class SimpleCEFClient : public CefClient,
return this;
}
+ /// The Chrome runtime already shows its own file dialog and saves downloads; these two hooks only exist for tests.
+ /// Test injection: answers without a dialog (an empty list cancels). Unset keeps the engine's dialog.
+ std::function()> chooseFiles;
+ /// Test injection: a non-empty folder receives downloads without a "save as" dialog.
+ std::filesystem::path downloadDirectory;
+ /// Test injection: called with the full path of each completed download.
+ std::function downloadFinished;
+
+ CefRefPtr GetDownloadHandler() override {
+ return this;
+ }
+ CefRefPtr GetDialogHandler() override {
+ return this;
+ }
+ bool OnFileDialog(CefRefPtr,
+ FileDialogMode,
+ const CefString&,
+ const CefString&,
+ const std::vector&,
+ const std::vector&,
+ const std::vector&,
+ CefRefPtr callback) override {
+ if (!chooseFiles)
+ return false;
+ std::vector files;
+ try {
+ for (const auto& file : chooseFiles())
+ files.emplace_back(file);
+ } catch (...) {
+ files.clear();
+ }
+ if (files.empty())
+ callback->Cancel();
+ else
+ callback->Continue(files);
+ return true;
+ }
+ bool OnBeforeDownload(CefRefPtr, CefRefPtr, const CefString& suggested, CefRefPtr callback) override {
+ if (downloadDirectory.empty())
+ return false;
+ const auto result = frontend::detail::uniqueDownloadPath(downloadDirectory, suggested.ToString());
+ callback->Continue(result.string(), false);
+ return true;
+ }
+ void OnDownloadUpdated(CefRefPtr, CefRefPtr item, CefRefPtr) override {
+ if (item->IsComplete() && downloadFinished)
+ downloadFinished(item->GetFullPath().ToString());
+ }
+
bool OnBeforeBrowse(CefRefPtr, CefRefPtr frame, CefRefPtr request, bool, bool) override {
// Frame navigations and subresources remain outside this top-level navigation policy.
return frame->IsMain() && !contentPolicy.allowsNavigation(request->GetURL().ToString());
diff --git a/include/webfront/frontend/Downloads.hpp b/include/webfront/frontend/Downloads.hpp
new file mode 100644
index 00000000..156c6bf1
--- /dev/null
+++ b/include/webfront/frontend/Downloads.hpp
@@ -0,0 +1,26 @@
+#pragma once
+
+#include
+#include
+#include
+
+namespace webfront::frontend::detail {
+
+/**
+ * @brief A path in `folder` that does not exist yet, for a download whose name the page suggested.
+ *
+ * Only the final component of the suggested name is kept (a page can neither choose a folder nor climb out of it), an
+ * empty name becomes "download", and an existing file is kept: "name", then "name (1).ext", "name (2).ext", and so on.
+ */
+inline std::filesystem::path uniqueDownloadPath(const std::filesystem::path& folder, std::string_view suggested) {
+ auto name = std::filesystem::path(std::string(suggested)).filename();
+ if (name.empty() || name == "." || name == "..")
+ name = "download";
+ auto result = folder / name;
+ std::error_code error;
+ for (int index = 1; std::filesystem::exists(result, error) && index < 10'000; ++index)
+ result = folder / (name.stem().string() + " (" + std::to_string(index) + ")" + name.extension().string());
+ return result;
+}
+
+} // namespace webfront::frontend::detail
diff --git a/include/webfront/frontend/WKWebView.hpp b/include/webfront/frontend/WKWebView.hpp
index ba822366..cb489f4d 100644
--- a/include/webfront/frontend/WKWebView.hpp
+++ b/include/webfront/frontend/WKWebView.hpp
@@ -3,14 +3,20 @@
#include "../ContentPolicy.hpp"
#include "Action.hpp"
#include "Capture.hpp"
+#include "Downloads.hpp"
#include "Url.hpp"
#include "Window.hpp"
#include
#include
#include
+#include
+#include
+#include