title: Chromium Embedded how to execute a javascript callback from c++?
body: <p>Recently I have been trying to bind my application to CEF, the process was slow but I made some progress over the last days. However there is one thing I just can't get to work.</p>
  
  <p>What I want to achieve is:
   1. Registering a callback function within Javascript (These functions will be called when an event happens in my application)
   2. Save the callback.
   3. When the event occurs, execute the registered callback.</p>
  
  <p>Now I got step 1 and 2 to work, but step 3 is where the problem is.
  I tested it first by registering a callback and then calling it with an executeCallback() function in Javascript and it worked. But whenever I try to execute the callback from within C++ it doesn't work.</p>
  
  <p>I create the setEventCallback in the onContextCread()</p>
  
  <pre><code>CefRefPtr&lt;CefV8Value&gt; object = context-&gt;GetGlobal();
  CefRefPtr&lt;CefV8Value&gt; simplixObj = CefV8Value::CreateObject(NULL);
  object-&gt;SetValue("simplix", simplixObj, V8_PROPERTY_ATTRIBUTE_NONE);
  func = CefV8Value::CreateFunction("setEventCallback", $simplixHandler);
  simplixObj-&gt;SetValue("setEventCallback", func, V8_PROPERTY_ATTRIBUTE_NONE);
  </code></pre>
  
  <p>With $simplixHandler being an instance of my own handler.
  Here is the setEventCallback in my handler:</p>
  
  <pre><code>bool SimplixHandler::setEventCallback(CefRefPtr&lt;CefV8Value&gt; object, const CefV8ValueList&amp; arguments, CefRefPtr&lt;CefV8Value&gt;&amp; retval, CefString&amp; exception)
  if (arguments.size() == 2 &amp;&amp; arguments[0]-&gt;IsString() &amp;&amp; arguments[1]-&gt;IsFunction()) {
      //Get name of callback
      std::string callbackName = arguments[0]-&gt;GetStringValue();
      //Get the current context
      CefRefPtr&lt;CefV8Context&gt; context = CefV8Context::GetCurrentContext();
      //Insert into callbacks
      $callbacks[callbackName] = std::make_pair(context, arguments[1]);
  
      return true;
  }
  else if(arguments.size() != 2) {
      exception = CefString("Invalid amount of parameters. Expected 2, received " + arguments.size());
      return false;
  }
  else {
      exception = CefString("Incompatible parameter types, expected (string, function).");
      return false;
  }
  </code></pre>
  
  <p>I then try to execute the callbacks by calling executeCallback:</p>
  
  <pre><code>bool SimplixHandler::executeCallback(std::string name, CefRefPtr&lt;CefListValue&gt; list) {
  bool handled = false;
  
  if (!$callbacks.empty() &amp;&amp; $callbacks.count(name) != 0) {
      std::pair&lt;CefRefPtr&lt;CefV8Context&gt;, CefRefPtr&lt;CefV8Value&gt;&gt; pair = $callbacks[name];
      CefRefPtr&lt;CefV8Context&gt; context = pair.first;
      CefRefPtr&lt;CefV8Value&gt; func = pair.second;
  
      context-&gt;Enter();
      CefV8ValueList callArgs;
  
      //First argument is function name
      CefRefPtr&lt;CefV8Value&gt; args = CefV8Value::CreateArray(list-&gt;GetSize());
      setList(list, args);
      callArgs.push_back(args);
  
      CefRefPtr&lt;CefV8Value&gt; retval = func-&gt;ExecuteFunction(NULL, callArgs);
      if (retval.get()) {
          if (retval-&gt;IsBool())
              handled = retval-&gt;GetBoolValue();
      }
  
      context-&gt;Exit();
  }
  
  return handled;
  }
  </code></pre>
  
  <p>I call this function from within my SimplixHandler, however I call callbacks by calling the specific event function, example: </p>
  
  <pre><code>bool SimplixHandler::notifyDirectoryChanged(std::string directory) {
  CefRefPtr&lt;CefListValue&gt; list = CefListValue::Create();
  
  list-&gt;SetSize(1);
  list-&gt;SetString(0, directory);
  
  return executeCallback("onDirectoryChange", list);
  }
  </code></pre>
  
  <p>And finally my Javascript:</p>
  
  <pre><code>function dirChange(dir) {
      document.getElementById("filename").innerHTML = "It's something";
  }
  
  simplix.setEventCallback("onDirectoryChange", dirChange);
  </code></pre>
  
  <p>I know the event gets called (because I first had nullpointer exception due to wrong linking of classes, that's fixed now), but the Javascript Callback does not get called.</p>
  
  <p>Does anyone know what I am doing wrong?</p>
  
  <p>Thanks in advance.</p>