Index: ipc/mojo/ipc_channel_mojo_host.cc |
diff --git a/ipc/mojo/ipc_channel_mojo_host.cc b/ipc/mojo/ipc_channel_mojo_host.cc |
index 4318a15f54379e5fb568ebd15735eb313b2ebf14..87290ceac69307bfac2b3765fed19a80b931d4bf 100644 |
--- a/ipc/mojo/ipc_channel_mojo_host.cc |
+++ b/ipc/mojo/ipc_channel_mojo_host.cc |
@@ -10,38 +10,106 @@ |
namespace IPC { |
-ChannelMojoHost::ChannelMojoHost(scoped_refptr<base::TaskRunner> task_runner) |
- : weak_factory_(this), task_runner_(task_runner), channel_(NULL) { |
+// The delete class lives on the IO thread to talk to ChannelMojo on |
+// behalf of ChannelMojoHost. |
+// |
+// The object must be touched only on the IO thread. |
+class ChannelMojoHost::ChannelDelegate : public ChannelMojo::Delegate { |
+ public: |
+ explicit ChannelDelegate(scoped_refptr<base::TaskRunner> io_task_runner); |
+ virtual ~ChannelDelegate(); |
+ |
+ // ChannelMojo::Delegate |
+ virtual base::WeakPtr<Delegate> ToWeakPtr() OVERRIDE; |
+ virtual void OnChannelCreated(base::WeakPtr<ChannelMojo> channel) OVERRIDE; |
+ virtual scoped_refptr<base::TaskRunner> GetIOTaskRunner() OVERRIDE; |
+ |
+ // Returns an weak ptr of ChannelDelegate instead of Delegate |
+ base::WeakPtr<ChannelDelegate> GetWeakPtr(); |
+ void OnClientLaunched(base::ProcessHandle process); |
+ void DeleteThisSoon(); |
+ |
+ private: |
+ scoped_refptr<base::TaskRunner> io_task_runner_; |
+ base::WeakPtrFactory<ChannelDelegate> weak_factory_; |
+ base::WeakPtr<ChannelMojo> channel_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ChannelDelegate); |
+}; |
+ |
+ChannelMojoHost::ChannelDelegate::ChannelDelegate( |
+ scoped_refptr<base::TaskRunner> io_task_runner) |
+ : io_task_runner_(io_task_runner), weak_factory_(this) { |
+} |
+ |
+ChannelMojoHost::ChannelDelegate::~ChannelDelegate() { |
+} |
+ |
+base::WeakPtr<ChannelMojo::Delegate> |
+ChannelMojoHost::ChannelDelegate::ToWeakPtr() { |
+ return weak_factory_.GetWeakPtr(); |
+} |
+ |
+base::WeakPtr<ChannelMojoHost::ChannelDelegate> |
+ChannelMojoHost::ChannelDelegate::GetWeakPtr() { |
+ return weak_factory_.GetWeakPtr(); |
+} |
+ |
+void ChannelMojoHost::ChannelDelegate::OnChannelCreated( |
+ base::WeakPtr<ChannelMojo> channel) { |
+ DCHECK(!channel_); |
+ channel_ = channel; |
+} |
+ |
+scoped_refptr<base::TaskRunner> |
+ChannelMojoHost::ChannelDelegate::GetIOTaskRunner() { |
+ return io_task_runner_; |
+} |
+ |
+void ChannelMojoHost::ChannelDelegate::OnClientLaunched( |
+ base::ProcessHandle process) { |
+ if (channel_) |
+ channel_->OnClientLaunched(process); |
+} |
+ |
+void ChannelMojoHost::ChannelDelegate::DeleteThisSoon() { |
+ io_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&base::DeletePointer<ChannelMojoHost::ChannelDelegate>, |
+ base::Unretained(this))); |
+} |
+ |
+// |
+// ChannelMojoHost |
+// |
+ |
+ChannelMojoHost::ChannelMojoHost(scoped_refptr<base::TaskRunner> io_task_runner) |
+ : weak_factory_(this), |
+ io_task_runner_(io_task_runner), |
+ channel_delegate_(new ChannelDelegate(io_task_runner)) { |
} |
ChannelMojoHost::~ChannelMojoHost() { |
} |
void ChannelMojoHost::OnClientLaunched(base::ProcessHandle process) { |
- if (task_runner_ == base::MessageLoop::current()->message_loop_proxy()) { |
- InvokeOnClientLaunched(process); |
+ if (io_task_runner_ == base::MessageLoop::current()->message_loop_proxy()) { |
+ channel_delegate_->OnClientLaunched(process); |
} else { |
- task_runner_->PostTask(FROM_HERE, |
- base::Bind(&ChannelMojoHost::InvokeOnClientLaunched, |
- weak_factory_.GetWeakPtr(), |
- process)); |
+ io_task_runner_->PostTask(FROM_HERE, |
+ base::Bind(&ChannelDelegate::OnClientLaunched, |
+ channel_delegate_->GetWeakPtr(), |
+ process)); |
} |
} |
-void ChannelMojoHost::InvokeOnClientLaunched(base::ProcessHandle process) { |
- if (!channel_) |
- return; |
- channel_->OnClientLaunched(process); |
-} |
- |
-void ChannelMojoHost::OnChannelCreated(ChannelMojo* channel) { |
- DCHECK(channel_ == NULL); |
- channel_ = channel; |
+ChannelMojo::Delegate* ChannelMojoHost::channel_delegate() const { |
+ return channel_delegate_.get(); |
} |
-void ChannelMojoHost::OnChannelDestroyed() { |
- DCHECK(channel_ != NULL); |
- channel_ = NULL; |
+void ChannelMojoHost::DelegateDeleter::operator()( |
+ ChannelMojoHost::ChannelDelegate* ptr) const { |
+ ptr->DeleteThisSoon(); |
} |
} // namespace IPC |