Skip to content

Commit

Permalink
Core - Remove WebBrowserExtensions.RegisterJsObject and WebBrowserExt…
Browse files Browse the repository at this point in the history
…ensions.RegisterAsyncJsObject

This is a breaking change, these methods are being removed as they are used for LegacyBinding which is being drastically changed in #2977

Resolves #2990
  • Loading branch information
amaitland committed Dec 11, 2019
1 parent a9d39d7 commit ffaff5d
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 83 deletions.
24 changes: 1 addition & 23 deletions CefSharp.Wpf.Example/Views/BrowserTabView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,12 @@ public BrowserTabView()

browser.RequestHandler = new ExampleRequestHandler();

//See https://github.com/cefsharp/CefSharp/issues/2246 for details on the two different binding options
if (CefSharpSettings.LegacyJavascriptBindingEnabled)
{
browser.RegisterJsObject("bound", new BoundObject(), options: BindingOptions.DefaultBinder);
}
else
{
//Objects can still be pre registered, they can also be registered when required, see ResolveObject below
//browser.JavascriptObjectRepository.Register("bound", new BoundObject(), isAsync:false, options: BindingOptions.DefaultBinder);
}

var bindingOptions = new BindingOptions()
{
Binder = BindingOptions.DefaultBinder.Binder,
MethodInterceptor = new MethodInterceptorLogger() // intercept .net methods calls from js and log it
};

//See https://github.com/cefsharp/CefSharp/issues/2246 for details on the two different binding options
if (CefSharpSettings.LegacyJavascriptBindingEnabled)
{
browser.RegisterAsyncJsObject("boundAsync", new AsyncBoundObject(), options: bindingOptions);
}
else
{
//Objects can still be pre registered, they can also be registered when required, see ResolveObject below
//browser.JavascriptObjectRepository.Register("boundAsync", new AsyncBoundObject(), isAsync: true, options: bindingOptions);
}

//To use the ResolveObject below and bind an object with isAsync:false we must set CefSharpSettings.WcfEnabled = true before
//the browser is initialized.
CefSharpSettings.WcfEnabled = true;
Expand Down Expand Up @@ -147,7 +125,7 @@ public BrowserTabView()
var errorBody = string.Format("<html><body bgcolor=\"white\"><h2>Failed to load URL {0} with error {1} ({2}).</h2></body></html>",
args.FailedUrl, args.ErrorText, args.ErrorCode);
args.Frame.LoadHtml(errorBody, base64Encode:true);
args.Frame.LoadHtml(errorBody, base64Encode: true);
};

CefExample.RegisterTestResources(browser);
Expand Down
60 changes: 0 additions & 60 deletions CefSharp/WebBrowserExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using CefSharp.Internals;
Expand All @@ -20,65 +19,6 @@ namespace CefSharp
/// </summary>
public static class WebBrowserExtensions
{
#region Legacy Javascript Binding
/// <summary>
/// Validates the browser before objects are registered
/// </summary>
private static void ValidateBrowserBeforeRegistering(this IWebBrowser webBrowser, [CallerMemberName] string callerName = "")
{
if (!CefSharpSettings.LegacyJavascriptBindingEnabled)
{
throw new Exception(@"CefSharpSettings.LegacyJavascriptBindingEnabled is currently false,
for legacy binding you must set CefSharpSettings.LegacyJavascriptBindingEnabled = true
before registering your first object see https://github.com/cefsharp/CefSharp/issues/2246
for details on the new binding options. If you perform cross-site navigations bound objects will
no longer be registered and you will have to migrate to the new method.");
}

if (webBrowser.IsBrowserInitialized)
{
throw new Exception("Browser is already initialized. " + callerName + " must be " +
"called before the underlying CEF browser is created.");
}

webBrowser.ThrowExceptionIfDisposed();
}

/// <summary>
/// Registers a Javascript object in this specific browser instance.
/// </summary>
/// <param name="webBrowser">The browser to perform the registering on</param>
/// <param name="name">The name of the object. (e.g. "foo", if you want the object to be accessible as window.foo).</param>
/// <param name="objectToBind">The object to be made accessible to Javascript.</param>
/// <param name="options">binding options - camelCaseJavascriptNames default to true </param>
/// <exception cref="Exception">Browser is already initialized. RegisterJsObject must be +
/// called before the underlying CEF browser is created.</exception>
public static void RegisterJsObject(this IWebBrowser webBrowser, string name, object objectToBind, BindingOptions options = null)
{
CefSharpSettings.WcfEnabled = true;
webBrowser.ValidateBrowserBeforeRegistering();
webBrowser.JavascriptObjectRepository.Register(name, objectToBind, false, options);
}

/// <summary>
/// <para>Asynchronously registers a Javascript object in this specific browser instance.</para>
/// <para>Only methods of the object will be availabe.</para>
/// </summary>
/// <param name="webBrowser">The browser to perform the registering on</param>
/// <param name="name">The name of the object. (e.g. "foo", if you want the object to be accessible as window.foo).</param>
/// <param name="objectToBind">The object to be made accessible to Javascript.</param>
/// <param name="options">binding options - camelCaseJavascriptNames default to true </param>
/// <exception cref="Exception">Browser is already initialized. RegisterJsObject must be +
/// called before the underlying CEF browser is created.</exception>
/// <remarks>The registered methods can only be called in an async way, they will all return immeditaly and the resulting
/// object will be a standard javascript Promise object which is usable to wait for completion or failure.</remarks>
public static void RegisterAsyncJsObject(this IWebBrowser webBrowser, string name, object objectToBind, BindingOptions options = null)
{
webBrowser.ValidateBrowserBeforeRegistering();
webBrowser.JavascriptObjectRepository.Register(name, objectToBind, true, options);
}
#endregion

/// <summary>
/// Returns the main (top-level) frame for the browser window.
/// </summary>
Expand Down

0 comments on commit ffaff5d

Please sign in to comment.