You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -2,29 +2,48 @@ Shared Buffer Between Native Application Code and Script
2
2
===
3
3
4
4
# Background
5
-
For some advanced scenarios, there is a need to exchange large amounts of data between the WebView2 application process and trusted web pages that are considered as part of the app. Some examples:
6
-
- Web page generates a large amount of data, and passes it to the native side to be further processed or fed to other parts of the app or OS. For example, the web page generates 100MBs of high DPI images to be printed and needs to pass that to the native code to print. See https://github.com/MicrosoftEdge/WebView2Feedback/issues/89.
7
-
- Native side generates a large amount of data for the web side to consume. The data might or might not come directly from files. For example the native side has generated terrabytes of data to produce different graphs on the web side. See https://github.com/MicrosoftEdge/WebView2Feedback/issues/1005.
8
-
To support these scenarios, we are adding an Edge WebView2 API to support sharing buffers between the WebView2 host app process and WebView2 renderer process, based on shared memory from the OS.
5
+
For some advanced scenarios, there is a need to exchange large amounts of data
6
+
between the WebView2 application process and trusted web pages that are considered
7
+
as part of the app. Some examples:
8
+
- Web page generates a large amount of data, and passes it to the native side to be
9
+
further processed or fed to other parts of the app or OS. For example, the web page
10
+
generates 100MBs of high DPI images to be printed and needs to pass that to the native
11
+
code to print. See https://github.com/MicrosoftEdge/WebView2Feedback/issues/89.
12
+
- Native side generates a large amount of data for the web side to consume. The data
13
+
might or might not come directly from files. For example the native side has generated
14
+
terrabytes of data to produce different graphs on the web side.
15
+
See https://github.com/MicrosoftEdge/WebView2Feedback/issues/1005.
9
16
10
-
The application code can use the APIs to create a shared buffer object, and share to scripts as [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) object.
17
+
To support these scenarios, we are adding an Edge WebView2 API to support sharing
18
+
buffers between the WebView2 host app process and WebView2 renderer process, based
19
+
on shared memory from the OS.
20
+
21
+
The application code can use the APIs to create a shared buffer object, and share to
22
+
scripts as [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) object.
11
23
Then both the native application code and the script will be able to access the same memory.
12
24
13
25
# Conceptual pages (How To)
14
26
15
-
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.
27
+
Besides using the memory address directly, the shared buffer object can be accessed
28
+
from native side via an IStream* object that you can get from the shared object.
16
29
17
-
When the application code calls `PostSharedBufferToScript`, the script side will receive a `SharedBufferReceived` event containing the buffer as an `ArrayBuffer` object.
18
-
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
30
+
When the application code calls `PostSharedBufferToScript`, the script side will
31
+
receive a `SharedBufferReceived` event containing the buffer as an `ArrayBuffer` object.
32
+
After receiving the shared buffer object, it can access it the same way as any other
33
+
ArrayBuffer object, including transering to a web worker to process the data on
19
34
the worker thread.
20
35
21
-
As shared buffer normally represent a large memory, instead of waiting for garbage collection to release the memory along with the owning objects, the application
22
-
should try to release the buffer as soon as it doesn't need access to it. This can be done by calling Close() method on the shared buffer object, or
36
+
As shared buffer normally represent a large memory, instead of waiting for garbage
37
+
collection to release the memory along with the owning objects, the application
38
+
should try to release the buffer as soon as it doesn't need access to it.
39
+
This can be done by calling Close() method on the shared buffer object, or
23
40
`chrome.webview.releaseSharedBuffer` from script.
24
41
25
-
As the memory could contain sensitive information and corrupted memory could crash the application, the application should only share buffer with trusted sites.
42
+
As the memory could contain sensitive information and corrupted memory could crash
43
+
the application, the application should only share buffer with trusted sites.
26
44
27
-
The application can use other messaging channel like `PostWebMessageAsJson` and `chrome.webview.postMessage` to inform the other side the desire to have a shared
45
+
The application can use other messaging channel like `PostWebMessageAsJson` and
46
+
`chrome.webview.postMessage` to inform the other side the desire to have a shared
28
47
buffer and the status of the buffer (data is produced or consumed).
29
48
30
49
# Examples
@@ -53,11 +72,11 @@ The script code will look like this:
0 commit comments