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