OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ipc/ipc_mojo_bootstrap.h" | 5 #include "ipc/ipc_mojo_bootstrap.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <memory> | 10 #include <memory> |
11 #include <utility> | 11 #include <utility> |
| 12 #include <vector> |
12 | 13 |
13 #include "base/callback.h" | 14 #include "base/callback.h" |
14 #include "base/logging.h" | 15 #include "base/logging.h" |
15 #include "base/macros.h" | 16 #include "base/macros.h" |
16 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
17 #include "base/process/process_handle.h" | |
18 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
19 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
20 #include "base/synchronization/lock.h" | 20 #include "base/synchronization/lock.h" |
21 #include "base/threading/thread_task_runner_handle.h" | 21 #include "base/threading/thread_task_runner_handle.h" |
22 #include "build/build_config.h" | 22 #include "build/build_config.h" |
23 #include "ipc/ipc_message_utils.h" | 23 #include "ipc/ipc_message_utils.h" |
24 #include "ipc/ipc_platform_file.h" | 24 #include "ipc/ipc_platform_file.h" |
25 #include "mojo/public/cpp/bindings/associated_group.h" | 25 #include "mojo/public/cpp/bindings/associated_group.h" |
26 #include "mojo/public/cpp/bindings/associated_group_controller.h" | 26 #include "mojo/public/cpp/bindings/associated_group_controller.h" |
27 #include "mojo/public/cpp/bindings/binding.h" | 27 #include "mojo/public/cpp/bindings/binding.h" |
28 #include "mojo/public/cpp/bindings/connector.h" | 28 #include "mojo/public/cpp/bindings/connector.h" |
29 #include "mojo/public/cpp/bindings/interface_endpoint_client.h" | 29 #include "mojo/public/cpp/bindings/interface_endpoint_client.h" |
30 #include "mojo/public/cpp/bindings/interface_endpoint_controller.h" | 30 #include "mojo/public/cpp/bindings/interface_endpoint_controller.h" |
31 #include "mojo/public/cpp/bindings/interface_id.h" | 31 #include "mojo/public/cpp/bindings/interface_id.h" |
| 32 #include "mojo/public/cpp/bindings/message.h" |
32 #include "mojo/public/cpp/bindings/message_header_validator.h" | 33 #include "mojo/public/cpp/bindings/message_header_validator.h" |
33 #include "mojo/public/cpp/bindings/pipe_control_message_handler.h" | 34 #include "mojo/public/cpp/bindings/pipe_control_message_handler.h" |
34 #include "mojo/public/cpp/bindings/pipe_control_message_handler_delegate.h" | 35 #include "mojo/public/cpp/bindings/pipe_control_message_handler_delegate.h" |
35 #include "mojo/public/cpp/bindings/pipe_control_message_proxy.h" | 36 #include "mojo/public/cpp/bindings/pipe_control_message_proxy.h" |
36 | 37 |
37 namespace IPC { | 38 namespace IPC { |
38 | 39 |
39 namespace { | 40 namespace { |
40 | 41 |
41 class ChannelAssociatedGroupController | 42 class ChannelAssociatedGroupController |
42 : public mojo::AssociatedGroupController, | 43 : public mojo::AssociatedGroupController, |
43 public mojo::MessageReceiver, | 44 public mojo::MessageReceiver, |
44 public mojo::PipeControlMessageHandlerDelegate { | 45 public mojo::PipeControlMessageHandlerDelegate { |
45 public: | 46 public: |
46 ChannelAssociatedGroupController(bool set_interface_id_namespace_bit, | 47 ChannelAssociatedGroupController( |
47 mojo::ScopedMessagePipeHandle handle) | 48 bool set_interface_id_namespace_bit, |
| 49 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) |
48 : mojo::AssociatedGroupController(base::ThreadTaskRunnerHandle::Get()), | 50 : mojo::AssociatedGroupController(base::ThreadTaskRunnerHandle::Get()), |
49 task_runner_(base::ThreadTaskRunnerHandle::Get()), | 51 task_runner_(ipc_task_runner), |
50 id_namespace_mask_(set_interface_id_namespace_bit ? | 52 proxy_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
51 mojo::kInterfaceIdNamespaceMask : 0), | 53 set_interface_id_namespace_bit_(set_interface_id_namespace_bit), |
52 associated_group_(CreateAssociatedGroup()), | |
53 connector_(std::move(handle), mojo::Connector::SINGLE_THREADED_SEND, | |
54 base::ThreadTaskRunnerHandle::Get()), | |
55 header_validator_( | 54 header_validator_( |
56 "IPC::mojom::Bootstrap [master] MessageHeaderValidator", this), | 55 "IPC::mojom::Bootstrap [master] MessageHeaderValidator", this), |
57 control_message_handler_(this), | 56 control_message_handler_(this), |
58 control_message_proxy_(&connector_) { | 57 control_message_proxy_thunk_(this), |
59 connector_.set_incoming_receiver(&header_validator_); | 58 control_message_proxy_(&control_message_proxy_thunk_) { |
60 connector_.set_connection_error_handler( | 59 thread_checker_.DetachFromThread(); |
61 base::Bind(&ChannelAssociatedGroupController::OnPipeError, | |
62 base::Unretained(this))); | |
63 control_message_handler_.SetDescription( | 60 control_message_handler_.SetDescription( |
64 "IPC::mojom::Bootstrap [master] PipeControlMessageHandler"); | 61 "IPC::mojom::Bootstrap [master] PipeControlMessageHandler"); |
65 } | 62 } |
66 | 63 |
67 mojo::AssociatedGroup* associated_group() { return associated_group_.get(); } | 64 void Bind(mojo::ScopedMessagePipeHandle handle) { |
| 65 DCHECK(thread_checker_.CalledOnValidThread()); |
| 66 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 67 thread_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 68 connector_.reset(new mojo::Connector( |
| 69 std::move(handle), mojo::Connector::SINGLE_THREADED_SEND, |
| 70 task_runner_)); |
| 71 connector_->set_incoming_receiver(&header_validator_); |
| 72 connector_->set_connection_error_handler( |
| 73 base::Bind(&ChannelAssociatedGroupController::OnPipeError, |
| 74 base::Unretained(this))); |
| 75 |
| 76 std::vector<std::unique_ptr<mojo::Message>> outgoing_messages; |
| 77 std::swap(outgoing_messages, outgoing_messages_); |
| 78 for (auto& message : outgoing_messages) |
| 79 SendMessage(message.get()); |
| 80 } |
| 81 |
| 82 void CreateChannelEndpoints(mojom::ChannelAssociatedPtr* sender, |
| 83 mojom::ChannelAssociatedRequest* receiver) { |
| 84 mojo::InterfaceId sender_id, receiver_id; |
| 85 if (set_interface_id_namespace_bit_) { |
| 86 sender_id = 1 | mojo::kInterfaceIdNamespaceMask; |
| 87 receiver_id = 1; |
| 88 } else { |
| 89 sender_id = 1; |
| 90 receiver_id = 1 | mojo::kInterfaceIdNamespaceMask; |
| 91 } |
| 92 |
| 93 { |
| 94 base::AutoLock locker(lock_); |
| 95 Endpoint* sender_endpoint = new Endpoint(this, sender_id); |
| 96 Endpoint* receiver_endpoint = new Endpoint(this, receiver_id); |
| 97 endpoints_.insert({ sender_id, sender_endpoint }); |
| 98 endpoints_.insert({ receiver_id, receiver_endpoint }); |
| 99 } |
| 100 |
| 101 mojo::ScopedInterfaceEndpointHandle sender_handle = |
| 102 CreateScopedInterfaceEndpointHandle(sender_id, true); |
| 103 mojo::ScopedInterfaceEndpointHandle receiver_handle = |
| 104 CreateScopedInterfaceEndpointHandle(receiver_id, true); |
| 105 |
| 106 sender->Bind(mojom::ChannelAssociatedPtrInfo(std::move(sender_handle), 0)); |
| 107 receiver->Bind(std::move(receiver_handle)); |
| 108 } |
68 | 109 |
69 void ShutDown() { | 110 void ShutDown() { |
70 DCHECK(thread_checker_.CalledOnValidThread()); | 111 DCHECK(thread_checker_.CalledOnValidThread()); |
71 connector_.CloseMessagePipe(); | 112 connector_->CloseMessagePipe(); |
72 OnPipeError(); | 113 OnPipeError(); |
73 associated_group_.reset(); | 114 connector_.reset(); |
74 } | |
75 | |
76 void SetProxyTaskRunner( | |
77 scoped_refptr<base::SingleThreadTaskRunner> proxy_task_runner) { | |
78 proxy_task_runner_ = proxy_task_runner; | |
79 } | 115 } |
80 | 116 |
81 // mojo::AssociatedGroupController: | 117 // mojo::AssociatedGroupController: |
82 void CreateEndpointHandlePair( | 118 void CreateEndpointHandlePair( |
83 mojo::ScopedInterfaceEndpointHandle* local_endpoint, | 119 mojo::ScopedInterfaceEndpointHandle* local_endpoint, |
84 mojo::ScopedInterfaceEndpointHandle* remote_endpoint) override { | 120 mojo::ScopedInterfaceEndpointHandle* remote_endpoint) override { |
85 base::AutoLock locker(lock_); | 121 base::AutoLock locker(lock_); |
86 uint32_t id = 0; | 122 uint32_t id = 0; |
87 do { | 123 do { |
88 if (next_interface_id_ >= mojo::kInterfaceIdNamespaceMask) | 124 if (next_interface_id_ >= mojo::kInterfaceIdNamespaceMask) |
89 next_interface_id_ = 1; | 125 next_interface_id_ = 2; |
90 id = (next_interface_id_++) | id_namespace_mask_; | 126 id = next_interface_id_++; |
| 127 if (set_interface_id_namespace_bit_) |
| 128 id |= mojo::kInterfaceIdNamespaceMask; |
91 } while (ContainsKey(endpoints_, id)); | 129 } while (ContainsKey(endpoints_, id)); |
92 | 130 |
93 Endpoint* endpoint = new Endpoint(this, id); | 131 Endpoint* endpoint = new Endpoint(this, id); |
94 if (encountered_error_) | 132 if (encountered_error_) |
95 endpoint->set_peer_closed(); | 133 endpoint->set_peer_closed(); |
96 endpoints_.insert({ id, endpoint }); | 134 endpoints_.insert({ id, endpoint }); |
97 | 135 |
98 *local_endpoint = CreateScopedInterfaceEndpointHandle(id, true); | 136 *local_endpoint = CreateScopedInterfaceEndpointHandle(id, true); |
99 *remote_endpoint = CreateScopedInterfaceEndpointHandle(id, false); | 137 *remote_endpoint = CreateScopedInterfaceEndpointHandle(id, false); |
100 } | 138 } |
(...skipping 13 matching lines...) Expand all Loading... |
114 } | 152 } |
115 | 153 |
116 void CloseEndpointHandle(mojo::InterfaceId id, bool is_local) override { | 154 void CloseEndpointHandle(mojo::InterfaceId id, bool is_local) override { |
117 if (!mojo::IsValidInterfaceId(id)) | 155 if (!mojo::IsValidInterfaceId(id)) |
118 return; | 156 return; |
119 | 157 |
120 base::AutoLock locker(lock_); | 158 base::AutoLock locker(lock_); |
121 if (!is_local) { | 159 if (!is_local) { |
122 DCHECK(ContainsKey(endpoints_, id)); | 160 DCHECK(ContainsKey(endpoints_, id)); |
123 DCHECK(!mojo::IsMasterInterfaceId(id)); | 161 DCHECK(!mojo::IsMasterInterfaceId(id)); |
124 NotifyEndpointClosedBeforeSent(id); | 162 control_message_proxy_.NotifyEndpointClosedBeforeSent(id); |
125 return; | 163 return; |
126 } | 164 } |
127 | 165 |
128 DCHECK(ContainsKey(endpoints_, id)); | 166 DCHECK(ContainsKey(endpoints_, id)); |
129 Endpoint* endpoint = endpoints_[id].get(); | 167 Endpoint* endpoint = endpoints_[id].get(); |
130 DCHECK(!endpoint->client()); | 168 DCHECK(!endpoint->client()); |
131 DCHECK(!endpoint->closed()); | 169 DCHECK(!endpoint->closed()); |
132 MarkClosedAndMaybeRemove(endpoint); | 170 MarkClosedAndMaybeRemove(endpoint); |
133 | 171 |
134 if (!mojo::IsMasterInterfaceId(id)) | 172 if (!mojo::IsMasterInterfaceId(id)) |
135 NotifyPeerEndpointClosed(id); | 173 control_message_proxy_.NotifyPeerEndpointClosed(id); |
136 } | 174 } |
137 | 175 |
138 mojo::InterfaceEndpointController* AttachEndpointClient( | 176 mojo::InterfaceEndpointController* AttachEndpointClient( |
139 const mojo::ScopedInterfaceEndpointHandle& handle, | 177 const mojo::ScopedInterfaceEndpointHandle& handle, |
140 mojo::InterfaceEndpointClient* client, | 178 mojo::InterfaceEndpointClient* client, |
141 scoped_refptr<base::SingleThreadTaskRunner> runner) override { | 179 scoped_refptr<base::SingleThreadTaskRunner> runner) override { |
142 const mojo::InterfaceId id = handle.id(); | 180 const mojo::InterfaceId id = handle.id(); |
143 | 181 |
144 DCHECK(mojo::IsValidInterfaceId(id)); | 182 DCHECK(mojo::IsValidInterfaceId(id)); |
145 DCHECK(client); | 183 DCHECK(client); |
(...skipping 17 matching lines...) Expand all Loading... |
163 DCHECK(mojo::IsValidInterfaceId(id)); | 201 DCHECK(mojo::IsValidInterfaceId(id)); |
164 | 202 |
165 base::AutoLock locker(lock_); | 203 base::AutoLock locker(lock_); |
166 DCHECK(ContainsKey(endpoints_, id)); | 204 DCHECK(ContainsKey(endpoints_, id)); |
167 | 205 |
168 Endpoint* endpoint = endpoints_[id].get(); | 206 Endpoint* endpoint = endpoints_[id].get(); |
169 endpoint->DetachClient(); | 207 endpoint->DetachClient(); |
170 } | 208 } |
171 | 209 |
172 void RaiseError() override { | 210 void RaiseError() override { |
173 if (task_runner_->BelongsToCurrentThread()) { | 211 if (IsRunningOnIPCThread()) { |
174 connector_.RaiseError(); | 212 connector_->RaiseError(); |
175 } else { | 213 } else { |
176 task_runner_->PostTask( | 214 task_runner_->PostTask( |
177 FROM_HERE, | 215 FROM_HERE, |
178 base::Bind(&ChannelAssociatedGroupController::RaiseError, this)); | 216 base::Bind(&ChannelAssociatedGroupController::RaiseError, this)); |
179 } | 217 } |
180 } | 218 } |
181 | 219 |
182 private: | 220 private: |
183 class Endpoint; | 221 class Endpoint; |
| 222 class ControlMessageProxyThunk; |
184 friend class Endpoint; | 223 friend class Endpoint; |
| 224 friend class ControlMessageProxyThunk; |
185 | 225 |
186 class Endpoint : public base::RefCountedThreadSafe<Endpoint>, | 226 class Endpoint : public base::RefCountedThreadSafe<Endpoint>, |
187 public mojo::InterfaceEndpointController { | 227 public mojo::InterfaceEndpointController { |
188 public: | 228 public: |
189 Endpoint(ChannelAssociatedGroupController* controller, mojo::InterfaceId id) | 229 Endpoint(ChannelAssociatedGroupController* controller, mojo::InterfaceId id) |
190 : controller_(controller), id_(id) {} | 230 : controller_(controller), id_(id) {} |
191 | 231 |
192 mojo::InterfaceId id() const { return id_; } | 232 mojo::InterfaceId id() const { return id_; } |
193 | 233 |
194 bool closed() const { | 234 bool closed() const { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 | 293 |
254 // TODO(rockot): Implement sync waiting. | 294 // TODO(rockot): Implement sync waiting. |
255 NOTREACHED(); | 295 NOTREACHED(); |
256 } | 296 } |
257 | 297 |
258 bool SyncWatch(const bool* should_stop) override { | 298 bool SyncWatch(const bool* should_stop) override { |
259 DCHECK(task_runner_->BelongsToCurrentThread()); | 299 DCHECK(task_runner_->BelongsToCurrentThread()); |
260 | 300 |
261 // It's not legal to make sync calls from the master endpoint's thread, | 301 // It's not legal to make sync calls from the master endpoint's thread, |
262 // and in fact they must only happen from the proxy task runner. | 302 // and in fact they must only happen from the proxy task runner. |
263 DCHECK(!controller_->task_runner_->BelongsToCurrentThread()); | 303 DCHECK(!controller_->IsRunningOnIPCThread()); |
264 DCHECK(controller_->proxy_task_runner_->BelongsToCurrentThread()); | 304 DCHECK(controller_->proxy_task_runner_->BelongsToCurrentThread()); |
265 | 305 |
266 // TODO(rockot): Implement sync waiting. | 306 // TODO(rockot): Implement sync waiting. |
267 NOTREACHED(); | 307 NOTREACHED(); |
268 return false; | 308 return false; |
269 } | 309 } |
270 | 310 |
271 private: | 311 private: |
272 friend class base::RefCountedThreadSafe<Endpoint>; | 312 friend class base::RefCountedThreadSafe<Endpoint>; |
273 | 313 |
274 ~Endpoint() override {} | 314 ~Endpoint() override {} |
275 | 315 |
276 ChannelAssociatedGroupController* const controller_; | 316 ChannelAssociatedGroupController* const controller_; |
277 const mojo::InterfaceId id_; | 317 const mojo::InterfaceId id_; |
278 | 318 |
279 bool closed_ = false; | 319 bool closed_ = false; |
280 bool peer_closed_ = false; | 320 bool peer_closed_ = false; |
281 mojo::InterfaceEndpointClient* client_ = nullptr; | 321 mojo::InterfaceEndpointClient* client_ = nullptr; |
282 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 322 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
283 | 323 |
284 DISALLOW_COPY_AND_ASSIGN(Endpoint); | 324 DISALLOW_COPY_AND_ASSIGN(Endpoint); |
285 }; | 325 }; |
286 | 326 |
| 327 class ControlMessageProxyThunk : public MessageReceiver { |
| 328 public: |
| 329 explicit ControlMessageProxyThunk( |
| 330 ChannelAssociatedGroupController* controller) |
| 331 : controller_(controller) {} |
| 332 |
| 333 private: |
| 334 // MessageReceiver: |
| 335 bool Accept(mojo::Message* message) override { |
| 336 return controller_->SendMessage(message); |
| 337 } |
| 338 |
| 339 ChannelAssociatedGroupController* controller_; |
| 340 |
| 341 DISALLOW_COPY_AND_ASSIGN(ControlMessageProxyThunk); |
| 342 }; |
| 343 |
287 ~ChannelAssociatedGroupController() override { | 344 ~ChannelAssociatedGroupController() override { |
288 base::AutoLock locker(lock_); | 345 base::AutoLock locker(lock_); |
289 | |
290 for (auto iter = endpoints_.begin(); iter != endpoints_.end();) { | 346 for (auto iter = endpoints_.begin(); iter != endpoints_.end();) { |
291 Endpoint* endpoint = iter->second.get(); | 347 Endpoint* endpoint = iter->second.get(); |
292 ++iter; | 348 ++iter; |
293 | 349 |
294 DCHECK(endpoint->closed()); | 350 DCHECK(endpoint->closed()); |
295 MarkPeerClosedAndMaybeRemove(endpoint); | 351 MarkPeerClosedAndMaybeRemove(endpoint); |
296 } | 352 } |
297 | 353 |
298 DCHECK(endpoints_.empty()); | 354 DCHECK(endpoints_.empty()); |
299 } | 355 } |
300 | 356 |
| 357 bool IsRunningOnIPCThread() { |
| 358 // |task_runner_| is always non-null but may incorrectly report that |
| 359 // BelongsToCurrentThread() == false during shutdown. By the time shutdown |
| 360 // occurs, |thread_task_runner_| will be non-null and is guaranteed to run |
| 361 // tasks on the same thread as |task_runner_|. |
| 362 return task_runner_->BelongsToCurrentThread() || |
| 363 (thread_task_runner_ && thread_task_runner_->BelongsToCurrentThread()); |
| 364 } |
| 365 |
301 bool SendMessage(mojo::Message* message) { | 366 bool SendMessage(mojo::Message* message) { |
302 if (task_runner_->BelongsToCurrentThread()) { | 367 if (IsRunningOnIPCThread()) { |
303 DCHECK(thread_checker_.CalledOnValidThread()); | 368 DCHECK(thread_checker_.CalledOnValidThread()); |
304 return connector_.Accept(message); | 369 if (!connector_) { |
| 370 // Pipe may not be bound yet, so we queue the message. |
| 371 std::unique_ptr<mojo::Message> queued_message(new mojo::Message); |
| 372 message->MoveTo(queued_message.get()); |
| 373 outgoing_messages_.emplace_back(std::move(queued_message)); |
| 374 return true; |
| 375 } |
| 376 return connector_->Accept(message); |
305 } else { | 377 } else { |
306 // We always post tasks to the master endpoint thread when called from the | 378 // We always post tasks to the master endpoint thread when called from the |
307 // proxy thread in order to simulate IPC::ChannelProxy::Send behavior. | 379 // proxy thread in order to simulate IPC::ChannelProxy::Send behavior. |
308 DCHECK(proxy_task_runner_ && | 380 DCHECK(proxy_task_runner_->BelongsToCurrentThread()); |
309 proxy_task_runner_->BelongsToCurrentThread()); | |
310 std::unique_ptr<mojo::Message> passed_message(new mojo::Message); | 381 std::unique_ptr<mojo::Message> passed_message(new mojo::Message); |
311 message->MoveTo(passed_message.get()); | 382 message->MoveTo(passed_message.get()); |
312 task_runner_->PostTask( | 383 task_runner_->PostTask( |
313 FROM_HERE, | 384 FROM_HERE, |
314 base::Bind( | 385 base::Bind( |
315 &ChannelAssociatedGroupController::SendMessageOnMasterThread, | 386 &ChannelAssociatedGroupController::SendMessageOnMasterThread, |
316 this, base::Passed(&passed_message))); | 387 this, base::Passed(&passed_message))); |
317 return true; | 388 return true; |
318 } | 389 } |
319 } | 390 } |
(...skipping 19 matching lines...) Expand all Loading... |
339 Endpoint* endpoint = iter->second.get(); | 410 Endpoint* endpoint = iter->second.get(); |
340 ++iter; | 411 ++iter; |
341 | 412 |
342 if (endpoint->client()) | 413 if (endpoint->client()) |
343 endpoints_to_notify.push_back(endpoint); | 414 endpoints_to_notify.push_back(endpoint); |
344 | 415 |
345 MarkPeerClosedAndMaybeRemove(endpoint); | 416 MarkPeerClosedAndMaybeRemove(endpoint); |
346 } | 417 } |
347 | 418 |
348 for (auto& endpoint : endpoints_to_notify) { | 419 for (auto& endpoint : endpoints_to_notify) { |
349 // Because an notification may in turn detach any endpoint, we have to | 420 // Because a notification may in turn detach any endpoint, we have to |
350 // check each client again here. | 421 // check each client again here. |
351 if (endpoint->client()) | 422 if (endpoint->client()) |
352 NotifyEndpointOfError(endpoint.get(), false /* force_async */); | 423 NotifyEndpointOfError(endpoint.get(), false /* force_async */); |
353 } | 424 } |
354 } | 425 } |
355 | 426 |
356 void NotifyEndpointOfError(Endpoint* endpoint, bool force_async) { | 427 void NotifyEndpointOfError(Endpoint* endpoint, bool force_async) { |
357 lock_.AssertAcquired(); | 428 lock_.AssertAcquired(); |
358 DCHECK(endpoint->task_runner() && endpoint->client()); | 429 DCHECK(endpoint->task_runner() && endpoint->client()); |
359 if (endpoint->task_runner()->BelongsToCurrentThread() && !force_async) { | 430 if (endpoint->task_runner()->BelongsToCurrentThread() && !force_async) { |
(...skipping 25 matching lines...) Expand all Loading... |
385 endpoints_.erase(endpoint->id()); | 456 endpoints_.erase(endpoint->id()); |
386 } | 457 } |
387 | 458 |
388 void MarkPeerClosedAndMaybeRemove(Endpoint* endpoint) { | 459 void MarkPeerClosedAndMaybeRemove(Endpoint* endpoint) { |
389 lock_.AssertAcquired(); | 460 lock_.AssertAcquired(); |
390 endpoint->set_peer_closed(); | 461 endpoint->set_peer_closed(); |
391 if (endpoint->closed() && endpoint->peer_closed()) | 462 if (endpoint->closed() && endpoint->peer_closed()) |
392 endpoints_.erase(endpoint->id()); | 463 endpoints_.erase(endpoint->id()); |
393 } | 464 } |
394 | 465 |
395 void NotifyPeerEndpointClosed(mojo::InterfaceId id) { | |
396 if (task_runner_->BelongsToCurrentThread()) { | |
397 if (connector_.is_valid()) | |
398 control_message_proxy_.NotifyPeerEndpointClosed(id); | |
399 } else { | |
400 task_runner_->PostTask( | |
401 FROM_HERE, | |
402 base::Bind(&ChannelAssociatedGroupController | |
403 ::NotifyPeerEndpointClosed, this, id)); | |
404 } | |
405 } | |
406 | |
407 void NotifyEndpointClosedBeforeSent(mojo::InterfaceId id) { | |
408 if (task_runner_->BelongsToCurrentThread()) { | |
409 if (connector_.is_valid()) | |
410 control_message_proxy_.NotifyEndpointClosedBeforeSent(id); | |
411 } else { | |
412 task_runner_->PostTask( | |
413 FROM_HERE, | |
414 base::Bind(&ChannelAssociatedGroupController | |
415 ::NotifyEndpointClosedBeforeSent, this, id)); | |
416 } | |
417 } | |
418 | |
419 Endpoint* FindOrInsertEndpoint(mojo::InterfaceId id, bool* inserted) { | 466 Endpoint* FindOrInsertEndpoint(mojo::InterfaceId id, bool* inserted) { |
420 lock_.AssertAcquired(); | 467 lock_.AssertAcquired(); |
421 DCHECK(!inserted || !*inserted); | 468 DCHECK(!inserted || !*inserted); |
422 | 469 |
423 auto iter = endpoints_.find(id); | 470 auto iter = endpoints_.find(id); |
424 if (iter != endpoints_.end()) | 471 if (iter != endpoints_.end()) |
425 return iter->second.get(); | 472 return iter->second.get(); |
426 | 473 |
427 Endpoint* endpoint = new Endpoint(this, id); | 474 Endpoint* endpoint = new Endpoint(this, id); |
428 endpoints_.insert({ id, endpoint }); | 475 endpoints_.insert({ id, endpoint }); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
503 RaiseError(); | 550 RaiseError(); |
504 } | 551 } |
505 | 552 |
506 Endpoint* GetEndpointForDispatch(mojo::InterfaceId id) { | 553 Endpoint* GetEndpointForDispatch(mojo::InterfaceId id) { |
507 lock_.AssertAcquired(); | 554 lock_.AssertAcquired(); |
508 bool inserted = false; | 555 bool inserted = false; |
509 Endpoint* endpoint = FindOrInsertEndpoint(id, &inserted); | 556 Endpoint* endpoint = FindOrInsertEndpoint(id, &inserted); |
510 if (inserted) { | 557 if (inserted) { |
511 MarkClosedAndMaybeRemove(endpoint); | 558 MarkClosedAndMaybeRemove(endpoint); |
512 if (!mojo::IsMasterInterfaceId(id)) | 559 if (!mojo::IsMasterInterfaceId(id)) |
513 NotifyPeerEndpointClosed(id); | 560 control_message_proxy_.NotifyPeerEndpointClosed(id); |
514 return nullptr; | 561 return nullptr; |
515 } | 562 } |
516 | 563 |
517 if (endpoint->closed()) | 564 if (endpoint->closed()) |
518 return nullptr; | 565 return nullptr; |
519 | 566 |
520 return endpoint; | 567 return endpoint; |
521 } | 568 } |
522 | 569 |
523 // mojo::PipeControlMessageHandlerDelegate: | 570 // mojo::PipeControlMessageHandlerDelegate: |
524 bool OnPeerAssociatedEndpointClosed(mojo::InterfaceId id) override { | 571 bool OnPeerAssociatedEndpointClosed(mojo::InterfaceId id) override { |
525 DCHECK(thread_checker_.CalledOnValidThread()); | 572 DCHECK(thread_checker_.CalledOnValidThread()); |
526 | 573 |
527 if (mojo::IsMasterInterfaceId(id)) | 574 if (mojo::IsMasterInterfaceId(id)) |
528 return false; | 575 return false; |
529 | 576 |
| 577 scoped_refptr<ChannelAssociatedGroupController> keepalive(this); |
530 base::AutoLock locker(lock_); | 578 base::AutoLock locker(lock_); |
531 scoped_refptr<Endpoint> endpoint = FindOrInsertEndpoint(id, nullptr); | 579 scoped_refptr<Endpoint> endpoint = FindOrInsertEndpoint(id, nullptr); |
532 if (!endpoint->peer_closed()) { | 580 if (!endpoint->peer_closed()) { |
533 if (endpoint->client()) | 581 if (endpoint->client()) |
534 NotifyEndpointOfError(endpoint.get(), false /* force_async */); | 582 NotifyEndpointOfError(endpoint.get(), false /* force_async */); |
535 MarkPeerClosedAndMaybeRemove(endpoint.get()); | 583 MarkPeerClosedAndMaybeRemove(endpoint.get()); |
536 } | 584 } |
537 | 585 |
538 return true; | 586 return true; |
539 } | 587 } |
540 | 588 |
541 bool OnAssociatedEndpointClosedBeforeSent(mojo::InterfaceId id) override { | 589 bool OnAssociatedEndpointClosedBeforeSent(mojo::InterfaceId id) override { |
542 DCHECK(thread_checker_.CalledOnValidThread()); | 590 DCHECK(thread_checker_.CalledOnValidThread()); |
543 | 591 |
544 if (mojo::IsMasterInterfaceId(id)) | 592 if (mojo::IsMasterInterfaceId(id)) |
545 return false; | 593 return false; |
546 | 594 |
547 base::AutoLock locker(lock_); | 595 base::AutoLock locker(lock_); |
548 Endpoint* endpoint = FindOrInsertEndpoint(id, nullptr); | 596 Endpoint* endpoint = FindOrInsertEndpoint(id, nullptr); |
549 DCHECK(!endpoint->closed()); | 597 DCHECK(!endpoint->closed()); |
550 MarkClosedAndMaybeRemove(endpoint); | 598 MarkClosedAndMaybeRemove(endpoint); |
551 control_message_proxy_.NotifyPeerEndpointClosed(id); | 599 control_message_proxy_.NotifyPeerEndpointClosed(id); |
552 return true; | 600 return true; |
553 } | 601 } |
554 | 602 |
555 // Checked in places which must be run on the master endpoint's thread. | 603 // Checked in places which must be run on the master endpoint's thread. |
556 base::ThreadChecker thread_checker_; | 604 base::ThreadChecker thread_checker_; |
557 | 605 |
558 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 606 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 607 |
| 608 // A TaskRunner that runs tasks on the same thread as |task_runner_| but which |
| 609 // is used exclusively to do thread safety checking. This is an unfortunate |
| 610 // consequence of bad interaction between some TaskRunner implementations and |
| 611 // MessageLoop destruction which may cause the user-provided |task_runner_| to |
| 612 // incorrectly report that BelongsToCurrentThread() == false during shutdown. |
| 613 scoped_refptr<base::SingleThreadTaskRunner> thread_task_runner_; |
| 614 |
559 scoped_refptr<base::SingleThreadTaskRunner> proxy_task_runner_; | 615 scoped_refptr<base::SingleThreadTaskRunner> proxy_task_runner_; |
560 const uint32_t id_namespace_mask_; | 616 const bool set_interface_id_namespace_bit_; |
561 std::unique_ptr<mojo::AssociatedGroup> associated_group_; | 617 std::unique_ptr<mojo::Connector> connector_; |
562 mojo::Connector connector_; | |
563 mojo::MessageHeaderValidator header_validator_; | 618 mojo::MessageHeaderValidator header_validator_; |
564 mojo::PipeControlMessageHandler control_message_handler_; | 619 mojo::PipeControlMessageHandler control_message_handler_; |
| 620 ControlMessageProxyThunk control_message_proxy_thunk_; |
| 621 mojo::PipeControlMessageProxy control_message_proxy_; |
| 622 |
| 623 // Outgoing messages that were sent before this controller was bound to a |
| 624 // real message pipe. |
| 625 std::vector<std::unique_ptr<mojo::Message>> outgoing_messages_; |
565 | 626 |
566 // Guards the fields below for thread-safe access. | 627 // Guards the fields below for thread-safe access. |
567 base::Lock lock_; | 628 base::Lock lock_; |
568 | 629 |
569 bool encountered_error_ = false; | 630 bool encountered_error_ = false; |
570 uint32_t next_interface_id_ = 1; | 631 |
| 632 // ID #1 is reserved for the mojom::Channel interface. |
| 633 uint32_t next_interface_id_ = 2; |
| 634 |
571 std::map<uint32_t, scoped_refptr<Endpoint>> endpoints_; | 635 std::map<uint32_t, scoped_refptr<Endpoint>> endpoints_; |
572 mojo::PipeControlMessageProxy control_message_proxy_; | |
573 | 636 |
574 DISALLOW_COPY_AND_ASSIGN(ChannelAssociatedGroupController); | 637 DISALLOW_COPY_AND_ASSIGN(ChannelAssociatedGroupController); |
575 }; | 638 }; |
576 | 639 |
577 class BootstrapMasterProxy { | 640 class MojoBootstrapImpl : public MojoBootstrap { |
578 public: | 641 public: |
579 BootstrapMasterProxy() {} | 642 MojoBootstrapImpl( |
580 ~BootstrapMasterProxy() { | 643 mojo::ScopedMessagePipeHandle handle, |
581 endpoint_client_.reset(); | 644 Delegate* delegate, |
582 proxy_.reset(); | 645 const scoped_refptr<ChannelAssociatedGroupController> controller) |
583 if (controller_) | 646 : controller_(controller), |
584 controller_->ShutDown(); | 647 handle_(std::move(handle)), |
| 648 delegate_(delegate) { |
| 649 associated_group_ = controller_->CreateAssociatedGroup(); |
585 } | 650 } |
586 | 651 |
587 void Bind(mojo::ScopedMessagePipeHandle handle) { | 652 ~MojoBootstrapImpl() override { |
588 DCHECK(!controller_); | 653 controller_->ShutDown(); |
589 controller_ = new ChannelAssociatedGroupController(true, std::move(handle)); | |
590 endpoint_client_.reset(new mojo::InterfaceEndpointClient( | |
591 controller_->CreateLocalEndpointHandle(mojo::kMasterInterfaceId), | |
592 nullptr, | |
593 base::MakeUnique<typename mojom::Bootstrap::ResponseValidator_>(), | |
594 false, base::ThreadTaskRunnerHandle::Get())); | |
595 proxy_.reset(new mojom::BootstrapProxy(endpoint_client_.get())); | |
596 proxy_->serialization_context()->group_controller = controller_; | |
597 } | |
598 | |
599 void set_connection_error_handler(const base::Closure& handler) { | |
600 DCHECK(endpoint_client_); | |
601 endpoint_client_->set_connection_error_handler(handler); | |
602 } | |
603 | |
604 mojo::AssociatedGroup* associated_group() { | |
605 DCHECK(controller_); | |
606 return controller_->associated_group(); | |
607 } | |
608 | |
609 ChannelAssociatedGroupController* controller() { | |
610 DCHECK(controller_); | |
611 return controller_.get(); | |
612 } | |
613 | |
614 mojom::Bootstrap* operator->() { | |
615 DCHECK(proxy_); | |
616 return proxy_.get(); | |
617 } | 654 } |
618 | 655 |
619 private: | 656 private: |
620 std::unique_ptr<mojom::BootstrapProxy> proxy_; | 657 // MojoBootstrap: |
| 658 void Connect() override { |
| 659 controller_->Bind(std::move(handle_)); |
| 660 |
| 661 IPC::mojom::ChannelAssociatedPtr sender; |
| 662 IPC::mojom::ChannelAssociatedRequest receiver; |
| 663 controller_->CreateChannelEndpoints(&sender, &receiver); |
| 664 |
| 665 delegate_->OnPipesAvailable(std::move(sender), std::move(receiver)); |
| 666 } |
| 667 |
| 668 mojo::AssociatedGroup* GetAssociatedGroup() override { |
| 669 return associated_group_.get(); |
| 670 } |
| 671 |
621 scoped_refptr<ChannelAssociatedGroupController> controller_; | 672 scoped_refptr<ChannelAssociatedGroupController> controller_; |
622 std::unique_ptr<mojo::InterfaceEndpointClient> endpoint_client_; | |
623 | 673 |
624 DISALLOW_COPY_AND_ASSIGN(BootstrapMasterProxy); | 674 mojo::ScopedMessagePipeHandle handle_; |
| 675 Delegate* delegate_; |
| 676 std::unique_ptr<mojo::AssociatedGroup> associated_group_; |
| 677 |
| 678 DISALLOW_COPY_AND_ASSIGN(MojoBootstrapImpl); |
625 }; | 679 }; |
626 | 680 |
627 class BootstrapMasterBinding { | |
628 public: | |
629 explicit BootstrapMasterBinding(mojom::Bootstrap* impl) { | |
630 stub_.set_sink(impl); | |
631 } | |
632 | |
633 ~BootstrapMasterBinding() { | |
634 endpoint_client_.reset(); | |
635 if (controller_) | |
636 controller_->ShutDown(); | |
637 } | |
638 | |
639 void set_connection_error_handler(const base::Closure& handler) { | |
640 DCHECK(endpoint_client_); | |
641 endpoint_client_->set_connection_error_handler(handler); | |
642 } | |
643 | |
644 mojo::AssociatedGroup* associated_group() { | |
645 DCHECK(controller_); | |
646 return controller_->associated_group(); | |
647 } | |
648 | |
649 ChannelAssociatedGroupController* controller() { | |
650 DCHECK(controller_); | |
651 return controller_.get(); | |
652 } | |
653 | |
654 void Bind(mojo::ScopedMessagePipeHandle handle) { | |
655 DCHECK(!controller_); | |
656 controller_ = | |
657 new ChannelAssociatedGroupController(false, std::move(handle)); | |
658 stub_.serialization_context()->group_controller = controller_; | |
659 | |
660 endpoint_client_.reset(new mojo::InterfaceEndpointClient( | |
661 controller_->CreateLocalEndpointHandle(mojo::kMasterInterfaceId), | |
662 &stub_, | |
663 base::MakeUnique<typename mojom::Bootstrap::RequestValidator_>(), | |
664 false, base::ThreadTaskRunnerHandle::Get())); | |
665 } | |
666 | |
667 private: | |
668 mojom::BootstrapStub stub_; | |
669 scoped_refptr<ChannelAssociatedGroupController> controller_; | |
670 std::unique_ptr<mojo::InterfaceEndpointClient> endpoint_client_; | |
671 | |
672 DISALLOW_COPY_AND_ASSIGN(BootstrapMasterBinding); | |
673 }; | |
674 | |
675 // MojoBootstrap for the server process. You should create the instance | |
676 // using MojoBootstrap::Create(). | |
677 class MojoServerBootstrap : public MojoBootstrap { | |
678 public: | |
679 MojoServerBootstrap(); | |
680 | |
681 private: | |
682 // MojoBootstrap implementation. | |
683 void Connect() override; | |
684 | |
685 mojo::AssociatedGroup* GetAssociatedGroup() override { | |
686 return bootstrap_.associated_group(); | |
687 } | |
688 | |
689 void SetProxyTaskRunner( | |
690 scoped_refptr<base::SingleThreadTaskRunner> task_runner) override { | |
691 bootstrap_.controller()->SetProxyTaskRunner(task_runner); | |
692 } | |
693 | |
694 void OnInitDone(int32_t peer_pid); | |
695 | |
696 BootstrapMasterProxy bootstrap_; | |
697 IPC::mojom::ChannelAssociatedPtrInfo send_channel_; | |
698 IPC::mojom::ChannelAssociatedRequest receive_channel_request_; | |
699 | |
700 DISALLOW_COPY_AND_ASSIGN(MojoServerBootstrap); | |
701 }; | |
702 | |
703 MojoServerBootstrap::MojoServerBootstrap() = default; | |
704 | |
705 void MojoServerBootstrap::Connect() { | |
706 DCHECK_EQ(state(), STATE_INITIALIZED); | |
707 | |
708 bootstrap_.Bind(TakeHandle()); | |
709 bootstrap_.set_connection_error_handler( | |
710 base::Bind(&MojoServerBootstrap::Fail, base::Unretained(this))); | |
711 | |
712 IPC::mojom::ChannelAssociatedRequest send_channel_request; | |
713 IPC::mojom::ChannelAssociatedPtrInfo receive_channel; | |
714 | |
715 bootstrap_.associated_group()->CreateAssociatedInterface( | |
716 mojo::AssociatedGroup::WILL_PASS_REQUEST, &send_channel_, | |
717 &send_channel_request); | |
718 bootstrap_.associated_group()->CreateAssociatedInterface( | |
719 mojo::AssociatedGroup::WILL_PASS_PTR, &receive_channel, | |
720 &receive_channel_request_); | |
721 | |
722 bootstrap_->Init( | |
723 std::move(send_channel_request), std::move(receive_channel), | |
724 GetSelfPID(), | |
725 base::Bind(&MojoServerBootstrap::OnInitDone, base::Unretained(this))); | |
726 | |
727 set_state(STATE_WAITING_ACK); | |
728 } | |
729 | |
730 void MojoServerBootstrap::OnInitDone(int32_t peer_pid) { | |
731 if (state() != STATE_WAITING_ACK) { | |
732 set_state(STATE_ERROR); | |
733 LOG(ERROR) << "Got inconsistent message from client."; | |
734 return; | |
735 } | |
736 | |
737 set_state(STATE_READY); | |
738 bootstrap_.set_connection_error_handler(base::Closure()); | |
739 delegate()->OnPipesAvailable(std::move(send_channel_), | |
740 std::move(receive_channel_request_), peer_pid); | |
741 } | |
742 | |
743 // MojoBootstrap for client processes. You should create the instance | |
744 // using MojoBootstrap::Create(). | |
745 class MojoClientBootstrap : public MojoBootstrap, public mojom::Bootstrap { | |
746 public: | |
747 MojoClientBootstrap(); | |
748 | |
749 private: | |
750 // MojoBootstrap implementation. | |
751 void Connect() override; | |
752 | |
753 mojo::AssociatedGroup* GetAssociatedGroup() override { | |
754 return binding_.associated_group(); | |
755 } | |
756 | |
757 void SetProxyTaskRunner( | |
758 scoped_refptr<base::SingleThreadTaskRunner> task_runner) override { | |
759 binding_.controller()->SetProxyTaskRunner(task_runner); | |
760 } | |
761 | |
762 // mojom::Bootstrap implementation. | |
763 void Init(mojom::ChannelAssociatedRequest receive_channel, | |
764 mojom::ChannelAssociatedPtrInfo send_channel, | |
765 int32_t peer_pid, | |
766 const InitCallback& callback) override; | |
767 | |
768 BootstrapMasterBinding binding_; | |
769 | |
770 DISALLOW_COPY_AND_ASSIGN(MojoClientBootstrap); | |
771 }; | |
772 | |
773 MojoClientBootstrap::MojoClientBootstrap() : binding_(this) {} | |
774 | |
775 void MojoClientBootstrap::Connect() { | |
776 binding_.Bind(TakeHandle()); | |
777 binding_.set_connection_error_handler( | |
778 base::Bind(&MojoClientBootstrap::Fail, base::Unretained(this))); | |
779 } | |
780 | |
781 void MojoClientBootstrap::Init(mojom::ChannelAssociatedRequest receive_channel, | |
782 mojom::ChannelAssociatedPtrInfo send_channel, | |
783 int32_t peer_pid, | |
784 const InitCallback& callback) { | |
785 callback.Run(GetSelfPID()); | |
786 set_state(STATE_READY); | |
787 binding_.set_connection_error_handler(base::Closure()); | |
788 delegate()->OnPipesAvailable(std::move(send_channel), | |
789 std::move(receive_channel), peer_pid); | |
790 } | |
791 | |
792 } // namespace | 681 } // namespace |
793 | 682 |
794 // MojoBootstrap | |
795 | |
796 // static | 683 // static |
797 std::unique_ptr<MojoBootstrap> MojoBootstrap::Create( | 684 std::unique_ptr<MojoBootstrap> MojoBootstrap::Create( |
798 mojo::ScopedMessagePipeHandle handle, | 685 mojo::ScopedMessagePipeHandle handle, |
799 Channel::Mode mode, | 686 Channel::Mode mode, |
800 Delegate* delegate) { | 687 Delegate* delegate, |
801 CHECK(mode == Channel::MODE_CLIENT || mode == Channel::MODE_SERVER); | 688 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) { |
802 std::unique_ptr<MojoBootstrap> self = | 689 return base::MakeUnique<MojoBootstrapImpl>( |
803 mode == Channel::MODE_CLIENT | 690 std::move(handle), delegate, |
804 ? std::unique_ptr<MojoBootstrap>(new MojoClientBootstrap) | 691 new ChannelAssociatedGroupController(mode == Channel::MODE_SERVER, |
805 : std::unique_ptr<MojoBootstrap>(new MojoServerBootstrap); | 692 ipc_task_runner)); |
806 | |
807 self->Init(std::move(handle), delegate); | |
808 return self; | |
809 } | |
810 | |
811 MojoBootstrap::MojoBootstrap() : delegate_(NULL), state_(STATE_INITIALIZED) { | |
812 } | |
813 | |
814 MojoBootstrap::~MojoBootstrap() {} | |
815 | |
816 void MojoBootstrap::Init(mojo::ScopedMessagePipeHandle handle, | |
817 Delegate* delegate) { | |
818 handle_ = std::move(handle); | |
819 delegate_ = delegate; | |
820 } | |
821 | |
822 base::ProcessId MojoBootstrap::GetSelfPID() const { | |
823 #if defined(OS_LINUX) | |
824 if (int global_pid = Channel::GetGlobalPid()) | |
825 return global_pid; | |
826 #endif // OS_LINUX | |
827 #if defined(OS_NACL) | |
828 return -1; | |
829 #else | |
830 return base::GetCurrentProcId(); | |
831 #endif // defined(OS_NACL) | |
832 } | |
833 | |
834 void MojoBootstrap::Fail() { | |
835 set_state(STATE_ERROR); | |
836 delegate()->OnBootstrapError(); | |
837 } | |
838 | |
839 bool MojoBootstrap::HasFailed() const { | |
840 return state() == STATE_ERROR; | |
841 } | |
842 | |
843 mojo::ScopedMessagePipeHandle MojoBootstrap::TakeHandle() { | |
844 return std::move(handle_); | |
845 } | 693 } |
846 | 694 |
847 } // namespace IPC | 695 } // namespace IPC |
OLD | NEW |