文章目录
- JS调用C++方法,OnQuery消息传递线程过程详解
- CefSettings.multi_threaded_message_loop参数
- multi_threaded_message_loop
- external_message_pump
- MainMessageLoopStd
- 实验
JS调用C++方法,OnQuery消息传递线程过程详解
之前的文章已经提到过JS调用C++方法的方式,我在开发的过程中碰到一个问题需要验证:
从JS发起的过程,到C++,也就是Browser进程这边,是不是都是同一个线程处理?
首先先琢磨了一下这个消息传递过程:
SimpleHandler::OnProcessMessageReceived
调用
CefMessageRouterBrowserSide的OnProcessMessageReceived:
bool SimpleHandler::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefProcessId source_process,CefRefPtr<CefProcessMessage> message) {CEF_REQUIRE_UI_THREAD();std::cout << "my_browser" << std::endl;if (message_router_->OnProcessMessageReceived(browser, frame, source_process,message)) {return true;}
}
接下来就是看一下OnProcessMessageReceived这个函数干了啥:
在CEF LIB代码的cef_message_router.cc文件中,这个文件定义了CefMessageRouterBrowserSide和CefMessageRouterRenderSide两个类:
基本差不多,我代码里是先用到了CefMessageRouterBrowserSide。
bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefProcessId source_process,CefRefPtr<CefProcessMessage> message) override {CEF_REQUIRE_UI_THREAD();const std::string& message_name = message->GetName();if (message_name == query_message_name_) {cmru::RendererMessage content = cmru::ParseRendererMessage(message);const int context_id = content.context_id;const int request_id = content.request_id;const bool persistent = content.is_persistent;if (handler_set_.empty()) {// No handlers so cancel the query.CancelUnhandledQuery(browser, frame, context_id, request_id);return true;}const int browser_id = browser->GetIdentifier();const int64_t query_id = query_id_generator_.GetNextId();CefRefPtr<CallbackImpl> callback =new CallbackImpl(this, browser_id, query_id, persistent,config_.message_size_threshold, query_message_name_);// Make a copy of the handler list in case the user adds or removes a// handler while we're iterating.const HandlerSet handlers = handler_set_;Handler* handler = std::visit([&](const auto& arg) -> CefMessageRouterBrowserSide::Handler* {for (auto handler : handlers) {bool ha