MessageLink.h (2753B)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * vim: sw=2 ts=4 et : 3 */ 4 /* This Source Code Form is subject to the terms of the Mozilla Public 5 * License, v. 2.0. If a copy of the MPL was not distributed with this 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 7 8 #ifndef ipc_glue_MessageLink_h 9 #define ipc_glue_MessageLink_h 10 11 #include <cstdint> 12 #include "base/message_loop.h" 13 #include "mojo/core/ports/node.h" 14 #include "mojo/core/ports/port_ref.h" 15 #include "mozilla/UniquePtr.h" 16 #include "mozilla/ipc/ScopedPort.h" 17 18 namespace IPC { 19 class Message; 20 class MessageReader; 21 class MessageWriter; 22 } // namespace IPC 23 24 namespace mozilla { 25 namespace ipc { 26 27 class MessageChannel; 28 class NodeController; 29 30 struct HasResultCodes { 31 enum Result { 32 MsgProcessed, 33 MsgDropped, 34 MsgNotKnown, 35 MsgNotAllowed, 36 MsgPayloadError, 37 MsgProcessingError, 38 MsgValueError 39 }; 40 }; 41 42 enum Side : uint8_t { ParentSide, ChildSide, UnknownSide }; 43 44 const char* StringFromIPCSide(Side side); 45 46 class MessageLink { 47 public: 48 typedef IPC::Message Message; 49 50 explicit MessageLink(MessageChannel* aChan); 51 virtual ~MessageLink(); 52 53 // n.b.: These methods all require that the channel monitor is 54 // held when they are invoked. 55 virtual void SendMessage(mozilla::UniquePtr<Message> msg) = 0; 56 57 // Synchronously close the connection, such that no further notifications will 58 // be delivered to the MessageChannel instance. Must be called with the 59 // channel monitor held. 60 virtual void Close() = 0; 61 62 virtual bool IsClosed() const = 0; 63 64 #ifdef FUZZING_SNAPSHOT 65 virtual Maybe<mojo::core::ports::PortName> GetPortName() { return Nothing(); } 66 #endif 67 68 protected: 69 MessageChannel* mChan; 70 }; 71 72 class PortLink final : public MessageLink { 73 using PortRef = mojo::core::ports::PortRef; 74 using PortStatus = mojo::core::ports::PortStatus; 75 using UserMessage = mojo::core::ports::UserMessage; 76 using UserMessageEvent = mojo::core::ports::UserMessageEvent; 77 78 public: 79 PortLink(MessageChannel* aChan, ScopedPort aPort); 80 virtual ~PortLink(); 81 82 void SendMessage(UniquePtr<Message> aMessage) override; 83 void Close() override; 84 85 bool IsClosed() const override; 86 87 #ifdef FUZZING_SNAPSHOT 88 Maybe<mojo::core::ports::PortName> GetPortName() override { 89 return Some(mPort.name()); 90 } 91 #endif 92 93 private: 94 class PortObserverThunk; 95 friend class PortObserverThunk; 96 97 void OnPortStatusChanged(); 98 99 // Called either when an error is detected on the port from the port observer, 100 // or when `SendClose()` is called. 101 void Clear(); 102 103 const RefPtr<NodeController> mNode; 104 const PortRef mPort; 105 106 RefPtr<PortObserverThunk> mObserver; 107 }; 108 109 } // namespace ipc 110 } // namespace mozilla 111 112 #endif // ifndef ipc_glue_MessageLink_h