Skip to content

Commit

Permalink
WPF/WinForms/OffScreen - CanExecuteJavascriptInMainFrame incorrectly …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
amaitland committed May 4, 2020
1 parent 71a190d commit 1824a03
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
4 changes: 2 additions & 2 deletions CefSharp.Core/Internals/ClientAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@ namespace CefSharp
{
if (frame->IsMain())
{
_browserControl->SetCanExecuteJavascriptOnMainFrame(true);
_browserControl->SetCanExecuteJavascriptOnMainFrame(frame->GetIdentifier(), true);
}

auto handler = _browserControl->RenderProcessMessageHandler;
Expand All @@ -1072,7 +1072,7 @@ namespace CefSharp
{
if (frame->IsMain())
{
_browserControl->SetCanExecuteJavascriptOnMainFrame(false);
_browserControl->SetCanExecuteJavascriptOnMainFrame(frame->GetIdentifier(), false);
}

auto handler = _browserControl->RenderProcessMessageHandler;
Expand Down
20 changes: 19 additions & 1 deletion CefSharp.OffScreen/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public class ChromiumWebBrowser : IRenderWebBrowser
/// </summary>
private int disposeSignaled;

/// <summary>
/// Used as workaround for issue https://github.com/cefsharp/CefSharp/issues/3021
/// </summary>
private long canExecuteJavascriptInMainFrameId;

/// <summary>
/// Gets a value indicating whether this instance is disposed.
/// </summary>
Expand Down Expand Up @@ -906,8 +911,21 @@ void IWebBrowserInternal.SetTooltipText(string tooltipText)
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;
}

Expand Down
20 changes: 19 additions & 1 deletion CefSharp.WinForms/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public class ChromiumWebBrowser : Control, IWebBrowserInternal, IWinFormsWebBrow
/// </summary>
private Control parkingControl;

/// <summary>
/// Used as workaround for issue https://github.com/cefsharp/CefSharp/issues/3021
/// </summary>
private long canExecuteJavascriptInMainFrameId;

/// <summary>
/// Gets a value indicating whether this instance is disposed.
/// </summary>
Expand Down Expand Up @@ -909,8 +914,21 @@ void IWebBrowserInternal.OnLoadError(LoadErrorEventArgs 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;
}

Expand Down
20 changes: 19 additions & 1 deletion CefSharp.Wpf/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ public class ChromiumWebBrowser : Control, IRenderWebBrowser, IWpfWebBrowser
/// </summary>
private int disposeSignaled;

/// <summary>
/// Used as workaround for issue https://github.com/cefsharp/CefSharp/issues/3021
/// </summary>
private long canExecuteJavascriptInMainFrameId;

/// <summary>
/// Gets a value indicating whether this instance is disposed.
/// </summary>
Expand Down Expand Up @@ -1251,8 +1256,21 @@ void IWebBrowserInternal.OnLoadError(LoadErrorEventArgs args)
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;
}

Expand Down
2 changes: 1 addition & 1 deletion CefSharp/Internals/IWebBrowserInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface IWebBrowserInternal : IWebBrowser
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);
Expand Down

0 comments on commit 1824a03

Please sign in to comment.