Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / sandbox / mac / xpc_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_XPC_MESSAGE_SERVER_H_
6 #define SANDBOX_MAC_XPC_MESSAGE_SERVER_H_
7
8 #include "base/mac/scoped_mach_port.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "sandbox/mac/message_server.h"
11 #include "sandbox/mac/xpc.h"
12 #include "sandbox/sandbox_export.h"
13
14 namespace sandbox {
15
16 class DispatchSourceMach;
17
18 // An implementation of MessageServer that uses XPC pipes to read and write XPC
19 // messages from a Mach port.
20 class SANDBOX_EXPORT XPCMessageServer : public MessageServer {
21  public:
22   // Creates a new XPC message server that will send messages to |demuxer|
23   // for handling. If the |server_receive_right| is non-NULL, this class will
24   // take ownership of the port and it will be used to receive messages.
25   // Otherwise the server will create a new receive right on which to listen.
26   XPCMessageServer(MessageDemuxer* demuxer,
27                    mach_port_t server_receive_right);
28   ~XPCMessageServer() override;
29
30   // MessageServer:
31   bool Initialize() override;
32   pid_t GetMessageSenderPID(IPCMessage request) override;
33   IPCMessage CreateReply(IPCMessage request) override;
34   bool SendReply(IPCMessage reply) override;
35   void ForwardMessage(IPCMessage request, mach_port_t destination) override;
36   // Creates an error reply message with a field "error" set to |error_code|.
37   void RejectMessage(IPCMessage request, int error_code) override;
38   mach_port_t GetServerPort() const override;
39
40  private:
41   // Reads a message from the XPC pipe.
42   void ReceiveMessage();
43
44   // The demuxer delegate. Weak.
45   MessageDemuxer* demuxer_;
46
47   // The Mach port on which the server is receiving requests.
48   base::mac::ScopedMachReceiveRight server_port_;
49
50   // MACH_RECV dispatch source that handles the |server_port_|.
51   scoped_ptr<DispatchSourceMach> dispatch_source_;
52
53   // The reply message, if one has been created.
54   xpc_object_t reply_message_;
55
56   DISALLOW_COPY_AND_ASSIGN(XPCMessageServer);
57 };
58
59 }  // namespace sandbox
60
61 #endif  // SANDBOX_MAC_XPC_MESSAGE_SERVER_H_