WebMessagePortCompat

abstract class WebMessagePortCompat
kotlin.Any
   ↳ androidx.webkit.WebMessagePortCompat

The Java representation of the HTML5 message ports.

A Message port represents one endpoint of a Message Channel. In Android webview, there is no separate Message Channel object. When a message channel is created, both ports are tangled to each other and started, and then returned in a MessagePort array, see WebViewCompat#createWebMessageChannel for creating a message channel.

When a message port is first created or received via transfer, it does not have a WebMessageCallback to receive web messages. The messages are queued until a WebMessageCallback is set.

A message port should be closed when it is not used by the embedder application anymore. A closed port cannot be transferred or cannot be reopened to send messages. Close can be called multiple times.

When a port is transferred to JS, it cannot be used to send or receive messages at the Java side anymore. Different from HTML5 Spec, a port cannot be transferred if one of these has ever happened: i. a message callback was set, ii. a message was posted on it. A transferred port cannot be closed by the application, since the ownership is also transferred.

It is possible to transfer both ports of a channel to JS, for example for communication between subframes.

Summary

Nested classes

abstract

The listener for handling MessagePort events.

Public methods

abstract Unit

Close the message port and free any resources associated with it.

abstract Unit
postMessage(@NonNull message: WebMessageCompat)

Post a WebMessage to the entangled port.

abstract Unit

Sets a callback to receive message events on the main thread.

abstract Unit

Sets a callback to receive message events on the handler that is provided by the application.

Public methods

close

abstract fun close(): Unit

Close the message port and free any resources associated with it.

This method should only be called if WebViewFeature#isFeatureSupported(String) returns true for WebViewFeature#WEB_MESSAGE_PORT_CLOSE.

postMessage

abstract fun postMessage(@NonNull message: WebMessageCompat): Unit

Post a WebMessage to the entangled port.

This method should only be called if WebViewFeature#isFeatureSupported(String) returns true for WebViewFeature#WEB_MESSAGE_PORT_POST_MESSAGE.

Parameters
message WebMessageCompat: the message from Java to JS.
Exceptions
IllegalStateException If message port is already transferred or closed.

setWebMessageCallback

abstract fun setWebMessageCallback(@NonNull callback: WebMessagePortCompat.WebMessageCallbackCompat): Unit

Sets a callback to receive message events on the main thread.

This method should only be called if WebViewFeature#isFeatureSupported(String) returns true for WebViewFeature#WEB_MESSAGE_PORT_SET_MESSAGE_CALLBACK.

Parameters
callback WebMessagePortCompat.WebMessageCallbackCompat: the message callback.

setWebMessageCallback

abstract fun setWebMessageCallback(@Nullable handler: Handler?, @NonNull callback: WebMessagePortCompat.WebMessageCallbackCompat): Unit

Sets a callback to receive message events on the handler that is provided by the application. If the handler is null the message events are received on the main thread.

This method should only be called if WebViewFeature#isFeatureSupported(String) returns true for WebViewFeature#WEB_MESSAGE_PORT_SET_MESSAGE_CALLBACK.

Parameters
handler Handler?: the handler to receive the message events.
callback Handler?: the message callback.