CommunityToolkit / Microsoft.Toolkit.Win32

ARCHIVE - This repository contained XAML Islands wrapper controls and tooling for XAML Islands with WinUI 2, see readme for more info about XAML Islands with WinUI 3 and the WindowsAppSDK.
https://aka.ms/windowsappsdk
Other
384 stars 89 forks source link

WPF WebView crash when calling InvokeStringAsync #51

Open kevingosse opened 6 years ago

kevingosse commented 6 years ago

I'm submitting a...

Current behavior

I'm hosting a WebView control in a simple WPF application. I call NavigateToString to load a script. Then the application always crash on InvokeScriptAsync.

Native callstack:

00 0000004b`e079c2f0 00007ffb`407186cf EdgeManager!wil::details::ReportFailure+0xec
01 0000004b`e079d830 00007ffb`40707881 EdgeManager!wil::details::ReportFailure_Hr+0x3f
02 0000004b`e079d890 00007ffb`407487b3 EdgeManager!wil::details::in1diag3::_FailFast_Hr+0x29
03 0000004b`e079d8e0 00007ffb`40745562 EdgeManager!WebViewControlWinRTHelpers::CreateWinRTUriFromString+0x67
04 0000004b`e079d950 00007ffa`78aa1e32 EdgeManager!Windows::Web::UI::WebViewControlBase::get_Source+0x92

Minimal reproduction of the problem with instructions

Repro project: WpfApp1.zip

Just launch it and click on the button

Environment

Nuget Package(s): 

Microsoft.Toolkit.Wpf.UI.Controls.WebView

Package Version(s): 

5.0.0-preview.gb86cb1c4cb

Windows 10 Build Number:
- [ ] Fall Creators Update (16299)
- [X] April 2018 Update (17134)
- [ ] October 2018 Update (17763)
- [ ] Insider Build (build number: )

Device form factor:
- [X] Desktop
- [ ] Mobile
- [ ] Xbox
- [ ] Surface Hub
- [ ] IoT

Visual Studio 
- [X] 2017 (version: 15.8.8)
- [ ] 2017 Preview (version: )
kevingosse commented 6 years ago

It looks like this sequence produces the same result:

WebView.Navigate((Uri)null);

Wait for the navigation to complete

WebView.NavigateToString("crash");

crashes

Just as before, the issue occurs in Windows::Web::UI::WebViewControlBase::get_Source which in turn calls WebViewControlWinRTHelpers::CreateWinRTUriFromString. I obviously don't have access to the source code of Edge, but I get the feeling that this happens when querying Source while the Uri is null.

kevingosse commented 6 years ago

My workaround is to implement a custom IUriToStreamResolver that returns my string, then use NavigateToLocalStreamUri to load it.

After that, calling InvokeScriptAsync works properly, which seems to confirm my hypothesis: the crash is caused by Source being read while the uri is null.

rjmurillo commented 6 years ago

@joshholmes. We do a check while navigating to determine if the process has the appropriate permission:

https://github.com/windows-toolkit/WindowsCommunityToolkit/blob/2e429199ca197ef22588fa1c7855e171d2dffa36/Microsoft.Toolkit.Win32/WebView.Shared/Interop/WinRT/WebViewControlHost.cs#L445-L449

There are a few cases we previously discussed during triage where we fail fast and we shouldn't