Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
web_view.hpp
Go to the documentation of this file.
1#pragma once
2
3// WebView embedding for Pulp
4// Wraps CHOC's WebView (ISC license) to provide embedded web content
5// inside plugin/app UIs — for Monaco editors, docs panels, custom UIs, etc.
6//
7// Usage:
8// auto wv = WebViewPanel::create(options);
9// wv->navigate("https://example.com");
10// wv->set_html("<h1>Hello</h1>");
11// wv->evaluate_js("document.title");
12// wv->bind("myFunc", [](const std::string& args) { ... });
13
14#include <string>
15#include <functional>
16#include <filesystem>
17#include <memory>
18#include <optional>
19#include <vector>
20
21namespace pulp::view {
22
23// Platform-native handle (NSView*, HWND, etc.)
24using NativeViewHandle = void*;
25
27 struct Resource {
28 std::vector<uint8_t> data;
29 std::string mime_type;
30 };
31 using FetchResource = std::function<std::optional<Resource>(const std::string& path)>;
32
33 bool enable_debug = false;
36 bool accept_first_click = true;
37 std::string initial_html;
38 std::string custom_user_agent;
40 std::string custom_scheme_uri;
41};
42
43// Simple structured message envelope for native <-> WebView communication.
44// payload_json must contain serialized JSON (object, array, string, number, bool, or null).
46 std::string type;
47 std::string payload_json = "null";
48 std::string id;
49};
50
51// Offline resource description for embedded WebView content. reference_path is
52// the URL used inside HTML (for example "app.js" or "./styles.css"), while
53// embedded_name is the key previously registered with AssetManager.
55 std::string reference_path;
56 std::string embedded_name;
57 std::string mime_type;
58};
59
61std::string encode_webview_message_json(const WebViewMessage& message);
62
65bool decode_webview_message_json(const std::string& json, WebViewMessage& message);
66
72
75std::string make_webview_eval_script(const std::string& request_id,
76 const std::string& js_source);
77
79std::string guess_webview_mime_type(const std::string& path);
80
82std::string make_webview_data_uri(const std::string& mime_type,
83 const std::vector<uint8_t>& bytes);
84
86std::string load_webview_embedded_text(const std::string& embedded_name);
87
90std::string make_webview_offline_html(const std::string& html,
91 const std::vector<WebViewEmbeddedAsset>& assets);
92
95 const std::string& html_embedded_name,
96 const std::vector<WebViewEmbeddedAsset>& assets);
97
99std::optional<WebViewOptions::Resource> fetch_webview_embedded_resource(
100 const std::string& path,
101 const std::string& html_embedded_name,
102 const std::vector<WebViewEmbeddedAsset>& assets);
103
107 std::string html_embedded_name,
108 std::vector<WebViewEmbeddedAsset> assets);
109
113std::optional<WebViewOptions::Resource> fetch_webview_directory_resource(
114 const std::string& path,
115 const std::filesystem::path& root_directory,
116 std::string index_filename = "index.html");
117
122 std::filesystem::path root_directory,
123 std::string index_filename = "index.html");
124
138
141
142namespace detail {
143
150 std::string (*detect)() = nullptr;
151 bool (*available)() = nullptr;
152};
153
158
159} // namespace detail
160
161// Embedded web view component.
162// Create with WebViewPanel::create(), then use native_handle() to embed the
163// platform WebView in a WindowHost/PluginViewHost that actually exposes and
164// accepts native child views. WebView backend availability and host embedding
165// availability are separate capabilities; callers must check the host handle
166// and attach_native_child_view() result.
168public:
169 using JsCallback = std::function<std::string(const std::string& args_json)>;
170 using EvalCallback = std::function<void(const std::string& result_json,
171 const std::string& error)>;
172 using MessageHandler = std::function<std::string(const WebViewMessage& message)>;
173 using ReadyHandler = std::function<void()>;
174
175 static std::unique_ptr<WebViewPanel> create(const WebViewOptions& options = {});
176
177 virtual ~WebViewPanel() = default;
178
179 // Whether the underlying native WebView is ready for navigation / JS
180 // evaluation. Some platforms become ready asynchronously after creation.
181 virtual bool is_ready() const = 0;
182
183 // Set a callback fired once the underlying native WebView becomes ready.
184 // If the backend is already ready when this is called, the handler runs
185 // immediately.
186 virtual void set_ready_handler(ReadyHandler handler) = 0;
187
188 // Get the native view handle for embedding (NSView*, HWND, etc.). This is
189 // the child surface handle only; successful embedding also requires a host
190 // whose native child-view attach contract returns true.
192
193 // In-process PNG snapshot of the rendered web content (macOS: WKWebView
194 // takeSnapshot — no screen-recording permission needed). This is how a native
195 // WebView overlay becomes headlessly capturable even though it isn't painted
196 // into the Pulp Skia canvas. Returns empty bytes when unsupported / not ready.
197 // Must be called on the main thread.
198 virtual std::vector<uint8_t> snapshot_png() { return {}; }
199
200 // Navigate to a URL
201 virtual void navigate(const std::string& url) = 0;
202
203 // Load HTML content directly
204 virtual void set_html(const std::string& html) = 0;
205
206 // Execute JavaScript in the webview context
207 virtual void evaluate_js(const std::string& js) = 0;
208
209 // Execute JavaScript and receive its eventual JSON-serializable result.
210 // Errors are surfaced via the error string.
211 virtual void evaluate_js(const std::string& js, EvalCallback callback) = 0;
212
213 // Bind a C++ function callable from JavaScript as window.<name>(args)
214 // The callback receives JSON args string and returns JSON result string
215 virtual void bind(const std::string& name, JsCallback callback) = 0;
216
217 // Register a structured JSON message handler for JavaScript callers using
218 // the built-in `__pulpPostMessage(...)` bridge.
219 virtual void set_message_handler(MessageHandler handler) = 0;
220
221 // Deliver a structured message to the current page. If the page has
222 // installed `make_webview_bridge_bootstrap_script()`, listeners registered
223 // via `window.pulp.on(...)` will receive it. A DOM CustomEvent named
224 // `pulp-message` is also dispatched.
225 virtual void post_message(const WebViewMessage& message) = 0;
226
227 // Resize the webview
228 virtual void set_size(uint32_t width, uint32_t height) = 0;
229};
230
231} // namespace pulp::view
Definition web_view.hpp:167
std::function< void(const std::string &result_json, const std::string &error)> EvalCallback
Definition web_view.hpp:170
virtual void set_ready_handler(ReadyHandler handler)=0
std::function< void()> ReadyHandler
Definition web_view.hpp:173
virtual void evaluate_js(const std::string &js, EvalCallback callback)=0
virtual std::vector< uint8_t > snapshot_png()
Definition web_view.hpp:198
std::function< std::string(const std::string &args_json)> JsCallback
Definition web_view.hpp:169
virtual void set_html(const std::string &html)=0
static std::unique_ptr< WebViewPanel > create(const WebViewOptions &options={})
virtual void bind(const std::string &name, JsCallback callback)=0
virtual void set_message_handler(MessageHandler handler)=0
virtual bool is_ready() const =0
virtual void navigate(const std::string &url)=0
virtual void post_message(const WebViewMessage &message)=0
std::function< std::string(const WebViewMessage &message)> MessageHandler
Definition web_view.hpp:172
virtual void set_size(uint32_t width, uint32_t height)=0
virtual ~WebViewPanel()=default
virtual void evaluate_js(const std::string &js)=0
virtual NativeViewHandle native_handle()=0
Definition accessibility_provider.hpp:65
const WebViewBackendProvider * set_webview_backend_provider(const WebViewBackendProvider *provider)
Install an override provider (for tests), or with nullptr remove it so the linked provider answers ag...
Hover-cursor tracking for platform window hosts.
Definition fwd.hpp:27
@ path
Definition anchor_strategy.hpp:42
bool decode_webview_message_json(const std::string &json, WebViewMessage &message)
Decode a structured WebView message from JSON.
std::string guess_webview_mime_type(const std::string &path)
Best-effort MIME type guess for an HTML-linked asset path.
std::string encode_webview_message_json(const WebViewMessage &message)
Encode a structured WebView message to canonical JSON.
std::string make_webview_offline_html(const std::string &html, const std::vector< WebViewEmbeddedAsset > &assets)
Rewrite simple src/href references in HTML to offline data: URIs backed by embedded assets.
std::string load_webview_embedded_text(const std::string &embedded_name)
Load a UTF-8 text asset previously registered with AssetManager.
WebViewOptions::FetchResource make_webview_directory_resource_fetcher(std::filesystem::path root_directory, std::string index_filename="index.html")
Create a resource callback that serves files from a directory through CHOC's fetchResource/customSche...
std::string make_webview_data_uri(const std::string &mime_type, const std::vector< uint8_t > &bytes)
Create a data: URI for an embedded WebView asset.
std::string make_webview_bridge_bootstrap_script()
JavaScript helper that installs a small window.pulp bridge on a page.
@ html
Definition design_ir.hpp:46
std::string make_webview_offline_html_from_embedded(const std::string &html_embedded_name, const std::vector< WebViewEmbeddedAsset > &assets)
Load embedded HTML and rewrite its referenced assets for offline use.
std::string make_webview_eval_script(const std::string &request_id, const std::string &js_source)
JavaScript wrapper that evaluates user code asynchronously and reports the result back through the in...
std::optional< WebViewOptions::Resource > fetch_webview_directory_resource(const std::string &path, const std::filesystem::path &root_directory, std::string index_filename="index.html")
Fetch a single file-system resource for a WebView custom resource server.
@ height
Definition css_animation.hpp:128
@ width
Definition css_animation.hpp:127
bool webview_backend_available()
Returns true if the host process has a usable WebView backend.
WebViewOptions::FetchResource make_webview_embedded_resource_fetcher(std::string html_embedded_name, std::vector< WebViewEmbeddedAsset > assets)
Create a resource callback that serves embedded HTML/assets through CHOC's fetchResource/customScheme...
std::optional< WebViewOptions::Resource > fetch_webview_embedded_resource(const std::string &path, const std::string &html_embedded_name, const std::vector< WebViewEmbeddedAsset > &assets)
Fetch a single embedded resource for a WebView custom resource server.
@ error
Definition design_ir.hpp:697
std::string detect_webview_backend()
Identifies the live WebView backend on this platform / build.
void * NativeViewHandle
Definition native_view_host.hpp:19
Definition web_view.hpp:54
std::string mime_type
Definition web_view.hpp:57
std::string reference_path
Definition web_view.hpp:55
std::string embedded_name
Definition web_view.hpp:56
Definition web_view.hpp:45
std::string payload_json
Definition web_view.hpp:47
std::string id
Definition web_view.hpp:48
std::string type
Definition web_view.hpp:46
Definition web_view.hpp:27
std::vector< uint8_t > data
Definition web_view.hpp:28
std::string mime_type
Definition web_view.hpp:29
Definition web_view.hpp:26
std::string custom_user_agent
Override User-Agent string.
Definition web_view.hpp:38
std::string initial_html
Optional placeholder HTML shown before navigate()/set_html().
Definition web_view.hpp:37
bool transparent_background
Definition web_view.hpp:35
bool enable_debug
Enable DevTools / Web Inspector.
Definition web_view.hpp:33
bool enable_debug_inspector
Request a separate inspector window when supported.
Definition web_view.hpp:34
FetchResource fetch_resource
Optional custom resource server.
Definition web_view.hpp:39
std::function< std::optional< Resource >(const std::string &path)> FetchResource
Definition web_view.hpp:31
std::string custom_scheme_uri
Optional home URI for fetch_resource, e.g. pulp://app.
Definition web_view.hpp:40
bool accept_first_click
macOS: first click on unfocused view is input
Definition web_view.hpp:36
The answer behind detect_webview_backend() and webview_backend_available().
Definition web_view.hpp:149
bool(* available)()
Definition web_view.hpp:151
std::string(* detect)()
Definition web_view.hpp:150