Avi Drissman | ea1be23 | 2022-09-14 23:29:06 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 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 | |
Peter Kasting | 604e91f | 2025-01-27 21:12:28 | [diff] [blame] | 10 | #include<algorithm> |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 11 | #include<map> |
| 12 | #include<memory> |
Arthur Sonzogni | 5bc3326c | 2024-02-29 19:39:05 | [diff] [blame] | 13 | #include<optional> |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 14 | #include<set> |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 15 | #include<utility> |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 16 | #include<vector> |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 17 | |
Hans Wennborg | 4ed044f | 2020-04-27 09:43:38 | [diff] [blame] | 18 | #include"base/check_op.h" |
Ken Rockot | b6f9212 | 2022-10-31 04:52:24 | [diff] [blame] | 19 | #include"base/containers/circular_deque.h" |
Lei Zhang | 812c539 | 2021-05-18 04:44:32 | [diff] [blame] | 20 | #include"base/containers/contains.h" |
François Doray | fad1b8b | 2024-01-04 20:22:53 | [diff] [blame] | 21 | #include"base/feature_list.h" |
Avi Drissman | cac43f2 | 2023-01-12 00:58:41 | [diff] [blame] | 22 | #include"base/functional/bind.h" |
| 23 | #include"base/functional/callback.h" |
Etienne Pierre-doray | fc7952f0 | 2025-06-06 00:04:33 | [diff] [blame] | 24 | #include"base/hash/hash.h" |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 25 | #include"base/memory/ptr_util.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 26 | #include"base/memory/raw_ptr.h" |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 27 | #include"base/no_destructor.h" |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 28 | #include"base/sequence_checker.h" |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 29 | #include"base/strings/stringprintf.h" |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 30 | #include"base/synchronization/lock.h" |
Ken Rockot | b6f9212 | 2022-10-31 04:52:24 | [diff] [blame] | 31 | #include"base/synchronization/waitable_event.h" |
Harkiran Bolaria | 8b1cf11 | 2020-09-15 22:58:34 | [diff] [blame] | 32 | #include"base/task/common/task_annotator.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 33 | #include"base/task/sequenced_task_runner.h" |
| 34 | #include"base/task/single_thread_task_runner.h" |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 35 | #include"base/thread_annotations.h" |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 36 | #include"base/trace_event/memory_allocator_dump.h" |
| 37 | #include"base/trace_event/memory_dump_manager.h" |
| 38 | #include"base/trace_event/memory_dump_provider.h" |
Alexander Timin | da1f2c5 | 2021-11-03 02:23:05 | [diff] [blame] | 39 | #include"base/trace_event/typed_macros.h" |
Ken Rockot | fb81dc0 | 2018-05-15 21:59:26 | [diff] [blame] | 40 | #include"ipc/ipc_channel.h" |
Scott Haseley | cf0f172 | 2023-11-03 21:39:01 | [diff] [blame] | 41 | #include"ipc/urgent_message_observer.h" |
François Doray | 927a082 | 2024-05-15 21:57:28 | [diff] [blame] | 42 | #include"mojo/public/c/system/types.h" |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 43 | #include"mojo/public/cpp/bindings/associated_group.h" |
| 44 | #include"mojo/public/cpp/bindings/associated_group_controller.h" |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 45 | #include"mojo/public/cpp/bindings/connector.h" |
Ken Rockot | c1b27ca | 2024-04-13 00:37:39 | [diff] [blame] | 46 | #include"mojo/public/cpp/bindings/features.h" |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 47 | #include"mojo/public/cpp/bindings/interface_endpoint_client.h" |
| 48 | #include"mojo/public/cpp/bindings/interface_endpoint_controller.h" |
| 49 | #include"mojo/public/cpp/bindings/interface_id.h" |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 50 | #include"mojo/public/cpp/bindings/message.h" |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 51 | #include"mojo/public/cpp/bindings/message_header_validator.h" |
Alexander Timin | da1f2c5 | 2021-11-03 02:23:05 | [diff] [blame] | 52 | #include"mojo/public/cpp/bindings/mojo_buildflags.h" |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 53 | #include"mojo/public/cpp/bindings/pipe_control_message_handler.h" |
| 54 | #include"mojo/public/cpp/bindings/pipe_control_message_handler_delegate.h" |
| 55 | #include"mojo/public/cpp/bindings/pipe_control_message_proxy.h" |
François Doray | 927a082 | 2024-05-15 21:57:28 | [diff] [blame] | 56 | #include"mojo/public/cpp/bindings/scoped_message_error_crash_key.h" |
Ken Rockot | aa20dcc | 2018-03-28 03:06:51 | [diff] [blame] | 57 | #include"mojo/public/cpp/bindings/sequence_local_sync_event_watcher.h" |
Alexander Timin | da1f2c5 | 2021-11-03 02:23:05 | [diff] [blame] | 58 | #include"mojo/public/cpp/bindings/tracing_helpers.h" |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 59 | |
| 60 | namespace IPC{ |
| 61 | |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 62 | classChannelAssociatedGroupController; |
| 63 | |
François Doray | fad1b8b | 2024-01-04 20:22:53 | [diff] [blame] | 64 | namespace{ |
| 65 | |
Peter Kasting | 4b18d0c | 2024-09-18 00:56:11 | [diff] [blame] | 66 | constinit thread_localbool off_sequence_binding_allowed=false; |
Ken Rockot | c5cc0273 | 2021-06-04 08:02:41 | [diff] [blame] | 67 | |
François Doray | fad1b8b | 2024-01-04 20:22:53 | [diff] [blame] | 68 | BASE_FEATURE(kMojoChannelAssociatedSendUsesRunOrPostTask, |
| 69 | "MojoChannelAssociatedSendUsesRunOrPostTask", |
François Doray | 80649802 | 2024-01-11 21:37:28 | [diff] [blame] | 70 | base::FEATURE_DISABLED_BY_DEFAULT); |
François Doray | fad1b8b | 2024-01-04 20:22:53 | [diff] [blame] | 71 | |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 72 | // Used to track some internal Channel state in pursuit of message leaks. |
| 73 | // |
Alison Gale | 81f4f2c7 | 2024-04-22 19:33:31 | [diff] [blame] | 74 | // TODO(crbug.com/40563310): Remove this. |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 75 | classControllerMemoryDumpProvider |
| 76 | :public base::trace_event::MemoryDumpProvider{ |
| 77 | public: |
| 78 | ControllerMemoryDumpProvider(){ |
| 79 | base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
| 80 | this,"IPCChannel",nullptr); |
| 81 | } |
| 82 | |
Peter Boström | c68c5aa | 2021-09-28 00:28:00 | [diff] [blame] | 83 | ControllerMemoryDumpProvider(constControllerMemoryDumpProvider&)=delete; |
| 84 | ControllerMemoryDumpProvider&operator=(constControllerMemoryDumpProvider&)= |
| 85 | delete; |
| 86 | |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 87 | ~ControllerMemoryDumpProvider() override{ |
| 88 | base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( |
| 89 | this); |
| 90 | } |
| 91 | |
| 92 | voidAddController(ChannelAssociatedGroupController* controller){ |
| 93 | base::AutoLock lock(lock_); |
| 94 | controllers_.insert(controller); |
| 95 | } |
| 96 | |
| 97 | voidRemoveController(ChannelAssociatedGroupController* controller){ |
| 98 | base::AutoLock lock(lock_); |
| 99 | controllers_.erase(controller); |
| 100 | } |
| 101 | |
| 102 | // base::trace_event::MemoryDumpProvider: |
| 103 | boolOnMemoryDump(const base::trace_event::MemoryDumpArgs& args, |
| 104 | base::trace_event::ProcessMemoryDump* pmd) override; |
| 105 | |
| 106 | private: |
| 107 | base::Lock lock_; |
Ali Hijazi | 133b2d9 | 2024-02-09 14:01:52 | [diff] [blame] | 108 | std::set<raw_ptr<ChannelAssociatedGroupController,SetExperimental>> |
| 109 | controllers_; |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | ControllerMemoryDumpProvider&GetMemoryDumpProvider(){ |
| 113 | static base::NoDestructor<ControllerMemoryDumpProvider> provider; |
| 114 | return*provider; |
| 115 | } |
| 116 | |
Siddhartha S | 0348442 | 2019-04-23 20:30:00 | [diff] [blame] | 117 | // Messages are grouped by this info when recording memory metrics. |
| 118 | structMessageMemoryDumpInfo{ |
| 119 | MessageMemoryDumpInfo(const mojo::Message& message) |
| 120 | : id(message.name()), profiler_tag(message.heap_profiler_tag()){} |
| 121 | MessageMemoryDumpInfo()=default; |
| 122 | |
| 123 | booloperator==(constMessageMemoryDumpInfo& other)const{ |
| 124 | return other.id== id&& other.profiler_tag== profiler_tag; |
| 125 | } |
| 126 | |
| 127 | uint32_t id=0; |
| 128 | constchar* profiler_tag=nullptr; |
| 129 | }; |
| 130 | |
| 131 | structMessageMemoryDumpInfoHash{ |
| 132 | size_toperator()(constMessageMemoryDumpInfo& info)const{ |
Daniel Cheng | 5c5a652 | 2019-11-19 18:03:36 | [diff] [blame] | 133 | return base::HashInts( |
| 134 | info.id, info.profiler_tag? base::FastHash(info.profiler_tag):0); |
Siddhartha S | 0348442 | 2019-04-23 20:30:00 | [diff] [blame] | 135 | } |
| 136 | }; |
| 137 | |
Scott Haseley | cf0f172 | 2023-11-03 21:39:01 | [diff] [blame] | 138 | classScopedUrgentMessageNotification{ |
| 139 | public: |
| 140 | explicitScopedUrgentMessageNotification( |
| 141 | UrgentMessageObserver* observer=nullptr) |
| 142 | : observer_(observer){ |
| 143 | if(observer_){ |
| 144 | observer_->OnUrgentMessageReceived(); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | ~ScopedUrgentMessageNotification(){ |
| 149 | if(observer_){ |
| 150 | observer_->OnUrgentMessageProcessed(); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | ScopedUrgentMessageNotification(ScopedUrgentMessageNotification&& other) |
| 155 | : observer_(std::exchange(other.observer_,nullptr)){} |
| 156 | |
| 157 | ScopedUrgentMessageNotification&operator=( |
| 158 | ScopedUrgentMessageNotification&& other){ |
| 159 | observer_= std::exchange(other.observer_,nullptr); |
| 160 | return*this; |
| 161 | } |
| 162 | |
| 163 | private: |
| 164 | raw_ptr<UrgentMessageObserver> observer_; |
| 165 | }; |
| 166 | |
François Doray | fad1b8b | 2024-01-04 20:22:53 | [diff] [blame] | 167 | }// namespace |
| 168 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 169 | classChannelAssociatedGroupController |
| 170 | :public mojo::AssociatedGroupController, |
| 171 | public mojo::MessageReceiver, |
| 172 | public mojo::PipeControlMessageHandlerDelegate{ |
| 173 | public: |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 174 | ChannelAssociatedGroupController( |
| 175 | bool set_interface_id_namespace_bit, |
Hajime Hoshi | a98f110 | 2017-11-20 06:34:35 | [diff] [blame] | 176 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
Ken Rockot | 7308d4b | 2022-12-13 06:35:56 | [diff] [blame] | 177 | const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner) |
rockot | b01ef6a | 2016-07-27 03:24:32 | [diff] [blame] | 178 | : task_runner_(ipc_task_runner), |
Hajime Hoshi | a98f110 | 2017-11-20 06:34:35 | [diff] [blame] | 179 | proxy_task_runner_(proxy_task_runner), |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 180 | set_interface_id_namespace_bit_(set_interface_id_namespace_bit), |
Dave Tapuska | f2df43e | 2019-10-10 22:10:10 | [diff] [blame] | 181 | dispatcher_(this), |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 182 | control_message_handler_(this), |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 183 | control_message_proxy_thunk_(this), |
| 184 | control_message_proxy_(&control_message_proxy_thunk_){ |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 185 | control_message_handler_.SetDescription( |
Ken Rockot | cd23f75 | 2020-06-20 01:22:31 | [diff] [blame] | 186 | "IPC::mojom::Bootstrap [primary] PipeControlMessageHandler"); |
Dave Tapuska | f2df43e | 2019-10-10 22:10:10 | [diff] [blame] | 187 | dispatcher_.SetValidator(std::make_unique<mojo::MessageHeaderValidator>( |
Ken Rockot | cd23f75 | 2020-06-20 01:22:31 | [diff] [blame] | 188 | "IPC::mojom::Bootstrap [primary] MessageHeaderValidator")); |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 189 | |
| 190 | GetMemoryDumpProvider().AddController(this); |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 191 | |
| 192 | DETACH_FROM_SEQUENCE(sequence_checker_); |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 193 | } |
| 194 | |
Peter Boström | 896f137 | 2021-11-05 01:12:30 | [diff] [blame] | 195 | ChannelAssociatedGroupController(constChannelAssociatedGroupController&)= |
| 196 | delete; |
| 197 | ChannelAssociatedGroupController&operator=( |
| 198 | constChannelAssociatedGroupController&)=delete; |
| 199 | |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 200 | size_tGetQueuedMessageCount(){ |
| 201 | base::AutoLock lock(outgoing_messages_lock_); |
| 202 | return outgoing_messages_.size(); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 203 | } |
| 204 | |
Siddhartha S | 0348442 | 2019-04-23 20:30:00 | [diff] [blame] | 205 | voidGetTopQueuedMessageMemoryDumpInfo(MessageMemoryDumpInfo* info, |
| 206 | size_t* count){ |
| 207 | std::unordered_map<MessageMemoryDumpInfo,size_t,MessageMemoryDumpInfoHash> |
| 208 | counts; |
| 209 | std::pair<MessageMemoryDumpInfo,size_t> top_message_info_and_count={ |
| 210 | MessageMemoryDumpInfo(),0}; |
Siddhartha | d1cfec1 | 2018-09-17 21:42:15 | [diff] [blame] | 211 | base::AutoLock lock(outgoing_messages_lock_); |
| 212 | for(constauto& message: outgoing_messages_){ |
Siddhartha S | 0348442 | 2019-04-23 20:30:00 | [diff] [blame] | 213 | auto it_and_inserted= counts.emplace(MessageMemoryDumpInfo(message),0); |
Siddhartha | d1cfec1 | 2018-09-17 21:42:15 | [diff] [blame] | 214 | it_and_inserted.first->second++; |
Siddhartha S | 0348442 | 2019-04-23 20:30:00 | [diff] [blame] | 215 | if(it_and_inserted.first->second> top_message_info_and_count.second) |
| 216 | top_message_info_and_count=*it_and_inserted.first; |
Siddhartha | d1cfec1 | 2018-09-17 21:42:15 | [diff] [blame] | 217 | } |
Siddhartha S | 0348442 | 2019-04-23 20:30:00 | [diff] [blame] | 218 | *info= top_message_info_and_count.first; |
| 219 | *count= top_message_info_and_count.second; |
Siddhartha | d1cfec1 | 2018-09-17 21:42:15 | [diff] [blame] | 220 | } |
| 221 | |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 222 | voidPause(){ |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 223 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 224 | CHECK(was_bound_or_message_sent_); |
| 225 | CHECK(!paused_); |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 226 | paused_=true; |
| 227 | } |
| 228 | |
| 229 | voidUnpause(){ |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 230 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 231 | CHECK(was_bound_or_message_sent_); |
| 232 | CHECK(paused_); |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 233 | paused_=false; |
rockot | 401fb2c | 2016-09-06 18:35:57 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | voidFlushOutgoingMessages(){ |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 237 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 238 | CHECK(was_bound_or_message_sent_); |
| 239 | |
rockot | c4cc691e | 2016-08-19 18:48:57 | [diff] [blame] | 240 | std::vector<mojo::Message> outgoing_messages; |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 241 | { |
| 242 | base::AutoLock lock(outgoing_messages_lock_); |
| 243 | std::swap(outgoing_messages, outgoing_messages_); |
| 244 | } |
Sigurdur Asgeirsson | d655dd65f | 2019-11-12 19:32:20 | [diff] [blame] | 245 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 246 | for(auto& message: outgoing_messages) |
rockot | c4cc691e | 2016-08-19 18:48:57 | [diff] [blame] | 247 | SendMessage(&message); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 248 | } |
| 249 | |
Ken Rockot | 9fae8bcd | 2021-06-04 22:09:49 | [diff] [blame] | 250 | voidBind(mojo::ScopedMessagePipeHandle handle, |
| 251 | mojo::PendingAssociatedRemote<mojom::Channel>* sender, |
| 252 | mojo::PendingAssociatedReceiver<mojom::Channel>* receiver){ |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 253 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 254 | |
Ken Rockot | 9fae8bcd | 2021-06-04 22:09:49 | [diff] [blame] | 255 | connector_= std::make_unique<mojo::Connector>( |
| 256 | std::move(handle), mojo::Connector::SINGLE_THREADED_SEND, |
| 257 | "IPC Channel"); |
| 258 | connector_->set_incoming_receiver(&dispatcher_); |
| 259 | connector_->set_connection_error_handler( |
| 260 | base::BindOnce(&ChannelAssociatedGroupController::OnPipeError, |
| 261 | base::Unretained(this))); |
| 262 | connector_->set_enforce_errors_from_incoming_receiver(false); |
Ken Rockot | 9fae8bcd | 2021-06-04 22:09:49 | [diff] [blame] | 263 | |
| 264 | // Don't let the Connector do any sort of queuing on our behalf. Individual |
| 265 | // messages bound for the IPC::ChannelProxy thread (i.e. that vast majority |
| 266 | // of messages received by this Connector) are already individually |
| 267 | // scheduled for dispatch by ChannelProxy, so Connector's normal mode of |
| 268 | // operation would only introduce a redundant scheduling step for most |
| 269 | // messages. |
| 270 | connector_->set_force_immediate_dispatch(true); |
| 271 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 272 | mojo::InterfaceId sender_id, receiver_id; |
| 273 | if(set_interface_id_namespace_bit_){ |
| 274 | sender_id=1| mojo::kInterfaceIdNamespaceMask; |
| 275 | receiver_id=1; |
| 276 | }else{ |
| 277 | sender_id=1; |
| 278 | receiver_id=1| mojo::kInterfaceIdNamespaceMask; |
| 279 | } |
| 280 | |
| 281 | { |
| 282 | base::AutoLock locker(lock_); |
| 283 | Endpoint* sender_endpoint=newEndpoint(this, sender_id); |
| 284 | Endpoint* receiver_endpoint=newEndpoint(this, receiver_id); |
| 285 | endpoints_.insert({ sender_id, sender_endpoint}); |
| 286 | endpoints_.insert({ receiver_id, receiver_endpoint}); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 287 | sender_endpoint->set_handle_created(); |
| 288 | receiver_endpoint->set_handle_created(); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | mojo::ScopedInterfaceEndpointHandle sender_handle= |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 292 | CreateScopedInterfaceEndpointHandle(sender_id); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 293 | mojo::ScopedInterfaceEndpointHandle receiver_handle= |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 294 | CreateScopedInterfaceEndpointHandle(receiver_id); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 295 | |
Ken Rockot | fbbd444 | 2021-06-04 01:19:14 | [diff] [blame] | 296 | *sender= mojo::PendingAssociatedRemote<mojom::Channel>( |
| 297 | std::move(sender_handle),0); |
Julie Jeongeun Kim | 903b34b | 2019-09-25 11:11:54 | [diff] [blame] | 298 | *receiver= mojo::PendingAssociatedReceiver<mojom::Channel>( |
| 299 | std::move(receiver_handle)); |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 300 | |
| 301 | if(!was_bound_or_message_sent_){ |
| 302 | was_bound_or_message_sent_=true; |
| 303 | DETACH_FROM_SEQUENCE(sequence_checker_); |
| 304 | } |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 305 | } |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 306 | |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 307 | voidStartReceiving(){ |
| 308 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 309 | CHECK(was_bound_or_message_sent_); |
| 310 | connector_->StartReceiving(task_runner_); |
| 311 | } |
Ken Rockot | 9fae8bcd | 2021-06-04 22:09:49 | [diff] [blame] | 312 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 313 | voidShutDown(){ |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 314 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
Ken Rockot | 3e7284bb | 2018-02-06 16:11:16 | [diff] [blame] | 315 | shut_down_=true; |
Bruce Dawson | a40d4847 | 2020-04-20 20:57:24 | [diff] [blame] | 316 | if(connector_) |
| 317 | connector_->CloseMessagePipe(); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 318 | OnPipeError(); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 319 | connector_.reset(); |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 320 | |
| 321 | base::AutoLock lock(outgoing_messages_lock_); |
Ken Rockot | 3e7284bb | 2018-02-06 16:11:16 | [diff] [blame] | 322 | outgoing_messages_.clear(); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | // mojo::AssociatedGroupController: |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 326 | mojo::InterfaceIdAssociateInterface( |
| 327 | mojo::ScopedInterfaceEndpointHandle handle_to_send) override{ |
| 328 | if(!handle_to_send.pending_association()) |
| 329 | return mojo::kInvalidInterfaceId; |
| 330 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 331 | uint32_t id=0; |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 332 | { |
| 333 | base::AutoLock locker(lock_); |
| 334 | do{ |
| 335 | if(next_interface_id_>= mojo::kInterfaceIdNamespaceMask) |
| 336 | next_interface_id_=2; |
| 337 | id= next_interface_id_++; |
| 338 | if(set_interface_id_namespace_bit_) |
| 339 | id|= mojo::kInterfaceIdNamespaceMask; |
Jan Wilken Dörrie | 73c901e | 2019-06-12 09:02:32 | [diff] [blame] | 340 | }while(base::Contains(endpoints_, id)); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 341 | |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 342 | Endpoint* endpoint=newEndpoint(this, id); |
| 343 | if(encountered_error_) |
| 344 | endpoint->set_peer_closed(); |
| 345 | endpoint->set_handle_created(); |
| 346 | endpoints_.insert({id, endpoint}); |
| 347 | } |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 348 | |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 349 | if(!NotifyAssociation(&handle_to_send, id)){ |
| 350 | // The peer handle of |handle_to_send|, which is supposed to join this |
| 351 | // associated group, has been closed. |
| 352 | { |
| 353 | base::AutoLock locker(lock_); |
| 354 | Endpoint* endpoint=FindEndpoint(id); |
| 355 | if(endpoint) |
| 356 | MarkClosedAndMaybeRemove(endpoint); |
| 357 | } |
| 358 | |
| 359 | control_message_proxy_.NotifyPeerEndpointClosed( |
| 360 | id, handle_to_send.disconnect_reason()); |
| 361 | } |
| 362 | return id; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | mojo::ScopedInterfaceEndpointHandleCreateLocalEndpointHandle( |
| 366 | mojo::InterfaceId id) override{ |
| 367 | if(!mojo::IsValidInterfaceId(id)) |
| 368 | return mojo::ScopedInterfaceEndpointHandle(); |
| 369 | |
Ken Rockot | cd23f75 | 2020-06-20 01:22:31 | [diff] [blame] | 370 | // 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] | 371 | // its namespace bit is supposed to be different than the value that this |
| 372 | // router would use. |
Ken Rockot | cd23f75 | 2020-06-20 01:22:31 | [diff] [blame] | 373 | if(!mojo::IsPrimaryInterfaceId(id)&& |
Yuzhu Shen | 9f87fb0 | 2017-08-11 17:07:06 | [diff] [blame] | 374 | set_interface_id_namespace_bit_== |
| 375 | mojo::HasInterfaceIdNamespaceBitSet(id)){ |
| 376 | return mojo::ScopedInterfaceEndpointHandle(); |
| 377 | } |
| 378 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 379 | base::AutoLock locker(lock_); |
| 380 | bool inserted=false; |
| 381 | Endpoint* endpoint=FindOrInsertEndpoint(id,&inserted); |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 382 | if(inserted){ |
| 383 | DCHECK(!endpoint->handle_created()); |
| 384 | if(encountered_error_) |
| 385 | endpoint->set_peer_closed(); |
| 386 | }else{ |
| 387 | if(endpoint->handle_created()) |
| 388 | return mojo::ScopedInterfaceEndpointHandle(); |
| 389 | } |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 390 | |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 391 | endpoint->set_handle_created(); |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 392 | returnCreateScopedInterfaceEndpointHandle(id); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 393 | } |
| 394 | |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 395 | voidCloseEndpointHandle( |
| 396 | mojo::InterfaceId id, |
Arthur Sonzogni | 59ac822 | 2023-11-10 09:46:54 | [diff] [blame] | 397 | const std::optional<mojo::DisconnectReason>& reason) override{ |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 398 | if(!mojo::IsValidInterfaceId(id)) |
| 399 | return; |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 400 | { |
| 401 | base::AutoLock locker(lock_); |
Jan Wilken Dörrie | 73c901e | 2019-06-12 09:02:32 | [diff] [blame] | 402 | DCHECK(base::Contains(endpoints_, id)); |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 403 | Endpoint* endpoint= endpoints_[id].get(); |
| 404 | DCHECK(!endpoint->client()); |
| 405 | DCHECK(!endpoint->closed()); |
| 406 | MarkClosedAndMaybeRemove(endpoint); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 407 | } |
| 408 | |
Ken Rockot | cd23f75 | 2020-06-20 01:22:31 | [diff] [blame] | 409 | if(!mojo::IsPrimaryInterfaceId(id)|| reason) |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 410 | control_message_proxy_.NotifyPeerEndpointClosed(id, reason); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 411 | } |
| 412 | |
Ken Rockot | c1b27ca | 2024-04-13 00:37:39 | [diff] [blame] | 413 | voidNotifyLocalEndpointOfPeerClosure(mojo::InterfaceId id) override{ |
Ken Rockot | c1b27ca | 2024-04-13 00:37:39 | [diff] [blame] | 414 | if(!task_runner_->RunsTasksInCurrentSequence()){ |
| 415 | task_runner_->PostTask( |
| 416 | FROM_HERE, base::BindOnce(&ChannelAssociatedGroupController:: |
| 417 | NotifyLocalEndpointOfPeerClosure, |
| 418 | base::WrapRefCounted(this), id)); |
| 419 | return; |
| 420 | } |
| 421 | OnPeerAssociatedEndpointClosed(id, std::nullopt); |
| 422 | } |
| 423 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 424 | mojo::InterfaceEndpointController*AttachEndpointClient( |
| 425 | const mojo::ScopedInterfaceEndpointHandle& handle, |
| 426 | mojo::InterfaceEndpointClient* client, |
Sam McNally | de5ae67 | 2017-06-19 23:34:45 | [diff] [blame] | 427 | scoped_refptr<base::SequencedTaskRunner> runner) override{ |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 428 | const mojo::InterfaceId id= handle.id(); |
| 429 | |
| 430 | DCHECK(mojo::IsValidInterfaceId(id)); |
| 431 | DCHECK(client); |
| 432 | |
| 433 | base::AutoLock locker(lock_); |
Jan Wilken Dörrie | 73c901e | 2019-06-12 09:02:32 | [diff] [blame] | 434 | DCHECK(base::Contains(endpoints_, id)); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 435 | |
| 436 | Endpoint* endpoint= endpoints_[id].get(); |
| 437 | endpoint->AttachClient(client, std::move(runner)); |
| 438 | |
| 439 | if(endpoint->peer_closed()) |
| 440 | NotifyEndpointOfError(endpoint,true/* force_async */); |
| 441 | |
| 442 | return endpoint; |
| 443 | } |
| 444 | |
| 445 | voidDetachEndpointClient( |
| 446 | const mojo::ScopedInterfaceEndpointHandle& handle) override{ |
| 447 | const mojo::InterfaceId id= handle.id(); |
| 448 | |
| 449 | DCHECK(mojo::IsValidInterfaceId(id)); |
| 450 | |
| 451 | base::AutoLock locker(lock_); |
Jan Wilken Dörrie | 73c901e | 2019-06-12 09:02:32 | [diff] [blame] | 452 | DCHECK(base::Contains(endpoints_, id)); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 453 | |
| 454 | Endpoint* endpoint= endpoints_[id].get(); |
| 455 | endpoint->DetachClient(); |
| 456 | } |
| 457 | |
| 458 | voidRaiseError() override{ |
Ken Rockot | 138153b | 2018-07-13 23:31:57 | [diff] [blame] | 459 | // We ignore errors on channel endpoints, leaving the pipe open. There are |
| 460 | // good reasons for this: |
| 461 | // |
| 462 | // * We should never close a channel endpoint in either process as long as |
| 463 | // the child process is still alive. The child's endpoint should only be |
| 464 | // closed implicitly by process death, and the browser's endpoint should |
| 465 | // only be closed after the child process is confirmed to be dead. Crash |
| 466 | // reporting logic in Chrome relies on this behavior in order to do the |
| 467 | // right thing. |
| 468 | // |
| 469 | // * There are two interesting conditions under which RaiseError() can be |
| 470 | // implicitly reached: an incoming message fails validation, or the |
| 471 | // local endpoint drops a response callback without calling it. |
| 472 | // |
| 473 | // * In the validation case, we also report the message as bad, and this |
| 474 | // will imminently trigger the common bad-IPC path in the browser, |
| 475 | // causing the browser to kill the offending renderer. |
| 476 | // |
| 477 | // * In the dropped response callback case, the net result of ignoring the |
| 478 | // issue is generally innocuous. While indicative of programmer error, |
| 479 | // it's not a severe failure and is already covered by separate DCHECKs. |
| 480 | // |
| 481 | // See https://crbug.com/861607 for additional discussion. |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 482 | } |
| 483 | |
Ken Rockot | 474df014 | 2017-07-12 13:28:56 | [diff] [blame] | 484 | boolPrefersSerializedMessages() override{returntrue;} |
| 485 | |
Scott Haseley | cf0f172 | 2023-11-03 21:39:01 | [diff] [blame] | 486 | voidSetUrgentMessageObserver(UrgentMessageObserver* observer){ |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 487 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 488 | CHECK(!was_bound_or_message_sent_); |
Scott Haseley | cf0f172 | 2023-11-03 21:39:01 | [diff] [blame] | 489 | urgent_message_observer_= observer; |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 490 | DETACH_FROM_SEQUENCE(sequence_checker_); |
Scott Haseley | cf0f172 | 2023-11-03 21:39:01 | [diff] [blame] | 491 | } |
| 492 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 493 | private: |
| 494 | classEndpoint; |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 495 | classControlMessageProxyThunk; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 496 | friendclassEndpoint; |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 497 | friendclassControlMessageProxyThunk; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 498 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 499 | // MessageWrapper objects are always destroyed under the controller's lock. On |
| 500 | // destruction, if the message it wrappers contains |
| 501 | // ScopedInterfaceEndpointHandles (which cannot be destructed under the |
| 502 | // controller's lock), the wrapper unlocks to clean them up. |
| 503 | classMessageWrapper{ |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 504 | public: |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 505 | MessageWrapper()=default; |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 506 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 507 | MessageWrapper(ChannelAssociatedGroupController* controller, |
| 508 | mojo::Message message) |
| 509 | : controller_(controller), value_(std::move(message)){} |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 510 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 511 | MessageWrapper(MessageWrapper&& other) |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 512 | : controller_(other.controller_), value_(std::move(other.value_)){} |
| 513 | |
Peter Boström | c68c5aa | 2021-09-28 00:28:00 | [diff] [blame] | 514 | MessageWrapper(constMessageWrapper&)=delete; |
| 515 | MessageWrapper&operator=(constMessageWrapper&)=delete; |
| 516 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 517 | ~MessageWrapper(){ |
| 518 | if(value_.associated_endpoint_handles()->empty()) |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 519 | return; |
| 520 | |
| 521 | controller_->lock_.AssertAcquired(); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 522 | { |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 523 | base::AutoUnlock unlocker(controller_->lock_); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 524 | value_.mutable_associated_endpoint_handles()->clear(); |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 528 | MessageWrapper&operator=(MessageWrapper&& other){ |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 529 | controller_= other.controller_; |
| 530 | value_= std::move(other.value_); |
| 531 | return*this; |
| 532 | } |
| 533 | |
Ken Rockot | b6f9212 | 2022-10-31 04:52:24 | [diff] [blame] | 534 | boolHasRequestId(uint64_t request_id){ |
| 535 | return!value_.IsNull()&& value_.version()>=1&& |
| 536 | value_.header_v1()->request_id== request_id; |
| 537 | } |
| 538 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 539 | mojo::Message& value(){return value_;} |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 540 | |
| 541 | private: |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 542 | raw_ptr<ChannelAssociatedGroupController> controller_=nullptr; |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 543 | mojo::Message value_; |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 544 | }; |
| 545 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 546 | classEndpoint:public base::RefCountedThreadSafe<Endpoint>, |
| 547 | public mojo::InterfaceEndpointController{ |
| 548 | public: |
| 549 | Endpoint(ChannelAssociatedGroupController* controller, mojo::InterfaceId id) |
| 550 | : controller_(controller), id_(id){} |
| 551 | |
Peter Boström | 896f137 | 2021-11-05 01:12:30 | [diff] [blame] | 552 | Endpoint(constEndpoint&)=delete; |
| 553 | Endpoint&operator=(constEndpoint&)=delete; |
| 554 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 555 | mojo::InterfaceId id()const{return id_;} |
| 556 | |
| 557 | bool closed()const{ |
| 558 | controller_->lock_.AssertAcquired(); |
| 559 | return closed_; |
| 560 | } |
| 561 | |
| 562 | void set_closed(){ |
| 563 | controller_->lock_.AssertAcquired(); |
| 564 | closed_=true; |
| 565 | } |
| 566 | |
| 567 | bool peer_closed()const{ |
| 568 | controller_->lock_.AssertAcquired(); |
| 569 | return peer_closed_; |
| 570 | } |
| 571 | |
| 572 | void set_peer_closed(){ |
| 573 | controller_->lock_.AssertAcquired(); |
| 574 | peer_closed_=true; |
| 575 | } |
| 576 | |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 577 | bool handle_created()const{ |
| 578 | controller_->lock_.AssertAcquired(); |
| 579 | return handle_created_; |
| 580 | } |
| 581 | |
| 582 | void set_handle_created(){ |
| 583 | controller_->lock_.AssertAcquired(); |
| 584 | handle_created_=true; |
| 585 | } |
| 586 | |
Arthur Sonzogni | 59ac822 | 2023-11-10 09:46:54 | [diff] [blame] | 587 | const std::optional<mojo::DisconnectReason>& disconnect_reason()const{ |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 588 | return disconnect_reason_; |
| 589 | } |
| 590 | |
| 591 | void set_disconnect_reason( |
Arthur Sonzogni | 59ac822 | 2023-11-10 09:46:54 | [diff] [blame] | 592 | const std::optional<mojo::DisconnectReason>& disconnect_reason){ |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 593 | disconnect_reason_= disconnect_reason; |
| 594 | } |
| 595 | |
Sam McNally | de5ae67 | 2017-06-19 23:34:45 | [diff] [blame] | 596 | base::SequencedTaskRunner* task_runner()const{ |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 597 | return task_runner_.get(); |
| 598 | } |
| 599 | |
Ken Rockot | c5cc0273 | 2021-06-04 08:02:41 | [diff] [blame] | 600 | bool was_bound_off_sequence()const{return was_bound_off_sequence_;} |
| 601 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 602 | mojo::InterfaceEndpointClient* client()const{ |
| 603 | controller_->lock_.AssertAcquired(); |
| 604 | return client_; |
| 605 | } |
| 606 | |
| 607 | voidAttachClient(mojo::InterfaceEndpointClient* client, |
Sam McNally | de5ae67 | 2017-06-19 23:34:45 | [diff] [blame] | 608 | scoped_refptr<base::SequencedTaskRunner> runner){ |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 609 | controller_->lock_.AssertAcquired(); |
| 610 | DCHECK(!client_); |
| 611 | DCHECK(!closed_); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 612 | |
| 613 | task_runner_= std::move(runner); |
| 614 | client_= client; |
Ken Rockot | c5cc0273 | 2021-06-04 08:02:41 | [diff] [blame] | 615 | |
Peter Kasting | 525a014 | 2023-03-08 21:56:27 | [diff] [blame] | 616 | if(off_sequence_binding_allowed){ |
Ken Rockot | c5cc0273 | 2021-06-04 08:02:41 | [diff] [blame] | 617 | was_bound_off_sequence_=true; |
Peter Kasting | 525a014 | 2023-03-08 21:56:27 | [diff] [blame] | 618 | } |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | voidDetachClient(){ |
| 622 | controller_->lock_.AssertAcquired(); |
| 623 | DCHECK(client_); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 624 | DCHECK(!closed_); |
| 625 | |
| 626 | task_runner_=nullptr; |
| 627 | client_=nullptr; |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 628 | sync_watcher_.reset(); |
| 629 | } |
| 630 | |
Arthur Sonzogni | 59ac822 | 2023-11-10 09:46:54 | [diff] [blame] | 631 | std::optional<uint32_t>EnqueueSyncMessage(MessageWrapper message){ |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 632 | controller_->lock_.AssertAcquired(); |
Ken Rockot | b6f9212 | 2022-10-31 04:52:24 | [diff] [blame] | 633 | if(exclusive_wait_&& exclusive_wait_->TryFulfillingWith(message)){ |
| 634 | exclusive_wait_=nullptr; |
Arthur Sonzogni | 59ac822 | 2023-11-10 09:46:54 | [diff] [blame] | 635 | return std::nullopt; |
Ken Rockot | b6f9212 | 2022-10-31 04:52:24 | [diff] [blame] | 636 | } |
| 637 | |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 638 | uint32_t id=GenerateSyncMessageId(); |
Ken Rockot | b6f9212 | 2022-10-31 04:52:24 | [diff] [blame] | 639 | sync_messages_.emplace_back(id, std::move(message)); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 640 | SignalSyncMessageEvent(); |
| 641 | return id; |
| 642 | } |
| 643 | |
| 644 | voidSignalSyncMessageEvent(){ |
| 645 | controller_->lock_.AssertAcquired(); |
yzshen | e25b5d5 | 2017-02-28 21:56:31 | [diff] [blame] | 646 | |
Ken Rockot | aa20dcc | 2018-03-28 03:06:51 | [diff] [blame] | 647 | if(sync_watcher_) |
| 648 | sync_watcher_->SignalEvent(); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 649 | } |
| 650 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 651 | MessageWrapperPopSyncMessage(uint32_t id){ |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 652 | controller_->lock_.AssertAcquired(); |
| 653 | if(sync_messages_.empty()|| sync_messages_.front().first!= id) |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 654 | returnMessageWrapper(); |
| 655 | MessageWrapper message= std::move(sync_messages_.front().second); |
Ken Rockot | b6f9212 | 2022-10-31 04:52:24 | [diff] [blame] | 656 | sync_messages_.pop_front(); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 657 | return message; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | // mojo::InterfaceEndpointController: |
| 661 | boolSendMessage(mojo::Message* message) override{ |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 662 | DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 663 | message->set_interface_id(id_); |
| 664 | return controller_->SendMessage(message); |
| 665 | } |
| 666 | |
| 667 | voidAllowWokenUpBySyncWatchOnSameThread() override{ |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 668 | DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 669 | |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 670 | EnsureSyncWatcherExists(); |
Ken Rockot | aa20dcc | 2018-03-28 03:06:51 | [diff] [blame] | 671 | sync_watcher_->AllowWokenUpBySyncWatchOnSameSequence(); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 672 | } |
| 673 | |
Ken Rockot | 5b3136c | 2021-04-26 17:45:59 | [diff] [blame] | 674 | boolSyncWatch(constbool& should_stop) override{ |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 675 | DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 676 | |
Ken Rockot | cd23f75 | 2020-06-20 01:22:31 | [diff] [blame] | 677 | // It's not legal to make sync calls from the primary endpoint's thread, |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 678 | // and in fact they must only happen from the proxy task runner. |
rockot | 7604e7b7 | 2016-07-28 17:37:39 | [diff] [blame] | 679 | DCHECK(!controller_->task_runner_->BelongsToCurrentThread()); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 680 | DCHECK(controller_->proxy_task_runner_->BelongsToCurrentThread()); |
| 681 | |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 682 | EnsureSyncWatcherExists(); |
Xuehui Xie | 32708269 | 2023-06-21 16:08:04 | [diff] [blame] | 683 | { |
| 684 | base::AutoLock locker(controller_->lock_); |
| 685 | if(peer_closed_){ |
| 686 | SignalSyncMessageEvent(); |
| 687 | } |
| 688 | } |
Ken Rockot | 5427297 | 2021-04-20 22:08:04 | [diff] [blame] | 689 | return sync_watcher_->SyncWatch(&should_stop); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 690 | } |
| 691 | |
Ken Rockot | b6f9212 | 2022-10-31 04:52:24 | [diff] [blame] | 692 | MessageWrapperWaitForIncomingSyncReply(uint64_t request_id){ |
Arthur Sonzogni | 59ac822 | 2023-11-10 09:46:54 | [diff] [blame] | 693 | std::optional<ExclusiveSyncWait> wait; |
Ken Rockot | b6f9212 | 2022-10-31 04:52:24 | [diff] [blame] | 694 | { |
| 695 | base::AutoLock lock(controller_->lock_); |
| 696 | for(auto&[id, message]: sync_messages_){ |
| 697 | if(message.HasRequestId(request_id)){ |
| 698 | return std::move(message); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | DCHECK(!exclusive_wait_); |
| 703 | wait.emplace(request_id); |
| 704 | exclusive_wait_=&wait.value(); |
| 705 | } |
| 706 | |
| 707 | wait->event.Wait(); |
| 708 | return std::move(wait->message); |
| 709 | } |
| 710 | |
Ken Rockot | 5b3136c | 2021-04-26 17:45:59 | [diff] [blame] | 711 | boolSyncWatchExclusive(uint64_t request_id) override{ |
Ken Rockot | b6f9212 | 2022-10-31 04:52:24 | [diff] [blame] | 712 | MessageWrapper message=WaitForIncomingSyncReply(request_id); |
| 713 | if(message.value().IsNull()||!client_){ |
| 714 | returnfalse; |
| 715 | } |
| 716 | |
| 717 | if(!client_->HandleIncomingMessage(&message.value())){ |
| 718 | base::AutoLock locker(controller_->lock_); |
| 719 | controller_->RaiseError(); |
| 720 | returnfalse; |
| 721 | } |
| 722 | |
| 723 | returntrue; |
Ken Rockot | 5b3136c | 2021-04-26 17:45:59 | [diff] [blame] | 724 | } |
| 725 | |
Ken Rockot | 9f69e16 | 2021-04-19 14:16:29 | [diff] [blame] | 726 | voidRegisterExternalSyncWaiter(uint64_t request_id) override{} |
| 727 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 728 | private: |
| 729 | friendclass base::RefCountedThreadSafe<Endpoint>; |
| 730 | |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 731 | ~Endpoint() override{ |
| 732 | controller_->lock_.AssertAcquired(); |
| 733 | DCHECK(!client_); |
| 734 | DCHECK(closed_); |
| 735 | DCHECK(peer_closed_); |
| 736 | DCHECK(!sync_watcher_); |
Ken Rockot | b6f9212 | 2022-10-31 04:52:24 | [diff] [blame] | 737 | if(exclusive_wait_){ |
| 738 | exclusive_wait_->event.Signal(); |
| 739 | } |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 740 | } |
| 741 | |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 742 | voidOnSyncMessageEventReady(){ |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 743 | DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 744 | |
Ken Rockot | 120b4b0 | 2022-12-01 01:44:05 | [diff] [blame] | 745 | // SUBTLE: The order of these scoped_refptrs matters. |
| 746 | // `controller_keepalive` MUST outlive `keepalive` because the Endpoint |
| 747 | // holds raw pointer to the AssociatedGroupController. |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 748 | scoped_refptr<AssociatedGroupController> controller_keepalive( |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 749 | controller_.get()); |
Ken Rockot | 120b4b0 | 2022-12-01 01:44:05 | [diff] [blame] | 750 | scoped_refptr<Endpoint> keepalive(this); |
Ken Rockot | aa20dcc | 2018-03-28 03:06:51 | [diff] [blame] | 751 | base::AutoLock locker(controller_->lock_); |
| 752 | bool more_to_process=false; |
| 753 | if(!sync_messages_.empty()){ |
| 754 | MessageWrapper message_wrapper= |
| 755 | std::move(sync_messages_.front().second); |
Ken Rockot | b6f9212 | 2022-10-31 04:52:24 | [diff] [blame] | 756 | sync_messages_.pop_front(); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 757 | |
Ken Rockot | aa20dcc | 2018-03-28 03:06:51 | [diff] [blame] | 758 | bool dispatch_succeeded; |
| 759 | mojo::InterfaceEndpointClient* client= client_; |
| 760 | { |
| 761 | base::AutoUnlock unlocker(controller_->lock_); |
| 762 | dispatch_succeeded= |
| 763 | client->HandleIncomingMessage(&message_wrapper.value()); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 764 | } |
| 765 | |
Ken Rockot | aa20dcc | 2018-03-28 03:06:51 | [diff] [blame] | 766 | if(!sync_messages_.empty()) |
| 767 | more_to_process=true; |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 768 | |
Ken Rockot | aa20dcc | 2018-03-28 03:06:51 | [diff] [blame] | 769 | if(!dispatch_succeeded) |
| 770 | controller_->RaiseError(); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 771 | } |
| 772 | |
Ken Rockot | aa20dcc | 2018-03-28 03:06:51 | [diff] [blame] | 773 | if(!more_to_process) |
| 774 | sync_watcher_->ResetEvent(); |
| 775 | |
| 776 | // If there are no queued sync messages and the peer has closed, there |
| 777 | // there won't be incoming sync messages in the future. If any |
| 778 | // SyncWatch() calls are on the stack for this endpoint, resetting the |
| 779 | // watcher will allow them to exit as the stack undwinds. |
| 780 | if(!more_to_process&& peer_closed_) |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 781 | sync_watcher_.reset(); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | voidEnsureSyncWatcherExists(){ |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 785 | DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 786 | if(sync_watcher_) |
| 787 | return; |
| 788 | |
Ken Rockot | aa20dcc | 2018-03-28 03:06:51 | [diff] [blame] | 789 | base::AutoLock locker(controller_->lock_); |
| 790 | sync_watcher_= std::make_unique<mojo::SequenceLocalSyncEventWatcher>( |
| 791 | base::BindRepeating(&Endpoint::OnSyncMessageEventReady, |
| 792 | base::Unretained(this))); |
| 793 | if(peer_closed_||!sync_messages_.empty()) |
| 794 | SignalSyncMessageEvent(); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | uint32_tGenerateSyncMessageId(){ |
| 798 | // Overflow is fine. |
| 799 | uint32_t id= next_sync_message_id_++; |
| 800 | DCHECK(sync_messages_.empty()|| sync_messages_.front().first!= id); |
| 801 | return id; |
| 802 | } |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 803 | |
Ken Rockot | b6f9212 | 2022-10-31 04:52:24 | [diff] [blame] | 804 | // Tracks the state of a pending sync wait which excludes all other incoming |
| 805 | // IPC on the waiting thread. |
| 806 | structExclusiveSyncWait{ |
| 807 | explicitExclusiveSyncWait(uint64_t request_id) |
| 808 | : request_id(request_id){} |
| 809 | ~ExclusiveSyncWait()=default; |
| 810 | |
| 811 | boolTryFulfillingWith(MessageWrapper& wrapper){ |
| 812 | if(!wrapper.HasRequestId(request_id)){ |
| 813 | returnfalse; |
| 814 | } |
| 815 | |
| 816 | message= std::move(wrapper); |
| 817 | event.Signal(); |
| 818 | returntrue; |
| 819 | } |
| 820 | |
| 821 | uint64_t request_id; |
| 822 | base::WaitableEvent event; |
| 823 | MessageWrapper message; |
| 824 | }; |
| 825 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 826 | const raw_ptr<ChannelAssociatedGroupController> controller_; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 827 | const mojo::InterfaceId id_; |
| 828 | |
| 829 | bool closed_=false; |
| 830 | bool peer_closed_=false; |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 831 | bool handle_created_=false; |
Ken Rockot | c5cc0273 | 2021-06-04 08:02:41 | [diff] [blame] | 832 | bool was_bound_off_sequence_=false; |
Arthur Sonzogni | 59ac822 | 2023-11-10 09:46:54 | [diff] [blame] | 833 | std::optional<mojo::DisconnectReason> disconnect_reason_; |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 834 | raw_ptr<mojo::InterfaceEndpointClient> client_=nullptr; |
Sam McNally | de5ae67 | 2017-06-19 23:34:45 | [diff] [blame] | 835 | scoped_refptr<base::SequencedTaskRunner> task_runner_; |
Ken Rockot | aa20dcc | 2018-03-28 03:06:51 | [diff] [blame] | 836 | std::unique_ptr<mojo::SequenceLocalSyncEventWatcher> sync_watcher_; |
Ken Rockot | b6f9212 | 2022-10-31 04:52:24 | [diff] [blame] | 837 | base::circular_deque<std::pair<uint32_t,MessageWrapper>> sync_messages_; |
Keishi Hattori | c1b0023 | 2022-11-22 09:04:26 | [diff] [blame] | 838 | raw_ptr<ExclusiveSyncWait> exclusive_wait_=nullptr; |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 839 | uint32_t next_sync_message_id_=0; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 840 | }; |
| 841 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 842 | classControlMessageProxyThunk:publicMessageReceiver{ |
| 843 | public: |
| 844 | explicitControlMessageProxyThunk( |
| 845 | ChannelAssociatedGroupController* controller) |
| 846 | : controller_(controller){} |
| 847 | |
Peter Boström | 896f137 | 2021-11-05 01:12:30 | [diff] [blame] | 848 | ControlMessageProxyThunk(constControlMessageProxyThunk&)=delete; |
| 849 | ControlMessageProxyThunk&operator=(constControlMessageProxyThunk&)= |
| 850 | delete; |
| 851 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 852 | private: |
| 853 | // MessageReceiver: |
| 854 | boolAccept(mojo::Message* message) override{ |
| 855 | return controller_->SendMessage(message); |
| 856 | } |
| 857 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 858 | raw_ptr<ChannelAssociatedGroupController> controller_; |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 859 | }; |
| 860 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 861 | ~ChannelAssociatedGroupController() override{ |
rockot | b01ef6a | 2016-07-27 03:24:32 | [diff] [blame] | 862 | DCHECK(!connector_); |
| 863 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 864 | base::AutoLock locker(lock_); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 865 | for(auto iter= endpoints_.begin(); iter!= endpoints_.end();){ |
| 866 | Endpoint* endpoint= iter->second.get(); |
| 867 | ++iter; |
| 868 | |
yzshen | e003d59 | 2017-01-24 21:42:17 | [diff] [blame] | 869 | if(!endpoint->closed()){ |
| 870 | // This happens when a NotifyPeerEndpointClosed message been received, |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 871 | // but the interface ID hasn't been used to create local endpoint |
| 872 | // handle. |
yzshen | e003d59 | 2017-01-24 21:42:17 | [diff] [blame] | 873 | DCHECK(!endpoint->client()); |
| 874 | DCHECK(endpoint->peer_closed()); |
Ken Rockot | cd4c1f1 | 2023-11-08 23:22:16 | [diff] [blame] | 875 | MarkClosed(endpoint); |
yzshen | e003d59 | 2017-01-24 21:42:17 | [diff] [blame] | 876 | }else{ |
Ken Rockot | cd4c1f1 | 2023-11-08 23:22:16 | [diff] [blame] | 877 | MarkPeerClosed(endpoint); |
yzshen | e003d59 | 2017-01-24 21:42:17 | [diff] [blame] | 878 | } |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 879 | } |
Ken Rockot | cd4c1f1 | 2023-11-08 23:22:16 | [diff] [blame] | 880 | endpoints_.clear(); |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 881 | |
| 882 | GetMemoryDumpProvider().RemoveController(this); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | boolSendMessage(mojo::Message* message){ |
Siddhartha S | 0348442 | 2019-04-23 20:30:00 | [diff] [blame] | 886 | DCHECK(message->heap_profiler_tag()); |
rockot | 7604e7b7 | 2016-07-28 17:37:39 | [diff] [blame] | 887 | if(task_runner_->BelongsToCurrentThread()){ |
François Doray | fad1b8b | 2024-01-04 20:22:53 | [diff] [blame] | 888 | returnSendMessageOnSequence(message); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 889 | } |
François Doray | fad1b8b | 2024-01-04 20:22:53 | [diff] [blame] | 890 | |
| 891 | // PostTask (or RunOrPostTask) so that `message` is sent after messages from |
| 892 | // tasks that are already queued (e.g. by `IPC::ChannelProxy::Send`). |
| 893 | auto callback= base::BindOnce( |
| 894 | &ChannelAssociatedGroupController::SendMessageOnSequenceViaTask,this, |
| 895 | std::move(*message)); |
| 896 | if(base::FeatureList::IsEnabled( |
| 897 | kMojoChannelAssociatedSendUsesRunOrPostTask)){ |
| 898 | task_runner_->RunOrPostTask(base::subtle::RunOrPostTaskPassKey(), |
| 899 | FROM_HERE, std::move(callback)); |
| 900 | }else{ |
| 901 | task_runner_->PostTask(FROM_HERE, std::move(callback)); |
| 902 | } |
| 903 | |
| 904 | returntrue; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 905 | } |
| 906 | |
François Doray | fad1b8b | 2024-01-04 20:22:53 | [diff] [blame] | 907 | boolSendMessageOnSequence(mojo::Message* message){ |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 908 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 909 | was_bound_or_message_sent_=true; |
| 910 | |
François Doray | fad1b8b | 2024-01-04 20:22:53 | [diff] [blame] | 911 | if(!connector_|| paused_){ |
| 912 | if(!shut_down_){ |
| 913 | base::AutoLock lock(outgoing_messages_lock_); |
| 914 | outgoing_messages_.emplace_back(std::move(*message)); |
| 915 | } |
| 916 | returntrue; |
| 917 | } |
François Doray | 927a082 | 2024-05-15 21:57:28 | [diff] [blame] | 918 | |
François Doray | fb40835 | 2024-10-29 17:11:59 | [diff] [blame] | 919 | MojoResult result= connector_->AcceptAndGetResult(message); |
| 920 | if(result== MOJO_RESULT_OK){ |
| 921 | returntrue; |
François Doray | 927a082 | 2024-05-15 21:57:28 | [diff] [blame] | 922 | } |
| 923 | |
Daniel Cheng | 84e7ad4 | 2025-05-26 23:02:27 | [diff] [blame] | 924 | CHECK(connector_->encountered_error()); |
François Doray | fb40835 | 2024-10-29 17:11:59 | [diff] [blame] | 925 | returnfalse; |
François Doray | fad1b8b | 2024-01-04 20:22:53 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | voidSendMessageOnSequenceViaTask(mojo::Message message){ |
| 929 | if(!SendMessageOnSequence(&message)){ |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 930 | RaiseError(); |
François Doray | fad1b8b | 2024-01-04 20:22:53 | [diff] [blame] | 931 | } |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | voidOnPipeError(){ |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 935 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 936 | |
| 937 | // We keep |this| alive here because it's possible for the notifications |
| 938 | // below to release all other references. |
| 939 | scoped_refptr<ChannelAssociatedGroupController> keepalive(this); |
| 940 | |
| 941 | base::AutoLock locker(lock_); |
| 942 | encountered_error_=true; |
| 943 | |
Ken Rockot | cd4c1f1 | 2023-11-08 23:22:16 | [diff] [blame] | 944 | std::vector<uint32_t> endpoints_to_remove; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 945 | std::vector<scoped_refptr<Endpoint>> endpoints_to_notify; |
| 946 | for(auto iter= endpoints_.begin(); iter!= endpoints_.end();){ |
| 947 | Endpoint* endpoint= iter->second.get(); |
| 948 | ++iter; |
| 949 | |
Ken Rockot | cd4c1f1 | 2023-11-08 23:22:16 | [diff] [blame] | 950 | if(endpoint->client()){ |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 951 | endpoints_to_notify.push_back(endpoint); |
Ken Rockot | cd4c1f1 | 2023-11-08 23:22:16 | [diff] [blame] | 952 | } |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 953 | |
Ken Rockot | cd4c1f1 | 2023-11-08 23:22:16 | [diff] [blame] | 954 | if(MarkPeerClosed(endpoint)){ |
| 955 | endpoints_to_remove.push_back(endpoint->id()); |
| 956 | } |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | for(auto& endpoint: endpoints_to_notify){ |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 960 | // Because a notification may in turn detach any endpoint, we have to |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 961 | // check each client again here. |
| 962 | if(endpoint->client()) |
| 963 | NotifyEndpointOfError(endpoint.get(),false/* force_async */); |
| 964 | } |
Ken Rockot | cd4c1f1 | 2023-11-08 23:22:16 | [diff] [blame] | 965 | |
| 966 | for(uint32_t id: endpoints_to_remove){ |
| 967 | endpoints_.erase(id); |
| 968 | } |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 969 | } |
| 970 | |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 971 | voidNotifyEndpointOfError(Endpoint* endpoint,bool force_async) |
| 972 | EXCLUSIVE_LOCKS_REQUIRED(lock_){ |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 973 | DCHECK(endpoint->task_runner()&& endpoint->client()); |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 974 | if(endpoint->task_runner()->RunsTasksInCurrentSequence()&&!force_async){ |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 975 | mojo::InterfaceEndpointClient* client= endpoint->client(); |
Arthur Sonzogni | 59ac822 | 2023-11-10 09:46:54 | [diff] [blame] | 976 | std::optional<mojo::DisconnectReason> reason( |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 977 | endpoint->disconnect_reason()); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 978 | |
| 979 | base::AutoUnlock unlocker(lock_); |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 980 | client->NotifyError(reason); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 981 | }else{ |
| 982 | endpoint->task_runner()->PostTask( |
Ari Chivukula | 2bf6f29 | 2022-11-28 18:29:46 | [diff] [blame] | 983 | FROM_HERE, |
| 984 | base::BindOnce(&ChannelAssociatedGroupController:: |
| 985 | NotifyEndpointOfErrorOnEndpointThread, |
| 986 | this, endpoint->id(), |
| 987 | // This is safe as `endpoint` is verified to be in |
| 988 | // `endpoints_` (a map with ownership) before use. |
| 989 | base::UnsafeDangling(endpoint))); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | |
Ari Chivukula | 2bf6f29 | 2022-11-28 18:29:46 | [diff] [blame] | 993 | // `endpoint` might be a dangling ptr and must be checked before dereference. |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 994 | voidNotifyEndpointOfErrorOnEndpointThread(mojo::InterfaceId id, |
Paul Semel | ff2ca67b | 2022-12-21 15:46:02 | [diff] [blame] | 995 | MayBeDangling<Endpoint> endpoint){ |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 996 | base::AutoLock locker(lock_); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 997 | auto iter= endpoints_.find(id); |
| 998 | if(iter== endpoints_.end()|| iter->second.get()!= endpoint) |
| 999 | return; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 1000 | if(!endpoint->client()) |
| 1001 | return; |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 1002 | |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 1003 | DCHECK(endpoint->task_runner()->RunsTasksInCurrentSequence()); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 1004 | NotifyEndpointOfError(endpoint,false/* force_async */); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 1005 | } |
| 1006 | |
Ken Rockot | cd4c1f1 | 2023-11-08 23:22:16 | [diff] [blame] | 1007 | // Marks `endpoint` as closed and returns true if and only if its peer was |
| 1008 | // also already closed. |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 1009 | boolMarkClosed(Endpoint* endpoint) EXCLUSIVE_LOCKS_REQUIRED(lock_){ |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 1010 | endpoint->set_closed(); |
Ken Rockot | cd4c1f1 | 2023-11-08 23:22:16 | [diff] [blame] | 1011 | return endpoint->peer_closed(); |
Ken Rockot | 6864546 | 2023-11-07 23:11:56 | [diff] [blame] | 1012 | } |
| 1013 | |
Ken Rockot | cd4c1f1 | 2023-11-08 23:22:16 | [diff] [blame] | 1014 | // Marks `endpoint` as having a closed peer and returns true if and only if |
| 1015 | // `endpoint` itself was also already closed. |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 1016 | boolMarkPeerClosed(Endpoint* endpoint) EXCLUSIVE_LOCKS_REQUIRED(lock_){ |
Igor Kraskevich | 0e42786 | 2023-11-08 10:13:53 | [diff] [blame] | 1017 | endpoint->set_peer_closed(); |
| 1018 | endpoint->SignalSyncMessageEvent(); |
Ken Rockot | cd4c1f1 | 2023-11-08 23:22:16 | [diff] [blame] | 1019 | return endpoint->closed(); |
| 1020 | } |
| 1021 | |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 1022 | voidMarkClosedAndMaybeRemove(Endpoint* endpoint) |
| 1023 | EXCLUSIVE_LOCKS_REQUIRED(lock_){ |
Ken Rockot | cd4c1f1 | 2023-11-08 23:22:16 | [diff] [blame] | 1024 | if(MarkClosed(endpoint)){ |
Ken Rockot | 6864546 | 2023-11-07 23:11:56 | [diff] [blame] | 1025 | endpoints_.erase(endpoint->id()); |
Ken Rockot | cd4c1f1 | 2023-11-08 23:22:16 | [diff] [blame] | 1026 | } |
| 1027 | } |
| 1028 | |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 1029 | voidMarkPeerClosedAndMaybeRemove(Endpoint* endpoint) |
| 1030 | EXCLUSIVE_LOCKS_REQUIRED(lock_){ |
Ken Rockot | cd4c1f1 | 2023-11-08 23:22:16 | [diff] [blame] | 1031 | if(MarkPeerClosed(endpoint)){ |
| 1032 | endpoints_.erase(endpoint->id()); |
| 1033 | } |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 1034 | } |
| 1035 | |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 1036 | Endpoint*FindOrInsertEndpoint(mojo::InterfaceId id,bool* inserted) |
| 1037 | EXCLUSIVE_LOCKS_REQUIRED(lock_){ |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 1038 | DCHECK(!inserted||!*inserted); |
| 1039 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 1040 | Endpoint* endpoint=FindEndpoint(id); |
| 1041 | if(!endpoint){ |
| 1042 | endpoint=newEndpoint(this, id); |
| 1043 | endpoints_.insert({id, endpoint}); |
| 1044 | if(inserted) |
| 1045 | *inserted=true; |
| 1046 | } |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 1047 | return endpoint; |
| 1048 | } |
| 1049 | |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 1050 | Endpoint*FindEndpoint(mojo::InterfaceId id) EXCLUSIVE_LOCKS_REQUIRED(lock_){ |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 1051 | auto iter= endpoints_.find(id); |
| 1052 | return iter!= endpoints_.end()? iter->second.get():nullptr; |
| 1053 | } |
| 1054 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 1055 | // mojo::MessageReceiver: |
| 1056 | boolAccept(mojo::Message* message) override{ |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 1057 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 1058 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 1059 | if(!message->DeserializeAssociatedEndpointHandles(this)) |
| 1060 | returnfalse; |
| 1061 | |
| 1062 | if(mojo::PipeControlMessageHandler::IsPipeControlMessage(message)) |
| 1063 | return control_message_handler_.Accept(message); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 1064 | |
| 1065 | mojo::InterfaceId id= message->interface_id(); |
Ken Rockot | 9f69e16 | 2021-04-19 14:16:29 | [diff] [blame] | 1066 | if(!mojo::IsValidInterfaceId(id)) |
| 1067 | returnfalse; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 1068 | |
Ken Rockot | 4fede455 | 2019-05-09 01:16:41 | [diff] [blame] | 1069 | base::ReleasableAutoLock locker(&lock_); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 1070 | Endpoint* endpoint=FindEndpoint(id); |
| 1071 | if(!endpoint) |
| 1072 | returntrue; |
| 1073 | |
| 1074 | mojo::InterfaceEndpointClient* client= endpoint->client(); |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 1075 | if(!client||!endpoint->task_runner()->RunsTasksInCurrentSequence()){ |
Ken Rockot | c5cc0273 | 2021-06-04 08:02:41 | [diff] [blame] | 1076 | // The ChannelProxy for this channel is bound to `proxy_task_runner_` and |
| 1077 | // by default legacy IPCs must dispatch to either the IO thread or the |
| 1078 | // proxy task runner. We generally impose the same constraint on |
| 1079 | // associated interface endpoints so that FIFO can be guaranteed across |
| 1080 | // all interfaces without stalling any of them to wait for a pending |
| 1081 | // endpoint to be bound. |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 1082 | // |
Ken Rockot | c5cc0273 | 2021-06-04 08:02:41 | [diff] [blame] | 1083 | // This allows us to assume that if an endpoint is not yet bound when we |
| 1084 | // receive a message targeting it, it *will* be bound on the proxy task |
| 1085 | // runner by the time a newly posted task runs there. Hence we simply post |
| 1086 | // a hopeful dispatch task to that task runner. |
| 1087 | // |
| 1088 | // As it turns out, there are even some instances of endpoints binding to |
| 1089 | // alternative (non-IO-thread, non-proxy) task runners, but still |
| 1090 | // ultimately relying on the fact that we schedule their messages on the |
| 1091 | // proxy task runner. So even if the endpoint is already bound, we |
| 1092 | // default to scheduling it on the proxy task runner as long as it's not |
| 1093 | // bound specifically to the IO task runner. |
| 1094 | // TODO(rockot): Try to sort out these cases and maybe eliminate them. |
| 1095 | // |
| 1096 | // Finally, it's also possible that an endpoint was bound to an |
| 1097 | // alternative task runner and it really does want its messages to |
| 1098 | // dispatch there. In that case `was_bound_off_sequence()` will be true to |
| 1099 | // signal that we should really use that task runner. |
| 1100 | const scoped_refptr<base::SequencedTaskRunner> task_runner= |
| 1101 | client&& endpoint->was_bound_off_sequence() |
| 1102 | ? endpoint->task_runner() |
| 1103 | : proxy_task_runner_.get(); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 1104 | |
Scott Haseley | cf0f172 | 2023-11-03 21:39:01 | [diff] [blame] | 1105 | ScopedUrgentMessageNotification scoped_urgent_message_notification( |
| 1106 | message->has_flag(mojo::Message::kFlagIsUrgent) |
| 1107 | ? urgent_message_observer_ |
| 1108 | :nullptr); |
| 1109 | |
rockot | c4cc691e | 2016-08-19 18:48:57 | [diff] [blame] | 1110 | if(message->has_flag(mojo::Message::kFlagIsSync)){ |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 1111 | MessageWrapper message_wrapper(this, std::move(*message)); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 1112 | // Sync messages may need to be handled by the endpoint if it's blocking |
| 1113 | // on a sync reply. We pass ownership of the message to the endpoint's |
| 1114 | // sync message queue. If the endpoint was blocking, it will dequeue the |
| 1115 | // message and dispatch it. Otherwise the posted |AcceptSyncMessage()| |
| 1116 | // call will dequeue the message and dispatch it. |
Arthur Sonzogni | 59ac822 | 2023-11-10 09:46:54 | [diff] [blame] | 1117 | std::optional<uint32_t> message_id= |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 1118 | endpoint->EnqueueSyncMessage(std::move(message_wrapper)); |
Ken Rockot | b6f9212 | 2022-10-31 04:52:24 | [diff] [blame] | 1119 | if(message_id){ |
| 1120 | task_runner->PostTask( |
| 1121 | FROM_HERE, |
| 1122 | base::BindOnce( |
| 1123 | &ChannelAssociatedGroupController::AcceptSyncMessage,this, |
Scott Haseley | cf0f172 | 2023-11-03 21:39:01 | [diff] [blame] | 1124 | id,*message_id, |
| 1125 | std::move(scoped_urgent_message_notification))); |
Ken Rockot | b6f9212 | 2022-10-31 04:52:24 | [diff] [blame] | 1126 | } |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 1127 | returntrue; |
| 1128 | } |
| 1129 | |
Ken Rockot | c5cc0273 | 2021-06-04 08:02:41 | [diff] [blame] | 1130 | // If |task_runner| has been torn down already, this PostTask will fail |
| 1131 | // and destroy |message|. That operation may need to in turn destroy |
Ken Rockot | 4fede455 | 2019-05-09 01:16:41 | [diff] [blame] | 1132 | // in-transit associated endpoints and thus acquire |lock_|. We no longer |
Ken Rockot | c5cc0273 | 2021-06-04 08:02:41 | [diff] [blame] | 1133 | // need the lock to be held now, so we can release it before the PostTask. |
Harkiran Bolaria | 8b1cf11 | 2020-09-15 22:58:34 | [diff] [blame] | 1134 | { |
| 1135 | // Grab interface name from |client| before releasing the lock to ensure |
| 1136 | // that |client| is safe to access. |
| 1137 | base::TaskAnnotator::ScopedSetIpcHash scoped_set_ipc_hash( |
| 1138 | client? client->interface_name():"unknown interface"); |
| 1139 | locker.Release(); |
Ken Rockot | c5cc0273 | 2021-06-04 08:02:41 | [diff] [blame] | 1140 | task_runner->PostTask( |
Harkiran Bolaria | 8b1cf11 | 2020-09-15 22:58:34 | [diff] [blame] | 1141 | FROM_HERE, |
| 1142 | base::BindOnce( |
Ken Rockot | c5cc0273 | 2021-06-04 08:02:41 | [diff] [blame] | 1143 | &ChannelAssociatedGroupController::AcceptOnEndpointThread,this, |
Scott Haseley | cf0f172 | 2023-11-03 21:39:01 | [diff] [blame] | 1144 | std::move(*message), |
| 1145 | std::move(scoped_urgent_message_notification))); |
Harkiran Bolaria | 8b1cf11 | 2020-09-15 22:58:34 | [diff] [blame] | 1146 | } |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 1147 | returntrue; |
| 1148 | } |
| 1149 | |
Ken Rockot | 4fede455 | 2019-05-09 01:16:41 | [diff] [blame] | 1150 | locker.Release(); |
Harkiran Bolaria | 8b1cf11 | 2020-09-15 22:58:34 | [diff] [blame] | 1151 | // It's safe to access |client| here without holding a lock, because this |
| 1152 | // code runs on a proxy thread and |client| can't be destroyed from any |
| 1153 | // thread. |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 1154 | return client->HandleIncomingMessage(message); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 1155 | } |
| 1156 | |
Scott Haseley | cf0f172 | 2023-11-03 21:39:01 | [diff] [blame] | 1157 | voidAcceptOnEndpointThread( |
| 1158 | mojo::Message message, |
| 1159 | ScopedUrgentMessageNotification scoped_urgent_message_notification){ |
Stephen Nusko | 8e40064 | 2020-09-16 13:48:59 | [diff] [blame] | 1160 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("mojom"), |
Ken Rockot | c5cc0273 | 2021-06-04 08:02:41 | [diff] [blame] | 1161 | "ChannelAssociatedGroupController::AcceptOnEndpointThread"); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 1162 | |
rockot | c4cc691e | 2016-08-19 18:48:57 | [diff] [blame] | 1163 | mojo::InterfaceId id= message.interface_id(); |
Ken Rockot | cd23f75 | 2020-06-20 01:22:31 | [diff] [blame] | 1164 | DCHECK(mojo::IsValidInterfaceId(id)&&!mojo::IsPrimaryInterfaceId(id)); |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 1165 | |
| 1166 | base::AutoLock locker(lock_); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 1167 | Endpoint* endpoint=FindEndpoint(id); |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 1168 | if(!endpoint) |
| 1169 | return; |
| 1170 | |
| 1171 | mojo::InterfaceEndpointClient* client= endpoint->client(); |
Greg Thompson | fe432e76 | 2021-06-04 06:47:14 | [diff] [blame] | 1172 | if(!client) |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 1173 | return; |
| 1174 | |
Ken Rockot | 0747c94 | 2021-12-07 20:51:59 | [diff] [blame] | 1175 | if(!endpoint->task_runner()->RunsTasksInCurrentSequence()&& |
| 1176 | !proxy_task_runner_->RunsTasksInCurrentSequence()){ |
| 1177 | return; |
| 1178 | } |
| 1179 | |
Alexander Timin | 24027abc | 2022-02-24 00:47:55 | [diff] [blame] | 1180 | // TODO(altimin): This event is temporarily kept as a debug fallback. Remove |
| 1181 | // it once the new implementation proves to be stable. |
Alexander Timin | da1f2c5 | 2021-11-03 02:23:05 | [diff] [blame] | 1182 | TRACE_EVENT( |
Alexander Timin | 24027abc | 2022-02-24 00:47:55 | [diff] [blame] | 1183 | TRACE_DISABLED_BY_DEFAULT("mojom"), |
Alexander Timin | da1f2c5 | 2021-11-03 02:23:05 | [diff] [blame] | 1184 | // Using client->interface_name() is safe here because this is a static |
| 1185 | // string defined for each mojo interface. |
| 1186 | perfetto::StaticString(client->interface_name()), |
| 1187 | [&](perfetto::EventContext& ctx){ |
| 1188 | staticconstuint8_t* toplevel_flow_enabled= |
| 1189 | TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED("toplevel.flow"); |
| 1190 | if(!*toplevel_flow_enabled) |
| 1191 | return; |
| 1192 | |
| 1193 | perfetto::Flow::Global(message.GetTraceId())(ctx); |
| 1194 | }); |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 1195 | |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 1196 | // Sync messages should never make their way to this method. |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 1197 | DCHECK(!message.has_flag(mojo::Message::kFlagIsSync)); |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 1198 | |
| 1199 | bool result=false; |
| 1200 | { |
| 1201 | base::AutoUnlock unlocker(lock_); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 1202 | result= client->HandleIncomingMessage(&message); |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | if(!result) |
| 1206 | RaiseError(); |
| 1207 | } |
| 1208 | |
Scott Haseley | cf0f172 | 2023-11-03 21:39:01 | [diff] [blame] | 1209 | voidAcceptSyncMessage( |
| 1210 | mojo::InterfaceId interface_id, |
| 1211 | uint32_t message_id, |
| 1212 | ScopedUrgentMessageNotification scoped_urgent_message_notification){ |
Stephen Nusko | 8e40064 | 2020-09-16 13:48:59 | [diff] [blame] | 1213 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("mojom"), |
| 1214 | "ChannelAssociatedGroupController::AcceptSyncMessage"); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 1215 | |
| 1216 | base::AutoLock locker(lock_); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 1217 | Endpoint* endpoint=FindEndpoint(interface_id); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 1218 | if(!endpoint) |
| 1219 | return; |
| 1220 | |
csharrison | 1af8d6ab | 2017-04-21 17:47:23 | [diff] [blame] | 1221 | // Careful, if the endpoint is detached its members are cleared. Check for |
| 1222 | // that before dereferencing. |
| 1223 | mojo::InterfaceEndpointClient* client= endpoint->client(); |
| 1224 | if(!client) |
| 1225 | return; |
| 1226 | |
Ken Rockot | 0747c94 | 2021-12-07 20:51:59 | [diff] [blame] | 1227 | if(!endpoint->task_runner()->RunsTasksInCurrentSequence()&& |
| 1228 | !proxy_task_runner_->RunsTasksInCurrentSequence()){ |
| 1229 | return; |
| 1230 | } |
| 1231 | |
Stephen Nusko | 8e40064 | 2020-09-16 13:48:59 | [diff] [blame] | 1232 | // Using client->interface_name() is safe here because this is a static |
| 1233 | // string defined for each mojo interface. |
| 1234 | TRACE_EVENT0("mojom", client->interface_name()); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 1235 | MessageWrapper message_wrapper= endpoint->PopSyncMessage(message_id); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 1236 | |
| 1237 | // The message must have already been dequeued by the endpoint waking up |
| 1238 | // from a sync wait. Nothing to do. |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 1239 | if(message_wrapper.value().IsNull()) |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 1240 | return; |
| 1241 | |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 1242 | bool result=false; |
| 1243 | { |
| 1244 | base::AutoUnlock unlocker(lock_); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 1245 | result= client->HandleIncomingMessage(&message_wrapper.value()); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | if(!result) |
| 1249 | RaiseError(); |
| 1250 | } |
| 1251 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 1252 | // mojo::PipeControlMessageHandlerDelegate: |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 1253 | boolOnPeerAssociatedEndpointClosed( |
| 1254 | mojo::InterfaceId id, |
Arthur Sonzogni | 59ac822 | 2023-11-10 09:46:54 | [diff] [blame] | 1255 | const std::optional<mojo::DisconnectReason>& reason) override{ |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 1256 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 1257 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 1258 | scoped_refptr<ChannelAssociatedGroupController> keepalive(this); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 1259 | base::AutoLock locker(lock_); |
| 1260 | scoped_refptr<Endpoint> endpoint=FindOrInsertEndpoint(id,nullptr); |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 1261 | if(reason) |
| 1262 | endpoint->set_disconnect_reason(reason); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 1263 | if(!endpoint->peer_closed()){ |
| 1264 | if(endpoint->client()) |
| 1265 | NotifyEndpointOfError(endpoint.get(),false/* force_async */); |
| 1266 | MarkPeerClosedAndMaybeRemove(endpoint.get()); |
| 1267 | } |
| 1268 | |
| 1269 | returntrue; |
| 1270 | } |
| 1271 | |
Ken Rockot | eb2366a | 2020-01-13 21:13:46 | [diff] [blame] | 1272 | boolWaitForFlushToComplete( |
| 1273 | mojo::ScopedMessagePipeHandle flush_pipe) override{ |
| 1274 | // We don't support async flushing on the IPC Channel pipe. |
| 1275 | returnfalse; |
| 1276 | } |
| 1277 | |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 1278 | const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
Ken Rockot | 4fede455 | 2019-05-09 01:16:41 | [diff] [blame] | 1279 | const scoped_refptr<base::SingleThreadTaskRunner> proxy_task_runner_; |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 1280 | constbool set_interface_id_namespace_bit_; |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 1281 | |
| 1282 | // Ensures sequenced access to members below. |
| 1283 | SEQUENCE_CHECKER(sequence_checker_); |
| 1284 | |
| 1285 | // Whether `Bind()` or `SendMessageOnSequence()` was called. |
| 1286 | // `sequence_checker_` can be detached when this is `false`. |
| 1287 | bool was_bound_or_message_sent_ GUARDED_BY_CONTEXT(sequence_checker_)=false; |
| 1288 | |
| 1289 | bool paused_ GUARDED_BY_CONTEXT(sequence_checker_)=false; |
| 1290 | bool shut_down_ GUARDED_BY_CONTEXT(sequence_checker_)=false; |
| 1291 | std::unique_ptr<mojo::Connector> connector_ |
| 1292 | GUARDED_BY_CONTEXT(sequence_checker_); |
| 1293 | mojo::MessageDispatcher dispatcher_ GUARDED_BY_CONTEXT(sequence_checker_); |
| 1294 | mojo::PipeControlMessageHandler control_message_handler_ |
| 1295 | GUARDED_BY_CONTEXT(sequence_checker_); |
| 1296 | ControlMessageProxyThunk control_message_proxy_thunk_ |
| 1297 | GUARDED_BY_CONTEXT(sequence_checker_); |
| 1298 | raw_ptr<UrgentMessageObserver> urgent_message_observer_ |
| 1299 | GUARDED_BY_CONTEXT(sequence_checker_)=nullptr; |
rockot | 5890954 | 2016-11-10 20:05:45 | [diff] [blame] | 1300 | |
| 1301 | // NOTE: It is unsafe to call into this object while holding |lock_|. |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 1302 | mojo::PipeControlMessageProxy control_message_proxy_; |
| 1303 | |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 1304 | // Outgoing messages sent before this controller Bound() to a pipe or while it |
| 1305 | // was paused. Protected by a lock to support memory dumps from any thread. |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 1306 | base::Lock outgoing_messages_lock_; |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 1307 | std::vector<mojo::Message> outgoing_messages_ |
| 1308 | GUARDED_BY(outgoing_messages_lock_); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 1309 | |
| 1310 | // Guards the fields below for thread-safe access. |
| 1311 | base::Lock lock_; |
| 1312 | |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 1313 | bool encountered_error_ GUARDED_BY(lock_)=false; |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 1314 | |
| 1315 | // ID #1 is reserved for the mojom::Channel interface. |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 1316 | uint32_t next_interface_id_ GUARDED_BY(lock_)=2; |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 1317 | |
François Doray | f8ec16da2 | 2024-01-17 21:55:12 | [diff] [blame] | 1318 | std::map<uint32_t, scoped_refptr<Endpoint>> endpoints_ GUARDED_BY(lock_); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 1319 | }; |
| 1320 | |
François Doray | fad1b8b | 2024-01-04 20:22:53 | [diff] [blame] | 1321 | namespace{ |
| 1322 | |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 1323 | boolControllerMemoryDumpProvider::OnMemoryDump( |
| 1324 | const base::trace_event::MemoryDumpArgs& args, |
| 1325 | base::trace_event::ProcessMemoryDump* pmd){ |
| 1326 | base::AutoLock lock(lock_); |
Ali Hijazi | 133b2d9 | 2024-02-09 14:01:52 | [diff] [blame] | 1327 | for(ChannelAssociatedGroupController* controller: controllers_){ |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 1328 | base::trace_event::MemoryAllocatorDump* dump= pmd->CreateAllocatorDump( |
| 1329 | base::StringPrintf("mojo/queued_ipc_channel_message/0x%"PRIxPTR, |
| 1330 | reinterpret_cast<uintptr_t>(controller))); |
| 1331 | dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameObjectCount, |
| 1332 | base::trace_event::MemoryAllocatorDump::kUnitsObjects, |
| 1333 | controller->GetQueuedMessageCount()); |
Siddhartha S | 0348442 | 2019-04-23 20:30:00 | [diff] [blame] | 1334 | MessageMemoryDumpInfo info; |
| 1335 | size_t count=0; |
| 1336 | controller->GetTopQueuedMessageMemoryDumpInfo(&info,&count); |
| 1337 | dump->AddScalar("top_message_name","id", info.id); |
Siddhartha | d1cfec1 | 2018-09-17 21:42:15 | [diff] [blame] | 1338 | dump->AddScalar("top_message_count", |
| 1339 | base::trace_event::MemoryAllocatorDump::kUnitsObjects, |
Siddhartha S | 0348442 | 2019-04-23 20:30:00 | [diff] [blame] | 1340 | count); |
| 1341 | |
| 1342 | if(info.profiler_tag){ |
| 1343 | // TODO(ssid): Memory dumps currently do not support adding string |
| 1344 | // arguments in background dumps. So, add this value as a trace event for |
| 1345 | // now. |
ssid | bc86cb7 | 2019-05-16 00:25:37 | [diff] [blame] | 1346 | TRACE_EVENT2(base::trace_event::MemoryDumpManager::kTraceCategory, |
Siddhartha S | 0348442 | 2019-04-23 20:30:00 | [diff] [blame] | 1347 | "ControllerMemoryDumpProvider::OnMemoryDump", |
ssid | bc86cb7 | 2019-05-16 00:25:37 | [diff] [blame] | 1348 | "top_queued_message_tag", info.profiler_tag, |
| 1349 | "count", count); |
Siddhartha S | 0348442 | 2019-04-23 20:30:00 | [diff] [blame] | 1350 | } |
Ken Rockot | 2b6de98 | 2018-03-20 22:28:13 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | returntrue; |
| 1354 | } |
| 1355 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 1356 | classMojoBootstrapImpl:publicMojoBootstrap{ |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 1357 | public: |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 1358 | MojoBootstrapImpl( |
| 1359 | mojo::ScopedMessagePipeHandle handle, |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 1360 | const scoped_refptr<ChannelAssociatedGroupController> controller) |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 1361 | : controller_(controller), |
| 1362 | associated_group_(controller), |
| 1363 | handle_(std::move(handle)){} |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 1364 | |
Peter Boström | c68c5aa | 2021-09-28 00:28:00 | [diff] [blame] | 1365 | MojoBootstrapImpl(constMojoBootstrapImpl&)=delete; |
| 1366 | MojoBootstrapImpl&operator=(constMojoBootstrapImpl&)=delete; |
| 1367 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 1368 | ~MojoBootstrapImpl() override{ |
| 1369 | controller_->ShutDown(); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | private: |
Julie Jeongeun Kim | 903b34b | 2019-09-25 11:11:54 | [diff] [blame] | 1373 | voidConnect( |
Ken Rockot | fbbd444 | 2021-06-04 01:19:14 | [diff] [blame] | 1374 | mojo::PendingAssociatedRemote<mojom::Channel>* sender, |
Julie Jeongeun Kim | 903b34b | 2019-09-25 11:11:54 | [diff] [blame] | 1375 | mojo::PendingAssociatedReceiver<mojom::Channel>* receiver) override{ |
Ken Rockot | 9fae8bcd | 2021-06-04 22:09:49 | [diff] [blame] | 1376 | controller_->Bind(std::move(handle_), sender, receiver); |
msramek | 5507fee | 2016-07-22 10:06:21 | [diff] [blame] | 1377 | } |
| 1378 | |
Ken Rockot | 9fae8bcd | 2021-06-04 22:09:49 | [diff] [blame] | 1379 | voidStartReceiving() override{ controller_->StartReceiving();} |
| 1380 | |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 1381 | voidPause() override{ |
| 1382 | controller_->Pause(); |
| 1383 | } |
| 1384 | |
| 1385 | voidUnpause() override{ |
| 1386 | controller_->Unpause(); |
rockot | 401fb2c | 2016-09-06 18:35:57 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | voidFlush() override{ |
| 1390 | controller_->FlushOutgoingMessages(); |
| 1391 | } |
| 1392 | |
msramek | 5507fee | 2016-07-22 10:06:21 | [diff] [blame] | 1393 | mojo::AssociatedGroup*GetAssociatedGroup() override{ |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 1394 | return&associated_group_; |
msramek | 5507fee | 2016-07-22 10:06:21 | [diff] [blame] | 1395 | } |
| 1396 | |
Scott Haseley | cf0f172 | 2023-11-03 21:39:01 | [diff] [blame] | 1397 | voidSetUrgentMessageObserver(UrgentMessageObserver* observer) override{ |
| 1398 | controller_->SetUrgentMessageObserver(observer); |
| 1399 | } |
| 1400 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 1401 | scoped_refptr<ChannelAssociatedGroupController> controller_; |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 1402 | mojo::AssociatedGroup associated_group_; |
msramek | 5507fee | 2016-07-22 10:06:21 | [diff] [blame] | 1403 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 1404 | mojo::ScopedMessagePipeHandle handle_; |
msramek | 5507fee | 2016-07-22 10:06:21 | [diff] [blame] | 1405 | }; |
| 1406 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 1407 | }// namespace |
| 1408 | |
Ken Rockot | c5cc0273 | 2021-06-04 08:02:41 | [diff] [blame] | 1409 | ScopedAllowOffSequenceChannelAssociatedBindings:: |
| 1410 | ScopedAllowOffSequenceChannelAssociatedBindings() |
Peter Kasting | 525a014 | 2023-03-08 21:56:27 | [diff] [blame] | 1411 | : resetter_(&off_sequence_binding_allowed,true){} |
Ken Rockot | c5cc0273 | 2021-06-04 08:02:41 | [diff] [blame] | 1412 | |
| 1413 | ScopedAllowOffSequenceChannelAssociatedBindings:: |
Peter Kasting | 525a014 | 2023-03-08 21:56:27 | [diff] [blame] | 1414 | ~ScopedAllowOffSequenceChannelAssociatedBindings()=default; |
Ken Rockot | c5cc0273 | 2021-06-04 08:02:41 | [diff] [blame] | 1415 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 1416 | // static |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 1417 | std::unique_ptr<MojoBootstrap>MojoBootstrap::Create( |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 1418 | mojo::ScopedMessagePipeHandle handle, |
| 1419 | Channel::Mode mode, |
Hajime Hoshi | a98f110 | 2017-11-20 06:34:35 | [diff] [blame] | 1420 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
Ken Rockot | 7308d4b | 2022-12-13 06:35:56 | [diff] [blame] | 1421 | const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner){ |
Jeremy Roman | 160eb92 | 2017-08-29 17:43:43 | [diff] [blame] | 1422 | return std::make_unique<MojoBootstrapImpl>( |
Ken Rockot | 7308d4b | 2022-12-13 06:35:56 | [diff] [blame] | 1423 | std::move(handle), |
| 1424 | base::MakeRefCounted<ChannelAssociatedGroupController>( |
| 1425 | mode==Channel::MODE_SERVER, ipc_task_runner, proxy_task_runner)); |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 1426 | } |
| 1427 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 1428 | }// namespace IPC |