Async calls via boundobject using callbacks?

372 views
Skip to first unread message

Vin Kamat

unread,
Feb 13, 2015, 1:14:48 PM2/13/15
to cefs...@googlegroups.com

In the current version of CefSharp pulled from nuget, is it possible to make Async calls from javascript to native C# bound object and return values via a callback?

I used to use an earlier fork where we had CefCallbackWrapper doing the job. 
Just wondering if there is something new in the current stable version to help with async calls. (using SchemaHandler and XHR is not an option for me at the moment as it involves tedious routing and mangled code.)

Javascript:
function CrunchImages(string imageCategory) {
    $
("#loadingDiv").show(); // show loading message
    bound
.CrunchImages(imageCategory, function(data) {
         $
("#result").html(data); // data would be a string returned by the C# class like "Images converted to thumbnails successfully!"
         $
("#loadingDiv").hide();
   
});
}


C# boundobject:
public string CrunchImages(string category)
{
   
// do the lengthy time consuming process here
   
return "Images converted to thumbnails successfully!";
}

Alex Maitland

unread,
Feb 15, 2015, 6:35:21 AM2/15/15
to cefs...@googlegroups.com
For true async `CefSharp` currently only supports the `SchemeHandler` approach. I seem to remember someone using one of the `OWIN` components with a `SchemeHandler` to do some of the plumbing, so that may not require as much code as you think.

There are some workarounds, like turning your method into a procedure which spawns a Task, when the task is complete then call `ExecuteScriptAsync`. I've seem many people use something similar.

With the introduction of IPC with `CEF3`, the matter of javascript callbacks become that much more complex until relatively recently. If your up for the challenge of implementing `AJSB` then the `CEF General Usage Guide` has a pretty detailed section.

https://code.google.com/p/chromiumembedded/wiki/GeneralUsage#Asynchronous_Bindings

Alex Maitland

unread,
Feb 15, 2015, 6:51:51 AM2/15/15
to cefs...@googlegroups.com
I should have also said modifying the current JSB implementation to support javascript function callbacks is possible, a little delicate....definitely possible.

https://code.google.com/p/chromiumembedded/wiki/JavaScriptIntegration#Using_JS_Callbacks

Vin Kamat

unread,
Feb 16, 2015, 3:08:20 PM2/16/15
to cefs...@googlegroups.com
Thanks. 

Is it possible to review this fork for JS Callbacks? I think this has already been solved in this fork.

Vin Kamat

unread,
Feb 16, 2015, 3:08:55 PM2/16/15
to cefs...@googlegroups.com
Forgot the link to the issue for the fork : https://github.com/cefsharp/CefSharp/pull/87

Alex Maitland

unread,
Feb 17, 2015, 3:29:32 AM2/17/15
to cefs...@googlegroups.com
Quick skim of the old PR and with it being targeted at `CefSharp1` and hence `Cef1`, so much has changed that I'm not sure any of it's relevant.

I think the changes required to add in a callback can be confined to the `BrowserSubProcess`. Without looking at it in huge detail I'd probably approach something like:

- Loop through the params and determine if there's a callback (function).
- If there's a function then store a reference to it and the relevant context
- Execute the `WCF` call in an async fashion, returning immediately letting `Cef` know the request has been handled.
- When the async call has finished then execute the callback.

Of course being partly in `c++` makes things so much harder, as there's no anonymous delegates to make life super easy.

As I said, haven't really though it thorough, that's just a rough idea.
Reply all
Reply to author
Forward
0 new messages