From b5e66ca7380ad8f22568fe652be3bb1565207b4b Mon Sep 17 00:00:00 2001 From: Sam Bent Date: Tue, 3 Aug 2021 09:48:01 -0700 Subject: [PATCH 1/2] Port WebBrowser completion events fix from 4.8 --- .../MS/Internal/Controls/WebBrowserEvent.cs | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Controls/WebBrowserEvent.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Controls/WebBrowserEvent.cs index b836287af35..f83b42405eb 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Controls/WebBrowserEvent.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Controls/WebBrowserEvent.cs @@ -24,8 +24,8 @@ using MS.Internal.AppModel; using MS.Internal.Interop; -//In order to avoid generating warnings about unknown message numbers and -//unknown pragmas when compiling your C# source code with the actual C# compiler, +//In order to avoid generating warnings about unknown message numbers and +//unknown pragmas when compiling your C# source code with the actual C# compiler, //you need to disable warnings 1634 and 1691. (Presharp Documentation) #pragma warning disable 1634, 1691 @@ -72,15 +72,15 @@ public void BeforeNavigate2(object pDisp, ref object url, ref object flags, ref UnsafeNativeMethods.IWebBrowser2 axIWebBrowser2 = (UnsafeNativeMethods.IWebBrowser2)pDisp; // If _parent.AxIWebBrowser2 != axIWebBrowser2, navigation happens in a nested [i]frame. - // in that case we do not want to enforce site locking as we want the default IE behavior to take - // over. + // in that case we do not want to enforce site locking as we want the default IE behavior to take + // over. if (_parent.AxIWebBrowser2 == axIWebBrowser2) { // The NavigatingToAboutBlank property indicates whether we are navigating to "about:blank" // as a result of navigating to null or stream/string navigation. // We set the NavigatingToAboutBlank bit to true in the WebBrowser DoNavigate method. When the above - // conditions occur, the NavigatingToAboutBlank is true and the source must be "about:blank". - // + // conditions occur, the NavigatingToAboutBlank is true and the source must be "about:blank". + // // But when end user navigates away from the current about:blank page (by clicking // on a hyperlink, Goback/Forward), or programmatically call GoBack and Forward, // When we get the navigating event, NavigatingToAboutBlank is true, but the source is not "about:blank". @@ -174,11 +174,11 @@ public void NavigateComplete2(object pDisp, ref object url) { Debug.Assert(url == null || url is string, "invalid url type"); - // Events only fired for top level navigation. + // Events only fired for top level navigation. UnsafeNativeMethods.IWebBrowser2 axIWebBrowser2 = (UnsafeNativeMethods.IWebBrowser2)pDisp; - if (_parent.AxIWebBrowser2 == axIWebBrowser2) + if (_parent.AxIWebBrowser2 == axIWebBrowser2 && !ShouldIgnoreCompletionEvent(ref url)) { - // If we are loading from stream. + // If we are loading from stream. if (_parent.DocumentStream != null) { Invariant.Assert(_parent.NavigatingToAboutBlank && @@ -207,7 +207,7 @@ public void NavigateComplete2(object pDisp, ref object url) { string urlString = (string)url; // When source set to null or navigating to stream/string, we navigate to "about:blank" - // internally. Make sure we pass null in the event args. + // internally. Make sure we pass null in the event args. if (_parent.NavigatingToAboutBlank) { Invariant.Assert(String.Compare(urlString, WebBrowser.AboutBlankUriString, StringComparison.OrdinalIgnoreCase) == 0); @@ -229,13 +229,13 @@ public void DocumentComplete(object pDisp, ref object url) { Debug.Assert(url == null || url is string, "invalid url type"); - // Events only fired for top level navigation. + // Events only fired for top level navigation. UnsafeNativeMethods.IWebBrowser2 axIWebBrowser2 = (UnsafeNativeMethods.IWebBrowser2)pDisp; - if (_parent.AxIWebBrowser2 == axIWebBrowser2) + if (_parent.AxIWebBrowser2 == axIWebBrowser2 && !ShouldIgnoreCompletionEvent(ref url)) { string urlString = (string)url; // When source set to null or navigating to stream/string, we navigate to "about:blank" - // internally. Make sure we pass null in the event args. + // internally. Make sure we pass null in the event args. if (_parent.NavigatingToAboutBlank) { Invariant.Assert(String.Compare(urlString, WebBrowser.AboutBlankUriString, StringComparison.OrdinalIgnoreCase) == 0); @@ -248,14 +248,23 @@ public void DocumentComplete(object pDisp, ref object url) } } + // if we are navigating to "about:blank", ignore completion events + // for any other url. These can happen despite our attempt to cancel + // outstanding navigations (see WebBrowser.DoNavigate), due to race + // conditions between the browser and us. + private bool ShouldIgnoreCompletionEvent(ref object url) + { + string urlString = url as string; + return (_parent.NavigatingToAboutBlank && + String.Compare(urlString, WebBrowser.AboutBlankUriString, StringComparison.OrdinalIgnoreCase) != 0); + } + public void CommandStateChange(long command, bool enable) { - if (command == NativeMethods.CSC_NAVIGATEBACK) - { + if (command == NativeMethods.CSC_NAVIGATEBACK) { _parent._canGoBack = enable; } - else if (command == NativeMethods.CSC_NAVIGATEFORWARD) - { + else if (command == NativeMethods.CSC_NAVIGATEFORWARD) { _parent._canGoForward = enable; } } From b105aff57fb953b5417cac3763a00105f46367ba Mon Sep 17 00:00:00 2001 From: Sam Bent Date: Mon, 16 Aug 2021 17:10:04 -0700 Subject: [PATCH 2/2] fix code format --- .../MS/Internal/Controls/WebBrowserEvent.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Controls/WebBrowserEvent.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Controls/WebBrowserEvent.cs index f83b42405eb..dc7176b3fa8 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Controls/WebBrowserEvent.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Controls/WebBrowserEvent.cs @@ -261,10 +261,12 @@ private bool ShouldIgnoreCompletionEvent(ref object url) public void CommandStateChange(long command, bool enable) { - if (command == NativeMethods.CSC_NAVIGATEBACK) { + if (command == NativeMethods.CSC_NAVIGATEBACK) + { _parent._canGoBack = enable; } - else if (command == NativeMethods.CSC_NAVIGATEFORWARD) { + else if (command == NativeMethods.CSC_NAVIGATEFORWARD) + { _parent._canGoForward = enable; } }