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