-
Notifications
You must be signed in to change notification settings - Fork 552
Expand file tree
/
Copy pathsharedbufferreceivedevent.yml
More file actions
133 lines (121 loc) · 4.77 KB
/
sharedbufferreceivedevent.yml
File metadata and controls
133 lines (121 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
### YamlMime:TSType
name: SharedBufferReceivedEvent
uid: 'WebView2Script!SharedBufferReceivedEvent:class'
package: WebView2Script!
fullName: SharedBufferReceivedEvent
summary: >-
Event object for the `chrome.webview.sharedbufferreceived` event. This event is dispatched when
`CoreWebView2.PostSharedBufferToScript` is successfully called.
remarks: |-
#### Examples
The following example sends data to script for one-time, read-only consumption.
First, in the native host app code, set data into the shared memory:
```cpp
wil::com_ptr<ICoreWebView2ExperimentalEnvironment10> environment;
CHECK_FAILURE(
m_appWindow->GetWebViewEnvironment()->QueryInterface(IID_PPV_ARGS(&environment)));
wil::com_ptr<ICoreWebView2ExperimentalSharedBuffer> sharedBuffer;
CHECK_FAILURE(environment->CreateSharedBuffer(bufferSize, &sharedBuffer));
// Add data to shared memory via IStream.
wil::com_ptr<IStream> stream;
CHECK_FAILURE(sharedBuffer->OpenStream(&stream));
CHECK_FAILURE(stream->Write(data, sizeof(data), nullptr));
PCWSTR additionalDataAsJson = L"{\"myBufferType\":\"bufferType1\"}";
if (fromFrame)
{
m_webviewFrame4->PostSharedBufferToScript(
sharedBuffer.get(), COREWEBVIEW2_SHARED_BUFFER_ACCESS_READ_ONLY,
additionalDataAsJson);
}
else
{
m_webView18->PostSharedBufferToScript(
sharedBuffer.get(), COREWEBVIEW2_SHARED_BUFFER_ACCESS_READ_ONLY,
additionalDataAsJson);
}
// Close the one-time shared buffer, to release resources.
sharedBuffer->Close();
```
In the HTML document, subscribe to and handle sharedbufferreceived event.
Next, in the HTML document, subscribe to and then handle the `sharedbufferreceived` event:
```html
window.chrome.webview.addEventListener("sharedbufferreceived", e => {
SharedBufferReceived(e);});
```
```html
let readOnlySharedBuffer;
function ShowReadOnlySharedBuffer() {
if (readOnlySharedBuffer) {
DisplaySharedBufferData(readOnlySharedBuffer);
} else {
// Post a web message to ask host to share the one time read only buffer.
chrome.webview.postMessage("RequestOneTimeShareBuffer");
}
}
function DisplaySharedBufferData(buffer) {
document.getElementById("shared-buffer-data").value =
new TextDecoder().decode(new Uint8Array(buffer));
}
function SharedBufferReceived(e) {
if (e.additionalData && e.additionalData.myBufferType == "bufferType1") {
readOnlySharedBuffer = e.getBuffer();
} else {
sharedBuffer = e.getBuffer();
}
DisplaySharedBufferData(e.getBuffer());
}
function ReleaseBuffer(buffer) {
window.chrome.webview.releaseBuffer(buffer);
}
```
isPreview: false
isDeprecated: false
type: class
properties:
- name: additionalData
uid: 'WebView2Script!SharedBufferReceivedEvent#additionalData:member'
package: WebView2Script!
fullName: additionalData
summary: >-
An object that is the result of parsing the `additionalDataAsJson` parameter to
`CoreWebView2.PostSharedBufferToScript` as a JSON string. This property will be `undefined` if
`additionalDataAsJson` is `nullptr` or the empty string.
remarks: ''
isPreview: false
isDeprecated: false
syntax:
content: 'additionalData: any;'
return:
type: any
- name: source
uid: 'WebView2Script!SharedBufferReceivedEvent#source:member'
package: WebView2Script!
fullName: source
summary: The source of the event is the `chrome.webview` object.
remarks: ''
isPreview: false
isDeprecated: false
syntax:
content: 'source: WebView;'
return:
type: '<xref uid="WebView2Script!WebView:class" />'
methods:
- name: getBuffer()
uid: 'WebView2Script!SharedBufferReceivedEvent#getBuffer:member(1)'
package: WebView2Script!
fullName: getBuffer()
summary: >-
Returns an `ArrayBuffer` object with the backing content from the shared buffer passed to
`CoreWebView2.PostSharedBufferToScript`<!-- -->. If `CoreWebView2.PostSharedBufferToScript` was called with the
buffer set to `ReadOnly`<!-- -->, then only read access is allowed to the buffer. If you try to modify the content
in a read-only buffer, it will cause an access violation in the WebView renderer process and crash the renderer
process.
remarks: ''
isPreview: false
isDeprecated: false
syntax:
content: 'getBuffer(): ArrayBuffer;'
return:
type: ArrayBuffer
description: An `ArrayBuffer` over the shared buffer passed to `CoreWebView2.PostSharedBufferToScript`.
extends: <a href="https://developer.mozilla.org/docs/Web/API/Event">Event</a>