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 in IO thread to talk to ChannelMojo on |
viettrungluu
2014/09/25 22:09:07
"delete" -> "delegate"
"in" -> "on the"
Hajime Morrita
2014/09/25 22:15:12
Done.
| |
14 : weak_factory_(this), task_runner_(task_runner), channel_(NULL) { | 14 // behalf of ChannelMojoHost. |
15 // | |
16 // The object must be touched only in IO thread. | |
viettrungluu
2014/09/25 22:09:07
"in" -> "on the"
Hajime Morrita
2014/09/25 22:15:12
Done.
| |
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 | |
26 // Returns an weak ptr of ChannelDelegate instead of Delegate | |
27 base::WeakPtr<ChannelDelegate> GetWeakPtr(); | |
28 void OnClientLaunched(base::ProcessHandle process); | |
29 void DeleteThisSoon(); | |
30 | |
31 private: | |
32 scoped_refptr<base::TaskRunner> io_task_runner_; | |
33 base::WeakPtrFactory<ChannelDelegate> weak_factory_; | |
34 base::WeakPtr<ChannelMojo> channel_; | |
35 | |
36 DISALLOW_COPY_AND_ASSIGN(ChannelDelegate); | |
37 }; | |
38 | |
39 ChannelMojoHost::ChannelDelegate::ChannelDelegate( | |
40 scoped_refptr<base::TaskRunner> io_task_runner) | |
41 : io_task_runner_(io_task_runner), weak_factory_(this) { | |
42 } | |
43 | |
44 ChannelMojoHost::ChannelDelegate::~ChannelDelegate() { | |
45 DCHECK_EQ(io_task_runner_, | |
46 base::MessageLoop::current()->message_loop_proxy()); | |
47 } | |
48 | |
49 base::WeakPtr<ChannelMojo::Delegate> | |
50 ChannelMojoHost::ChannelDelegate::ToWeakPtr() { | |
51 return weak_factory_.GetWeakPtr(); | |
52 } | |
53 | |
54 base::WeakPtr<ChannelMojoHost::ChannelDelegate> | |
55 ChannelMojoHost::ChannelDelegate::GetWeakPtr() { | |
56 return weak_factory_.GetWeakPtr(); | |
57 } | |
58 | |
59 void ChannelMojoHost::ChannelDelegate::OnChannelCreated( | |
60 base::WeakPtr<ChannelMojo> channel) { | |
61 DCHECK(!channel_); | |
62 channel_ = channel; | |
63 } | |
64 | |
65 void ChannelMojoHost::ChannelDelegate::OnClientLaunched( | |
66 base::ProcessHandle process) { | |
67 if (channel_) | |
68 channel_->OnClientLaunched(process); | |
69 } | |
70 | |
71 void ChannelMojoHost::ChannelDelegate::DeleteThisSoon() { | |
72 io_task_runner_->PostTask( | |
73 FROM_HERE, | |
74 base::Bind(&base::DeletePointer<ChannelMojoHost::ChannelDelegate>, | |
75 base::Unretained(this))); | |
76 } | |
77 | |
78 // | |
79 // ChannelMojoHost | |
80 // | |
81 | |
82 ChannelMojoHost::ChannelMojoHost(scoped_refptr<base::TaskRunner> io_task_runner) | |
83 : weak_factory_(this), | |
84 io_task_runner_(io_task_runner), | |
85 channel_delegate_(new ChannelDelegate(io_task_runner)) { | |
15 } | 86 } |
16 | 87 |
17 ChannelMojoHost::~ChannelMojoHost() { | 88 ChannelMojoHost::~ChannelMojoHost() { |
18 } | 89 } |
19 | 90 |
20 void ChannelMojoHost::OnClientLaunched(base::ProcessHandle process) { | 91 void ChannelMojoHost::OnClientLaunched(base::ProcessHandle process) { |
21 if (task_runner_ == base::MessageLoop::current()->message_loop_proxy()) { | 92 if (io_task_runner_ == base::MessageLoop::current()->message_loop_proxy()) { |
22 InvokeOnClientLaunched(process); | 93 channel_delegate_->OnClientLaunched(process); |
23 } else { | 94 } else { |
24 task_runner_->PostTask(FROM_HERE, | 95 io_task_runner_->PostTask(FROM_HERE, |
25 base::Bind(&ChannelMojoHost::InvokeOnClientLaunched, | 96 base::Bind(&ChannelDelegate::OnClientLaunched, |
26 weak_factory_.GetWeakPtr(), | 97 channel_delegate_->GetWeakPtr(), |
27 process)); | 98 process)); |
28 } | 99 } |
29 } | 100 } |
30 | 101 |
31 void ChannelMojoHost::InvokeOnClientLaunched(base::ProcessHandle process) { | 102 ChannelMojo::Delegate* ChannelMojoHost::channel_delegate() const { |
32 if (!channel_) | 103 return channel_delegate_.get(); |
33 return; | |
34 channel_->OnClientLaunched(process); | |
35 } | 104 } |
36 | 105 |
37 void ChannelMojoHost::OnChannelCreated(ChannelMojo* channel) { | 106 void ChannelMojoHost::DelegateDeleter::operator()( |
38 DCHECK(channel_ == NULL); | 107 ChannelMojoHost::ChannelDelegate* ptr) const { |
39 channel_ = channel; | 108 ptr->DeleteThisSoon(); |
40 } | |
41 | |
42 void ChannelMojoHost::OnChannelDestroyed() { | |
43 DCHECK(channel_ != NULL); | |
44 channel_ = NULL; | |
45 } | 109 } |
46 | 110 |
47 } // namespace IPC | 111 } // namespace IPC |
OLD | NEW |