Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / sandbox / mac / mach_message_server.h
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.
4
5 #ifndef SANDBOX_MAC_MACH_MESSAGE_SERVER_H_
6 #define SANDBOX_MAC_MACH_MESSAGE_SERVER_H_
7
8 #include <mach/mach.h>
9
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"
14
15 namespace sandbox {
16
17 class DispatchSourceMach;
18
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 {
24  public:
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;
34
35   // MessageServer:
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;
45
46  private:
47   // Event handler for the |server_source_| that reads a message from the queue
48   // and processes it.
49   void ReceiveMessage();
50
51   // The demuxer delegate. Weak.
52   MessageDemuxer* demuxer_;
53
54   // The Mach port on which the server is receiving requests.
55   base::mac::ScopedMachReceiveRight server_port_;
56
57   // The size of the two message buffers below.
58   const mach_msg_size_t buffer_size_;
59
60   // Request and reply buffers used in ReceiveMessage.
61   base::mac::ScopedMachVM request_buffer_;
62   base::mac::ScopedMachVM reply_buffer_;
63
64   // MACH_RECV dispatch source that handles the |server_port_|.
65   scoped_ptr<DispatchSourceMach> dispatch_source_;
66
67   // Whether or not ForwardMessage() was called during ReceiveMessage().
68   bool did_forward_message_;
69 };
70
71 }  // namespace sandbox
72
73 #endif  // SANDBOX_MAC_MACH_MESSAGE_SERVER_H_