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 class ChannelMojoHost::ChannelDelegateTraits { |
| 14 public: |
| 15 static void Destruct(const ChannelMojoHost::ChannelDelegate* ptr); |
| 16 }; |
| 17 |
13 // The delete class lives on the IO thread to talk to ChannelMojo on | 18 // The delete class lives on the IO thread to talk to ChannelMojo on |
14 // behalf of ChannelMojoHost. | 19 // behalf of ChannelMojoHost. |
15 // | 20 // |
16 // The object must be touched only on the IO thread. | 21 // The object must be touched only on the IO thread. |
17 class ChannelMojoHost::ChannelDelegate : public ChannelMojo::Delegate { | 22 class ChannelMojoHost::ChannelDelegate |
| 23 : public base::RefCountedThreadSafe<ChannelMojoHost::ChannelDelegate, |
| 24 ChannelMojoHost::ChannelDelegateTraits>, |
| 25 public ChannelMojo::Delegate { |
18 public: | 26 public: |
19 explicit ChannelDelegate(scoped_refptr<base::TaskRunner> io_task_runner); | 27 explicit ChannelDelegate( |
20 ~ChannelDelegate() override; | 28 scoped_refptr<base::SequencedTaskRunner> io_task_runner); |
21 | 29 |
22 // ChannelMojo::Delegate | 30 // ChannelMojo::Delegate |
23 base::WeakPtr<Delegate> ToWeakPtr() override; | 31 base::WeakPtr<Delegate> ToWeakPtr() override; |
24 void OnChannelCreated(base::WeakPtr<ChannelMojo> channel) override; | 32 void OnChannelCreated(base::WeakPtr<ChannelMojo> channel) override; |
25 scoped_refptr<base::TaskRunner> GetIOTaskRunner() override; | 33 scoped_refptr<base::TaskRunner> GetIOTaskRunner() override; |
26 | 34 |
27 // Returns an weak ptr of ChannelDelegate instead of Delegate | 35 // Returns an weak ptr of ChannelDelegate instead of Delegate |
28 base::WeakPtr<ChannelDelegate> GetWeakPtr(); | 36 base::WeakPtr<ChannelDelegate> GetWeakPtr(); |
29 void OnClientLaunched(base::ProcessHandle process); | 37 void OnClientLaunched(base::ProcessHandle process); |
30 void DeleteThisSoon(); | 38 void DeleteThisSoon() const; |
31 | 39 |
32 private: | 40 private: |
33 scoped_refptr<base::TaskRunner> io_task_runner_; | 41 friend class base::DeleteHelper<ChannelDelegate>; |
| 42 |
| 43 ~ChannelDelegate() override; |
| 44 |
| 45 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; |
34 base::WeakPtr<ChannelMojo> channel_; | 46 base::WeakPtr<ChannelMojo> channel_; |
35 base::WeakPtrFactory<ChannelDelegate> weak_factory_; | 47 base::WeakPtrFactory<ChannelDelegate> weak_factory_; |
36 | 48 |
37 DISALLOW_COPY_AND_ASSIGN(ChannelDelegate); | 49 DISALLOW_COPY_AND_ASSIGN(ChannelDelegate); |
38 }; | 50 }; |
39 | 51 |
40 ChannelMojoHost::ChannelDelegate::ChannelDelegate( | 52 ChannelMojoHost::ChannelDelegate::ChannelDelegate( |
41 scoped_refptr<base::TaskRunner> io_task_runner) | 53 scoped_refptr<base::SequencedTaskRunner> io_task_runner) |
42 : io_task_runner_(io_task_runner), weak_factory_(this) { | 54 : io_task_runner_(io_task_runner), weak_factory_(this) { |
43 } | 55 } |
44 | 56 |
45 ChannelMojoHost::ChannelDelegate::~ChannelDelegate() { | 57 ChannelMojoHost::ChannelDelegate::~ChannelDelegate() { |
46 } | 58 } |
47 | 59 |
48 base::WeakPtr<ChannelMojo::Delegate> | 60 base::WeakPtr<ChannelMojo::Delegate> |
49 ChannelMojoHost::ChannelDelegate::ToWeakPtr() { | 61 ChannelMojoHost::ChannelDelegate::ToWeakPtr() { |
50 return weak_factory_.GetWeakPtr(); | 62 return weak_factory_.GetWeakPtr(); |
51 } | 63 } |
(...skipping 13 matching lines...) Expand all Loading... |
65 ChannelMojoHost::ChannelDelegate::GetIOTaskRunner() { | 77 ChannelMojoHost::ChannelDelegate::GetIOTaskRunner() { |
66 return io_task_runner_; | 78 return io_task_runner_; |
67 } | 79 } |
68 | 80 |
69 void ChannelMojoHost::ChannelDelegate::OnClientLaunched( | 81 void ChannelMojoHost::ChannelDelegate::OnClientLaunched( |
70 base::ProcessHandle process) { | 82 base::ProcessHandle process) { |
71 if (channel_) | 83 if (channel_) |
72 channel_->OnClientLaunched(process); | 84 channel_->OnClientLaunched(process); |
73 } | 85 } |
74 | 86 |
75 void ChannelMojoHost::ChannelDelegate::DeleteThisSoon() { | 87 void ChannelMojoHost::ChannelDelegate::DeleteThisSoon() const { |
76 io_task_runner_->PostTask( | 88 io_task_runner_->DeleteSoon(FROM_HERE, this); |
77 FROM_HERE, | |
78 base::Bind(&base::DeletePointer<ChannelMojoHost::ChannelDelegate>, | |
79 base::Unretained(this))); | |
80 } | 89 } |
81 | 90 |
82 // | 91 // |
83 // ChannelMojoHost | 92 // ChannelMojoHost |
84 // | 93 // |
85 | 94 |
86 ChannelMojoHost::ChannelMojoHost(scoped_refptr<base::TaskRunner> io_task_runner) | 95 ChannelMojoHost::ChannelMojoHost( |
| 96 scoped_refptr<base::SequencedTaskRunner> io_task_runner) |
87 : io_task_runner_(io_task_runner), | 97 : io_task_runner_(io_task_runner), |
88 channel_delegate_(new ChannelDelegate(io_task_runner)), | 98 channel_delegate_(new ChannelDelegate(io_task_runner)), |
89 weak_factory_(this) { | 99 weak_factory_(this) { |
90 } | 100 } |
91 | 101 |
92 ChannelMojoHost::~ChannelMojoHost() { | 102 ChannelMojoHost::~ChannelMojoHost() { |
93 } | 103 } |
94 | 104 |
95 void ChannelMojoHost::OnClientLaunched(base::ProcessHandle process) { | 105 void ChannelMojoHost::OnClientLaunched(base::ProcessHandle process) { |
96 if (io_task_runner_ == base::MessageLoop::current()->message_loop_proxy()) { | 106 if (io_task_runner_ == base::MessageLoop::current()->message_loop_proxy()) { |
97 channel_delegate_->OnClientLaunched(process); | 107 channel_delegate_->OnClientLaunched(process); |
98 } else { | 108 } else { |
99 io_task_runner_->PostTask(FROM_HERE, | 109 io_task_runner_->PostTask(FROM_HERE, |
100 base::Bind(&ChannelDelegate::OnClientLaunched, | 110 base::Bind(&ChannelDelegate::OnClientLaunched, |
101 channel_delegate_->GetWeakPtr(), | 111 channel_delegate_, process)); |
102 process)); | |
103 } | 112 } |
104 } | 113 } |
105 | 114 |
106 ChannelMojo::Delegate* ChannelMojoHost::channel_delegate() const { | 115 ChannelMojo::Delegate* ChannelMojoHost::channel_delegate() const { |
107 return channel_delegate_.get(); | 116 return channel_delegate_.get(); |
108 } | 117 } |
109 | 118 |
110 void ChannelMojoHost::DelegateDeleter::operator()( | 119 // static |
111 ChannelMojoHost::ChannelDelegate* ptr) const { | 120 void ChannelMojoHost::ChannelDelegateTraits::Destruct( |
| 121 const ChannelMojoHost::ChannelDelegate* ptr) { |
112 ptr->DeleteThisSoon(); | 122 ptr->DeleteThisSoon(); |
113 } | 123 } |
114 | 124 |
115 } // namespace IPC | 125 } // namespace IPC |
OLD | NEW |