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
3 // found in the LICENSE file.
5 #include "ipc/mojo/ipc_channel_mojo_host.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "ipc/mojo/ipc_channel_mojo.h"
14 class ChannelMojoHost::ChannelDelegateTraits
{
16 static void Destruct(const ChannelMojoHost::ChannelDelegate
* ptr
);
19 // The delete class lives on the IO thread to talk to ChannelMojo on
20 // behalf of ChannelMojoHost.
22 // The object must be touched only on the IO thread.
23 class ChannelMojoHost::ChannelDelegate
24 : public base::RefCountedThreadSafe
<ChannelMojoHost::ChannelDelegate
,
25 ChannelMojoHost::ChannelDelegateTraits
>,
26 public ChannelMojo::Delegate
{
28 explicit ChannelDelegate(
29 scoped_refptr
<base::SequencedTaskRunner
> io_task_runner
);
31 // ChannelMojo::Delegate
32 base::WeakPtr
<Delegate
> ToWeakPtr() override
;
33 void OnChannelCreated(base::WeakPtr
<ChannelMojo
> channel
) override
;
35 // Returns an weak ptr of ChannelDelegate instead of Delegate
36 base::WeakPtr
<ChannelDelegate
> GetWeakPtr();
37 void OnClientLaunched(base::ProcessHandle process
);
38 void DeleteThisSoon() const;
41 friend class base::DeleteHelper
<ChannelDelegate
>;
43 ~ChannelDelegate() override
;
45 scoped_refptr
<base::SequencedTaskRunner
> io_task_runner_
;
46 base::WeakPtr
<ChannelMojo
> channel_
;
47 base::WeakPtrFactory
<ChannelDelegate
> weak_factory_
;
49 DISALLOW_COPY_AND_ASSIGN(ChannelDelegate
);
52 ChannelMojoHost::ChannelDelegate::ChannelDelegate(
53 scoped_refptr
<base::SequencedTaskRunner
> io_task_runner
)
54 : io_task_runner_(io_task_runner
), weak_factory_(this) {
57 ChannelMojoHost::ChannelDelegate::~ChannelDelegate() {
60 base::WeakPtr
<ChannelMojo::Delegate
>
61 ChannelMojoHost::ChannelDelegate::ToWeakPtr() {
62 return weak_factory_
.GetWeakPtr();
65 base::WeakPtr
<ChannelMojoHost::ChannelDelegate
>
66 ChannelMojoHost::ChannelDelegate::GetWeakPtr() {
67 return weak_factory_
.GetWeakPtr();
70 void ChannelMojoHost::ChannelDelegate::OnChannelCreated(
71 base::WeakPtr
<ChannelMojo
> channel
) {
76 void ChannelMojoHost::ChannelDelegate::OnClientLaunched(
77 base::ProcessHandle process
) {
79 channel_
->OnClientLaunched(process
);
82 void ChannelMojoHost::ChannelDelegate::DeleteThisSoon() const {
83 io_task_runner_
->DeleteSoon(FROM_HERE
, this);
90 ChannelMojoHost::ChannelMojoHost(
91 scoped_refptr
<base::SequencedTaskRunner
> io_task_runner
)
92 : io_task_runner_(io_task_runner
),
93 channel_delegate_(new ChannelDelegate(io_task_runner
)),
97 ChannelMojoHost::~ChannelMojoHost() {
100 void ChannelMojoHost::OnClientLaunched(base::ProcessHandle process
) {
101 if (io_task_runner_
== base::MessageLoop::current()->task_runner()) {
102 channel_delegate_
->OnClientLaunched(process
);
104 io_task_runner_
->PostTask(FROM_HERE
,
105 base::Bind(&ChannelDelegate::OnClientLaunched
,
106 channel_delegate_
, process
));
110 ChannelMojo::Delegate
* ChannelMojoHost::channel_delegate() const {
111 return channel_delegate_
.get();
115 void ChannelMojoHost::ChannelDelegateTraits::Destruct(
116 const ChannelMojoHost::ChannelDelegate
* ptr
) {
117 ptr
->DeleteThisSoon();