Freigeben über


CoreWebView2.PostSharedBufferToScript Method

Definition

Share a shared buffer object with script of the main frame in the WebView.

public void PostSharedBufferToScript (Microsoft.Web.WebView2.Core.CoreWebView2SharedBuffer sharedBuffer, Microsoft.Web.WebView2.Core.CoreWebView2SharedBufferAccess access, string additionalDataAsJson);
member this.PostSharedBufferToScript : Microsoft.Web.WebView2.Core.CoreWebView2SharedBuffer * Microsoft.Web.WebView2.Core.CoreWebView2SharedBufferAccess * string -> unit
Public Sub PostSharedBufferToScript (sharedBuffer As CoreWebView2SharedBuffer, access As CoreWebView2SharedBufferAccess, additionalDataAsJson As String)

Parameters

sharedBuffer
CoreWebView2SharedBuffer

The CoreWebView2SharedBuffer object to be shared with script.

additionalDataAsJson
String

Additional data to be send to script. If it is not null or empty string, and it is not a valid JSON string, ArgumentException will be thrown.

Examples

SafeHandle fileMappingHandle;
using (CoreWebView2SharedBuffer buffer = WebViewEnvironment.CreateSharedBuffer(bufferSize))
{
    fileMappingHandle = buffer.FileMappingHandle;
    AssertCondition(fileMappingHandle.IsInvalid == false, "FileMappingHandle of a valid shared buffer should be valid");
    using (Stream stream = buffer.OpenStream())
    {
        using (StreamWriter writer = new StreamWriter(stream))
        {
            writer.Write(data);
        }
    }
    string additionalDataAsJson = "{\"myBufferType\":\"bufferType1\"}";
    if (forWebView)
    {
        ((CoreWebView2)sender).PostSharedBufferToScript(buffer, CoreWebView2SharedBufferAccess.ReadOnly, additionalDataAsJson);
    }
    else
    {
        ((CoreWebView2Frame)sender).PostSharedBufferToScript(buffer, CoreWebView2SharedBufferAccess.ReadOnly, additionalDataAsJson);
    }
}
AssertCondition(fileMappingHandle.IsInvalid == true, "FileMappingHandle of a disposed shared buffer should be invalid");

Remarks

The script will receive a sharedbufferreceived event from chrome.webview. The event arg for that event will have the following methods and properties.

PropertyDescription
getBuffer()A method that returns an ArrayBuffer object with the backing content from the shared buffer.
additionalDataAn object as the result of parsing additionalDataAsJson as JSON string. This property will be undefined if additionalDataAsJson is nullptr or empty string.
sourceWith a value set as chrome.webview object.

If access is ReadOnly, the script will only have read access to the buffer. If the script tries to modify the content in a read only buffer, it will cause an access violation in WebView renderer process and crash the renderer process.

If the shared buffer is already closed, the API throws COMException with error code of RO_E_CLOSED. The script code should call chrome.webview.releaseBuffer with the shared buffer as the parameter to release underlying resources as soon as it does not need access to the shared buffer any more.

The application can post the same shared buffer object to multiple web pages or iframes, or post to the same web page or iframe multiple times. Each PostSharedBufferToScript will create a separate ArrayBuffer object with its own view of the memory and is separately released. The underlying shared memory will be released when all the views are released.

Sharing a buffer to script has security risk. You should only share buffer with trusted site. If a buffer is shared to a untrusted site, possible sensitive information could be leaked. If a buffer is shared as modifiable by the script and the script modifies it in an unexpected way, it could result in corrupted data that might even crash the application.

The example code shows how to send data to script for one time read only consumption.

Applies to