LCOV - code coverage report
Current view: top level - ipc/chromium/src/chrome/common - ipc_channel.h (source / functions) Hit Total Coverage
Test: output.info Lines: 3 5 60.0 %
Date: 2017-07-14 16:53:18 Functions: 3 6 50.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2             : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
       3             : // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
       4             : // Use of this source code is governed by a BSD-style license that can be
       5             : // found in the LICENSE file.
       6             : 
       7             : #ifndef CHROME_COMMON_IPC_CHANNEL_H_
       8             : #define CHROME_COMMON_IPC_CHANNEL_H_
       9             : 
      10             : #include <string>
      11             : 
      12             : #include <queue>
      13             : #include "chrome/common/ipc_message.h"
      14             : 
      15             : namespace IPC {
      16             : 
      17             : //------------------------------------------------------------------------------
      18             : 
      19             : class Channel {
      20             :   // Security tests need access to the pipe handle.
      21             :   friend class ChannelTest;
      22             : 
      23             :  public:
      24             :   // Implemented by consumers of a Channel to receive messages.
      25          34 :   class Listener {
      26             :    public:
      27           0 :     virtual ~Listener() {}
      28             : 
      29             :     // Called when a message is received.
      30             :     virtual void OnMessageReceived(Message&& message) = 0;
      31             : 
      32             :     // Called when the channel is connected and we have received the internal
      33             :     // Hello message from the peer.
      34           2 :     virtual void OnChannelConnected(int32_t peer_pid) {}
      35             : 
      36             :     // Called when an error is detected that causes the channel to close.
      37             :     // This method is not called when a channel is closed normally.
      38           0 :     virtual void OnChannelError() {}
      39             : 
      40             :     // If the listener has queued messages, swap them for |queue| like so
      41             :     //   swap(impl->my_queued_messages, queue);
      42           2 :     virtual void GetQueuedMessages(std::queue<Message>& queue) {}
      43             :   };
      44             : 
      45             :   enum Mode {
      46             :     MODE_SERVER,
      47             :     MODE_CLIENT
      48             :   };
      49             : 
      50             :   enum {
      51             :     // The maximum message size in bytes. Attempting to receive a
      52             :     // message of this size or bigger results in a channel error.
      53             :     kMaximumMessageSize = 256 * 1024 * 1024,
      54             : 
      55             :     // Ammount of data to read at once from the pipe.
      56             :     kReadBufferSize = 4 * 1024,
      57             : 
      58             :     // Maximum size of a message that we allow to be copied (rather than moved).
      59             :     kMaxCopySize = 32 * 1024,
      60             :   };
      61             : 
      62             :   // Initialize a Channel.
      63             :   //
      64             :   // |channel_id| identifies the communication Channel.
      65             :   // |mode| specifies whether this Channel is to operate in server mode or
      66             :   // client mode.  In server mode, the Channel is responsible for setting up the
      67             :   // IPC object, whereas in client mode, the Channel merely connects to the
      68             :   // already established IPC object.
      69             :   // |listener| receives a callback on the current thread for each newly
      70             :   // received message.
      71             :   //
      72             :   Channel(const std::wstring& channel_id, Mode mode, Listener* listener);
      73             : 
      74             :   // XXX it would nice not to have yet more platform-specific code in
      75             :   // here but it's just not worth the trouble.
      76             : # if defined(OS_POSIX)
      77             :   // Connect to a pre-created channel |fd| as |mode|.
      78             :   Channel(int fd, Mode mode, Listener* listener);
      79             : # elif defined(OS_WIN)
      80             :   // Connect to a pre-created channel as |mode|.  Clients connect to
      81             :   // the pre-existing server pipe, and servers take over |server_pipe|.
      82             :   Channel(const std::wstring& channel_id, void* server_pipe,
      83             :           Mode mode, Listener* listener);
      84             : # endif
      85             : 
      86             :   ~Channel();
      87             : 
      88             :   // Connect the pipe.  On the server side, this will initiate
      89             :   // waiting for connections.  On the client, it attempts to
      90             :   // connect to a pre-existing pipe.  Note, calling Connect()
      91             :   // will not block the calling thread and may complete
      92             :   // asynchronously.
      93             :   bool Connect();
      94             : 
      95             :   // Close this Channel explicitly.  May be called multiple times.
      96             :   void Close();
      97             : 
      98             :   // Modify the Channel's listener.
      99             :   Listener* set_listener(Listener* listener);
     100             : 
     101             :   // Send a message over the Channel to the listener on the other end.
     102             :   //
     103             :   // |message| must be allocated using operator new.  This object will be
     104             :   // deleted once the contents of the Message have been sent.
     105             :   //
     106             :   // If you Send() a message on a Close()'d channel, we delete the message
     107             :   // immediately.
     108             :   bool Send(Message* message);
     109             : 
     110             :   // Unsound_IsClosed() and Unsound_NumQueuedMessages() are safe to call from
     111             :   // any thread, but the value returned may be out of date, because we don't
     112             :   // use any synchronization when reading or writing it.
     113             :   bool Unsound_IsClosed() const;
     114             :   uint32_t Unsound_NumQueuedMessages() const;
     115             : 
     116             : #if defined(OS_POSIX)
     117             :   // On POSIX an IPC::Channel wraps a socketpair(), this method returns the
     118             :   // FD # for the client end of the socket and the equivalent FD# to use for
     119             :   // mapping it into the Child process.
     120             :   // This method may only be called on the server side of a channel.
     121             :   //
     122             :   // If the kTestingChannelID flag is specified on the command line then
     123             :   // a named FIFO is used as the channel transport mechanism rather than a
     124             :   // socketpair() in which case this method returns -1 for both parameters.
     125             :   void GetClientFileDescriptorMapping(int *src_fd, int *dest_fd) const;
     126             : 
     127             :   // Return the file descriptor for communication with the peer.
     128             :   int GetFileDescriptor() const;
     129             : 
     130             :   // Reset the file descriptor for communication with the peer.
     131             :   void ResetFileDescriptor(int fd);
     132             : 
     133             :   // Close the client side of the socketpair.
     134             :   void CloseClientFileDescriptor();
     135             : 
     136             : #elif defined(OS_WIN)
     137             :   // Return the server pipe handle.
     138             :   void* GetServerPipeHandle() const;
     139             : #endif  // defined(OS_POSIX)
     140             : 
     141             :   // Generates a channel ID that's non-predictable and unique.
     142             :   static std::wstring GenerateUniqueRandomChannelID();
     143             : 
     144             :   // Generates a channel ID that, if passed to the client as a shared secret,
     145             :   // will validate that the client's authenticity. On platforms that do not
     146             :   // require additional validation this is simply calls GenerateUniqueRandomChannelID().
     147             :   // For portability the prefix should not include the \ character.
     148             :   static std::wstring GenerateVerifiedChannelID(const std::wstring& prefix);
     149             : 
     150             : #if defined(MOZ_WIDGET_ANDROID)
     151             :   // Used to set the first IPC file descriptor in the child process on Android. See
     152             :   // ipc_channel_posix.cc for further details on how this is used.
     153             :   static void SetClientChannelFd(int fd);
     154             : #endif // defined(MOZ_WIDGET_ANDROID)
     155             : 
     156             :  private:
     157             :   // PIMPL to which all channel calls are delegated.
     158             :   class ChannelImpl;
     159             :   ChannelImpl *channel_impl_;
     160             : 
     161             :   enum {
     162             : #if defined(OS_MACOSX)
     163             :     // If the channel receives a message that contains file descriptors, then
     164             :     // it will reply back with this message, indicating that the message has
     165             :     // been received. The sending channel can then close any descriptors that
     166             :     // had been marked as auto_close. This works around a sendmsg() bug on BSD
     167             :     // where the kernel can eagerly close file descriptors that are in message
     168             :     // queues but not yet delivered.
     169             :     RECEIVED_FDS_MESSAGE_TYPE = kuint16max - 1,
     170             : #endif
     171             : 
     172             :     // The Hello message is internal to the Channel class.  It is sent
     173             :     // by the peer when the channel is connected.  The message contains
     174             :     // just the process id (pid).  The message has a special routing_id
     175             :     // (MSG_ROUTING_NONE) and type (HELLO_MESSAGE_TYPE).
     176             :     HELLO_MESSAGE_TYPE = kuint16max  // Maximum value of message type (uint16_t),
     177             :                                      // to avoid conflicting with normal
     178             :                                      // message types, which are enumeration
     179             :                                      // constants starting from 0.
     180             :   };
     181             : };
     182             : 
     183             : }  // namespace IPC
     184             : 
     185             : #endif  // CHROME_COMMON_IPC_CHANNEL_H_

Generated by: LCOV version 1.13