@@ -12,15 +12,15 @@ preview: |-
1212 Web content seamlessly.
1313---
1414
15- While most Web content designed entirely for screen display&mdash ; and there is
16- * a lot* of it&mdash ; will spend its life in the somewhat restricted sandbox
17- implemented by a web browser, taking advantage of Web technologies to provide
18- rich user interfaces in all kinds of consumer devices requires at least * some*
19- degree of integration with the rest of their software and hardware. This is
20- where a Web engine like WPE designed to be * embeddable* shines: not only does WPE
21- provide a [ stable API] [ wpewebkit-api ] , it is also comprehensive in
22- supporting a number of ways to * integrate* with its environment further
23- than the plethora of available [ Web platform APIs] [ mdn-web-apis ] .
15+ Most Web content is designed entirely for screen display&mdash ; and there is * a
16+ lot* of it&mdash ; so it will spend its life in the somewhat restricted sandbox
17+ implemented by a web browser. But rich user interfaces using Web technologies
18+ in all kinds of consumer devices require * some* degree of integration, an
19+ escape hatch to interact with the rest of their software and hardware. This is
20+ where a Web engine like WPE designed to be * embeddable* shines: not only does
21+ WPE provide a [ stable API] [ wpewebkit-api ] , it is also comprehensive in
22+ supporting a number of ways to * integrate* with its environment further than
23+ the plethora of available [ Web platform APIs] [ mdn-web-apis ] .
2424
2525Integrating a “Web view” (the main [ entry point of the WPE embedding
2626API] [ api-webview ] ) involves providing extension points, which allow the
@@ -79,6 +79,32 @@ on_create_view(CogShell *shell, CogPlatform *platform)
7979}
8080```
8181
82+ <details>
83+ <summary>What is <code>g_autoptr</code>?
84+ Does it relate to <code>g_steal_pointer</code>?
85+ This does not look like C!</summary><div>
86+
87+ In the shown code examples, `g_autoptr(T)` is a preprocessor macro provided by
88+ GLib that declares a pointer variable of the `T` type, and arranges for
89+ freeing resources automatically when the variable goes out of scope. For
90+ objects this results in
91+ [g_object_unref()](https://docs.gtk.org/gobject/method.Object.unref.html)
92+ being called.
93+
94+ Internally the macro takes advantage of the `__attribute__((cleanup, ...))`
95+ compiler extension, which is supported by GCC and Clang. GLib also includes [a
96+ convenience
97+ macro](https://docs.gtk.org/glib/func.DEFINE_AUTOPTR_CLEANUP_FUNC.html) that
98+ can be used to define cleanups for your own types.
99+
100+ As for `g_steal_pointer`, it is useful to indicate that the ownership of a
101+ pointer declared with `g_autoptr` is transferred outside from the current
102+ scope. The function returns the same pointer passed as parameter and
103+ resets it to `NULL`, thus preventing cleanup functions from running.
104+
105+ </div></details>
106+
107+
82108The size has been kept small thanks to reusing code from the [Cog
83109core](https://github.com/Igalia/cog#cog) library. As a bonus, it should
84110run on Wayland, X11, and even on a bare display using the <abbr title="Direct
@@ -201,14 +227,16 @@ WPE WebKit has public API for this. Roughly:
201227 [ WebKitURISchemeRequest] ( https://people.igalia.com/aperez/Documentation/wpe-webkit-1.1/class.URISchemeRequest.html ) .
2022283 . The function generates data to be returned as the result of the load,
203229 as a [ GInputStream] ( https://docs.gtk.org/gio/class.InputStream.html )
204- and calls [ webkit_uri_scheme_request_finish()] ( https://people.igalia.com/aperez/Documentation/wpe-webkit-1.1/method.URISchemeRequest.finish.html ) .
230+ and calls [ webkit_uri_scheme_request_finish()] ( https://people.igalia.com/aperez/Documentation/wpe-webkit-1.1/method.URISchemeRequest.finish.html ) . This sends the stream to WebKit as the
231+ response, indicating the length of the response (if known), and the
232+ MIME content type of the data in the stream.
2052334 . WebKit will now read the data from the input stream.
206234
207235
208236### Echoes
209237
210- Let's add an echo handler to our [ minimal browser] ( #intermission )
211- that returns the requested URI as plain text . Registering the scheme is
238+ Let's add an echo handler to our [ minimal browser] ( #intermission ) that
239+ replies back with the requested URI. Registering the scheme is
212240straightforward enough:
213241
214242``` c
@@ -223,6 +251,25 @@ configure_web_context(WebKitWebContext *context)
223251}
224252```
225253
254+ <details>
255+ <summary>What are “user data” and “destroy notify”?</summary><div>
256+
257+ The `userdata` parameter above is a convention used in many C libraries, and
258+ specially in these based on GLib when there are callback functions involved.
259+ It allows the *user* to supply a pointer to arbitrary *data*, which will be
260+ passed later on as a parameter to the callback (`handle_echo_request` in the
261+ example) when it gets invoked later on.
262+
263+ As for the `destroy_notify` parameter, it allows passing a function with the
264+ signature `void func(void*)` (type
265+ [GDestroyNotify](https://docs.gtk.org/glib/callback.DestroyNotify.html)) which
266+ is invoked with `userdata` as the argument once the user data is no longer
267+ needed. In the example above, this callback function would be invoked when the
268+ URI scheme is unregistered. Or, from a different perspective, this callback is
269+ used to *notify* that the user data can now be *destroyed*.
270+
271+ </div></details>
272+
226273One way of implementing `handle_echo_request()` could be wrapping the request
227274URI, which is part of the `WebKitURISchemeRequest` parameter to the handler,
228275stash it into a [GBytes](https://docs.gtk.org/glib/struct.Bytes.html)
@@ -283,6 +330,11 @@ which allows fine-grained control of the response, including setting
283330and finishing the request instead with
284331` webkit_uri_scheme_request_finish_with_response() ` .
285332
333+ Note that WebKit cuts some corners when using CORS with custom URI schemes:
334+ handlers will * not* receive preflight ` OPTIONS ` requests. Instead, the CORS
335+ headers from the replies are inspected, and if access needs to be denied
336+ then the data stream with the response contents is discarded.
337+
286338In addition to providing a complete CORS-enabled custom URI scheme [ example] ( https://gist.github.com/aperezdc/74809a6cd617faf445c22097a47bcb50 ) ,
287339we recommend the [ Will It CORS?] ( https://httptoolkit.com/will-it-cors ) tool
288340to help troubleshoot issues.
@@ -330,6 +382,20 @@ The user script messages part of the
330382 ` window.webkit.messageHandlers.<name>.postMessage() ` , the signal is
331383 emitted, and the native callback functions invoked.
332384
385+ <details >
386+ <summary >Haven't I seen <code >postMessage()</code > elsewhere?</summary ><div >
387+
388+ [ Yes] ( https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage ) ,
389+ [ you] ( https://developer.mozilla.org/en-US/docs/Web/API/Worker/postMessage )
390+ [ have] ( https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/postMessage ) .
391+ The name is the same because it provides a similar functionality (send a
392+ message), it guarantees little (the receiver should validate messages), and
393+ there are [ similar
394+ restrictions] ( https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm )
395+ in the kind of values that can be passed along.
396+
397+ </div ></details >
398+
333399### It's All JavaScript
334400
335401Let's add a feature to our [ minimal browser] ( #intermission ) that will allow
@@ -471,7 +537,7 @@ which can be used to resolve the promise—either on the spot, or
471537asynchronously later on.
472538
473539
474- ### Further Ideas (Bis)
540+ ### Even More Ideas
475541
476542User script messages are a powerful and rather flexible facility to make WPE
477543integrate web content into a complete system. The provided example is rather
0 commit comments