Repository navigation
Description
Due to Chromium changes the current JSB implementation no longer works as it previously did, bound objects are no longer available after cross-site navigation. For details on that see #1203 for historic details.
With the 63.0.0 release changes have been made to revamp the way objects are bound. There will be two options, the legacy behaviour which will still work for Single Page Applications and where only a single domain is used. The new features which will have quite a few advantages.
Legacy Binding
You will need to set CefSharpSettings.LegacyJavascriptBindingEnabled = true before you register your first object (both methods RegisterJsObject and RegisterAsyncJsObject).
//For legacy biding we'll still have support for
CefSharpSettings.LegacyJavascriptBindingEnabled = true;
browser.RegisterJsObject("bound", new BoundObject());
browser.RegisterAsyncJsObject("boundAsync", new BoundAsyncObject());If you perform cross-site navigation's you will no longer be able to use this method to bind objects. If you only use a single ChromiumWebBrowser instance in your application then it's possible to limit the number of render processes using the renderer-process-limit=1 command line flag. See https://github.com/cefsharp/CefSharp/blob/cefsharp/63/CefSharp.Example/CefExample.cs#L61 for an example.
New Binding Method
The new binding method has many advantages
- Bind and unbind objects by name
- Bind a subset of objects to different pages (including popups)
- Delete/unbind a method
- Resolve a bound object dynamically
Binding is now initiated by JavaScript, the CefSharp.BindObjectAsync method returns a Promise that is resolved when the objects are available. Objects are created in the global context (window, the same as the previous behaviour). If you call CefSharp.BindObjectAsync without any params then all registered objects will be bound. Binding by name is the more descriptive options.
<script type="text/javascript">
(async function()
{
// <embed user provided code here>
await CefSharp.BindObjectAsync("boundAsync", "bound");
boundAsync.hello('CefSharp').then(function (res)
{
assert.equal(res, "Hello CefSharp")
asyncCallback();
});
})();
(async () =>
{
await CefSharp.BindObjectAsync("boundAsync", "bound");
boundAsync.hello('CefSharp').then(function (res)
{
assert.equal(res, "Hello CefSharp")
asyncCallback();
});
})();
CefSharp.BindObjectAsync("boundAsync2").then(function(result)
{
boundAsync2.hello('CefSharp').then(function (res)
{
assert.equal(res, "Hello CefSharp")
// NOTE the ability to delete a bound object
assert.equal(true, CefSharp.DeleteBoundObject("boundAsync2"), "Object was unbound");
assert.ok(window.boundAsync2 === undefined, "boundAsync2 is now undefined");
asyncCallback();
});
});
</script>You have two options for registering an object in .Net, the first is similar to the old behaviour where an object is registered in advance. The second options is more flexible and allows objects to be Resolved when required.
//Firstly you can register an object in a similar fashion to before
//For standard object registration (equivalent to RegisterJsObject)
browser.JavascriptObjectRepository.Register("bound", new BoundObject(), false, options);
//For async object registration (equivalent to RegisterJsObject)
browser.JavascriptObjectRepository.Register("boundAsync", new BoundObject(), true, options);
//Ability to resolve an object, instead of forcing object registration before the browser has been initialized.
browser.JavascriptObjectRepository.ResolveObject += (sender, e) =>
{
var repo = e.ObjectRepository;
if (e.ObjectName == "boundAsync2")
{
repo.Register("boundAsync2", new AsyncBoundObject(), isAsync: true, options: bindingOptions);
}
};To be notified in .Net when objects have been bound in JavaScript then you can subscribe to the ObjectBoundInJavascript event
browser.JavascriptObjectRepository.ObjectBoundInJavascript += (sender, e) =>
{
var name = e.ObjectName;
Debug.WriteLine($"Object {e.ObjectName} was bound successfully.");
}; For reference the set of QUnit test cases used are https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Example/Resources/BindingTest.html
Activity
RegisterJsObject and RegisterAsyncJsObject can now only be used when …
RegisterJsObject and RegisterAsyncJsObject can now only be used when …
Implement Javascript Binding v2 (#2247)
Implement Javascript Binding v2 (#2247)
amaitland commentedon Jan 22, 2018
IJavascriptObjectRepository.ObjectBoundInJavascriptevent has been added. It's called once per object name that was bound, unclear if it should only be called once and passed in a list of names? Thoughts/comments welcome.Example111 commentedon Jan 23, 2018
I'm doing
browser.JavascriptObjectRepository.Register("boundAsync", new BoundObject(), true, options);However the method is getting null data. The paramters passed in the js call are null. If I use
then I get the parameters.
browser.JavascriptObjectRepository.Register("boundAsync", new BoundObject(), false, options);amaitland commentedon Jan 23, 2018
@Example111 Can you provide a mode detail example? Are you upgrading an existing app or developing a new one?
If you set
isAsync:truethen only methods on your object will be exposed, as there is no way to have anasyncproperty.Example111 commentedon Jan 23, 2018
Hi @amaitland sorry I wasn't clear. This is an existing app and was working fine with cross site and the old way to register.
We are trying something like this now this now:
Register:
browser.JavascriptObjectRepository.Register("bound", jsBridge, true, options);C# function:
JS:
window.bound.f({a: "1"});It works but our data parameter is always null. Instead of having the values passed from js
amaitland commentedon Jan 23, 2018
@Example111 Thanks for the more detailed example 👍
There was a minor breaking change to add support for
dynamickeyword, if you changeto
Basically use the interface instead of the implementation and hopefully it'll start working again. Will need to amend the documentation to alert people of the subtle change.
Or alternatively use the
dynamickeyword to access the object likechris-araman commentedon Jan 25, 2018
We use CEF on Mac and CefSharp on Windows. With the new CefSharp binding implementation, would we need to use Javascript logic like
CefSharp.BindObjectAsync(...)on Windows only, while maintaining our exisiting Javascript logic on Mac?amaitland commentedon Jan 26, 2018
@chris-araman You'd be the most qualified to answer your own questions as I've got no idea what your requirements are. Can you use the legacy mode? How many browser instances are you using at any one time? If only one then you can limit the number of render processes.
amaitland commentedon Jan 30, 2018
binding metadatahas been added, so subsequent calls toCefSharp.BindObjectAsyncwill obtain themetadatafrom the cache.CefSharp.BindObjectAsyncwill now return aobjectwithSuccess,Message, andCount(number of objects bound).Fixed a bug where
Propertiesweren't being analyzed forsyncbound objects.63.0.0-pre03will be out shortly.8 remaining items
fillmorejl commentedon Feb 19, 2018
Ok, I see now, the extra step of calling
CefSharp.BindObjectAsyncto get it into the DOM, completely overlooked that part. Thanks!amaitland commentedon Feb 19, 2018
No problem. I'll update the original post with some more details when I'm in front of a computer. Try make it clearer
omarkelov commentedon Feb 24, 2018
Hello! And sorry for my bad english.
I'm working on my first CefSharp app and I need some help.
Shortly, when the page is loaded the program does some usefull actions on the page and calls some methods in my BoundObject via JavaScript. After that it redirects to another url and does exactly the same actions and redirects again and so on...
The only problem is memory leaks. As I figured out, my app leaks if I use object binding in JavaScript. "cefsharp.browsersubprocess.exe" increases memory usage by 30Mb per every redirect. I guess I do some serious mistakes with binding object.
Here is main parts of my code:
script.js looks like:
I'v also tried to unbind object and set it undefined in javascript before redirection:
But it also didn't work. Memory usage continues to increase with every redirect.
I would be very grateful for any kind of help.
amaitland commentedon Feb 24, 2018
@CoolPixEll Please fork https://github.com/cefsharp/CefSharp.MinimalExample and provide a working example that reproduces your problem. Then push the changes to GitHub, no zip files thank you.
lewk2 commentedon Feb 27, 2018
I'm having a terrible time with the new object binding - I can get trivial binding to work using built-in types, but as soon as I have a method that returns an object, the promise just dies.
I have forked the minimal example, and created a very simple example where an object is bound. This object contains two methods, one returning a simple string and one returning a child object:
I can bind OK to the top-level object, but calls to the method that returns the ChildBoundObject doesn't work.
I attempted an alternative approach, which was to register the ChildBoundObject also within the
JavascriptObjectRepository_ResolveObjectmethod, but then i hit a problem where i could find no good way to update that object (I tried deleting it from JavaScript - but it never updated again.You can see the fork here:
https://github.com/lewk2/CefSharp.MinimalExample
Having a totally flat object heirarchy is a real pain for me, since I want to walk down an object graph and still be able to access methods as I go. It cannot be that what I'm doing is unsupported - it has to be a bug (I hope, otherwise I must return to IE11 land with the WebBrowser control - not a happy thought!)
amaitland commentedon Feb 27, 2018
@lewk2 Thanks for the reproducible example, it is indeed a bug. Short term you can return a struct or an array of classes, it's only returning a single class that's broken. https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Example/AsyncBoundObject.cs#L41
Objects are cached within the render process, deleting an object does not currently remove the object from the meta data cache, I will look to see what's involved in adding a second removeFromCache param.
lewk2 commentedon Feb 28, 2018
OK, I did a little more polishing of the test fork (which was actually missing a final git push, because I managed to miss the prompt for my password before leaving for home!).
I can confirm I can get an array of objects returned from a method - but they seem to come back looking more like a flat rendering of the properties of that class. I no longer have any callable methods, and now have accessible properties (which in full async mode should not exist).
I extended the fork to show the general broken case, and also the flattening effect.
I would also like to float the idea that perhaps having even basic type properties (string, int, bool) being able accessible without using promises would be a nice feature. It causes quite a lot of bloat in javascript to access lots of string fields with endless .then calls otherwise...
Regarding the object cache - my plan is to mount just a single proxy bridge class, and work from there. I only started to insert multiple objects as a work-around to my above problems. I could imagine people being blocked in the future if there is no way to remove / replace an object though through the lifecycle of the app... so having a true 'delete' (either in JavaScript or C#) is probably needed.
I now have a difficult choice - we have a project that needs to create a plugin architecture, and I must decide if we fall back to IE11 embedded (which is a total dead-end), move back to CEF legacy binding (which means API / object model we build now within plugins will probably break when we move forward) or to help get the modern bindings working well.
I'm happy to put some effort into 'getting the new stuff working' - but if this is going to take weeks, i need to pick another path...
Anything I can do to get testing or dev effort applied to improving this, I'm happy to pitch in.
amaitland commentedon Mar 3, 2018
Previous versions only supported returning
StructswithFields, support was added forPropertiesand subsequently the intention was to add support for classes as well. There is no support for accessing a method on an object returned from a method. The returned objects aren't bound. That functionality is complex and would require quite a lot of work. You are welcome to submit aPR.That's not possible with the
asyncimplementation. TheV8engine runs in an entirely different process and theasyncversion usesIPCto communicate. You can use thesyncversion if you require properties.As for the
.thencalls, I'd personally useawait.Legacybinding is only related to how objects are registered, there are still two options, thesyncandasyncversions.CefGlueandChromiumFxare both capableCEFwrappers,CefSharpisn't the only option.amaitland commentedon Mar 3, 2018
I should hopefully have time to resolve the final two items related to this issue in the coming week
classforasyncbinding (commit e47dd3e)General Usage Guideto include details on new binding implementation.I will then finally close #2234
amaitland commentedon Mar 8, 2018
The
63.0.2packages have been pushed toNuget.org, should appear shortly.Includes the bug fix for returning a class using
asyncbinding.amaitland commentedon Mar 23, 2018
The
General Usage Guidehas been updated, see https://github.com/cefsharp/CefSharp/wiki/General-Usage#3-how-do-you-expose-a-net-class-to-javascriptamaitland commentedon Mar 23, 2018
Additional note for those using
Legacy Bindingand creating multipleChromiumWebBrowserinstances that reference the samedomainand have the same object names registered, will need to isolate the browser instances through the use of aReuqestContext, so they have a uniqueRender Process, perChromiumWebBrowserinstance, the current implementation. See #2301 for one such case.There is the potential for this to be a problem with the newer implementation as well, should only be an issue if you bind a different object type using the same key. e.g. browser one has object of type
BoundOjbectTestregistered asboundand browser two has object of typeObjectTestBindregistered asbound. Same solution applies, create theChromiumWebBrowserinstances with a uniqueRequestContext. Again this should only be a problem if the samedomainis used, the browser will attempt to shareRender Processesbetween instances. This will be resolved in #2306.See RequestContext for more details
Metalfusion commentedon Jul 6, 2019
Seems that CefSharp.DeleteBoundObject(name) doesn't also perform the action of CefSharp.RemoveObjectFromCache(name).
Basically trying to replace a binding like this doesn't work:
browser.JavascriptObjectRepository.Register("myObj", myBoundObj, true, options); // C#await CefSharp.BindObjectAsync("myObj");// Using the bound object works here.await CefSharp.DeleteBoundObject("myObj");browser.JavascriptObjectRepository.Register("myObj", myBoundObj, true, options); // C#await CefSharp.BindObjectAsync("myObj");// Using the object here doesn't work because of the cache.Adding a call to CefSharp.RemoveObjectFromCache() before DeleteBoundObject() seems to fix the issue, but is unintuitive in my opinion.
In my particular use case I'm also using a dynamic object to which I add delegates and re-register the same instance to the JavascriptObjectRepository, but this shouldn't really affect the cache issue.
rajnithin commentedon Mar 5, 2023
getting error in javacript code Uncaught ReferenceError: CefSharp is not defined...i am not finding any .js file after adding cefsharp to my .net project in vs