Skip to content

Commit 987f988

Browse files
merceyzamaitland
authored andcommitted
Refactoring - Moved RegisterJsObject and RegisterAsyncJsObject to WebBrowserExtensions (#2703)
1 parent 12b27f9 commit 987f988

File tree

5 files changed

+73
-251
lines changed

5 files changed

+73
-251
lines changed

CefSharp.OffScreen/ChromiumWebBrowser.cs

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -545,82 +545,6 @@ public void Load(string url)
545545
}
546546
}
547547

548-
/// <summary>
549-
/// Registers a Javascript object in this specific browser instance.
550-
/// </summary>
551-
/// <param name="name">The name of the object. (e.g. "foo", if you want the object to be accessible as window.foo).</param>
552-
/// <param name="objectToBind">The object to be made accessible to Javascript.</param>
553-
/// <param name="options">binding options - camelCaseJavascriptNames default to true </param>
554-
/// <exception cref="System.Exception">Browser is already initialized. RegisterJsObject must be +
555-
/// called before the underlying CEF browser is created.</exception>
556-
public void RegisterJsObject(string name, object objectToBind, BindingOptions options = null)
557-
{
558-
if (!CefSharpSettings.LegacyJavascriptBindingEnabled)
559-
{
560-
throw new Exception(@"CefSharpSettings.LegacyJavascriptBindingEnabled is currently false,
561-
for legacy binding you must set CefSharpSettings.LegacyJavascriptBindingEnabled = true
562-
before registering your first object see https://github.com/cefsharp/CefSharp/issues/2246
563-
for details on the new binding options. If you perform cross-site navigations bound objects will
564-
no longer be registered and you will have to migrate to the new method.");
565-
}
566-
567-
if (IsBrowserInitialized)
568-
{
569-
throw new Exception("Browser is already initialized. RegisterJsObject must be " +
570-
"called before the underlying CEF browser is created.");
571-
}
572-
573-
//Enable WCF if not already enabled
574-
CefSharpSettings.WcfEnabled = true;
575-
576-
var objectRepository = managedCefBrowserAdapter.JavascriptObjectRepository;
577-
578-
if (objectRepository == null)
579-
{
580-
throw new Exception("Object Repository Null, Browser has likely been Disposed.");
581-
}
582-
583-
objectRepository.Register(name, objectToBind, false, options);
584-
}
585-
586-
/// <summary>
587-
/// <para>Asynchronously registers a Javascript object in this specific browser instance.</para>
588-
/// <para>Only methods of the object will be availabe.</para>
589-
/// </summary>
590-
/// <param name="name">The name of the object. (e.g. "foo", if you want the object to be accessible as window.foo).</param>
591-
/// <param name="objectToBind">The object to be made accessible to Javascript.</param>
592-
/// <param name="options">binding options - camelCaseJavascriptNames default to true </param>
593-
/// <exception cref="System.Exception">Browser is already initialized. RegisterJsObject must be +
594-
/// called before the underlying CEF browser is created.</exception>
595-
/// <remarks>The registered methods can only be called in an async way, they will all return immeditaly and the resulting
596-
/// object will be a standard javascript Promise object which is usable to wait for completion or failure.</remarks>
597-
public void RegisterAsyncJsObject(string name, object objectToBind, BindingOptions options = null)
598-
{
599-
if (!CefSharpSettings.LegacyJavascriptBindingEnabled)
600-
{
601-
throw new Exception(@"CefSharpSettings.LegacyJavascriptBindingEnabled is currently false,
602-
for legacy binding you must set CefSharpSettings.LegacyJavascriptBindingEnabled = true
603-
before registering your first object see https://github.com/cefsharp/CefSharp/issues/2246
604-
for details on the new binding options. If you perform cross-site navigations bound objects will
605-
no longer be registered and you will have to migrate to the new method.");
606-
}
607-
608-
if (IsBrowserInitialized)
609-
{
610-
throw new Exception("Browser is already initialized. RegisterJsObject must be " +
611-
"called before the underlying CEF browser is created.");
612-
}
613-
614-
var objectRepository = managedCefBrowserAdapter.JavascriptObjectRepository;
615-
616-
if (objectRepository == null)
617-
{
618-
throw new Exception("Object Repository Null, Browser has likely been Disposed.");
619-
}
620-
621-
objectRepository.Register(name, objectToBind, true, options);
622-
}
623-
624548
/// <summary>
625549
/// The javascript object repository, one repository per ChromiumWebBrowser instance.
626550
/// </summary>

CefSharp.WinForms/ChromiumWebBrowser.cs

Lines changed: 5 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -537,93 +537,17 @@ public void Load(string url)
537537
}
538538
}
539539

540-
/// <summary>
541-
/// Registers a Javascript object in this specific browser instance.
542-
/// </summary>
543-
/// <param name="name">The name of the object. (e.g. "foo", if you want the object to be accessible as window.foo).</param>
544-
/// <param name="objectToBind">The object to be made accessible to Javascript.</param>
545-
/// <param name="options">binding options - camelCaseJavascriptNames default to true </param>
546-
/// <exception cref="System.Exception">Browser is already initialized. RegisterJsObject must be +
547-
/// called before the underlying CEF browser is created.</exception>
548-
public void RegisterJsObject(string name, object objectToBind, BindingOptions options = null)
549-
{
550-
if (!CefSharpSettings.LegacyJavascriptBindingEnabled)
551-
{
552-
throw new Exception(@"CefSharpSettings.LegacyJavascriptBindingEnabled is currently false,
553-
for legacy binding you must set CefSharpSettings.LegacyJavascriptBindingEnabled = true
554-
before registering your first object see https://github.com/cefsharp/CefSharp/issues/2246
555-
for details on the new binding options. If you perform cross-site navigations bound objects will
556-
no longer be registered and you will have to migrate to the new method.");
557-
}
558-
559-
if (IsBrowserInitialized)
560-
{
561-
throw new Exception("Browser is already initialized. RegisterJsObject must be " +
562-
"called before the underlying CEF browser is created.");
563-
}
564-
565-
InitializeFieldsAndCefIfRequired();
566-
567-
//Enable WCF if not already enabled
568-
CefSharpSettings.WcfEnabled = true;
569-
570-
var objectRepository = managedCefBrowserAdapter.JavascriptObjectRepository;
571-
572-
if (objectRepository == null)
573-
{
574-
throw new Exception("Object Repository Null, Browser has likely been Disposed.");
575-
}
576-
577-
objectRepository.Register(name, objectToBind, false, options);
578-
}
579-
580-
/// <summary>
581-
/// <para>Asynchronously registers a Javascript object in this specific browser instance.</para>
582-
/// <para>Only methods of the object will be availabe.</para>
583-
/// </summary>
584-
/// <param name="name">The name of the object. (e.g. "foo", if you want the object to be accessible as window.foo).</param>
585-
/// <param name="objectToBind">The object to be made accessible to Javascript.</param>
586-
/// <param name="options">binding options - camelCaseJavascriptNames default to true </param>
587-
/// <exception cref="System.Exception">Browser is already initialized. RegisterJsObject must be +
588-
/// called before the underlying CEF browser is created.</exception>
589-
/// <remarks>The registered methods can only be called in an async way, they will all return immeditaly and the resulting
590-
/// object will be a standard javascript Promise object which is usable to wait for completion or failure.</remarks>
591-
public void RegisterAsyncJsObject(string name, object objectToBind, BindingOptions options = null)
592-
{
593-
if (!CefSharpSettings.LegacyJavascriptBindingEnabled)
594-
{
595-
throw new Exception(@"CefSharpSettings.LegacyJavascriptBindingEnabled is currently false,
596-
for legacy binding you must set CefSharpSettings.LegacyJavascriptBindingEnabled = true
597-
before registering your first object see https://github.com/cefsharp/CefSharp/issues/2246
598-
for details on the new binding options. If you perform cross-site navigations bound objects will
599-
no longer be registered and you will have to migrate to the new method.");
600-
}
601-
602-
if (IsBrowserInitialized)
603-
{
604-
throw new Exception("Browser is already initialized. RegisterJsObject must be " +
605-
"called before the underlying CEF browser is created.");
606-
}
607-
608-
InitializeFieldsAndCefIfRequired();
609-
610-
var objectRepository = managedCefBrowserAdapter.JavascriptObjectRepository;
611-
612-
if (objectRepository == null)
613-
{
614-
throw new Exception("Object Repository Null, Browser has likely been Disposed.");
615-
}
616-
617-
objectRepository.Register(name, objectToBind, true, options);
618-
}
619-
620540
/// <summary>
621541
/// The javascript object repository, one repository per ChromiumWebBrowser instance.
622542
/// </summary>
623543
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
624544
public IJavascriptObjectRepository JavascriptObjectRepository
625545
{
626-
get { return managedCefBrowserAdapter == null ? null : managedCefBrowserAdapter.JavascriptObjectRepository; }
546+
get
547+
{
548+
InitializeFieldsAndCefIfRequired();
549+
return managedCefBrowserAdapter == null ? null : managedCefBrowserAdapter.JavascriptObjectRepository;
550+
}
627551
}
628552

629553
/// <summary>

CefSharp.Wpf/ChromiumWebBrowser.cs

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,81 +2297,6 @@ public void UseLegacyKeyboardHandler()
22972297
}
22982298
}
22992299

2300-
/// <summary>
2301-
/// Registers a Javascript object in this specific browser instance.
2302-
/// </summary>
2303-
/// <param name="name">The name of the object. (e.g. "foo", if you want the object to be accessible as window.foo).</param>
2304-
/// <param name="objectToBind">The object to be made accessible to Javascript.</param>
2305-
/// <param name="options">binding options - camelCaseJavascriptNames default to true </param>
2306-
/// <exception cref="Exception">Browser is already initialized. RegisterJsObject must be +
2307-
/// called before the underlying CEF browser is created.</exception>
2308-
public void RegisterJsObject(string name, object objectToBind, BindingOptions options = null)
2309-
{
2310-
if (!CefSharpSettings.LegacyJavascriptBindingEnabled)
2311-
{
2312-
throw new Exception(@"CefSharpSettings.LegacyJavascriptBindingEnabled is currently false,
2313-
for legacy binding you must set CefSharpSettings.LegacyJavascriptBindingEnabled = true
2314-
before registering your first object see https://github.com/cefsharp/CefSharp/issues/2246
2315-
for details on the new binding options. If you perform cross-site navigations bound objects will
2316-
no longer be registered and you will have to migrate to the new method.");
2317-
}
2318-
2319-
if (InternalIsBrowserInitialized())
2320-
{
2321-
throw new Exception("Browser is already initialized. RegisterJsObject must be " +
2322-
"called before the underlying CEF browser is created.");
2323-
}
2324-
2325-
//Enable WCF if not already enabled
2326-
CefSharpSettings.WcfEnabled = true;
2327-
2328-
var objectRepository = managedCefBrowserAdapter.JavascriptObjectRepository;
2329-
2330-
if (objectRepository == null)
2331-
{
2332-
throw new Exception("Object Repository Null, Browser has likely been Disposed.");
2333-
}
2334-
2335-
objectRepository.Register(name, objectToBind, false, options);
2336-
}
2337-
2338-
/// <summary>
2339-
/// <para>Asynchronously registers a Javascript object in this specific browser instance.</para>
2340-
/// <para>Only methods of the object will be availabe.</para>
2341-
/// </summary>
2342-
/// <param name="name">The name of the object. (e.g. "foo", if you want the object to be accessible as window.foo).</param>
2343-
/// <param name="objectToBind">The object to be made accessible to Javascript.</param>
2344-
/// <param name="options">binding options - camelCaseJavascriptNames default to true </param>
2345-
/// <exception cref="System.Exception">Browser is already initialized. RegisterJsObject must be +
2346-
/// called before the underlying CEF browser is created.</exception>
2347-
/// <remarks>The registered methods can only be called in an async way, they will all return immeditaly and the resulting
2348-
/// object will be a standard javascript Promise object which is usable to wait for completion or failure.</remarks>
2349-
public void RegisterAsyncJsObject(string name, object objectToBind, BindingOptions options = null)
2350-
{
2351-
if (!CefSharpSettings.LegacyJavascriptBindingEnabled)
2352-
{
2353-
throw new Exception(@"CefSharpSettings.LegacyJavascriptBindingEnabled is currently false,
2354-
for legacy binding you must set CefSharpSettings.LegacyJavascriptBindingEnabled = true
2355-
before registering your first object see https://github.com/cefsharp/CefSharp/issues/2246
2356-
for details on the new binding options. If you perform cross-site navigations bound objects will
2357-
no longer be registered and you will have to migrate to the new method.");
2358-
}
2359-
2360-
if (InternalIsBrowserInitialized())
2361-
{
2362-
throw new Exception("Browser is already initialized. RegisterJsObject must be " +
2363-
"called before the underlying CEF browser is created.");
2364-
}
2365-
var objectRepository = managedCefBrowserAdapter.JavascriptObjectRepository;
2366-
2367-
if (objectRepository == null)
2368-
{
2369-
throw new Exception("Object Repository Null, Browser has likely been Disposed.");
2370-
}
2371-
2372-
objectRepository.Register(name, objectToBind, true, options);
2373-
}
2374-
23752300
/// <summary>
23762301
/// The javascript object repository, one repository per ChromiumWebBrowser instance.
23772302
/// </summary>

CefSharp/IWebBrowser.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,6 @@ public interface IWebBrowser : IDisposable
7979
/// <param name="url">The URL to be loaded.</param>
8080
void Load(string url);
8181

82-
/// <summary>
83-
/// Registers a Javascript object in this specific browser instance.
84-
/// </summary>
85-
/// <param name="name">The name of the object. (e.g. "foo", if you want the object to be accessible as window.foo).</param>
86-
/// <param name="objectToBind">The object to be made accessible to Javascript.</param>
87-
/// <param name="options">binding options - camelCaseJavascriptNames default to true </param>
88-
void RegisterJsObject(string name, object objectToBind, BindingOptions options = null);
89-
90-
/// <summary>
91-
/// <para>Asynchronously registers a Javascript object in this specific browser instance.</para>
92-
/// <para>Only methods of the object will be available.</para>
93-
/// </summary>
94-
/// <param name="name">The name of the object. (e.g. "foo", if you want the object to be accessible as window.foo).</param>
95-
/// <param name="objectToBind">The object to be made accessible to Javascript.</param>
96-
/// <param name="options">binding options - camelCaseJavascriptNames default to true </param>
97-
/// <remarks>The registered methods can only be called in an async way, they will all return immeditaly and the resulting
98-
/// object will be a standard javascript Promise object which is usable to wait for completion or failure.</remarks>
99-
void RegisterAsyncJsObject(string name, object objectToBind, BindingOptions options = null);
100-
10182
/// <summary>
10283
/// The javascript object repository, one repository per ChromiumWebBrowser instance.
10384
/// </summary>

0 commit comments

Comments
 (0)