Skip to content

Commit

Permalink
Marked IPC::ChannelProxy as non thread-safe. Added DCHECKs to verify …
Browse files Browse the repository at this point in the history
…that public methods (with Send() being the only exception) are called on the thread that created the object.

Tests calling Send() from a wrong thread should be fixed first before similar check can be added to Send(). See http://crbug.com/163523 for details.

BUG=163091,163523

Review URL: https://chromiumcodereview.appspot.com/11308278

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@171106 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
alexeypa@chromium.org committed Dec 5, 2012
1 parent 14649ec commit f775d10
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
20 changes: 20 additions & 0 deletions ipc/ipc_channel_proxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,15 @@ ChannelProxy::ChannelProxy(Context* context)
}

ChannelProxy::~ChannelProxy() {
DCHECK(CalledOnValidThread());

Close();
}

void ChannelProxy::Init(const IPC::ChannelHandle& channel_handle,
Channel::Mode mode,
bool create_pipe_now) {
DCHECK(CalledOnValidThread());
DCHECK(!did_init_);
#if defined(OS_POSIX)
// When we are creating a server on POSIX, we need its file descriptor
Expand Down Expand Up @@ -338,6 +341,8 @@ void ChannelProxy::Init(const IPC::ChannelHandle& channel_handle,
}

void ChannelProxy::Close() {
DCHECK(CalledOnValidThread());

// Clear the backpointer to the listener so that any pending calls to
// Context::OnDispatchMessage or OnDispatchError will be ignored. It is
// possible that the channel could be closed while it is receiving messages!
Expand All @@ -351,6 +356,9 @@ void ChannelProxy::Close() {

bool ChannelProxy::Send(Message* message) {
DCHECK(did_init_);

// TODO(alexeypa): add DCHECK(CalledOnValidThread()) here. Currently there are
// tests that call Send() from a wrong thread. See http://crbug.com/163523.
if (outgoing_message_filter())
message = outgoing_message_filter()->Rewrite(message);

Expand All @@ -366,37 +374,49 @@ bool ChannelProxy::Send(Message* message) {
}

void ChannelProxy::AddFilter(MessageFilter* filter) {
DCHECK(CalledOnValidThread());

context_->AddFilter(filter);
}

void ChannelProxy::RemoveFilter(MessageFilter* filter) {
DCHECK(CalledOnValidThread());

context_->ipc_task_runner()->PostTask(
FROM_HERE, base::Bind(&Context::OnRemoveFilter, context_.get(),
make_scoped_refptr(filter)));
}

void ChannelProxy::ClearIPCTaskRunner() {
DCHECK(CalledOnValidThread());

context()->ClearIPCTaskRunner();
}

#if defined(OS_POSIX) && !defined(OS_NACL)
// See the TODO regarding lazy initialization of the channel in
// ChannelProxy::Init().
int ChannelProxy::GetClientFileDescriptor() {
DCHECK(CalledOnValidThread());

Channel* channel = context_.get()->channel_.get();
// Channel must have been created first.
DCHECK(channel) << context_.get()->channel_id_;
return channel->GetClientFileDescriptor();
}

int ChannelProxy::TakeClientFileDescriptor() {
DCHECK(CalledOnValidThread());

Channel* channel = context_.get()->channel_.get();
// Channel must have been created first.
DCHECK(channel) << context_.get()->channel_id_;
return channel->TakeClientFileDescriptor();
}

bool ChannelProxy::GetClientEuid(uid_t* client_euid) const {
DCHECK(CalledOnValidThread());

Channel* channel = context_.get()->channel_.get();
// Channel must have been created first.
DCHECK(channel) << context_.get()->channel_id_;
Expand Down
3 changes: 2 additions & 1 deletion ipc/ipc_channel_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/synchronization/lock.h"
#include "base/threading/non_thread_safe.h"
#include "ipc/ipc_channel.h"
#include "ipc/ipc_channel_handle.h"
#include "ipc/ipc_listener.h"
Expand Down Expand Up @@ -51,7 +52,7 @@ class SendCallbackHelper;
// The consumer of IPC::ChannelProxy is responsible for allocating the Thread
// instance where the IPC::Channel will be created and operated.
//
class IPC_EXPORT ChannelProxy : public Sender {
class IPC_EXPORT ChannelProxy : public Sender, public base::NonThreadSafe {
public:
struct MessageFilterTraits;

Expand Down

0 comments on commit f775d10

Please sign in to comment.