Calling a C++ function from javascript

Do not post support requests, bug reports or feature requests. Discuss CEF here. Non-CEF related discussion goes in General Discussion!

Calling a C++ function from javascript

Postby giridharb54 » Thu Nov 21, 2013 4:40 am

After checking out the Chromium Embedded Framework example I have a question. I need native interaction with the embedded part of my window. However, in the CEF example, all I saw was the c++ sending messages to the browser, not the other way around. I was wondering if there is any way to send a message from JavaScript from c++??? . I saw that there are IPC messaging and posting tasks. Please let us know the right way to do it.
giridharb54
Expert
 
Posts: 100
Joined: Thu Oct 17, 2013 2:15 am

Re: Calling a C++ function from javascript

Postby magreenblatt » Thu Nov 21, 2013 9:21 am

See the JavaScriptIntegration and GeneralUsage wiki pages.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Calling a C++ function from javascript

Postby giridharb54 » Thu Nov 28, 2013 8:13 am

hi ,

I went through the javascript integration and general usage wiki pages. I am succesfully able to hook up CEF3 with sample application. I am able to execute javascript from c++ via executescript. I still couldn't figure out a way to call c++ code from javascript. Please share some sample code,functions to call and use.
giridharb54
Expert
 
Posts: 100
Joined: Thu Oct 17, 2013 2:15 am

Re: Calling a C++ function from javascript

Postby magreenblatt » Thu Nov 28, 2013 9:18 am

Look at the usage of CefV8Handler.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Calling a C++ function from javascript

Postby giridharb54 » Thu Nov 28, 2013 10:14 am

Here is the code i have written.Please correct me if anything is wrong.
Code: Select all
   #include  "cefapplicationhandler.h"
#include "include/cef_app.h"
#include "applicationV8Handler.h"


bool cefapplicationhandler ::OnProcessMessageReceived(
                CefRefPtr<CefBrowser> browser,
                CefProcessId source_process,
                CefRefPtr<CefProcessMessage> message)  {
                       
      if (message->GetName() == "InvokeScript") {
   
            return true; // message handled
       }
      return false;
                       
}
void cefapplicationhandler::OnWebKitInitialized()
{
   std::string extensionCode =
  "var test;"
  "if (!test)"
  "  test = {};"
  "(function() {"
  "  test.myfunc = function() {"
  "    native function myfunc();"
  "    return myfunc();"
  "  };"
  "})();";

// Create an instance of my CefV8Handler object.
CefRefPtr<CefV8Handler> handler = new applicationV8Handler();

// Register the extension.
CefRegisterExtension("v8/test", extensionCode, handler);
}

Code: Select all
#include "include\cef_v8.h"

class applicationV8Handler : public CefV8Handler
{
public:
  applicationV8Handler() {}

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

    // Function does not exist.
    return false;
  }

  // Provide the reference counting implementation for this class.
  IMPLEMENT_REFCOUNTING(applicationV8Handler);
};


I worte the above code.it is not hitting the onwebkinitialised function at all.Please let me know how to make it work
giridharb54
Expert
 
Posts: 100
Joined: Thu Oct 17, 2013 2:15 am

Re: Calling a C++ function from javascript

Postby magreenblatt » Thu Nov 28, 2013 11:07 am

V8-related callbacks will be executed in the render process. The Wiki documents contain links to debugging tips. Make sure you implement CefApp::GetRenderProcessHandler().
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Calling a C++ function from javascript

Postby giridharb54 » Thu Nov 28, 2013 1:51 pm

hi i implemented CefApp::GetRenderProcessHandler().It is hitting the breakpoints now. I have a javascript code as below.
Code: Select all
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
alert(window.myfunc());
}
</script>
</head>

<body>

<button onclick="myFunction()">Try it</button>

<p>By clicking the button above, a function will be called. The function will alert a message.</p>

</body>
</html>


I have a breakpoint set inside execute function of applicationv8handler. when i click on the button i execute alert(window.myfunc());
it is not hitting the breakpoint in the execute function. Please let me know what should i change.
giridharb54
Expert
 
Posts: 100
Joined: Thu Oct 17, 2013 2:15 am

Re: Calling a C++ function from javascript

Postby giridharb54 » Thu Nov 28, 2013 3:32 pm

I figured it out.Basically i had to call alert(test.myfunc). thanks for the replies.
giridharb54
Expert
 
Posts: 100
Joined: Thu Oct 17, 2013 2:15 am


Return to CEF Discussion

Who is online

Users browsing this forum: No registered users and 151 guests