Fix typo in 9b54bd30006c008b4a951331b273613d5bac3abf
[pm.git] / ipc / glue / MessageLink.h
blob1b7035f8589f92a0fb7f5a700ea1f7f73e74efe3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: sw=4 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/. */
8 #ifndef ipc_glue_MessageLink_h
9 #define ipc_glue_MessageLink_h 1
11 #include "base/basictypes.h"
12 #include "base/message_loop.h"
14 #include "mozilla/WeakPtr.h"
15 #include "mozilla/ipc/Transport.h"
17 namespace mozilla {
18 namespace ipc {
20 class MessageChannel;
22 struct HasResultCodes
24 enum Result {
25 MsgProcessed,
26 MsgDropped,
27 MsgNotKnown,
28 MsgNotAllowed,
29 MsgPayloadError,
30 MsgProcessingError,
31 MsgRouteError,
32 MsgValueError
36 enum Side {
37 ParentSide,
38 ChildSide,
39 UnknownSide
42 enum ChannelState {
43 ChannelClosed,
44 ChannelOpening,
45 ChannelConnected,
46 ChannelTimeout,
47 ChannelClosing,
48 ChannelError
51 // What happens if Interrupt calls race?
52 enum RacyInterruptPolicy {
53 RIPError,
54 RIPChildWins,
55 RIPParentWins
58 class MessageListener
59 : protected HasResultCodes,
60 public mozilla::SupportsWeakPtr<MessageListener>
62 public:
63 MOZ_DECLARE_WEAKREFERENCE_TYPENAME(MessageListener)
64 typedef IPC::Message Message;
66 virtual ~MessageListener() { }
68 virtual void OnChannelClose() = 0;
69 virtual void OnChannelError() = 0;
70 virtual Result OnMessageReceived(const Message& aMessage) = 0;
71 virtual Result OnMessageReceived(const Message& aMessage, Message *& aReply) = 0;
72 virtual Result OnCallReceived(const Message& aMessage, Message *& aReply) = 0;
73 virtual void OnProcessingError(Result aError, const char* aMsgName) = 0;
74 virtual void OnChannelConnected(int32_t peer_pid) {}
75 virtual bool OnReplyTimeout() {
76 return false;
79 virtual void OnEnteredCxxStack() {
80 NS_RUNTIMEABORT("default impl shouldn't be invoked");
82 virtual void OnExitedCxxStack() {
83 NS_RUNTIMEABORT("default impl shouldn't be invoked");
85 virtual void OnEnteredCall() {
86 NS_RUNTIMEABORT("default impl shouldn't be invoked");
88 virtual void OnExitedCall() {
89 NS_RUNTIMEABORT("default impl shouldn't be invoked");
91 /* This callback is called when a sync message is sent that begins a new IPC transaction
92 (i.e., when it is not part of an existing sequence of nested messages). */
93 virtual void OnBeginSyncTransaction() {
95 virtual RacyInterruptPolicy MediateInterruptRace(const Message& parent,
96 const Message& child)
98 return RIPChildWins;
101 virtual void OnEnteredSyncSend() {
103 virtual void OnExitedSyncSend() {
106 virtual void ProcessRemoteNativeEventsInInterruptCall() {
109 // FIXME/bug 792652: this doesn't really belong here, but a
110 // large refactoring is needed to put it where it belongs.
111 virtual int32_t GetProtocolTypeId() = 0;
114 class MessageLink
116 public:
117 typedef IPC::Message Message;
119 explicit MessageLink(MessageChannel *aChan);
120 virtual ~MessageLink();
122 // n.b.: These methods all require that the channel monitor is
123 // held when they are invoked.
124 virtual void EchoMessage(Message *msg) = 0;
125 virtual void SendMessage(Message *msg) = 0;
126 virtual void SendClose() = 0;
128 virtual bool Unsound_IsClosed() const = 0;
129 virtual uint32_t Unsound_NumQueuedMessages() const = 0;
131 protected:
132 MessageChannel *mChan;
135 class ProcessLink
136 : public MessageLink,
137 public Transport::Listener
139 void OnCloseChannel();
140 void OnChannelOpened();
141 void OnTakeConnectedChannel();
142 void OnEchoMessage(Message* msg);
144 void AssertIOThread() const
146 MOZ_ASSERT(mIOLoop == MessageLoop::current(),
147 "not on I/O thread!");
150 public:
151 explicit ProcessLink(MessageChannel *chan);
152 virtual ~ProcessLink();
154 // The ProcessLink will register itself as the IPC::Channel::Listener on the
155 // transport passed here. If the transport already has a listener registered
156 // then a listener chain will be established (the ProcessLink listener
157 // methods will be called first and may call some methods on the original
158 // listener as well). Once the channel is closed (either via normal shutdown
159 // or a pipe error) the chain will be destroyed and the original listener
160 // will again be registered.
161 void Open(Transport* aTransport, MessageLoop *aIOLoop, Side aSide);
163 // Run on the I/O thread, only when using inter-process link.
164 // These methods acquire the monitor and forward to the
165 // similarly named methods in AsyncChannel below
166 // (OnMessageReceivedFromLink(), etc)
167 virtual void OnMessageReceived(const Message& msg) override;
168 virtual void OnChannelConnected(int32_t peer_pid) override;
169 virtual void OnChannelError() override;
171 virtual void EchoMessage(Message *msg) override;
172 virtual void SendMessage(Message *msg) override;
173 virtual void SendClose() override;
175 virtual bool Unsound_IsClosed() const override;
176 virtual uint32_t Unsound_NumQueuedMessages() const override;
178 protected:
179 Transport* mTransport;
180 MessageLoop* mIOLoop; // thread where IO happens
181 Transport::Listener* mExistingListener; // channel's previous listener
182 #ifdef MOZ_NUWA_PROCESS
183 bool mIsToNuwaProcess;
184 #endif
187 class ThreadLink : public MessageLink
189 public:
190 ThreadLink(MessageChannel *aChan, MessageChannel *aTargetChan);
191 virtual ~ThreadLink();
193 virtual void EchoMessage(Message *msg) override;
194 virtual void SendMessage(Message *msg) override;
195 virtual void SendClose() override;
197 virtual bool Unsound_IsClosed() const override;
198 virtual uint32_t Unsound_NumQueuedMessages() const override;
200 protected:
201 MessageChannel* mTargetChan;
204 } // namespace ipc
205 } // namespace mozilla
207 #endif // ifndef ipc_glue_MessageLink_h