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