morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 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. |
| 4 | |
amistry | d4aa70d | 2016-06-23 07:52:37 | [diff] [blame] | 5 | #include "ipc/ipc_mojo_bootstrap.h" |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 6 | |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 7 | #include <inttypes.h> |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 8 | #include <stdint.h> |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 9 | |
| 10 | #include <map> |
| 11 | #include <memory> |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 12 | #include <set> |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 13 | #include <utility> |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 14 | #include <vector> |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 15 | |
rockot | a21316a | 2016-06-19 17:08:36 | [diff] [blame] | 16 | #include "base/callback.h" |
Brett Wilson | a62d9c0 | 2017-09-20 20:53:20 | [diff] [blame] | 17 | #include "base/containers/queue.h" |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 18 | #include "base/logging.h" |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 19 | #include "base/macros.h" |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 20 | #include "base/memory/ptr_util.h" |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 21 | #include "base/no_destructor.h" |
Gabriel Charette | 1452023 | 2018-04-30 23:27:22 | [diff] [blame] | 22 | #include "base/sequenced_task_runner.h" |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 23 | #include "base/single_thread_task_runner.h" |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 24 | #include "base/strings/stringprintf.h" |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 25 | #include "base/synchronization/lock.h" |
Sam McNally | de5ae67 | 2017-06-19 23:34:45 | [diff] [blame] | 26 | #include "base/threading/thread_checker.h" |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 27 | #include "base/threading/thread_task_runner_handle.h" |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 28 | #include "base/trace_event/memory_allocator_dump.h" |
| 29 | #include "base/trace_event/memory_dump_manager.h" |
| 30 | #include "base/trace_event/memory_dump_provider.h" |
Ken Rockot | fb81dc0 | 2018-05-15 21:59:26 | [diff] [blame] | 31 | #include "ipc/ipc_channel.h" |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 32 | #include "mojo/public/cpp/bindings/associated_group.h" |
| 33 | #include "mojo/public/cpp/bindings/associated_group_controller.h" |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 34 | #include "mojo/public/cpp/bindings/connector.h" |
| 35 | #include "mojo/public/cpp/bindings/interface_endpoint_client.h" |
| 36 | #include "mojo/public/cpp/bindings/interface_endpoint_controller.h" |
| 37 | #include "mojo/public/cpp/bindings/interface_id.h" |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 38 | #include "mojo/public/cpp/bindings/message.h" |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 39 | #include "mojo/public/cpp/bindings/message_header_validator.h" |
| 40 | #include "mojo/public/cpp/bindings/pipe_control_message_handler.h" |
| 41 | #include "mojo/public/cpp/bindings/pipe_control_message_handler_delegate.h" |
| 42 | #include "mojo/public/cpp/bindings/pipe_control_message_proxy.h" |
Ken Rockot | aa20dcc | 2018-03-28 03:06:51 | [diff] [blame] | 43 | #include "mojo/public/cpp/bindings/sequence_local_sync_event_watcher.h" |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 44 | |
| 45 | namespace IPC { |
| 46 | |
| 47 | namespace { |
| 48 | |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 49 | class ChannelAssociatedGroupController; |
| 50 | |
| 51 | // Used to track some internal Channel state in pursuit of message leaks. |
| 52 | // |
| 53 | // TODO(https://crbug.com/813045): Remove this. |
| 54 | class ControllerMemoryDumpProvider |
| 55 | : public base::trace_event::MemoryDumpProvider { |
| 56 | public: |
| 57 | ControllerMemoryDumpProvider() { |
| 58 | base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
| 59 | this, "IPCChannel", nullptr); |
| 60 | } |
| 61 | |
| 62 | ~ControllerMemoryDumpProvider() override { |
| 63 | base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( |
| 64 | this); |
| 65 | } |
| 66 | |
| 67 | void AddController(ChannelAssociatedGroupController* controller) { |
| 68 | base::AutoLock lock(lock_); |
| 69 | controllers_.insert(controller); |
| 70 | } |
| 71 | |
| 72 | void RemoveController(ChannelAssociatedGroupController* controller) { |
| 73 | base::AutoLock lock(lock_); |
| 74 | controllers_.erase(controller); |
| 75 | } |
| 76 | |
| 77 | // base::trace_event::MemoryDumpProvider: |
| 78 | bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args, |
| 79 | base::trace_event::ProcessMemoryDump* pmd) override; |
| 80 | |
| 81 | private: |
| 82 | base::Lock lock_; |
| 83 | std::set<ChannelAssociatedGroupController*> controllers_; |
| 84 | |
| 85 | DISALLOW_COPY_AND_ASSIGN(ControllerMemoryDumpProvider); |
| 86 | }; |
| 87 | |
| 88 | ControllerMemoryDumpProvider& GetMemoryDumpProvider() { |
| 89 | static base::NoDestructor<ControllerMemoryDumpProvider> provider; |
| 90 | return *provider; |
| 91 | } |
| 92 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 93 | class ChannelAssociatedGroupController |
| 94 | : public mojo::AssociatedGroupController, |
| 95 | public mojo::MessageReceiver, |
| 96 | public mojo::PipeControlMessageHandlerDelegate { |
| 97 | public: |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 98 | ChannelAssociatedGroupController( |
| 99 | bool set_interface_id_namespace_bit, |
Hajime Hoshi | a98f110 | 2017-11-20 06:34:35 | [diff] [blame] | 100 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
| 101 | const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner) |
rockot | b01ef6a | 2016-07-27 03:24:32 | [diff] [blame] | 102 | : task_runner_(ipc_task_runner), |
Hajime Hoshi | a98f110 | 2017-11-20 06:34:35 | [diff] [blame] | 103 | proxy_task_runner_(proxy_task_runner), |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 104 | set_interface_id_namespace_bit_(set_interface_id_namespace_bit), |
rockot | 222e7dd | 2016-08-24 23:37:11 | [diff] [blame] | 105 | filters_(this), |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 106 | control_message_handler_(this), |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 107 | control_message_proxy_thunk_(this), |
| 108 | control_message_proxy_(&control_message_proxy_thunk_) { |
| 109 | thread_checker_.DetachFromThread(); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 110 | control_message_handler_.SetDescription( |
| 111 | "IPC::mojom::Bootstrap [master] PipeControlMessageHandler"); |
rockot | 222e7dd | 2016-08-24 23:37:11 | [diff] [blame] | 112 | filters_.Append<mojo::MessageHeaderValidator>( |
| 113 | "IPC::mojom::Bootstrap [master] MessageHeaderValidator"); |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 114 | |
| 115 | GetMemoryDumpProvider().AddController(this); |
| 116 | } |
| 117 | |
| 118 | size_t GetQueuedMessageCount() { |
| 119 | base::AutoLock lock(outgoing_messages_lock_); |
| 120 | return outgoing_messages_.size(); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 121 | } |
| 122 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 123 | void Bind(mojo::ScopedMessagePipeHandle handle) { |
| 124 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 125 | DCHECK(task_runner_->BelongsToCurrentThread()); |
rockot | 9098435 | 2016-07-25 17:36:19 | [diff] [blame] | 126 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 127 | connector_.reset(new mojo::Connector( |
| 128 | std::move(handle), mojo::Connector::SINGLE_THREADED_SEND, |
| 129 | task_runner_)); |
rockot | 222e7dd | 2016-08-24 23:37:11 | [diff] [blame] | 130 | connector_->set_incoming_receiver(&filters_); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 131 | connector_->set_connection_error_handler( |
| 132 | base::Bind(&ChannelAssociatedGroupController::OnPipeError, |
| 133 | base::Unretained(this))); |
jcivelli | 2207af1 | 2017-01-26 20:46:00 | [diff] [blame] | 134 | connector_->SetWatcherHeapProfilerTag("IPC Channel"); |
rockot | 401fb2c | 2016-09-06 18:35:57 | [diff] [blame] | 135 | } |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 136 | |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 137 | void Pause() { |
| 138 | DCHECK(!paused_); |
| 139 | paused_ = true; |
| 140 | } |
| 141 | |
| 142 | void Unpause() { |
| 143 | DCHECK(paused_); |
| 144 | paused_ = false; |
rockot | 401fb2c | 2016-09-06 18:35:57 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void FlushOutgoingMessages() { |
rockot | c4cc691e | 2016-08-19 18:48:57 | [diff] [blame] | 148 | std::vector<mojo::Message> outgoing_messages; |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 149 | { |
| 150 | base::AutoLock lock(outgoing_messages_lock_); |
| 151 | std::swap(outgoing_messages, outgoing_messages_); |
| 152 | } |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 153 | for (auto& message : outgoing_messages) |
rockot | c4cc691e | 2016-08-19 18:48:57 | [diff] [blame] | 154 | SendMessage(&message); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void CreateChannelEndpoints(mojom::ChannelAssociatedPtr* sender, |
| 158 | mojom::ChannelAssociatedRequest* receiver) { |
| 159 | mojo::InterfaceId sender_id, receiver_id; |
| 160 | if (set_interface_id_namespace_bit_) { |
| 161 | sender_id = 1 | mojo::kInterfaceIdNamespaceMask; |
| 162 | receiver_id = 1; |
| 163 | } else { |
| 164 | sender_id = 1; |
| 165 | receiver_id = 1 | mojo::kInterfaceIdNamespaceMask; |
| 166 | } |
| 167 | |
| 168 | { |
| 169 | base::AutoLock locker(lock_); |
| 170 | Endpoint* sender_endpoint = new Endpoint(this, sender_id); |
| 171 | Endpoint* receiver_endpoint = new Endpoint(this, receiver_id); |
| 172 | endpoints_.insert({ sender_id, sender_endpoint }); |
| 173 | endpoints_.insert({ receiver_id, receiver_endpoint }); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 174 | sender_endpoint->set_handle_created(); |
| 175 | receiver_endpoint->set_handle_created(); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | mojo::ScopedInterfaceEndpointHandle sender_handle = |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 179 | CreateScopedInterfaceEndpointHandle(sender_id); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 180 | mojo::ScopedInterfaceEndpointHandle receiver_handle = |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 181 | CreateScopedInterfaceEndpointHandle(receiver_id); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 182 | |
| 183 | sender->Bind(mojom::ChannelAssociatedPtrInfo(std::move(sender_handle), 0)); |
Ken Rockot | 96d1b7b5 | 2017-05-13 00:29:21 | [diff] [blame] | 184 | *receiver = mojom::ChannelAssociatedRequest(std::move(receiver_handle)); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 185 | } |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 186 | |
| 187 | void ShutDown() { |
| 188 | DCHECK(thread_checker_.CalledOnValidThread()); |
Ken Rockot | 3e7284bb | 2018-02-06 16:11:16 | [diff] [blame] | 189 | shut_down_ = true; |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 190 | connector_->CloseMessagePipe(); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 191 | OnPipeError(); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 192 | connector_.reset(); |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 193 | |
| 194 | base::AutoLock lock(outgoing_messages_lock_); |
Ken Rockot | 3e7284bb | 2018-02-06 16:11:16 | [diff] [blame] | 195 | outgoing_messages_.clear(); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // mojo::AssociatedGroupController: |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 199 | mojo::InterfaceId AssociateInterface( |
| 200 | mojo::ScopedInterfaceEndpointHandle handle_to_send) override { |
| 201 | if (!handle_to_send.pending_association()) |
| 202 | return mojo::kInvalidInterfaceId; |
| 203 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 204 | uint32_t id = 0; |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 205 | { |
| 206 | base::AutoLock locker(lock_); |
| 207 | do { |
| 208 | if (next_interface_id_ >= mojo::kInterfaceIdNamespaceMask) |
| 209 | next_interface_id_ = 2; |
| 210 | id = next_interface_id_++; |
| 211 | if (set_interface_id_namespace_bit_) |
| 212 | id |= mojo::kInterfaceIdNamespaceMask; |
| 213 | } while (ContainsKey(endpoints_, id)); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 214 | |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 215 | Endpoint* endpoint = new Endpoint(this, id); |
| 216 | if (encountered_error_) |
| 217 | endpoint->set_peer_closed(); |
| 218 | endpoint->set_handle_created(); |
| 219 | endpoints_.insert({id, endpoint}); |
| 220 | } |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 221 | |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 222 | if (!NotifyAssociation(&handle_to_send, id)) { |
| 223 | // The peer handle of |handle_to_send|, which is supposed to join this |
| 224 | // associated group, has been closed. |
| 225 | { |
| 226 | base::AutoLock locker(lock_); |
| 227 | Endpoint* endpoint = FindEndpoint(id); |
| 228 | if (endpoint) |
| 229 | MarkClosedAndMaybeRemove(endpoint); |
| 230 | } |
| 231 | |
| 232 | control_message_proxy_.NotifyPeerEndpointClosed( |
| 233 | id, handle_to_send.disconnect_reason()); |
| 234 | } |
| 235 | return id; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | mojo::ScopedInterfaceEndpointHandle CreateLocalEndpointHandle( |
| 239 | mojo::InterfaceId id) override { |
| 240 | if (!mojo::IsValidInterfaceId(id)) |
| 241 | return mojo::ScopedInterfaceEndpointHandle(); |
| 242 | |
Yuzhu Shen | 9f87fb0 | 2017-08-11 17:07:06 | [diff] [blame] | 243 | // Unless it is the master ID, |id| is from the remote side and therefore |
| 244 | // its namespace bit is supposed to be different than the value that this |
| 245 | // router would use. |
| 246 | if (!mojo::IsMasterInterfaceId(id) && |
| 247 | set_interface_id_namespace_bit_ == |
| 248 | mojo::HasInterfaceIdNamespaceBitSet(id)) { |
| 249 | return mojo::ScopedInterfaceEndpointHandle(); |
| 250 | } |
| 251 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 252 | base::AutoLock locker(lock_); |
| 253 | bool inserted = false; |
| 254 | Endpoint* endpoint = FindOrInsertEndpoint(id, &inserted); |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 255 | if (inserted) { |
| 256 | DCHECK(!endpoint->handle_created()); |
| 257 | if (encountered_error_) |
| 258 | endpoint->set_peer_closed(); |
| 259 | } else { |
| 260 | if (endpoint->handle_created()) |
| 261 | return mojo::ScopedInterfaceEndpointHandle(); |
| 262 | } |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 263 | |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 264 | endpoint->set_handle_created(); |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 265 | return CreateScopedInterfaceEndpointHandle(id); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 266 | } |
| 267 | |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 268 | void CloseEndpointHandle( |
| 269 | mojo::InterfaceId id, |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 270 | const base::Optional<mojo::DisconnectReason>& reason) override { |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 271 | if (!mojo::IsValidInterfaceId(id)) |
| 272 | return; |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 273 | { |
| 274 | base::AutoLock locker(lock_); |
Yuzhu Shen | 7bcd8ebf | 2017-10-02 23:21:14 | [diff] [blame] | 275 | DCHECK(ContainsKey(endpoints_, id)); |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 276 | Endpoint* endpoint = endpoints_[id].get(); |
| 277 | DCHECK(!endpoint->client()); |
| 278 | DCHECK(!endpoint->closed()); |
| 279 | MarkClosedAndMaybeRemove(endpoint); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 280 | } |
| 281 | |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 282 | if (!mojo::IsMasterInterfaceId(id) || reason) |
| 283 | control_message_proxy_.NotifyPeerEndpointClosed(id, reason); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | mojo::InterfaceEndpointController* AttachEndpointClient( |
| 287 | const mojo::ScopedInterfaceEndpointHandle& handle, |
| 288 | mojo::InterfaceEndpointClient* client, |
Sam McNally | de5ae67 | 2017-06-19 23:34:45 | [diff] [blame] | 289 | scoped_refptr<base::SequencedTaskRunner> runner) override { |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 290 | const mojo::InterfaceId id = handle.id(); |
| 291 | |
| 292 | DCHECK(mojo::IsValidInterfaceId(id)); |
| 293 | DCHECK(client); |
| 294 | |
| 295 | base::AutoLock locker(lock_); |
Yuzhu Shen | 7bcd8ebf | 2017-10-02 23:21:14 | [diff] [blame] | 296 | DCHECK(ContainsKey(endpoints_, id)); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 297 | |
| 298 | Endpoint* endpoint = endpoints_[id].get(); |
| 299 | endpoint->AttachClient(client, std::move(runner)); |
| 300 | |
| 301 | if (endpoint->peer_closed()) |
| 302 | NotifyEndpointOfError(endpoint, true /* force_async */); |
| 303 | |
| 304 | return endpoint; |
| 305 | } |
| 306 | |
| 307 | void DetachEndpointClient( |
| 308 | const mojo::ScopedInterfaceEndpointHandle& handle) override { |
| 309 | const mojo::InterfaceId id = handle.id(); |
| 310 | |
| 311 | DCHECK(mojo::IsValidInterfaceId(id)); |
| 312 | |
| 313 | base::AutoLock locker(lock_); |
Yuzhu Shen | 7bcd8ebf | 2017-10-02 23:21:14 | [diff] [blame] | 314 | DCHECK(ContainsKey(endpoints_, id)); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 315 | |
| 316 | Endpoint* endpoint = endpoints_[id].get(); |
| 317 | endpoint->DetachClient(); |
| 318 | } |
| 319 | |
| 320 | void RaiseError() override { |
rockot | 7604e7b7 | 2016-07-28 17:37:39 | [diff] [blame] | 321 | if (task_runner_->BelongsToCurrentThread()) { |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 322 | connector_->RaiseError(); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 323 | } else { |
| 324 | task_runner_->PostTask( |
| 325 | FROM_HERE, |
| 326 | base::Bind(&ChannelAssociatedGroupController::RaiseError, this)); |
| 327 | } |
| 328 | } |
| 329 | |
Ken Rockot | 474df014 | 2017-07-12 13:28:56 | [diff] [blame] | 330 | bool PrefersSerializedMessages() override { return true; } |
| 331 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 332 | private: |
| 333 | class Endpoint; |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 334 | class ControlMessageProxyThunk; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 335 | friend class Endpoint; |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 336 | friend class ControlMessageProxyThunk; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 337 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 338 | // MessageWrapper objects are always destroyed under the controller's lock. On |
| 339 | // destruction, if the message it wrappers contains |
| 340 | // ScopedInterfaceEndpointHandles (which cannot be destructed under the |
| 341 | // controller's lock), the wrapper unlocks to clean them up. |
| 342 | class MessageWrapper { |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 343 | public: |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 344 | MessageWrapper() = default; |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 345 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 346 | MessageWrapper(ChannelAssociatedGroupController* controller, |
| 347 | mojo::Message message) |
| 348 | : controller_(controller), value_(std::move(message)) {} |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 349 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 350 | MessageWrapper(MessageWrapper&& other) |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 351 | : controller_(other.controller_), value_(std::move(other.value_)) {} |
| 352 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 353 | ~MessageWrapper() { |
| 354 | if (value_.associated_endpoint_handles()->empty()) |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 355 | return; |
| 356 | |
| 357 | controller_->lock_.AssertAcquired(); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 358 | { |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 359 | base::AutoUnlock unlocker(controller_->lock_); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 360 | value_.mutable_associated_endpoint_handles()->clear(); |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 364 | MessageWrapper& operator=(MessageWrapper&& other) { |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 365 | controller_ = other.controller_; |
| 366 | value_ = std::move(other.value_); |
| 367 | return *this; |
| 368 | } |
| 369 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 370 | mojo::Message& value() { return value_; } |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 371 | |
| 372 | private: |
| 373 | ChannelAssociatedGroupController* controller_ = nullptr; |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 374 | mojo::Message value_; |
| 375 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 376 | DISALLOW_COPY_AND_ASSIGN(MessageWrapper); |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 377 | }; |
| 378 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 379 | class Endpoint : public base::RefCountedThreadSafe<Endpoint>, |
| 380 | public mojo::InterfaceEndpointController { |
| 381 | public: |
| 382 | Endpoint(ChannelAssociatedGroupController* controller, mojo::InterfaceId id) |
| 383 | : controller_(controller), id_(id) {} |
| 384 | |
| 385 | mojo::InterfaceId id() const { return id_; } |
| 386 | |
| 387 | bool closed() const { |
| 388 | controller_->lock_.AssertAcquired(); |
| 389 | return closed_; |
| 390 | } |
| 391 | |
| 392 | void set_closed() { |
| 393 | controller_->lock_.AssertAcquired(); |
| 394 | closed_ = true; |
| 395 | } |
| 396 | |
| 397 | bool peer_closed() const { |
| 398 | controller_->lock_.AssertAcquired(); |
| 399 | return peer_closed_; |
| 400 | } |
| 401 | |
| 402 | void set_peer_closed() { |
| 403 | controller_->lock_.AssertAcquired(); |
| 404 | peer_closed_ = true; |
| 405 | } |
| 406 | |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 407 | bool handle_created() const { |
| 408 | controller_->lock_.AssertAcquired(); |
| 409 | return handle_created_; |
| 410 | } |
| 411 | |
| 412 | void set_handle_created() { |
| 413 | controller_->lock_.AssertAcquired(); |
| 414 | handle_created_ = true; |
| 415 | } |
| 416 | |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 417 | const base::Optional<mojo::DisconnectReason>& disconnect_reason() const { |
| 418 | return disconnect_reason_; |
| 419 | } |
| 420 | |
| 421 | void set_disconnect_reason( |
| 422 | const base::Optional<mojo::DisconnectReason>& disconnect_reason) { |
| 423 | disconnect_reason_ = disconnect_reason; |
| 424 | } |
| 425 | |
Sam McNally | de5ae67 | 2017-06-19 23:34:45 | [diff] [blame] | 426 | base::SequencedTaskRunner* task_runner() const { |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 427 | return task_runner_.get(); |
| 428 | } |
| 429 | |
| 430 | mojo::InterfaceEndpointClient* client() const { |
| 431 | controller_->lock_.AssertAcquired(); |
| 432 | return client_; |
| 433 | } |
| 434 | |
| 435 | void AttachClient(mojo::InterfaceEndpointClient* client, |
Sam McNally | de5ae67 | 2017-06-19 23:34:45 | [diff] [blame] | 436 | scoped_refptr<base::SequencedTaskRunner> runner) { |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 437 | controller_->lock_.AssertAcquired(); |
| 438 | DCHECK(!client_); |
| 439 | DCHECK(!closed_); |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 440 | DCHECK(runner->RunsTasksInCurrentSequence()); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 441 | |
| 442 | task_runner_ = std::move(runner); |
| 443 | client_ = client; |
| 444 | } |
| 445 | |
| 446 | void DetachClient() { |
| 447 | controller_->lock_.AssertAcquired(); |
| 448 | DCHECK(client_); |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 449 | DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 450 | DCHECK(!closed_); |
| 451 | |
| 452 | task_runner_ = nullptr; |
| 453 | client_ = nullptr; |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 454 | sync_watcher_.reset(); |
| 455 | } |
| 456 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 457 | uint32_t EnqueueSyncMessage(MessageWrapper message) { |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 458 | controller_->lock_.AssertAcquired(); |
| 459 | uint32_t id = GenerateSyncMessageId(); |
| 460 | sync_messages_.emplace(id, std::move(message)); |
| 461 | SignalSyncMessageEvent(); |
| 462 | return id; |
| 463 | } |
| 464 | |
| 465 | void SignalSyncMessageEvent() { |
| 466 | controller_->lock_.AssertAcquired(); |
yzshen | e25b5d5 | 2017-02-28 21:56:31 | [diff] [blame] | 467 | |
Ken Rockot | aa20dcc | 2018-03-28 03:06:51 | [diff] [blame] | 468 | if (sync_watcher_) |
| 469 | sync_watcher_->SignalEvent(); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 470 | } |
| 471 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 472 | MessageWrapper PopSyncMessage(uint32_t id) { |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 473 | controller_->lock_.AssertAcquired(); |
| 474 | if (sync_messages_.empty() || sync_messages_.front().first != id) |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 475 | return MessageWrapper(); |
| 476 | MessageWrapper message = std::move(sync_messages_.front().second); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 477 | sync_messages_.pop(); |
| 478 | return message; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | // mojo::InterfaceEndpointController: |
| 482 | bool SendMessage(mojo::Message* message) override { |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 483 | DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 484 | message->set_interface_id(id_); |
| 485 | return controller_->SendMessage(message); |
| 486 | } |
| 487 | |
| 488 | void AllowWokenUpBySyncWatchOnSameThread() override { |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 489 | DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 490 | |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 491 | EnsureSyncWatcherExists(); |
Ken Rockot | aa20dcc | 2018-03-28 03:06:51 | [diff] [blame] | 492 | sync_watcher_->AllowWokenUpBySyncWatchOnSameSequence(); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | bool SyncWatch(const bool* should_stop) override { |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 496 | DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 497 | |
| 498 | // It's not legal to make sync calls from the master endpoint's thread, |
| 499 | // and in fact they must only happen from the proxy task runner. |
rockot | 7604e7b7 | 2016-07-28 17:37:39 | [diff] [blame] | 500 | DCHECK(!controller_->task_runner_->BelongsToCurrentThread()); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 501 | DCHECK(controller_->proxy_task_runner_->BelongsToCurrentThread()); |
| 502 | |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 503 | EnsureSyncWatcherExists(); |
| 504 | return sync_watcher_->SyncWatch(should_stop); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | private: |
| 508 | friend class base::RefCountedThreadSafe<Endpoint>; |
| 509 | |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 510 | ~Endpoint() override { |
| 511 | controller_->lock_.AssertAcquired(); |
| 512 | DCHECK(!client_); |
| 513 | DCHECK(closed_); |
| 514 | DCHECK(peer_closed_); |
| 515 | DCHECK(!sync_watcher_); |
| 516 | } |
| 517 | |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 518 | void OnSyncMessageEventReady() { |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 519 | DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 520 | |
| 521 | scoped_refptr<Endpoint> keepalive(this); |
| 522 | scoped_refptr<AssociatedGroupController> controller_keepalive( |
| 523 | controller_); |
Ken Rockot | aa20dcc | 2018-03-28 03:06:51 | [diff] [blame] | 524 | base::AutoLock locker(controller_->lock_); |
| 525 | bool more_to_process = false; |
| 526 | if (!sync_messages_.empty()) { |
| 527 | MessageWrapper message_wrapper = |
| 528 | std::move(sync_messages_.front().second); |
| 529 | sync_messages_.pop(); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 530 | |
Ken Rockot | aa20dcc | 2018-03-28 03:06:51 | [diff] [blame] | 531 | bool dispatch_succeeded; |
| 532 | mojo::InterfaceEndpointClient* client = client_; |
| 533 | { |
| 534 | base::AutoUnlock unlocker(controller_->lock_); |
| 535 | dispatch_succeeded = |
| 536 | client->HandleIncomingMessage(&message_wrapper.value()); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 537 | } |
| 538 | |
Ken Rockot | aa20dcc | 2018-03-28 03:06:51 | [diff] [blame] | 539 | if (!sync_messages_.empty()) |
| 540 | more_to_process = true; |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 541 | |
Ken Rockot | aa20dcc | 2018-03-28 03:06:51 | [diff] [blame] | 542 | if (!dispatch_succeeded) |
| 543 | controller_->RaiseError(); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 544 | } |
| 545 | |
Ken Rockot | aa20dcc | 2018-03-28 03:06:51 | [diff] [blame] | 546 | if (!more_to_process) |
| 547 | sync_watcher_->ResetEvent(); |
| 548 | |
| 549 | // If there are no queued sync messages and the peer has closed, there |
| 550 | // there won't be incoming sync messages in the future. If any |
| 551 | // SyncWatch() calls are on the stack for this endpoint, resetting the |
| 552 | // watcher will allow them to exit as the stack undwinds. |
| 553 | if (!more_to_process && peer_closed_) |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 554 | sync_watcher_.reset(); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | void EnsureSyncWatcherExists() { |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 558 | DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 559 | if (sync_watcher_) |
| 560 | return; |
| 561 | |
Ken Rockot | aa20dcc | 2018-03-28 03:06:51 | [diff] [blame] | 562 | base::AutoLock locker(controller_->lock_); |
| 563 | sync_watcher_ = std::make_unique<mojo::SequenceLocalSyncEventWatcher>( |
| 564 | base::BindRepeating(&Endpoint::OnSyncMessageEventReady, |
| 565 | base::Unretained(this))); |
| 566 | if (peer_closed_ || !sync_messages_.empty()) |
| 567 | SignalSyncMessageEvent(); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | uint32_t GenerateSyncMessageId() { |
| 571 | // Overflow is fine. |
| 572 | uint32_t id = next_sync_message_id_++; |
| 573 | DCHECK(sync_messages_.empty() || sync_messages_.front().first != id); |
| 574 | return id; |
| 575 | } |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 576 | |
| 577 | ChannelAssociatedGroupController* const controller_; |
| 578 | const mojo::InterfaceId id_; |
| 579 | |
| 580 | bool closed_ = false; |
| 581 | bool peer_closed_ = false; |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 582 | bool handle_created_ = false; |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 583 | base::Optional<mojo::DisconnectReason> disconnect_reason_; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 584 | mojo::InterfaceEndpointClient* client_ = nullptr; |
Sam McNally | de5ae67 | 2017-06-19 23:34:45 | [diff] [blame] | 585 | scoped_refptr<base::SequencedTaskRunner> task_runner_; |
Ken Rockot | aa20dcc | 2018-03-28 03:06:51 | [diff] [blame] | 586 | std::unique_ptr<mojo::SequenceLocalSyncEventWatcher> sync_watcher_; |
Brett Wilson | a62d9c0 | 2017-09-20 20:53:20 | [diff] [blame] | 587 | base::queue<std::pair<uint32_t, MessageWrapper>> sync_messages_; |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 588 | uint32_t next_sync_message_id_ = 0; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 589 | |
| 590 | DISALLOW_COPY_AND_ASSIGN(Endpoint); |
| 591 | }; |
| 592 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 593 | class ControlMessageProxyThunk : public MessageReceiver { |
| 594 | public: |
| 595 | explicit ControlMessageProxyThunk( |
| 596 | ChannelAssociatedGroupController* controller) |
| 597 | : controller_(controller) {} |
| 598 | |
| 599 | private: |
| 600 | // MessageReceiver: |
| 601 | bool Accept(mojo::Message* message) override { |
| 602 | return controller_->SendMessage(message); |
| 603 | } |
| 604 | |
| 605 | ChannelAssociatedGroupController* controller_; |
| 606 | |
| 607 | DISALLOW_COPY_AND_ASSIGN(ControlMessageProxyThunk); |
| 608 | }; |
| 609 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 610 | ~ChannelAssociatedGroupController() override { |
rockot | b01ef6a | 2016-07-27 03:24:32 | [diff] [blame] | 611 | DCHECK(!connector_); |
| 612 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 613 | base::AutoLock locker(lock_); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 614 | for (auto iter = endpoints_.begin(); iter != endpoints_.end();) { |
| 615 | Endpoint* endpoint = iter->second.get(); |
| 616 | ++iter; |
| 617 | |
yzshen | e003d59 | 2017-01-24 21:42:17 | [diff] [blame] | 618 | if (!endpoint->closed()) { |
| 619 | // This happens when a NotifyPeerEndpointClosed message been received, |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 620 | // but the interface ID hasn't been used to create local endpoint |
| 621 | // handle. |
yzshen | e003d59 | 2017-01-24 21:42:17 | [diff] [blame] | 622 | DCHECK(!endpoint->client()); |
| 623 | DCHECK(endpoint->peer_closed()); |
| 624 | MarkClosedAndMaybeRemove(endpoint); |
| 625 | } else { |
| 626 | MarkPeerClosedAndMaybeRemove(endpoint); |
| 627 | } |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | DCHECK(endpoints_.empty()); |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 631 | |
| 632 | GetMemoryDumpProvider().RemoveController(this); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | bool SendMessage(mojo::Message* message) { |
rockot | 7604e7b7 | 2016-07-28 17:37:39 | [diff] [blame] | 636 | if (task_runner_->BelongsToCurrentThread()) { |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 637 | DCHECK(thread_checker_.CalledOnValidThread()); |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 638 | if (!connector_ || paused_) { |
Ken Rockot | 37ddd815 | 2018-02-22 18:18:46 | [diff] [blame] | 639 | if (!shut_down_) { |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 640 | base::AutoLock lock(outgoing_messages_lock_); |
Ken Rockot | 3e7284bb | 2018-02-06 16:11:16 | [diff] [blame] | 641 | outgoing_messages_.emplace_back(std::move(*message)); |
Ken Rockot | 37ddd815 | 2018-02-22 18:18:46 | [diff] [blame] | 642 | |
Lukasz Anforowicz | b29fffc2 | 2018-05-29 17:37:33 | [diff] [blame] | 643 | // TODO(https://crbug.com/813045): Change this to a DCHECK. Typically |
| 644 | // this queue won't exceed something like 50 messages even on slow |
| 645 | // devices - higher numbers probably indicate that IPC messages are |
| 646 | // leaked. |
| 647 | CHECK_LE(outgoing_messages_.size(), |
| 648 | MojoBootstrap::kMaxOutgoingMessagesSizeForTesting); |
Ken Rockot | 37ddd815 | 2018-02-22 18:18:46 | [diff] [blame] | 649 | } |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 650 | return true; |
| 651 | } |
| 652 | return connector_->Accept(message); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 653 | } else { |
Ken Rockot | fb81dc0 | 2018-05-15 21:59:26 | [diff] [blame] | 654 | // Do a message size check here so we don't lose valuable stack |
| 655 | // information to the task scheduler. |
| 656 | CHECK_LE(message->data_num_bytes(), Channel::kMaximumMessageSize); |
| 657 | |
rockot | becd3f74 | 2016-11-08 20:47:00 | [diff] [blame] | 658 | // We always post tasks to the master endpoint thread when called from |
| 659 | // other threads in order to simulate IPC::ChannelProxy::Send behavior. |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 660 | task_runner_->PostTask( |
| 661 | FROM_HERE, |
| 662 | base::Bind( |
| 663 | &ChannelAssociatedGroupController::SendMessageOnMasterThread, |
rockot | c4cc691e | 2016-08-19 18:48:57 | [diff] [blame] | 664 | this, base::Passed(message))); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 665 | return true; |
| 666 | } |
| 667 | } |
| 668 | |
rockot | c4cc691e | 2016-08-19 18:48:57 | [diff] [blame] | 669 | void SendMessageOnMasterThread(mojo::Message message) { |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 670 | DCHECK(thread_checker_.CalledOnValidThread()); |
rockot | c4cc691e | 2016-08-19 18:48:57 | [diff] [blame] | 671 | if (!SendMessage(&message)) |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 672 | RaiseError(); |
| 673 | } |
| 674 | |
| 675 | void OnPipeError() { |
| 676 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 677 | |
| 678 | // We keep |this| alive here because it's possible for the notifications |
| 679 | // below to release all other references. |
| 680 | scoped_refptr<ChannelAssociatedGroupController> keepalive(this); |
| 681 | |
| 682 | base::AutoLock locker(lock_); |
| 683 | encountered_error_ = true; |
| 684 | |
| 685 | std::vector<scoped_refptr<Endpoint>> endpoints_to_notify; |
| 686 | for (auto iter = endpoints_.begin(); iter != endpoints_.end();) { |
| 687 | Endpoint* endpoint = iter->second.get(); |
| 688 | ++iter; |
| 689 | |
| 690 | if (endpoint->client()) |
| 691 | endpoints_to_notify.push_back(endpoint); |
| 692 | |
| 693 | MarkPeerClosedAndMaybeRemove(endpoint); |
| 694 | } |
| 695 | |
| 696 | for (auto& endpoint : endpoints_to_notify) { |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 697 | // Because a notification may in turn detach any endpoint, we have to |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 698 | // check each client again here. |
| 699 | if (endpoint->client()) |
| 700 | NotifyEndpointOfError(endpoint.get(), false /* force_async */); |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | void NotifyEndpointOfError(Endpoint* endpoint, bool force_async) { |
| 705 | lock_.AssertAcquired(); |
| 706 | DCHECK(endpoint->task_runner() && endpoint->client()); |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 707 | if (endpoint->task_runner()->RunsTasksInCurrentSequence() && !force_async) { |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 708 | mojo::InterfaceEndpointClient* client = endpoint->client(); |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 709 | base::Optional<mojo::DisconnectReason> reason( |
| 710 | endpoint->disconnect_reason()); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 711 | |
| 712 | base::AutoUnlock unlocker(lock_); |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 713 | client->NotifyError(reason); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 714 | } else { |
| 715 | endpoint->task_runner()->PostTask( |
| 716 | FROM_HERE, |
tzik | 1d692a2e | 2017-07-03 11:01:26 | [diff] [blame] | 717 | base::Bind(&ChannelAssociatedGroupController:: |
| 718 | NotifyEndpointOfErrorOnEndpointThread, |
| 719 | this, endpoint->id(), base::Unretained(endpoint))); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 720 | } |
| 721 | } |
| 722 | |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 723 | void NotifyEndpointOfErrorOnEndpointThread(mojo::InterfaceId id, |
| 724 | Endpoint* endpoint) { |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 725 | base::AutoLock locker(lock_); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 726 | auto iter = endpoints_.find(id); |
| 727 | if (iter == endpoints_.end() || iter->second.get() != endpoint) |
| 728 | return; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 729 | if (!endpoint->client()) |
| 730 | return; |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 731 | |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 732 | DCHECK(endpoint->task_runner()->RunsTasksInCurrentSequence()); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 733 | NotifyEndpointOfError(endpoint, false /* force_async */); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | void MarkClosedAndMaybeRemove(Endpoint* endpoint) { |
| 737 | lock_.AssertAcquired(); |
| 738 | endpoint->set_closed(); |
| 739 | if (endpoint->closed() && endpoint->peer_closed()) |
| 740 | endpoints_.erase(endpoint->id()); |
| 741 | } |
| 742 | |
| 743 | void MarkPeerClosedAndMaybeRemove(Endpoint* endpoint) { |
| 744 | lock_.AssertAcquired(); |
| 745 | endpoint->set_peer_closed(); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 746 | endpoint->SignalSyncMessageEvent(); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 747 | if (endpoint->closed() && endpoint->peer_closed()) |
| 748 | endpoints_.erase(endpoint->id()); |
| 749 | } |
| 750 | |
| 751 | Endpoint* FindOrInsertEndpoint(mojo::InterfaceId id, bool* inserted) { |
| 752 | lock_.AssertAcquired(); |
| 753 | DCHECK(!inserted || !*inserted); |
| 754 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 755 | Endpoint* endpoint = FindEndpoint(id); |
| 756 | if (!endpoint) { |
| 757 | endpoint = new Endpoint(this, id); |
| 758 | endpoints_.insert({id, endpoint}); |
| 759 | if (inserted) |
| 760 | *inserted = true; |
| 761 | } |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 762 | return endpoint; |
| 763 | } |
| 764 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 765 | Endpoint* FindEndpoint(mojo::InterfaceId id) { |
| 766 | lock_.AssertAcquired(); |
| 767 | auto iter = endpoints_.find(id); |
| 768 | return iter != endpoints_.end() ? iter->second.get() : nullptr; |
| 769 | } |
| 770 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 771 | // mojo::MessageReceiver: |
| 772 | bool Accept(mojo::Message* message) override { |
| 773 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 774 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 775 | if (!message->DeserializeAssociatedEndpointHandles(this)) |
| 776 | return false; |
| 777 | |
| 778 | if (mojo::PipeControlMessageHandler::IsPipeControlMessage(message)) |
| 779 | return control_message_handler_.Accept(message); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 780 | |
| 781 | mojo::InterfaceId id = message->interface_id(); |
| 782 | DCHECK(mojo::IsValidInterfaceId(id)); |
| 783 | |
| 784 | base::AutoLock locker(lock_); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 785 | Endpoint* endpoint = FindEndpoint(id); |
| 786 | if (!endpoint) |
| 787 | return true; |
| 788 | |
| 789 | mojo::InterfaceEndpointClient* client = endpoint->client(); |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 790 | if (!client || !endpoint->task_runner()->RunsTasksInCurrentSequence()) { |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 791 | // No client has been bound yet or the client runs tasks on another |
| 792 | // thread. We assume the other thread must always be the one on which |
| 793 | // |proxy_task_runner_| runs tasks, since that's the only valid scenario. |
| 794 | // |
| 795 | // If the client is not yet bound, it must be bound by the time this task |
| 796 | // runs or else it's programmer error. |
| 797 | DCHECK(proxy_task_runner_); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 798 | |
rockot | c4cc691e | 2016-08-19 18:48:57 | [diff] [blame] | 799 | if (message->has_flag(mojo::Message::kFlagIsSync)) { |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 800 | MessageWrapper message_wrapper(this, std::move(*message)); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 801 | // Sync messages may need to be handled by the endpoint if it's blocking |
| 802 | // on a sync reply. We pass ownership of the message to the endpoint's |
| 803 | // sync message queue. If the endpoint was blocking, it will dequeue the |
| 804 | // message and dispatch it. Otherwise the posted |AcceptSyncMessage()| |
| 805 | // call will dequeue the message and dispatch it. |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 806 | uint32_t message_id = |
| 807 | endpoint->EnqueueSyncMessage(std::move(message_wrapper)); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 808 | proxy_task_runner_->PostTask( |
| 809 | FROM_HERE, |
| 810 | base::Bind(&ChannelAssociatedGroupController::AcceptSyncMessage, |
| 811 | this, id, message_id)); |
| 812 | return true; |
| 813 | } |
| 814 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 815 | proxy_task_runner_->PostTask( |
| 816 | FROM_HERE, |
| 817 | base::Bind(&ChannelAssociatedGroupController::AcceptOnProxyThread, |
rockot | c4cc691e | 2016-08-19 18:48:57 | [diff] [blame] | 818 | this, base::Passed(message))); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 819 | return true; |
| 820 | } |
| 821 | |
| 822 | // We do not expect to receive sync responses on the master endpoint thread. |
| 823 | // If it's happening, it's a bug. |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 824 | DCHECK(!message->has_flag(mojo::Message::kFlagIsSync) || |
| 825 | !message->has_flag(mojo::Message::kFlagIsResponse)); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 826 | |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 827 | base::AutoUnlock unlocker(lock_); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 828 | return client->HandleIncomingMessage(message); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 829 | } |
| 830 | |
rockot | c4cc691e | 2016-08-19 18:48:57 | [diff] [blame] | 831 | void AcceptOnProxyThread(mojo::Message message) { |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 832 | DCHECK(proxy_task_runner_->BelongsToCurrentThread()); |
| 833 | |
rockot | c4cc691e | 2016-08-19 18:48:57 | [diff] [blame] | 834 | mojo::InterfaceId id = message.interface_id(); |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 835 | DCHECK(mojo::IsValidInterfaceId(id) && !mojo::IsMasterInterfaceId(id)); |
| 836 | |
| 837 | base::AutoLock locker(lock_); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 838 | Endpoint* endpoint = FindEndpoint(id); |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 839 | if (!endpoint) |
| 840 | return; |
| 841 | |
| 842 | mojo::InterfaceEndpointClient* client = endpoint->client(); |
| 843 | if (!client) |
| 844 | return; |
| 845 | |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 846 | DCHECK(endpoint->task_runner()->RunsTasksInCurrentSequence()); |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 847 | |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 848 | // Sync messages should never make their way to this method. |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 849 | DCHECK(!message.has_flag(mojo::Message::kFlagIsSync)); |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 850 | |
| 851 | bool result = false; |
| 852 | { |
| 853 | base::AutoUnlock unlocker(lock_); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 854 | result = client->HandleIncomingMessage(&message); |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | if (!result) |
| 858 | RaiseError(); |
| 859 | } |
| 860 | |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 861 | void AcceptSyncMessage(mojo::InterfaceId interface_id, uint32_t message_id) { |
| 862 | DCHECK(proxy_task_runner_->BelongsToCurrentThread()); |
| 863 | |
| 864 | base::AutoLock locker(lock_); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 865 | Endpoint* endpoint = FindEndpoint(interface_id); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 866 | if (!endpoint) |
| 867 | return; |
| 868 | |
csharrison | 1af8d6ab | 2017-04-21 17:47:23 | [diff] [blame] | 869 | // Careful, if the endpoint is detached its members are cleared. Check for |
| 870 | // that before dereferencing. |
| 871 | mojo::InterfaceEndpointClient* client = endpoint->client(); |
| 872 | if (!client) |
| 873 | return; |
| 874 | |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 875 | DCHECK(endpoint->task_runner()->RunsTasksInCurrentSequence()); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 876 | MessageWrapper message_wrapper = endpoint->PopSyncMessage(message_id); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 877 | |
| 878 | // The message must have already been dequeued by the endpoint waking up |
| 879 | // from a sync wait. Nothing to do. |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 880 | if (message_wrapper.value().IsNull()) |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 881 | return; |
| 882 | |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 883 | bool result = false; |
| 884 | { |
| 885 | base::AutoUnlock unlocker(lock_); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 886 | result = client->HandleIncomingMessage(&message_wrapper.value()); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | if (!result) |
| 890 | RaiseError(); |
| 891 | } |
| 892 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 893 | // mojo::PipeControlMessageHandlerDelegate: |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 894 | bool OnPeerAssociatedEndpointClosed( |
| 895 | mojo::InterfaceId id, |
| 896 | const base::Optional<mojo::DisconnectReason>& reason) override { |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 897 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 898 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 899 | scoped_refptr<ChannelAssociatedGroupController> keepalive(this); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 900 | base::AutoLock locker(lock_); |
| 901 | scoped_refptr<Endpoint> endpoint = FindOrInsertEndpoint(id, nullptr); |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 902 | if (reason) |
| 903 | endpoint->set_disconnect_reason(reason); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 904 | if (!endpoint->peer_closed()) { |
| 905 | if (endpoint->client()) |
| 906 | NotifyEndpointOfError(endpoint.get(), false /* force_async */); |
| 907 | MarkPeerClosedAndMaybeRemove(endpoint.get()); |
| 908 | } |
| 909 | |
| 910 | return true; |
| 911 | } |
| 912 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 913 | // Checked in places which must be run on the master endpoint's thread. |
| 914 | base::ThreadChecker thread_checker_; |
| 915 | |
| 916 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 917 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 918 | scoped_refptr<base::SingleThreadTaskRunner> proxy_task_runner_; |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 919 | const bool set_interface_id_namespace_bit_; |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 920 | bool paused_ = false; |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 921 | std::unique_ptr<mojo::Connector> connector_; |
rockot | 222e7dd | 2016-08-24 23:37:11 | [diff] [blame] | 922 | mojo::FilterChain filters_; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 923 | mojo::PipeControlMessageHandler control_message_handler_; |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 924 | ControlMessageProxyThunk control_message_proxy_thunk_; |
rockot | 5890954 | 2016-11-10 20:05:45 | [diff] [blame] | 925 | |
| 926 | // NOTE: It is unsafe to call into this object while holding |lock_|. |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 927 | mojo::PipeControlMessageProxy control_message_proxy_; |
| 928 | |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 929 | // Guards access to |outgoing_messages_| only. Used to support memory dumps |
| 930 | // which may be triggered from any thread. |
| 931 | base::Lock outgoing_messages_lock_; |
| 932 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 933 | // Outgoing messages that were sent before this controller was bound to a |
| 934 | // real message pipe. |
rockot | c4cc691e | 2016-08-19 18:48:57 | [diff] [blame] | 935 | std::vector<mojo::Message> outgoing_messages_; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 936 | |
| 937 | // Guards the fields below for thread-safe access. |
| 938 | base::Lock lock_; |
| 939 | |
| 940 | bool encountered_error_ = false; |
Ken Rockot | 3e7284bb | 2018-02-06 16:11:16 | [diff] [blame] | 941 | bool shut_down_ = false; |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 942 | |
| 943 | // ID #1 is reserved for the mojom::Channel interface. |
| 944 | uint32_t next_interface_id_ = 2; |
| 945 | |
Yuzhu Shen | 7bcd8ebf | 2017-10-02 23:21:14 | [diff] [blame] | 946 | std::map<uint32_t, scoped_refptr<Endpoint>> endpoints_; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 947 | |
| 948 | DISALLOW_COPY_AND_ASSIGN(ChannelAssociatedGroupController); |
| 949 | }; |
| 950 | |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 951 | bool ControllerMemoryDumpProvider::OnMemoryDump( |
| 952 | const base::trace_event::MemoryDumpArgs& args, |
| 953 | base::trace_event::ProcessMemoryDump* pmd) { |
| 954 | base::AutoLock lock(lock_); |
| 955 | for (auto* controller : controllers_) { |
| 956 | base::trace_event::MemoryAllocatorDump* dump = pmd->CreateAllocatorDump( |
| 957 | base::StringPrintf("mojo/queued_ipc_channel_message/0x%" PRIxPTR, |
| 958 | reinterpret_cast<uintptr_t>(controller))); |
| 959 | dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameObjectCount, |
| 960 | base::trace_event::MemoryAllocatorDump::kUnitsObjects, |
| 961 | controller->GetQueuedMessageCount()); |
| 962 | } |
| 963 | |
| 964 | return true; |
| 965 | } |
| 966 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 967 | class MojoBootstrapImpl : public MojoBootstrap { |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 968 | public: |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 969 | MojoBootstrapImpl( |
| 970 | mojo::ScopedMessagePipeHandle handle, |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 971 | const scoped_refptr<ChannelAssociatedGroupController> controller) |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 972 | : controller_(controller), |
| 973 | associated_group_(controller), |
| 974 | handle_(std::move(handle)) {} |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 975 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 976 | ~MojoBootstrapImpl() override { |
| 977 | controller_->ShutDown(); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | private: |
rockot | a628d0b | 2017-02-09 08:40:15 | [diff] [blame] | 981 | void Connect(mojom::ChannelAssociatedPtr* sender, |
| 982 | mojom::ChannelAssociatedRequest* receiver) override { |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 983 | controller_->Bind(std::move(handle_)); |
rockot | a628d0b | 2017-02-09 08:40:15 | [diff] [blame] | 984 | controller_->CreateChannelEndpoints(sender, receiver); |
msramek | 5507fee | 2016-07-22 10:06:21 | [diff] [blame] | 985 | } |
| 986 | |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 987 | void Pause() override { |
| 988 | controller_->Pause(); |
| 989 | } |
| 990 | |
| 991 | void Unpause() override { |
| 992 | controller_->Unpause(); |
rockot | 401fb2c | 2016-09-06 18:35:57 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | void Flush() override { |
| 996 | controller_->FlushOutgoingMessages(); |
| 997 | } |
| 998 | |
msramek | 5507fee | 2016-07-22 10:06:21 | [diff] [blame] | 999 | mojo::AssociatedGroup* GetAssociatedGroup() override { |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 1000 | return &associated_group_; |
msramek | 5507fee | 2016-07-22 10:06:21 | [diff] [blame] | 1001 | } |
| 1002 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 1003 | scoped_refptr<ChannelAssociatedGroupController> controller_; |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 1004 | mojo::AssociatedGroup associated_group_; |
msramek | 5507fee | 2016-07-22 10:06:21 | [diff] [blame] | 1005 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 1006 | mojo::ScopedMessagePipeHandle handle_; |
msramek | 5507fee | 2016-07-22 10:06:21 | [diff] [blame] | 1007 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 1008 | DISALLOW_COPY_AND_ASSIGN(MojoBootstrapImpl); |
msramek | 5507fee | 2016-07-22 10:06:21 | [diff] [blame] | 1009 | }; |
| 1010 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 1011 | } // namespace |
| 1012 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 1013 | // static |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 1014 | std::unique_ptr<MojoBootstrap> MojoBootstrap::Create( |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 1015 | mojo::ScopedMessagePipeHandle handle, |
| 1016 | Channel::Mode mode, |
Hajime Hoshi | a98f110 | 2017-11-20 06:34:35 | [diff] [blame] | 1017 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
| 1018 | const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner) { |
Jeremy Roman | 160eb92 | 2017-08-29 17:43:43 | [diff] [blame] | 1019 | return std::make_unique<MojoBootstrapImpl>( |
Hajime Hoshi | a98f110 | 2017-11-20 06:34:35 | [diff] [blame] | 1020 | std::move(handle), |
| 1021 | new ChannelAssociatedGroupController(mode == Channel::MODE_SERVER, |
| 1022 | ipc_task_runner, proxy_task_runner)); |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 1023 | } |
| 1024 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 1025 | } // namespace IPC |