~ubuntu-branches/ubuntu/gutsy/virtualbox-ose/gutsy

« back to all changes in this revision

Viewing changes to src/libs/xpcom18a4/ipc/ipcd/shared/src/ipcm.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2007-09-08 16:44:58 UTC
  • Revision ID: james.westby@ubuntu.com-20070908164458-wao29470vqtr8ksy
Tags: upstream-1.5.0-dfsg2
ImportĀ upstreamĀ versionĀ 1.5.0-dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ***** BEGIN LICENSE BLOCK *****
 
2
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
3
 *
 
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/
 
8
 *
 
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.
 
13
 *
 
14
 * The Original Code is Mozilla IPC.
 
15
 *
 
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.
 
20
 *
 
21
 * Contributor(s):
 
22
 *   Darin Fisher <darin@netscape.com>
 
23
 *
 
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.
 
35
 *
 
36
 * ***** END LICENSE BLOCK ***** */
 
37
 
 
38
#include <string.h>
 
39
#include "ipcm.h"
 
40
#include "pratom.h"
 
41
 
 
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}
 
48
};
 
49
 
 
50
PRUint32
 
51
IPCM_NewRequestIndex()
 
52
{
 
53
    static PRInt32 sRequestIndex;
 
54
    return (PRUint32) PR_AtomicIncrement(&sRequestIndex);
 
55
}
 
56
 
 
57
#if 0
 
58
 
 
59
//
 
60
// MSG_TYPE values
 
61
//
 
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;
 
75
 
 
76
//
 
77
// CLIENT_INFO message
 
78
//
 
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
//  +-----------------------------------------+
 
104
//
 
105
 
 
106
struct ipcmClientInfoHeader
 
107
{
 
108
    PRUint32 mType;
 
109
    PRUint32 mID;
 
110
    PRUint32 mRequestIndex;
 
111
    PRUint16 mNameStart;
 
112
    PRUint16 mNameCount;
 
113
    PRUint16 mTargetStart;
 
114
    PRUint16 mTargetCount;
 
115
};
 
116
 
 
117
ipcmMessageClientInfo::ipcmMessageClientInfo(PRUint32 cID, PRUint32 rIdx, const char *names[], const nsID *targets[])
 
118
{
 
119
    ipcmClientInfoHeader hdr = {0};
 
120
 
 
121
    hdr.mType = MSG_TYPE;
 
122
    hdr.mID = cID;
 
123
    hdr.mRequestIndex = rIdx;
 
124
    hdr.mNameStart = sizeof(hdr);
 
125
 
 
126
    PRUint32 i, namesLen = 0;
 
127
 
 
128
    i = 0;
 
129
    while (names[i]) {
 
130
        namesLen += (strlen(names[i]) + 1);
 
131
        ++hdr.mNameCount;
 
132
        ++i;
 
133
    }
 
134
 
 
135
    i = 0;
 
136
    while (targets[i]) {
 
137
        ++hdr.mTargetCount;
 
138
        ++i;
 
139
    }
 
140
 
 
141
    //
 
142
    // compute target array starting offset
 
143
    //
 
144
    hdr.mTargetStart = hdr.mNameStart + namesLen;
 
145
 
 
146
    //
 
147
    // compute message length
 
148
    //
 
149
    PRUint32 dataLen = sizeof(hdr) + namesLen + hdr.mTargetCount * sizeof(nsID);
 
150
 
 
151
    Init(IPCM_TARGET, NULL, dataLen);
 
152
    
 
153
    //
 
154
    // write message data
 
155
    //
 
156
    SetData(0, (const char *) &hdr, sizeof(hdr));
 
157
 
 
158
    PRUint32 offset = sizeof(hdr);
 
159
 
 
160
    for (i = 0; names[i]; ++i) {
 
161
        PRUint32 len = strlen(names[i]) + 1;
 
162
        SetData(offset, names[i], len);
 
163
        offset += len;
 
164
    }
 
165
 
 
166
    for (i = 0; targets[i]; ++i) {
 
167
        PRUint32 len = sizeof(nsID);
 
168
        SetData(offset, (const char *) targets[i], len);
 
169
        offset += len;
 
170
    }
 
171
}
 
172
 
 
173
PRUint32
 
174
ipcmMessageClientInfo::ClientID() const
 
175
{
 
176
    ipcmClientInfoHeader *hdr = (ipcmClientInfoHeader *) Data();
 
177
    return hdr->mID;
 
178
}
 
179
 
 
180
PRUint32
 
181
ipcmMessageClientInfo::RequestIndex() const
 
182
{
 
183
    ipcmClientInfoHeader *hdr = (ipcmClientInfoHeader *) Data();
 
184
    return hdr->mRequestIndex;
 
185
}
 
186
 
 
187
PRUint32
 
188
ipcmMessageClientInfo::NameCount() const
 
189
{
 
190
    ipcmClientInfoHeader *hdr = (ipcmClientInfoHeader *) Data();
 
191
    return hdr->mNameCount;
 
192
}
 
193
 
 
194
PRUint32
 
195
ipcmMessageClientInfo::TargetCount() const
 
196
{
 
197
    ipcmClientInfoHeader *hdr = (ipcmClientInfoHeader *) Data();
 
198
    return hdr->mTargetCount;
 
199
}
 
200
 
 
201
const char *
 
202
ipcmMessageClientInfo::NextName(const char *name) const
 
203
{
 
204
    ipcmClientInfoHeader *hdr = (ipcmClientInfoHeader *) Data();
 
205
 
 
206
    if (!name)
 
207
        return (const char *) hdr + hdr->mNameStart;
 
208
 
 
209
    name += strlen(name) + 1;
 
210
    if (name == (const char *) hdr + hdr->mTargetStart)
 
211
        name = NULL;
 
212
    return name;
 
213
}
 
214
 
 
215
const nsID *
 
216
ipcmMessageClientInfo::NextTarget(const nsID *target) const
 
217
{
 
218
    ipcmClientInfoHeader *hdr = (ipcmClientInfoHeader *) Data();
 
219
 
 
220
    if (!target)
 
221
        return (const nsID *) (Data() + hdr->mTargetStart);
 
222
 
 
223
    if (++target == (const nsID *) (MsgBuf() + MsgLen()))
 
224
        target = NULL;
 
225
    return target;
 
226
}
 
227
#endif
 
228
 
 
229
//
 
230
// FORWARD message
 
231
//
 
232
//  +-------------------------+
 
233
//  | DWORD : MSG_TYPE        |
 
234
//  +-------------------------+
 
235
//  | clientID                |
 
236
//  +-------------------------+
 
237
//  | innerMsgHeader          |
 
238
//  +-------------------------+
 
239
//  | innerMsgData            |
 
240
//  +-------------------------+
 
241
//
 
242
 
 
243
ipcmMessageForward::ipcmMessageForward(PRUint32 type,
 
244
                                       PRUint32 cID,
 
245
                                       const nsID &target,
 
246
                                       const char *data,
 
247
                                       PRUint32 dataLen)
 
248
{
 
249
    int len = sizeof(ipcmMessageHeader) +  // IPCM header
 
250
              sizeof(cID) +                // cID
 
251
              IPC_MSG_HEADER_SIZE +        // innerMsgHeader
 
252
              dataLen;                     // innerMsgData
 
253
 
 
254
    Init(IPCM_TARGET, NULL, len);
 
255
 
 
256
    ipcmMessageHeader ipcmHdr =
 
257
        { type, IPCM_NewRequestIndex() };
 
258
 
 
259
    SetData(0, (char *) &ipcmHdr, sizeof(ipcmHdr));
 
260
    SetData(sizeof(ipcmHdr), (char *) &cID, sizeof(cID));
 
261
 
 
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;
 
267
 
 
268
    SetData(sizeof(ipcmHdr) + sizeof(cID), (char *) &hdr, IPC_MSG_HEADER_SIZE);
 
269
    if (data)
 
270
        SetInnerData(0, data, dataLen);
 
271
}
 
272
 
 
273
void
 
274
ipcmMessageForward::SetInnerData(PRUint32 offset, const char *data, PRUint32 dataLen)
 
275
{
 
276
    SetData(sizeof(ipcmMessageHeader) + 4 + IPC_MSG_HEADER_SIZE + offset, data, dataLen);
 
277
}
 
278
 
 
279
PRUint32
 
280
ipcmMessageForward::ClientID() const
 
281
{
 
282
    return ((PRUint32 *) Data())[2];
 
283
}
 
284
 
 
285
const nsID &
 
286
ipcmMessageForward::InnerTarget() const
 
287
{
 
288
    ipcMessageHeader *hdr = (ipcMessageHeader *) (Data() + 12);
 
289
    return hdr->mTarget;
 
290
}
 
291
 
 
292
const char *
 
293
ipcmMessageForward::InnerData() const
 
294
{
 
295
    return Data() + 12 + IPC_MSG_HEADER_SIZE;
 
296
}
 
297
 
 
298
PRUint32
 
299
ipcmMessageForward::InnerDataLen() const
 
300
{
 
301
    ipcMessageHeader *hdr = (ipcMessageHeader *) (Data() + 12);
 
302
    return hdr->mLen - IPC_MSG_HEADER_SIZE;
 
303
}