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_MACH_MESSAGE_SERVER_H_
6 #define SANDBOX_MAC_MACH_MESSAGE_SERVER_H_
10 #include "base/mac/scoped_mach_port.h"
11 #include "base/mac/scoped_mach_vm.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "sandbox/mac/message_server.h"
17 class DispatchSourceMach
;
19 // A Mach message server that operates a receive port. Messages are received
20 // and then passed to the MessageDemuxer for handling. The Demuxer
21 // can use the server class to send a reply, forward the message to a
22 // different port, or reply to the message with a MIG error.
23 class MachMessageServer
: public MessageServer
{
25 // Creates a new Mach message server that will send messages to |demuxer|
26 // for handling. If the |server_receive_right| is non-NULL, this class will
27 // take ownership of the port and it will be used to receive messages.
28 // Otherwise the server will create a new receive right.
29 // The maximum size of messages is specified by |buffer_size|.
30 MachMessageServer(MessageDemuxer
* demuxer
,
31 mach_port_t server_receive_right
,
32 mach_msg_size_t buffer_size
);
33 ~MachMessageServer() override
;
36 bool Initialize() override
;
37 pid_t
GetMessageSenderPID(IPCMessage request
) override
;
38 IPCMessage
CreateReply(IPCMessage request
) override
;
39 bool SendReply(IPCMessage reply
) override
;
40 void ForwardMessage(IPCMessage request
, mach_port_t destination
) override
;
41 // Replies to the message with the specified |error_code| as a MIG
42 // error_reply RetCode.
43 void RejectMessage(IPCMessage request
, int error_code
) override
;
44 mach_port_t
GetServerPort() const override
;
47 // Event handler for the |server_source_| that reads a message from the queue
49 void ReceiveMessage();
51 // The demuxer delegate. Weak.
52 MessageDemuxer
* demuxer_
;
54 // The Mach port on which the server is receiving requests.
55 base::mac::ScopedMachReceiveRight server_port_
;
57 // The size of the two message buffers below.
58 const mach_msg_size_t buffer_size_
;
60 // Request and reply buffers used in ReceiveMessage.
61 base::mac::ScopedMachVM request_buffer_
;
62 base::mac::ScopedMachVM reply_buffer_
;
64 // MACH_RECV dispatch source that handles the |server_port_|.
65 scoped_ptr
<DispatchSourceMach
> dispatch_source_
;
67 // Whether or not ForwardMessage() was called during ReceiveMessage().
68 bool did_forward_message_
;
71 } // namespace sandbox
73 #endif // SANDBOX_MAC_MACH_MESSAGE_SERVER_H_