Skip to content

Commit

Permalink
RefCounted types should not have public destructors, ipc/ edition
Browse files Browse the repository at this point in the history
BUG=123295
TEST=none


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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@134421 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
rsleevi@chromium.org committed Apr 28, 2012
1 parent ef40efe commit f729d15
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 59 deletions.
34 changes: 6 additions & 28 deletions ipc/ipc_channel_proxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,10 @@

namespace IPC {

// This helper ensures the message is deleted if the task is deleted without
// having been run.
class SendCallbackHelper
: public base::RefCountedThreadSafe<SendCallbackHelper> {
public:
SendCallbackHelper(ChannelProxy::Context* context, Message* message)
: context_(context),
message_(message) {
}

void Send() {
context_->OnSendMessage(message_.release());
}

private:
scoped_refptr<ChannelProxy::Context> context_;
scoped_ptr<Message> message_;

DISALLOW_COPY_AND_ASSIGN(SendCallbackHelper);
};

//------------------------------------------------------------------------------

ChannelProxy::MessageFilter::MessageFilter() {}

ChannelProxy::MessageFilter::~MessageFilter() {}

void ChannelProxy::MessageFilter::OnFilterAdded(Channel* channel) {}

void ChannelProxy::MessageFilter::OnFilterRemoved() {}
Expand All @@ -59,6 +36,8 @@ void ChannelProxy::MessageFilter::OnDestruct() const {
delete this;
}

ChannelProxy::MessageFilter::~MessageFilter() {}

//------------------------------------------------------------------------------

ChannelProxy::Context::Context(Channel::Listener* listener,
Expand Down Expand Up @@ -186,13 +165,12 @@ void ChannelProxy::Context::OnChannelClosed() {
}

// Called on the IPC::Channel thread
void ChannelProxy::Context::OnSendMessage(Message* message) {
void ChannelProxy::Context::OnSendMessage(scoped_ptr<Message> message) {
if (!channel_.get()) {
delete message;
OnChannelClosed();
return;
}
if (!channel_->Send(message))
if (!channel_->Send(message.release()))
OnChannelError();
}

Expand Down Expand Up @@ -368,8 +346,8 @@ bool ChannelProxy::Send(Message* message) {

context_->ipc_message_loop()->PostTask(
FROM_HERE,
base::Bind(&SendCallbackHelper::Send,
new SendCallbackHelper(context_.get(), message)));
base::Bind(&ChannelProxy::Context::OnSendMessage,
context_, base::Passed(scoped_ptr<Message>(message))));
return true;
}

Expand Down
11 changes: 9 additions & 2 deletions ipc/ipc_channel_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class IPC_EXPORT ChannelProxy : public Message::Sender {
: public base::RefCountedThreadSafe<MessageFilter, MessageFilterTraits> {
public:
MessageFilter();
virtual ~MessageFilter();

// Called on the background thread to provide the filter with access to the
// channel. Called when the IPC channel is initialized or when AddFilter
Expand Down Expand Up @@ -90,6 +89,13 @@ class IPC_EXPORT ChannelProxy : public Message::Sender {
// derived classes the option of controlling which thread they're deleted
// on etc.
virtual void OnDestruct() const;

protected:
virtual ~MessageFilter();

private:
friend class base::RefCountedThreadSafe<MessageFilter,
MessageFilterTraits>;
};

struct MessageFilterTraits {
Expand All @@ -98,6 +104,7 @@ class IPC_EXPORT ChannelProxy : public Message::Sender {
}
};


// Interface for a filter to be imposed on outgoing messages which can
// re-write the message. Used mainly for testing.
class OutgoingMessageFilter {
Expand Down Expand Up @@ -233,7 +240,7 @@ class IPC_EXPORT ChannelProxy : public Message::Sender {
const Channel::Mode& mode);

// Methods called on the IO thread.
void OnSendMessage(Message* message_ptr);
void OnSendMessage(scoped_ptr<Message> message_ptr);
void OnAddFilter();
void OnRemoveFilter(MessageFilter* filter);

Expand Down
3 changes: 3 additions & 0 deletions ipc/ipc_sync_channel_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,9 @@ class TestSyncMessageFilter : public SyncMessageFilter {
worker_->Done();
}

private:
virtual ~TestSyncMessageFilter() {}

Worker* worker_;
base::Thread thread_;
};
Expand Down
54 changes: 27 additions & 27 deletions ipc/ipc_sync_message_filter.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

Expand All @@ -21,9 +21,6 @@ SyncMessageFilter::SyncMessageFilter(base::WaitableEvent* shutdown_event)
shutdown_event_(shutdown_event) {
}

SyncMessageFilter::~SyncMessageFilter() {
}

bool SyncMessageFilter::Send(Message* message) {
{
base::AutoLock auto_lock(lock_);
Expand Down Expand Up @@ -69,29 +66,6 @@ bool SyncMessageFilter::Send(Message* message) {
return pending_message.send_result;
}

void SyncMessageFilter::SendOnIOThread(Message* message) {
if (channel_) {
channel_->Send(message);
return;
}

if (message->is_sync()) {
// We don't know which thread sent it, but it doesn't matter, just signal
// them all.
SignalAllEvents();
}

delete message;
}

void SyncMessageFilter::SignalAllEvents() {
base::AutoLock auto_lock(lock_);
for (PendingSyncMessages::iterator iter = pending_sync_messages_.begin();
iter != pending_sync_messages_.end(); ++iter) {
(*iter)->done_event->Signal();
}
}

void SyncMessageFilter::OnFilterAdded(Channel* channel) {
channel_ = channel;
base::AutoLock auto_lock(lock_);
Expand Down Expand Up @@ -125,4 +99,30 @@ bool SyncMessageFilter::OnMessageReceived(const Message& message) {
return false;
}

SyncMessageFilter::~SyncMessageFilter() {
}

void SyncMessageFilter::SendOnIOThread(Message* message) {
if (channel_) {
channel_->Send(message);
return;
}

if (message->is_sync()) {
// We don't know which thread sent it, but it doesn't matter, just signal
// them all.
SignalAllEvents();
}

delete message;
}

void SyncMessageFilter::SignalAllEvents() {
base::AutoLock auto_lock(lock_);
for (PendingSyncMessages::iterator iter = pending_sync_messages_.begin();
iter != pending_sync_messages_.end(); ++iter) {
(*iter)->done_event->Signal();
}
}

} // namespace IPC
6 changes: 4 additions & 2 deletions ipc/ipc_sync_message_filter.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

Expand Down Expand Up @@ -30,7 +30,6 @@ class IPC_EXPORT SyncMessageFilter : public ChannelProxy::MessageFilter,
public Message::Sender {
public:
explicit SyncMessageFilter(base::WaitableEvent* shutdown_event);
virtual ~SyncMessageFilter();

// Message::Sender implementation.
virtual bool Send(Message* message) OVERRIDE;
Expand All @@ -41,6 +40,9 @@ class IPC_EXPORT SyncMessageFilter : public ChannelProxy::MessageFilter,
virtual void OnChannelClosing() OVERRIDE;
virtual bool OnMessageReceived(const Message& message) OVERRIDE;

protected:
virtual ~SyncMessageFilter();

private:
void SendOnIOThread(Message* message);
// Signal all the pending sends as done, used in an error condition.
Expand Down

0 comments on commit f729d15

Please sign in to comment.