diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index 7a27a3262..5968b5a55 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -1048,7 +1048,7 @@ namespace CefSharp { if (frame->IsMain()) { - _browserControl->SetCanExecuteJavascriptOnMainFrame(true); + _browserControl->SetCanExecuteJavascriptOnMainFrame(frame->GetIdentifier(), true); } auto handler = _browserControl->RenderProcessMessageHandler; @@ -1072,7 +1072,7 @@ namespace CefSharp { if (frame->IsMain()) { - _browserControl->SetCanExecuteJavascriptOnMainFrame(false); + _browserControl->SetCanExecuteJavascriptOnMainFrame(frame->GetIdentifier(), false); } auto handler = _browserControl->RenderProcessMessageHandler; diff --git a/CefSharp.OffScreen/ChromiumWebBrowser.cs b/CefSharp.OffScreen/ChromiumWebBrowser.cs index 2452adec3..2158cca5c 100644 --- a/CefSharp.OffScreen/ChromiumWebBrowser.cs +++ b/CefSharp.OffScreen/ChromiumWebBrowser.cs @@ -50,6 +50,11 @@ namespace CefSharp.OffScreen /// private int disposeSignaled; + /// + /// Used as workaround for issue https://github.com/cefsharp/CefSharp/issues/3021 + /// + private long canExecuteJavascriptInMainFrameId; + /// /// Gets a value indicating whether this instance is disposed. /// @@ -906,8 +911,21 @@ namespace CefSharp.OffScreen TooltipText = tooltipText; } - void IWebBrowserInternal.SetCanExecuteJavascriptOnMainFrame(bool canExecute) + void IWebBrowserInternal.SetCanExecuteJavascriptOnMainFrame(long frameId, bool canExecute) { + //When loading pages of a different origin the frameId changes + //For the first loading of a new origin the messages from the render process + //Arrive in a different order than expected, the OnContextCreated message + //arrives before the OnContextReleased, then the message for OnContextReleased + //incorrectly overrides the value + //https://github.com/cefsharp/CefSharp/issues/3021 + + if (frameId > canExecuteJavascriptInMainFrameId && !canExecute) + { + return; + } + + canExecuteJavascriptInMainFrameId = frameId; CanExecuteJavascriptInMainFrame = canExecute; } diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index 09d9fd107..7e92b42bf 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -87,6 +87,11 @@ namespace CefSharp.WinForms /// private Control parkingControl; + /// + /// Used as workaround for issue https://github.com/cefsharp/CefSharp/issues/3021 + /// + private long canExecuteJavascriptInMainFrameId; + /// /// Gets a value indicating whether this instance is disposed. /// @@ -909,8 +914,21 @@ namespace CefSharp.WinForms } } - void IWebBrowserInternal.SetCanExecuteJavascriptOnMainFrame(bool canExecute) + void IWebBrowserInternal.SetCanExecuteJavascriptOnMainFrame(long frameId, bool canExecute) { + //When loading pages of a different origin the frameId changes + //For the first loading of a new origin the messages from the render process + //Arrive in a different order than expected, the OnContextCreated message + //arrives before the OnContextReleased, then the message for OnContextReleased + //incorrectly overrides the value + //https://github.com/cefsharp/CefSharp/issues/3021 + + if (frameId > canExecuteJavascriptInMainFrameId && !canExecute) + { + return; + } + + canExecuteJavascriptInMainFrameId = frameId; CanExecuteJavascriptInMainFrame = canExecute; } diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs index cdef6e5a9..10b3bd247 100644 --- a/CefSharp.Wpf/ChromiumWebBrowser.cs +++ b/CefSharp.Wpf/ChromiumWebBrowser.cs @@ -146,6 +146,11 @@ namespace CefSharp.Wpf /// private int disposeSignaled; + /// + /// Used as workaround for issue https://github.com/cefsharp/CefSharp/issues/3021 + /// + private long canExecuteJavascriptInMainFrameId; + /// /// Gets a value indicating whether this instance is disposed. /// @@ -1251,8 +1256,21 @@ namespace CefSharp.Wpf LoadError?.Invoke(this, args); } - void IWebBrowserInternal.SetCanExecuteJavascriptOnMainFrame(bool canExecute) + void IWebBrowserInternal.SetCanExecuteJavascriptOnMainFrame(long frameId, bool canExecute) { + //When loading pages of a different origin the frameId changes + //For the first loading of a new origin the messages from the render process + //Arrive in a different order than expected, the OnContextCreated message + //arrives before the OnContextReleased, then the message for OnContextReleased + //incorrectly overrides the value + //https://github.com/cefsharp/CefSharp/issues/3021 + + if (frameId > canExecuteJavascriptInMainFrameId && !canExecute) + { + return; + } + + canExecuteJavascriptInMainFrameId = frameId; CanExecuteJavascriptInMainFrame = canExecute; } diff --git a/CefSharp/Internals/IWebBrowserInternal.cs b/CefSharp/Internals/IWebBrowserInternal.cs index 357876ca6..52d88ba58 100644 --- a/CefSharp/Internals/IWebBrowserInternal.cs +++ b/CefSharp/Internals/IWebBrowserInternal.cs @@ -16,7 +16,7 @@ namespace CefSharp.Internals void SetLoadingStateChange(LoadingStateChangedEventArgs args); void SetTitle(TitleChangedEventArgs args); void SetTooltipText(string tooltipText); - void SetCanExecuteJavascriptOnMainFrame(bool canExecute); + void SetCanExecuteJavascriptOnMainFrame(long frameId, bool canExecute); void SetJavascriptMessageReceived(JavascriptMessageReceivedEventArgs args); void OnFrameLoadStart(FrameLoadStartEventArgs args);