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 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 7 | #include <stdint.h> |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 8 | |
| 9 | #include <map> |
| 10 | #include <memory> |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 11 | #include <queue> |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 12 | #include <utility> |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 13 | #include <vector> |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 14 | |
rockot | a21316a | 2016-06-19 17:08:36 | [diff] [blame] | 15 | #include "base/callback.h" |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 16 | #include "base/logging.h" |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 17 | #include "base/macros.h" |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 18 | #include "base/memory/ptr_util.h" |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 19 | #include "base/single_thread_task_runner.h" |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 20 | #include "base/synchronization/lock.h" |
Sam McNally | de5ae67 | 2017-06-19 23:34:45 | [diff] [blame] | 21 | #include "base/threading/thread_checker.h" |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 22 | #include "base/threading/thread_task_runner_handle.h" |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 23 | #include "mojo/public/cpp/bindings/associated_group.h" |
| 24 | #include "mojo/public/cpp/bindings/associated_group_controller.h" |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 25 | #include "mojo/public/cpp/bindings/connector.h" |
| 26 | #include "mojo/public/cpp/bindings/interface_endpoint_client.h" |
| 27 | #include "mojo/public/cpp/bindings/interface_endpoint_controller.h" |
| 28 | #include "mojo/public/cpp/bindings/interface_id.h" |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 29 | #include "mojo/public/cpp/bindings/message.h" |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 30 | #include "mojo/public/cpp/bindings/message_header_validator.h" |
| 31 | #include "mojo/public/cpp/bindings/pipe_control_message_handler.h" |
| 32 | #include "mojo/public/cpp/bindings/pipe_control_message_handler_delegate.h" |
| 33 | #include "mojo/public/cpp/bindings/pipe_control_message_proxy.h" |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 34 | #include "mojo/public/cpp/bindings/sync_event_watcher.h" |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 35 | |
| 36 | namespace IPC { |
| 37 | |
| 38 | namespace { |
| 39 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 40 | class ChannelAssociatedGroupController |
| 41 | : public mojo::AssociatedGroupController, |
| 42 | public mojo::MessageReceiver, |
| 43 | public mojo::PipeControlMessageHandlerDelegate { |
| 44 | public: |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 45 | ChannelAssociatedGroupController( |
| 46 | bool set_interface_id_namespace_bit, |
| 47 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) |
rockot | b01ef6a | 2016-07-27 03:24:32 | [diff] [blame] | 48 | : task_runner_(ipc_task_runner), |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 49 | proxy_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 50 | set_interface_id_namespace_bit_(set_interface_id_namespace_bit), |
rockot | 222e7dd | 2016-08-24 23:37:11 | [diff] [blame] | 51 | filters_(this), |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 52 | control_message_handler_(this), |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 53 | control_message_proxy_thunk_(this), |
| 54 | control_message_proxy_(&control_message_proxy_thunk_) { |
| 55 | thread_checker_.DetachFromThread(); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 56 | control_message_handler_.SetDescription( |
| 57 | "IPC::mojom::Bootstrap [master] PipeControlMessageHandler"); |
rockot | 222e7dd | 2016-08-24 23:37:11 | [diff] [blame] | 58 | filters_.Append<mojo::MessageHeaderValidator>( |
| 59 | "IPC::mojom::Bootstrap [master] MessageHeaderValidator"); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 60 | } |
| 61 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 62 | void Bind(mojo::ScopedMessagePipeHandle handle) { |
| 63 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 64 | DCHECK(task_runner_->BelongsToCurrentThread()); |
rockot | 9098435 | 2016-07-25 17:36:19 | [diff] [blame] | 65 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 66 | connector_.reset(new mojo::Connector( |
| 67 | std::move(handle), mojo::Connector::SINGLE_THREADED_SEND, |
| 68 | task_runner_)); |
rockot | 222e7dd | 2016-08-24 23:37:11 | [diff] [blame] | 69 | connector_->set_incoming_receiver(&filters_); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 70 | connector_->set_connection_error_handler( |
| 71 | base::Bind(&ChannelAssociatedGroupController::OnPipeError, |
| 72 | base::Unretained(this))); |
jcivelli | 2207af1 | 2017-01-26 20:46:00 | [diff] [blame] | 73 | connector_->SetWatcherHeapProfilerTag("IPC Channel"); |
rockot | 401fb2c | 2016-09-06 18:35:57 | [diff] [blame] | 74 | } |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 75 | |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 76 | void Pause() { |
| 77 | DCHECK(!paused_); |
| 78 | paused_ = true; |
| 79 | } |
| 80 | |
| 81 | void Unpause() { |
| 82 | DCHECK(paused_); |
| 83 | paused_ = false; |
rockot | 401fb2c | 2016-09-06 18:35:57 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void FlushOutgoingMessages() { |
rockot | c4cc691e | 2016-08-19 18:48:57 | [diff] [blame] | 87 | std::vector<mojo::Message> outgoing_messages; |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 88 | std::swap(outgoing_messages, outgoing_messages_); |
| 89 | for (auto& message : outgoing_messages) |
rockot | c4cc691e | 2016-08-19 18:48:57 | [diff] [blame] | 90 | SendMessage(&message); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void CreateChannelEndpoints(mojom::ChannelAssociatedPtr* sender, |
| 94 | mojom::ChannelAssociatedRequest* receiver) { |
| 95 | mojo::InterfaceId sender_id, receiver_id; |
| 96 | if (set_interface_id_namespace_bit_) { |
| 97 | sender_id = 1 | mojo::kInterfaceIdNamespaceMask; |
| 98 | receiver_id = 1; |
| 99 | } else { |
| 100 | sender_id = 1; |
| 101 | receiver_id = 1 | mojo::kInterfaceIdNamespaceMask; |
| 102 | } |
| 103 | |
| 104 | { |
| 105 | base::AutoLock locker(lock_); |
| 106 | Endpoint* sender_endpoint = new Endpoint(this, sender_id); |
| 107 | Endpoint* receiver_endpoint = new Endpoint(this, receiver_id); |
| 108 | endpoints_.insert({ sender_id, sender_endpoint }); |
| 109 | endpoints_.insert({ receiver_id, receiver_endpoint }); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 110 | sender_endpoint->set_handle_created(); |
| 111 | receiver_endpoint->set_handle_created(); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | mojo::ScopedInterfaceEndpointHandle sender_handle = |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 115 | CreateScopedInterfaceEndpointHandle(sender_id); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 116 | mojo::ScopedInterfaceEndpointHandle receiver_handle = |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 117 | CreateScopedInterfaceEndpointHandle(receiver_id); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 118 | |
| 119 | sender->Bind(mojom::ChannelAssociatedPtrInfo(std::move(sender_handle), 0)); |
Ken Rockot | 96d1b7b5 | 2017-05-13 00:29:21 | [diff] [blame] | 120 | *receiver = mojom::ChannelAssociatedRequest(std::move(receiver_handle)); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 121 | } |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 122 | |
| 123 | void ShutDown() { |
| 124 | DCHECK(thread_checker_.CalledOnValidThread()); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 125 | connector_->CloseMessagePipe(); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 126 | OnPipeError(); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 127 | connector_.reset(); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // mojo::AssociatedGroupController: |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 131 | mojo::InterfaceId AssociateInterface( |
| 132 | mojo::ScopedInterfaceEndpointHandle handle_to_send) override { |
| 133 | if (!handle_to_send.pending_association()) |
| 134 | return mojo::kInvalidInterfaceId; |
| 135 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 136 | uint32_t id = 0; |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 137 | { |
| 138 | base::AutoLock locker(lock_); |
| 139 | do { |
| 140 | if (next_interface_id_ >= mojo::kInterfaceIdNamespaceMask) |
| 141 | next_interface_id_ = 2; |
| 142 | id = next_interface_id_++; |
| 143 | if (set_interface_id_namespace_bit_) |
| 144 | id |= mojo::kInterfaceIdNamespaceMask; |
| 145 | } while (ContainsKey(endpoints_, id)); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 146 | |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 147 | Endpoint* endpoint = new Endpoint(this, id); |
| 148 | if (encountered_error_) |
| 149 | endpoint->set_peer_closed(); |
| 150 | endpoint->set_handle_created(); |
| 151 | endpoints_.insert({id, endpoint}); |
| 152 | } |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 153 | |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 154 | if (!NotifyAssociation(&handle_to_send, id)) { |
| 155 | // The peer handle of |handle_to_send|, which is supposed to join this |
| 156 | // associated group, has been closed. |
| 157 | { |
| 158 | base::AutoLock locker(lock_); |
| 159 | Endpoint* endpoint = FindEndpoint(id); |
| 160 | if (endpoint) |
| 161 | MarkClosedAndMaybeRemove(endpoint); |
| 162 | } |
| 163 | |
| 164 | control_message_proxy_.NotifyPeerEndpointClosed( |
| 165 | id, handle_to_send.disconnect_reason()); |
| 166 | } |
| 167 | return id; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | mojo::ScopedInterfaceEndpointHandle CreateLocalEndpointHandle( |
| 171 | mojo::InterfaceId id) override { |
| 172 | if (!mojo::IsValidInterfaceId(id)) |
| 173 | return mojo::ScopedInterfaceEndpointHandle(); |
| 174 | |
Yuzhu Shen | 9f87fb0 | 2017-08-11 17:07:06 | [diff] [blame^] | 175 | // Unless it is the master ID, |id| is from the remote side and therefore |
| 176 | // its namespace bit is supposed to be different than the value that this |
| 177 | // router would use. |
| 178 | if (!mojo::IsMasterInterfaceId(id) && |
| 179 | set_interface_id_namespace_bit_ == |
| 180 | mojo::HasInterfaceIdNamespaceBitSet(id)) { |
| 181 | return mojo::ScopedInterfaceEndpointHandle(); |
| 182 | } |
| 183 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 184 | base::AutoLock locker(lock_); |
| 185 | bool inserted = false; |
| 186 | Endpoint* endpoint = FindOrInsertEndpoint(id, &inserted); |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 187 | if (inserted) { |
| 188 | DCHECK(!endpoint->handle_created()); |
| 189 | if (encountered_error_) |
| 190 | endpoint->set_peer_closed(); |
| 191 | } else { |
| 192 | if (endpoint->handle_created()) |
| 193 | return mojo::ScopedInterfaceEndpointHandle(); |
| 194 | } |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 195 | |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 196 | endpoint->set_handle_created(); |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 197 | return CreateScopedInterfaceEndpointHandle(id); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 198 | } |
| 199 | |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 200 | void CloseEndpointHandle( |
| 201 | mojo::InterfaceId id, |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 202 | const base::Optional<mojo::DisconnectReason>& reason) override { |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 203 | if (!mojo::IsValidInterfaceId(id)) |
| 204 | return; |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 205 | { |
| 206 | base::AutoLock locker(lock_); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 207 | DCHECK(ContainsKey(endpoints_, id)); |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 208 | Endpoint* endpoint = endpoints_[id].get(); |
| 209 | DCHECK(!endpoint->client()); |
| 210 | DCHECK(!endpoint->closed()); |
| 211 | MarkClosedAndMaybeRemove(endpoint); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 212 | } |
| 213 | |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 214 | if (!mojo::IsMasterInterfaceId(id) || reason) |
| 215 | control_message_proxy_.NotifyPeerEndpointClosed(id, reason); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | mojo::InterfaceEndpointController* AttachEndpointClient( |
| 219 | const mojo::ScopedInterfaceEndpointHandle& handle, |
| 220 | mojo::InterfaceEndpointClient* client, |
Sam McNally | de5ae67 | 2017-06-19 23:34:45 | [diff] [blame] | 221 | scoped_refptr<base::SequencedTaskRunner> runner) override { |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 222 | const mojo::InterfaceId id = handle.id(); |
| 223 | |
| 224 | DCHECK(mojo::IsValidInterfaceId(id)); |
| 225 | DCHECK(client); |
| 226 | |
| 227 | base::AutoLock locker(lock_); |
| 228 | DCHECK(ContainsKey(endpoints_, id)); |
| 229 | |
| 230 | Endpoint* endpoint = endpoints_[id].get(); |
| 231 | endpoint->AttachClient(client, std::move(runner)); |
| 232 | |
| 233 | if (endpoint->peer_closed()) |
| 234 | NotifyEndpointOfError(endpoint, true /* force_async */); |
| 235 | |
| 236 | return endpoint; |
| 237 | } |
| 238 | |
| 239 | void DetachEndpointClient( |
| 240 | const mojo::ScopedInterfaceEndpointHandle& handle) override { |
| 241 | const mojo::InterfaceId id = handle.id(); |
| 242 | |
| 243 | DCHECK(mojo::IsValidInterfaceId(id)); |
| 244 | |
| 245 | base::AutoLock locker(lock_); |
| 246 | DCHECK(ContainsKey(endpoints_, id)); |
| 247 | |
| 248 | Endpoint* endpoint = endpoints_[id].get(); |
| 249 | endpoint->DetachClient(); |
| 250 | } |
| 251 | |
| 252 | void RaiseError() override { |
rockot | 7604e7b7 | 2016-07-28 17:37:39 | [diff] [blame] | 253 | if (task_runner_->BelongsToCurrentThread()) { |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 254 | connector_->RaiseError(); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 255 | } else { |
| 256 | task_runner_->PostTask( |
| 257 | FROM_HERE, |
| 258 | base::Bind(&ChannelAssociatedGroupController::RaiseError, this)); |
| 259 | } |
| 260 | } |
| 261 | |
Ken Rockot | 474df014 | 2017-07-12 13:28:56 | [diff] [blame] | 262 | bool PrefersSerializedMessages() override { return true; } |
| 263 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 264 | private: |
| 265 | class Endpoint; |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 266 | class ControlMessageProxyThunk; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 267 | friend class Endpoint; |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 268 | friend class ControlMessageProxyThunk; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 269 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 270 | // MessageWrapper objects are always destroyed under the controller's lock. On |
| 271 | // destruction, if the message it wrappers contains |
| 272 | // ScopedInterfaceEndpointHandles (which cannot be destructed under the |
| 273 | // controller's lock), the wrapper unlocks to clean them up. |
| 274 | class MessageWrapper { |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 275 | public: |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 276 | MessageWrapper() = default; |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 277 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 278 | MessageWrapper(ChannelAssociatedGroupController* controller, |
| 279 | mojo::Message message) |
| 280 | : controller_(controller), value_(std::move(message)) {} |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 281 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 282 | MessageWrapper(MessageWrapper&& other) |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 283 | : controller_(other.controller_), value_(std::move(other.value_)) {} |
| 284 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 285 | ~MessageWrapper() { |
| 286 | if (value_.associated_endpoint_handles()->empty()) |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 287 | return; |
| 288 | |
| 289 | controller_->lock_.AssertAcquired(); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 290 | { |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 291 | base::AutoUnlock unlocker(controller_->lock_); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 292 | value_.mutable_associated_endpoint_handles()->clear(); |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 296 | MessageWrapper& operator=(MessageWrapper&& other) { |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 297 | controller_ = other.controller_; |
| 298 | value_ = std::move(other.value_); |
| 299 | return *this; |
| 300 | } |
| 301 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 302 | mojo::Message& value() { return value_; } |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 303 | |
| 304 | private: |
| 305 | ChannelAssociatedGroupController* controller_ = nullptr; |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 306 | mojo::Message value_; |
| 307 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 308 | DISALLOW_COPY_AND_ASSIGN(MessageWrapper); |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 309 | }; |
| 310 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 311 | class Endpoint : public base::RefCountedThreadSafe<Endpoint>, |
| 312 | public mojo::InterfaceEndpointController { |
| 313 | public: |
| 314 | Endpoint(ChannelAssociatedGroupController* controller, mojo::InterfaceId id) |
| 315 | : controller_(controller), id_(id) {} |
| 316 | |
| 317 | mojo::InterfaceId id() const { return id_; } |
| 318 | |
| 319 | bool closed() const { |
| 320 | controller_->lock_.AssertAcquired(); |
| 321 | return closed_; |
| 322 | } |
| 323 | |
| 324 | void set_closed() { |
| 325 | controller_->lock_.AssertAcquired(); |
| 326 | closed_ = true; |
| 327 | } |
| 328 | |
| 329 | bool peer_closed() const { |
| 330 | controller_->lock_.AssertAcquired(); |
| 331 | return peer_closed_; |
| 332 | } |
| 333 | |
| 334 | void set_peer_closed() { |
| 335 | controller_->lock_.AssertAcquired(); |
| 336 | peer_closed_ = true; |
| 337 | } |
| 338 | |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 339 | bool handle_created() const { |
| 340 | controller_->lock_.AssertAcquired(); |
| 341 | return handle_created_; |
| 342 | } |
| 343 | |
| 344 | void set_handle_created() { |
| 345 | controller_->lock_.AssertAcquired(); |
| 346 | handle_created_ = true; |
| 347 | } |
| 348 | |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 349 | const base::Optional<mojo::DisconnectReason>& disconnect_reason() const { |
| 350 | return disconnect_reason_; |
| 351 | } |
| 352 | |
| 353 | void set_disconnect_reason( |
| 354 | const base::Optional<mojo::DisconnectReason>& disconnect_reason) { |
| 355 | disconnect_reason_ = disconnect_reason; |
| 356 | } |
| 357 | |
Sam McNally | de5ae67 | 2017-06-19 23:34:45 | [diff] [blame] | 358 | base::SequencedTaskRunner* task_runner() const { |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 359 | return task_runner_.get(); |
| 360 | } |
| 361 | |
| 362 | mojo::InterfaceEndpointClient* client() const { |
| 363 | controller_->lock_.AssertAcquired(); |
| 364 | return client_; |
| 365 | } |
| 366 | |
| 367 | void AttachClient(mojo::InterfaceEndpointClient* client, |
Sam McNally | de5ae67 | 2017-06-19 23:34:45 | [diff] [blame] | 368 | scoped_refptr<base::SequencedTaskRunner> runner) { |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 369 | controller_->lock_.AssertAcquired(); |
| 370 | DCHECK(!client_); |
| 371 | DCHECK(!closed_); |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 372 | DCHECK(runner->RunsTasksInCurrentSequence()); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 373 | |
| 374 | task_runner_ = std::move(runner); |
| 375 | client_ = client; |
| 376 | } |
| 377 | |
| 378 | void DetachClient() { |
| 379 | controller_->lock_.AssertAcquired(); |
| 380 | DCHECK(client_); |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 381 | DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 382 | DCHECK(!closed_); |
| 383 | |
| 384 | task_runner_ = nullptr; |
| 385 | client_ = nullptr; |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 386 | sync_watcher_.reset(); |
| 387 | } |
| 388 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 389 | uint32_t EnqueueSyncMessage(MessageWrapper message) { |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 390 | controller_->lock_.AssertAcquired(); |
| 391 | uint32_t id = GenerateSyncMessageId(); |
| 392 | sync_messages_.emplace(id, std::move(message)); |
| 393 | SignalSyncMessageEvent(); |
| 394 | return id; |
| 395 | } |
| 396 | |
| 397 | void SignalSyncMessageEvent() { |
| 398 | controller_->lock_.AssertAcquired(); |
yzshen | e25b5d5 | 2017-02-28 21:56:31 | [diff] [blame] | 399 | |
| 400 | if (sync_message_event_) |
| 401 | sync_message_event_->Signal(); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 402 | } |
| 403 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 404 | MessageWrapper PopSyncMessage(uint32_t id) { |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 405 | controller_->lock_.AssertAcquired(); |
| 406 | if (sync_messages_.empty() || sync_messages_.front().first != id) |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 407 | return MessageWrapper(); |
| 408 | MessageWrapper message = std::move(sync_messages_.front().second); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 409 | sync_messages_.pop(); |
| 410 | return message; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | // mojo::InterfaceEndpointController: |
| 414 | bool SendMessage(mojo::Message* message) override { |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 415 | DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 416 | message->set_interface_id(id_); |
| 417 | return controller_->SendMessage(message); |
| 418 | } |
| 419 | |
| 420 | void AllowWokenUpBySyncWatchOnSameThread() override { |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 421 | DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 422 | |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 423 | EnsureSyncWatcherExists(); |
| 424 | sync_watcher_->AllowWokenUpBySyncWatchOnSameThread(); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | bool SyncWatch(const bool* should_stop) override { |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 428 | DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 429 | |
| 430 | // It's not legal to make sync calls from the master endpoint's thread, |
| 431 | // and in fact they must only happen from the proxy task runner. |
rockot | 7604e7b7 | 2016-07-28 17:37:39 | [diff] [blame] | 432 | DCHECK(!controller_->task_runner_->BelongsToCurrentThread()); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 433 | DCHECK(controller_->proxy_task_runner_->BelongsToCurrentThread()); |
| 434 | |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 435 | EnsureSyncWatcherExists(); |
| 436 | return sync_watcher_->SyncWatch(should_stop); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | private: |
| 440 | friend class base::RefCountedThreadSafe<Endpoint>; |
| 441 | |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 442 | ~Endpoint() override { |
| 443 | controller_->lock_.AssertAcquired(); |
| 444 | DCHECK(!client_); |
| 445 | DCHECK(closed_); |
| 446 | DCHECK(peer_closed_); |
| 447 | DCHECK(!sync_watcher_); |
| 448 | } |
| 449 | |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 450 | void OnSyncMessageEventReady() { |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 451 | DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 452 | |
| 453 | scoped_refptr<Endpoint> keepalive(this); |
| 454 | scoped_refptr<AssociatedGroupController> controller_keepalive( |
| 455 | controller_); |
| 456 | |
| 457 | bool reset_sync_watcher = false; |
| 458 | { |
| 459 | base::AutoLock locker(controller_->lock_); |
| 460 | bool more_to_process = false; |
| 461 | if (!sync_messages_.empty()) { |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 462 | MessageWrapper message_wrapper = |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 463 | std::move(sync_messages_.front().second); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 464 | sync_messages_.pop(); |
| 465 | |
| 466 | bool dispatch_succeeded; |
| 467 | mojo::InterfaceEndpointClient* client = client_; |
| 468 | { |
| 469 | base::AutoUnlock unlocker(controller_->lock_); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 470 | dispatch_succeeded = |
| 471 | client->HandleIncomingMessage(&message_wrapper.value()); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | if (!sync_messages_.empty()) |
| 475 | more_to_process = true; |
| 476 | |
| 477 | if (!dispatch_succeeded) |
| 478 | controller_->RaiseError(); |
| 479 | } |
| 480 | |
| 481 | if (!more_to_process) |
| 482 | sync_message_event_->Reset(); |
| 483 | |
| 484 | // If there are no queued sync messages and the peer has closed, there |
| 485 | // there won't be incoming sync messages in the future. |
| 486 | reset_sync_watcher = !more_to_process && peer_closed_; |
| 487 | } |
| 488 | |
| 489 | if (reset_sync_watcher) { |
| 490 | // If a SyncWatch() call (or multiple ones) of this interface endpoint |
| 491 | // is on the call stack, resetting the sync watcher will allow it to |
| 492 | // exit when the call stack unwinds to that frame. |
| 493 | sync_watcher_.reset(); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | void EnsureSyncWatcherExists() { |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 498 | DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 499 | if (sync_watcher_) |
| 500 | return; |
| 501 | |
| 502 | { |
| 503 | base::AutoLock locker(controller_->lock_); |
yzshen | e25b5d5 | 2017-02-28 21:56:31 | [diff] [blame] | 504 | if (!sync_message_event_) { |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 505 | sync_message_event_ = base::MakeUnique<base::WaitableEvent>( |
| 506 | base::WaitableEvent::ResetPolicy::MANUAL, |
| 507 | base::WaitableEvent::InitialState::NOT_SIGNALED); |
yzshen | e25b5d5 | 2017-02-28 21:56:31 | [diff] [blame] | 508 | if (peer_closed_ || !sync_messages_.empty()) |
| 509 | SignalSyncMessageEvent(); |
| 510 | } |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 511 | } |
| 512 | |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 513 | sync_watcher_ = base::MakeUnique<mojo::SyncEventWatcher>( |
| 514 | sync_message_event_.get(), |
| 515 | base::Bind(&Endpoint::OnSyncMessageEventReady, |
| 516 | base::Unretained(this))); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | uint32_t GenerateSyncMessageId() { |
| 520 | // Overflow is fine. |
| 521 | uint32_t id = next_sync_message_id_++; |
| 522 | DCHECK(sync_messages_.empty() || sync_messages_.front().first != id); |
| 523 | return id; |
| 524 | } |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 525 | |
| 526 | ChannelAssociatedGroupController* const controller_; |
| 527 | const mojo::InterfaceId id_; |
| 528 | |
| 529 | bool closed_ = false; |
| 530 | bool peer_closed_ = false; |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 531 | bool handle_created_ = false; |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 532 | base::Optional<mojo::DisconnectReason> disconnect_reason_; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 533 | mojo::InterfaceEndpointClient* client_ = nullptr; |
Sam McNally | de5ae67 | 2017-06-19 23:34:45 | [diff] [blame] | 534 | scoped_refptr<base::SequencedTaskRunner> task_runner_; |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 535 | std::unique_ptr<mojo::SyncEventWatcher> sync_watcher_; |
| 536 | std::unique_ptr<base::WaitableEvent> sync_message_event_; |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 537 | std::queue<std::pair<uint32_t, MessageWrapper>> sync_messages_; |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 538 | uint32_t next_sync_message_id_ = 0; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 539 | |
| 540 | DISALLOW_COPY_AND_ASSIGN(Endpoint); |
| 541 | }; |
| 542 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 543 | class ControlMessageProxyThunk : public MessageReceiver { |
| 544 | public: |
| 545 | explicit ControlMessageProxyThunk( |
| 546 | ChannelAssociatedGroupController* controller) |
| 547 | : controller_(controller) {} |
| 548 | |
| 549 | private: |
| 550 | // MessageReceiver: |
| 551 | bool Accept(mojo::Message* message) override { |
| 552 | return controller_->SendMessage(message); |
| 553 | } |
| 554 | |
| 555 | ChannelAssociatedGroupController* controller_; |
| 556 | |
| 557 | DISALLOW_COPY_AND_ASSIGN(ControlMessageProxyThunk); |
| 558 | }; |
| 559 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 560 | ~ChannelAssociatedGroupController() override { |
rockot | b01ef6a | 2016-07-27 03:24:32 | [diff] [blame] | 561 | DCHECK(!connector_); |
| 562 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 563 | base::AutoLock locker(lock_); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 564 | for (auto iter = endpoints_.begin(); iter != endpoints_.end();) { |
| 565 | Endpoint* endpoint = iter->second.get(); |
| 566 | ++iter; |
| 567 | |
yzshen | e003d59 | 2017-01-24 21:42:17 | [diff] [blame] | 568 | if (!endpoint->closed()) { |
| 569 | // This happens when a NotifyPeerEndpointClosed message been received, |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 570 | // but the interface ID hasn't been used to create local endpoint |
| 571 | // handle. |
yzshen | e003d59 | 2017-01-24 21:42:17 | [diff] [blame] | 572 | DCHECK(!endpoint->client()); |
| 573 | DCHECK(endpoint->peer_closed()); |
| 574 | MarkClosedAndMaybeRemove(endpoint); |
| 575 | } else { |
| 576 | MarkPeerClosedAndMaybeRemove(endpoint); |
| 577 | } |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | DCHECK(endpoints_.empty()); |
| 581 | } |
| 582 | |
| 583 | bool SendMessage(mojo::Message* message) { |
rockot | 7604e7b7 | 2016-07-28 17:37:39 | [diff] [blame] | 584 | if (task_runner_->BelongsToCurrentThread()) { |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 585 | DCHECK(thread_checker_.CalledOnValidThread()); |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 586 | if (!connector_ || paused_) { |
rockot | c4cc691e | 2016-08-19 18:48:57 | [diff] [blame] | 587 | outgoing_messages_.emplace_back(std::move(*message)); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 588 | return true; |
| 589 | } |
| 590 | return connector_->Accept(message); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 591 | } else { |
rockot | becd3f74 | 2016-11-08 20:47:00 | [diff] [blame] | 592 | // We always post tasks to the master endpoint thread when called from |
| 593 | // other threads in order to simulate IPC::ChannelProxy::Send behavior. |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 594 | task_runner_->PostTask( |
| 595 | FROM_HERE, |
| 596 | base::Bind( |
| 597 | &ChannelAssociatedGroupController::SendMessageOnMasterThread, |
rockot | c4cc691e | 2016-08-19 18:48:57 | [diff] [blame] | 598 | this, base::Passed(message))); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 599 | return true; |
| 600 | } |
| 601 | } |
| 602 | |
rockot | c4cc691e | 2016-08-19 18:48:57 | [diff] [blame] | 603 | void SendMessageOnMasterThread(mojo::Message message) { |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 604 | DCHECK(thread_checker_.CalledOnValidThread()); |
rockot | c4cc691e | 2016-08-19 18:48:57 | [diff] [blame] | 605 | if (!SendMessage(&message)) |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 606 | RaiseError(); |
| 607 | } |
| 608 | |
| 609 | void OnPipeError() { |
| 610 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 611 | |
| 612 | // We keep |this| alive here because it's possible for the notifications |
| 613 | // below to release all other references. |
| 614 | scoped_refptr<ChannelAssociatedGroupController> keepalive(this); |
| 615 | |
| 616 | base::AutoLock locker(lock_); |
| 617 | encountered_error_ = true; |
| 618 | |
| 619 | std::vector<scoped_refptr<Endpoint>> endpoints_to_notify; |
| 620 | for (auto iter = endpoints_.begin(); iter != endpoints_.end();) { |
| 621 | Endpoint* endpoint = iter->second.get(); |
| 622 | ++iter; |
| 623 | |
| 624 | if (endpoint->client()) |
| 625 | endpoints_to_notify.push_back(endpoint); |
| 626 | |
| 627 | MarkPeerClosedAndMaybeRemove(endpoint); |
| 628 | } |
| 629 | |
| 630 | for (auto& endpoint : endpoints_to_notify) { |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 631 | // Because a notification may in turn detach any endpoint, we have to |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 632 | // check each client again here. |
| 633 | if (endpoint->client()) |
| 634 | NotifyEndpointOfError(endpoint.get(), false /* force_async */); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | void NotifyEndpointOfError(Endpoint* endpoint, bool force_async) { |
| 639 | lock_.AssertAcquired(); |
| 640 | DCHECK(endpoint->task_runner() && endpoint->client()); |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 641 | if (endpoint->task_runner()->RunsTasksInCurrentSequence() && !force_async) { |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 642 | mojo::InterfaceEndpointClient* client = endpoint->client(); |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 643 | base::Optional<mojo::DisconnectReason> reason( |
| 644 | endpoint->disconnect_reason()); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 645 | |
| 646 | base::AutoUnlock unlocker(lock_); |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 647 | client->NotifyError(reason); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 648 | } else { |
| 649 | endpoint->task_runner()->PostTask( |
| 650 | FROM_HERE, |
tzik | 1d692a2e | 2017-07-03 11:01:26 | [diff] [blame] | 651 | base::Bind(&ChannelAssociatedGroupController:: |
| 652 | NotifyEndpointOfErrorOnEndpointThread, |
| 653 | this, endpoint->id(), base::Unretained(endpoint))); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 657 | void NotifyEndpointOfErrorOnEndpointThread(mojo::InterfaceId id, |
| 658 | Endpoint* endpoint) { |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 659 | base::AutoLock locker(lock_); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 660 | auto iter = endpoints_.find(id); |
| 661 | if (iter == endpoints_.end() || iter->second.get() != endpoint) |
| 662 | return; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 663 | if (!endpoint->client()) |
| 664 | return; |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 665 | |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 666 | DCHECK(endpoint->task_runner()->RunsTasksInCurrentSequence()); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 667 | NotifyEndpointOfError(endpoint, false /* force_async */); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | void MarkClosedAndMaybeRemove(Endpoint* endpoint) { |
| 671 | lock_.AssertAcquired(); |
| 672 | endpoint->set_closed(); |
| 673 | if (endpoint->closed() && endpoint->peer_closed()) |
| 674 | endpoints_.erase(endpoint->id()); |
| 675 | } |
| 676 | |
| 677 | void MarkPeerClosedAndMaybeRemove(Endpoint* endpoint) { |
| 678 | lock_.AssertAcquired(); |
| 679 | endpoint->set_peer_closed(); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 680 | endpoint->SignalSyncMessageEvent(); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 681 | if (endpoint->closed() && endpoint->peer_closed()) |
| 682 | endpoints_.erase(endpoint->id()); |
| 683 | } |
| 684 | |
| 685 | Endpoint* FindOrInsertEndpoint(mojo::InterfaceId id, bool* inserted) { |
| 686 | lock_.AssertAcquired(); |
| 687 | DCHECK(!inserted || !*inserted); |
| 688 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 689 | Endpoint* endpoint = FindEndpoint(id); |
| 690 | if (!endpoint) { |
| 691 | endpoint = new Endpoint(this, id); |
| 692 | endpoints_.insert({id, endpoint}); |
| 693 | if (inserted) |
| 694 | *inserted = true; |
| 695 | } |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 696 | return endpoint; |
| 697 | } |
| 698 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 699 | Endpoint* FindEndpoint(mojo::InterfaceId id) { |
| 700 | lock_.AssertAcquired(); |
| 701 | auto iter = endpoints_.find(id); |
| 702 | return iter != endpoints_.end() ? iter->second.get() : nullptr; |
| 703 | } |
| 704 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 705 | // mojo::MessageReceiver: |
| 706 | bool Accept(mojo::Message* message) override { |
| 707 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 708 | |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 709 | if (!message->DeserializeAssociatedEndpointHandles(this)) |
| 710 | return false; |
| 711 | |
| 712 | if (mojo::PipeControlMessageHandler::IsPipeControlMessage(message)) |
| 713 | return control_message_handler_.Accept(message); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 714 | |
| 715 | mojo::InterfaceId id = message->interface_id(); |
| 716 | DCHECK(mojo::IsValidInterfaceId(id)); |
| 717 | |
| 718 | base::AutoLock locker(lock_); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 719 | Endpoint* endpoint = FindEndpoint(id); |
| 720 | if (!endpoint) |
| 721 | return true; |
| 722 | |
| 723 | mojo::InterfaceEndpointClient* client = endpoint->client(); |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 724 | if (!client || !endpoint->task_runner()->RunsTasksInCurrentSequence()) { |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 725 | // No client has been bound yet or the client runs tasks on another |
| 726 | // thread. We assume the other thread must always be the one on which |
| 727 | // |proxy_task_runner_| runs tasks, since that's the only valid scenario. |
| 728 | // |
| 729 | // If the client is not yet bound, it must be bound by the time this task |
| 730 | // runs or else it's programmer error. |
| 731 | DCHECK(proxy_task_runner_); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 732 | |
rockot | c4cc691e | 2016-08-19 18:48:57 | [diff] [blame] | 733 | if (message->has_flag(mojo::Message::kFlagIsSync)) { |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 734 | MessageWrapper message_wrapper(this, std::move(*message)); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 735 | // Sync messages may need to be handled by the endpoint if it's blocking |
| 736 | // on a sync reply. We pass ownership of the message to the endpoint's |
| 737 | // sync message queue. If the endpoint was blocking, it will dequeue the |
| 738 | // message and dispatch it. Otherwise the posted |AcceptSyncMessage()| |
| 739 | // call will dequeue the message and dispatch it. |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 740 | uint32_t message_id = |
| 741 | endpoint->EnqueueSyncMessage(std::move(message_wrapper)); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 742 | proxy_task_runner_->PostTask( |
| 743 | FROM_HERE, |
| 744 | base::Bind(&ChannelAssociatedGroupController::AcceptSyncMessage, |
| 745 | this, id, message_id)); |
| 746 | return true; |
| 747 | } |
| 748 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 749 | proxy_task_runner_->PostTask( |
| 750 | FROM_HERE, |
| 751 | base::Bind(&ChannelAssociatedGroupController::AcceptOnProxyThread, |
rockot | c4cc691e | 2016-08-19 18:48:57 | [diff] [blame] | 752 | this, base::Passed(message))); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 753 | return true; |
| 754 | } |
| 755 | |
| 756 | // We do not expect to receive sync responses on the master endpoint thread. |
| 757 | // If it's happening, it's a bug. |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 758 | DCHECK(!message->has_flag(mojo::Message::kFlagIsSync) || |
| 759 | !message->has_flag(mojo::Message::kFlagIsResponse)); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 760 | |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 761 | base::AutoUnlock unlocker(lock_); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 762 | return client->HandleIncomingMessage(message); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 763 | } |
| 764 | |
rockot | c4cc691e | 2016-08-19 18:48:57 | [diff] [blame] | 765 | void AcceptOnProxyThread(mojo::Message message) { |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 766 | DCHECK(proxy_task_runner_->BelongsToCurrentThread()); |
| 767 | |
rockot | c4cc691e | 2016-08-19 18:48:57 | [diff] [blame] | 768 | mojo::InterfaceId id = message.interface_id(); |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 769 | DCHECK(mojo::IsValidInterfaceId(id) && !mojo::IsMasterInterfaceId(id)); |
| 770 | |
| 771 | base::AutoLock locker(lock_); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 772 | Endpoint* endpoint = FindEndpoint(id); |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 773 | if (!endpoint) |
| 774 | return; |
| 775 | |
| 776 | mojo::InterfaceEndpointClient* client = endpoint->client(); |
| 777 | if (!client) |
| 778 | return; |
| 779 | |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 780 | DCHECK(endpoint->task_runner()->RunsTasksInCurrentSequence()); |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 781 | |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 782 | // Sync messages should never make their way to this method. |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 783 | DCHECK(!message.has_flag(mojo::Message::kFlagIsSync)); |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 784 | |
| 785 | bool result = false; |
| 786 | { |
| 787 | base::AutoUnlock unlocker(lock_); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 788 | result = client->HandleIncomingMessage(&message); |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | if (!result) |
| 792 | RaiseError(); |
| 793 | } |
| 794 | |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 795 | void AcceptSyncMessage(mojo::InterfaceId interface_id, uint32_t message_id) { |
| 796 | DCHECK(proxy_task_runner_->BelongsToCurrentThread()); |
| 797 | |
| 798 | base::AutoLock locker(lock_); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 799 | Endpoint* endpoint = FindEndpoint(interface_id); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 800 | if (!endpoint) |
| 801 | return; |
| 802 | |
csharrison | 1af8d6ab | 2017-04-21 17:47:23 | [diff] [blame] | 803 | // Careful, if the endpoint is detached its members are cleared. Check for |
| 804 | // that before dereferencing. |
| 805 | mojo::InterfaceEndpointClient* client = endpoint->client(); |
| 806 | if (!client) |
| 807 | return; |
| 808 | |
peary2 | 8cd3bd2 | 2017-06-29 02:15:28 | [diff] [blame] | 809 | DCHECK(endpoint->task_runner()->RunsTasksInCurrentSequence()); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 810 | MessageWrapper message_wrapper = endpoint->PopSyncMessage(message_id); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 811 | |
| 812 | // The message must have already been dequeued by the endpoint waking up |
| 813 | // from a sync wait. Nothing to do. |
yzshen | ea784ea | 2017-01-31 21:20:20 | [diff] [blame] | 814 | if (message_wrapper.value().IsNull()) |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 815 | return; |
| 816 | |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 817 | bool result = false; |
| 818 | { |
| 819 | base::AutoUnlock unlocker(lock_); |
yzshen | 0a597131 | 2017-02-02 05:13:47 | [diff] [blame] | 820 | result = client->HandleIncomingMessage(&message_wrapper.value()); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | if (!result) |
| 824 | RaiseError(); |
| 825 | } |
| 826 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 827 | // mojo::PipeControlMessageHandlerDelegate: |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 828 | bool OnPeerAssociatedEndpointClosed( |
| 829 | mojo::InterfaceId id, |
| 830 | const base::Optional<mojo::DisconnectReason>& reason) override { |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 831 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 832 | |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 833 | DCHECK(!mojo::IsMasterInterfaceId(id) || reason); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 834 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 835 | scoped_refptr<ChannelAssociatedGroupController> keepalive(this); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 836 | base::AutoLock locker(lock_); |
| 837 | scoped_refptr<Endpoint> endpoint = FindOrInsertEndpoint(id, nullptr); |
yzshen | 8be41d3a | 2017-01-23 20:40:37 | [diff] [blame] | 838 | if (reason) |
| 839 | endpoint->set_disconnect_reason(reason); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 840 | if (!endpoint->peer_closed()) { |
| 841 | if (endpoint->client()) |
| 842 | NotifyEndpointOfError(endpoint.get(), false /* force_async */); |
| 843 | MarkPeerClosedAndMaybeRemove(endpoint.get()); |
| 844 | } |
| 845 | |
| 846 | return true; |
| 847 | } |
| 848 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 849 | // Checked in places which must be run on the master endpoint's thread. |
| 850 | base::ThreadChecker thread_checker_; |
| 851 | |
| 852 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 853 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 854 | scoped_refptr<base::SingleThreadTaskRunner> proxy_task_runner_; |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 855 | const bool set_interface_id_namespace_bit_; |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 856 | bool paused_ = false; |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 857 | std::unique_ptr<mojo::Connector> connector_; |
rockot | 222e7dd | 2016-08-24 23:37:11 | [diff] [blame] | 858 | mojo::FilterChain filters_; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 859 | mojo::PipeControlMessageHandler control_message_handler_; |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 860 | ControlMessageProxyThunk control_message_proxy_thunk_; |
rockot | 5890954 | 2016-11-10 20:05:45 | [diff] [blame] | 861 | |
| 862 | // NOTE: It is unsafe to call into this object while holding |lock_|. |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 863 | mojo::PipeControlMessageProxy control_message_proxy_; |
| 864 | |
| 865 | // Outgoing messages that were sent before this controller was bound to a |
| 866 | // real message pipe. |
rockot | c4cc691e | 2016-08-19 18:48:57 | [diff] [blame] | 867 | std::vector<mojo::Message> outgoing_messages_; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 868 | |
| 869 | // Guards the fields below for thread-safe access. |
| 870 | base::Lock lock_; |
| 871 | |
| 872 | bool encountered_error_ = false; |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 873 | |
| 874 | // ID #1 is reserved for the mojom::Channel interface. |
| 875 | uint32_t next_interface_id_ = 2; |
| 876 | |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 877 | std::map<uint32_t, scoped_refptr<Endpoint>> endpoints_; |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 878 | |
| 879 | DISALLOW_COPY_AND_ASSIGN(ChannelAssociatedGroupController); |
| 880 | }; |
| 881 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 882 | class MojoBootstrapImpl : public MojoBootstrap { |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 883 | public: |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 884 | MojoBootstrapImpl( |
| 885 | mojo::ScopedMessagePipeHandle handle, |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 886 | const scoped_refptr<ChannelAssociatedGroupController> controller) |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 887 | : controller_(controller), |
| 888 | associated_group_(controller), |
| 889 | handle_(std::move(handle)) {} |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 890 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 891 | ~MojoBootstrapImpl() override { |
| 892 | controller_->ShutDown(); |
rockot | 02b8e18 | 2016-07-13 20:08:30 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | private: |
rockot | a628d0b | 2017-02-09 08:40:15 | [diff] [blame] | 896 | void Connect(mojom::ChannelAssociatedPtr* sender, |
| 897 | mojom::ChannelAssociatedRequest* receiver) override { |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 898 | controller_->Bind(std::move(handle_)); |
rockot | a628d0b | 2017-02-09 08:40:15 | [diff] [blame] | 899 | controller_->CreateChannelEndpoints(sender, receiver); |
msramek | 5507fee | 2016-07-22 10:06:21 | [diff] [blame] | 900 | } |
| 901 | |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 902 | void Pause() override { |
| 903 | controller_->Pause(); |
| 904 | } |
| 905 | |
| 906 | void Unpause() override { |
| 907 | controller_->Unpause(); |
rockot | 401fb2c | 2016-09-06 18:35:57 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | void Flush() override { |
| 911 | controller_->FlushOutgoingMessages(); |
| 912 | } |
| 913 | |
msramek | 5507fee | 2016-07-22 10:06:21 | [diff] [blame] | 914 | mojo::AssociatedGroup* GetAssociatedGroup() override { |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 915 | return &associated_group_; |
msramek | 5507fee | 2016-07-22 10:06:21 | [diff] [blame] | 916 | } |
| 917 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 918 | scoped_refptr<ChannelAssociatedGroupController> controller_; |
yzshen | 2859a2ac | 2017-02-14 22:24:25 | [diff] [blame] | 919 | mojo::AssociatedGroup associated_group_; |
msramek | 5507fee | 2016-07-22 10:06:21 | [diff] [blame] | 920 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 921 | mojo::ScopedMessagePipeHandle handle_; |
msramek | 5507fee | 2016-07-22 10:06:21 | [diff] [blame] | 922 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 923 | DISALLOW_COPY_AND_ASSIGN(MojoBootstrapImpl); |
msramek | 5507fee | 2016-07-22 10:06:21 | [diff] [blame] | 924 | }; |
| 925 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 926 | } // namespace |
| 927 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 928 | // static |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 929 | std::unique_ptr<MojoBootstrap> MojoBootstrap::Create( |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 930 | mojo::ScopedMessagePipeHandle handle, |
| 931 | Channel::Mode mode, |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 932 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) { |
| 933 | return base::MakeUnique<MojoBootstrapImpl>( |
rockot | a628d0b | 2017-02-09 08:40:15 | [diff] [blame] | 934 | std::move(handle), new ChannelAssociatedGroupController( |
| 935 | mode == Channel::MODE_SERVER, ipc_task_runner)); |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 936 | } |
| 937 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 938 | } // namespace IPC |