1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
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"
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>
30 // WARNING: this takes into account the private, special-message-type
31 // enum in ipc_channel.h. They need to be kept in sync.
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.
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.
61 const base::ProcessHandle kInvalidProcessHandle
= INVALID_HANDLE_VALUE
;
63 const base::ProcessHandle kInvalidProcessHandle
= -1;
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.
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'.
84 enum Action
{ Send
, Recv
};
86 Trigger(Action action
, int32_t msg
) :
95 class ProtocolCloneContext
97 typedef mozilla::dom::ContentParent ContentParent
;
98 typedef mozilla::net::NeckoParent NeckoParent
;
100 ContentParent
* mContentParent
;
101 NeckoParent
* mNeckoParent
;
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
128 enum ActorDestroyReason
{
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
169 * This function is used to clone this protocol actor.
171 * see IProtocol::CloneProtocol()
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
>
187 explicit IToplevelProtocol(ProtocolId aProtoId
)
188 : mProtocolId(aProtoId
)
191 MOZ_ASSERT(NS_IsMainThread() || AllowNonMainThreadUse());
194 ~IToplevelProtocol();
197 * Add an actor to the list of actors that have been opened by this
200 void AddOpenedActor(IToplevelProtocol
* aActor
);
203 void SetTransport(Transport
* 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();
240 static bool AllowNonMainThreadUse() {
241 #ifdef MOZ_IPDL_TESTS
242 return sAllowNonMainThreadUse
;
249 LinkedList
<IToplevelProtocol
> mOpenActors
; // All protocol actors opened by this.
251 ProtocolId mProtocolId
;
254 #ifdef MOZ_IPDL_TESTS
255 static bool sAllowNonMainThreadUse
;
264 return !!PR_GetEnv("MOZ_IPC_MESSAGE_LOG");
271 LoggingEnabledFor(const char *aTopLevelProtocol
)
274 const char *filter
= PR_GetEnv("MOZ_IPC_MESSAGE_LOG");
278 return strcmp(filter
, "1") == 0 || strcmp(filter
, aTopLevelProtocol
) == 0;
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
{};
294 Bridge(const PrivateIPDLInterface
&,
295 MessageChannel
*, base::ProcessHandle
, MessageChannel
*, base::ProcessHandle
,
296 ProtocolId
, ProtocolId
);
299 Open(const PrivateIPDLInterface
&,
300 MessageChannel
*, base::ProcessHandle
, Transport::Mode
,
301 ProtocolId
, ProtocolId
);
304 UnpackChannelOpened(const PrivateIPDLInterface
&,
306 TransportDescriptor
*, base::ProcessId
*, ProtocolId
*);
309 } // namespace mozilla
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
)
327 if (IPC::ReadParam(aMsg
, aIter
, &id
)) {
334 static void Log(const paramType
& aParam
, std::wstring
* aLog
)
336 aLog
->append(StringPrintf(L
"(%d)", aParam
.mId
));
343 #endif // mozilla_ipc_ProtocolUtils_h