Chromium mojo
作者:互联网
参考来自:https://blog.csdn.net/qq_27022241/article/details/10390679
记录下虽然还不知道对不对
当前使用的是无响应的
render 端
定义接口
chromium\src\example\public\mojom\ping_responder.mojom 定义了mojom文件 来定义一个简单的Logger接口
module example.mojom; interface PingResponder { Ping() => (int32 random); };
chromium\src\example\public\mojom\BUILD.gn GN目标生成绑定
import("//mojo/public/tools/bindings/mojom.gni") mojom("mojom") { sources = [ "ping_responder.mojom" ] }
确保所有需要此接口的目标都依赖于此接口,在对应模块下的BUILD.gn中添加像这样的一行:
"//example/public/mojom",
在例子中,因为renderer和browser进程中都需要这个接口 因此两个进程都要在各自的模块下添加依赖
创建管道&发送消息&发送一个PendingReceiver 至Browser
我是在chromium\src\third_party\blink\renderer\core\html\canvas\html_canvas_element.cc/chromium\src\third_party\blink\renderer\core\html\canvas\html_canvas_element.h中整的
这个是成员变量
chromium\src\third_party\blink\renderer\core\html\canvas\html_canvas_element.h
#include "example/public/mojom/ping_responder.mojom.h" ....
//创建管道 mojo::Remote<example::mojom::PingResponder> ping_responder; mojo::PendingReceiver<example::mojom::PingResponder> receiver = ping_responder.BindNewPipeAndPassReceiver();
chromium\src\third_party\blink\renderer\core\html\canvas\html_canvas_element.cc
void OnPong(int32_t num) {} //发送消息
ping_responder->Ping(base::BindOnce(&OnPong));
在third_party/blink/renderer/core/BUILD.gn 添加依赖
"//example/public/mojom",
browser端
1.实现接口
chromium\src\content\browser\BUILD.gn
"//example/public/mojom",
chromium\src\content\browser\renderer_host\render_frame_host_impl.cc
PingResponderImpl::PingResponderImpl(mojo::PendingReceiver<example::mojom::PingResponder> receiver) : receiver_(this, std::move(receiver)) {} PingResponderImpl::~PingResponderImpl() {} // example::mojom::PingResponder: void PingResponderImpl::Ping(PingCallback callback) { // Respond with a random 4, chosen by fair dice roll. std::move(callback).Run(4); }
chromium\src\content\browser\renderer_host\render_frame_host_impl.h
class PingResponderImpl : example::mojom::PingResponder { public: explicit PingResponderImpl(mojo::PendingReceiver<example::mojom::PingResponder> receiver) ; PingResponderImpl(const PingResponderImpl&) = delete; PingResponderImpl& operator=(const PingResponderImpl&) = delete; ~PingResponderImpl() override ; // example::mojom::PingResponder: void Ping(PingCallback callback) override ; private: mojo::Receiver<example::mojom::PingResponder> receiver_; };
2.注册接口
chromium\src\content\browser\renderer_host\render_frame_host_impl.h
#include "example/public/mojom/ping_responder.mojom.h" ... class CONTENT_EXPORT RenderFrameHostImpl ... public: void GetPingResponder(mojo::PendingReceiver<example::mojom::PingResponder> receiver); private std::unique_ptr<PingResponderImpl> ping_responder_; }
chromium\src\content\browser\renderer_host\render_frame_host_impl.cc
void RenderFrameHostImpl::GetPingResponder( mojo::PendingReceiver<example::mojom::PingResponder> receiver) { ping_responder_ = std::make_unique<PingResponderImpl>(std::move(receiver)); }
chromium\src\content\browser\browser_interface_binders.cc
void PopulateFrameBinders(RenderFrameHostImpl* host, mojo::BinderMap* map) { ... map->Add<example::mojom::PingResponder>(base::BindRepeating( &RenderFrameHostImpl::GetPingResponder, base::Unretained(host)));
编译
1.编译生成接口对应的源文件
使用ninja编译目标:
//在src/out/default目录下执行 ninja example/public/mojom
执行完毕后,将会在chromium\src\out\Default\gen\example\public\mojom中生成相应的文件
2.编译content模块
使用ninja编译:
// 在src/out//Default目录下执行 ninja content
至此,大功告成,编译后会Log输出“hello”。
标签:src,mojom,chromium,example,public,PingResponderImpl,Chromium,mojo 来源: https://www.cnblogs.com/Galesaur-wcy/p/15642737.html