原文出处:https://bitbucket.org/chromiumembedded/cef/wiki/GeneralUsage#markdown-header-threads
Inter-Process Communication (IPC)
Since CEF3 runs in multiple processes it is necessary to provide mechanisms for communicating between those processes. CefBrowser and CefFrame objects exist in both the browser and render processes which helps to facilitate this process. Each CefBrowser and CefFrame object also has a unique ID value associated with it that will match on both sides of the process boundary.
Process Startup Messages
Startup data can be associated with a specific CefBrowser instance at creation time via the CefRefPtr extra_info parameter to CefBrowserHost::CreateBrowser. That extra_info data will be delivered to every renderer process associated with that CefBrowser via the CefRenderProcessHandler::OnBrowserCreated callback. See the “Processes” section for more information about when and how a new renderer process will be spawned.
Process Runtime Messages
Messages can be passed between processes at runtime using the CefProcessMessage class. These messages are associated with a specific CefBrowser and CefFrame instance and sent using the CefFrame::SendProcessMessage() method. The process message should contain whatever state information is required via CefProcessMessage::GetArgumentList().
// Create the message object.
CefRefPtr msg= CefProcessMessage::Create(“my_message”);// Retrieve the argument list object.
CefRefPtr args = msg>GetArgumentList();// Populate the argument values.
args->SetString(0, “my string”);
args->SetInt(0, 10);// Send the process message to the main frame in the render process.
// Use PID_BROWSER instead when sending a message to the browser process.
browser->GetMainFrame()->SendProcessMessage(PID_RENDERER, msg);
A message sent from the browser process to the render process will arrive in CefRenderProcessHandler::OnProcessMessageReceived(). A message sent from the render process to the browser process will arrive in CefClient::OnProcessMessageReceived().bool MyHandler::OnProcessMessageReceived(
CefRefPtr browser,
CefRefPtr frame,
CefProcessId source_process,
CefRefPtr message) {
// Check the message name.
const std::string& message_name = message->GetName();
if (message_name == “my_message”) {
// Handle the message here…
return true;
}
return false;
}