乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      [Chrome源碼閱讀]IPC通信初探

       wusiqi111 2020-09-16

      RenderWidgetHost繼承于IPC::Channel::Listener,所以它必然需要實現(xiàn)Listener::OnMessageReceived純虛函數(shù)。

      Chrome對這個函數(shù)進行了宏的封裝,宏的寫法有點類似于MFC/wxWidget的事件響應(yīng)表,不同點在于Chrome里的宏是對函數(shù)的實現(xiàn)封裝,而MFC/wxWidget里卻是對事件表的初始化。

      IPC::Channel::Sender類的實現(xiàn)

      class Channel : public Message ::Sender {// Security tests need access to the pipe handle.friend class ChannelTest ;public:// Implemented by consumers of a Channel to receive messages.class Listener {public:virtual ~ Listener () {}// Called when a message is received.virtual void OnMessageReceived ( const Message & message ) = 0;// Called when the channel is connected and we have received the internal// Hello message from the peer.virtual void OnChannelConnected ( int32 peer_pid ) {}// Called when an error is detected that causes the channel to close.// This method is not called when a channel is closed normally.virtual void OnChannelError () {}};

      RenderWidgetHost類需要處理RenderProcess(進程)發(fā)過來的一部分Message,更準確的說是RenderProcessHost接收到Render Process的消息,然后一層一層傳遞過來的。

      RenderWidgetHost里的宏,用來實現(xiàn)OnMessageReceived函數(shù)。以ViewHostMsg開頭的message class是從Render Process發(fā)向Render Process Host(browser)進程的。相反,ViewMsg開頭的類是從Render Process Host(browser) 發(fā)向Render Process的。它們都定義在文件\src\chrome\common\render_messages_internal.h中。
      IPC_DEFINE_MESSAGE_MAP (RenderWidgetHost )IPC_MESSAGE_HANDLER( ViewHostMsg_RenderViewReady , OnMsgRenderViewReady )IPC_MESSAGE_HANDLER( ViewHostMsg_RenderViewGone , OnMsgRenderViewGone )IPC_MESSAGE_HANDLER( ViewHostMsg_Close , OnMsgClose )IPC_MESSAGE_HANDLER( ViewHostMsg_RequestMove , OnMsgRequestMove )IPC_MESSAGE_HANDLER( ViewHostMsg_PaintRect , OnMsgPaintRect )IPC_MESSAGE_HANDLER( ViewHostMsg_ScrollRect , OnMsgScrollRect )IPC_MESSAGE_HANDLER( ViewHostMsg_HandleInputEvent_ACK , OnMsgInputEventAck )IPC_MESSAGE_HANDLER( ViewHostMsg_Focus , OnMsgFocus )IPC_MESSAGE_HANDLER( ViewHostMsg_Blur , OnMsgBlur )IPC_MESSAGE_HANDLER( ViewHostMsg_SetCursor , OnMsgSetCursor )IPC_MESSAGE_HANDLER( ViewHostMsg_ImeUpdateStatus , OnMsgImeUpdateStatus )IPC_MESSAGE_UNHANDLED_ERROR()IPC_END_MESSAGE_MAP ()
      我們現(xiàn)在嘗試著來拆解這些宏。這些宏定義在文件\src\chrome\common\ipc_message_macros.h中。
      首先是IPC_DEFINE_MESSAGE_MAP宏,傳遞的參數(shù)是一個類的名字:
      #define IPC_DEFINE_MESSAGE_MAP(class_name) \void class_name::OnMessageReceived(const IPC::Message& msg) \  IPC_BEGIN_MESSAGE_MAP(class_name, msg)

      如果傳遞的class_name是RenderWidgetHost,那么這個宏展開就是:

      void RenderWidgetHost::OnMessageReceived(const IPC::Message& msg)IPC_BEGIN_MESSAGE_MAP(RenderWidgetHost, msg)
      很明顯的就是它是函數(shù)OnMessageReceived的定義,接下來展開IPC_BEGIN_MESSAGE_MAP宏:
      #define IPC_BEGIN_MESSAGE_MAP(class_name, msg) \  { \    typedef class_name _IpcMessageHandlerClass; \    const IPC::Message& ipc_message__ = msg; \    bool msg_is_ok__ = true; \    switch (ipc_message__.type()) { \
      繼續(xù)展開來就是:
      {typedef RenderWidgetHost _IpcMessageHandlerClass;const IPC::Message& ipc_message__ = msg;bool msg_is_ok__ = true;switch (ipc_message__.type()) {
      看到這里,差不多明了函數(shù)要干的事情了,就是switch...case的組合,來判斷當(dāng)前的Message是什么類型,然后執(zhí)行響應(yīng)的函數(shù)體。
      宏IPC_MESSAGE_HANDLER(ViewHostMsg_RenderViewReady, OnMsgRenderViewReady):
      #define IPC_MESSAGE_FORWARD(msg_class, obj, member_func) \  case msg_class::ID: \    msg_is_ok__ = msg_class::Dispatch(&ipc_message__, obj, &member_func); \    break;#define IPC_MESSAGE_HANDLER(msg_class, member_func) \  IPC_MESSAGE_FORWARD(msg_class, this, _IpcMessageHandlerClass::member_func)

      看到case...關(guān)鍵字了。展開來就是:
      case ViewHostMsg_RenderViewReady::ID:msg_is_ok__ = ViewHostMsg_RenderViewReady::Dispatch(&ipc_message__, this, _IpcMessageHandlerClass::OnMsgRenderViewReady);break;

      所有的Msg_class類都繼承于IPC::Message,各種message class也是用宏來定義的。

      #define IPC_MESSAGE_ROUTED0(msg_class) \  class msg_class : public IPC::Message { \   public: \    enum { ID = msg_class##__ID }; \    msg_class(int32 routing_id) \        : IPC::Message(routing_id, ID, PRIORITY_NORMAL) {} \  };

      ID就是類的名字與"__ID"字符串的鏈接。

      IPC::Message這個類根據(jù)消息響應(yīng)函數(shù)參數(shù)的不同,提供了4個重載的Dispatch模板函數(shù):

      template<class T>static bool Dispatch(const Message* msg, T* obj, void (T::*func)()) {(obj->*func)();return true;}template<class T>static bool Dispatch(const Message* msg, T* obj, void (T::*func)() const) {(obj->*func)();return true;}template<class T>static bool Dispatch(const Message* msg, T* obj,void (T::*func)(const Message&)) {(obj->*func)(*msg);return true;}template<class T>static bool Dispatch(const Message* msg, T* obj,void (T::*func)(const Message&) const) {(obj->*func)(*msg);return true;}

      僅僅是簡單的轉(zhuǎn)調(diào)用傳入進來的類成員函數(shù)。

      所以總體來看,這一套宏定義了一個IPC::Channel::Listener繼承類的OnMessageReceived函數(shù)體,根據(jù)message的不同類型,轉(zhuǎn)調(diào)用響應(yīng)的成員函數(shù)體。

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多