1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: sw=4 ts=4 et :
3 : */
4 : /* ***** BEGIN LICENSE BLOCK *****
5 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 : *
7 : * The contents of this file are subject to the Mozilla Public License Version
8 : * 1.1 (the "License"); you may not use this file except in compliance with
9 : * the License. You may obtain a copy of the License at
10 : * http://www.mozilla.org/MPL/
11 : *
12 : * Software distributed under the License is distributed on an "AS IS" basis,
13 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 : * for the specific language governing rights and limitations under the
15 : * License.
16 : *
17 : * The Original Code is Mozilla IPC.
18 : *
19 : * The Initial Developer of the Original Code is
20 : * Chris Jones <jones.chris.g@gmail.com>
21 : * Portions created by the Initial Developer are Copyright (C) 2009
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributor(s):
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #ifndef mozilla_ipc_ProtocolUtils_h
41 : #define mozilla_ipc_ProtocolUtils_h 1
42 :
43 : #include "base/process.h"
44 : #include "base/process_util.h"
45 : #include "chrome/common/ipc_message_utils.h"
46 :
47 : #include "prenv.h"
48 :
49 : #include "IPCMessageStart.h"
50 : #include "mozilla/ipc/Shmem.h"
51 : #include "mozilla/ipc/Transport.h"
52 :
53 : // WARNING: this takes into account the private, special-message-type
54 : // enum in ipc_channel.h. They need to be kept in sync.
55 : namespace {
56 : // XXX the max message ID is actually kuint32max now ... when this
57 : // changed, the assumptions of the special message IDs changed in that
58 : // they're not carving out messages from likely-unallocated space, but
59 : // rather carving out messages from the end of space allocated to
60 : // protocol 0. Oops! We can get away with this until protocol 0
61 : // starts approaching its 65,536th message.
62 : enum {
63 : CHANNEL_OPENED_MESSAGE_TYPE = kuint16max - 6,
64 : SHMEM_DESTROYED_MESSAGE_TYPE = kuint16max - 5,
65 : UNBLOCK_CHILD_MESSAGE_TYPE = kuint16max - 4,
66 : BLOCK_CHILD_MESSAGE_TYPE = kuint16max - 3,
67 : SHMEM_CREATED_MESSAGE_TYPE = kuint16max - 2,
68 : GOODBYE_MESSAGE_TYPE = kuint16max - 1
69 : };
70 : }
71 :
72 : namespace mozilla {
73 : namespace ipc {
74 :
75 : class AsyncChannel;
76 :
77 : // Used to pass references to protocol actors across the wire.
78 : // Actors created on the parent-side have a positive ID, and actors
79 : // allocated on the child side have a negative ID.
80 : struct ActorHandle
81 : {
82 : int mId;
83 : };
84 :
85 : // Used internally to represent a "trigger" that might cause a state
86 : // transition. Triggers are normalized across parent+child to Send
87 : // and Recv (instead of child-in, child-out, parent-in, parent-out) so
88 : // that they can share the same state machine implementation. To
89 : // further normalize, |Send| is used for 'call', |Recv| for 'answer'.
90 : struct Trigger
91 : {
92 : enum Action { Send, Recv };
93 :
94 0 : Trigger(Action action, int32 msg) :
95 : mAction(action),
96 0 : mMsg(msg)
97 0 : {}
98 :
99 : Action mAction;
100 : int32 mMsg;
101 : };
102 :
103 : template<class ListenerT>
104 : class /*NS_INTERFACE_CLASS*/ IProtocolManager
105 2 : {
106 : public:
107 : enum ActorDestroyReason {
108 : FailedConstructor,
109 : Deletion,
110 : AncestorDeletion,
111 : NormalShutdown,
112 : AbnormalShutdown
113 : };
114 :
115 : typedef base::ProcessHandle ProcessHandle;
116 :
117 : virtual int32 Register(ListenerT*) = 0;
118 : virtual int32 RegisterID(ListenerT*, int32) = 0;
119 : virtual ListenerT* Lookup(int32) = 0;
120 : virtual void Unregister(int32) = 0;
121 : virtual void RemoveManagee(int32, ListenerT*) = 0;
122 :
123 : virtual Shmem::SharedMemory* CreateSharedMemory(
124 : size_t, SharedMemory::SharedMemoryType, bool, int32*) = 0;
125 : virtual bool AdoptSharedMemory(Shmem::SharedMemory*, int32*) = 0;
126 : virtual Shmem::SharedMemory* LookupSharedMemory(int32) = 0;
127 : virtual bool IsTrackingSharedMemory(Shmem::SharedMemory*) = 0;
128 : virtual bool DestroySharedMemory(Shmem&) = 0;
129 :
130 : // XXX odd ducks, acknowledged
131 : virtual ProcessHandle OtherProcess() const = 0;
132 : virtual AsyncChannel* GetIPCChannel() = 0;
133 : };
134 :
135 :
136 : inline bool
137 0 : LoggingEnabled()
138 : {
139 : #if defined(DEBUG)
140 0 : return !!PR_GetEnv("MOZ_IPC_MESSAGE_LOG");
141 : #else
142 : return false;
143 : #endif
144 : }
145 :
146 :
147 : typedef IPCMessageStart ProtocolId;
148 :
149 : struct PrivateIPDLInterface {};
150 :
151 : bool
152 : Bridge(const PrivateIPDLInterface&,
153 : AsyncChannel*, base::ProcessHandle, AsyncChannel*, base::ProcessHandle,
154 : ProtocolId);
155 :
156 : bool
157 : Open(const PrivateIPDLInterface&,
158 : AsyncChannel*, base::ProcessHandle, Transport::Mode,
159 : ProtocolId);
160 :
161 : bool
162 : UnpackChannelOpened(const PrivateIPDLInterface&,
163 : const IPC::Message&,
164 : TransportDescriptor*, base::ProcessId*, ProtocolId*);
165 :
166 : } // namespace ipc
167 : } // namespace mozilla
168 :
169 :
170 : namespace IPC {
171 :
172 : template <>
173 : struct ParamTraits<mozilla::ipc::ActorHandle>
174 : {
175 : typedef mozilla::ipc::ActorHandle paramType;
176 :
177 : static void Write(Message* aMsg, const paramType& aParam)
178 : {
179 : IPC::WriteParam(aMsg, aParam.mId);
180 : }
181 :
182 0 : static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
183 : {
184 : int id;
185 0 : if (IPC::ReadParam(aMsg, aIter, &id)) {
186 0 : aResult->mId = id;
187 0 : return true;
188 : }
189 0 : return false;
190 : }
191 :
192 : static void Log(const paramType& aParam, std::wstring* aLog)
193 : {
194 : aLog->append(StringPrintf(L"(%d)", aParam.mId));
195 : }
196 : };
197 :
198 : } // namespace IPC
199 :
200 :
201 : #endif // mozilla_ipc_ProtocolUtils_h
|