Class WebContext

All Implemented Interfaces:
Proxy

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

Manages aspects common to all WebKitWebViews

The WebKitWebContext manages all aspects common to all WebKitWebViews.

You can define the WebKitCacheModel with webkit_web_context_set_cache_model(), depending on the needs of your application. You can access the WebKitSecurityManager to specify the behaviour of your application regarding security using webkit_web_context_get_security_manager().

It is also possible to change your preferred language or enable spell checking, using webkit_web_context_set_preferred_languages(), webkit_web_context_set_spell_checking_languages() and webkit_web_context_set_spell_checking_enabled().

You can use webkit_web_context_register_uri_scheme() to register custom URI schemes, and manage several other settings.

TLS certificate validation failure is now treated as a transport error by default. To handle TLS failures differently, you can connect to WebKitWebView::load-failed-with-tls-errors. Alternatively, you can use webkit_web_context_set_tls_errors_policy() to set the policy TLSErrorsPolicy.IGNORE; however, this is not appropriate for Internet applications.

  • Constructor Details

    • WebContext

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

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

    • getType

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

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

      protected WebContext 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
    • getDefault

      public static WebContext getDefault()
      Gets the default web context.
      Returns:
      a WebKitWebContext
    • addPathToSandbox

      public void addPathToSandbox(String path, boolean readOnly)

      Adds a path to be mounted in the sandbox.

      path must exist before any web process has been created. It is a fatal error to add paths after a web process has been spawned.

      Paths under /sys, /proc, and /dev are invalid. Attempting to add all of / is not valid. Since 2.40, adding the user's entire home directory or /home is also not valid.

      See also webkit_web_context_set_sandbox_enabled()

      Parameters:
      path - an absolute path to mount in the sandbox
      readOnly - if true the path will be read-only
      Since:
      2.26
    • getCacheModel

      public CacheModel getCacheModel()

      Returns the current cache model.

      For more information about this value check the documentation of the function webkit_web_context_set_cache_model().

      Returns:
      the current WebKitCacheModel
    • getGeolocationManager

      public GeolocationManager getGeolocationManager()
      Get the WebKitGeolocationManager of context.
      Returns:
      the WebKitGeolocationManager of context.
      Since:
      2.26
    • getNetworkSessionForAutomation

      public @Nullable NetworkSession getNetworkSessionForAutomation()
      Get the WebKitNetworkSession used for automation sessions started in context.
      Returns:
      a WebKitNetworkSession, or null if automation is not enabled
      Since:
      2.40
    • getSecurityManager

      public SecurityManager getSecurityManager()
      Get the WebKitSecurityManager of context.
      Returns:
      the WebKitSecurityManager of context.
    • getSpellCheckingEnabled

      public boolean getSpellCheckingEnabled()
      Get whether spell checking feature is currently enabled.
      Returns:
      true If spell checking is enabled, or false otherwise.
    • getSpellCheckingLanguages

      public String[] getSpellCheckingLanguages()

      Get the the list of spell checking languages.

      Get the the list of spell checking languages associated with context, or null if no languages have been previously set.

      See webkit_web_context_set_spell_checking_languages() for more details on the format of the languages in the list.

      Returns:
      A null-terminated array of languages if available, or null otherwise.
    • getTimeZoneOverride

      public String getTimeZoneOverride()
      Get the WebKitWebContext:time-zone-override property.
      Since:
      2.38
    • initializeNotificationPermissions

      public void initializeNotificationPermissions(List<SecurityOrigin> allowedOrigins, List<SecurityOrigin> disallowedOrigins)

      Sets initial desktop notification permissions for the context.

      allowedOrigins and disallowedOrigins must each be GList of WebKitSecurityOrigin objects representing origins that will, respectively, either always or never have permission to show desktop notifications. No WebKitNotificationPermissionRequest will ever be generated for any of the security origins represented in allowedOrigins or disallowedOrigins. This function is necessary because some webpages proactively check whether they have permission to display notifications without ever creating a permission request.

      This function only affects web processes that have not already been created. The best time to call it is when handling WebKitWebContext::initialize-notification-permissions so as to ensure that new web processes receive the most recent set of permissions.

      Parameters:
      allowedOrigins - a GList of security origins
      disallowedOrigins - a GList of security origins
      Since:
      2.16
    • isAutomationAllowed

      public boolean isAutomationAllowed()

      Get whether automation is allowed in context.

      See also webkit_web_context_set_automation_allowed().

      Returns:
      true if automation is allowed or false otherwise.
      Since:
      2.18
    • registerUriScheme

      public void registerUriScheme(String scheme, @Nullable URISchemeRequestCallback callback)

      Register scheme in context.

      Register scheme in context, so that when an URI request with scheme is made in the WebKitWebContext, the WebKitURISchemeRequestCallback registered will be called with a WebKitURISchemeRequest. It is possible to handle URI scheme requests asynchronously, by calling g_object_ref() on the WebKitURISchemeRequest and calling webkit_uri_scheme_request_finish() later when the data of the request is available or webkit_uri_scheme_request_finish_error() in case of error.

      static void
      about_uri_scheme_request_cb (WebKitURISchemeRequest *request,
                                   gpointer                user_data)
      {
          GInputStream *stream;
          gsize         stream_length;
          const gchar  *path = webkit_uri_scheme_request_get_path (request);
      
          if (!g_strcmp0 (path, "memory")) {
              // Create a GInputStream with the contents of memory about page, and set its length to stream_length
          } else if (!g_strcmp0 (path, "applications")) {
              // Create a GInputStream with the contents of applications about page, and set its length to stream_length
          } else if (!g_strcmp0 (path, "example")) {
              gchar *contents = g_strdup_printf ("<html><body><p>Example about page</p></body></html>");
              stream_length = strlen (contents);
              stream = g_memory_input_stream_new_from_data (contents, stream_length, g_free);
          } else {
              GError *error = g_error_new (ABOUT_HANDLER_ERROR, ABOUT_HANDLER_ERROR_INVALID, "Invalid about:%s page.", path);
              webkit_uri_scheme_request_finish_error (request, error);
              g_error_free (error);
              return;
          }
          webkit_uri_scheme_request_finish (request, stream, stream_length, "text/html");
          g_object_unref (stream);
      }
      
      Parameters:
      scheme - the network scheme to register
      callback - a WebKitURISchemeRequestCallback
    • sendMessageToAllExtensions

      public void sendMessageToAllExtensions(UserMessage message)

      Send message to all web process extensions associated to context.

      If message is floating, it's consumed.

      Parameters:
      message - a WebKitUserMessage
      Since:
      2.28
    • setAutomationAllowed

      public void setAutomationAllowed(boolean allowed)

      Set whether automation is allowed in context.

      When automation is enabled the browser could be controlled by another process by requesting an automation session. When a new automation session is requested the signal WebKitWebContext::automation-started is emitted. Automation is disabled by default, so you need to explicitly call this method passing true to enable it.

      Note that only one WebKitWebContext can have automation enabled, so this will do nothing if there's another WebKitWebContext with automation already enabled.

      Parameters:
      allowed - value to set
      Since:
      2.18
    • setCacheModel

      public void setCacheModel(CacheModel cacheModel)

      Specifies a usage model for WebViews.

      Specifies a usage model for WebViews, which WebKit will use to determine its caching behavior. All web views follow the cache model. This cache model determines the RAM and disk space to use for caching previously viewed content .

      Research indicates that users tend to browse within clusters of documents that hold resources in common, and to revisit previously visited documents. WebKit and the frameworks below it include built-in caches that take advantage of these patterns, substantially improving document load speed in browsing situations. The WebKit cache model controls the behaviors of all of these caches, including various WebCore caches.

      Browsers can improve document load speed substantially by specifying CacheModel.WEB_BROWSER. Applications without a browsing interface can reduce memory usage substantially by specifying CacheModel.DOCUMENT_VIEWER. The default value is CacheModel.WEB_BROWSER.

      Parameters:
      cacheModel - a WebKitCacheModel
    • setPreferredLanguages

      public void setPreferredLanguages(@Nullable String @Nullable [] languages)

      Set the list of preferred languages.

      Set the list of preferred languages, sorted from most desirable to least desirable. The list will be used in the following ways:

      • Determining how to build the Accept-Language HTTP header that will be included in the network requests started by the WebKitWebContext.
      • Setting the values of navigator.language and navigator.languages.
      • The first item in the list sets the default locale for JavaScript Intl functions.
      Parameters:
      languages - a null-terminated list of language identifiers
    • setSpellCheckingEnabled

      public void setSpellCheckingEnabled(boolean enabled)
      Enable or disable the spell checking feature.
      Parameters:
      enabled - Value to be set
    • setSpellCheckingLanguages

      public void setSpellCheckingLanguages(@Nullable String @Nullable [] languages)

      Set the list of spell checking languages to be used for spell checking.

      The locale string typically is in the form lang_COUNTRY, where lang is an ISO-639 language code, and COUNTRY is an ISO-3166 country code. For instance, sv_FI for Swedish as written in Finland or pt_BR for Portuguese as written in Brazil.

      You need to call this function with a valid list of languages at least once in order to properly enable the spell checking feature in WebKit.

      Parameters:
      languages - a null-terminated list of spell checking languages
    • setWebProcessExtensionsDirectory

      public void setWebProcessExtensionsDirectory(String directory)

      Set the directory where WebKit will look for web process extensions.

      This method must be called before loading anything in this context, otherwise it will not have any effect. You can connect to WebKitWebContext::initialize-web-process-extensions to call this method before anything is loaded.

      If your web process extension is installed to an unusual location, then you may also need to call webkit_web_context_add_path_to_sandbox().

      Parameters:
      directory - the directory to add
    • setWebProcessExtensionsInitializationUserData

      public void setWebProcessExtensionsInitializationUserData(Variant userData)

      Set user data to be passed to Web Extensions on initialization.

      The data will be passed to the WebKitWebProcessExtensionInitializeWithUserDataFunction. This method must be called before loading anything in this context, otherwise it will not have any effect. You can connect to WebKitWebContext::initialize-web-process-extensions to call this method before anything is loaded.

      Parameters:
      userData - a GVariant
      Since:
      2.4
    • onAutomationStarted

      This signal is emitted when a new automation request is made. Note that it will never be emitted if automation is not enabled in context, see webkit_web_context_set_automation_allowed() for more details.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      Since:
      2.18
      See Also:
    • emitAutomationStarted

      public void emitAutomationStarted(@Nullable AutomationSession session)
      Emits the "automation-started" signal. See onAutomationStarted(WebContext.AutomationStartedCallback).
    • onInitializeNotificationPermissions

      This signal is emitted when a WebKitWebContext needs to set initial notification permissions for a web process. It is emitted when a new web process is about to be launched, and signals the most appropriate moment to use webkit_web_context_initialize_notification_permissions(). If no notification permissions have changed since the last time this signal was emitted, then there is no need to call webkit_web_context_initialize_notification_permissions() again.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      Since:
      2.16
      See Also:
    • emitInitializeNotificationPermissions

      public void emitInitializeNotificationPermissions()
      Emits the "initialize-notification-permissions" signal. See onInitializeNotificationPermissions(WebContext.InitializeNotificationPermissionsCallback).
    • onInitializeWebProcessExtensions

      This signal is emitted when a new web process is about to be launched. It signals the most appropriate moment to use webkit_web_context_set_web_process_extensions_initialization_user_data() and webkit_web_context_set_web_process_extensions_directory().
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      Since:
      2.4
      See Also:
    • emitInitializeWebProcessExtensions

      public void emitInitializeWebProcessExtensions()
      Emits the "initialize-web-process-extensions" signal. See onInitializeWebProcessExtensions(WebContext.InitializeWebProcessExtensionsCallback).
    • onUserMessageReceived

      This signal is emitted when a WebKitUserMessage is received from a web process extension. You can reply to the message using webkit_user_message_send_reply().

      You can handle the user message asynchronously by calling g_object_ref() on message and returning true.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      Since:
      2.28
      See Also:
    • emitUserMessageReceived

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

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