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_MESSAGE_SERVER_H_ 6 #define SANDBOX_MAC_MESSAGE_SERVER_H_ 7 8 #include <mach/mach.h> 9 #include <unistd.h> 10 11 #include "sandbox/mac/xpc.h" 12 13 namespace sandbox { 14 15 // A message received by a MessageServer. Each concrete implementation of 16 // that interface will handle the fields of this union appropriately. 17 // Consumers should treat this as an opaque handle. 18 union IPCMessage { 19 mach_msg_header_t* mach; 20 xpc_object_t xpc; 21 }; 22 23 // A delegate interface for MessageServer that handles processing of 24 // incoming intercepted IPC messages. 25 class MessageDemuxer { 26 public: 27 // Handle a |request| message. The message is owned by the server. Use the 28 // server's methods to create and send a reply message. 29 virtual void DemuxMessage(IPCMessage request) = 0; 30 31 protected: ~MessageDemuxer()32 virtual ~MessageDemuxer() {} 33 }; 34 35 // An interaface for an IPC server that implements Mach messaging semantics. 36 // The concrete implementation may be powered by raw Mach messages, XPC, or 37 // some other technology. This interface is the abstraction on top of those 38 // that enables message interception. 39 class MessageServer { 40 public: ~MessageServer()41 virtual ~MessageServer() {} 42 43 // Initializes the class and starts running the message server. If this 44 // returns false, no other methods may be called on this class. 45 virtual bool Initialize() = 0; 46 47 // Blocks the calling thread while the server shuts down. This prevents 48 // the server from receiving new messages. After this method is called, 49 // no other methods may be called on this class. 50 virtual void Shutdown() = 0; 51 52 // Given a received request message, returns the PID of the sending process. 53 virtual pid_t GetMessageSenderPID(IPCMessage request) = 0; 54 55 // Creates a reply message from a request message. The result is owned by 56 // the server. 57 virtual IPCMessage CreateReply(IPCMessage request) = 0; 58 59 // Sends a reply message. Returns true if the message was sent successfully. 60 virtual bool SendReply(IPCMessage reply) = 0; 61 62 // Forwards the original |request| to the |destination| for handling. 63 virtual void ForwardMessage(IPCMessage request, mach_port_t destination) = 0; 64 65 // Replies to the received |request| message by creating a reply and setting 66 // the specified |error_code| in a field that is interpreted by the 67 // underlying IPC system. 68 virtual void RejectMessage(IPCMessage request, int error_code) = 0; 69 70 // Returns the Mach port on which the MessageServer is listening. 71 virtual mach_port_t GetServerPort() const = 0; 72 }; 73 74 } // namespace sandbox 75 76 #endif // SANDBOX_MAC_MESSAGE_SERVER_H_ 77