Skip to content

Commit 59018db

Browse files
authored
Update SharedBuffer.md
1 parent 1e652bf commit 59018db

File tree

1 file changed

+59
-31
lines changed

1 file changed

+59
-31
lines changed

specs/SharedBuffer.md

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Then both the native application code and the script will be able to access the
1414

1515
Besides using the memory address directly, the shared buffer object can be accessed from native side via an IStream* object that you can get from the shared object.
1616

17-
When the application code calls `ShareBufferToScript`, the script side will receive a `SharedBufferReceived` event containing the buffer as an `ArrayBuffer` object.
17+
When the application code calls `PostSharedBufferToScript`, the script side will receive a `SharedBufferReceived` event containing the buffer as an `ArrayBuffer` object.
1818
After receiving the shared buffer object, it can access it the same way as any other ArrayBuffer object, including transering to a web worker to process the data on
1919
the worker thread.
2020

@@ -61,17 +61,17 @@ The script code will look like this:
6161
CHECK_FAILURE(environment->CreateSharedBuffer(bufferSize, &sharedBuffer));
6262
// Fill data into the shared memory via IStream.
6363
wil::com_ptr<IStream> stream;
64-
CHECK_FAILURE(sharedBuffer->GetAccessStream(&stream));
64+
CHECK_FAILURE(sharedBuffer->GetStream(&stream));
6565
CHECK_FAILURE(stream->Write(data, dataSize, nullptr));
6666
PCWSTR additionalDataAsJson = L"{\"read_only\":true}";
6767
if (forFrame)
6868
{
69-
m_webviewFrame->ShareBufferToScript(
69+
m_webviewFrame->PostSharedBufferToScript(
7070
sharedBuffer.get(), /*isReadOnlyToScript*/TRUE, additionalDataAsJson);
7171
}
7272
else
7373
{
74-
m_webView->ShareBufferToScript(
74+
m_webView->PostSharedBufferToScript(
7575
sharedBuffer.get(), /*isReadOnlyToScript*/TRUE, additionalDataAsJson);
7676
}
7777
// Explicitly close the one time shared buffer to ensure that the resource is released timely.
@@ -82,7 +82,7 @@ The script code will look like this:
8282
```c#
8383
var sharedBuffer = WebViewEnvironment.CreateSharedBuffer(bufferSize);
8484
// Fill data using access Stream
85-
using (Stream stream = sharedBuffer.GetAccessStream())
85+
using (Stream stream = sharedBuffer.GetStream())
8686
{
8787
using (StreamWriter writer = new StreamWriter(stream))
8888
{
@@ -92,11 +92,11 @@ The script code will look like this:
9292
string additionalDataAsJson = "{\"read_only\":true}";
9393
if (forFrame)
9494
{
95-
m_webviewFrame.ShareBufferToScript(sharedBuffer, /*isReadOnlyToScript*/true, additionalDataAsJson);
95+
m_webviewFrame.PostSharedBufferToScript(sharedBuffer, /*isReadOnlyToScript*/true, additionalDataAsJson);
9696
}
9797
else
9898
{
99-
m_webview.ShareBufferToScript(sharedBuffer, /*isReadOnlyToScript*/true, additionalDataAsJson);
99+
m_webview.PostSharedBufferToScript(sharedBuffer, /*isReadOnlyToScript*/true, additionalDataAsJson);
100100
}
101101
// Explicitly close the one time shared buffer to ensure that the resource is released timely.
102102
sharedBuffer.Close();
@@ -106,9 +106,9 @@ The script code will look like this:
106106
## Win32 C++
107107
```
108108
interface ICoreWebView2StagingEnvironment2 : IUnknown {
109-
/// Create a shared memory based buffer with the specifid size in bytes.
109+
/// Create a shared memory based buffer with the specified size in bytes.
110110
/// The buffer can be shared with web contents in WebView by calling
111-
/// `ShareBufferToScript` on `CoreWebView2` or `CoreWebViewFrame` object.
111+
/// `PostSharedBufferToScript` on `CoreWebView2` or `CoreWebViewFrame` object.
112112
/// Once shared, the same content of the buffer will be accessible from both
113113
/// the app process and script in WebView. Modification to the content will be visible
114114
/// to all parties that have access to the buffer.
@@ -126,10 +126,10 @@ interface ICoreWebView2StagingSharedBuffer : IUnknown {
126126
[propget] HRESULT Buffer([out, retval] BYTE** value);
127127
128128
/// Get an IStream object that can be used to access the shared buffer.
129-
HRESULT GetAccessStream([out, retval] IStream** value);
129+
HRESULT GetStream([out, retval] IStream** value);
130130
131131
/// The file mapping handle of the shared memory of the buffer.
132-
/// Normal app should use `Buffer` or `GetAccessStream` to get memory address
132+
/// Normal app should use `Buffer` or `GetStream` to get memory address
133133
/// or IStream object to access the buffer.
134134
/// For advanced scenario, you could duplicate this handle to another application
135135
/// process and create a mapping from the duplicated handle in that process to access
@@ -138,19 +138,23 @@ interface ICoreWebView2StagingSharedBuffer : IUnknown {
138138
139139
/// Release the backing shared memory. The application should call this API when no
140140
/// access to the buffer is needed any more, to ensure that the underlying resources
141-
/// are released timely even if the shared buffer object itself is not released due to some leaked
142-
/// reference.
141+
/// are released timely even if the shared buffer object itself is not released due to
142+
/// some leaked reference.
143143
/// After the shared buffer is closed, accessing properties of the object will fail with
144144
/// `HRESULT_FROM_WIN32(ERROR_INVALID_STATE)`. Operations like Read or Write on the IStream objects
145-
/// returned from `GetAccessStream` will fail with `HRESULT_FROM_WIN32(ERROR_INVALID_STATE)`.
146-
/// `ShareBufferToScript` will also fail with `HRESULT_FROM_WIN32(ERROR_INVALID_STATE)`.
145+
/// returned from `GetStream` will fail with `HRESULT_FROM_WIN32(ERROR_INVALID_STATE)`.
146+
/// `PostSharedBufferToScript` will also fail with `HRESULT_FROM_WIN32(ERROR_INVALID_STATE)`.
147147
///
148148
/// The script code should call `chrome.webview.releaseSharedBuffer` with
149149
/// the shared buffer as the parameter to release underlying resources as soon
150150
/// as it does not need access the shared buffer any more.
151151
/// When script tries to access the buffer after calling `chrome.webview.releaseSharedBuffer`,
152152
/// JavaScript `TypeError` exception will be raised complaining about accessing a detached ArrayBuffer,
153153
/// the same exception when trying to access a transferred ArrayBuffer.
154+
///
155+
/// Closing the buffer object on native side doesn't impact access from Script and releasing the buffer
156+
/// from script doesn't impact access to the buffer from native side.
157+
/// The underlying shared memory will be released by the OS when both native and script side releases the buffer.
154158
HRESULT Close();
155159
}
156160
@@ -169,11 +173,11 @@ interface ICoreWebView2Staging7 : IUnknown {
169173
///
170174
/// The script code should call `chrome.webview.releaseSharedBuffer` with
171175
/// the shared buffer as the parameter to release underlying resources as soon
172-
/// as it does not need access the shared buffer any more.
176+
/// as it does not need access to the shared buffer any more.
173177
///
174178
/// Sharing a buffer to script has security risk. You should only share buffer with trusted site.
175179
///
176-
HRESULT ShareBufferToScript(
180+
HRESULT PostSharedBufferToScript(
177181
[in] ICoreWebView2StagingSharedBuffer* sharedBuffer,
178182
[in] BOOL isReadOnlyToScript,
179183
[in] LPCWSTR additionalDataAsJson);
@@ -194,11 +198,11 @@ interface ICoreWebView2StagingFrame2 : IUnknown {
194198
///
195199
/// The script code should call `chrome.webview.releaseSharedBuffer` with
196200
/// the shared buffer as the parameter to release underlying resources as soon
197-
/// as it does not need access the shared buffer any more.
201+
/// as it does not need access to the shared buffer any more.
198202
///
199203
/// Sharing a buffer to script has security risk. You should only share buffer with trusted site.
200204
///
201-
HRESULT ShareBufferToScript(
205+
HRESULT PostSharedBufferToScript(
202206
[in] ICoreWebView2StagingSharedBuffer* sharedBuffer,
203207
[in] BOOL isReadOnlyToScript,
204208
[in] LPCWSTR additionalDataAsJson);
@@ -213,26 +217,41 @@ namespace Microsoft.Web.WebView2.Core
213217

214218
class CoreWebView2Environment
215219
{
216-
public CoreWebView2SharedBuffer CreateSharedBuffer(ulong Size);
220+
public CoreWebView2SharedBuffer CreateSharedBuffer(ulong size);
217221
}
218222

219-
class CoreWebView2SharedBuffer
223+
class CoreWebView2SharedBuffer : System.IDisposable
220224
{
221225
public ulong Size { get; };
226+
227+
/// The raw memory address of the buffer.
228+
/// You can cast it to pointer to real data types like byte* to access the memory from `unsafe` code region.
229+
/// Normal app should use `GetStream` to get a Stream object to access the buffer.
222230
public IntPtr Buffer { get; };
231+
232+
/// The native file mapping handle of the shared memory of the buffer.
233+
/// Normal app should use `GetStream` to get a Stream object to access the buffer.
234+
/// For advanced scenario, you could use native APIs to duplicate this handle to another application
235+
/// process and create a mapping from the duplicated handle in that process to access
236+
/// the buffer from that separate process.
223237
public IntPtr Handle { get; };
224-
public Stream GetAccessStream();
238+
239+
public Stream GetStream();
240+
225241
void Close();
242+
243+
// IDisposable
244+
public void Dispose();
226245
}
227246

228247
runtimeclass CoreWebView2
229248
{
230-
public void ShareBufferToScript(CoreWebView2SharedBuffer sharedBuffer, bool isReadOnlyToScript, string additionalDataAsJson);
249+
public void PostSharedBufferToScript(CoreWebView2SharedBuffer sharedBuffer, bool isReadOnlyToScript, string additionalDataAsJson);
231250
}
232251

233252
class CoreWebView2Frame
234253
{
235-
public void ShareBufferToScript(CoreWebView2SharedBuffer sharedBuffer, bool isReadOnlyToScript, string additionalDataAsJson);
254+
public void PostSharedBufferToScript(CoreWebView2SharedBuffer sharedBuffer, bool isReadOnlyToScript, string additionalDataAsJson);
236255
}
237256
}
238257

@@ -246,35 +265,44 @@ namespace Microsoft.Web.WebView2.Core
246265
{
247266
[interface_name("Microsoft.Web.WebView2.Core.ICoreWebView2StagingEnvironment2")]
248267
{
249-
CoreWebView2SharedBuffer CreateSharedBuffer(UInt64 Size);
268+
CoreWebView2SharedBuffer CreateSharedBuffer(UInt64 size);
250269
}
251270
}
252271

253-
runtimeclass CoreWebView2SharedBuffer
272+
runtimeclass CoreWebView2SharedBuffer : Windows.Foundation.IClosable
254273
{
255274
UInt64 Size { get; };
256-
Windows.Storage.Streams.IRandomAccessStream GetAccessStream();
257-
void Close();
275+
276+
Windows.Storage.Streams.IRandomAccessStream GetStream();
277+
258278
[interface_name("Microsoft.Web.WebView2.Core.ICoreWebView2SharedBuffer_Manual")]
259279
{
280+
/// A reference to the underlying memory of the shared buffer.
281+
/// You can get IMemoryBufferByteAccess from the object to access the memory as an array of bytes.
282+
/// See [I](https://docs.microsoft.com/en-us/uwp/api/windows.foundation.imemorybufferreference?view=winrt-22621)
283+
/// for more details.
260284
Windows.Foundation.IMemoryBufferReference Buffer { get; };
261285
}
286+
262287
// Note that we are not exposing Handle from WinRT API.
263-
}
288+
289+
// IClosable
290+
void Close();
291+
}
264292

265293
runtimeclass CoreWebView2
266294
{
267295
[interface_name("Microsoft.Web.WebView2.Core.ICoreWebView2Staging7")]
268296
{
269-
void ShareBufferToScript(CoreWebView2SharedBuffer sharedBuffer, Boolean isReadOnlyToScript, String additionalDataAsJson);
297+
void PostSharedBufferToScript(CoreWebView2SharedBuffer sharedBuffer, Boolean isReadOnlyToScript, String additionalDataAsJson);
270298
}
271299
}
272300

273301
runtimeclass CoreWebView2Frame
274302
{
275303
[interface_name("Microsoft.Web.WebView2.Core.ICoreWebView2StagingFrame2")]
276304
{
277-
void ShareBufferToScript(CoreWebView2SharedBuffer sharedBuffer, Boolean isReadOnlyToScript, String additionalDataAsJson);
305+
void PostSharedBufferToScript(CoreWebView2SharedBuffer sharedBuffer, Boolean isReadOnlyToScript, String additionalDataAsJson);
278306
}
279307
}
280308
}

0 commit comments

Comments
 (0)