Skip to content

Commit

Permalink
Add support for sockets that can listen and accept a connection.
Browse files Browse the repository at this point in the history
These sockets allow one connection at a time, however clients can
connect and disconnect repeatedly.

These are going to be used by Cloud Print, Remoting and
Automation.

BUG=NONE
TEST=BUILD

Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=69660

Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=69690

Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=69694

Review URL: http://codereview.chromium.org/5749001

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@69696 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
dmaclach@chromium.org committed Dec 20, 2010
1 parent c7f91e8 commit 22b42c5
Show file tree
Hide file tree
Showing 12 changed files with 807 additions and 326 deletions.
3 changes: 2 additions & 1 deletion ipc/file_descriptor_set_posix_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

// This test is POSIX only.

#include "ipc/file_descriptor_set_posix.h"

#include <unistd.h>
#include <fcntl.h>

#include "base/basictypes.h"
#include "base/eintr_wrapper.h"
#include "ipc/file_descriptor_set_posix.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {
Expand Down
1 change: 1 addition & 0 deletions ipc/ipc.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
],
'sources': [
'file_descriptor_set_posix_unittest.cc',
'ipc_channel_posix_unittest.cc',
'ipc_fuzzing_tests.cc',
'ipc_message_unittest.cc',
'ipc_send_fds_test.cc',
Expand Down
58 changes: 53 additions & 5 deletions ipc/ipc_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@
namespace IPC {

//------------------------------------------------------------------------------
// See
// http://www.chromium.org/developers/design-documents/inter-process-communication
// for overview of IPC in Chromium.

// Channels are implemented using named pipes on Windows, and
// socket pairs (or in some special cases unix domain sockets) on POSIX.
// On Windows we access pipes in various processes by name.
// On POSIX we pass file descriptors to child processes and assign names to them
// in a lookup table.
// In general on POSIX we do not use unix domain sockets due to security
// concerns and the fact that they can leave garbage around the file system
// (MacOS does not support abstract named unix domain sockets).
// You can use unix domain sockets if you like on POSIX by constructing the
// the channel with the mode set to one of the NAMED modes. NAMED modes are
// currently used by automation and service processes.

class Channel : public Message::Sender {
// Security tests need access to the pipe handle.
Expand All @@ -34,12 +49,29 @@ class Channel : public Message::Sender {
// Called when an error is detected that causes the channel to close.
// This method is not called when a channel is closed normally.
virtual void OnChannelError() {}

#if defined(OS_POSIX)
// Called on the server side when a channel that listens for connections
// denies an attempt to connect.
virtual void OnChannelDenied() {}

// Called on the server side when a channel that listens for connections
// has an error that causes the listening channel to close.
virtual void OnChannelListenError() {}
#endif // OS_POSIX
};

enum Mode {
MODE_NONE,
MODE_SERVER,
MODE_CLIENT
MODE_CLIENT,
// Channels on Windows are named by default and accessible from other
// processes. On POSIX channels are anonymous by default and not accessible
// from other processes. Named channels work via named unix domain sockets.
// On Windows MODE_NAMED_SERVER == MODE_SERVER and
// MODE_NAMED_CLIENT == MODE_CLIENT.
MODE_NAMED_SERVER,
MODE_NAMED_CLIENT,
};

enum {
Expand Down Expand Up @@ -77,6 +109,10 @@ class Channel : public Message::Sender {
bool Connect() WARN_UNUSED_RESULT;

// Close this Channel explicitly. May be called multiple times.
// On POSIX calling close on an IPC channel that listens for connections will
// cause it to close any accepted connections, and it will stop listening for
// new connections. If you just want to close the currently accepted
// connection and listen for new ones, use ResetToAcceptingConnectionState.
void Close();

// Modify the Channel's listener.
Expand All @@ -92,11 +128,23 @@ class Channel : public Message::Sender {
// On POSIX an IPC::Channel wraps a socketpair(), this method returns the
// FD # for the client end of the socket.
// This method may only be called on the server side of a channel.
//
// If the kTestingChannelID flag is specified on the command line then
// a named FIFO is used as the channel transport mechanism rather than a
// socketpair() in which case this method returns -1.
int GetClientFileDescriptor() const;

// On POSIX an IPC::Channel can either wrap an established socket, or it
// can wrap a socket that is listening for connections. Currently an
// IPC::Channel that listens for connections can only accept one connection
// at a time.

// Returns true if the channel supports listening for connections.
bool AcceptsConnections() const;

// Returns true if the channel supports listening for connections and is
// currently connected.
bool HasAcceptedConnection() const;

// Closes any currently connected socket, and returns to a listening state
// for more connections.
void ResetToAcceptingConnectionState();
#endif // defined(OS_POSIX)

protected:
Expand Down
Loading

0 comments on commit 22b42c5

Please sign in to comment.