Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(91)

Side by Side Diff: ipc/ipc_mojo_bootstrap.cc

Issue 2167973002: Revert of Support early associated interface binding on ChannelMojo (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@explicit-channel-ipc-task-runner
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ipc/ipc_mojo_bootstrap.h ('k') | ipc/ipc_mojo_bootstrap_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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>
13 12
14 #include "base/callback.h" 13 #include "base/callback.h"
15 #include "base/logging.h" 14 #include "base/logging.h"
16 #include "base/macros.h" 15 #include "base/macros.h"
17 #include "base/memory/ptr_util.h" 16 #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"
33 #include "mojo/public/cpp/bindings/message_header_validator.h" 32 #include "mojo/public/cpp/bindings/message_header_validator.h"
34 #include "mojo/public/cpp/bindings/pipe_control_message_handler.h" 33 #include "mojo/public/cpp/bindings/pipe_control_message_handler.h"
35 #include "mojo/public/cpp/bindings/pipe_control_message_handler_delegate.h" 34 #include "mojo/public/cpp/bindings/pipe_control_message_handler_delegate.h"
36 #include "mojo/public/cpp/bindings/pipe_control_message_proxy.h" 35 #include "mojo/public/cpp/bindings/pipe_control_message_proxy.h"
37 36
38 namespace IPC { 37 namespace IPC {
39 38
40 namespace { 39 namespace {
41 40
42 class ChannelAssociatedGroupController 41 class ChannelAssociatedGroupController
43 : public mojo::AssociatedGroupController, 42 : public mojo::AssociatedGroupController,
44 public mojo::MessageReceiver, 43 public mojo::MessageReceiver,
45 public mojo::PipeControlMessageHandlerDelegate { 44 public mojo::PipeControlMessageHandlerDelegate {
46 public: 45 public:
47 ChannelAssociatedGroupController( 46 ChannelAssociatedGroupController(bool set_interface_id_namespace_bit,
48 bool set_interface_id_namespace_bit, 47 mojo::ScopedMessagePipeHandle handle)
49 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner)
50 : mojo::AssociatedGroupController(base::ThreadTaskRunnerHandle::Get()), 48 : mojo::AssociatedGroupController(base::ThreadTaskRunnerHandle::Get()),
51 task_runner_(ipc_task_runner), 49 task_runner_(base::ThreadTaskRunnerHandle::Get()),
52 proxy_task_runner_(base::ThreadTaskRunnerHandle::Get()), 50 id_namespace_mask_(set_interface_id_namespace_bit ?
53 set_interface_id_namespace_bit_(set_interface_id_namespace_bit), 51 mojo::kInterfaceIdNamespaceMask : 0),
52 associated_group_(CreateAssociatedGroup()),
53 connector_(std::move(handle), mojo::Connector::SINGLE_THREADED_SEND,
54 base::ThreadTaskRunnerHandle::Get()),
54 header_validator_( 55 header_validator_(
55 "IPC::mojom::Bootstrap [master] MessageHeaderValidator", this), 56 "IPC::mojom::Bootstrap [master] MessageHeaderValidator", this),
56 control_message_handler_(this), 57 control_message_handler_(this),
57 control_message_proxy_thunk_(this), 58 control_message_proxy_(&connector_) {
58 control_message_proxy_(&control_message_proxy_thunk_) { 59 connector_.set_incoming_receiver(&header_validator_);
59 thread_checker_.DetachFromThread(); 60 connector_.set_connection_error_handler(
61 base::Bind(&ChannelAssociatedGroupController::OnPipeError,
62 base::Unretained(this)));
60 control_message_handler_.SetDescription( 63 control_message_handler_.SetDescription(
61 "IPC::mojom::Bootstrap [master] PipeControlMessageHandler"); 64 "IPC::mojom::Bootstrap [master] PipeControlMessageHandler");
62 } 65 }
63 66
64 void Bind(mojo::ScopedMessagePipeHandle handle) { 67 mojo::AssociatedGroup* associated_group() { return associated_group_.get(); }
65 DCHECK(thread_checker_.CalledOnValidThread());
66 DCHECK(task_runner_->BelongsToCurrentThread());
67 connector_.reset(new mojo::Connector(
68 std::move(handle), mojo::Connector::SINGLE_THREADED_SEND,
69 task_runner_));
70 connector_->set_incoming_receiver(&header_validator_);
71 connector_->set_connection_error_handler(
72 base::Bind(&ChannelAssociatedGroupController::OnPipeError,
73 base::Unretained(this)));
74
75 std::vector<std::unique_ptr<mojo::Message>> outgoing_messages;
76 std::swap(outgoing_messages, outgoing_messages_);
77 for (auto& message : outgoing_messages)
78 SendMessage(message.get());
79 }
80
81 void CreateChannelEndpoints(mojom::ChannelAssociatedPtr* sender,
82 mojom::ChannelAssociatedRequest* receiver) {
83 mojo::InterfaceId sender_id, receiver_id;
84 if (set_interface_id_namespace_bit_) {
85 sender_id = 1 | mojo::kInterfaceIdNamespaceMask;
86 receiver_id = 1;
87 } else {
88 sender_id = 1;
89 receiver_id = 1 | mojo::kInterfaceIdNamespaceMask;
90 }
91
92 {
93 base::AutoLock locker(lock_);
94 Endpoint* sender_endpoint = new Endpoint(this, sender_id);
95 Endpoint* receiver_endpoint = new Endpoint(this, receiver_id);
96 endpoints_.insert({ sender_id, sender_endpoint });
97 endpoints_.insert({ receiver_id, receiver_endpoint });
98 }
99
100 mojo::ScopedInterfaceEndpointHandle sender_handle =
101 CreateScopedInterfaceEndpointHandle(sender_id, true);
102 mojo::ScopedInterfaceEndpointHandle receiver_handle =
103 CreateScopedInterfaceEndpointHandle(receiver_id, true);
104
105 sender->Bind(mojom::ChannelAssociatedPtrInfo(std::move(sender_handle), 0));
106 receiver->Bind(std::move(receiver_handle));
107 }
108 68
109 void ShutDown() { 69 void ShutDown() {
110 DCHECK(thread_checker_.CalledOnValidThread()); 70 DCHECK(thread_checker_.CalledOnValidThread());
111 connector_->CloseMessagePipe(); 71 connector_.CloseMessagePipe();
112 OnPipeError(); 72 OnPipeError();
113 connector_.reset(); 73 associated_group_.reset();
74 }
75
76 void SetProxyTaskRunner(
77 scoped_refptr<base::SingleThreadTaskRunner> proxy_task_runner) {
78 proxy_task_runner_ = proxy_task_runner;
114 } 79 }
115 80
116 // mojo::AssociatedGroupController: 81 // mojo::AssociatedGroupController:
117 void CreateEndpointHandlePair( 82 void CreateEndpointHandlePair(
118 mojo::ScopedInterfaceEndpointHandle* local_endpoint, 83 mojo::ScopedInterfaceEndpointHandle* local_endpoint,
119 mojo::ScopedInterfaceEndpointHandle* remote_endpoint) override { 84 mojo::ScopedInterfaceEndpointHandle* remote_endpoint) override {
120 base::AutoLock locker(lock_); 85 base::AutoLock locker(lock_);
121 uint32_t id = 0; 86 uint32_t id = 0;
122 do { 87 do {
123 if (next_interface_id_ >= mojo::kInterfaceIdNamespaceMask) 88 if (next_interface_id_ >= mojo::kInterfaceIdNamespaceMask)
124 next_interface_id_ = 2; 89 next_interface_id_ = 1;
125 id = next_interface_id_++; 90 id = (next_interface_id_++) | id_namespace_mask_;
126 if (set_interface_id_namespace_bit_)
127 id |= mojo::kInterfaceIdNamespaceMask;
128 } while (ContainsKey(endpoints_, id)); 91 } while (ContainsKey(endpoints_, id));
129 92
130 Endpoint* endpoint = new Endpoint(this, id); 93 Endpoint* endpoint = new Endpoint(this, id);
131 if (encountered_error_) 94 if (encountered_error_)
132 endpoint->set_peer_closed(); 95 endpoint->set_peer_closed();
133 endpoints_.insert({ id, endpoint }); 96 endpoints_.insert({ id, endpoint });
134 97
135 *local_endpoint = CreateScopedInterfaceEndpointHandle(id, true); 98 *local_endpoint = CreateScopedInterfaceEndpointHandle(id, true);
136 *remote_endpoint = CreateScopedInterfaceEndpointHandle(id, false); 99 *remote_endpoint = CreateScopedInterfaceEndpointHandle(id, false);
137 } 100 }
(...skipping 13 matching lines...) Expand all
151 } 114 }
152 115
153 void CloseEndpointHandle(mojo::InterfaceId id, bool is_local) override { 116 void CloseEndpointHandle(mojo::InterfaceId id, bool is_local) override {
154 if (!mojo::IsValidInterfaceId(id)) 117 if (!mojo::IsValidInterfaceId(id))
155 return; 118 return;
156 119
157 base::AutoLock locker(lock_); 120 base::AutoLock locker(lock_);
158 if (!is_local) { 121 if (!is_local) {
159 DCHECK(ContainsKey(endpoints_, id)); 122 DCHECK(ContainsKey(endpoints_, id));
160 DCHECK(!mojo::IsMasterInterfaceId(id)); 123 DCHECK(!mojo::IsMasterInterfaceId(id));
161 control_message_proxy_.NotifyEndpointClosedBeforeSent(id); 124 NotifyEndpointClosedBeforeSent(id);
162 return; 125 return;
163 } 126 }
164 127
165 DCHECK(ContainsKey(endpoints_, id)); 128 DCHECK(ContainsKey(endpoints_, id));
166 Endpoint* endpoint = endpoints_[id].get(); 129 Endpoint* endpoint = endpoints_[id].get();
167 DCHECK(!endpoint->client()); 130 DCHECK(!endpoint->client());
168 DCHECK(!endpoint->closed()); 131 DCHECK(!endpoint->closed());
169 MarkClosedAndMaybeRemove(endpoint); 132 MarkClosedAndMaybeRemove(endpoint);
170 133
171 if (!mojo::IsMasterInterfaceId(id)) 134 if (!mojo::IsMasterInterfaceId(id))
172 control_message_proxy_.NotifyPeerEndpointClosed(id); 135 NotifyPeerEndpointClosed(id);
173 } 136 }
174 137
175 mojo::InterfaceEndpointController* AttachEndpointClient( 138 mojo::InterfaceEndpointController* AttachEndpointClient(
176 const mojo::ScopedInterfaceEndpointHandle& handle, 139 const mojo::ScopedInterfaceEndpointHandle& handle,
177 mojo::InterfaceEndpointClient* client, 140 mojo::InterfaceEndpointClient* client,
178 scoped_refptr<base::SingleThreadTaskRunner> runner) override { 141 scoped_refptr<base::SingleThreadTaskRunner> runner) override {
179 const mojo::InterfaceId id = handle.id(); 142 const mojo::InterfaceId id = handle.id();
180 143
181 DCHECK(mojo::IsValidInterfaceId(id)); 144 DCHECK(mojo::IsValidInterfaceId(id));
182 DCHECK(client); 145 DCHECK(client);
(...skipping 18 matching lines...) Expand all
201 164
202 base::AutoLock locker(lock_); 165 base::AutoLock locker(lock_);
203 DCHECK(ContainsKey(endpoints_, id)); 166 DCHECK(ContainsKey(endpoints_, id));
204 167
205 Endpoint* endpoint = endpoints_[id].get(); 168 Endpoint* endpoint = endpoints_[id].get();
206 endpoint->DetachClient(); 169 endpoint->DetachClient();
207 } 170 }
208 171
209 void RaiseError() override { 172 void RaiseError() override {
210 if (task_runner_->BelongsToCurrentThread()) { 173 if (task_runner_->BelongsToCurrentThread()) {
211 connector_->RaiseError(); 174 connector_.RaiseError();
212 } else { 175 } else {
213 task_runner_->PostTask( 176 task_runner_->PostTask(
214 FROM_HERE, 177 FROM_HERE,
215 base::Bind(&ChannelAssociatedGroupController::RaiseError, this)); 178 base::Bind(&ChannelAssociatedGroupController::RaiseError, this));
216 } 179 }
217 } 180 }
218 181
219 private: 182 private:
220 class Endpoint; 183 class Endpoint;
221 class ControlMessageProxyThunk;
222 friend class Endpoint; 184 friend class Endpoint;
223 friend class ControlMessageProxyThunk;
224 185
225 class Endpoint : public base::RefCountedThreadSafe<Endpoint>, 186 class Endpoint : public base::RefCountedThreadSafe<Endpoint>,
226 public mojo::InterfaceEndpointController { 187 public mojo::InterfaceEndpointController {
227 public: 188 public:
228 Endpoint(ChannelAssociatedGroupController* controller, mojo::InterfaceId id) 189 Endpoint(ChannelAssociatedGroupController* controller, mojo::InterfaceId id)
229 : controller_(controller), id_(id) {} 190 : controller_(controller), id_(id) {}
230 191
231 mojo::InterfaceId id() const { return id_; } 192 mojo::InterfaceId id() const { return id_; }
232 193
233 bool closed() const { 194 bool closed() const {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 const mojo::InterfaceId id_; 277 const mojo::InterfaceId id_;
317 278
318 bool closed_ = false; 279 bool closed_ = false;
319 bool peer_closed_ = false; 280 bool peer_closed_ = false;
320 mojo::InterfaceEndpointClient* client_ = nullptr; 281 mojo::InterfaceEndpointClient* client_ = nullptr;
321 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 282 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
322 283
323 DISALLOW_COPY_AND_ASSIGN(Endpoint); 284 DISALLOW_COPY_AND_ASSIGN(Endpoint);
324 }; 285 };
325 286
326 class ControlMessageProxyThunk : public MessageReceiver {
327 public:
328 explicit ControlMessageProxyThunk(
329 ChannelAssociatedGroupController* controller)
330 : controller_(controller) {}
331
332 private:
333 // MessageReceiver:
334 bool Accept(mojo::Message* message) override {
335 return controller_->SendMessage(message);
336 }
337
338 ChannelAssociatedGroupController* controller_;
339
340 DISALLOW_COPY_AND_ASSIGN(ControlMessageProxyThunk);
341 };
342
343 ~ChannelAssociatedGroupController() override { 287 ~ChannelAssociatedGroupController() override {
344 base::AutoLock locker(lock_); 288 base::AutoLock locker(lock_);
289
345 for (auto iter = endpoints_.begin(); iter != endpoints_.end();) { 290 for (auto iter = endpoints_.begin(); iter != endpoints_.end();) {
346 Endpoint* endpoint = iter->second.get(); 291 Endpoint* endpoint = iter->second.get();
347 ++iter; 292 ++iter;
348 293
349 DCHECK(endpoint->closed()); 294 DCHECK(endpoint->closed());
350 MarkPeerClosedAndMaybeRemove(endpoint); 295 MarkPeerClosedAndMaybeRemove(endpoint);
351 } 296 }
352 297
353 DCHECK(endpoints_.empty()); 298 DCHECK(endpoints_.empty());
354 } 299 }
355 300
356 bool SendMessage(mojo::Message* message) { 301 bool SendMessage(mojo::Message* message) {
357 if (task_runner_->BelongsToCurrentThread()) { 302 if (task_runner_->BelongsToCurrentThread()) {
358 DCHECK(thread_checker_.CalledOnValidThread()); 303 DCHECK(thread_checker_.CalledOnValidThread());
359 if (!connector_) { 304 return connector_.Accept(message);
360 // Pipe may not be bound yet, so we queue the message.
361 std::unique_ptr<mojo::Message> queued_message(new mojo::Message);
362 message->MoveTo(queued_message.get());
363 outgoing_messages_.emplace_back(std::move(queued_message));
364 return true;
365 }
366 return connector_->Accept(message);
367 } else { 305 } else {
368 // We always post tasks to the master endpoint thread when called from the 306 // We always post tasks to the master endpoint thread when called from the
369 // proxy thread in order to simulate IPC::ChannelProxy::Send behavior. 307 // proxy thread in order to simulate IPC::ChannelProxy::Send behavior.
370 DCHECK(proxy_task_runner_ && 308 DCHECK(proxy_task_runner_ &&
371 proxy_task_runner_->BelongsToCurrentThread()); 309 proxy_task_runner_->BelongsToCurrentThread());
372 std::unique_ptr<mojo::Message> passed_message(new mojo::Message); 310 std::unique_ptr<mojo::Message> passed_message(new mojo::Message);
373 message->MoveTo(passed_message.get()); 311 message->MoveTo(passed_message.get());
374 task_runner_->PostTask( 312 task_runner_->PostTask(
375 FROM_HERE, 313 FROM_HERE,
376 base::Bind( 314 base::Bind(
(...skipping 24 matching lines...) Expand all
401 Endpoint* endpoint = iter->second.get(); 339 Endpoint* endpoint = iter->second.get();
402 ++iter; 340 ++iter;
403 341
404 if (endpoint->client()) 342 if (endpoint->client())
405 endpoints_to_notify.push_back(endpoint); 343 endpoints_to_notify.push_back(endpoint);
406 344
407 MarkPeerClosedAndMaybeRemove(endpoint); 345 MarkPeerClosedAndMaybeRemove(endpoint);
408 } 346 }
409 347
410 for (auto& endpoint : endpoints_to_notify) { 348 for (auto& endpoint : endpoints_to_notify) {
411 // Because a notification may in turn detach any endpoint, we have to 349 // Because an notification may in turn detach any endpoint, we have to
412 // check each client again here. 350 // check each client again here.
413 if (endpoint->client()) 351 if (endpoint->client())
414 NotifyEndpointOfError(endpoint.get(), false /* force_async */); 352 NotifyEndpointOfError(endpoint.get(), false /* force_async */);
415 } 353 }
416 } 354 }
417 355
418 void NotifyEndpointOfError(Endpoint* endpoint, bool force_async) { 356 void NotifyEndpointOfError(Endpoint* endpoint, bool force_async) {
419 lock_.AssertAcquired(); 357 lock_.AssertAcquired();
420 DCHECK(endpoint->task_runner() && endpoint->client()); 358 DCHECK(endpoint->task_runner() && endpoint->client());
421 if (endpoint->task_runner()->BelongsToCurrentThread() && !force_async) { 359 if (endpoint->task_runner()->BelongsToCurrentThread() && !force_async) {
(...skipping 25 matching lines...) Expand all
447 endpoints_.erase(endpoint->id()); 385 endpoints_.erase(endpoint->id());
448 } 386 }
449 387
450 void MarkPeerClosedAndMaybeRemove(Endpoint* endpoint) { 388 void MarkPeerClosedAndMaybeRemove(Endpoint* endpoint) {
451 lock_.AssertAcquired(); 389 lock_.AssertAcquired();
452 endpoint->set_peer_closed(); 390 endpoint->set_peer_closed();
453 if (endpoint->closed() && endpoint->peer_closed()) 391 if (endpoint->closed() && endpoint->peer_closed())
454 endpoints_.erase(endpoint->id()); 392 endpoints_.erase(endpoint->id());
455 } 393 }
456 394
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
457 Endpoint* FindOrInsertEndpoint(mojo::InterfaceId id, bool* inserted) { 419 Endpoint* FindOrInsertEndpoint(mojo::InterfaceId id, bool* inserted) {
458 lock_.AssertAcquired(); 420 lock_.AssertAcquired();
459 DCHECK(!inserted || !*inserted); 421 DCHECK(!inserted || !*inserted);
460 422
461 auto iter = endpoints_.find(id); 423 auto iter = endpoints_.find(id);
462 if (iter != endpoints_.end()) 424 if (iter != endpoints_.end())
463 return iter->second.get(); 425 return iter->second.get();
464 426
465 Endpoint* endpoint = new Endpoint(this, id); 427 Endpoint* endpoint = new Endpoint(this, id);
466 endpoints_.insert({ id, endpoint }); 428 endpoints_.insert({ id, endpoint });
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 RaiseError(); 503 RaiseError();
542 } 504 }
543 505
544 Endpoint* GetEndpointForDispatch(mojo::InterfaceId id) { 506 Endpoint* GetEndpointForDispatch(mojo::InterfaceId id) {
545 lock_.AssertAcquired(); 507 lock_.AssertAcquired();
546 bool inserted = false; 508 bool inserted = false;
547 Endpoint* endpoint = FindOrInsertEndpoint(id, &inserted); 509 Endpoint* endpoint = FindOrInsertEndpoint(id, &inserted);
548 if (inserted) { 510 if (inserted) {
549 MarkClosedAndMaybeRemove(endpoint); 511 MarkClosedAndMaybeRemove(endpoint);
550 if (!mojo::IsMasterInterfaceId(id)) 512 if (!mojo::IsMasterInterfaceId(id))
551 control_message_proxy_.NotifyPeerEndpointClosed(id); 513 NotifyPeerEndpointClosed(id);
552 return nullptr; 514 return nullptr;
553 } 515 }
554 516
555 if (endpoint->closed()) 517 if (endpoint->closed())
556 return nullptr; 518 return nullptr;
557 519
558 return endpoint; 520 return endpoint;
559 } 521 }
560 522
561 // mojo::PipeControlMessageHandlerDelegate: 523 // mojo::PipeControlMessageHandlerDelegate:
562 bool OnPeerAssociatedEndpointClosed(mojo::InterfaceId id) override { 524 bool OnPeerAssociatedEndpointClosed(mojo::InterfaceId id) override {
563 DCHECK(thread_checker_.CalledOnValidThread()); 525 DCHECK(thread_checker_.CalledOnValidThread());
564 526
565 if (mojo::IsMasterInterfaceId(id)) 527 if (mojo::IsMasterInterfaceId(id))
566 return false; 528 return false;
567 529
568 scoped_refptr<ChannelAssociatedGroupController> keepalive(this);
569 base::AutoLock locker(lock_); 530 base::AutoLock locker(lock_);
570 scoped_refptr<Endpoint> endpoint = FindOrInsertEndpoint(id, nullptr); 531 scoped_refptr<Endpoint> endpoint = FindOrInsertEndpoint(id, nullptr);
571 if (!endpoint->peer_closed()) { 532 if (!endpoint->peer_closed()) {
572 if (endpoint->client()) 533 if (endpoint->client())
573 NotifyEndpointOfError(endpoint.get(), false /* force_async */); 534 NotifyEndpointOfError(endpoint.get(), false /* force_async */);
574 MarkPeerClosedAndMaybeRemove(endpoint.get()); 535 MarkPeerClosedAndMaybeRemove(endpoint.get());
575 } 536 }
576 537
577 return true; 538 return true;
578 } 539 }
(...skipping 10 matching lines...) Expand all
589 MarkClosedAndMaybeRemove(endpoint); 550 MarkClosedAndMaybeRemove(endpoint);
590 control_message_proxy_.NotifyPeerEndpointClosed(id); 551 control_message_proxy_.NotifyPeerEndpointClosed(id);
591 return true; 552 return true;
592 } 553 }
593 554
594 // Checked in places which must be run on the master endpoint's thread. 555 // Checked in places which must be run on the master endpoint's thread.
595 base::ThreadChecker thread_checker_; 556 base::ThreadChecker thread_checker_;
596 557
597 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 558 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
598 scoped_refptr<base::SingleThreadTaskRunner> proxy_task_runner_; 559 scoped_refptr<base::SingleThreadTaskRunner> proxy_task_runner_;
599 const bool set_interface_id_namespace_bit_; 560 const uint32_t id_namespace_mask_;
600 std::unique_ptr<mojo::Connector> connector_; 561 std::unique_ptr<mojo::AssociatedGroup> associated_group_;
562 mojo::Connector connector_;
601 mojo::MessageHeaderValidator header_validator_; 563 mojo::MessageHeaderValidator header_validator_;
602 mojo::PipeControlMessageHandler control_message_handler_; 564 mojo::PipeControlMessageHandler control_message_handler_;
603 ControlMessageProxyThunk control_message_proxy_thunk_;
604 mojo::PipeControlMessageProxy control_message_proxy_;
605
606 // Outgoing messages that were sent before this controller was bound to a
607 // real message pipe.
608 std::vector<std::unique_ptr<mojo::Message>> outgoing_messages_;
609 565
610 // Guards the fields below for thread-safe access. 566 // Guards the fields below for thread-safe access.
611 base::Lock lock_; 567 base::Lock lock_;
612 568
613 bool encountered_error_ = false; 569 bool encountered_error_ = false;
614 570 uint32_t next_interface_id_ = 1;
615 // ID #1 is reserved for the mojom::Channel interface.
616 uint32_t next_interface_id_ = 2;
617
618 std::map<uint32_t, scoped_refptr<Endpoint>> endpoints_; 571 std::map<uint32_t, scoped_refptr<Endpoint>> endpoints_;
572 mojo::PipeControlMessageProxy control_message_proxy_;
619 573
620 DISALLOW_COPY_AND_ASSIGN(ChannelAssociatedGroupController); 574 DISALLOW_COPY_AND_ASSIGN(ChannelAssociatedGroupController);
621 }; 575 };
622 576
623 class MojoBootstrapImpl : public MojoBootstrap { 577 class BootstrapMasterProxy {
624 public: 578 public:
625 MojoBootstrapImpl( 579 BootstrapMasterProxy() {}
626 mojo::ScopedMessagePipeHandle handle, 580 ~BootstrapMasterProxy() {
627 Delegate* delegate, 581 endpoint_client_.reset();
628 const scoped_refptr<ChannelAssociatedGroupController> controller) 582 proxy_.reset();
629 : controller_(controller), 583 if (controller_)
630 handle_(std::move(handle)), 584 controller_->ShutDown();
631 delegate_(delegate) { 585 }
632 associated_group_ = controller_->CreateAssociatedGroup(); 586
633 } 587 void Bind(mojo::ScopedMessagePipeHandle handle) {
634 588 DCHECK(!controller_);
635 ~MojoBootstrapImpl() override { 589 controller_ = new ChannelAssociatedGroupController(true, std::move(handle));
636 controller_->ShutDown(); 590 endpoint_client_.reset(new mojo::InterfaceEndpointClient(
637 } 591 controller_->CreateLocalEndpointHandle(mojo::kMasterInterfaceId),
638 592 nullptr,
639 private: 593 base::MakeUnique<typename mojom::Bootstrap::ResponseValidator_>(),
640 // MojoBootstrap: 594 false, base::ThreadTaskRunnerHandle::Get()));
641 void Connect() override { 595 proxy_.reset(new mojom::BootstrapProxy(endpoint_client_.get()));
642 controller_->Bind(std::move(handle_)); 596 proxy_->serialization_context()->group_controller = controller_;
643 597 }
644 IPC::mojom::ChannelAssociatedPtr sender; 598
645 IPC::mojom::ChannelAssociatedRequest receiver; 599 void set_connection_error_handler(const base::Closure& handler) {
646 controller_->CreateChannelEndpoints(&sender, &receiver); 600 DCHECK(endpoint_client_);
647 601 endpoint_client_->set_connection_error_handler(handler);
648 delegate_->OnPipesAvailable(std::move(sender), std::move(receiver)); 602 }
649 } 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 }
618
619 private:
620 std::unique_ptr<mojom::BootstrapProxy> proxy_;
621 scoped_refptr<ChannelAssociatedGroupController> controller_;
622 std::unique_ptr<mojo::InterfaceEndpointClient> endpoint_client_;
623
624 DISALLOW_COPY_AND_ASSIGN(BootstrapMasterProxy);
625 };
626
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;
650 684
651 mojo::AssociatedGroup* GetAssociatedGroup() override { 685 mojo::AssociatedGroup* GetAssociatedGroup() override {
652 return associated_group_.get(); 686 return bootstrap_.associated_group();
653 } 687 }
654 688
655 scoped_refptr<ChannelAssociatedGroupController> controller_; 689 void SetProxyTaskRunner(
656 690 scoped_refptr<base::SingleThreadTaskRunner> task_runner) override {
657 mojo::ScopedMessagePipeHandle handle_; 691 bootstrap_.controller()->SetProxyTaskRunner(task_runner);
658 Delegate* delegate_; 692 }
659 std::unique_ptr<mojo::AssociatedGroup> associated_group_; 693
660 694 void OnInitDone(int32_t peer_pid);
661 DISALLOW_COPY_AND_ASSIGN(MojoBootstrapImpl); 695
662 }; 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 }
663 791
664 } // namespace 792 } // namespace
665 793
794 // MojoBootstrap
795
666 // static 796 // static
667 std::unique_ptr<MojoBootstrap> MojoBootstrap::Create( 797 std::unique_ptr<MojoBootstrap> MojoBootstrap::Create(
668 mojo::ScopedMessagePipeHandle handle, 798 mojo::ScopedMessagePipeHandle handle,
669 Channel::Mode mode, 799 Channel::Mode mode,
670 Delegate* delegate, 800 Delegate* delegate) {
671 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) { 801 CHECK(mode == Channel::MODE_CLIENT || mode == Channel::MODE_SERVER);
672 return base::MakeUnique<MojoBootstrapImpl>( 802 std::unique_ptr<MojoBootstrap> self =
673 std::move(handle), delegate, 803 mode == Channel::MODE_CLIENT
674 new ChannelAssociatedGroupController(mode == Channel::MODE_SERVER, 804 ? std::unique_ptr<MojoBootstrap>(new MojoClientBootstrap)
675 ipc_task_runner)); 805 : std::unique_ptr<MojoBootstrap>(new MojoServerBootstrap);
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_);
676 } 845 }
677 846
678 } // namespace IPC 847 } // namespace IPC
OLDNEW
« no previous file with comments | « ipc/ipc_mojo_bootstrap.h ('k') | ipc/ipc_mojo_bootstrap_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698