Refactoring - Moved RegisterJsObject and RegisterAsyncJsObject to WebBrowserExtensions (#2703)

This commit is contained in:
Chris
2019-03-23 04:31:09 +01:00
committed by Alex Maitland
parent 12b27f91d5
commit 987f988dac
5 changed files with 73 additions and 251 deletions
-76
View File
@@ -545,82 +545,6 @@ namespace CefSharp.OffScreen
}
}
/// <summary>
/// Registers a Javascript object in this specific browser instance.
/// </summary>
/// <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="System.Exception">Browser is already initialized. RegisterJsObject must be +
/// called before the underlying CEF browser is created.</exception>
public void RegisterJsObject(string name, object objectToBind, BindingOptions options = null)
{
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 (IsBrowserInitialized)
{
throw new Exception("Browser is already initialized. RegisterJsObject must be " +
"called before the underlying CEF browser is created.");
}
//Enable WCF if not already enabled
CefSharpSettings.WcfEnabled = true;
var objectRepository = managedCefBrowserAdapter.JavascriptObjectRepository;
if (objectRepository == null)
{
throw new Exception("Object Repository Null, Browser has likely been Disposed.");
}
objectRepository.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="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="System.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 void RegisterAsyncJsObject(string name, object objectToBind, BindingOptions options = null)
{
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 (IsBrowserInitialized)
{
throw new Exception("Browser is already initialized. RegisterJsObject must be " +
"called before the underlying CEF browser is created.");
}
var objectRepository = managedCefBrowserAdapter.JavascriptObjectRepository;
if (objectRepository == null)
{
throw new Exception("Object Repository Null, Browser has likely been Disposed.");
}
objectRepository.Register(name, objectToBind, true, options);
}
/// <summary>
/// The javascript object repository, one repository per ChromiumWebBrowser instance.
/// </summary>
+5 -81
View File
@@ -537,93 +537,17 @@ namespace CefSharp.WinForms
}
}
/// <summary>
/// Registers a Javascript object in this specific browser instance.
/// </summary>
/// <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="System.Exception">Browser is already initialized. RegisterJsObject must be +
/// called before the underlying CEF browser is created.</exception>
public void RegisterJsObject(string name, object objectToBind, BindingOptions options = null)
{
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 (IsBrowserInitialized)
{
throw new Exception("Browser is already initialized. RegisterJsObject must be " +
"called before the underlying CEF browser is created.");
}
InitializeFieldsAndCefIfRequired();
//Enable WCF if not already enabled
CefSharpSettings.WcfEnabled = true;
var objectRepository = managedCefBrowserAdapter.JavascriptObjectRepository;
if (objectRepository == null)
{
throw new Exception("Object Repository Null, Browser has likely been Disposed.");
}
objectRepository.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="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="System.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 void RegisterAsyncJsObject(string name, object objectToBind, BindingOptions options = null)
{
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 (IsBrowserInitialized)
{
throw new Exception("Browser is already initialized. RegisterJsObject must be " +
"called before the underlying CEF browser is created.");
}
InitializeFieldsAndCefIfRequired();
var objectRepository = managedCefBrowserAdapter.JavascriptObjectRepository;
if (objectRepository == null)
{
throw new Exception("Object Repository Null, Browser has likely been Disposed.");
}
objectRepository.Register(name, objectToBind, true, options);
}
/// <summary>
/// The javascript object repository, one repository per ChromiumWebBrowser instance.
/// </summary>
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public IJavascriptObjectRepository JavascriptObjectRepository
{
get { return managedCefBrowserAdapter == null ? null : managedCefBrowserAdapter.JavascriptObjectRepository; }
get
{
InitializeFieldsAndCefIfRequired();
return managedCefBrowserAdapter == null ? null : managedCefBrowserAdapter.JavascriptObjectRepository;
}
}
/// <summary>
-75
View File
@@ -2297,81 +2297,6 @@ namespace CefSharp.Wpf
}
}
/// <summary>
/// Registers a Javascript object in this specific browser instance.
/// </summary>
/// <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 void RegisterJsObject(string name, object objectToBind, BindingOptions options = null)
{
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 (InternalIsBrowserInitialized())
{
throw new Exception("Browser is already initialized. RegisterJsObject must be " +
"called before the underlying CEF browser is created.");
}
//Enable WCF if not already enabled
CefSharpSettings.WcfEnabled = true;
var objectRepository = managedCefBrowserAdapter.JavascriptObjectRepository;
if (objectRepository == null)
{
throw new Exception("Object Repository Null, Browser has likely been Disposed.");
}
objectRepository.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="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="System.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 void RegisterAsyncJsObject(string name, object objectToBind, BindingOptions options = null)
{
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 (InternalIsBrowserInitialized())
{
throw new Exception("Browser is already initialized. RegisterJsObject must be " +
"called before the underlying CEF browser is created.");
}
var objectRepository = managedCefBrowserAdapter.JavascriptObjectRepository;
if (objectRepository == null)
{
throw new Exception("Object Repository Null, Browser has likely been Disposed.");
}
objectRepository.Register(name, objectToBind, true, options);
}
/// <summary>
/// The javascript object repository, one repository per ChromiumWebBrowser instance.
/// </summary>
-19
View File
@@ -79,25 +79,6 @@ namespace CefSharp
/// <param name="url">The URL to be loaded.</param>
void Load(string url);
/// <summary>
/// Registers a Javascript object in this specific browser instance.
/// </summary>
/// <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>
void RegisterJsObject(string name, object objectToBind, BindingOptions options = null);
/// <summary>
/// <para>Asynchronously registers a Javascript object in this specific browser instance.</para>
/// <para>Only methods of the object will be available.</para>
/// </summary>
/// <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>
/// <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>
void RegisterAsyncJsObject(string name, object objectToBind, BindingOptions options = null);
/// <summary>
/// The javascript object repository, one repository per ChromiumWebBrowser instance.
/// </summary>
+68
View File
@@ -6,6 +6,7 @@ using System;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using CefSharp.Internals;
@@ -18,6 +19,65 @@ 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>
@@ -1039,6 +1099,14 @@ namespace CefSharp
return stringBuilder.ToString();
}
private static void ThrowExceptionIfDisposed(this IWebBrowser browser)
{
if (browser.IsDisposed)
{
throw new ObjectDisposedException("browser", "Browser has been disposed");
}
}
private static void ThrowExceptionIfFrameNull(IFrame frame)
{
if (frame == null)