Skip to content

Commit 1824a03

Browse files
committed
WPF/WinForms/OffScreen - CanExecuteJavascriptInMainFrame incorrectly false after loading page with different origin
Proof of concept for a workaround. The frameId for the second OnContextReleased that arrives after the OnContextCreated from the old render process strangely has a frameId greater than the new frameId TODO: Unit tests Issue #3021
1 parent 71a190d commit 1824a03

File tree

5 files changed

+60
-6
lines changed

5 files changed

+60
-6
lines changed

CefSharp.Core/Internals/ClientAdapter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,7 @@ namespace CefSharp
10481048
{
10491049
if (frame->IsMain())
10501050
{
1051-
_browserControl->SetCanExecuteJavascriptOnMainFrame(true);
1051+
_browserControl->SetCanExecuteJavascriptOnMainFrame(frame->GetIdentifier(), true);
10521052
}
10531053

10541054
auto handler = _browserControl->RenderProcessMessageHandler;
@@ -1072,7 +1072,7 @@ namespace CefSharp
10721072
{
10731073
if (frame->IsMain())
10741074
{
1075-
_browserControl->SetCanExecuteJavascriptOnMainFrame(false);
1075+
_browserControl->SetCanExecuteJavascriptOnMainFrame(frame->GetIdentifier(), false);
10761076
}
10771077

10781078
auto handler = _browserControl->RenderProcessMessageHandler;

CefSharp.OffScreen/ChromiumWebBrowser.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ public class ChromiumWebBrowser : IRenderWebBrowser
5050
/// </summary>
5151
private int disposeSignaled;
5252

53+
/// <summary>
54+
/// Used as workaround for issue https://github.com/cefsharp/CefSharp/issues/3021
55+
/// </summary>
56+
private long canExecuteJavascriptInMainFrameId;
57+
5358
/// <summary>
5459
/// Gets a value indicating whether this instance is disposed.
5560
/// </summary>
@@ -906,8 +911,21 @@ void IWebBrowserInternal.SetTooltipText(string tooltipText)
906911
TooltipText = tooltipText;
907912
}
908913

909-
void IWebBrowserInternal.SetCanExecuteJavascriptOnMainFrame(bool canExecute)
914+
void IWebBrowserInternal.SetCanExecuteJavascriptOnMainFrame(long frameId, bool canExecute)
910915
{
916+
//When loading pages of a different origin the frameId changes
917+
//For the first loading of a new origin the messages from the render process
918+
//Arrive in a different order than expected, the OnContextCreated message
919+
//arrives before the OnContextReleased, then the message for OnContextReleased
920+
//incorrectly overrides the value
921+
//https://github.com/cefsharp/CefSharp/issues/3021
922+
923+
if (frameId > canExecuteJavascriptInMainFrameId && !canExecute)
924+
{
925+
return;
926+
}
927+
928+
canExecuteJavascriptInMainFrameId = frameId;
911929
CanExecuteJavascriptInMainFrame = canExecute;
912930
}
913931

CefSharp.WinForms/ChromiumWebBrowser.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ public class ChromiumWebBrowser : Control, IWebBrowserInternal, IWinFormsWebBrow
8787
/// </summary>
8888
private Control parkingControl;
8989

90+
/// <summary>
91+
/// Used as workaround for issue https://github.com/cefsharp/CefSharp/issues/3021
92+
/// </summary>
93+
private long canExecuteJavascriptInMainFrameId;
94+
9095
/// <summary>
9196
/// Gets a value indicating whether this instance is disposed.
9297
/// </summary>
@@ -909,8 +914,21 @@ void IWebBrowserInternal.OnLoadError(LoadErrorEventArgs args)
909914
}
910915
}
911916

912-
void IWebBrowserInternal.SetCanExecuteJavascriptOnMainFrame(bool canExecute)
917+
void IWebBrowserInternal.SetCanExecuteJavascriptOnMainFrame(long frameId, bool canExecute)
913918
{
919+
//When loading pages of a different origin the frameId changes
920+
//For the first loading of a new origin the messages from the render process
921+
//Arrive in a different order than expected, the OnContextCreated message
922+
//arrives before the OnContextReleased, then the message for OnContextReleased
923+
//incorrectly overrides the value
924+
//https://github.com/cefsharp/CefSharp/issues/3021
925+
926+
if (frameId > canExecuteJavascriptInMainFrameId && !canExecute)
927+
{
928+
return;
929+
}
930+
931+
canExecuteJavascriptInMainFrameId = frameId;
914932
CanExecuteJavascriptInMainFrame = canExecute;
915933
}
916934

CefSharp.Wpf/ChromiumWebBrowser.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ public class ChromiumWebBrowser : Control, IRenderWebBrowser, IWpfWebBrowser
146146
/// </summary>
147147
private int disposeSignaled;
148148

149+
/// <summary>
150+
/// Used as workaround for issue https://github.com/cefsharp/CefSharp/issues/3021
151+
/// </summary>
152+
private long canExecuteJavascriptInMainFrameId;
153+
149154
/// <summary>
150155
/// Gets a value indicating whether this instance is disposed.
151156
/// </summary>
@@ -1251,8 +1256,21 @@ void IWebBrowserInternal.OnLoadError(LoadErrorEventArgs args)
12511256
LoadError?.Invoke(this, args);
12521257
}
12531258

1254-
void IWebBrowserInternal.SetCanExecuteJavascriptOnMainFrame(bool canExecute)
1259+
void IWebBrowserInternal.SetCanExecuteJavascriptOnMainFrame(long frameId, bool canExecute)
12551260
{
1261+
//When loading pages of a different origin the frameId changes
1262+
//For the first loading of a new origin the messages from the render process
1263+
//Arrive in a different order than expected, the OnContextCreated message
1264+
//arrives before the OnContextReleased, then the message for OnContextReleased
1265+
//incorrectly overrides the value
1266+
//https://github.com/cefsharp/CefSharp/issues/3021
1267+
1268+
if (frameId > canExecuteJavascriptInMainFrameId && !canExecute)
1269+
{
1270+
return;
1271+
}
1272+
1273+
canExecuteJavascriptInMainFrameId = frameId;
12561274
CanExecuteJavascriptInMainFrame = canExecute;
12571275
}
12581276

CefSharp/Internals/IWebBrowserInternal.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public interface IWebBrowserInternal : IWebBrowser
1616
void SetLoadingStateChange(LoadingStateChangedEventArgs args);
1717
void SetTitle(TitleChangedEventArgs args);
1818
void SetTooltipText(string tooltipText);
19-
void SetCanExecuteJavascriptOnMainFrame(bool canExecute);
19+
void SetCanExecuteJavascriptOnMainFrame(long frameId, bool canExecute);
2020
void SetJavascriptMessageReceived(JavascriptMessageReceivedEventArgs args);
2121

2222
void OnFrameLoadStart(FrameLoadStartEventArgs args);

0 commit comments

Comments
 (0)