OnContextCreatedmethod never called? JavaScript window bind

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.

OnContextCreatedmethod never called? JavaScript window bind

Postby me12 » Thu Aug 07, 2014 8:43 am

Hi guys,

I am currently trying to get JavaScript window binding to work. Therefore I derived my ClientApp from CefRenderProcessHandler and implemented my custom OnContextCreatedmethod in the ClientApp.

Sadly the variable is undefined in JavaScript. Whats also strange, is that OnContextCreated is never called. (Or at least the breakpoint never triggers there. Even though I use "--renderer-startup-dialog" and attached the debugger to the render process. As described here: http://www.chromium.org/developers/how-tos/debugging)

Any ideas what I could have done wrong?

Here is the full source-code:

Code: Select all
// ClientApp.h
class ClientApp : public CView, public CefApp, public CefBrowserProcessHandler, public CefRenderProcessHandler  {
   
public:
    ClientApp();

   // CefApp methods:
   virtual CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler() OVERRIDE { return this; }

   // CefBrowserProcessHandler methods:
   virtual void OnContextInitialized() OVERRIDE;
   
   // CefRenderProcessHandler methods
   virtual void OnContextCreated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context) OVERRIDE;

private:
   // Include the default reference counting implementation.
   IMPLEMENT_REFCOUNTING(ClientApp);
};

// ClientApp.cpp

// [...]

void ClientApp::OnContextCreated( CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context )
{
   // Retrieve the context's window object.
   CefRefPtr<CefV8Value> object = context->GetGlobal();

   // Create a new V8 string value. See the "Basic JS Types" section below.
   CefRefPtr<CefV8Value> str = CefV8Value::CreateString("My Value!");

   // Add the string to the window object as "window.myval". See the "JS Objects" section below.
   object->SetValue("myval", str, V8_PROPERTY_ATTRIBUTE_NONE);
}


In my HTML-File I simply added:

Code: Select all
<script language="JavaScript">
alert(window.myval); // Shows an alert box with "My Value!"
</script>


Which always prints "undefined".

Any help would be appreciated.
me12
Techie
 
Posts: 31
Joined: Thu Jul 10, 2014 12:46 pm

Re: OnContextCreatedmethod never called? JavaScript window b

Postby magreenblatt » Thu Aug 07, 2014 8:52 am

You need to implement CefApp:GetRenderProcessHandler.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: OnContextCreatedmethod never called? JavaScript window b

Postby me12 » Fri Aug 08, 2014 6:51 am

Thank you very much for your help Marshall. That at least seems to work now.

CEF3 is a really cool project. I just wish there was a better documentation. (Or maybe I just can't to find it)

What I am currently trying to do is to read a value from JavaScript back to my C++ code. The main page about that (https://code.google.com/p/chromiumembed ... ntegration) sadly only shows some code-snippets and as a complete CEF3-newbie, you have no idea where to put them.

Is there somewhere a complete example to use that just does some JS variable passing and callbacks? Also the example solution with "cefclient" and "cefsimple" does not help much. cefsimple does not do anything with JS and cefclient is way to complex and seems to handle everything with an additional abstraction. (I.e. there is stuff with delegates, message routers, etc.)

Also what would be really helpful for everyone who is just starting out with CEF3, is a diagram that shows the different processes and what sub-threads they might have. what process is creating what and who is communicating with what in which way. Currently the documentation just gives some hints about that.

(Maybe the whole thing is easier to grasp if you come from CEF1.)

If anyone has a simple test app that sends a variable value from JS down to C++ or does something with callbacks, I would be very happy.
me12
Techie
 
Posts: 31
Joined: Thu Jul 10, 2014 12:46 pm

Re: OnContextCreatedmethod never called? JavaScript window b

Postby magreenblatt » Fri Aug 08, 2014 7:19 am

Have you seen https://code.google.com/p/chromiumembed ... neralUsage? It's not a simple example of just JavaScript usage, but it should answer most of your questions.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: OnContextCreatedmethod never called? JavaScript window b

Postby me12 » Fri Aug 08, 2014 8:57 am

Thanks for that fast response. Yes I read that page a few times. Yet I didn't get everything.

This is how I imagine the process/thread structure of CEF3: (No idea if this completely wrong though.)

Image

So the question is, how does this diagram map to CefClient and CefApp?

I thought the main process is called the browser (which is on the left in this diagram). This browser process also has the application logic. (If you don't specify otherwise with some flags). From that I would assume that this process is unique and spawns the other processes as needed.

But some lines later on the wiki page it says:

"Provide an implementation of CefClient to handle browser-instance-specific callbacks.
Call CefBrowserHost::CreateBrowser() to create a browser instance and manage the browser life span using CefLifeSpanHandler."

Oh, so we have multiple browsers then? Does this have anything to do with the browser process on my diagram?

And each browser can have multiple frames?
me12
Techie
 
Posts: 31
Joined: Thu Jul 10, 2014 12:46 pm


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 96 guests