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_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   virtual ~MachMessageServer();
     34 
     35   // MessageServer:
     36   virtual bool Initialize() OVERRIDE;
     37   virtual pid_t GetMessageSenderPID(IPCMessage request) OVERRIDE;
     38   virtual IPCMessage CreateReply(IPCMessage request) OVERRIDE;
     39   virtual bool SendReply(IPCMessage reply) OVERRIDE;
     40   virtual void ForwardMessage(IPCMessage request,
     41                               mach_port_t destination) OVERRIDE;
     42   // Replies to the message with the specified |error_code| as a MIG
     43   // error_reply RetCode.
     44   virtual void RejectMessage(IPCMessage request, int error_code) OVERRIDE;
     45   virtual mach_port_t GetServerPort() const OVERRIDE;
     46 
     47  private:
     48   // Event handler for the |server_source_| that reads a message from the queue
     49   // and processes it.
     50   void ReceiveMessage();
     51 
     52   // The demuxer delegate. Weak.
     53   MessageDemuxer* demuxer_;
     54 
     55   // The Mach port on which the server is receiving requests.
     56   base::mac::ScopedMachReceiveRight server_port_;
     57 
     58   // The size of the two message buffers below.
     59   const mach_msg_size_t buffer_size_;
     60 
     61   // Request and reply buffers used in ReceiveMessage.
     62   base::mac::ScopedMachVM request_buffer_;
     63   base::mac::ScopedMachVM reply_buffer_;
     64 
     65   // MACH_RECV dispatch source that handles the |server_port_|.
     66   scoped_ptr<DispatchSourceMach> dispatch_source_;
     67 
     68   // Whether or not ForwardMessage() was called during ReceiveMessage().
     69   bool did_forward_message_;
     70 };
     71 
     72 }  // namespace sandbox
     73 
     74 #endif  // SANDBOX_MAC_MACH_MESSAGE_SERVER_H_
     75