Communication between browser process and render process in CEF

Source: Internet
Author: User
Tags skia



Https://bitbucket.org/chromiumembedded/cef/wiki/GeneralUsage mentioned several mechanisms that CEF provides for communication between the browser and browser processes, and I experimented with the process Runtime messages this way, using Cefprocessmessage and Cefbrowser::sendprocessmessage ().



I was in the CEF in the JS and C + + interactive article on the basis of the completion, we say the basic steps, the side gives the key code.


Foruok original, if you need to reprint please pay attention to Foruok subscription number "program Horizon" contact Foruok.

      1. Send Message


Send a message using Cefbrowser::sendprocessmessage (), sendprocessmessage the first parameter is Cefprocessid, send to BROWSER process, use Pid_browser, Send to the render process, using Pid_renderer.



My code for sending messages in the render process is as follows (within the Execute method of Clientv8handler):


CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create("login_msg");

        // Retrieve the argument list object.
        CefRefPtr<CefListValue> args = msg->GetArgumentList();

        // Populate the argument values.
        args->SetSize(2);
        args->SetString(0, strUser);
        args->SetString(1, strPassword);

        // Send the process message to the browser process.
        CefV8Context::GetCurrentContext()->GetBrowser()->SendProcessMessage(PID_BROWSER, msg);


I sent the login message (the render process) received in that Cef_js_integration example to the browser process.


      1. Processing messages


Browser process side, rewrite cefclient::onprocessmessagereceived () This method to handle cross-process messages.



In Cef_js_integration, for example, the Onprocessmessagereceived method is overridden in ClientHandler:


bool ClientHandler::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
    CefProcessId source_process,
    CefRefPtr<CefProcessMessage> message)
{
    const std::string& messageName = message->GetName();
    if (messageName == "login_msg") 
    {   
        // extract message
        CefRefPtr<CefListValue> args = message->GetArgumentList();
        CefString strUser = args->GetString(0);
        CefString strPassword = args->GetString(1);
        TCHAR szLog[256] = { 0 };
        _stprintf_s(szLog, 256, _T("BrowserProcess, user - %s, password - %s\r\n"), strUser.c_str(), strPassword.c_str());
        OutputDebugString(szLog);

        //send reply to render process
        CefRefPtr<CefProcessMessage> outMsg = CefProcessMessage::Create("login_reply");

        // Retrieve the argument list object.
        CefRefPtr<CefListValue> replyArgs = outMsg->GetArgumentList();

        // Populate the argument values.
        replyArgs->SetSize(1);
        replyArgs->SetInt(0, 0);

        // Send the process message to the renderer process.
        browser->SendProcessMessage(PID_RENDERER, outMsg);

        return true;
    }
    return false;
}


As you can see, the above code sends a message to the render process.



The render process side, overriding the Cefrenderprocesshandler::onprocessmessagereceived () method to handle messages from the browser process, in the Clientapprender class:


bool ClientAppRenderer::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
    CefProcessId source_process,
    CefRefPtr<CefProcessMessage> message)
{
    const std::string& messageName = message->GetName();
    if (messageName == "login_reply")
    {
        // extract message
        CefRefPtr<CefListValue> args = message->GetArgumentList();
        int status = args->GetInt(0);
        OutputDebugString(status == 0 ? _T("Renderer process, login ok\r\n") : _T("Renderer process, login failed\r\n"));
        CefRefPtr<CefFrame> frame = browser->GetMainFrame();
        frame->ExecuteJavaScript("alert(‘Got Login Reply from Browser process‘)", frame->GetURL(), 0);
        return true;
    }
    return false;
}


Well, that's the whole process.



Other reference articles:


    • CEF Windows Development Environment Setup
    • CEF Load Ppapi Plugin
    • VS2013 compiling the simplest Ppapi plugin
    • Understanding the design of PPAPI
    • Ppapi plugin-to-browser interaction process
    • Windows compiled from the source CEF
    • Media_stream_video Example of compiling PPAPI
    • PPAPI plug-in drawing and input event handling
    • Creating a local window in the PPAPI plug-in
    • PPAPI plug-in communication with the browser
    • Skia compiled from source under Windows
    • Using Skia drawing in the Ppapi plug-in
    • Load a picture resource in a DLL to generate a Skbitmap object in Skia
    • Ppapi+skia implementation of Graffiti Board
    • 3D graphics interface using chromium in PPAPI
    • Using OpenGL ES drawing in Ppapi
    • JS in CEF interacts with C + +


Communication between browser process and render process in CEF


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.