Skip to content

Commit

Permalink
Add IWebBrowser.CanExecuteJavascriptInMainFrame property, set to true…
Browse files Browse the repository at this point in the history
…/false when V8Context is created/released

Added checks in the execute/eval javscript methods in WebBrowserExtensions
Made possibly by the addition of OnContextReleased IPC message being added
  • Loading branch information
amaitland committed Mar 27, 2017
1 parent 0a2693f commit eecf3ad
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CefSharp.Core/Internals/ClientAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,8 @@ namespace CefSharp
CefFrameWrapper frameWrapper(browser->GetFrame(GetInt64(argList, 0)));

handler->OnContextCreated(_browserControl, browserWrapper, %frameWrapper);

_browserControl->SetCanExecuteJavascriptOnMainFrame(true);
}

handled = true;
Expand All @@ -1105,6 +1107,8 @@ namespace CefSharp
CefFrameWrapper frameWrapper(browser->GetFrame(GetInt64(argList, 0)));

handler->OnContextReleased(_browserControl, browserWrapper, %frameWrapper);

_browserControl->SetCanExecuteJavascriptOnMainFrame(false);
}

handled = true;
Expand Down
12 changes: 12 additions & 0 deletions CefSharp.OffScreen/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,13 @@ public class ChromiumWebBrowser : IRenderWebBrowser
/// </summary>
public event EventHandler NewScreenshot;

/// <summary>
/// A flag that indicates if you can execute javascript in the main frame.
/// Flag is set to true in IRenderProcessMessageHandler.OnContextCreated.
/// and false in IRenderProcessMessageHandler.OnContextReleased
/// </summary>
public bool CanExecuteJavascriptInMainFrame { get; private set; }

/// <summary>
/// Top Left position of the popup.
/// </summary>
Expand Down Expand Up @@ -938,6 +945,11 @@ void IWebBrowserInternal.SetTooltipText(string tooltipText)
TooltipText = tooltipText;
}

void IWebBrowserInternal.SetCanExecuteJavascriptOnMainFrame(bool canExecute)
{
CanExecuteJavascriptInMainFrame = canExecute;
}

/// <summary>
/// Creates a new bitmap with the dimensions of firstBitmap, then
/// draws the firstBitmap, then overlays the secondBitmap
Expand Down
13 changes: 13 additions & 0 deletions CefSharp.WinForms/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,14 @@ public class ChromiumWebBrowser : Control, IWebBrowserInternal, IWinFormsWebBrow
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public bool IsBrowserInitialized { get; private set; }

/// <summary>
/// A flag that indicates if you can execute javascript in the main frame.
/// Flag is set to true in IRenderProcessMessageHandler.OnContextCreated.
/// and false in IRenderProcessMessageHandler.OnContextReleased
/// </summary>
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public bool CanExecuteJavascriptInMainFrame { get; private set; }

/// <summary>
/// Initializes static members of the <see cref="ChromiumWebBrowser"/> class.
/// </summary>
Expand Down Expand Up @@ -664,6 +672,11 @@ void IWebBrowserInternal.OnLoadError(LoadErrorEventArgs args)
}
}

void IWebBrowserInternal.SetCanExecuteJavascriptOnMainFrame(bool canExecute)
{
CanExecuteJavascriptInMainFrame = canExecute;
}

/// <summary>
/// Gets the browser adapter.
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions CefSharp.Wpf/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,13 @@ public class ChromiumWebBrowser : ContentControl, IRenderWebBrowser, IWpfWebBrow
/// <value>The redo command.</value>
public ICommand RedoCommand { get; private set; }

/// <summary>
/// A flag that indicates if you can execute javascript in the main frame.
/// Flag is set to true in IRenderProcessMessageHandler.OnContextCreated.
/// and false in IRenderProcessMessageHandler.OnContextReleased
/// </summary>
public bool CanExecuteJavascriptInMainFrame { get; private set; }

/// <summary>
/// Initializes static members of the <see cref="ChromiumWebBrowser"/> class.
/// </summary>
Expand Down Expand Up @@ -861,6 +868,11 @@ void IWebBrowserInternal.OnLoadError(LoadErrorEventArgs args)
}
}

void IWebBrowserInternal.SetCanExecuteJavascriptOnMainFrame(bool canExecute)
{
CanExecuteJavascriptInMainFrame = canExecute;
}

/// <summary>
/// Gets the browser adapter.
/// </summary>
Expand Down
9 changes: 8 additions & 1 deletion CefSharp/IWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,14 @@ public interface IWebBrowser : IDisposable
string TooltipText { get; }

/// <summary>
/// Attempts to give focus to the IWpfWebBrowser control.
/// A flag that indicates if you can execute javascript in the main frame.
/// Flag is set to true in IRenderProcessMessageHandler.OnContextCreated.
/// and false in IRenderProcessMessageHandler.OnContextReleased
/// </summary>
bool CanExecuteJavascriptInMainFrame { get; }

/// <summary>
/// Attempts to give focus to the IWebBrowser control.
/// </summary>
/// <returns><c>true</c> if keyboard focus and logical focus were set to this element; <c>false</c> if only logical focus
/// was set to this element, or if the call to this method did not force the focus to change.</returns>
Expand Down
1 change: 1 addition & 0 deletions CefSharp/Internals/IWebBrowserInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public interface IWebBrowserInternal : IWebBrowser
void SetLoadingStateChange(LoadingStateChangedEventArgs args);
void SetTitle(TitleChangedEventArgs args);
void SetTooltipText(string tooltipText);
void SetCanExecuteJavascriptOnMainFrame(bool canExecute);

void OnFrameLoadStart(FrameLoadStartEventArgs args);
void OnFrameLoadEnd(FrameLoadEndEventArgs args);
Expand Down
16 changes: 16 additions & 0 deletions CefSharp/WebBrowserExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ public static void ExecuteScriptAsync(this IWebBrowser browser, string methodNam
/// <param name="script">The Javascript code that should be executed.</param>
public static void ExecuteScriptAsync(this IWebBrowser browser, string script)
{
if (browser.CanExecuteJavascriptInMainFrame == false)
{
throw new Exception(@"Unable to execute javascript at this time, scripts can only be executed within a V8Context.
Use the IWebBrowser.CanExecuteJavascriptInMainFrame property to guard against this exception.
See https://github.com/cefsharp/CefSharp/wiki/General-Usage#when-can-i-start-executing-javascript
for more details on when you can execute javascript.");
}

using (var frame = browser.GetMainFrame())
{
ThrowExceptionIfFrameNull(frame);
Expand Down Expand Up @@ -726,6 +734,14 @@ public static Task<JavascriptResponse> EvaluateScriptAsync(this IWebBrowser brow
throw new ArgumentOutOfRangeException("timeout", "Timeout greater than Maximum allowable value of " + UInt32.MaxValue);
}

if(browser.CanExecuteJavascriptInMainFrame == false)
{
throw new Exception(@"Unable to execute javascript at this time, scripts can only be executed within a V8Context.
Use the IWebBrowser.CanExecuteJavascriptInMainFrame property to guard against this exception.
See https://github.com/cefsharp/CefSharp/wiki/General-Usage#when-can-i-start-executing-javascript
for more details on when you can execute javascript.");
}

using (var frame = browser.GetMainFrame())
{
ThrowExceptionIfFrameNull(frame);
Expand Down

0 comments on commit eecf3ad

Please sign in to comment.