ladybird/Libraries/LibWebView/WebUI.h
Timothy Flynn 41aeb9e63a LibWeb+LibWebView+WebContent: Introduce a WebUI framework
When we build internal pages (e.g. about:settings), there is currently
quite a lot of boilerplate needed to communicate between the browser and
the page. This includes creating IDL for the page and the IPC for every
message sent between the processes.

These internal pages are also special in that they have privileged
access to and control over the browser process.

The framework introduced here serves to ease the setup of new internal
pages and to reduce the access that WebContent processes have to the
browser process. WebUI pages can send requests to the browser process
via a `ladybird.sendMessage` API. Responses from the browser are passed
through a WebUIMessage event. So, for example, an internal page may:

    ladybird.sendMessage("getDataFor", { id: 123 });

    document.addEventListener("WebUIMessage", event => {
        if (event.name === "gotData") {
            console.assert(event.data.id === 123);
        }
    });

To handle these messages, we set up a new IPC connection between the
browser and WebContent processes. This connection is torn down when
the user navigates away from the internal page.
2025-03-28 07:31:10 -04:00

63 lines
2.4 KiB
C++

/*
* Copyright (c) 2025, Tim Flynn <trflynn89@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Function.h>
#include <AK/HashMap.h>
#include <AK/JsonValue.h>
#include <AK/NonnullRefPtr.h>
#include <AK/RefPtr.h>
#include <AK/String.h>
#include <LibIPC/ConnectionToServer.h>
#include <LibIPC/Transport.h>
#include <LibWebView/Forward.h>
#include <WebContent/WebUIClientEndpoint.h>
#include <WebContent/WebUIServerEndpoint.h>
namespace WebView {
class WebUI
: public IPC::ConnectionToServer<WebUIClientEndpoint, WebUIServerEndpoint>
, public WebUIClientEndpoint {
public:
static ErrorOr<RefPtr<WebUI>> create(WebContentClient&, String host);
virtual ~WebUI();
String const& host() const { return m_host; }
protected:
WebUI(WebContentClient&, IPC::Transport, String host);
using Interface = Function<void(JsonValue)>;
virtual void register_interfaces() { }
void register_interface(StringView name, Interface);
private:
virtual void die() override;
virtual void received_message(String name, JsonValue data) override;
WebContentClient& m_client;
String m_host;
HashMap<StringView, Interface> m_interfaces;
};
#define WEB_UI(WebUIType) \
public: \
static NonnullRefPtr<WebUIType> create(WebContentClient& client, IPC::Transport transport, String host) \
{ \
return adopt_ref(*new WebUIType(client, move(transport), move(host))); \
} \
\
private: \
WebUIType(WebContentClient& client, IPC::Transport transport, String host) \
: WebView::WebUI(client, move(transport), move(host)) \
{ \
}
}