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 "mojo/edk/system/channel_manager.h" | 5 #include "mojo/edk/system/channel_manager.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "mojo/edk/system/channel.h" | 11 #include "mojo/edk/system/channel.h" |
12 #include "mojo/edk/system/channel_endpoint.h" | 12 #include "mojo/edk/system/channel_endpoint.h" |
13 #include "mojo/edk/system/message_pipe_dispatcher.h" | 13 #include "mojo/edk/system/message_pipe_dispatcher.h" |
14 | 14 |
| 15 using mojo::platform::TaskRunner; |
15 using mojo::util::MakeRefCounted; | 16 using mojo::util::MakeRefCounted; |
16 using mojo::util::MutexLocker; | 17 using mojo::util::MutexLocker; |
17 using mojo::util::RefPtr; | 18 using mojo::util::RefPtr; |
18 | 19 |
19 namespace mojo { | 20 namespace mojo { |
20 namespace system { | 21 namespace system { |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 // TODO(vtl): |channel| and |callback_thread_task_runner| should be rvalue | 25 // TODO(vtl): |channel| and |callback_thread_task_runner| should be rvalue |
25 // references, but that currently doesn't work with base::Bind. | 26 // references, but that currently doesn't work with base::Bind. |
26 void ShutdownChannelHelper( | 27 void ShutdownChannelHelper(RefPtr<Channel> channel, |
27 RefPtr<Channel> channel, | 28 const base::Closure& callback, |
28 const base::Closure& callback, | 29 RefPtr<TaskRunner> callback_thread_task_runner) { |
29 RefPtr<embedder::PlatformTaskRunner> callback_thread_task_runner) { | |
30 channel->Shutdown(); | 30 channel->Shutdown(); |
31 if (callback_thread_task_runner) | 31 if (callback_thread_task_runner) |
32 callback_thread_task_runner->PostTask(callback); | 32 callback_thread_task_runner->PostTask(callback); |
33 else | 33 else |
34 callback.Run(); | 34 callback.Run(); |
35 } | 35 } |
36 | 36 |
37 } // namespace | 37 } // namespace |
38 | 38 |
39 ChannelManager::ChannelManager( | 39 ChannelManager::ChannelManager(embedder::PlatformSupport* platform_support, |
40 embedder::PlatformSupport* platform_support, | 40 RefPtr<TaskRunner>&& io_thread_task_runner, |
41 RefPtr<embedder::PlatformTaskRunner>&& io_thread_task_runner, | 41 ConnectionManager* connection_manager) |
42 ConnectionManager* connection_manager) | |
43 : platform_support_(platform_support), | 42 : platform_support_(platform_support), |
44 io_thread_task_runner_(std::move(io_thread_task_runner)), | 43 io_thread_task_runner_(std::move(io_thread_task_runner)), |
45 connection_manager_(connection_manager) { | 44 connection_manager_(connection_manager) { |
46 DCHECK(platform_support_); | 45 DCHECK(platform_support_); |
47 DCHECK(io_thread_task_runner_); | 46 DCHECK(io_thread_task_runner_); |
48 // (|connection_manager_| may be null.) | 47 // (|connection_manager_| may be null.) |
49 } | 48 } |
50 | 49 |
51 ChannelManager::~ChannelManager() { | 50 ChannelManager::~ChannelManager() { |
52 // |Shutdown()| must be called before destruction and have been completed. | 51 // |Shutdown()| must be called before destruction and have been completed. |
(...skipping 11 matching lines...) Expand all Loading... |
64 MutexLocker locker(&mutex_); | 63 MutexLocker locker(&mutex_); |
65 channels.swap(channels_); | 64 channels.swap(channels_); |
66 } | 65 } |
67 | 66 |
68 for (auto& channel : channels) | 67 for (auto& channel : channels) |
69 channel.second->Shutdown(); | 68 channel.second->Shutdown(); |
70 } | 69 } |
71 | 70 |
72 void ChannelManager::Shutdown( | 71 void ChannelManager::Shutdown( |
73 const base::Closure& callback, | 72 const base::Closure& callback, |
74 RefPtr<embedder::PlatformTaskRunner>&& callback_thread_task_runner) { | 73 RefPtr<TaskRunner>&& callback_thread_task_runner) { |
75 RefPtr<embedder::PlatformTaskRunner> cttr = | 74 RefPtr<TaskRunner> cttr = std::move(callback_thread_task_runner); |
76 std::move(callback_thread_task_runner); | |
77 io_thread_task_runner_->PostTask(base::Bind(&ChannelManager::ShutdownHelper, | 75 io_thread_task_runner_->PostTask(base::Bind(&ChannelManager::ShutdownHelper, |
78 base::Unretained(this), callback, | 76 base::Unretained(this), callback, |
79 base::Passed(&cttr))); | 77 base::Passed(&cttr))); |
80 } | 78 } |
81 | 79 |
82 RefPtr<MessagePipeDispatcher> ChannelManager::CreateChannelOnIOThread( | 80 RefPtr<MessagePipeDispatcher> ChannelManager::CreateChannelOnIOThread( |
83 ChannelId channel_id, | 81 ChannelId channel_id, |
84 embedder::ScopedPlatformHandle platform_handle) { | 82 embedder::ScopedPlatformHandle platform_handle) { |
85 RefPtr<ChannelEndpoint> bootstrap_channel_endpoint; | 83 RefPtr<ChannelEndpoint> bootstrap_channel_endpoint; |
86 auto dispatcher = MessagePipeDispatcher::CreateRemoteMessagePipe( | 84 auto dispatcher = MessagePipeDispatcher::CreateRemoteMessagePipe( |
87 &bootstrap_channel_endpoint); | 85 &bootstrap_channel_endpoint); |
88 CreateChannelOnIOThreadHelper(channel_id, platform_handle.Pass(), | 86 CreateChannelOnIOThreadHelper(channel_id, platform_handle.Pass(), |
89 std::move(bootstrap_channel_endpoint)); | 87 std::move(bootstrap_channel_endpoint)); |
90 return dispatcher; | 88 return dispatcher; |
91 } | 89 } |
92 | 90 |
93 RefPtr<Channel> ChannelManager::CreateChannelWithoutBootstrapOnIOThread( | 91 RefPtr<Channel> ChannelManager::CreateChannelWithoutBootstrapOnIOThread( |
94 ChannelId channel_id, | 92 ChannelId channel_id, |
95 embedder::ScopedPlatformHandle platform_handle) { | 93 embedder::ScopedPlatformHandle platform_handle) { |
96 return CreateChannelOnIOThreadHelper(channel_id, platform_handle.Pass(), | 94 return CreateChannelOnIOThreadHelper(channel_id, platform_handle.Pass(), |
97 nullptr); | 95 nullptr); |
98 } | 96 } |
99 | 97 |
100 RefPtr<MessagePipeDispatcher> ChannelManager::CreateChannel( | 98 RefPtr<MessagePipeDispatcher> ChannelManager::CreateChannel( |
101 ChannelId channel_id, | 99 ChannelId channel_id, |
102 embedder::ScopedPlatformHandle platform_handle, | 100 embedder::ScopedPlatformHandle platform_handle, |
103 const base::Closure& callback, | 101 const base::Closure& callback, |
104 RefPtr<embedder::PlatformTaskRunner>&& callback_thread_task_runner) { | 102 RefPtr<TaskRunner>&& callback_thread_task_runner) { |
105 DCHECK(!callback.is_null()); | 103 DCHECK(!callback.is_null()); |
106 // (|callback_thread_task_runner| may be null.) | 104 // (|callback_thread_task_runner| may be null.) |
107 | 105 |
108 RefPtr<ChannelEndpoint> bootstrap_channel_endpoint; | 106 RefPtr<ChannelEndpoint> bootstrap_channel_endpoint; |
109 auto dispatcher = MessagePipeDispatcher::CreateRemoteMessagePipe( | 107 auto dispatcher = MessagePipeDispatcher::CreateRemoteMessagePipe( |
110 &bootstrap_channel_endpoint); | 108 &bootstrap_channel_endpoint); |
111 // TODO(vtl): This is needed, since |base::Passed()| doesn't work with an | 109 // TODO(vtl): This is needed, since |base::Passed()| doesn't work with an |
112 // rvalue reference. | 110 // rvalue reference. |
113 RefPtr<embedder::PlatformTaskRunner> cttr = | 111 RefPtr<TaskRunner> cttr = std::move(callback_thread_task_runner); |
114 std::move(callback_thread_task_runner); | |
115 io_thread_task_runner_->PostTask(base::Bind( | 112 io_thread_task_runner_->PostTask(base::Bind( |
116 &ChannelManager::CreateChannelHelper, base::Unretained(this), channel_id, | 113 &ChannelManager::CreateChannelHelper, base::Unretained(this), channel_id, |
117 base::Passed(&platform_handle), base::Passed(&bootstrap_channel_endpoint), | 114 base::Passed(&platform_handle), base::Passed(&bootstrap_channel_endpoint), |
118 callback, base::Passed(&cttr))); | 115 callback, base::Passed(&cttr))); |
119 return dispatcher; | 116 return dispatcher; |
120 } | 117 } |
121 | 118 |
122 RefPtr<Channel> ChannelManager::GetChannel(ChannelId channel_id) const { | 119 RefPtr<Channel> ChannelManager::GetChannel(ChannelId channel_id) const { |
123 MutexLocker locker(&mutex_); | 120 MutexLocker locker(&mutex_); |
124 auto it = channels_.find(channel_id); | 121 auto it = channels_.find(channel_id); |
(...skipping 13 matching lines...) Expand all Loading... |
138 DCHECK(it != channels_.end()); | 135 DCHECK(it != channels_.end()); |
139 channel = std::move(it->second); | 136 channel = std::move(it->second); |
140 channels_.erase(it); | 137 channels_.erase(it); |
141 } | 138 } |
142 channel->Shutdown(); | 139 channel->Shutdown(); |
143 } | 140 } |
144 | 141 |
145 void ChannelManager::ShutdownChannel( | 142 void ChannelManager::ShutdownChannel( |
146 ChannelId channel_id, | 143 ChannelId channel_id, |
147 const base::Closure& callback, | 144 const base::Closure& callback, |
148 RefPtr<embedder::PlatformTaskRunner>&& callback_thread_task_runner) { | 145 RefPtr<TaskRunner>&& callback_thread_task_runner) { |
149 RefPtr<Channel> channel; | 146 RefPtr<Channel> channel; |
150 { | 147 { |
151 MutexLocker locker(&mutex_); | 148 MutexLocker locker(&mutex_); |
152 auto it = channels_.find(channel_id); | 149 auto it = channels_.find(channel_id); |
153 DCHECK(it != channels_.end()); | 150 DCHECK(it != channels_.end()); |
154 channel.swap(it->second); | 151 channel.swap(it->second); |
155 channels_.erase(it); | 152 channels_.erase(it); |
156 } | 153 } |
157 channel->WillShutdownSoon(); | 154 channel->WillShutdownSoon(); |
158 // TODO(vtl): This is needed, since |base::Passed()| doesn't work with an | 155 // TODO(vtl): This is needed, since |base::Passed()| doesn't work with an |
159 // rvalue reference. | 156 // rvalue reference. |
160 RefPtr<embedder::PlatformTaskRunner> cttr = | 157 RefPtr<TaskRunner> cttr = std::move(callback_thread_task_runner); |
161 std::move(callback_thread_task_runner); | |
162 io_thread_task_runner_->PostTask(base::Bind(&ShutdownChannelHelper, | 158 io_thread_task_runner_->PostTask(base::Bind(&ShutdownChannelHelper, |
163 base::Passed(&channel), callback, | 159 base::Passed(&channel), callback, |
164 base::Passed(&cttr))); | 160 base::Passed(&cttr))); |
165 } | 161 } |
166 | 162 |
167 void ChannelManager::ShutdownHelper( | 163 void ChannelManager::ShutdownHelper( |
168 const base::Closure& callback, | 164 const base::Closure& callback, |
169 util::RefPtr<embedder::PlatformTaskRunner> callback_thread_task_runner) { | 165 util::RefPtr<TaskRunner> callback_thread_task_runner) { |
170 ShutdownOnIOThread(); | 166 ShutdownOnIOThread(); |
171 if (callback_thread_task_runner) | 167 if (callback_thread_task_runner) |
172 callback_thread_task_runner->PostTask(callback); | 168 callback_thread_task_runner->PostTask(callback); |
173 else | 169 else |
174 callback.Run(); | 170 callback.Run(); |
175 } | 171 } |
176 | 172 |
177 RefPtr<Channel> ChannelManager::CreateChannelOnIOThreadHelper( | 173 RefPtr<Channel> ChannelManager::CreateChannelOnIOThreadHelper( |
178 ChannelId channel_id, | 174 ChannelId channel_id, |
179 embedder::ScopedPlatformHandle platform_handle, | 175 embedder::ScopedPlatformHandle platform_handle, |
(...skipping 14 matching lines...) Expand all Loading... |
194 } | 190 } |
195 channel->SetChannelManager(this); | 191 channel->SetChannelManager(this); |
196 return channel; | 192 return channel; |
197 } | 193 } |
198 | 194 |
199 void ChannelManager::CreateChannelHelper( | 195 void ChannelManager::CreateChannelHelper( |
200 ChannelId channel_id, | 196 ChannelId channel_id, |
201 embedder::ScopedPlatformHandle platform_handle, | 197 embedder::ScopedPlatformHandle platform_handle, |
202 RefPtr<ChannelEndpoint> bootstrap_channel_endpoint, | 198 RefPtr<ChannelEndpoint> bootstrap_channel_endpoint, |
203 const base::Closure& callback, | 199 const base::Closure& callback, |
204 RefPtr<embedder::PlatformTaskRunner> callback_thread_task_runner) { | 200 RefPtr<TaskRunner> callback_thread_task_runner) { |
205 CreateChannelOnIOThreadHelper(channel_id, platform_handle.Pass(), | 201 CreateChannelOnIOThreadHelper(channel_id, platform_handle.Pass(), |
206 std::move(bootstrap_channel_endpoint)); | 202 std::move(bootstrap_channel_endpoint)); |
207 if (callback_thread_task_runner) | 203 if (callback_thread_task_runner) |
208 callback_thread_task_runner->PostTask(callback); | 204 callback_thread_task_runner->PostTask(callback); |
209 else | 205 else |
210 callback.Run(); | 206 callback.Run(); |
211 } | 207 } |
212 | 208 |
213 } // namespace system | 209 } // namespace system |
214 } // namespace mojo | 210 } // namespace mojo |
OLD | NEW |