Welcome Guest Search | Active Topics | Sign In | Register

Call into C# code Options
Christian Porzio
Posted: Friday, September 1, 2017 11:54:19 AM
Rank: Advanced Member
Groups: Member

Joined: 10/4/2016
Posts: 104
Hi,

I read a couple of the posts that keep reminding the danger of calling C# code from JavaScript, yet your documentation is showing examples on how to do that: https://www.essentialobjects.com/doc/webbrowser/advanced/jsext.aspx

And I used it to implement a simple feature allowing my web page to toggle ON/OFF the access to the Debugger. And this works just fine.

Code: JavaScript
var SB_ALLOWED_DEBUG = false;

        // Call this function to turn ON/OFF the Inspect menu option in the contextual menu
        function ToggleDebug() {
            SB_ALLOWED_DEBUG = !SB_ALLOWED_DEBUG;
            setDebug(SB_ALLOWED_DEBUG)
        }


Code: C#
webContainer.WebView.RegisterJSExtensionFunction("setDebug", new JSExtInvokeHandler(WebView_JSSetDebug));


Code: C#
void WebView_JSSetDebug(object sender, JSExtInvokeArgs e)
        {
            try
            {
                WebDebug = (bool)e.Arguments[0];
            }
            catch (Exception ex)
            {
                // Handle exception here...
            }
        }


However as soon as I am going to make this available to my customers they will ask for probably more advanced features, and that is where I am getting cold feet.

So to begin with, is there any potential problem with the simple feature shown here?

What are the dangers one must avoid when implementing this callback pattern using RegisterJSExtensionFunction.

I thank you many advance for your input on that matter

Regards
eo_support
Posted: Friday, September 1, 2017 1:22:23 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,225
Hi,

There is no direct danger of calling into C# from JavaScript. In fact that's one half of the two way communication we provide: the other way being using EvalScript or QueueScriptCall to call JavaScript from C#.

There are however several implications that you must keep in mind:

1. For the Chromium browser engine, the render process runs the JavaScript engine. Once JavaScript engine calls into C#, it will wait until the C# call returns. During this period NOTHING will happen in the rendering process. This means screen will stop updating, mouse/keyboard events will not be processed, JavaScript timer will not be fired, etc. You can simply think the render will freeze during the C# call. This means it's important for your C# code to return as soon as possible;

2. The rendering process is in fact a different process than your process. So JavaScript and C# lives in two completely different OS processes. This means any calls in between in fact involves remote calls between two processes and there are performance overhead for such calls;

3. You can not call back into the JavaScript from your C# code. In another word, a call path of JavaScript -> JS extension function -> C# -> JavaScript is not allowed. Allowing this would cause JavaScript engine to be re-entered and could cause all kind of problems.

As long as you take precaution to avoid those issues, you can use the JS extension feature as much as you like.

Thanks!
Christian Porzio
Posted: Tuesday, September 5, 2017 6:39:47 PM
Rank: Advanced Member
Groups: Member

Joined: 10/4/2016
Posts: 104
Thank you so much!!!

This sums up very well the point.


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.