Preference of CefRegisterExtension over CreateFunction()

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Preference of CefRegisterExtension over CreateFunction()

Postby swtsvn » Mon Apr 27, 2015 10:31 am

Hi all,
I am new to Cef and JS. Can anyone kindly point me towards why one would use one method over the other ?
In CefRegisterExtension method() , the JS is provided as a string, It is called from render thread, and is not advisable to call bulky native functions.
In CreateFunction() method as well, it has to be called from render thread in OnContextCreated(), and is not advisable to handle bulky native functions.
Both can handle callbacks to JS functions.

I am not sure what they mean by complex extensions using CefRegisterExtensions in https://bitbucket.org/chromiumembedded/ ... ow-binding.

Can anyone throw light on when or why one method could be preferred over another. ?
swtsvn
Techie
 
Posts: 21
Joined: Wed Apr 22, 2015 4:56 pm

Re: Preference of CefRegisterExtension over CreateFunction()

Postby swtsvn » Tue Apr 28, 2015 10:43 am

Anyone ?
swtsvn
Techie
 
Posts: 21
Joined: Wed Apr 22, 2015 4:56 pm

Re: Preference of CefRegisterExtension over CreateFunction()

Postby Cherno » Wed Apr 29, 2015 1:18 am

I use an extension to register an object with native callbacks towards the c++ layer (that I always want to be in the browsers).
When I need a function on a non permanent basis, then I use a create function.

Both require communication across processes (render -> UI), so get used to call from js in async way (with success / fail callbacks back to js)

C.
Cherno
Techie
 
Posts: 11
Joined: Thu Apr 09, 2015 5:55 am

Re: Preference of CefRegisterExtension over CreateFunction()

Postby swtsvn » Thu Apr 30, 2015 2:46 pm

Thanks Cherno,

Do you mean, everytime a new browser/tab (CefBrowserHost::CreateBrowser) is created, it would call OnContextCreated(), and attach the functions to the window of the browser, thereby using more twice the memory for those functions, if there are two browsers in total?
Which would mean onWebKitInitialized would be called only once through the lifespan of the application, irrespective of the total browsers/tabs created, and all of then can acccess the native functions, and there will be only one set of memory alloted for these functions, thereby saving memory?

Or something else? Can you elaborate on "on a non permanent basis"?
swtsvn
Techie
 
Posts: 21
Joined: Wed Apr 22, 2015 4:56 pm

Re: Preference of CefRegisterExtension over CreateFunction()

Postby Cherno » Mon May 04, 2015 4:00 am

swtsvn wrote:Thanks Cherno,

Do you mean, everytime a new browser/tab (CefBrowserHost::CreateBrowser) is created, it would call OnContextCreated(), and attach the functions to the window of the browser, thereby using more twice the memory for those functions, if there are two browsers in total?
Which would mean onWebKitInitialized would be called only once through the lifespan of the application, irrespective of the total browsers/tabs created, and all of then can acccess the native functions, and there will be only one set of memory alloted for these functions, thereby saving memory?

Or something else? Can you elaborate on "on a non permanent basis"?


I don´t have the knowledge about CEF or Chrome V8 internals in order to evaluate the memory cost/savings of extensions vs functions, but I assume that extensions also take memory per browser for each of the functions registered at the browser "window" context. If is there any memory saving by not manually registering the functions at OnContextCreated, it might be small. In my limited knowledge, the benefit I get with the extension is having a common API registered without much effort (function registering and handling is harder to manage, due to the multiprocess nature of CEF/Chrome).

I can´t imagine a case where you would register thousands of functions so the cost would be a burden, but maybe your needs are different than mine.

In order to decrease (a lot) the amount of functions I need outside of the common api, I use async messages with success/fail callbacks back to javascript. I find it much easier to maintain than registering functions. I only use functions if I really need a synchronous answer, but I try to avoid it as much as possible, as async calls don´t block the render process and are easier to transform to standard xhr calls if I ever use that code on a web server.
Cherno
Techie
 
Posts: 11
Joined: Thu Apr 09, 2015 5:55 am

Re: Preference of CefRegisterExtension over CreateFunction()

Postby swtsvn » Mon May 04, 2015 11:59 am

Thanks Cherno,
I am talking in terms of about 40 functions. all of them asynchronous. JS calls the native code function -> which is sent to render thread-> which is sent to custom thread that executes the functions -> sends back results to render thread -> which calls javascript callback function.

Currently I am using CreateFunction() because it is easier to iterate over the function-name array and call createfunction for each.

I use async messages with success/fail callbacks back to javascript. I find it much easier to maintain than registering functions.

It seems like the asyn messages you are talking about, are different from the one I mentioned above where, those async functions are also using CreateFunction() in my implementation. Can you kindly elaborate a bit on the async message process you are talking about?
swtsvn
Techie
 
Posts: 21
Joined: Wed Apr 22, 2015 4:56 pm

Re: Preference of CefRegisterExtension over CreateFunction()

Postby Cherno » Tue May 05, 2015 1:41 am

I have registered an extension with a function to send a data request using the cefQuery support (so I don´t have to do the Render -> UI comm by myself) :

Code: Select all
            BFCef.dataRequest = function(dataId, success_callback, failure_callback) {
                return window.cefQuery({
                    request: dataId,
                    persistent: false,
                    onSuccess: success_callback,
                    onFailure: failure_callback
                });
           };


At the java side, I call the function with all the data I´ll need at the c++ side encoded as a json object:

Code: Select all
        BFCef.dataRequest(JSON.stringify({ request: "AccountingStats", query: "getStats" }),
            function (response) {
                var parsedResponse = JSON.parse(response);
                ... (code that handles response data) ...       
                })
            },
            function (error, errorMsg) {
                ... (code that handles error) ...
            }
        );


For the c++ side, I can register query handlers by browser and then I handle the requests at the corresponding handler:

Code: Select all
bool XXXX::OnAsyncQuery(const String& request, CefRefPtr<BFCefQueryCallback> callback)
{
    Json::Reader reader;
    Json::Value jRequest;
    if( reader.parse(request, jRequest) && jRequest["request"].isString() )
    {
        const String rqId = jRequest["request"].asString();

        ... code dealing with requests by type ...

        callback->Success(strResponse);  // finally call the success or failure callback with a json object string
        return true;
    }

    callback->Failure(-1, "No query handler available");
    return false;
}


Both the request and the answer back to the callback use json objects to communicate. It adds a bit of overhead for the callbacks and the encode/decode of the JSon, but so far I am ok paying that price.
Cherno
Techie
 
Posts: 11
Joined: Thu Apr 09, 2015 5:55 am

Re: Preference of CefRegisterExtension over CreateFunction()

Postby swtsvn » Fri May 22, 2015 10:49 am

Thanks Cherno for your input.
swtsvn
Techie
 
Posts: 21
Joined: Wed Apr 22, 2015 4:56 pm


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 42 guests