JS induced C++ callbacks by using cef_message_router.h

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.

JS induced C++ callbacks by using cef_message_router.h

Postby dmKO » Fri May 20, 2016 12:40 pm

Hi all,

First of all: I am using Windows 10 and Visual Studio Community 2015, CEF 32bit branch 2454 and windowless rendering (OSR).

Following the usage example in cef_message_router.h, I am currently trying to induce C++ callbacks by calling a certain function (in this case window.cefQuery) in Javascript in order to start reading out DOM related CefV8Values (computed and filled in by Javascript) in my RenderProcessHandle maybe earlier than waiting for LoadHandler::OnLoadEnd for a frame.

More specific, my goal is to read out coordinates of DOM nodes (e.g. text input fields) via Javascript, with which the user might interact, and highlight them at the corresponding position in the texture, which I receive from my CefRenderHandler -- and that as soon as possible. But executing Javascript on the main frame's load start call is too early (DOM tree doesn't seem to have been loaded completely or some times even at all), and on load end seems quite late for showing an overlay at these coordinates on pages where a lot of subframes have to finish loading first. At the moment, I traverse the DOM tree on load start of the first subframe (if any exist), so that the main frame has already started and then again on OnLoadingStateChange callback with isLoading=false. But as mentioned, receiving those DOM coordinates asap would be much appreciated and JS induced callbacks seem promising.

I implemented everything according to the given usage example, testing it in cefsimple: That means, I added a member of type CefMessageRouterBrowserSide to SimpleHandler (CefClient), added CefMessageRouterRendererSide as member to SimpleApp (CefApp), wrote my own MyHandler which implements the interface CefMessageRouterBrowserSide::Handler and my own bool OnQuery(...) method in it.
I tested it by injecting the following Javascript code on load end of a frame:
Code: Select all
void SimpleHandler::OnLoadEnd(CefRefPtr<CefBrowser> browser,
   CefRefPtr<CefFrame> frame,
   int httpStatusCode)
{
   std::string cefQuery =
      "alert('Calling cefQuery...');\
      window.cefQuery(\
         {\
            request: 'my_request',\
            persistent: false,\
            onSuccess: function(response) { print(response); },\
            onFailure: function(error_code, error_message) {}\
         }\
      );\
      alert('cefQuery was called!'); ";

   frame->ExecuteJavaScript(cefQuery, frame->GetURL(), 0);
}

The first alert shows up, the second doesn't. Furthermore, MyHandler::OnQuery is not called, which would print something to the console when called. What leads me to the conclusion that window.cefQuery does not exist, although it was named like this in CefMessageRouterConfig.

So, I've read the whole usage example more carefully again and realized that the CefMessageRouter instances have to be created in the browser/renderer process. I checked it using CefCurrentlyOn(TID_UI) and CefCurrentlyOn(TID_RENDERER)... and because of CefCurrentlyOn(TID_RENDERER) in SimpleApp the whole program crashes. Here, Visual Studios callstack:
Code: Select all
>   libcef.dll!content::ContentClient::renderer() Line 76   C++
    libcef.dll!CefContentRendererClient::Get() Line 284   C++
    libcef.dll!CefTaskRunnerImpl::GetTaskRunner(cef_thread_id_t threadId) Line 54   C++
    libcef.dll!CefCurrentlyOn(cef_thread_id_t threadId) Line 13   C++
    libcef.dll!cef_currently_on(cef_thread_id_t threadId) Line 756   C++
    cefsimple.exe!CefCurrentlyOn(cef_thread_id_t threadId) Line 686   C++
    cefsimple.exe!SimpleApp::SimpleApp() Line 54   C++


I assume the non-existence of window.cefQuery might be linked to SimpleApp (maybe, there's no proof) not being on the renderer thread. But I also have no idea why it crashes when I try to identify the current thread. Did I miss something?

If you have any other ideas how to determine when to traverse the DOM tree when ready, let me know. ;)
Thanks in advance.

Daniel
Working on EU project MAMEM, developing GazeTheWeb - a gaze controlled web browser, based on (you'll guess it) CEF3. ;)
dmKO
Newbie
 
Posts: 5
Joined: Sat Mar 12, 2016 1:32 pm

Return to Support Forum

Who is online

Users browsing this forum: Google [Bot] and 52 guests