handling js messages in c++

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.

handling js messages in c++

Postby gkellerer » Fri Sep 14, 2018 2:13 am

hi all,

i am new to cef development and got stuck knowing not how to reach my goal. I'd like to build a cef-based application. this application - lets call it Geoclient - should work as a bridge between an old mfc application and a new geo-service (like for e.g. google maps). so the workflow would be:
- user press button in application
- cef-based geoclient wil start and open the web page with the geodata service.
- user can see a given coordinate as point on the card. It is transferd from the geoclient to the webservice as an url parameter.
- now the user can move that point and click on an apply button to send the changed point back to the geoclient application.

and there is my problem. The web service sends a javascript message when i press the apply button and i do not know where to handle or even receive this missage in my geoclient application on the c++ side. i tried a lot by googling, reading the documentation and the wiki but i do not understand where to start.

thanks for your help,
Georg
gkellerer
Newbie
 
Posts: 7
Joined: Fri Sep 14, 2018 1:49 am

Re: handling js messages in c++

Postby salvadordf » Fri Sep 14, 2018 7:39 am

Maintainer of the CEF4Delphi, WebView4Delphi, WebUI4Delphi and WebUI4CSharp projects.
User avatar
salvadordf
Expert
 
Posts: 129
Joined: Sun Dec 18, 2016 8:39 am
Location: Spain

Re: handling js messages in c++

Postby gkellerer » Mon Sep 17, 2018 6:03 am

hello,

thanks for the links. i allready read this documents before posting but i still do not know where to start. Where exactly do i have to start if i want to receive js messages in my client app? is there a simple way to see all message posts that are sent from the website to the browser?
gkellerer
Newbie
 
Posts: 7
Joined: Fri Sep 14, 2018 1:49 am

Re: handling js messages in c++

Postby salvadordf » Mon Sep 17, 2018 6:26 am

I can only give you Delphi code so allow me to speak in generic terms.

You can register an extension and call that custom function in JavaScript code when the web service sends a message.
That custom function is executed in the render process in native code (c++)
If you need to send some results or notifications from that custom function to the browser process you can use the IPC functions from the second link.

See the "tests" directory in CEF3 source code for more details :
https://bitbucket.org/chromiumembedded/ ... ?at=master
Maintainer of the CEF4Delphi, WebView4Delphi, WebUI4Delphi and WebUI4CSharp projects.
User avatar
salvadordf
Expert
 
Posts: 129
Joined: Sun Dec 18, 2016 8:39 am
Location: Spain

Re: handling js messages in c++

Postby gkellerer » Mon Sep 17, 2018 7:18 am

hello,

it seems that i have problems in understanding the architecure behind the scenes. my client application opens a normal website with a form where a card is located and a button for applay changes. if i understood things right this form is present in the browser and the renderer process. when somebody now clicks on the button and the message is sent to the client application: from which context does that message start and where does it go? do i need to register a specific message to get notified in the client or is it possible to write a generic message handler in the client which is able to see every message that comes from the renerer or browser process? my questions may seem stupid bu ti am blind and i don't know if there were pictures in the documentation that would have made things clearer.
gkellerer
Newbie
 
Posts: 7
Joined: Fri Sep 14, 2018 1:49 am

Re: handling js messages in c++

Postby salvadordf » Mon Sep 17, 2018 8:04 am

Your application has an embedded web browser that opens a web page which includes some JavaScript function to receive the web service messages.
Let's call that JavaScript function "MessageReceiver".

In order to notify your application that a web service message has been received you need to create a custom JavaScript function. Let's call that function "MyCustomFunction" and use it inside "MessageReceiver".

"MyCustomFunction" is a especial JavaScript function that executes native code in your application and you have to create it "registering an extension" (this is explained in the first link)

All of that would be all you need in case the embedded web browser use a single process but Chromium creates a different process to execute JavaScript. That means that "MyCustomFunction" is executed in a different process and you need to use IPC functions to notify the main process in your application.

For more details about the multi-process architecture in Chromium read this :
http://www.chromium.org/developers/desi ... chitecture
Maintainer of the CEF4Delphi, WebView4Delphi, WebUI4Delphi and WebUI4CSharp projects.
User avatar
salvadordf
Expert
 
Posts: 129
Joined: Sun Dec 18, 2016 8:39 am
Location: Spain

Re: handling js messages in c++

Postby gkellerer » Tue Sep 18, 2018 3:30 am

firt of all i'd like to thank you for your patience. I understand things no much better. but there's one last question: in my cef-based application i have to wait for a window.postmessage event which contains the data i need. would it than be possible to add an event listener from cef to the js context that takes the data and copies it to variables availabe through js binding?
gkellerer
Newbie
 
Posts: 7
Joined: Fri Sep 14, 2018 1:49 am

Re: handling js messages in c++

Postby salvadordf » Tue Sep 18, 2018 3:49 am

Use the "ExecuteJavascript" function.

I'm sorry but I can't give you c++ examples but if you search "ExecuteJavascript" in this forum you will find many examples to do what you need.
Maintainer of the CEF4Delphi, WebView4Delphi, WebUI4Delphi and WebUI4CSharp projects.
User avatar
salvadordf
Expert
 
Posts: 129
Joined: Sun Dec 18, 2016 8:39 am
Location: Spain


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 31 guests