WebView2Feedback icon indicating copy to clipboard operation
WebView2Feedback copied to clipboard

[Problem/Bug]: CoreWebView2.AddWebResourceRequestedFilter("*", Document) doesn't work for secondary WebViews in the same process

Open Manish3177 opened this issue 1 year ago • 4 comments

What happened?

We have an application where we want to inspect each frame-navigation and run some logic asynchronously to figure out if we want to allow that navigation. Failing that check, we want the iframe to stay on the page where the link was clicked. To achieve that, we first tried using the FrameNavigationStarting event whose args even have a Cancel property. The problem that made it not usable for us was that the args don't allow deferral so we couldn't run our asynchronous check.

As an alternative, we attempted using WebResourceRequested event which does support args.GetDeferral() method. We set up the following filters to intercept all navigation requests and some requests with a certain prefix:

		private void InitializeWebView2EventHandlers(...)
		{
			_webView.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.Document);
			_webView2.CoreWebView2.AddWebResourceRequestedFilter("prefix:*", CoreWebView2WebResourceContext.All);
			_webView.CoreWebView2.WebResourceRequested += CoreWebView2_WebResourceRequested;
		}

This works fine for the first WebView2 control we create on our main form of our application. We also create additional forms with new WebView2 controls and wire them up using the exact same code. However, the first filter does not work. Interestingly, the second filter does! I have tried changing the second parameter for first filter to All as well but it doesn't help.

Importance

Important. My app's user experience is significantly compromised.

Runtime Channel

Stable release (WebView2 Runtime)

Runtime Version

126.0.2592.102

SDK Version

1.0.2535.41

Framework

Winforms

Operating System

Windows 11

OS Version

10.0.22631.3737

Repro steps

Steps to reproduce and relevant code is included in the problem description.

Repros in Edge Browser

No, issue does not reproduce in the corresponding Edge version

Regression

No, this never worked

Last working version (if regression)

No response

AB#52931928

Manish3177 avatar Jul 17 '24 21:07 Manish3177

Further testing shows that occasionally, the WebResourceRequested event fails to fire for the primary form as well. We have not been able to establish a pattern here. Perhaps this is really the same issue as #2341. However, that seems to indicate that the event never fires but, in our testing, it seems to fire about half of the times for the main window.

Manish3177 avatar Jul 24 '24 21:07 Manish3177

Hi, it seems your iframe may be a different origin, which would prevent you from getting WebResourceRequested event for such iframes. For this one, you'd need to use the AddWebResourceRequestedFilter with the SourceKind parameter overload

ghost avatar Jul 26 '24 04:07 ghost

Hi, thanks for the recommendation. Specifying ResourceSourceKinds.All for the third parameter seems to address the erratic behavior we were seeing on the primary form/WebView2 control. However, it doesn't help getting the event to fire on the secondary form/WebView2 controls. I tried with ResourceContext.All in addition to ResourceSourceKinds.All as well. To reiterate, on those forms, it fires for "prefix:" URLs just not for "anythingElse:". On the off chance that the bug was specific to "", I tried replacing the "" filter with "https://" and "http://" but that didn't work either.

To provide a little more information, we are intercepting the NewWindowRequested event, created a new form, and assigning args.NewWindow = newForm.WebView2Control.CoreWebWebView2.

Manish3177 avatar Jul 30 '24 14:07 Manish3177

✅ Successfully linked to Azure Boards work item(s):

champnic avatar Jul 30 '24 21:07 champnic

Hi, It's good to see that a work item has been created for this. Is there any status update that can be shared?

Manish3177 avatar Aug 06 '24 22:08 Manish3177

We are currently looking into it; will keep you posted here if there's any updates

victorhuangwq avatar Aug 16 '24 17:08 victorhuangwq

Hi @Manish3177 , when I try with our .NET sample app on an opened window with WebView2, I see the WebResourceRequested event fires for document when I add the filter as expected. For the opened window, are you registering the filter and WebResourceRequested event before or after you set the NewWindow property? In past we had some timing issues with the event not being registered on time for the first navigation of an opened WebView. Could you try changing the order you add the filter and the event to setting the NewWindow property to see if helps? You can try the following options: -Register WebResourceRequested and filter before setting NewWindow property on NewWindowRequested. -Register WebResourceRequested and filter aafter setting NewWindow property on NewWindowRequested but in the event handler. -Register WebResourceRequested and filter on NavigationStarting handler of the NewWindow's first navigation. (This would miss the request for the first navigation)

ghost avatar Aug 27 '24 17:08 ghost

Hi Çağrı,

Our code currently attaches all event handlers, including the one for WebResourceRequested event, right after awaiting the call to webView.EnsureCoreWebView2Async. While we have not tried exactly what you are suggesting, we have tried attaching the event handler for WebResourceRequested on the first NavigationCompleted event firing. That helped a little bit in that, it was able to catch subsequent navigations of the top level window but it still didn't catch any navigations of iframes within that window. I will try to get a minimal repro app out to you sometime next week.

Ultimately, the event not firing reliably is a deal breaker for us. Even if it were to work 99% of the times on being attached in a strategic place, that would still not be good enough. Placing it in a very specific place seems fragile and inspires a feeling of uneasiness that it may not work under certain circumstances/configurations.

Despite some problems with [Frame]NavigationStarting events, since those are 100% reliable, I think we will implement our solution using those for now. The problems with those don't have as severe an impact. Also, we think we can find workarounds to those but not this problem.

Manish3177 avatar Sep 03 '24 15:09 Manish3177

Can you try adding the event handler and the filter after setting the NewWindow property as per NewWindow remarks?

ghost avatar Sep 04 '24 11:09 ghost

Closing this for now as I haven't heard back from you. Feel free to re-open if you are still running into this after trying (https://github.com/MicrosoftEdge/WebView2Feedback/issues/4689#issuecomment-2328646908)

ghost avatar Sep 17 '24 08:09 ghost