SharedArrayBuffer


public final class SharedArrayBuffer


The Java representation of a JavaScript SharedArrayBuffer.

A SharedArrayBuffer is a region of memory that is shared between the application and the web content running in WebView, which means that writes performed on one side are visible on the other side without copying the data or posting another message.

Use allocate to allocate a new buffer, and send it to web content by posting a WebMessageCompat carrying it, or by replying with postMessage. Web content that is not cross-origin isolated cannot use a SharedArrayBuffer; see setCrossOriginIsolatedAllowlist.

Shared memory provides no notification when its contents change. An application that needs to observe writes made by web content has to read the buffer again, or has to be told by web content through a separate message.

Any web content that holds this buffer can write arbitrary data into it at any time. Treat the contents as untrusted input.

See also
SharedMemory

Summary

Public methods

static @NonNull SharedArrayBuffer
@AnyThread
@RequiresFeature(name = WebViewFeature.WEB_MESSAGE_SHARED_ARRAY_BUFFER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
allocate(@Nullable String name, @IntRange(from = 1) int size)

Allocates a new SharedArrayBuffer of size bytes.

@IntRange(from = 1) int

Returns the size of the shared memory region in bytes.

@NonNull ByteBuffer

Returns a direct ByteBuffer that gives access to the shared memory region.

Public methods

allocate

@AnyThread
@RequiresFeature(name = WebViewFeature.WEB_MESSAGE_SHARED_ARRAY_BUFFER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static @NonNull SharedArrayBuffer allocate(@Nullable String name, @IntRange(from = 1) int size)

Allocates a new SharedArrayBuffer of size bytes.

The memory is allocated as an android.os.SharedMemory region in the application's process, and is accounted against the application.

Parameters
@Nullable String name

an optional name for the underlying shared memory region, which is only used for debugging purposes and does not need to be unique. See create.

@IntRange(from = 1) int size

the size of the buffer in bytes.

Returns
@NonNull SharedArrayBuffer

the newly allocated SharedArrayBuffer.

Throws
android.system.ErrnoException

if the shared memory region could not be allocated.

java.lang.IllegalArgumentException

if size is outside of the supported range.

java.lang.UnsupportedOperationException

if the WEB_MESSAGE_SHARED_ARRAY_BUFFER feature is not supported. This should be checked before use with isFeatureSupported.

getSize

public @IntRange(from = 1) int getSize()

Returns the size of the shared memory region in bytes.

mapAndCreateByteBuffer

public @NonNull ByteBuffer mapAndCreateByteBuffer()

Returns a direct ByteBuffer that gives access to the shared memory region.

The region is mapped into the application's address space the first time this method is called. Every call returns a new ByteBuffer over that same mapping, with its own independent position, limit and mark. All of them, as well as the SharedArrayBuffer held by web content, observe each other's writes.

The returned buffer has a capacity of getSize bytes, and its byte order is nativeOrder, which matches the byte order of JavaScript typed arrays such as Int32Array. Calling duplicate or slice on the returned buffer resets the byte order to BIG_ENDIAN.

Returns
@NonNull ByteBuffer

a direct ByteBuffer backed by the shared memory region.

Throws
android.system.ErrnoException

if the shared memory region could not be mapped.