1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef SANDBOX_MAC_XPC_MESSAGE_SERVER_H_
6 #define SANDBOX_MAC_XPC_MESSAGE_SERVER_H_
8 #include <AvailabilityMacros.h>
10 #include "base/mac/dispatch_source_mach.h"
11 #include "base/mac/scoped_mach_port.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "sandbox/mac/message_server.h"
14 #include "sandbox/mac/xpc.h"
15 #include "sandbox/sandbox_export.h"
19 // An implementation of MessageServer that uses XPC pipes to read and write XPC
20 // messages from a Mach port.
21 class SANDBOX_EXPORT XPCMessageServer
: public MessageServer
{
23 // Creates a new XPC message server that will send messages to |demuxer|
24 // for handling. If the |server_receive_right| is non-NULL, this class will
25 // take ownership of the port and it will be used to receive messages.
26 // Otherwise the server will create a new receive right on which to listen.
27 XPCMessageServer(MessageDemuxer
* demuxer
,
28 mach_port_t server_receive_right
);
29 ~XPCMessageServer() override
;
32 bool Initialize() override
;
33 pid_t
GetMessageSenderPID(IPCMessage request
) override
;
34 IPCMessage
CreateReply(IPCMessage request
) override
;
35 bool SendReply(IPCMessage reply
) override
;
36 void ForwardMessage(IPCMessage request
, mach_port_t destination
) override
;
37 // Creates an error reply message with a field "error" set to |error_code|.
38 void RejectMessage(IPCMessage request
, int error_code
) override
;
39 mach_port_t
GetServerPort() const override
;
42 // Reads a message from the XPC pipe.
43 void ReceiveMessage();
45 // The demuxer delegate. Weak.
46 MessageDemuxer
* demuxer_
;
48 // The Mach port on which the server is receiving requests.
49 base::mac::ScopedMachReceiveRight server_port_
;
51 // MACH_RECV dispatch source that handles the |server_port_|.
52 scoped_ptr
<base::DispatchSourceMach
> dispatch_source_
;
54 // The reply message, if one has been created.
55 xpc_object_t reply_message_
;
57 DISALLOW_COPY_AND_ASSIGN(XPCMessageServer
);
60 } // namespace sandbox
62 #endif // SANDBOX_MAC_XPC_MESSAGE_SERVER_H_