ipc/chromium/src/chrome/common/ipc_channel.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/chrome/common/ipc_channel.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,163 @@
     1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#ifndef CHROME_COMMON_IPC_CHANNEL_H_
     1.9 +#define CHROME_COMMON_IPC_CHANNEL_H_
    1.10 +
    1.11 +#include <queue>
    1.12 +#include "chrome/common/ipc_message.h"
    1.13 +
    1.14 +namespace IPC {
    1.15 +
    1.16 +//------------------------------------------------------------------------------
    1.17 +
    1.18 +class Channel : public Message::Sender {
    1.19 +  // Security tests need access to the pipe handle.
    1.20 +  friend class ChannelTest;
    1.21 +
    1.22 + public:
    1.23 +  // Implemented by consumers of a Channel to receive messages.
    1.24 +  class Listener {
    1.25 +   public:
    1.26 +    virtual ~Listener() {}
    1.27 +
    1.28 +    // Called when a message is received.
    1.29 +    virtual void OnMessageReceived(const Message& message) = 0;
    1.30 +
    1.31 +    // Called when the channel is connected and we have received the internal
    1.32 +    // Hello message from the peer.
    1.33 +    virtual void OnChannelConnected(int32_t peer_pid) {}
    1.34 +
    1.35 +    // Called when an error is detected that causes the channel to close.
    1.36 +    // This method is not called when a channel is closed normally.
    1.37 +    virtual void OnChannelError() {}
    1.38 +
    1.39 +    // If the listener has queued messages, swap them for |queue| like so
    1.40 +    //   swap(impl->my_queued_messages, queue);
    1.41 +    virtual void GetQueuedMessages(std::queue<Message>& queue) {}
    1.42 +  };
    1.43 +
    1.44 +  enum Mode {
    1.45 +    MODE_SERVER,
    1.46 +    MODE_CLIENT
    1.47 +  };
    1.48 +
    1.49 +  enum {
    1.50 +    // The maximum message size in bytes. Attempting to receive a
    1.51 +    // message of this size or bigger results in a channel error.
    1.52 +    kMaximumMessageSize = 256 * 1024 * 1024,
    1.53 +
    1.54 +    // Ammount of data to read at once from the pipe.
    1.55 +    kReadBufferSize = 4 * 1024
    1.56 +  };
    1.57 +
    1.58 +  // Initialize a Channel.
    1.59 +  //
    1.60 +  // |channel_id| identifies the communication Channel.
    1.61 +  // |mode| specifies whether this Channel is to operate in server mode or
    1.62 +  // client mode.  In server mode, the Channel is responsible for setting up the
    1.63 +  // IPC object, whereas in client mode, the Channel merely connects to the
    1.64 +  // already established IPC object.
    1.65 +  // |listener| receives a callback on the current thread for each newly
    1.66 +  // received message.
    1.67 +  //
    1.68 +  Channel(const std::wstring& channel_id, Mode mode, Listener* listener);
    1.69 +
    1.70 +  // XXX it would nice not to have yet more platform-specific code in
    1.71 +  // here but it's just not worth the trouble.
    1.72 +# if defined(OS_POSIX)
    1.73 +  // Connect to a pre-created channel |fd| as |mode|.
    1.74 +  Channel(int fd, Mode mode, Listener* listener);
    1.75 +# elif defined(OS_WIN)
    1.76 +  // Connect to a pre-created channel as |mode|.  Clients connect to
    1.77 +  // the pre-existing server pipe, and servers take over |server_pipe|.
    1.78 +  Channel(const std::wstring& channel_id, void* server_pipe,
    1.79 +	  Mode mode, Listener* listener);
    1.80 +# endif
    1.81 +
    1.82 +  ~Channel();
    1.83 +
    1.84 +  // Connect the pipe.  On the server side, this will initiate
    1.85 +  // waiting for connections.  On the client, it attempts to
    1.86 +  // connect to a pre-existing pipe.  Note, calling Connect()
    1.87 +  // will not block the calling thread and may complete
    1.88 +  // asynchronously.
    1.89 +  bool Connect();
    1.90 +
    1.91 +  // Close this Channel explicitly.  May be called multiple times.
    1.92 +  void Close();
    1.93 +
    1.94 +  // Modify the Channel's listener.
    1.95 +  Listener* set_listener(Listener* listener);
    1.96 +
    1.97 +  // Send a message over the Channel to the listener on the other end.
    1.98 +  //
    1.99 +  // |message| must be allocated using operator new.  This object will be
   1.100 +  // deleted once the contents of the Message have been sent.
   1.101 +  //
   1.102 +  // If you Send() a message on a Close()'d channel, we delete the message
   1.103 +  // immediately.
   1.104 +  virtual bool Send(Message* message);
   1.105 +
   1.106 +  // Unsound_IsClosed() and Unsound_NumQueuedMessages() are safe to call from
   1.107 +  // any thread, but the value returned may be out of date, because we don't
   1.108 +  // use any synchronization when reading or writing it.
   1.109 +  bool Unsound_IsClosed() const;
   1.110 +  uint32_t Unsound_NumQueuedMessages() const;
   1.111 +
   1.112 +#if defined(OS_POSIX)
   1.113 +  // On POSIX an IPC::Channel wraps a socketpair(), this method returns the
   1.114 +  // FD # for the client end of the socket and the equivalent FD# to use for
   1.115 +  // mapping it into the Child process.
   1.116 +  // This method may only be called on the server side of a channel.
   1.117 +  //
   1.118 +  // If the kTestingChannelID flag is specified on the command line then
   1.119 +  // a named FIFO is used as the channel transport mechanism rather than a
   1.120 +  // socketpair() in which case this method returns -1 for both parameters.
   1.121 +  void GetClientFileDescriptorMapping(int *src_fd, int *dest_fd) const;
   1.122 +
   1.123 +  // Return the file descriptor for communication with the peer.
   1.124 +  int GetFileDescriptor() const;
   1.125 +
   1.126 +  // Reset the file descriptor for communication with the peer.
   1.127 +  void ResetFileDescriptor(int fd);
   1.128 +
   1.129 +  // Close the client side of the socketpair.
   1.130 +  void CloseClientFileDescriptor();
   1.131 +
   1.132 +#elif defined(OS_WIN)
   1.133 +  // Return the server pipe handle.
   1.134 +  void* GetServerPipeHandle() const;
   1.135 +#endif  // defined(OS_POSIX)
   1.136 +
   1.137 + private:
   1.138 +  // PIMPL to which all channel calls are delegated.
   1.139 +  class ChannelImpl;
   1.140 +  ChannelImpl *channel_impl_;
   1.141 +
   1.142 +  enum {
   1.143 +#if defined(OS_MACOSX)
   1.144 +    // If the channel receives a message that contains file descriptors, then
   1.145 +    // it will reply back with this message, indicating that the message has
   1.146 +    // been received. The sending channel can then close any descriptors that
   1.147 +    // had been marked as auto_close. This works around a sendmsg() bug on BSD
   1.148 +    // where the kernel can eagerly close file descriptors that are in message
   1.149 +    // queues but not yet delivered.
   1.150 +    RECEIVED_FDS_MESSAGE_TYPE = kuint16max - 1,
   1.151 +#endif
   1.152 +
   1.153 +    // The Hello message is internal to the Channel class.  It is sent
   1.154 +    // by the peer when the channel is connected.  The message contains
   1.155 +    // just the process id (pid).  The message has a special routing_id
   1.156 +    // (MSG_ROUTING_NONE) and type (HELLO_MESSAGE_TYPE).
   1.157 +    HELLO_MESSAGE_TYPE = kuint16max  // Maximum value of message type (uint16_t),
   1.158 +                                     // to avoid conflicting with normal
   1.159 +                                     // message types, which are enumeration
   1.160 +                                     // constants starting from 0.
   1.161 +  };
   1.162 +};
   1.163 +
   1.164 +}  // namespace IPC
   1.165 +
   1.166 +#endif  // CHROME_COMMON_IPC_CHANNEL_H_

mercurial