diff --git a/CefSharp.OffScreen/ChromiumWebBrowser.cs b/CefSharp.OffScreen/ChromiumWebBrowser.cs index 5d5791b77..604d49c25 100644 --- a/CefSharp.OffScreen/ChromiumWebBrowser.cs +++ b/CefSharp.OffScreen/ChromiumWebBrowser.cs @@ -545,82 +545,6 @@ namespace CefSharp.OffScreen } } - /// - /// Registers a Javascript object in this specific browser instance. - /// - /// The name of the object. (e.g. "foo", if you want the object to be accessible as window.foo). - /// The object to be made accessible to Javascript. - /// binding options - camelCaseJavascriptNames default to true - /// Browser is already initialized. RegisterJsObject must be + - /// called before the underlying CEF browser is created. - 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); - } - - /// - /// Asynchronously registers a Javascript object in this specific browser instance. - /// Only methods of the object will be availabe. - /// - /// The name of the object. (e.g. "foo", if you want the object to be accessible as window.foo). - /// The object to be made accessible to Javascript. - /// binding options - camelCaseJavascriptNames default to true - /// Browser is already initialized. RegisterJsObject must be + - /// called before the underlying CEF browser is created. - /// 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. - 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); - } - /// /// The javascript object repository, one repository per ChromiumWebBrowser instance. /// diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index a5e16bfb3..e1abf18f6 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -537,93 +537,17 @@ namespace CefSharp.WinForms } } - /// - /// Registers a Javascript object in this specific browser instance. - /// - /// The name of the object. (e.g. "foo", if you want the object to be accessible as window.foo). - /// The object to be made accessible to Javascript. - /// binding options - camelCaseJavascriptNames default to true - /// Browser is already initialized. RegisterJsObject must be + - /// called before the underlying CEF browser is created. - 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); - } - - /// - /// Asynchronously registers a Javascript object in this specific browser instance. - /// Only methods of the object will be availabe. - /// - /// The name of the object. (e.g. "foo", if you want the object to be accessible as window.foo). - /// The object to be made accessible to Javascript. - /// binding options - camelCaseJavascriptNames default to true - /// Browser is already initialized. RegisterJsObject must be + - /// called before the underlying CEF browser is created. - /// 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. - 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); - } - /// /// The javascript object repository, one repository per ChromiumWebBrowser instance. /// [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] public IJavascriptObjectRepository JavascriptObjectRepository { - get { return managedCefBrowserAdapter == null ? null : managedCefBrowserAdapter.JavascriptObjectRepository; } + get + { + InitializeFieldsAndCefIfRequired(); + return managedCefBrowserAdapter == null ? null : managedCefBrowserAdapter.JavascriptObjectRepository; + } } /// diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs index 093869079..67b1f7254 100644 --- a/CefSharp.Wpf/ChromiumWebBrowser.cs +++ b/CefSharp.Wpf/ChromiumWebBrowser.cs @@ -2297,81 +2297,6 @@ namespace CefSharp.Wpf } } - /// - /// Registers a Javascript object in this specific browser instance. - /// - /// The name of the object. (e.g. "foo", if you want the object to be accessible as window.foo). - /// The object to be made accessible to Javascript. - /// binding options - camelCaseJavascriptNames default to true - /// Browser is already initialized. RegisterJsObject must be + - /// called before the underlying CEF browser is created. - 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); - } - - /// - /// Asynchronously registers a Javascript object in this specific browser instance. - /// Only methods of the object will be availabe. - /// - /// The name of the object. (e.g. "foo", if you want the object to be accessible as window.foo). - /// The object to be made accessible to Javascript. - /// binding options - camelCaseJavascriptNames default to true - /// Browser is already initialized. RegisterJsObject must be + - /// called before the underlying CEF browser is created. - /// 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. - 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); - } - /// /// The javascript object repository, one repository per ChromiumWebBrowser instance. /// diff --git a/CefSharp/IWebBrowser.cs b/CefSharp/IWebBrowser.cs index 3447e1d6a..75bf4ec48 100644 --- a/CefSharp/IWebBrowser.cs +++ b/CefSharp/IWebBrowser.cs @@ -79,25 +79,6 @@ namespace CefSharp /// The URL to be loaded. void Load(string url); - /// - /// Registers a Javascript object in this specific browser instance. - /// - /// The name of the object. (e.g. "foo", if you want the object to be accessible as window.foo). - /// The object to be made accessible to Javascript. - /// binding options - camelCaseJavascriptNames default to true - void RegisterJsObject(string name, object objectToBind, BindingOptions options = null); - - /// - /// Asynchronously registers a Javascript object in this specific browser instance. - /// Only methods of the object will be available. - /// - /// The name of the object. (e.g. "foo", if you want the object to be accessible as window.foo). - /// The object to be made accessible to Javascript. - /// binding options - camelCaseJavascriptNames default to true - /// 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. - void RegisterAsyncJsObject(string name, object objectToBind, BindingOptions options = null); - /// /// The javascript object repository, one repository per ChromiumWebBrowser instance. /// diff --git a/CefSharp/WebBrowserExtensions.cs b/CefSharp/WebBrowserExtensions.cs index 8dc8dc13f..e8e6e3310 100644 --- a/CefSharp/WebBrowserExtensions.cs +++ b/CefSharp/WebBrowserExtensions.cs @@ -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 /// public static class WebBrowserExtensions { + #region Legacy Javascript Binding + /// + /// Validates the browser before objects are registered + /// + 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(); + } + + /// + /// Registers a Javascript object in this specific browser instance. + /// + /// The browser to perform the registering on + /// The name of the object. (e.g. "foo", if you want the object to be accessible as window.foo). + /// The object to be made accessible to Javascript. + /// binding options - camelCaseJavascriptNames default to true + /// Browser is already initialized. RegisterJsObject must be + + /// called before the underlying CEF browser is created. + 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); + } + + /// + /// Asynchronously registers a Javascript object in this specific browser instance. + /// Only methods of the object will be availabe. + /// + /// The browser to perform the registering on + /// The name of the object. (e.g. "foo", if you want the object to be accessible as window.foo). + /// The object to be made accessible to Javascript. + /// binding options - camelCaseJavascriptNames default to true + /// Browser is already initialized. RegisterJsObject must be + + /// called before the underlying CEF browser is created. + /// 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. + public static void RegisterAsyncJsObject(this IWebBrowser webBrowser, string name, object objectToBind, BindingOptions options = null) + { + webBrowser.ValidateBrowserBeforeRegistering(); + webBrowser.JavascriptObjectRepository.Register(name, objectToBind, true, options); + } + #endregion + /// /// Returns the main (top-level) frame for the browser window. /// @@ -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)