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