diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 040c1c97..bae7bb40 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +- 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 3bbf9fce..718e1135 100644 --- a/docs/FRONTENDS.md +++ b/docs/FRONTENDS.md @@ -102,6 +102,53 @@ 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 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 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 +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{}); +} +``` + +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. + +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()` 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..97882628 100644 --- a/include/webfront/WebFront.hpp +++ b/include/webfront/WebFront.hpp @@ -952,6 +952,49 @@ template ; using UI = WebFront::UI; +#ifdef WEBFRONT_EMBED_CEF +inline constexpr bool cefAvailable = true; +#else +inline constexpr bool cefAvailable = false; +#endif + +/** + * @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 || cefAvailable; + +namespace detail { +/// 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), -DWEBFRONT_EMBED_WKWEBVIEW=ON (macOS) or -DWEBFRONT_EMBED_CEF=ON"); + } + static void open(std::string_view, std::string_view) { + initialize(); + } +}; + +#if defined(_WIN32) || defined(__APPLE__) || defined(__linux__) +using NativeOrAbsentFrontend = std::conditional_t; +#else +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 + +/** + * @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..b8f5edbc 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,30 @@ SCENARIO("The native engine availability is a constant") { #endif } +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") { + 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;