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