summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordmaclach@chromium.org <dmaclach@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-02-03 20:35:09 +0000
committerdmaclach@chromium.org <dmaclach@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-02-03 20:35:09 +0000
commit1707726c90f2e3eca0c91e08586130988bd6edfd (patch)
tree8e36a6b66a3004386274f8b9b9d91d9207572588
parentc4ca989fc5da9169319c96ecf8549a3bc082981b (diff)
downloadchromium_src-1707726c90f2e3eca0c91e08586130988bd6edfd.zip
chromium_src-1707726c90f2e3eca0c91e08586130988bd6edfd.tar.gz
chromium_src-1707726c90f2e3eca0c91e08586130988bd6edfd.tar.bz2
Clean up channel modes
Makes channel modes flags instead of a straight enum allowing us to check properties directly. BUG=none TEST=BUILD Review URL: http://codereview.chromium.org/6334061 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@73658 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/plugin/plugin_channel_base.cc2
-rw-r--r--ipc/ipc_channel.h23
-rw-r--r--ipc/ipc_channel_posix.cc59
-rw-r--r--ipc/ipc_channel_posix.h4
-rw-r--r--ipc/ipc_channel_proxy.cc2
-rw-r--r--ipc/ipc_channel_win.cc24
-rw-r--r--ipc/ipc_sync_channel_unittest.cc4
7 files changed, 51 insertions, 67 deletions
diff --git a/chrome/plugin/plugin_channel_base.cc b/chrome/plugin/plugin_channel_base.cc
index e251e9e..3c43124 100644
--- a/chrome/plugin/plugin_channel_base.cc
+++ b/chrome/plugin/plugin_channel_base.cc
@@ -44,7 +44,7 @@ PluginChannelBase* PluginChannelBase::GetChannel(
if (!channel->channel_valid()) {
channel->channel_handle_ = channel_handle;
- if (mode == IPC::Channel::MODE_SERVER) {
+ if (mode & IPC::Channel::MODE_SERVER_FLAG) {
channel->channel_handle_.name.append(".");
channel->channel_handle_.name.append(base::IntToString(next_pipe_id++));
}
diff --git a/ipc/ipc_channel.h b/ipc/ipc_channel.h
index 3b8afd8..91d9f2b 100644
--- a/ipc/ipc_channel.h
+++ b/ipc/ipc_channel.h
@@ -62,17 +62,26 @@ class Channel : public Message::Sender {
#endif // OS_POSIX
};
+ // Flags to test modes
+ enum ModeFlags {
+ MODE_NO_FLAG = 0x0,
+ MODE_SERVER_FLAG = 0x1,
+ MODE_CLIENT_FLAG = 0x2,
+ MODE_NAMED_FLAG = 0x4
+ };
+
+ // Some Standard Modes
enum Mode {
- MODE_NONE,
- MODE_SERVER,
- MODE_CLIENT,
+ MODE_NONE = MODE_NO_FLAG,
+ MODE_SERVER = MODE_SERVER_FLAG,
+ MODE_CLIENT = MODE_CLIENT_FLAG,
// 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,
+ // On Windows MODE_NAMED_SERVER is equivalent to MODE_SERVER and
+ // MODE_NAMED_CLIENT is equivalent to MODE_CLIENT.
+ MODE_NAMED_SERVER = MODE_SERVER_FLAG | MODE_NAMED_FLAG,
+ MODE_NAMED_CLIENT = MODE_CLIENT_FLAG | MODE_NAMED_FLAG,
};
enum {
diff --git a/ipc/ipc_channel_posix.cc b/ipc/ipc_channel_posix.cc
index 9a7c55d..93a94d6 100644
--- a/ipc/ipc_channel_posix.cc
+++ b/ipc/ipc_channel_posix.cc
@@ -318,21 +318,9 @@ Channel::ChannelImpl::ChannelImpl(const IPC::ChannelHandle& channel_handle,
listener_(listener),
must_unlink_(false),
factory_(this) {
- // Check to see if we want to implement using domain sockets.
- bool uses_domain_socket = false;
- bool listening_socket = false;
- if (mode_ == MODE_NAMED_SERVER) {
- uses_domain_socket = true;
- listening_socket = true;
- mode_ = MODE_SERVER;
- } else if (mode_ == MODE_NAMED_CLIENT) {
- uses_domain_socket = true;
- mode_ = MODE_CLIENT;
- }
- if (!CreatePipe(channel_handle, uses_domain_socket, listening_socket)) {
+ if (!CreatePipe(channel_handle)) {
// The pipe may have been closed already.
- const char *modestr = (mode_ == MODE_SERVER
- || mode_ == MODE_NAMED_SERVER) ? "server" : "client";
+ const char *modestr = (mode_ & MODE_SERVER_FLAG) ? "server" : "client";
// The pipe may have been closed already.
LOG(WARNING) << "Unable to create pipe named \"" << channel_handle.name
<< "\" in " << modestr << " mode";
@@ -367,9 +355,8 @@ bool SocketPair(int* fd1, int* fd2) {
return true;
}
-bool Channel::ChannelImpl::CreatePipe(const IPC::ChannelHandle& channel_handle,
- bool uses_domain_sockets,
- bool listening_socket) {
+bool Channel::ChannelImpl::CreatePipe(
+ const IPC::ChannelHandle& channel_handle) {
DCHECK(server_listen_pipe_ == -1 && pipe_ == -1);
// Four possible cases:
@@ -397,21 +384,24 @@ bool Channel::ChannelImpl::CreatePipe(const IPC::ChannelHandle& channel_handle,
return false;
}
#endif // IPC_USES_READWRITE
- } else if (uses_domain_sockets) {
+ } else if (mode_ & MODE_NAMED_FLAG) {
// Case 2 from comment above.
must_unlink_ = true;
- if (mode_ == MODE_SERVER) {
+ if (mode_ & MODE_SERVER_FLAG) {
if (!CreateServerUnixDomainSocket(pipe_name_, &pipe_)) {
return false;
}
- } else if (mode_ == MODE_CLIENT) {
+ } else if (mode_ & MODE_CLIENT_FLAG) {
if (!CreateClientUnixDomainSocket(pipe_name_, &pipe_)) {
return false;
}
+ } else {
+ NOTREACHED();
+ return false;
}
} else {
pipe_ = PipeMap::GetInstance()->Lookup(pipe_name_);
- if (mode_ == MODE_CLIENT) {
+ if (mode_ & MODE_CLIENT_FLAG) {
if (pipe_ != -1) {
// Case 3 from comment above.
// We only allow one connection.
@@ -432,7 +422,7 @@ bool Channel::ChannelImpl::CreatePipe(const IPC::ChannelHandle& channel_handle,
pipe_ = base::GlobalDescriptors::GetInstance()->Get(kPrimaryIPCChannel);
}
- } else if (mode_ == MODE_SERVER) {
+ } else if (mode_ & MODE_SERVER_FLAG) {
// Case 4b from comment above.
if (pipe_ != -1) {
LOG(ERROR) << "Server already exists for " << pipe_name_;
@@ -442,22 +432,20 @@ bool Channel::ChannelImpl::CreatePipe(const IPC::ChannelHandle& channel_handle,
return false;
PipeMap::GetInstance()->Insert(pipe_name_, client_pipe_);
} else {
- LOG(FATAL) << "Unknown mode " << mode_;
+ NOTREACHED();
return false;
}
}
- if (mode_ == MODE_SERVER) {
- if (listening_socket) {
- server_listen_pipe_ = pipe_;
- pipe_ = -1;
- }
+ if ((mode_ & MODE_SERVER_FLAG) && (mode_ & MODE_NAMED_FLAG)) {
+ server_listen_pipe_ = pipe_;
+ pipe_ = -1;
}
#if defined(IPC_USES_READWRITE)
// Create a dedicated socketpair() for exchanging file descriptors.
// See comments for IPC_USES_READWRITE for details.
- if (mode_ == MODE_CLIENT) {
+ if (mode_ & MODE_CLIENT_FLAG) {
if (!SocketPair(&fd_pipe_, &remote_fd_pipe_)) {
return false;
}
@@ -729,7 +717,7 @@ bool Channel::ChannelImpl::ProcessIncomingMessages() {
NOTREACHED();
}
#if defined(IPC_USES_READWRITE)
- if (mode_ == MODE_SERVER) {
+ if (mode_ & MODE_SERVER_FLAG) {
// With IPC_USES_READWRITE, the Hello message from the client to the
// server also contains the fd_pipe_, which will be used for all
// subsequent file descriptor passing.
@@ -852,7 +840,7 @@ bool Channel::ChannelImpl::ProcessOutgoingMessages() {
if (bytes_written == 1) {
fd_written = pipe_;
#if defined(IPC_USES_READWRITE)
- if (mode_ != MODE_SERVER && IsHelloMessage(msg)) {
+ if ((mode_ & MODE_CLIENT_FLAG) && IsHelloMessage(msg)) {
DCHECK_EQ(msg->file_descriptor_set()->size(), 1U);
}
if (!msgh.msg_controllen) {
@@ -1007,7 +995,7 @@ void Channel::ChannelImpl::OnFileCanReadWithoutBlocking(int fd) {
send_server_hello_msg = true;
waiting_connect_ = false;
} else if (fd == pipe_) {
- if (waiting_connect_ && mode_ == MODE_SERVER) {
+ if (waiting_connect_ && (mode_ & MODE_SERVER_FLAG)) {
send_server_hello_msg = true;
waiting_connect_ = false;
}
@@ -1044,15 +1032,18 @@ bool Channel::ChannelImpl::AcceptConnection() {
this);
QueueHelloMessage();
- if (mode_ == MODE_CLIENT) {
+ if (mode_ & MODE_CLIENT_FLAG) {
// If we are a client we want to send a hello message out immediately.
// In server mode we will send a hello message when we receive one from a
// client.
waiting_connect_ = false;
return ProcessOutgoingMessages();
- } else {
+ } else if (mode_ & MODE_SERVER_FLAG) {
waiting_connect_ = true;
return true;
+ } else {
+ NOTREACHED();
+ return false;
}
}
diff --git a/ipc/ipc_channel_posix.h b/ipc/ipc_channel_posix.h
index ecfd41a..cee613a 100644
--- a/ipc/ipc_channel_posix.h
+++ b/ipc/ipc_channel_posix.h
@@ -56,9 +56,7 @@ class Channel::ChannelImpl : public MessageLoopForIO::Watcher {
void ResetToAcceptingConnectionState();
private:
- bool CreatePipe(const IPC::ChannelHandle& channel_handle,
- bool uses_domain_sockets,
- bool listening_socket);
+ bool CreatePipe(const IPC::ChannelHandle& channel_handle);
bool ProcessIncomingMessages();
bool ProcessOutgoingMessages();
diff --git a/ipc/ipc_channel_proxy.cc b/ipc/ipc_channel_proxy.cc
index e6976ac..d0f0a54 100644
--- a/ipc/ipc_channel_proxy.cc
+++ b/ipc/ipc_channel_proxy.cc
@@ -307,7 +307,7 @@ void ChannelProxy::Init(const IPC::ChannelHandle& channel_handle,
// to be created immediately so that it can be accessed and passed
// to other processes. Forcing it to be created immediately avoids
// race conditions that may otherwise arise.
- if (mode == Channel::MODE_SERVER || mode == Channel::MODE_NAMED_SERVER) {
+ if (mode & Channel::MODE_SERVER_FLAG) {
create_pipe_now = true;
}
#endif // defined(OS_POSIX)
diff --git a/ipc/ipc_channel_win.cc b/ipc/ipc_channel_win.cc
index 717cb04..9182200 100644
--- a/ipc/ipc_channel_win.cc
+++ b/ipc/ipc_channel_win.cc
@@ -103,25 +103,9 @@ Channel::ChannelImpl::ChannelImpl(const IPC::ChannelHandle &channel_handle,
ALLOW_THIS_IN_INITIALIZER_LIST(output_state_(this)),
pipe_(INVALID_HANDLE_VALUE),
listener_(listener),
- waiting_connect_(mode == MODE_SERVER || mode == MODE_NAMED_SERVER),
+ waiting_connect_(mode & MODE_SERVER_FLAG),
processing_incoming_(false),
ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) {
- switch(mode) {
- case MODE_NONE:
- LOG(FATAL) << "Bad mode for " << channel_handle.name;
- break;
- case MODE_SERVER:
- case MODE_CLIENT:
- break;
- case MODE_NAMED_SERVER:
- mode = MODE_SERVER;
- break;
- case MODE_NAMED_CLIENT:
- mode = MODE_CLIENT;
- break;
- // Intentionally no default case here so that the compiler
- // will check that we handle all the cases in the enum.
- }
if (!CreatePipe(channel_handle, mode)) {
// The pipe may have been closed already.
LOG(WARNING) << "Unable to create pipe named \"" << channel_handle.name <<
@@ -195,7 +179,7 @@ bool Channel::ChannelImpl::CreatePipe(const IPC::ChannelHandle &channel_handle,
Mode mode) {
DCHECK(pipe_ == INVALID_HANDLE_VALUE);
const std::wstring pipe_name = PipeName(channel_handle.name);
- if (mode == MODE_SERVER) {
+ if (mode & MODE_SERVER_FLAG) {
SECURITY_ATTRIBUTES security_attributes = {0};
security_attributes.bInheritHandle = FALSE;
security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
@@ -217,7 +201,7 @@ bool Channel::ChannelImpl::CreatePipe(const IPC::ChannelHandle &channel_handle,
5000, // timeout in milliseconds (XXX tune)
&security_attributes);
LocalFree(security_attributes.lpSecurityDescriptor);
- } else {
+ } else if (mode & MODE_CLIENT_FLAG) {
pipe_ = CreateFileW(pipe_name.c_str(),
GENERIC_READ | GENERIC_WRITE,
0,
@@ -226,6 +210,8 @@ bool Channel::ChannelImpl::CreatePipe(const IPC::ChannelHandle &channel_handle,
SECURITY_SQOS_PRESENT | SECURITY_IDENTIFICATION |
FILE_FLAG_OVERLAPPED,
NULL);
+ } else {
+ NOTREACHED();
}
if (pipe_ == INVALID_HANDLE_VALUE) {
// If this process is being closed, the pipe may be gone already.
diff --git a/ipc/ipc_sync_channel_unittest.cc b/ipc/ipc_sync_channel_unittest.cc
index de40083..11e361e 100644
--- a/ipc/ipc_sync_channel_unittest.cc
+++ b/ipc/ipc_sync_channel_unittest.cc
@@ -233,7 +233,7 @@ void RunTest(std::vector<Worker*> workers) {
// First we create the workers that are channel servers, or else the other
// workers' channel initialization might fail because the pipe isn't created..
for (size_t i = 0; i < workers.size(); ++i) {
- if (workers[i]->mode() == Channel::MODE_SERVER) {
+ if (workers[i]->mode() & Channel::MODE_SERVER_FLAG) {
workers[i]->Start();
workers[i]->WaitForChannelCreation();
}
@@ -241,7 +241,7 @@ void RunTest(std::vector<Worker*> workers) {
// now create the clients
for (size_t i = 0; i < workers.size(); ++i) {
- if (workers[i]->mode() == Channel::MODE_CLIENT)
+ if (workers[i]->mode() & Channel::MODE_CLIENT_FLAG)
workers[i]->Start();
}