~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/ipc/ipcd/daemon/src/ipcd.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

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 "prlog.h"
 
39
#include "prio.h"
 
40
 
 
41
#include "ipcConfig.h"
 
42
#include "ipcLog.h"
 
43
#include "ipcMessage.h"
 
44
#include "ipcClient.h"
 
45
#include "ipcModuleReg.h"
 
46
#include "ipcModule.h"
 
47
#include "ipcCommandModule.h"
 
48
#include "ipcdPrivate.h"
 
49
#include "ipcd.h"
 
50
 
 
51
//-----------------------------------------------------------------------------
 
52
 
 
53
void
 
54
IPC_NotifyParent()
 
55
{
 
56
    PRFileDesc *fd = PR_GetInheritedFD(IPC_STARTUP_PIPE_NAME);
 
57
    if (fd) {
 
58
        char c = IPC_STARTUP_PIPE_MAGIC;
 
59
        PR_Write(fd, &c, 1);
 
60
        PR_Close(fd);
 
61
    }
 
62
}
 
63
 
 
64
//-----------------------------------------------------------------------------
 
65
 
 
66
PRStatus
 
67
IPC_DispatchMsg(ipcClient *client, const ipcMessage *msg)
 
68
{
 
69
    PR_ASSERT(client);
 
70
    PR_ASSERT(msg);
 
71
 
 
72
    // remember if client is expecting SYNC_REPLY.  we'll add that flag to the
 
73
    // next message sent to the client.
 
74
    if (msg->TestFlag(IPC_MSG_FLAG_SYNC_QUERY)) {
 
75
        PR_ASSERT(client->GetExpectsSyncReply() == PR_FALSE);
 
76
        client->SetExpectsSyncReply(PR_TRUE);
 
77
    }
 
78
 
 
79
    if (msg->Target().Equals(IPCM_TARGET)) {
 
80
        IPCM_HandleMsg(client, msg);
 
81
        return PR_SUCCESS;
 
82
    }
 
83
 
 
84
    return IPC_DispatchMsg(client, msg->Target(), msg->Data(), msg->DataLen());
 
85
}
 
86
 
 
87
PRStatus
 
88
IPC_SendMsg(ipcClient *client, ipcMessage *msg)
 
89
{
 
90
    PR_ASSERT(msg);
 
91
 
 
92
    if (client == NULL) {
 
93
        //
 
94
        // broadcast
 
95
        //
 
96
        for (int i=0; i<ipcClientCount; ++i)
 
97
            IPC_SendMsg(&ipcClients[i], msg->Clone());
 
98
        delete msg;
 
99
        return PR_SUCCESS;
 
100
    }
 
101
 
 
102
    // add SYNC_REPLY flag to message if client is expecting...
 
103
    if (client->GetExpectsSyncReply()) {
 
104
        msg->SetFlag(IPC_MSG_FLAG_SYNC_REPLY);
 
105
        client->SetExpectsSyncReply(PR_FALSE);
 
106
    }
 
107
 
 
108
    if (client->HasTarget(msg->Target()))
 
109
        return IPC_PlatformSendMsg(client, msg);
 
110
 
 
111
    LOG(("  no registered message handler\n"));
 
112
    return PR_FAILURE;
 
113
}
 
114
 
 
115
//-----------------------------------------------------------------------------
 
116
// IPC daemon methods
 
117
//-----------------------------------------------------------------------------
 
118
 
 
119
PRStatus
 
120
IPC_SendMsg(ipcClient *client, const nsID &target, const void *data, PRUint32 dataLen)
 
121
{
 
122
    return IPC_SendMsg(client, new ipcMessage(target, (const char *) data, dataLen));
 
123
}
 
124
 
 
125
ipcClient *
 
126
IPC_GetClientByID(PRUint32 clientID)
 
127
{
 
128
    // linear search OK since number of clients should be small
 
129
    for (int i = 0; i < ipcClientCount; ++i) {
 
130
        if (ipcClients[i].ID() == clientID)
 
131
            return &ipcClients[i];
 
132
    }
 
133
    return NULL;
 
134
}
 
135
 
 
136
ipcClient *
 
137
IPC_GetClientByName(const char *name)
 
138
{
 
139
    // linear search OK since number of clients should be small
 
140
    for (int i = 0; i < ipcClientCount; ++i) {
 
141
        if (ipcClients[i].HasName(name))
 
142
            return &ipcClients[i];
 
143
    }
 
144
    return NULL;
 
145
}
 
146
 
 
147
void
 
148
IPC_EnumClients(ipcClientEnumFunc func, void *closure)
 
149
{
 
150
    PR_ASSERT(func);
 
151
    for (int i = 0; i < ipcClientCount; ++i) {
 
152
        if (func(closure, &ipcClients[i], ipcClients[i].ID()) == PR_FALSE)
 
153
            break;
 
154
    }
 
155
}
 
156
 
 
157
PRUint32
 
158
IPC_GetClientID(ipcClient *client)
 
159
{
 
160
    PR_ASSERT(client);
 
161
    return client->ID();
 
162
}
 
163
 
 
164
PRBool
 
165
IPC_ClientHasName(ipcClient *client, const char *name)
 
166
{
 
167
    PR_ASSERT(client);
 
168
    PR_ASSERT(name);
 
169
    return client->HasName(name);
 
170
}
 
171
 
 
172
PRBool
 
173
IPC_ClientHasTarget(ipcClient *client, const nsID &target)
 
174
{
 
175
    PR_ASSERT(client);
 
176
    return client->HasTarget(target);
 
177
}
 
178
 
 
179
void
 
180
IPC_EnumClientNames(ipcClient *client, ipcClientNameEnumFunc func, void *closure)
 
181
{
 
182
    PR_ASSERT(client);
 
183
    PR_ASSERT(func);
 
184
    const ipcStringNode *node = client->Names();
 
185
    while (node) {
 
186
        if (func(closure, client, node->Value()) == PR_FALSE)
 
187
            break;
 
188
        node = node->mNext;
 
189
    }
 
190
}
 
191
 
 
192
void
 
193
IPC_EnumClientTargets(ipcClient *client, ipcClientTargetEnumFunc func, void *closure)
 
194
{
 
195
    PR_ASSERT(client);
 
196
    PR_ASSERT(func);
 
197
    const ipcIDNode *node = client->Targets();
 
198
    while (node) {
 
199
        if (func(closure, client, node->Value()) == PR_FALSE)
 
200
            break;
 
201
        node = node->mNext;
 
202
    }
 
203
}