Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<name>/captures/<name>.png` and numbered `<name>/0001.png`... for an external encoder such as ffmpeg.
Expand Down
47 changes: 47 additions & 0 deletions docs/FRONTENDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <bool Embedded>
int run(const webfront::WebFrontConfig& config) {
if constexpr (Embedded) {
std::optional<webfront::EmbeddedWebFront> 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::embeddedFrontendAvailable>(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`,
Expand Down
43 changes: 43 additions & 0 deletions include/webfront/WebFront.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,49 @@ template <frontend::FrontendType Frontend, http::BuffersPolicyType Policy = http
using WebFrontWithFrontend = BasicWF<NetProvider, fs::DefaultFS, Policy, Frontend>;
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<frontend::nativeWebViewAvailable, frontend::NativeWebViewFrontend, NoEmbeddedFrontend>;
#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<frontend::nativeWebViewAvailable || !cefAvailable, NativeOrAbsentFrontend, frontend::CEFFrontend>;
} // 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<detail::EmbeddedFrontendType>;

} // namespace webfront

// Optional templates instantiate only when the developer/native facade is selected.
Expand Down
25 changes: 25 additions & 0 deletions test/unit/WebFrontTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <catch2/catch_test_macros.hpp>
#include <catch2/generators/catch_generators.hpp>
#include <catch2/matchers/catch_matchers.hpp>
#include <catch2/matchers/catch_matchers_string.hpp>
#include <webfront/networking/NetworkingMock.hpp>
#include <webfront/tooling/Logger.hpp>
#include <webfront/WebFront.hpp>
Expand Down Expand Up @@ -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<decltype(embeddedFrontendAvailable), const bool>);
#ifdef WEBFRONT_EMBED_CEF
STATIC_REQUIRE(embeddedFrontendAvailable);
STATIC_REQUIRE(std::is_same_v<detail::EmbeddedFrontendType, std::conditional_t<frontend::nativeWebViewAvailable, frontend::NativeWebViewFrontend, frontend::CEFFrontend>>);
#else
STATIC_REQUIRE(embeddedFrontendAvailable == frontend::nativeWebViewAvailable);
#endif
STATIC_REQUIRE(std::is_class_v<EmbeddedWebFront>);
STATIC_REQUIRE(std::is_same_v<EmbeddedWebFront, WebFrontWithFrontend<detail::EmbeddedFrontendType>>);
STATIC_REQUIRE(frontend::FrontendType<detail::EmbeddedFrontendType>);
#if defined(_WIN32) || defined(__APPLE__) || defined(__linux__)
#ifndef WEBFRONT_EMBED_CEF
STATIC_REQUIRE(std::is_same_v<detail::EmbeddedFrontendType, frontend::NativeWebViewFrontend> == 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<WebFrontNetworkingMock, TestFilesystem, FailingNonBlockingFrontend>;
Expand Down
Loading