Bumping manifests a=b2g-bump
[gecko.git] / ipc / glue / ProtocolUtils.h
blobe522090352968149a4132728e23b44165258b652
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 mozilla_ipc_ProtocolUtils_h
9 #define mozilla_ipc_ProtocolUtils_h 1
11 #include "base/process.h"
12 #include "base/process_util.h"
13 #include "chrome/common/ipc_message_utils.h"
15 #include "prenv.h"
17 #include "IPCMessageStart.h"
18 #include "mozilla/Attributes.h"
19 #include "mozilla/ipc/FileDescriptor.h"
20 #include "mozilla/ipc/Shmem.h"
21 #include "mozilla/ipc/Transport.h"
22 #include "mozilla/ipc/MessageLink.h"
23 #include "mozilla/LinkedList.h"
24 #include "MainThreadUtils.h"
26 #if defined(ANDROID) && defined(DEBUG)
27 #include <android/log.h>
28 #endif
30 // WARNING: this takes into account the private, special-message-type
31 // enum in ipc_channel.h. They need to be kept in sync.
32 namespace {
33 // XXX the max message ID is actually kuint32max now ... when this
34 // changed, the assumptions of the special message IDs changed in that
35 // they're not carving out messages from likely-unallocated space, but
36 // rather carving out messages from the end of space allocated to
37 // protocol 0. Oops! We can get away with this until protocol 0
38 // starts approaching its 65,536th message.
39 enum {
40 CHANNEL_OPENED_MESSAGE_TYPE = kuint16max - 5,
41 SHMEM_DESTROYED_MESSAGE_TYPE = kuint16max - 4,
42 SHMEM_CREATED_MESSAGE_TYPE = kuint16max - 3,
43 GOODBYE_MESSAGE_TYPE = kuint16max - 2
45 // kuint16max - 1 is used by ipc_channel.h.
49 namespace mozilla {
50 namespace dom {
51 class ContentParent;
54 namespace net {
55 class NeckoParent;
58 namespace ipc {
60 #ifdef XP_WIN
61 const base::ProcessHandle kInvalidProcessHandle = INVALID_HANDLE_VALUE;
62 #else
63 const base::ProcessHandle kInvalidProcessHandle = -1;
64 #endif
66 class ProtocolFdMapping;
67 class ProtocolCloneContext;
69 // Used to pass references to protocol actors across the wire.
70 // Actors created on the parent-side have a positive ID, and actors
71 // allocated on the child side have a negative ID.
72 struct ActorHandle
74 int mId;
77 // Used internally to represent a "trigger" that might cause a state
78 // transition. Triggers are normalized across parent+child to Send
79 // and Recv (instead of child-in, child-out, parent-in, parent-out) so
80 // that they can share the same state machine implementation. To
81 // further normalize, |Send| is used for 'call', |Recv| for 'answer'.
82 struct Trigger
84 enum Action { Send, Recv };
86 Trigger(Action action, int32_t msg) :
87 mAction(action),
88 mMsg(msg)
91 Action mAction;
92 int32_t mMsg;
95 class ProtocolCloneContext
97 typedef mozilla::dom::ContentParent ContentParent;
98 typedef mozilla::net::NeckoParent NeckoParent;
100 ContentParent* mContentParent;
101 NeckoParent* mNeckoParent;
103 public:
104 ProtocolCloneContext()
105 : mContentParent(nullptr)
106 , mNeckoParent(nullptr)
109 void SetContentParent(ContentParent* aContentParent)
111 mContentParent = aContentParent;
114 ContentParent* GetContentParent() { return mContentParent; }
116 void SetNeckoParent(NeckoParent* aNeckoParent)
118 mNeckoParent = aNeckoParent;
121 NeckoParent* GetNeckoParent() { return mNeckoParent; }
124 template<class ListenerT>
125 class /*NS_INTERFACE_CLASS*/ IProtocolManager
127 public:
128 enum ActorDestroyReason {
129 FailedConstructor,
130 Deletion,
131 AncestorDeletion,
132 NormalShutdown,
133 AbnormalShutdown
136 typedef base::ProcessHandle ProcessHandle;
138 virtual int32_t Register(ListenerT*) = 0;
139 virtual int32_t RegisterID(ListenerT*, int32_t) = 0;
140 virtual ListenerT* Lookup(int32_t) = 0;
141 virtual void Unregister(int32_t) = 0;
142 virtual void RemoveManagee(int32_t, ListenerT*) = 0;
144 virtual Shmem::SharedMemory* CreateSharedMemory(
145 size_t, SharedMemory::SharedMemoryType, bool, int32_t*) = 0;
146 virtual bool AdoptSharedMemory(Shmem::SharedMemory*, int32_t*) = 0;
147 virtual Shmem::SharedMemory* LookupSharedMemory(int32_t) = 0;
148 virtual bool IsTrackingSharedMemory(Shmem::SharedMemory*) = 0;
149 virtual bool DestroySharedMemory(Shmem&) = 0;
151 // XXX odd ducks, acknowledged
152 virtual ProcessHandle OtherProcess() const = 0;
153 virtual MessageChannel* GetIPCChannel() = 0;
155 // The implementation of function is generated by code generator.
156 virtual void CloneManagees(ListenerT* aSource,
157 ProtocolCloneContext* aCtx) = 0;
160 typedef IPCMessageStart ProtocolId;
163 * All RPC protocols should implement this interface.
165 class IProtocol : protected MessageListener
167 public:
169 * This function is used to clone this protocol actor.
171 * see IProtocol::CloneProtocol()
173 virtual IProtocol*
174 CloneProtocol(MessageChannel* aChannel,
175 ProtocolCloneContext* aCtx) = 0;
179 * All top-level protocols should inherit this class.
181 * IToplevelProtocol tracks all top-level protocol actors created from
182 * this protocol actor.
184 class IToplevelProtocol : public LinkedListElement<IToplevelProtocol>
186 protected:
187 explicit IToplevelProtocol(ProtocolId aProtoId)
188 : mProtocolId(aProtoId)
189 , mTrans(nullptr)
191 MOZ_ASSERT(NS_IsMainThread() || AllowNonMainThreadUse());
194 ~IToplevelProtocol();
197 * Add an actor to the list of actors that have been opened by this
198 * protocol.
200 void AddOpenedActor(IToplevelProtocol* aActor);
202 public:
203 void SetTransport(Transport* aTrans)
205 mTrans = aTrans;
208 Transport* GetTransport() const { return mTrans; }
210 ProtocolId GetProtocolId() const { return mProtocolId; }
213 * Return first of actors of top level protocols opened by this one.
215 IToplevelProtocol* GetFirstOpenedActors()
217 MOZ_ASSERT(NS_IsMainThread() || AllowNonMainThreadUse());
218 return mOpenActors.getFirst();
220 const IToplevelProtocol* GetFirstOpenedActors() const
222 MOZ_ASSERT(NS_IsMainThread() || AllowNonMainThreadUse());
223 return mOpenActors.getFirst();
226 virtual IToplevelProtocol*
227 CloneToplevel(const InfallibleTArray<ProtocolFdMapping>& aFds,
228 base::ProcessHandle aPeerProcess,
229 ProtocolCloneContext* aCtx);
231 void CloneOpenedToplevels(IToplevelProtocol* aTemplate,
232 const InfallibleTArray<ProtocolFdMapping>& aFds,
233 base::ProcessHandle aPeerProcess,
234 ProtocolCloneContext* aCtx);
236 #ifdef MOZ_IPDL_TESTS
237 static void SetAllowNonMainThreadUse();
238 #endif
240 static bool AllowNonMainThreadUse() {
241 #ifdef MOZ_IPDL_TESTS
242 return sAllowNonMainThreadUse;
243 #else
244 return false;
245 #endif
248 private:
249 LinkedList<IToplevelProtocol> mOpenActors; // All protocol actors opened by this.
251 ProtocolId mProtocolId;
252 Transport* mTrans;
254 #ifdef MOZ_IPDL_TESTS
255 static bool sAllowNonMainThreadUse;
256 #endif
260 inline bool
261 LoggingEnabled()
263 #if defined(DEBUG)
264 return !!PR_GetEnv("MOZ_IPC_MESSAGE_LOG");
265 #else
266 return false;
267 #endif
270 inline bool
271 LoggingEnabledFor(const char *aTopLevelProtocol)
273 #if defined(DEBUG)
274 const char *filter = PR_GetEnv("MOZ_IPC_MESSAGE_LOG");
275 if (!filter) {
276 return false;
278 return strcmp(filter, "1") == 0 || strcmp(filter, aTopLevelProtocol) == 0;
279 #else
280 return false;
281 #endif
284 MOZ_NEVER_INLINE void
285 ProtocolErrorBreakpoint(const char* aMsg);
287 MOZ_NEVER_INLINE void
288 FatalError(const char* aProtocolName, const char* aMsg,
289 base::ProcessHandle aHandle, bool aIsParent);
291 struct PrivateIPDLInterface {};
293 bool
294 Bridge(const PrivateIPDLInterface&,
295 MessageChannel*, base::ProcessHandle, MessageChannel*, base::ProcessHandle,
296 ProtocolId, ProtocolId);
298 bool
299 Open(const PrivateIPDLInterface&,
300 MessageChannel*, base::ProcessHandle, Transport::Mode,
301 ProtocolId, ProtocolId);
303 bool
304 UnpackChannelOpened(const PrivateIPDLInterface&,
305 const IPC::Message&,
306 TransportDescriptor*, base::ProcessId*, ProtocolId*);
308 } // namespace ipc
309 } // namespace mozilla
312 namespace IPC {
314 template <>
315 struct ParamTraits<mozilla::ipc::ActorHandle>
317 typedef mozilla::ipc::ActorHandle paramType;
319 static void Write(Message* aMsg, const paramType& aParam)
321 IPC::WriteParam(aMsg, aParam.mId);
324 static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
326 int id;
327 if (IPC::ReadParam(aMsg, aIter, &id)) {
328 aResult->mId = id;
329 return true;
331 return false;
334 static void Log(const paramType& aParam, std::wstring* aLog)
336 aLog->append(StringPrintf(L"(%d)", aParam.mId));
340 } // namespace IPC
343 #endif // mozilla_ipc_ProtocolUtils_h