WebViewCompat
open class WebViewCompat
kotlin.Any | |
↳ | androidx.webkit.WebViewCompat |
Compatibility version of android.webkit.WebView
Summary
Nested classes |
|
---|---|
abstract |
Callback interface supplied to |
Public methods |
|
---|---|
open static Array<WebMessagePortCompat!> |
createWebMessageChannel(@NonNull webview: WebView) Creates a message channel to communicate with JS and returns the message ports that represent the endpoints of this message channel. |
open static PackageInfo? |
getCurrentWebViewPackage(@NonNull context: Context) If WebView has already been loaded into the current process this method will return the package that was used to load it. |
open static Uri |
Returns a URL pointing to the privacy policy for Safe Browsing reporting. |
open static WebChromeClient? |
getWebChromeClient(@NonNull webview: WebView) Gets the WebChromeClient. |
open static WebViewClient |
getWebViewClient(@NonNull webview: WebView) Gets the WebViewClient for the WebView argument. |
open static WebViewRenderProcess? |
getWebViewRenderProcess(@NonNull webview: WebView) Gets the WebView renderer associated with this WebView. |
open static WebViewRenderProcessClient? |
getWebViewRenderProcessClient(@NonNull webview: WebView) Gets the renderer client object associated with this WebView. |
open static Unit |
postVisualStateCallback(@NonNull webview: WebView, requestId: Long, @NonNull callback: WebViewCompat.VisualStateCallback) Posts a |
open static Unit |
postWebMessage(@NonNull webview: WebView, @NonNull message: WebMessageCompat, @NonNull targetOrigin: Uri) Post a message to main frame. |
open static Unit |
setSafeBrowsingWhitelist(@NonNull hosts: MutableList<String!>, @Nullable callback: ValueCallback<Boolean!>?) Sets the list of hosts (domain names/IP addresses) that are exempt from SafeBrowsing checks. |
open static Unit |
setWebViewRenderProcessClient(@NonNull webview: WebView, @NonNull executor: Executor, @NonNull webViewRenderProcessClient: WebViewRenderProcessClient) Sets the renderer client object associated with this WebView. |
open static Unit |
setWebViewRenderProcessClient(@NonNull webview: WebView, @Nullable webViewRenderProcessClient: WebViewRenderProcessClient?) Sets the renderer client object associated with this WebView. |
open static Unit |
startSafeBrowsing(@NonNull context: Context, @Nullable callback: ValueCallback<Boolean!>?) Starts Safe Browsing initialization. |
Public methods
createWebMessageChannel
@NonNull open static fun createWebMessageChannel(@NonNull webview: WebView): Array<WebMessagePortCompat!>
Creates a message channel to communicate with JS and returns the message ports that represent the endpoints of this message channel. The HTML5 message channel functionality is described here
The returned message channels are entangled and already in started state.
This method should only be called if WebViewFeature#isFeatureSupported(String)
returns true for WebViewFeature#CREATE_WEB_MESSAGE_CHANNEL
.
Return | |
---|---|
Array<WebMessagePortCompat!>: an array of size two, containing the two message ports that form the message channel. |
getCurrentWebViewPackage
@Nullable open static fun getCurrentWebViewPackage(@NonNull context: Context): PackageInfo?
If WebView has already been loaded into the current process this method will return the package that was used to load it. Otherwise, the package that would be used if the WebView was loaded right now will be returned; this does not cause WebView to be loaded, so this information may become outdated at any time. The WebView package changes either when the current WebView package is updated, disabled, or uninstalled. It can also be changed through a Developer Setting. If the WebView package changes, any app process that has loaded WebView will be killed. The next time the app starts and loads WebView it will use the new WebView package instead.
Return | |
---|---|
PackageInfo?: the current WebView package, or null if there is none. |
getSafeBrowsingPrivacyPolicyUrl
@NonNull open static fun getSafeBrowsingPrivacyPolicyUrl(): Uri
Returns a URL pointing to the privacy policy for Safe Browsing reporting.
This method should only be called if WebViewFeature#isFeatureSupported(String)
returns true for WebViewFeature#SAFE_BROWSING_PRIVACY_POLICY_URL
.
Return | |
---|---|
Uri: the url pointing to a privacy policy document which can be displayed to users. |
getWebChromeClient
@Nullable open static fun getWebChromeClient(@NonNull webview: WebView): WebChromeClient?
Gets the WebChromeClient.
This method should only be called if WebViewFeature#isFeatureSupported(String)
returns true for WebViewFeature#GET_WEB_CHROME_CLIENT
.
Return | |
---|---|
WebChromeClient?: the WebChromeClient, or null if not yet set |
getWebViewClient
@NonNull open static fun getWebViewClient(@NonNull webview: WebView): WebViewClient
Gets the WebViewClient for the WebView argument.
This method should only be called if WebViewFeature#isFeatureSupported(String)
returns true for WebViewFeature#GET_WEB_VIEW_CLIENT
.
Return | |
---|---|
WebViewClient: the WebViewClient, or a default client if not yet set |
getWebViewRenderProcess
@Nullable open static fun getWebViewRenderProcess(@NonNull webview: WebView): WebViewRenderProcess?
Gets the WebView renderer associated with this WebView.
In Android O and above, WebView may run in "multiprocess" mode. In multiprocess mode, rendering of web content is performed by a sandboxed renderer process separate to the application process. This renderer process may be shared with other WebViews in the application, but is not shared with other application processes.
If WebView is running in multiprocess mode, this method returns a handle to the renderer process associated with the WebView, which can be used to control the renderer process.
This method should only be called if WebViewFeature#isFeatureSupported(String)
returns true for WebViewFeature#GET_WEB_VIEW_RENDERER
.
Return | |
---|---|
WebViewRenderProcess?: the WebViewRenderProcess renderer handle associated with this android.webkit.WebView , or null if WebView is not runing in multiprocess mode. |
getWebViewRenderProcessClient
@Nullable open static fun getWebViewRenderProcessClient(@NonNull webview: WebView): WebViewRenderProcessClient?
Gets the renderer client object associated with this WebView.
This method should only be called if WebViewFeature#isFeatureSupported(String)
returns true for WebViewFeature#WEB_VIEW_RENDERER_CLIENT_BASIC_USAGE
.
Return | |
---|---|
WebViewRenderProcessClient?: the WebViewRenderProcessClient object associated with this WebView, if one has been set via setWebViewRenderProcessClient(WebView,WebViewRenderProcessClient) or null otherwise. |
postVisualStateCallback
open static fun postVisualStateCallback(@NonNull webview: WebView, requestId: Long, @NonNull callback: WebViewCompat.VisualStateCallback): Unit
Posts a VisualStateCallback
, which will be called when the current state of the WebView is ready to be drawn.
Because updates to the DOM are processed asynchronously, updates to the DOM may not immediately be reflected visually by subsequent WebView#onDraw
invocations. The VisualStateCallback
provides a mechanism to notify the caller when the contents of the DOM at the current time are ready to be drawn the next time the WebView
draws.
The next draw after the callback completes is guaranteed to reflect all the updates to the DOM up to the point at which the VisualStateCallback
was posted, but it may also contain updates applied after the callback was posted.
The state of the DOM covered by this API includes the following:
- primitive HTML elements (div, img, span, etc..)
- images
- CSS animations
- WebGL
- canvas
- the video tag
To guarantee that the WebView
will successfully render the first frame after the VisualStateCallback#onComplete
method has been called a set of conditions must be met:
- If the
WebView
's visibility is set toVISIBLE
then * theWebView
must be attached to the view hierarchy. - If the
WebView
's visibility is set toINVISIBLE
then theWebView
must be attached to the view hierarchy and must be madeVISIBLE
from theVisualStateCallback#onComplete
method. - If the
WebView
's visibility is set toGONE
then theWebView
must be attached to the view hierarchy and itsLayoutParams
's width and height need to be set to fixed values and must be madeVISIBLE
from theVisualStateCallback#onComplete
method.
When using this API it is also recommended to enable pre-rasterization if the is off screen to avoid flickering. See android.webkit.WebSettings#setOffscreenPreRaster
for more details and do consider its caveats.
This method should only be called if WebViewFeature#isFeatureSupported(String)
returns true for WebViewFeature#VISUAL_STATE_CALLBACK
.
Parameters | |
---|---|
requestId |
WebView: An id that will be returned in the callback to allow callers to match requests with callbacks. |
callback |
WebView: The callback to be invoked. |
postWebMessage
open static fun postWebMessage(@NonNull webview: WebView, @NonNull message: WebMessageCompat, @NonNull targetOrigin: Uri): Unit
Post a message to main frame. The embedded application can restrict the messages to a certain target origin. See HTML5 spec for how target origin can be used.
A target origin can be set as a wildcard ("*"). However this is not recommended. See the page above for security issues.
This method should only be called if WebViewFeature#isFeatureSupported(String)
returns true for WebViewFeature#POST_WEB_MESSAGE
.
Parameters | |
---|---|
message |
WebView: the WebMessage |
targetOrigin |
WebView: the target origin. |
setSafeBrowsingWhitelist
open static fun setSafeBrowsingWhitelist(@NonNull hosts: MutableList<String!>, @Nullable callback: ValueCallback<Boolean!>?): Unit
Sets the list of hosts (domain names/IP addresses) that are exempt from SafeBrowsing checks. The list is global for all the WebViews.
Each rule should take one of these:
Rule | Example | Matches Subdomain |
---|---|---|
HOSTNAME | example.com | Yes |
.HOSTNAME | .example.com | No |
IPV4_LITERAL | 192.168.1.1 | No |
IPV6_LITERAL_WITH_BRACKETS | [10:20:30:40:50:60:70:80] | No |
All other rules, including wildcards, are invalid.
The correct syntax for hosts is defined by RFC 3986.
This method should only be called if WebViewFeature#isFeatureSupported(String)
returns true for WebViewFeature#SAFE_BROWSING_WHITELIST
.
Parameters | |
---|---|
hosts |
MutableList<String!>: the list of hosts |
callback |
MutableList<String!>: will be called with true if hosts are successfully added to the whitelist. It will be called with false if any hosts are malformed. The callback will be run on the UI thread |
setWebViewRenderProcessClient
open static fun setWebViewRenderProcessClient(@NonNull webview: WebView, @NonNull executor: Executor, @NonNull webViewRenderProcessClient: WebViewRenderProcessClient): Unit
Sets the renderer client object associated with this WebView.
The renderer client encapsulates callbacks relevant to WebView renderer state. See WebViewRenderProcessClient
for details.
Although many WebView instances may share a single underlying renderer, and renderers may live either in the application process, or in a sandboxed process that is isolated from the application process, instances of WebViewRenderProcessClient
are set per-WebView. Callbacks represent renderer events from the perspective of this WebView, and may or may not be correlated with renderer events affecting other WebViews.
The renderer client encapsulates callbacks relevant to WebView renderer state. See WebViewRenderProcessClient
for details.
Although many WebView instances may share a single underlying renderer, and renderers may live either in the application process, or in a sandboxed process that is isolated from the application process, instances of WebViewRenderProcessClient
are set per-WebView. Callbacks represent renderer events from the perspective of this WebView, and may or may not be correlated with renderer events affecting other WebViews.
This method should only be called if WebViewFeature#isFeatureSupported(String)
returns true for WebViewFeature#WEB_VIEW_RENDERER_CLIENT_BASIC_USAGE
.
Parameters | |
---|---|
webview |
WebView: the WebView on which to monitor responsiveness. |
executor |
WebView: the Executor that will be used to execute callbacks. |
webViewRenderProcessClient |
WebView: the WebViewRenderProcessClient to set for callbacks. |
setWebViewRenderProcessClient
open static fun setWebViewRenderProcessClient(@NonNull webview: WebView, @Nullable webViewRenderProcessClient: WebViewRenderProcessClient?): Unit
Sets the renderer client object associated with this WebView.
See WebViewCompat#setWebViewRenderProcessClient(WebView,Executor,WebViewRenderProcessClient)
for details, with the following differences:
Callbacks will execute directly on the thread on which this WebView was instantiated.
Passing null
for webViewRenderProcessClient
will clear the renderer client object for this WebView.
This method should only be called if WebViewFeature#isFeatureSupported(String)
returns true for WebViewFeature#WEB_VIEW_RENDERER_CLIENT_BASIC_USAGE
.
Parameters | |
---|---|
webview |
WebView: the WebView on which to monitor responsiveness. |
webViewRenderProcessClient |
WebView: the WebViewRenderProcessClient to set for callbacks. |
startSafeBrowsing
open static fun startSafeBrowsing(@NonNull context: Context, @Nullable callback: ValueCallback<Boolean!>?): Unit
Starts Safe Browsing initialization.
URL loads are not guaranteed to be protected by Safe Browsing until after callback
is invoked with true
. Safe Browsing is not fully supported on all devices. For those devices callback
will receive false
.
This should not be called if Safe Browsing has been disabled by manifest tag or
. This prepares resources used for Safe Browsing.
This should be called with the Application Context (and will always use the Application context to do its work regardless).
This method should only be called if WebViewFeature#isFeatureSupported(String)
returns true for WebViewFeature#START_SAFE_BROWSING
.
Parameters | |
---|---|
context |
Context: Application Context. |
callback |
Context: will be called on the UI thread with true if initialization is successful, false otherwise. |