CEF3 js callback function

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.

CEF3 js callback function

Postby StudyCef » Sun Jun 05, 2016 2:45 am

My CEF3 version is
Code: Select all
#define CEF_VERSION "3.2357.1291.g47e6d4b"
#define CEF_VERSION_MAJOR 3
#define CEF_COMMIT_NUMBER 1291
#define CEF_COMMIT_HASH "47e6d4bf84444eb6cb4d4509231a8c9ee878a584"
#define COPYRIGHT_YEAR 2015

#define CHROME_VERSION_MAJOR 43
#define CHROME_VERSION_MINOR 0
#define CHROME_VERSION_BUILD 2357
#define CHROME_VERSION_PATCH 130

I can c++ Call js In CefV8HandlerImpl.cpp
Code: Select all
// in CefV8HandlerImpl.cpp
bool CefV8HandlerImpl::Execute(const CefString& name
   , CefRefPtr<CefV8Value> object               
   , const CefV8ValueList& arguments           
   , CefRefPtr<CefV8Value>& retval             
   , CefString& exception)                       
{
   // In the CefV8Handler::Execute implementation for “registerJavascriptFunction”.
   bool handle = false;
   if (name == "registerJavascriptFunction"){
      if (arguments.size() == 2 && arguments[0]->IsString() &&
         arguments[1]->IsFunction()) {
         //std::string message_name = arguments[0]->GetStringValue();
         std::string message_name = "binding_test";

         CefRefPtr<CefV8Context> context = CefV8Context::GetCurrentContext();
         int browser_id = context->GetBrowser()->GetIdentifier();
         client_app_rendererImpl_->SetMessageCallback(message_name, browser_id, context, arguments[1]);
         handle = true;
         return true;
      }
   }
else if (name == "sendMessage"){
         typedef std::map<std::pair<std::string, int>, std::pair<CefRefPtr<CefV8Context>, CefRefPtr<CefV8Value> > >  CallbackMap;
         CefRefPtr<CefV8Context> context = CefV8Context::GetCurrentContext();
         if (!client_app_rendererImpl_->callback_map_.empty()) {
            const CefString& message_name = "binding_test";
            CallbackMap::const_iterator it = client_app_rendererImpl_->callback_map_.find(
               std::make_pair(message_name.ToString(),
               context->GetBrowser()->GetIdentifier()));

            if (it != client_app_rendererImpl_->callback_map_.end()) {
               // Keep a local reference to the objects. The callback may remove itself
               // from the callback map.
               CefRefPtr<CefV8Context> context = it->second.first;
               CefRefPtr<CefV8Value> callback = it->second.second;      
               // Enter the context.
               context->Enter();               
               CefV8ValueList arguments;
               // First argument is the message name.
               arguments.push_back(CefV8Value::CreateString(message_name));
               CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create(message_name);
               CefRefPtr<CefListValue> list = message->GetArgumentList();
               arguments->SetString(0, cef_json);
               CefRefPtr<CefBrowser> browser = self->GetBrowser();*/
               Json::Value json;
               json["ClientSpeed"] = "My_success";
               std::string str = json.toStyledString();
               CefString cef_json = CefString(str);
         
               CefRefPtr<CefV8Value> args = CefV8Value::CreateString(cef_json);   
               arguments.push_back(args);
                  
               bool handled = false;
               // Execute the callback.
               CefRefPtr<CefV8Value> retval = callback->ExecuteFunction(NULL, arguments);
               if (retval.get()) {
                  if (retval->IsBool())
                     handled = retval->GetBoolValue();
               }
               // Exit the context.
               context->Exit();
            }
         }
         
      }



And I want to use c++ callback js callback function in RootWindowWin::RootWndProc ,but I don't know how to do it,And I don't find use c++ callback js callback function in CEF3 Demo in RootWindowWin::RootWndProc,this is I should can find method?
StudyCef
Techie
 
Posts: 25
Joined: Tue Dec 15, 2015 7:38 pm

Return to Support Forum

Who is online

Users browsing this forum: Majestic-12 [Bot] and 63 guests