Class WebProcessExtension

All Implemented Interfaces:
Proxy

@Generated("org.javagi.JavaGI") public final class WebProcessExtension extends GObject

Represents an extension of the web process.

WebKitWebProcessExtension is a loadable module for the web process. It allows you to execute code in the web process and being able to use the DOM API, to change any request or to inject custom JavaScript code, for example.

To create a WebKitWebProcessExtension you should write a module with an initialization function that could be either webkit_web_process_extension_initialize() with prototype WebKitWebProcessExtensionInitializeFunction or webkit_web_process_extension_initialize_with_user_data() with prototype WebKitWebProcessExtensionInitializeWithUserDataFunction. This function has to be public and it has to use the G_MODULE_EXPORT macro. It is called when the web process is initialized.

static void
web_page_created_callback (WebKitWebProcessExtension *extension,
                           WebKitWebPage             *web_page,
                           gpointer                   user_data)
{
    g_print ("Page %d created for %s\\n",
             webkit_web_page_get_id (web_page),
             webkit_web_page_get_uri (web_page));
}

G_MODULE_EXPORT void
webkit_web_process_extension_initialize (WebKitWebProcessExtension *extension)
{
    g_signal_connect (extension, "page-created",
                      G_CALLBACK (web_page_created_callback),
                      NULL);
}

The previous piece of code shows a trivial example of an extension that notifies when a WebKitWebPage is created.

WebKit has to know where it can find the created WebKitWebProcessExtension. To do so you should use the webkit_web_context_set_web_extensions_directory() function. The signal WebKitWebContext::initialize-web-extensions is the recommended place to call it.

To provide the initialization data used by the webkit_web_process_extension_initialize_with_user_data() function, you have to call webkit_web_context_set_web_extensions_initialization_user_data() with the desired data as parameter. You can see an example of this in the following piece of code:

#define WEB_EXTENSIONS_DIRECTORY // ...

static void
initialize_web_extensions (WebKitWebContext *context,
                           gpointer          user_data)
{
  // Web Extensions get a different ID for each Web Process
  static guint32 unique_id = 0;

  webkit_web_context_set_web_extensions_directory (
     context, WEB_EXTENSIONS_DIRECTORY);
  webkit_web_context_set_web_extensions_initialization_user_data (
     context, g_variant_new_uint32 (unique_id++));
}

int main (int argc, char **argv)
{
  g_signal_connect (webkit_web_context_get_default (),
                   "initialize-web-extensions",
                    G_CALLBACK (initialize_web_extensions),
                    NULL);

  GtkWidget *view = webkit_web_view_new ();

  // ...
}
Since:
2.40
  • Constructor Details

    • WebProcessExtension

      public WebProcessExtension(MemorySegment address)
      Create a WebProcessExtension instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • WebProcessExtension

      public WebProcessExtension()
      Create a new WebProcessExtension.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the WebProcessExtension class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected WebProcessExtension asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class GObject
      Returns:
      the instance as if it were its parent type
    • getPage

      public WebPage getPage(long pageId)
      Get the web page of the given pageId.
      Parameters:
      pageId - the identifier of the WebKitWebPage to get
      Returns:
      the WebKitWebPage for the given pageId, or null if the identifier doesn't correspond to an existing web page.
      Since:
      2.40
    • sendMessageToContext

      public void sendMessageToContext(UserMessage message, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)

      Send message to the WebKitWebContext corresponding to extension. If message is floating, it's consumed.

      If you don't expect any reply, or you simply want to ignore it, you can pass null as calback. When the operation is finished, callback will be called. You can then call webkit_web_process_extension_send_message_to_context_finish() to get the message reply.

      Parameters:
      message - a WebKitUserMessage
      cancellable - a GCancellable or null to ignore
      callback - A GAsyncReadyCallback to call when the request is satisfied or null
      Since:
      2.40
    • sendMessageToContextFinish

      public UserMessage sendMessageToContextFinish(AsyncResult result) throws GErrorException
      Finish an asynchronous operation started with webkit_web_process_extension_send_message_to_context().
      Parameters:
      result - a GAsyncResult
      Returns:
      a WebKitUserMessage with the reply or null in case of error.
      Throws:
      GErrorException - see GError
      Since:
      2.40
    • onPageCreated

      This signal is emitted when a new WebKitWebPage is created in the Web Process.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      Since:
      2.40
      See Also:
    • emitPageCreated

      public void emitPageCreated(@Nullable WebPage webPage)
      Emits the "page-created" signal. See onPageCreated(WebProcessExtension.PageCreatedCallback).
    • onUserMessageReceived

      This signal is emitted when a WebKitUserMessage is received from the WebKitWebContext corresponding to extension. Messages sent by WebKitWebContext are always broadcasted to all web extensions and they can't be replied to. Calling webkit_user_message_send_reply() will do nothing.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      Since:
      2.40
      See Also:
    • emitUserMessageReceived

      public void emitUserMessageReceived(@Nullable UserMessage message)
      Emits the "user-message-received" signal. See onUserMessageReceived(WebProcessExtension.UserMessageReceivedCallback).
    • builder

      public static WebProcessExtension.Builder<? extends WebProcessExtension.Builder> builder()
      A WebProcessExtension.Builder object constructs a WebProcessExtension with the specified properties. Use the various set...() methods to set properties, and finish construction with WebProcessExtension.Builder.build().
      Returns:
      the builder object