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