Home | History | Annotate | Download | only in mac
      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:
     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:
     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   // Given a received request message, returns the PID of the sending process.
     48   virtual pid_t GetMessageSenderPID(IPCMessage request) = 0;
     49 
     50   // Creates a reply message from a request message. The result is owned by
     51   // the server.
     52   virtual IPCMessage CreateReply(IPCMessage request) = 0;
     53 
     54   // Sends a reply message. Returns true if the message was sent successfully.
     55   virtual bool SendReply(IPCMessage reply) = 0;
     56 
     57   // Forwards the original |request| to the |destination| for handling.
     58   virtual void ForwardMessage(IPCMessage request, mach_port_t destination) = 0;
     59 
     60   // Replies to the received |request| message by creating a reply and setting
     61   // the specified |error_code| in a field that is interpreted by the
     62   // underlying IPC system.
     63   virtual void RejectMessage(IPCMessage request, int error_code) = 0;
     64 
     65   // Returns the Mach port on which the MessageServer is listening.
     66   virtual mach_port_t GetServerPort() const = 0;
     67 };
     68 
     69 }  // namespace sandbox
     70 
     71 #endif  // SANDBOX_MAC_MESSAGE_SERVER_H_
     72