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"
17 #if defined(MAC_OS_X_VERSION_10_7) && \
18 MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_7
19 // Redeclare methods that only exist on 10.7+ to suppress
20 // -Wpartial-availability warnings.
22 XPC_EXPORT XPC_NONNULL1 XPC_NONNULL2
void
23 xpc_dictionary_set_int64(xpc_object_t xdict
, const char* key
, int64_t value
);
25 XPC_EXPORT XPC_NONNULL1
void xpc_release(xpc_object_t object
);
31 // An implementation of MessageServer that uses XPC pipes to read and write XPC
32 // messages from a Mach port.
33 class SANDBOX_EXPORT XPCMessageServer
: public MessageServer
{
35 // Creates a new XPC message server that will send messages to |demuxer|
36 // for handling. If the |server_receive_right| is non-NULL, this class will
37 // take ownership of the port and it will be used to receive messages.
38 // Otherwise the server will create a new receive right on which to listen.
39 XPCMessageServer(MessageDemuxer
* demuxer
,
40 mach_port_t server_receive_right
);
41 ~XPCMessageServer() override
;
44 bool Initialize() override
;
45 pid_t
GetMessageSenderPID(IPCMessage request
) override
;
46 IPCMessage
CreateReply(IPCMessage request
) override
;
47 bool SendReply(IPCMessage reply
) override
;
48 void ForwardMessage(IPCMessage request
, mach_port_t destination
) override
;
49 // Creates an error reply message with a field "error" set to |error_code|.
50 void RejectMessage(IPCMessage request
, int error_code
) override
;
51 mach_port_t
GetServerPort() const override
;
54 // Reads a message from the XPC pipe.
55 void ReceiveMessage();
57 // The demuxer delegate. Weak.
58 MessageDemuxer
* demuxer_
;
60 // The Mach port on which the server is receiving requests.
61 base::mac::ScopedMachReceiveRight server_port_
;
63 // MACH_RECV dispatch source that handles the |server_port_|.
64 scoped_ptr
<base::DispatchSourceMach
> dispatch_source_
;
66 // The reply message, if one has been created.
67 xpc_object_t reply_message_
;
69 DISALLOW_COPY_AND_ASSIGN(XPCMessageServer
);
72 } // namespace sandbox
74 #endif // SANDBOX_MAC_XPC_MESSAGE_SERVER_H_