Class WebContext
- All Implemented Interfaces:
Proxy
WebKitWebView
<!-- -->s
The WebKitWebContext
manages all aspects common to all
WebKitWebView
<!-- -->s.
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.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Functional interface declaration of theAutomationStartedCallback
callback.static class
WebContext.Builder<B extends WebContext.Builder<B>>
Inner class implementing a builder pattern to construct a GObject with properties.static interface
Functional interface declaration of theInitializeNotificationPermissionsCallback
callback.static interface
Functional interface declaration of theInitializeWebProcessExtensionsCallback
callback.static interface
Functional interface declaration of theUserMessageReceivedCallback
callback.static class
Nested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass
-
Constructor Summary
ConstructorDescriptionCreate a newWebKitWebContext
.WebContext
(MemorySegment address) Create a WebContext proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addPathToSandbox
(String path, boolean readOnly) Adds a path to be mounted in the sandbox.protected WebContext
asParent()
Returns this instance as if it were its parent type.static WebContext.Builder
<? extends WebContext.Builder> builder()
AWebContext.Builder
object constructs aWebContext
with the specified properties.void
emitAutomationStarted
(AutomationSession session) Emits the "automation-started" signal.void
Emits the "initialize-notification-permissions" signal.void
Emits the "initialize-web-process-extensions" signal.boolean
emitUserMessageReceived
(UserMessage message) Emits the "user-message-received" signal.Returns the current cache model.static WebContext
Gets the default web context.Get theWebKitGeolocationManager
of this WebContext.Get theWebKitNetworkSession
used for automation sessions started in this WebContext.Get theWebKitSecurityManager
of this WebContext.boolean
Get whether spell checking feature is currently enabled.String[]
Get the the list of spell checking languages.Get theWebKitWebContext
:time-zone-override property.static Type
getType()
Get the GType of the WebContext classvoid
initializeNotificationPermissions
(List<SecurityOrigin> allowedOrigins, List<SecurityOrigin> disallowedOrigins) Sets initial desktop notification permissions for the this WebContext.boolean
Get whether automation is allowed in this WebContext.This signal is emitted when a new automation request is made.This signal is emitted when aWebKitWebContext
needs to set initial notification permissions for a web process.This signal is emitted when a new web process is about to be launched.This signal is emitted when aWebKitUserMessage
is received from a web process extension.void
registerUriScheme
(String scheme, URISchemeRequestCallback callback) Registerscheme
in this WebContext.void
sendMessageToAllExtensions
(UserMessage message) Sendmessage
to all web process extensions associated to this WebContext.void
setAutomationAllowed
(boolean allowed) Set whether automation is allowed in this WebContext.void
setCacheModel
(CacheModel cacheModel) Specifies a usage model for WebViews.void
setPreferredLanguages
(@Nullable String[] languages) Set the list of preferred languages.void
setSpellCheckingEnabled
(boolean enabled) Enable or disable the spell checking feature.void
setSpellCheckingLanguages
(String[] languages) Set the list of spell checking languages to be used for spell checking.void
setWebProcessExtensionsDirectory
(String directory) Set the directory where WebKit will look for web process extensions.void
Set user data to be passed to Web Extensions on initialization.Methods inherited from class org.gnome.gobject.GObject
addToggleRef, addWeakPointer, bindProperty, bindProperty, bindProperty, bindPropertyFull, bindPropertyFull, bindPropertyWithClosures, bindPropertyWithClosures, compatControl, connect, connect, connect, constructed, disconnect, dispatchPropertiesChanged, dispose, dupData, dupQdata, emit, emitNotify, finalize_, forceFloating, freezeNotify, get, getData, getMemoryLayout, getProperty, getProperty, getProperty, getQdata, getv, interfaceFindProperty, interfaceInstallProperty, interfaceListProperties, isFloating, newInstance, newInstance, newInstance, newInstance, newv, notify_, notify_, notifyByPspec, onNotify, ref, refSink, removeToggleRef, removeWeakPointer, replaceData, replaceQdata, runDispose, set, setData, setDataFull, setProperty, setProperty, setProperty, setQdata, setQdataFull, setv, stealData, stealQdata, takeRef, thawNotify, unref, watchClosure, weakRef, weakUnref, withProperties
Methods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, getPrivate, readGClass, writeGClass
Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
WebContext
Create a WebContext proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
WebContext
Create a newWebKitWebContext
.- Throws:
UnsupportedPlatformException
- when run on a platform other than linux
-
-
Method Details
-
getType
-
asParent
Returns this instance as if it were its parent type. This is mostly synonymous to the Javasuper
keyword, but will set the native typeclass function pointers to the parent type. When overriding a native virtual method in Java, "chaining up" withsuper.methodName()
doesn't work, because it invokes the overridden function pointer again. To chain up, callasParent().methodName()
. This will call the native function pointer of this virtual method in the typeclass of the parent type. -
getDefault
Gets the default web context.- Returns:
- a
WebKitWebContext
- Throws:
UnsupportedPlatformException
- when run on a platform other than linux
-
addPathToSandbox
Adds a path to be mounted in the sandbox.path
must exist before any web process has been created; otherwise, it will be silently ignored. 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 sandboxreadOnly
- iftrue
the path will be read-only
-
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
Get theWebKitGeolocationManager
of this WebContext.- Returns:
- the
WebKitGeolocationManager
of this WebContext.
-
getNetworkSessionForAutomation
Get theWebKitNetworkSession
used for automation sessions started in this WebContext.- Returns:
- a
WebKitNetworkSession
, ornull
if automation is not enabled
-
getSecurityManager
Get theWebKitSecurityManager
of this WebContext.- Returns:
- the
WebKitSecurityManager
of this WebContext.
-
getSpellCheckingEnabled
public boolean getSpellCheckingEnabled()Get whether spell checking feature is currently enabled.- Returns:
true
If spell checking is enabled, orfalse
otherwise.
-
getSpellCheckingLanguages
Get the the list of spell checking languages.Get the the list of spell checking languages associated with this WebContext, 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, ornull
otherwise.
-
getTimeZoneOverride
Get theWebKitWebContext
:time-zone-override property. -
initializeNotificationPermissions
public void initializeNotificationPermissions(List<SecurityOrigin> allowedOrigins, List<SecurityOrigin> disallowedOrigins) Sets initial desktop notification permissions for the this WebContext.allowedOrigins
anddisallowedOrigins
must each beGList
ofWebKitSecurityOrigin
objects representing origins that will, respectively, either always or never have permission to show desktop notifications. NoWebKitNotificationPermissionRequest
will ever be generated for any of the security origins represented inallowedOrigins
ordisallowedOrigins
. 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
- aGList
of security originsdisallowedOrigins
- aGList
of security origins
-
isAutomationAllowed
public boolean isAutomationAllowed()Get whether automation is allowed in this WebContext.See also webkit_web_context_set_automation_allowed().
- Returns:
true
if automation is allowed orfalse
otherwise.
-
registerUriScheme
Registerscheme
in this WebContext.Register
scheme
in this WebContext, so that when an URI request withscheme
is made in theWebKitWebContext
, theWebKitURISchemeRequestCallback
registered will be called with aWebKitURISchemeRequest
. It is possible to handle URI scheme requests asynchronously, by calling g_object_ref() on theWebKitURISchemeRequest
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 registercallback
- aWebKitURISchemeRequestCallback
-
sendMessageToAllExtensions
Sendmessage
to all web process extensions associated to this WebContext.If
message
is floating, it's consumed.- Parameters:
message
- aWebKitUserMessage
-
setAutomationAllowed
public void setAutomationAllowed(boolean allowed) Set whether automation is allowed in this WebContext.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 passingtrue
to enable it.Note that only one
WebKitWebContext
can have automation enabled, so this will do nothing if there's anotherWebKitWebContext
with automation already enabled.- Parameters:
allowed
- value to set
-
setCacheModel
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 specifyingCacheModel.DOCUMENT_VIEWER
. The default value isCacheModel.WEB_BROWSER
.- Parameters:
cacheModel
- aWebKitCacheModel
-
setPreferredLanguages
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 theWebKitWebContext
. - Setting the values of
navigator.language
andnavigator.languages
. - The first item in the list sets the default locale for JavaScript
Intl
functions.
- Parameters:
languages
- anull
-terminated list of language identifiers
- Determining how to build the
-
setSpellCheckingEnabled
public void setSpellCheckingEnabled(boolean enabled) Enable or disable the spell checking feature.- Parameters:
enabled
- Value to be set
-
setSpellCheckingLanguages
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
- anull
-terminated list of spell checking languages
-
setWebProcessExtensionsDirectory
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.- Parameters:
directory
- the directory to add
-
setWebProcessExtensionsInitializationUserData
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 toWebKitWebContext
::initialize-web-process-extensions to call this method before anything is loaded.- Parameters:
userData
- aGVariant
-
onAutomationStarted
public SignalConnection<WebContext.AutomationStartedCallback> onAutomationStarted(WebContext.AutomationStartedCallback handler) This signal is emitted when a new automation request is made. Note that it will never be emitted if automation is not enabled incontext
, 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
- See Also:
-
emitAutomationStarted
Emits the "automation-started" signal. SeeonAutomationStarted(org.gnome.webkit.WebContext.AutomationStartedCallback)
. -
onInitializeNotificationPermissions
public SignalConnection<WebContext.InitializeNotificationPermissionsCallback> onInitializeNotificationPermissions(WebContext.InitializeNotificationPermissionsCallback handler) This signal is emitted when aWebKitWebContext
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
- See Also:
-
emitInitializeNotificationPermissions
public void emitInitializeNotificationPermissions()Emits the "initialize-notification-permissions" signal. SeeonInitializeNotificationPermissions(org.gnome.webkit.WebContext.InitializeNotificationPermissionsCallback)
. -
onInitializeWebProcessExtensions
public SignalConnection<WebContext.InitializeWebProcessExtensionsCallback> onInitializeWebProcessExtensions(WebContext.InitializeWebProcessExtensionsCallback handler) 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
- See Also:
-
emitInitializeWebProcessExtensions
public void emitInitializeWebProcessExtensions()Emits the "initialize-web-process-extensions" signal. SeeonInitializeWebProcessExtensions(org.gnome.webkit.WebContext.InitializeWebProcessExtensionsCallback)
. -
onUserMessageReceived
public SignalConnection<WebContext.UserMessageReceivedCallback> onUserMessageReceived(WebContext.UserMessageReceivedCallback handler) This signal is emitted when aWebKitUserMessage
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 returningtrue
.- Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitUserMessageReceived
Emits the "user-message-received" signal. SeeonUserMessageReceived(org.gnome.webkit.WebContext.UserMessageReceivedCallback)
. -
builder
AWebContext.Builder
object constructs aWebContext
with the specified properties. Use the variousset...()
methods to set properties, and finish construction withWebContext.Builder.build()
.
-