MicrosoftEdge / WebView2Feedback

Feedback and discussions about Microsoft Edge WebView2
https://aka.ms/webview2
449 stars 55 forks source link

[WinRT Host Objects] Support for passing IBuffer/ArrayBuffer between host and Web content #5

Closed phraemer closed 2 years ago

phraemer commented 5 years ago

Some background: Currently using WebView in UWP. We use AddWebAllowedObject to inject a WinRT object into the web context. Using that object we can call back out to the host for data returned as an IBuffer.

AB#20507495

david-risney commented 5 years ago

Thanks! Yes, an AddWebAllowedObject-like API is something we're looking into in the future. I'll ensure we cover this scenario.

radujipa commented 3 years ago

I can confirm both https://github.com/MicrosoftEdge/WebView2Feedback/issues/1416 and https://github.com/MicrosoftEdge/WebView2Feedback/issues/1702 having replicated these issues using the 2.7.0-prerelease.210827001 in UWP.

Simply reinforcing the need for a mechanism to inject both Windows and custom WinRT APIs into WebView2 for remote (https://\*.mydomain.com) and local (ms-apps-web://* or SetVirtualHostNameToFolderMapping from https://github.com/microsoft/microsoft-ui-xaml/issues/1967) content.

It would be great to have both the "namespace" / "passive" (Content URIs) and instanced objects (AddWebAllowedObject / AddHostObjectToScript) mechanisms working in WebView2.

For the time being, am I correct in the assessment that there's no available workarounds with the prerelease package?

Has there been any progress or updates to timelines or scope of these features for WebView2 in UWP?

radujipa commented 3 years ago

I have tested the 2.8.0-prerelease.210927001 version of the package with the above WebView 2 issues still present.

champnic commented 2 years ago

This should now be available in XAML 2.8 release: https://docs.microsoft.com/en-us/microsoft-edge/webview2/how-to/winrt-from-js?tabs=csharp

Let us know if you run into any issues. Thanks!

Take-A-Byte commented 1 year ago

Could someone please help me find a way to send IBuffer to WebView2? I could not find any comment that addresses the problem that @phraemer raised. @phraemer correct me if I am wrong ...

cc: @champnic @david-risney

david-risney commented 1 year ago

The WebView2 feature lets you use CoreWebView2Environment.CreateSharedBuffer to create a buffer object that you can use in winrt on the app side, post to script using CoreWebView2.PostSharedBufferToScript where it will be exposed as an ArrayBuffer in script.

Take-A-Byte commented 1 year ago

Thanks, @david-risney for your answer. That's helpful. Is that the only way we send a buffer to JS as this does not allow having buffers as the return type of functions on the app side (C# code) which are called from JS?

I was wondering about this as having it as a return type makes code more readable and easier to follow.

Also, Are these usable in C# - UWP? Because with the following code, it is not able to close the writer stream

           var baseStr = "Checking if small stream works";
            using (var strstream = GenerateStreamFromString(baseStr))
            {
                var environment = await CoreWebView2Environment.CreateAsync();
                using (CoreWebView2SharedBuffer sharedBuffer = environment.CreateSharedBuffer((ulong)strstream.Length))
                {
                    using (var stream = sharedBuffer.OpenStream())
                    {
                        using (StreamWriter writer = new StreamWriter(stream.AsStreamForWrite()))
                        {
                            writer.Write(strstream);
                        }
                    }
                }
            }
david-risney commented 1 year ago

@Take-A-Byte That's the only way at the moment. Its not support for generic IBuffer, but this specific shared buffer so I would be surprised but if there's enough interest we can look into supporting the shared buffer as a part of AddHostObjectToScript.

When you say it is not able to close the writer stream, what errors or exceptions do you see exactly? (cc @LiangTheDev)

Take-A-Byte commented 1 year ago

@david-risney It times out. Here is the exact error message that I get on the screen - image

david-risney commented 1 year ago

I've opened a new issue for this: https://github.com/MicrosoftEdge/WebView2Feedback/issues/3108 thanks

LiangTheDev commented 1 year ago

FYI, I've posted a potential workaround for the issue in https://github.com/MicrosoftEdge/WebView2Feedback/issues/3108.

Take-A-Byte commented 1 year ago

@david-risney Thanks for creating the issue and @LiangTheDev Thanks for the workaround, it works 🚀