OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ipc/mojo/ipc_channel_mojo_host.h" | 5 #include "ipc/mojo/ipc_channel_mojo_host.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "ipc/mojo/ipc_channel_mojo.h" | 9 #include "ipc/mojo/ipc_channel_mojo.h" |
10 | 10 |
11 namespace IPC { | 11 namespace IPC { |
12 | 12 |
13 ChannelMojoHost::ChannelMojoHost(scoped_refptr<base::TaskRunner> task_runner) | 13 // The delete class lives on the IO thread to talk to ChannelMojo on |
14 : weak_factory_(this), task_runner_(task_runner), channel_(NULL) { | 14 // behalf of ChannelMojoHost. |
| 15 // |
| 16 // The object must be touched only on the IO thread. |
| 17 class ChannelMojoHost::ChannelDelegate : public ChannelMojo::Delegate { |
| 18 public: |
| 19 explicit ChannelDelegate(scoped_refptr<base::TaskRunner> io_task_runner); |
| 20 virtual ~ChannelDelegate(); |
| 21 |
| 22 // ChannelMojo::Delegate |
| 23 virtual base::WeakPtr<Delegate> ToWeakPtr() OVERRIDE; |
| 24 virtual void OnChannelCreated(base::WeakPtr<ChannelMojo> channel) OVERRIDE; |
| 25 virtual scoped_refptr<base::TaskRunner> GetIOTaskRunner() OVERRIDE; |
| 26 |
| 27 // Returns an weak ptr of ChannelDelegate instead of Delegate |
| 28 base::WeakPtr<ChannelDelegate> GetWeakPtr(); |
| 29 void OnClientLaunched(base::ProcessHandle process); |
| 30 void DeleteThisSoon(); |
| 31 |
| 32 private: |
| 33 scoped_refptr<base::TaskRunner> io_task_runner_; |
| 34 base::WeakPtrFactory<ChannelDelegate> weak_factory_; |
| 35 base::WeakPtr<ChannelMojo> channel_; |
| 36 |
| 37 DISALLOW_COPY_AND_ASSIGN(ChannelDelegate); |
| 38 }; |
| 39 |
| 40 ChannelMojoHost::ChannelDelegate::ChannelDelegate( |
| 41 scoped_refptr<base::TaskRunner> io_task_runner) |
| 42 : io_task_runner_(io_task_runner), weak_factory_(this) { |
| 43 } |
| 44 |
| 45 ChannelMojoHost::ChannelDelegate::~ChannelDelegate() { |
| 46 } |
| 47 |
| 48 base::WeakPtr<ChannelMojo::Delegate> |
| 49 ChannelMojoHost::ChannelDelegate::ToWeakPtr() { |
| 50 return weak_factory_.GetWeakPtr(); |
| 51 } |
| 52 |
| 53 base::WeakPtr<ChannelMojoHost::ChannelDelegate> |
| 54 ChannelMojoHost::ChannelDelegate::GetWeakPtr() { |
| 55 return weak_factory_.GetWeakPtr(); |
| 56 } |
| 57 |
| 58 void ChannelMojoHost::ChannelDelegate::OnChannelCreated( |
| 59 base::WeakPtr<ChannelMojo> channel) { |
| 60 DCHECK(!channel_); |
| 61 channel_ = channel; |
| 62 } |
| 63 |
| 64 scoped_refptr<base::TaskRunner> |
| 65 ChannelMojoHost::ChannelDelegate::GetIOTaskRunner() { |
| 66 return io_task_runner_; |
| 67 } |
| 68 |
| 69 void ChannelMojoHost::ChannelDelegate::OnClientLaunched( |
| 70 base::ProcessHandle process) { |
| 71 if (channel_) |
| 72 channel_->OnClientLaunched(process); |
| 73 } |
| 74 |
| 75 void ChannelMojoHost::ChannelDelegate::DeleteThisSoon() { |
| 76 io_task_runner_->PostTask( |
| 77 FROM_HERE, |
| 78 base::Bind(&base::DeletePointer<ChannelMojoHost::ChannelDelegate>, |
| 79 base::Unretained(this))); |
| 80 } |
| 81 |
| 82 // |
| 83 // ChannelMojoHost |
| 84 // |
| 85 |
| 86 ChannelMojoHost::ChannelMojoHost(scoped_refptr<base::TaskRunner> io_task_runner) |
| 87 : weak_factory_(this), |
| 88 io_task_runner_(io_task_runner), |
| 89 channel_delegate_(new ChannelDelegate(io_task_runner)) { |
15 } | 90 } |
16 | 91 |
17 ChannelMojoHost::~ChannelMojoHost() { | 92 ChannelMojoHost::~ChannelMojoHost() { |
18 } | 93 } |
19 | 94 |
20 void ChannelMojoHost::OnClientLaunched(base::ProcessHandle process) { | 95 void ChannelMojoHost::OnClientLaunched(base::ProcessHandle process) { |
21 if (task_runner_ == base::MessageLoop::current()->message_loop_proxy()) { | 96 if (io_task_runner_ == base::MessageLoop::current()->message_loop_proxy()) { |
22 InvokeOnClientLaunched(process); | 97 channel_delegate_->OnClientLaunched(process); |
23 } else { | 98 } else { |
24 task_runner_->PostTask(FROM_HERE, | 99 io_task_runner_->PostTask(FROM_HERE, |
25 base::Bind(&ChannelMojoHost::InvokeOnClientLaunched, | 100 base::Bind(&ChannelDelegate::OnClientLaunched, |
26 weak_factory_.GetWeakPtr(), | 101 channel_delegate_->GetWeakPtr(), |
27 process)); | 102 process)); |
28 } | 103 } |
29 } | 104 } |
30 | 105 |
31 void ChannelMojoHost::InvokeOnClientLaunched(base::ProcessHandle process) { | 106 ChannelMojo::Delegate* ChannelMojoHost::channel_delegate() const { |
32 if (!channel_) | 107 return channel_delegate_.get(); |
33 return; | |
34 channel_->OnClientLaunched(process); | |
35 } | 108 } |
36 | 109 |
37 void ChannelMojoHost::OnChannelCreated(ChannelMojo* channel) { | 110 void ChannelMojoHost::DelegateDeleter::operator()( |
38 DCHECK(channel_ == NULL); | 111 ChannelMojoHost::ChannelDelegate* ptr) const { |
39 channel_ = channel; | 112 ptr->DeleteThisSoon(); |
40 } | |
41 | |
42 void ChannelMojoHost::OnChannelDestroyed() { | |
43 DCHECK(channel_ != NULL); | |
44 channel_ = NULL; | |
45 } | 113 } |
46 | 114 |
47 } // namespace IPC | 115 } // namespace IPC |
OLD | NEW |