Bug 458256. Use LoadLibraryW instead of LoadLibrary (patch by DougT). r+sr=vlad
[wine-gecko.git] / ipc / ipcd / shared / src / ipcm.cpp
blob7f7ed060498f284645caa96ddff1b58506061eb9
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is Mozilla IPC.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2002
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Darin Fisher <darin@netscape.com>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include <string.h>
39 #include "ipcm.h"
40 #include "pratom.h"
42 const nsID IPCM_TARGET =
43 { /* 753ca8ff-c8c2-4601-b115-8c2944da1150 */
44 0x753ca8ff,
45 0xc8c2,
46 0x4601,
47 {0xb1, 0x15, 0x8c, 0x29, 0x44, 0xda, 0x11, 0x50}
50 PRUint32
51 IPCM_NewRequestIndex()
53 static PRInt32 sRequestIndex;
54 return (PRUint32) PR_AtomicIncrement(&sRequestIndex);
57 #if 0
60 // MSG_TYPE values
62 const PRUint32 ipcmMessagePing::MSG_TYPE = IPCM_MSG_TYPE_PING;
63 const PRUint32 ipcmMessageError::MSG_TYPE = IPCM_MSG_TYPE_ERROR;
64 const PRUint32 ipcmMessageClientHello::MSG_TYPE = IPCM_MSG_TYPE_CLIENT_HELLO;
65 const PRUint32 ipcmMessageClientID::MSG_TYPE = IPCM_MSG_TYPE_CLIENT_ID;
66 const PRUint32 ipcmMessageClientInfo::MSG_TYPE = IPCM_MSG_TYPE_CLIENT_INFO;
67 const PRUint32 ipcmMessageClientAddName::MSG_TYPE = IPCM_MSG_TYPE_CLIENT_ADD_NAME;
68 const PRUint32 ipcmMessageClientDelName::MSG_TYPE = IPCM_MSG_TYPE_CLIENT_DEL_NAME;
69 const PRUint32 ipcmMessageClientAddTarget::MSG_TYPE = IPCM_MSG_TYPE_CLIENT_ADD_TARGET;
70 const PRUint32 ipcmMessageClientDelTarget::MSG_TYPE = IPCM_MSG_TYPE_CLIENT_DEL_TARGET;
71 const PRUint32 ipcmMessageQueryClientByName::MSG_TYPE = IPCM_MSG_TYPE_QUERY_CLIENT_BY_NAME;
72 const PRUint32 ipcmMessageQueryClientInfo::MSG_TYPE = IPCM_MSG_TYPE_QUERY_CLIENT_INFO;
73 const PRUint32 ipcmMessageForward::MSG_TYPE = IPCM_MSG_TYPE_FORWARD;
74 const PRUint32 ipcmMessageClientStatus::MSG_TYPE = IPCM_MSG_TYPE_CLIENT_STATUS;
77 // CLIENT_INFO message
79 // +-----------------------------------------+
80 // | DWORD : MSG_TYPE |
81 // +--------------------+--------------------+
82 // | DWORD : clientID |
83 // +--------------------+--------------------+
84 // | DWORD : requestIndex |
85 // +--------------------+--------------------+
86 // | WORD : nameStart | WORD : nameCount |
87 // +--------------------+--------------------+
88 // | WORD : targetStart | WORD : targetCount |
89 // +--------------------+--------------------+
90 // | name[0] | (null byte) |
91 // +--------------------+--------------------+
92 // . . .
93 // . . .
94 // +--------------------+--------------------+
95 // | name[count - 1] | (null byte) |
96 // +--------------------+--------------------+
97 // | target[0] |
98 // +-----------------------------------------+
99 // . . .
100 // . . .
101 // +-----------------------------------------+
102 // | target[count - 1] |
103 // +-----------------------------------------+
106 struct ipcmClientInfoHeader
108 PRUint32 mType;
109 PRUint32 mID;
110 PRUint32 mRequestIndex;
111 PRUint16 mNameStart;
112 PRUint16 mNameCount;
113 PRUint16 mTargetStart;
114 PRUint16 mTargetCount;
117 ipcmMessageClientInfo::ipcmMessageClientInfo(PRUint32 cID, PRUint32 rIdx, const char *names[], const nsID *targets[])
119 ipcmClientInfoHeader hdr = {0};
121 hdr.mType = MSG_TYPE;
122 hdr.mID = cID;
123 hdr.mRequestIndex = rIdx;
124 hdr.mNameStart = sizeof(hdr);
126 PRUint32 i, namesLen = 0;
128 i = 0;
129 while (names[i]) {
130 namesLen += (strlen(names[i]) + 1);
131 ++hdr.mNameCount;
132 ++i;
135 i = 0;
136 while (targets[i]) {
137 ++hdr.mTargetCount;
138 ++i;
142 // compute target array starting offset
144 hdr.mTargetStart = hdr.mNameStart + namesLen;
147 // compute message length
149 PRUint32 dataLen = sizeof(hdr) + namesLen + hdr.mTargetCount * sizeof(nsID);
151 Init(IPCM_TARGET, NULL, dataLen);
154 // write message data
156 SetData(0, (const char *) &hdr, sizeof(hdr));
158 PRUint32 offset = sizeof(hdr);
160 for (i = 0; names[i]; ++i) {
161 PRUint32 len = strlen(names[i]) + 1;
162 SetData(offset, names[i], len);
163 offset += len;
166 for (i = 0; targets[i]; ++i) {
167 PRUint32 len = sizeof(nsID);
168 SetData(offset, (const char *) targets[i], len);
169 offset += len;
173 PRUint32
174 ipcmMessageClientInfo::ClientID() const
176 ipcmClientInfoHeader *hdr = (ipcmClientInfoHeader *) Data();
177 return hdr->mID;
180 PRUint32
181 ipcmMessageClientInfo::RequestIndex() const
183 ipcmClientInfoHeader *hdr = (ipcmClientInfoHeader *) Data();
184 return hdr->mRequestIndex;
187 PRUint32
188 ipcmMessageClientInfo::NameCount() const
190 ipcmClientInfoHeader *hdr = (ipcmClientInfoHeader *) Data();
191 return hdr->mNameCount;
194 PRUint32
195 ipcmMessageClientInfo::TargetCount() const
197 ipcmClientInfoHeader *hdr = (ipcmClientInfoHeader *) Data();
198 return hdr->mTargetCount;
201 const char *
202 ipcmMessageClientInfo::NextName(const char *name) const
204 ipcmClientInfoHeader *hdr = (ipcmClientInfoHeader *) Data();
206 if (!name)
207 return (const char *) hdr + hdr->mNameStart;
209 name += strlen(name) + 1;
210 if (name == (const char *) hdr + hdr->mTargetStart)
211 name = NULL;
212 return name;
215 const nsID *
216 ipcmMessageClientInfo::NextTarget(const nsID *target) const
218 ipcmClientInfoHeader *hdr = (ipcmClientInfoHeader *) Data();
220 if (!target)
221 return (const nsID *) (Data() + hdr->mTargetStart);
223 if (++target == (const nsID *) (MsgBuf() + MsgLen()))
224 target = NULL;
225 return target;
227 #endif
230 // FORWARD message
232 // +-------------------------+
233 // | DWORD : MSG_TYPE |
234 // +-------------------------+
235 // | clientID |
236 // +-------------------------+
237 // | innerMsgHeader |
238 // +-------------------------+
239 // | innerMsgData |
240 // +-------------------------+
243 ipcmMessageForward::ipcmMessageForward(PRUint32 type,
244 PRUint32 cID,
245 const nsID &target,
246 const char *data,
247 PRUint32 dataLen)
249 int len = sizeof(ipcmMessageHeader) + // IPCM header
250 sizeof(cID) + // cID
251 IPC_MSG_HEADER_SIZE + // innerMsgHeader
252 dataLen; // innerMsgData
254 Init(IPCM_TARGET, NULL, len);
256 ipcmMessageHeader ipcmHdr =
257 { type, IPCM_NewRequestIndex() };
259 SetData(0, (char *) &ipcmHdr, sizeof(ipcmHdr));
260 SetData(sizeof(ipcmHdr), (char *) &cID, sizeof(cID));
262 ipcMessageHeader hdr;
263 hdr.mLen = IPC_MSG_HEADER_SIZE + dataLen;
264 hdr.mVersion = IPC_MSG_VERSION;
265 hdr.mFlags = 0;
266 hdr.mTarget = target;
268 SetData(sizeof(ipcmHdr) + sizeof(cID), (char *) &hdr, IPC_MSG_HEADER_SIZE);
269 if (data)
270 SetInnerData(0, data, dataLen);
273 void
274 ipcmMessageForward::SetInnerData(PRUint32 offset, const char *data, PRUint32 dataLen)
276 SetData(sizeof(ipcmMessageHeader) + 4 + IPC_MSG_HEADER_SIZE + offset, data, dataLen);
279 PRUint32
280 ipcmMessageForward::ClientID() const
282 return ((PRUint32 *) Data())[2];
285 const nsID &
286 ipcmMessageForward::InnerTarget() const
288 ipcMessageHeader *hdr = (ipcMessageHeader *) (Data() + 12);
289 return hdr->mTarget;
292 const char *
293 ipcmMessageForward::InnerData() const
295 return Data() + 12 + IPC_MSG_HEADER_SIZE;
298 PRUint32
299 ipcmMessageForward::InnerDataLen() const
301 ipcMessageHeader *hdr = (ipcMessageHeader *) (Data() + 12);
302 return hdr->mLen - IPC_MSG_HEADER_SIZE;