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