Call a Javascript function from 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.

Call a Javascript function from C++

Postby winapiwrapper » Thu Mar 01, 2018 9:27 pm

Hi,

I am creating a chat client with CEF3 on Windows 8.1.
I created a message-only window which can receive network messages at any time.

I would like to call Javascript functions by their name and without registering the callback via Javascript, like in this example: https://bitbucket.org/chromiumembedded/cef/wiki/JavaScriptIntegration.md#markdown-header-executing-functions.

I could use ExecuteJavascript, but will it be able to handle large strings containing Javascript objects? (those objects may contain binary data)

How to achieve this? Network messages can arrive at any time, that's why the solution needs to be thread-safe.

Also, how to access V8 from a C++ function and from outside the render process? C++ calls will come from the message-only window, but I don't know how to forward them.
winapiwrapper
Techie
 
Posts: 35
Joined: Sat Feb 24, 2018 10:27 am

Re: Call a Javascript function from C++

Postby Czarek » Fri Mar 02, 2018 2:27 am

Big data should be sent to javascript using network layer.

You can communicate with V8 renderer process using IPC messaging.
Maintainer of the CEF Python, PHP Desktop and CEF C API projects. My LinkedIn.
User avatar
Czarek
Virtuoso
 
Posts: 1927
Joined: Sun Nov 06, 2011 2:12 am

Re: Call a Javascript function from C++

Postby winapiwrapper » Fri Mar 02, 2018 8:46 am

What do you mean by "network layer"? I use Winsock to handle network operations.

IPC is pretty slow, can't I just call a Javascript function whenever I want in my C++ code, using V8?
winapiwrapper
Techie
 
Posts: 35
Joined: Sat Feb 24, 2018 10:27 am

Re: Call a Javascript function from C++

Postby winapiwrapper » Sat Mar 03, 2018 5:56 am

Do I really need to use IPC if I want to achieve that?
winapiwrapper
Techie
 
Posts: 35
Joined: Sat Feb 24, 2018 10:27 am

Re: Call a Javascript function from C++

Postby winapiwrapper » Sat Mar 10, 2018 8:44 am

I'm new to CEF, could anybody give me a hint?
Can I execute Javascript from any thread in my C++ app?
winapiwrapper
Techie
 
Posts: 35
Joined: Sat Feb 24, 2018 10:27 am

Re: Call a Javascript function from C++

Postby magreenblatt » Sat Mar 10, 2018 10:54 am

V8 functions can only be called from inside the renderer process. IPC is documented here: https://bitbucket.org/chromiumembedded/ ... t-bindings
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Call a Javascript function from C++

Postby winapiwrapper » Sat Mar 10, 2018 1:11 pm

Thank you!

I wrote the code below, but CefV8Context::GetCurrentContext is null when a message is received by the renderer.
ClientApp is my CefApp implementation and is defined like this: class ClientApp : public CefApp, public CefBrowserProcessHandler, public CefRenderProcessHandler

Code: Select all
bool ClientApp::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefProcessId source_process, CefRefPtr<CefProcessMessage> message)
{
   const std::string& msg = message->GetName();

   if (CefCurrentlyOn(TID_RENDERER))
   {      
      std::cout << "renderer" << std::endl;
      CefRefPtr<CefV8Context> context = CefV8Context::GetCurrentContext();
      std::cout << context << std::endl; // =00000000
      if (context != NULL)
      {
         CefV8ValueList args;
         MyV8Handler::g_callback->ExecuteFunctionWithContext(context, NULL, args);
      }

      return true;
   }

   return false;
}


In order to call my JS function, I created a thread in my browser process. This thread sends a message to the renderer which should execute a JS callback registered beforehand.
Here's what my thread does:

Code: Select all
// waits a few seconds
// then:
CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create(CefString("msg"));
   CefRefPtr<CefListValue> args = message->GetArgumentList();
   args->SetInt(0, 1);
   args->SetString(1, CefString("hi v8!"));

   browser->SendProcessMessage(PID_RENDERER, message);


Did I miss something? CefCurrentlyOn(TID_RENDERER) is true so I don't understand why GetCurrentContext is null.
winapiwrapper
Techie
 
Posts: 35
Joined: Sat Feb 24, 2018 10:27 am

Re: Call a Javascript function from C++

Postby magreenblatt » Sat Mar 10, 2018 3:13 pm

You will only have a current context when you enter C++ code from JS. Use CefFrame::GetV8Context for your use case.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Call a Javascript function from C++

Postby winapiwrapper » Sun Mar 11, 2018 7:14 am

Thank you very much! It works like a charm now!
winapiwrapper
Techie
 
Posts: 35
Joined: Sat Feb 24, 2018 10:27 am


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 51 guests