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 1
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/Assertions.h"
16 #include "mozilla/UniquePtr.h"
17 #include "mozilla/ipc/ScopedPort.h"
18 
19 namespace IPC {
20 class Message;
21 class MessageReader;
22 class MessageWriter;
23 }  // namespace IPC
24 
25 namespace mozilla {
26 namespace ipc {
27 
28 class MessageChannel;
29 class NodeController;
30 
31 struct HasResultCodes {
32   enum Result {
33     MsgProcessed,
34     MsgDropped,
35     MsgNotKnown,
36     MsgNotAllowed,
37     MsgPayloadError,
38     MsgProcessingError,
39     MsgRouteError,
40     MsgValueError
41   };
42 };
43 
44 enum Side : uint8_t { ParentSide, ChildSide, UnknownSide };
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   virtual void SendClose() = 0;
57 
58   virtual bool IsClosed() const = 0;
59 
60  protected:
61   MessageChannel* mChan;
62 };
63 
64 class PortLink final : public MessageLink {
65   using PortRef = mojo::core::ports::PortRef;
66   using PortStatus = mojo::core::ports::PortStatus;
67   using UserMessage = mojo::core::ports::UserMessage;
68   using UserMessageEvent = mojo::core::ports::UserMessageEvent;
69 
70  public:
71   PortLink(MessageChannel* aChan, ScopedPort aPort);
72   virtual ~PortLink();
73 
74   void SendMessage(UniquePtr<Message> aMessage) override;
75   void SendClose() override;
76 
77   bool IsClosed() const override;
78 
79  private:
80   class PortObserverThunk;
81   friend class PortObserverThunk;
82 
83   void OnPortStatusChanged();
84 
85   // Called either when an error is detected on the port from the port observer,
86   // or when `SendClose()` is called.
87   void Clear();
88 
89   const RefPtr<NodeController> mNode;
90   const PortRef mPort;
91 
92   RefPtr<PortObserverThunk> mObserver;
93 };
94 
95 }  // namespace ipc
96 }  // namespace mozilla
97 
98 #endif  // ifndef ipc_glue_MessageLink_h
99