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: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ app.show();

Recompilation is explicit. The resulting application runs offline without the project directory, developer callbacks or downloaded tool cache. The selected native engine's deployment requirements still apply; this is not an application installer.

The examples' [LifeFrontend.hpp](examples/life/LifeFrontend.hpp) selects the native engine or the default browser for both stages.
The examples' [LifeFrontend.hpp](examples/life/LifeFrontend.hpp) selects `NativeOrBrowserFrontend` for both stages: a native window when the engine is built and a session exists, the default browser otherwise.
Your own application can select `webfront::NativeApp` or `webfront::NativeAppWithAssets` explicitly when it requires a native window.

## Use WebFront in your project
Expand Down Expand Up @@ -281,6 +281,8 @@ int main() {
}
```

To get a native window when one can be opened and the system browser otherwise (for example over SSH, in a container or in CI, where no display exists), use `WebFrontWithFrontend<webfront::frontend::NativeOrBrowserFrontend>`: no `#if` is needed, and the reason for a fallback is printed once. See [docs/FRONTENDS.md](docs/FRONTENDS.md).

Native engines are detected by default for consumer builds, whatever variables the parent project defines, and for standalone builds except with `ENABLE_TESTING=ON` or `ENABLE_FUZZING=ON`. The helper applies the native engine's target setup and defines `WEBFRONT_NATIVE_WINDOW` only when that engine is enabled; otherwise CMake prints installation advice and the application uses the default browser. On Windows, detection requires an MSVC-compatible compiler and downloads the pinned WebView2 SDK; the Evergreen runtime must be installed separately. Linux needs WebKitGTK 6.0 development files, and macOS needs Clang and macOS 13.3 or later. Explicit `WEBFRONT_EMBED_<ENGINE>=ON|OFF` values take precedence; `WEBFRONT_AUTODETECT_NATIVE=OFF` disables detection. No option needs to be set before `add_subdirectory`/CPM for the default path.

For explicit engine selection, `webfront_native_frontend_option(out)` and `webfront_setup_frontends_for_target(MyApp)` are also callable from the parent or other directories. The latter applies setup for all enabled engines; the native helper selects only the platform's native engine and never CEF. See [WKWebView](docs/WKWEBVIEW.md), [WebKitGTK](docs/WEBKITGTK.md) and the [frontend architecture](docs/adr/README.md).
Expand Down
6 changes: 6 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Unreleased

- Add `frontend::nativeWebViewAvailable` and `frontend::NativeOrBrowserFrontend` (#398): a native
window when the platform engine is compiled in and a session exists, the system browser otherwise,
with the reason printed once and available from `fallbackReason()`. An application needs neither a
`WEBFRONT_EMBED_*` macro nor a second application type. A frontend may define an optional
`static bool closeServerAfterOpen()` to override its `action` at run time.

- Accept ADR-019 for the delivered local automation contract, shared window and native grants,
with consumer Jasmine coverage, the five-model qualification and the final PR #407 CI evidence.
Record the remaining distribution publication gate and deferred remote/browser transports.
Expand Down
19 changes: 19 additions & 0 deletions docs/FRONTENDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,25 @@ their own CTest timeouts.
WKWebView adds native main-thread, Quit/closure, repeated-opening, initial HTTP failure, stalled-loading
deadline and data-store/inspector/menu tests. See [WKWebView verification](WKWEBVIEW.md#unattended-verification).

## Native window with browser fallback

`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`,
missing runtime), it initializes the browser frontend and uses the system browser for the whole process and prints the reason once to
`std::cerr`. Only `initialize()` triggers the fallback: if the native `open()` throws, the exception
propagates as for any frontend. `fallbackReason()` returns that reason and `usesNativeWindow()` tells which path was taken
once the application is constructed.

```cpp
using App = webfront::WebFrontWithFrontend<webfront::frontend::NativeOrBrowserFrontend>;
```

A native window stops the server when it closes; the browser path leaves it running until Enter, as
`DefaultBrowserFrontend` does. Because `action` is `constexpr`, the frontend reports this through the
optional `closeServerAfterOpen()` hook, which `show()` consults after `open()` returns.
`NativeOrBrowserFrontendFor<Native, Browser>` composes any two frontends the same way.

## Direct engine access

Every frontend currently loads its page over HTTP and exchanges bridge messages over a WebSocket. [ADR-017](adr/ADR-017-acces-direct-aux-frontends.md)
Expand Down
13 changes: 2 additions & 11 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Only two examples: the minimal application and the successive Game of Life stages.
include(Frontends)

# The Life examples use the native window when its engine is built and otherwise fall back to the system browser,
# which needs no dependency: a missing engine is explained here, never an error.
# The Life examples use NativeOrBrowserFrontend: the native window when its engine is built and a session exists,
# the system browser otherwise. A missing engine is explained here, never an error.
webfront_native_frontend_option(WEBFRONT_LIFE_NATIVE_OPTION)
set(WEBFRONT_LIFE_NATIVE_WINDOW OFF)
if(WEBFRONT_LIFE_NATIVE_OPTION)
Expand All @@ -18,12 +18,6 @@ if(NOT WEBFRONT_LIFE_NATIVE_WINDOW)
" ${_life_advice}\n"
" CEF (-DWEBFRONT_EMBED_CEF=ON) makes CEFFrontend available to your own applications; the examples do not select it.")
endif()
# Select the same frontend for the developer and embedded Life stages.
function(webfront_life_frontend target)
if(WEBFRONT_LIFE_NATIVE_WINDOW)
target_compile_definitions(${target} PRIVATE WEBFRONT_LIFE_NATIVE_WINDOW)
endif()
endfunction()

add_executable(HelloWorld hello-world/HelloWorld.cpp)
target_link_libraries(HelloWorld PRIVATE WebFront WebFront_warnings WebFront_options)
Expand All @@ -36,12 +30,10 @@ target_link_libraries(WebFrontLifeConsole PRIVATE WebFront_warnings WebFront_opt
add_executable(WebFrontLifeDeveloper life/LifeDeveloper.cpp)
target_link_libraries(WebFrontLifeDeveloper PRIVATE WebFront WebFront_warnings WebFront_options)
webfront_setup_frontends_for_target(WebFrontLifeDeveloper)
webfront_life_frontend(WebFrontLifeDeveloper)

add_executable(WebFrontLifeReact life/react/LifeReact.cpp)
target_link_libraries(WebFrontLifeReact PRIVATE WebFront WebFront_warnings WebFront_options)
webfront_setup_frontends_for_target(WebFrontLifeReact)
webfront_life_frontend(WebFrontLifeReact)

set(WEBFRONT_LIFE_ASSETS "" CACHE FILEPATH "LifeUI.hpp generated by wf embed (enables the offline Life example)")
if(WEBFRONT_LIFE_ASSETS)
Expand All @@ -52,5 +44,4 @@ if(WEBFRONT_LIFE_ASSETS)
target_link_libraries(WebFrontLifeEmbedded PRIVATE WebFront WebFront_warnings WebFront_options)
target_compile_definitions(WebFrontLifeEmbedded PRIVATE WEBFRONT_LIFE_ASSETS_HEADER="${WEBFRONT_LIFE_ASSETS}")
webfront_setup_frontends_for_target(WebFrontLifeEmbedded)
webfront_life_frontend(WebFrontLifeEmbedded)
endif()
2 changes: 0 additions & 2 deletions examples/life/LifeDeveloper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ int main(int argc, char** argv) {
}
try {
webfront::demo::prepareLifeProject(options->project);
if constexpr (!webfront::demo::lifeNativeWindow)
std::cerr << webfront::demo::browserNotice << '\n';
webfront::demo::LifeApp app{options->project};
app.cppFunction("life.step", &webfront::demo::nextLife);
if (!options->environment)
Expand Down
2 changes: 0 additions & 2 deletions examples/life/LifeEmbedded.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

int main() {
try {
if constexpr (!webfront::demo::lifeNativeWindow)
std::cerr << webfront::demo::browserNotice << '\n';
webfront::demo::LifeAppWithAssets<webfront::fs::LifeUI> app;
app.cppFunction("life.step", &webfront::demo::nextLife);
app.show();
Expand Down
16 changes: 3 additions & 13 deletions examples/life/LifeFrontend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,13 @@

namespace webfront::demo {

// examples/CMakeLists.txt defines WEBFRONT_LIFE_NATIVE_WINDOW when this platform's native engine is built.
// Otherwise the Life examples open the system browser, which needs no extra dependency.
#if defined(WEBFRONT_LIFE_NATIVE_WINDOW)
using LifeFrontend = frontend::NativeWebViewFrontend;
inline constexpr bool lifeNativeWindow = true;
#else
using LifeFrontend = frontend::DefaultBrowserFrontend;
inline constexpr bool lifeNativeWindow = false;
#endif
// A native window when this platform's engine is built and a session exists, the system browser otherwise
// (no engine, SSH, container, CI). The reason for a fallback is printed once by the frontend.
using LifeFrontend = frontend::NativeOrBrowserFrontend;

using LifeApp = BasicDeveloperApp<LifeFrontend>;

template <fs::Provider... Assets>
using LifeAppWithAssets = BasicWF<NetProvider, fs::DefaultFSWith<Assets...>, http::DefaultBuffersPolicy, LifeFrontend>;

inline constexpr const char* browserNotice =
"No native window engine was built: the default browser is opened instead. For a native window, install the "
"engine for your platform and run cmake again (see the README).";

} // namespace webfront::demo
4 changes: 3 additions & 1 deletion include/webfront/WebFront.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,9 @@ class BasicWF {
Frontend::open(httpAddress, httpPort, htmlFilename);
else
Frontend::open(httpPort, htmlFilename);
if constexpr (Frontend::action == frontend::Action::closeServerAfterOpen)
if constexpr (requires { Frontend::closeServerAfterOpen(); })
return Frontend::closeServerAfterOpen() ? WindowAction::closeWindow : WindowAction::none;
else if constexpr (Frontend::action == frontend::Action::closeServerAfterOpen)
return WindowAction::closeWindow;
else
return WindowAction::none;
Expand Down
118 changes: 118 additions & 0 deletions include/webfront/frontend/Frontend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

#include <concepts>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <optional>
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>

Expand All @@ -36,6 +41,7 @@ namespace webfront::frontend {
* diagnostic naming its enabling CMake option. ContentPolicy rules and adapter capabilities
* are documented in docs/CONTENT_POLICY.md.
* An optional open(address, port, file, policy, window) overload also receives the application's WindowOptions.
* An optional static bool closeServerAfterOpen() overrides `action` at run time (NativeOrBrowserFrontend).
* An optional open(address, port, file) overload receives the configured address; build URLs with
* detail::applicationUrl() so all frontends preserve the same URI-reference semantics.
*/
Expand Down Expand Up @@ -95,6 +101,118 @@ struct CEFFrontend {
}
};

namespace detail {

#if defined(_WIN32) || defined(__APPLE__) || defined(__linux__)
using PlatformNativeFrontend = NativeWebViewFrontend;
#else
// No native engine exists on this platform: the fallback always takes the browser.
struct PlatformNativeFrontend {
static constexpr Action action{Action::closeServerAfterOpen};

static void initialize() {
throw std::runtime_error("no native web view exists for this platform");
}
static void open(std::string_view, std::string_view) {
initialize();
}
};
#endif

// Calls the richest open() the frontend offers, as BasicWF does.
template <typename Frontend>
void openWith(std::string_view address, std::string_view port, std::string_view file, const ContentPolicy& policy, const WindowOptions& window) {
if constexpr (requires { Frontend::open(address, port, file, policy, window); })
Frontend::open(address, port, file, policy, window);
else if constexpr (requires { Frontend::open(address, port, file, policy); })
Frontend::open(address, port, file, policy);
else if constexpr (requires { Frontend::open(address, port, file); })
Frontend::open(address, port, file);
else
Frontend::open(port, file);
}

} // namespace detail

/**
* @brief A native window when one can be opened, the system browser otherwise.
*
* initialize() tries Native::initialize(); if it throws (engine not compiled in, no display, missing runtime), the
* frontend remembers the reason, prints it once to std::cerr and uses Browser for the whole process. The decision is
* per frontend type and made once, like every initialize(). Only initialize() triggers the fallback: an exception
* from the native open() propagates, because a window that failed after it started cannot be told apart from one
* the user closed. Applications need neither a WEBFRONT_EMBED_* macro nor a
* second application type.
*
* `action` is that of the native window; the browser path is reported at run time through closeServerAfterOpen(),
* which the application consults after open() returns.
*/
template <FrontendType Native, FrontendType Browser>
struct NativeOrBrowserFrontendFor {
static constexpr Action action{Native::action};

/// Why the browser is used, or nullopt when the native window is. Meaningful after initialize().
[[nodiscard]] static std::optional<std::string> fallbackReason() {
return state().reason;
}
[[nodiscard]] static bool usesNativeWindow() {
return !state().reason.has_value();
}
/// Whether the server stops once open() returns: true for a native window, false for the browser.
[[nodiscard]] static bool closeServerAfterOpen() {
if constexpr (Native::action == Action::closeServerAfterOpen)
return usesNativeWindow();
else
return false;
}

// Called once per process by BasicWF (std::call_once). It resets the decision, so a direct caller must not
// race it with open(); the unit tests call it repeatedly on purpose.
static void initialize() {
auto& s = state();
s.reason.reset();
try {
Native::initialize();
} catch (const std::exception& e) {
s.reason = e.what();
}
if (s.reason) {
std::cerr << "WebFront: opening the system browser instead of a native window: " << *s.reason << '\n';
Browser::initialize();
}
}

static void open(std::string_view port, std::string_view file) {
if (usesNativeWindow())
Native::open(port, file);
else
Browser::open(port, file);
}
static void open(std::string_view address, std::string_view port, std::string_view file) {
open(address, port, file, ContentPolicy{}, WindowOptions{});
}
static void open(std::string_view address, std::string_view port, std::string_view file, const ContentPolicy& policy) {
open(address, port, file, policy, WindowOptions{});
}
static void open(std::string_view address, std::string_view port, std::string_view file, const ContentPolicy& policy, const WindowOptions& window) {
if (usesNativeWindow())
detail::openWith<Native>(address, port, file, policy, window);
else
detail::openWith<Browser>(address, port, file, policy, window);
}

private:
struct State {
std::optional<std::string> reason;
};
static State& state() {
static State instance;
return instance;
}
};

using NativeOrBrowserFrontend = NativeOrBrowserFrontendFor<detail::PlatformNativeFrontend, DefaultBrowserFrontend>;

// WEBFRONT_EMBED_CEF only means "CEF is available"; it never changes which frontend
// plain WebFront selects. Applications opt into CEF explicitly via WebFrontWithFrontend<CEFFrontend>.
using DefaultFrontend = DefaultBrowserFrontend;
Expand Down
14 changes: 14 additions & 0 deletions include/webfront/frontend/NativeWebView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@
// Other platforms have no native alias; their applications can select another frontend.
namespace webfront::frontend {

/**
* @brief True when this platform's native web view engine is compiled in (WEBFRONT_EMBED_WEBKITGTK on Linux,
* WEBFRONT_EMBED_WEBVIEW2 on Windows, WEBFRONT_EMBED_WKWEBVIEW on macOS).
*
* It says nothing about the session: a native window can still fail at run time, for instance without a display.
* NativeOrBrowserFrontend handles that case.
*/
#if (defined(_WIN32) && defined(WEBFRONT_EMBED_WEBVIEW2)) || (defined(__APPLE__) && defined(WEBFRONT_EMBED_WKWEBVIEW)) \
|| (defined(__linux__) && defined(WEBFRONT_EMBED_WEBKITGTK))
inline constexpr bool nativeWebViewAvailable = true;
#else
inline constexpr bool nativeWebViewAvailable = false;
#endif

#if defined(_WIN32)
/** @brief Opt-in Windows web view; availability and options are those of WebView2Frontend. */
using NativeWebViewFrontend = WebView2Frontend;
Expand Down
4 changes: 0 additions & 4 deletions test/cmake/native-autodetect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,4 @@ if(TEST_EXAMPLES)
function(webfront_setup_frontends_for_target target)
endfunction()
add_subdirectory("${ROOT}/examples" examples)
get_target_property(definitions WebFrontLifeDeveloper COMPILE_DEFINITIONS)
if("WEBFRONT_LIFE_NATIVE_WINDOW" IN_LIST definitions)
message(FATAL_ERROR "An unsupported platform must use the default browser")
endif()
endif()
Loading
Loading