JS binding - breakpoint on FrameCToCpp GetV8Context

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 binding - breakpoint on FrameCToCpp GetV8Context

Postby SinnerSmile » Mon Jan 06, 2014 7:36 pm

As a continuation to my last post in this topic viewtopic.php?f=6&t=10652&start=10

For a moment I started to think, that I know, what i'm doing, now i'm not so sure.
Long story short: Got Var Ploaded in JS, that will turn to 1 when window.onload event appears, want to check for this value turned 1 in C++, and, as it will happend - do something and turn it to 0. Attempting to do this check in win32 main loop callback function. This way I'm trying to trigger some C++ function on JS event.

Got this code inside callback WndProc:
Code: Select all
        switch(msg) {//cases, cases//}       

   CefRefPtr<CefBrowser> browser;
   CefRefPtr<CefFrame> frame;
   CefRefPtr<CefV8Context> context;
   if (cf_handler.get()) {browser = cf_handler->GetBrowser();}
   if (browser.get()) {frame = browser->GetMainFrame();}
   if (frame.get()) {context = frame->GetV8Context();}
   if (context.get()) {CefRefPtr<CefV8Value> windowobj = context->GetGlobal();
    CefRefPtr<CefV8Value> GetPloaded = windowobj->GetValue("Ploaded");
   int Ploaded = GetPloaded->GetIntValue();

if (Ploaded == 1 && NiceCheck == 1) {
   context->GetGlobal()->SetValue("Ploaded", CefV8Value::CreateInt(0), V8_PROPERTY_ATTRIBUTE_NONE);
   NiceCheck = 0;
   jprint("Page is loaded - CEF");
}
}


   return DefWindowProc(hWnd,msg,wParam,lParam);


jprint prints string into JS div.

This thing builds fine, but on run gives breakpoint
> Brest.exe!CefFrameCToCpp::GetV8Context() Line 324 + 0x14 bytes C++
on
cef_v8context_t* _retval = struct_->get_v8context(struct_); string in frame_ctocpp.cc

I have a bit of simmilar stuff in jprint() function:
Code: Select all
void jprint(gchar *data) {
  CefRefPtr<CefBrowser> browser;
       if (cf_handler.get())
       browser = cf_handler->GetBrowser();

      if (browser.get()) {
  CefRefPtr<CefFrame> frame = browser->GetMainFrame();

 

  gchar *jprint = g_strdup_printf("%s%s%s", "print('", data, "');");

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


and jprint is triggered by button in WM_COMMAND, works fine, but if I put in this function
CefRefPtr<CefV8Context> context = frame->GetV8Context();,
just to test it, I see the same breakpoint behavior each time I push the button.
Full code, if needed: https://gist.github.com/anonymous/6d9661ffb769d21b5d6c
Sadly, wiki, and cefclient project did not helped me a lot with this =(
Hope for Your help.
SinnerSmile
Techie
 
Posts: 34
Joined: Sun Aug 25, 2013 8:07 am

Re: JS binding - breakpoint on FrameCToCpp GetV8Context

Postby SinnerSmile » Tue Jan 07, 2014 7:20 pm

eagerly waiting for answer yet, I'm stuck with this :(
SinnerSmile
Techie
 
Posts: 34
Joined: Sun Aug 25, 2013 8:07 am

Re: JS binding - breakpoint on FrameCToCpp GetV8Context

Postby magreenblatt » Tue Jan 07, 2014 7:28 pm

Why don't you start at a higher level and tell me what you're trying to accomplish. What is the use case?
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: JS binding - breakpoint on FrameCToCpp GetV8Context

Postby SinnerSmile » Tue Jan 07, 2014 8:56 pm

OK, Pretty simple case, I want to trigger some C++ function when window.onload JS event occurs, that's it.
Thought of doing it, by checking for JS variable in the main loop (variable set in JS, when window.onload occurs)
SinnerSmile
Techie
 
Posts: 34
Joined: Sun Aug 25, 2013 8:07 am

Re: JS binding - breakpoint on FrameCToCpp GetV8Context

Postby magreenblatt » Tue Jan 07, 2014 9:03 pm

SinnerSmile wrote:OK, Pretty simple case, I want to trigger some C++ function when window.onload JS event occurs, that's it.
Thought of doing it, by checking for JS variable in the main loop (variable set in JS, when window.onload occurs)

1. Create a JS function window object binding as described here.
2. Call the function from JavaScript:
Code: Select all
window.onload = window.myfunc;
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: JS binding - breakpoint on FrameCToCpp GetV8Context

Postby SinnerSmile » Tue Jan 07, 2014 11:20 pm

Ah, yes, Thank You, that's far more elegant way, yet, I need to get a V8context. As I get it, there is not so much difference in binding value or function, in particular, something tells me, that, if i drop this binding code in WM_CREATE, I will struck at the same issue with GetV8Context, that I encountered, when trying to bind a value.

Just tested, dropped some lines that gets a frame and then frame->GetV8Context(); in WM_CREATE after CreateBrowserSynch();
same > Brest.exe!CefFrameCToCpp::GetV8Context() Line 324 + 0x14 bytes C++ breakpoint appears
Am I doing something totally wrong?
SinnerSmile
Techie
 
Posts: 34
Joined: Sun Aug 25, 2013 8:07 am

Re: JS binding - breakpoint on FrameCToCpp GetV8Context

Postby magreenblatt » Tue Jan 07, 2014 11:49 pm

The V8 context can only be accessed from the renderer process. That is most easily done using the OnContextCreated method. See the GeneralUsage wiki page for useful background information.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: JS binding - breakpoint on FrameCToCpp GetV8Context

Postby SinnerSmile » Fri Jan 10, 2014 5:27 pm

Good evening.
I digged more in this, and I can hope, that I finally understood some obvious things.
Yet, I can't properly trigger C++ function on JS event.
I made small header with classes, that, as I understand it, must implement this function binding, here's the code:
Code: Select all
#ifndef MYV8STUFF_H_
#define MYV8STUFF_H_

#include "include/cef_client.h"

class MyV8handler : public CefV8Handler {

public:
   MyV8handler() { }
   virtual bool Execute(const CefString& name, CefRefPtr<CefV8Value> object, const CefV8ValueList& arguments, CefRefPtr<CefV8Value>& retval, CefString& exception) OVERRIDE
   {
      if (name == "post") {
      // Return my string value.
      retval = CefV8Value::CreateString("TestingJSBinding");
      return true;
      }

    // Function does not exist.
    return false;
   }
   IMPLEMENT_REFCOUNTING(MyV8handler);
};


class WinBindhandler : public CefRenderProcessHandler {

public:
   WinBindhandler() { }
    void OnContextCreated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context) OVERRIDE
   {
    // Retrieve the context's window object.
    CefRefPtr<CefV8Value> object = context->GetGlobal();
    // Create an instance of my CefV8Handler object.
    CefRefPtr<CefV8Handler> handler = new MyV8handler();
   
     CefRefPtr<CefV8Value> func = CefV8Value::CreateFunction("post", handler);
   
     object->SetValue("post", func, V8_PROPERTY_ATTRIBUTE_NONE);
   }
   IMPLEMENT_REFCOUNTING(WinBindhandler);
};



#endif


in my main .cpp only added #include "MyV8Stuff.h"

Whole thing compiles well, but not working.
in JS I got window.onload = alert(window.post()); and window.post() on HTML button onclick() event.
On page load i see no alert box, on button click the same, each time Visual studio console gives me
[0111/002858:INFO:CONSOLE(14)] "Uncaught TypeError: Object [object global] has no method 'post'", source: http://localhost:4242/chattest (14)
I set some breakpoints on overrided Execute and OnContextCreated in my classes, they are never triggered.
What am I missing?
SinnerSmile
Techie
 
Posts: 34
Joined: Sun Aug 25, 2013 8:07 am

Re: JS binding - breakpoint on FrameCToCpp GetV8Context

Postby magreenblatt » Fri Jan 10, 2014 5:40 pm

You need to implement CefApp to return the CefRenderProcessHandler instance, and pass the CefApp instance into CefExecuteProcess.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: JS binding - breakpoint on FrameCToCpp GetV8Context

Postby SinnerSmile » Fri Jan 10, 2014 5:43 pm

magreenblatt wrote:You need to implement CefApp to return the CefRenderProcessHandler instance, and pass the CefApp instance into CefExecuteProcess.

Ah, can't get the idea of how I need exactly to do this on the run without some digging in, can You, maybe, give me some example, or direction where can I find it?
SinnerSmile
Techie
 
Posts: 34
Joined: Sun Aug 25, 2013 8:07 am

Next

Return to Support Forum

Who is online

Users browsing this forum: No registered users and 204 guests