From e197cc6ab4fab829c1487d4cc45d46022311140a Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Sun, 1 Nov 2026 16:28:07 +0100 Subject: [PATCH 1/5] Declare the embedded frontend without consumer-side macros (#467) Add webfront::embeddedFrontendAvailable and webfront::EmbeddedWebFront, defined in every configuration. Without an engine, constructing EmbeddedWebFront throws std::runtime_error. --- docs/CHANGELOG.md | 3 +++ docs/FRONTENDS.md | 17 +++++++++++++++++ include/webfront/WebFront.hpp | 35 +++++++++++++++++++++++++++++++++++ test/unit/WebFrontTests.cpp | 18 ++++++++++++++++++ 4 files changed, 73 insertions(+) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 040c1c97..809ee7a7 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +- Declare the embedded engine without consumer-side macros (#467): `webfront::embeddedFrontendAvailable` (`constexpr bool`) and + `webfront::EmbeddedWebFront` (the native-window application type, whose construction throws `std::runtime_error` when the + build has no engine) exist in every configuration. See `docs/FRONTENDS.md`. - Capture the web view as PNG images (#468): `webFrontCapture.screenshot(name)` and `webFrontCapture.record(name, {seconds, framesPerSecond})` (also `startRecording`/`stopRecording`) in the specs of `webfront_add_jasmine_test` write `webfront-jasmine//captures/.png` and numbered `/0001.png`... for an external encoder such as ffmpeg. diff --git a/docs/FRONTENDS.md b/docs/FRONTENDS.md index 3bbf9fce..676d4f1c 100644 --- a/docs/FRONTENDS.md +++ b/docs/FRONTENDS.md @@ -102,6 +102,23 @@ deadline and data-store/inspector/menu tests. See [WKWebView verification](WKWEB ## Native window with browser fallback +Without a preprocessor test, `webfront::embeddedFrontendAvailable` (`inline constexpr bool`, true when a native engine is +compiled in; CEF is not counted) and `webfront::EmbeddedWebFront` (always defined) tell an application what the build holds. +Without an engine, constructing `EmbeddedWebFront` throws `std::runtime_error`, so the fallback is an ordinary `try`/`catch`: + +```cpp +if constexpr (webfront::embeddedFrontendAvailable) { + try { + webfront::EmbeddedWebFront window{config}; + return serve(window); + } catch (const std::exception&) { /* fall back to the browser */ } +} +webfront::WebFront web{config}; +return serve(web); +``` + +`if constexpr` keeps the absent branch from being instantiated when `serve` needs engine-specific members. + `frontend::nativeWebViewAvailable` is `true` when this platform's native engine is compiled in. It says nothing about the session. `frontend::NativeOrBrowserFrontend` decides at run time: its `initialize()` tries the native engine and, if that throws (engine not compiled in, no `DISPLAY`/`WAYLAND_DISPLAY`, diff --git a/include/webfront/WebFront.hpp b/include/webfront/WebFront.hpp index 607a3572..7e592ff2 100644 --- a/include/webfront/WebFront.hpp +++ b/include/webfront/WebFront.hpp @@ -952,6 +952,41 @@ template ; using UI = WebFront::UI; +/** + * @brief True when this platform's embedded web view engine is compiled in (WEBFRONT_EMBED_WEBKITGTK, WEBFRONT_EMBED_WEBVIEW2 + * or WEBFRONT_EMBED_WKWEBVIEW). Always defined, so applications need no preprocessor test. CEF is not counted: it is selected + * explicitly with WebFrontWithFrontend. Like frontend::nativeWebViewAvailable, it says nothing about the + * session (a missing display still fails at run time). + */ +inline constexpr bool embeddedFrontendAvailable = frontend::nativeWebViewAvailable; + +namespace detail { +/// Stands in for the native engine where none is compiled in: constructing the application throws std::runtime_error. +struct NoEmbeddedFrontend { + static constexpr frontend::Action action{frontend::Action::closeServerAfterOpen}; + + static void initialize() { + throw std::runtime_error("no embedded web view engine in this build: configure with -DWEBFRONT_EMBED_WEBKITGTK=ON (Linux), " + "-DWEBFRONT_EMBED_WEBVIEW2=ON (Windows) or -DWEBFRONT_EMBED_WKWEBVIEW=ON (macOS)"); + } + static void open(std::string_view, std::string_view) { + initialize(); + } +}; + +#if defined(_WIN32) || defined(__APPLE__) || defined(__linux__) +using EmbeddedFrontendType = std::conditional_t; +#else +using EmbeddedFrontendType = NoEmbeddedFrontend; +#endif +} // namespace detail + +/** + * @brief The application type with the embedded web view window. Always defined: without an engine in the build, constructing it + * throws std::runtime_error, so falling back to the system browser is an ordinary try/catch. + */ +using EmbeddedWebFront = WebFrontWithFrontend; + } // namespace webfront // Optional templates instantiate only when the developer/native facade is selected. diff --git a/test/unit/WebFrontTests.cpp b/test/unit/WebFrontTests.cpp index cafb5048..0bd5565c 100644 --- a/test/unit/WebFrontTests.cpp +++ b/test/unit/WebFrontTests.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -487,6 +488,23 @@ SCENARIO("The native engine availability is a constant") { #endif } +SCENARIO("The embedded frontend is declared in every configuration") { + STATIC_REQUIRE(std::is_same_v); + STATIC_REQUIRE(embeddedFrontendAvailable == frontend::nativeWebViewAvailable); + STATIC_REQUIRE(std::is_class_v); + STATIC_REQUIRE(std::is_same_v>); + STATIC_REQUIRE(frontend::FrontendType); +#if defined(_WIN32) || defined(__APPLE__) || defined(__linux__) + STATIC_REQUIRE(std::is_same_v == embeddedFrontendAvailable); +#endif + if constexpr (!embeddedFrontendAvailable) { + THEN("constructing it throws std::runtime_error naming the missing engine") { + REQUIRE_THROWS_WITH(EmbeddedWebFront{}, Catch::Matchers::ContainsSubstring("no embedded web view engine")); + REQUIRE_THROWS_AS(EmbeddedWebFront{}, std::runtime_error); + } + } +} + SCENARIO("The frontend contract: a failing open() stops the server whatever its action") { GIVEN("a frontend that only launches an external program and fails to do so") { using App = BasicWFWithFrontend; From 0a9ec883ff2a7314c693647fa8f4e55c03b3d065 Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Sun, 1 Nov 2026 16:58:23 +0100 Subject: [PATCH 2/5] Count CEF as an embedded engine in embeddedFrontendAvailable and EmbeddedWebFront (#467) --- docs/CHANGELOG.md | 7 ++++--- docs/FRONTENDS.md | 4 ++-- include/webfront/WebFront.hpp | 26 +++++++++++++++++--------- test/unit/WebFrontTests.cpp | 7 +++++++ 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 809ee7a7..bae7bb40 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -2,9 +2,10 @@ ## Unreleased -- Declare the embedded engine without consumer-side macros (#467): `webfront::embeddedFrontendAvailable` (`constexpr bool`) and - `webfront::EmbeddedWebFront` (the native-window application type, whose construction throws `std::runtime_error` when the - build has no engine) exist in every configuration. See `docs/FRONTENDS.md`. +- Declare the embedded engine without consumer-side macros (#467): `webfront::embeddedFrontendAvailable` (`constexpr bool`, true + when a native web view or CEF is compiled in) and `webfront::EmbeddedWebFront` (the application type with the native web view, + or with CEF when only CEF is built) exist in every configuration; constructing `EmbeddedWebFront` throws + `std::runtime_error` when the build has no engine. See `docs/FRONTENDS.md`. - Capture the web view as PNG images (#468): `webFrontCapture.screenshot(name)` and `webFrontCapture.record(name, {seconds, framesPerSecond})` (also `startRecording`/`stopRecording`) in the specs of `webfront_add_jasmine_test` write `webfront-jasmine//captures/.png` and numbered `/0001.png`... for an external encoder such as ffmpeg. diff --git a/docs/FRONTENDS.md b/docs/FRONTENDS.md index 676d4f1c..0cdd742e 100644 --- a/docs/FRONTENDS.md +++ b/docs/FRONTENDS.md @@ -102,8 +102,8 @@ deadline and data-store/inspector/menu tests. See [WKWebView verification](WKWEB ## Native window with browser fallback -Without a preprocessor test, `webfront::embeddedFrontendAvailable` (`inline constexpr bool`, true when a native engine is -compiled in; CEF is not counted) and `webfront::EmbeddedWebFront` (always defined) tell an application what the build holds. +Without a preprocessor test, `webfront::embeddedFrontendAvailable` (`inline constexpr bool`, true when a native engine or CEF is +compiled in) and `webfront::EmbeddedWebFront` (always defined; the native web view when compiled in, otherwise CEF) tell an application what the build holds. Without an engine, constructing `EmbeddedWebFront` throws `std::runtime_error`, so the fallback is an ordinary `try`/`catch`: ```cpp diff --git a/include/webfront/WebFront.hpp b/include/webfront/WebFront.hpp index 7e592ff2..97882628 100644 --- a/include/webfront/WebFront.hpp +++ b/include/webfront/WebFront.hpp @@ -952,22 +952,28 @@ template ; using UI = WebFront::UI; +#ifdef WEBFRONT_EMBED_CEF +inline constexpr bool cefAvailable = true; +#else +inline constexpr bool cefAvailable = false; +#endif + /** - * @brief True when this platform's embedded web view engine is compiled in (WEBFRONT_EMBED_WEBKITGTK, WEBFRONT_EMBED_WEBVIEW2 - * or WEBFRONT_EMBED_WKWEBVIEW). Always defined, so applications need no preprocessor test. CEF is not counted: it is selected - * explicitly with WebFrontWithFrontend. Like frontend::nativeWebViewAvailable, it says nothing about the - * session (a missing display still fails at run time). + * @brief True when at least one embedded engine is compiled in: the platform's native web view (WEBFRONT_EMBED_WEBKITGTK, + * WEBFRONT_EMBED_WEBVIEW2 or WEBFRONT_EMBED_WKWEBVIEW) or CEF (WEBFRONT_EMBED_CEF). Always defined, so applications need no + * preprocessor test. Like frontend::nativeWebViewAvailable, it says nothing about the session (a missing display still fails at + * run time). */ -inline constexpr bool embeddedFrontendAvailable = frontend::nativeWebViewAvailable; +inline constexpr bool embeddedFrontendAvailable = frontend::nativeWebViewAvailable || cefAvailable; namespace detail { -/// Stands in for the native engine where none is compiled in: constructing the application throws std::runtime_error. +/// Stands in where no engine is compiled in: constructing the application throws std::runtime_error. struct NoEmbeddedFrontend { static constexpr frontend::Action action{frontend::Action::closeServerAfterOpen}; static void initialize() { throw std::runtime_error("no embedded web view engine in this build: configure with -DWEBFRONT_EMBED_WEBKITGTK=ON (Linux), " - "-DWEBFRONT_EMBED_WEBVIEW2=ON (Windows) or -DWEBFRONT_EMBED_WKWEBVIEW=ON (macOS)"); + "-DWEBFRONT_EMBED_WEBVIEW2=ON (Windows), -DWEBFRONT_EMBED_WKWEBVIEW=ON (macOS) or -DWEBFRONT_EMBED_CEF=ON"); } static void open(std::string_view, std::string_view) { initialize(); @@ -975,10 +981,12 @@ struct NoEmbeddedFrontend { }; #if defined(_WIN32) || defined(__APPLE__) || defined(__linux__) -using EmbeddedFrontendType = std::conditional_t; +using NativeOrAbsentFrontend = std::conditional_t; #else -using EmbeddedFrontendType = NoEmbeddedFrontend; +using NativeOrAbsentFrontend = NoEmbeddedFrontend; #endif +// The native web view wins when both exist; CEF is the engine of a build that has no native one. +using EmbeddedFrontendType = std::conditional_t; } // namespace detail /** diff --git a/test/unit/WebFrontTests.cpp b/test/unit/WebFrontTests.cpp index 0bd5565c..b8f5edbc 100644 --- a/test/unit/WebFrontTests.cpp +++ b/test/unit/WebFrontTests.cpp @@ -490,12 +490,19 @@ SCENARIO("The native engine availability is a constant") { SCENARIO("The embedded frontend is declared in every configuration") { STATIC_REQUIRE(std::is_same_v); +#ifdef WEBFRONT_EMBED_CEF + STATIC_REQUIRE(embeddedFrontendAvailable); + STATIC_REQUIRE(std::is_same_v>); +#else STATIC_REQUIRE(embeddedFrontendAvailable == frontend::nativeWebViewAvailable); +#endif STATIC_REQUIRE(std::is_class_v); STATIC_REQUIRE(std::is_same_v>); STATIC_REQUIRE(frontend::FrontendType); #if defined(_WIN32) || defined(__APPLE__) || defined(__linux__) +#ifndef WEBFRONT_EMBED_CEF STATIC_REQUIRE(std::is_same_v == embeddedFrontendAvailable); +#endif #endif if constexpr (!embeddedFrontendAvailable) { THEN("constructing it throws std::runtime_error naming the missing engine") { From b05425f351a2ca1a2e129a65e0960927d314608c Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Sun, 1 Nov 2026 16:59:36 +0100 Subject: [PATCH 3/5] Fix the embedded frontend fallback example: limit the try to construction and correct the if constexpr note (#467) --- docs/FRONTENDS.md | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/docs/FRONTENDS.md b/docs/FRONTENDS.md index 0cdd742e..97c65163 100644 --- a/docs/FRONTENDS.md +++ b/docs/FRONTENDS.md @@ -104,20 +104,28 @@ deadline and data-store/inspector/menu tests. See [WKWebView verification](WKWEB Without a preprocessor test, `webfront::embeddedFrontendAvailable` (`inline constexpr bool`, true when a native engine or CEF is compiled in) and `webfront::EmbeddedWebFront` (always defined; the native web view when compiled in, otherwise CEF) tell an application what the build holds. -Without an engine, constructing `EmbeddedWebFront` throws `std::runtime_error`, so the fallback is an ordinary `try`/`catch`: +Without an engine, constructing `EmbeddedWebFront` throws `std::runtime_error`, so the fallback is an ordinary `try`/`catch`. +Limit the `try` to the construction: `initialize()` runs there, which is where a missing engine or display fails. Serving +stays outside, so an error of the server or of the application propagates instead of being taken for a frontend failure and +serving a second time in the browser. ```cpp +std::optional window; if constexpr (webfront::embeddedFrontendAvailable) { try { - webfront::EmbeddedWebFront window{config}; - return serve(window); - } catch (const std::exception&) { /* fall back to the browser */ } + window.emplace(config); + } catch (const std::exception&) { /* no usable engine: fall back to the browser */ } } +if (window) + return serve(*window); webfront::WebFront web{config}; return serve(web); ``` -`if constexpr` keeps the absent branch from being instantiated when `serve` needs engine-specific members. +`if constexpr` here is only a shortcut: the condition does not depend on a template parameter, so the discarded branch must +still compile. That holds because `EmbeddedWebFront` is a complete application type in every build (without an engine its +construction throws), so a `serve` generic over the application type compiles for both calls. To drop an instantiation that is +invalid without an engine, make the selection dependent, for example by passing the type to a function template. `frontend::nativeWebViewAvailable` is `true` when this platform's native engine is compiled in. It says nothing about the session. `frontend::NativeOrBrowserFrontend` decides at run time: its `initialize()` From 1f7d5424172fb06f712a31c4c86426237c0ea019 Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Sun, 1 Nov 2026 17:01:00 +0100 Subject: [PATCH 4/5] State when each embedded engine reports an unusable runtime and what the fallback catches (#467) --- docs/FRONTENDS.md | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/docs/FRONTENDS.md b/docs/FRONTENDS.md index 97c65163..7f2a509a 100644 --- a/docs/FRONTENDS.md +++ b/docs/FRONTENDS.md @@ -104,10 +104,21 @@ deadline and data-store/inspector/menu tests. See [WKWebView verification](WKWEB Without a preprocessor test, `webfront::embeddedFrontendAvailable` (`inline constexpr bool`, true when a native engine or CEF is compiled in) and `webfront::EmbeddedWebFront` (always defined; the native web view when compiled in, otherwise CEF) tell an application what the build holds. -Without an engine, constructing `EmbeddedWebFront` throws `std::runtime_error`, so the fallback is an ordinary `try`/`catch`. -Limit the `try` to the construction: `initialize()` runs there, which is where a missing engine or display fails. Serving -stays outside, so an error of the server or of the application propagates instead of being taken for a frontend failure and -serving a second time in the browser. +Without any engine, constructing `EmbeddedWebFront` throws `std::runtime_error`, so that case falls back with an ordinary `try`/`catch`. + +What the constructor reports at run time depends on the engine, because each runs its own initialization at a different moment: + +| Engine | Unusable engine or display is reported by | +| --- | --- | +| no engine compiled in | the constructor (`std::runtime_error`) | +| WebKitGTK, WKWebView | the constructor (`initialize()`: no display, missing runtime) | +| WebView2 | `show()`: `initialize()` is empty, a missing Evergreen runtime fails when the window opens | +| CEF | `show()`: `CefInitialize` runs in `open()` | + +The constructor also builds the HTTP server, so a `catch` around it receives a bind or configuration error as well as a frontend +failure. The fallback then constructs a second server with the same configuration, which fails again with the same error +and propagates: nothing is hidden, but the attempt is repeated. Keep the `try` to the construction and serving outside it, so +an error of the application is never taken for a frontend failure: ```cpp std::optional window; @@ -122,6 +133,11 @@ webfront::WebFront web{config}; return serve(web); ``` +This does not cover WebView2 and CEF failures, which surface from `show()` inside `serve`. An application that wants a fallback for them +catches around its own `show()` call and then serves with `WebFront`; it decides which errors count as a frontend failure. +For WebKitGTK and WKWebView, `frontend::NativeOrBrowserFrontend` below is the more precise tool: it intercepts only the failure +of the frontend's `initialize()`, which runs before the server is built, so no second server is constructed. + `if constexpr` here is only a shortcut: the condition does not depend on a template parameter, so the discarded branch must still compile. That holds because `EmbeddedWebFront` is a complete application type in every build (without an engine its construction throws), so a `serve` generic over the application type compiles for both calls. To drop an instantiation that is From ecbacd0458f5f70e63fc8c3ca4518fa0e69a3b60 Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Sun, 1 Nov 2026 17:02:33 +0100 Subject: [PATCH 5/5] Select the embedded frontend in a function template in the fallback example (#467) --- docs/FRONTENDS.md | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/docs/FRONTENDS.md b/docs/FRONTENDS.md index 7f2a509a..718e1135 100644 --- a/docs/FRONTENDS.md +++ b/docs/FRONTENDS.md @@ -121,16 +121,23 @@ and propagates: nothing is hidden, but the attempt is repeated. Keep the `try` t an error of the application is never taken for a frontend failure: ```cpp -std::optional window; -if constexpr (webfront::embeddedFrontendAvailable) { - try { - window.emplace(config); - } catch (const std::exception&) { /* no usable engine: fall back to the browser */ } +template +int run(const webfront::WebFrontConfig& config) { + if constexpr (Embedded) { + std::optional window; + try { + window.emplace(config); + } catch (const std::exception&) { /* no usable engine: fall back to the browser */ } + if (window) + return serve(*window); + } + webfront::WebFront web{config}; + return serve(web); +} + +int main() { + return run(webfront::WebFrontConfig{}); } -if (window) - return serve(*window); -webfront::WebFront web{config}; -return serve(web); ``` This does not cover WebView2 and CEF failures, which surface from `show()` inside `serve`. An application that wants a fallback for them @@ -138,10 +145,9 @@ catches around its own `show()` call and then serves with `WebFront`; it decides For WebKitGTK and WKWebView, `frontend::NativeOrBrowserFrontend` below is the more precise tool: it intercepts only the failure of the frontend's `initialize()`, which runs before the server is built, so no second server is constructed. -`if constexpr` here is only a shortcut: the condition does not depend on a template parameter, so the discarded branch must -still compile. That holds because `EmbeddedWebFront` is a complete application type in every build (without an engine its -construction throws), so a `serve` generic over the application type compiles for both calls. To drop an instantiation that is -invalid without an engine, make the selection dependent, for example by passing the type to a function template. +The selection is a function template on a `bool` because the condition of an `if constexpr` outside a template does not +depend on a template parameter, so its discarded branch would still be checked. Here the branch that names `EmbeddedWebFront` +is not instantiated when the argument is `false`, so a `serve` that does not accept that type compiles without an engine. `frontend::nativeWebViewAvailable` is `true` when this platform's native engine is compiled in. It says nothing about the session. `frontend::NativeOrBrowserFrontend` decides at run time: its `initialize()`