Chromium Code Reviews| 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> |
| (...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 435 | 435 |
| 436 mojo::InterfaceEndpointClient* client = endpoint->client(); | 436 mojo::InterfaceEndpointClient* client = endpoint->client(); |
| 437 if (!client || !endpoint->task_runner()->BelongsToCurrentThread()) { | 437 if (!client || !endpoint->task_runner()->BelongsToCurrentThread()) { |
| 438 // No client has been bound yet or the client runs tasks on another | 438 // No client has been bound yet or the client runs tasks on another |
| 439 // thread. We assume the other thread must always be the one on which | 439 // thread. We assume the other thread must always be the one on which |
| 440 // |proxy_task_runner_| runs tasks, since that's the only valid scenario. | 440 // |proxy_task_runner_| runs tasks, since that's the only valid scenario. |
| 441 // | 441 // |
| 442 // If the client is not yet bound, it must be bound by the time this task | 442 // If the client is not yet bound, it must be bound by the time this task |
| 443 // runs or else it's programmer error. | 443 // runs or else it's programmer error. |
| 444 DCHECK(proxy_task_runner_); | 444 DCHECK(proxy_task_runner_); |
| 445 CHECK(false); | |
|
Ken Rockot(use gerrit already)
2016/07/13 22:56:25
Forgot to remove this on the last CL. :o
| |
| 446 std::unique_ptr<mojo::Message> passed_message(new mojo::Message); | 445 std::unique_ptr<mojo::Message> passed_message(new mojo::Message); |
| 447 message->MoveTo(passed_message.get()); | 446 message->MoveTo(passed_message.get()); |
| 448 proxy_task_runner_->PostTask( | 447 proxy_task_runner_->PostTask( |
| 449 FROM_HERE, | 448 FROM_HERE, |
| 450 base::Bind(&ChannelAssociatedGroupController::AcceptOnProxyThread, | 449 base::Bind(&ChannelAssociatedGroupController::AcceptOnProxyThread, |
| 451 this, base::Passed(&passed_message))); | 450 this, base::Passed(&passed_message))); |
| 452 return true; | 451 return true; |
| 453 } | 452 } |
| 454 | 453 |
| 455 // We do not expect to receive sync responses on the master endpoint thread. | 454 // We do not expect to receive sync responses on the master endpoint thread. |
| 456 // If it's happening, it's a bug. | 455 // If it's happening, it's a bug. |
| 457 DCHECK(!message->has_flag(mojo::Message::kFlagIsSync)); | 456 DCHECK(!message->has_flag(mojo::Message::kFlagIsSync)); |
| 458 | 457 |
| 459 bool result = false; | 458 bool result = false; |
| 460 { | 459 { |
| 461 base::AutoUnlock unlocker(lock_); | 460 base::AutoUnlock unlocker(lock_); |
| 462 result = client->HandleIncomingMessage(message); | 461 result = client->HandleIncomingMessage(message); |
| 463 } | 462 } |
| 464 | 463 |
| 465 if (!result) | 464 if (!result) |
| 466 RaiseError(); | 465 RaiseError(); |
| 467 | 466 |
| 468 return true; | 467 return true; |
| 469 } | 468 } |
| 470 | 469 |
| 471 void AcceptOnProxyThread(std::unique_ptr<mojo::Message> message) { | 470 void AcceptOnProxyThread(std::unique_ptr<mojo::Message> message) { |
| 472 DCHECK(proxy_task_runner_->BelongsToCurrentThread()); | 471 DCHECK(proxy_task_runner_->BelongsToCurrentThread()); |
| 473 | 472 |
| 474 // TODO(rockot): Implement this. | 473 mojo::InterfaceId id = message->interface_id(); |
| 475 NOTREACHED(); | 474 DCHECK(mojo::IsValidInterfaceId(id) && !mojo::IsMasterInterfaceId(id)); |
| 475 | |
| 476 base::AutoLock locker(lock_); | |
| 477 bool inserted = false; | |
| 478 Endpoint* endpoint = FindOrInsertEndpoint(id, &inserted); | |
| 479 | |
| 480 // The endpoint must have already been inserted by Accept(), and if the | |
|
yzshen1
2016/07/13 23:25:09
It seems to be possible that |inserted| is true. I
Ken Rockot(use gerrit already)
2016/07/14 00:39:09
Ah your right, I had convinced myself that #2 was
| |
| 481 // system is behaving as intended and consumers are well-behaved, it should | |
| 482 // be impossible for the endpoint to have been removed yet. | |
| 483 DCHECK(!inserted); | |
| 484 | |
| 485 mojo::InterfaceEndpointClient* client = endpoint->client(); | |
| 486 DCHECK(client); | |
|
yzshen1
2016/07/13 23:25:09
The local endpoint could already be detached, righ
Ken Rockot(use gerrit already)
2016/07/14 00:39:09
Indeed. Done.
| |
| 487 DCHECK(endpoint->task_runner()->BelongsToCurrentThread()); | |
| 488 | |
| 489 // TODO(rockot): Implement sync dispatch. For now, sync messages are | |
| 490 // unsupported here. | |
| 491 DCHECK(!message->has_flag(mojo::Message::kFlagIsSync)); | |
| 492 | |
| 493 bool result = false; | |
| 494 { | |
| 495 base::AutoUnlock unlocker(lock_); | |
| 496 result = client->HandleIncomingMessage(message.get()); | |
| 497 } | |
| 498 | |
| 499 if (!result) | |
| 500 RaiseError(); | |
| 476 } | 501 } |
| 477 | 502 |
| 478 // mojo::PipeControlMessageHandlerDelegate: | 503 // mojo::PipeControlMessageHandlerDelegate: |
| 479 bool OnPeerAssociatedEndpointClosed(mojo::InterfaceId id) override { | 504 bool OnPeerAssociatedEndpointClosed(mojo::InterfaceId id) override { |
| 480 DCHECK(thread_checker_.CalledOnValidThread()); | 505 DCHECK(thread_checker_.CalledOnValidThread()); |
| 481 | 506 |
| 482 if (mojo::IsMasterInterfaceId(id)) | 507 if (mojo::IsMasterInterfaceId(id)) |
| 483 return false; | 508 return false; |
| 484 | 509 |
| 485 base::AutoLock locker(lock_); | 510 base::AutoLock locker(lock_); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 554 void set_connection_error_handler(const base::Closure& handler) { | 579 void set_connection_error_handler(const base::Closure& handler) { |
| 555 DCHECK(endpoint_client_); | 580 DCHECK(endpoint_client_); |
| 556 endpoint_client_->set_connection_error_handler(handler); | 581 endpoint_client_->set_connection_error_handler(handler); |
| 557 } | 582 } |
| 558 | 583 |
| 559 mojo::AssociatedGroup* associated_group() { | 584 mojo::AssociatedGroup* associated_group() { |
| 560 DCHECK(controller_); | 585 DCHECK(controller_); |
| 561 return controller_->associated_group(); | 586 return controller_->associated_group(); |
| 562 } | 587 } |
| 563 | 588 |
| 589 ChannelAssociatedGroupController* controller() { | |
| 590 DCHECK(controller_); | |
| 591 return controller_.get(); | |
| 592 } | |
| 593 | |
| 564 mojom::Bootstrap* operator->() { | 594 mojom::Bootstrap* operator->() { |
| 565 DCHECK(proxy_); | 595 DCHECK(proxy_); |
| 566 return proxy_.get(); | 596 return proxy_.get(); |
| 567 } | 597 } |
| 568 | 598 |
| 569 private: | 599 private: |
| 570 std::unique_ptr<mojom::BootstrapProxy> proxy_; | 600 std::unique_ptr<mojom::BootstrapProxy> proxy_; |
| 571 scoped_refptr<ChannelAssociatedGroupController> controller_; | 601 scoped_refptr<ChannelAssociatedGroupController> controller_; |
| 572 std::unique_ptr<mojo::InterfaceEndpointClient> endpoint_client_; | 602 std::unique_ptr<mojo::InterfaceEndpointClient> endpoint_client_; |
| 573 | 603 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 589 void set_connection_error_handler(const base::Closure& handler) { | 619 void set_connection_error_handler(const base::Closure& handler) { |
| 590 DCHECK(endpoint_client_); | 620 DCHECK(endpoint_client_); |
| 591 endpoint_client_->set_connection_error_handler(handler); | 621 endpoint_client_->set_connection_error_handler(handler); |
| 592 } | 622 } |
| 593 | 623 |
| 594 mojo::AssociatedGroup* associated_group() { | 624 mojo::AssociatedGroup* associated_group() { |
| 595 DCHECK(controller_); | 625 DCHECK(controller_); |
| 596 return controller_->associated_group(); | 626 return controller_->associated_group(); |
| 597 } | 627 } |
| 598 | 628 |
| 629 ChannelAssociatedGroupController* controller() { | |
| 630 DCHECK(controller_); | |
| 631 return controller_.get(); | |
| 632 } | |
| 633 | |
| 599 void Bind(mojo::ScopedMessagePipeHandle handle) { | 634 void Bind(mojo::ScopedMessagePipeHandle handle) { |
| 600 DCHECK(!controller_); | 635 DCHECK(!controller_); |
| 601 controller_ = | 636 controller_ = |
| 602 new ChannelAssociatedGroupController(false, std::move(handle)); | 637 new ChannelAssociatedGroupController(false, std::move(handle)); |
| 603 stub_.serialization_context()->group_controller = controller_; | 638 stub_.serialization_context()->group_controller = controller_; |
| 604 | 639 |
| 605 endpoint_client_.reset(new mojo::InterfaceEndpointClient( | 640 endpoint_client_.reset(new mojo::InterfaceEndpointClient( |
| 606 controller_->CreateLocalEndpointHandle(mojo::kMasterInterfaceId), | 641 controller_->CreateLocalEndpointHandle(mojo::kMasterInterfaceId), |
| 607 &stub_, | 642 &stub_, |
| 608 base::MakeUnique<typename mojom::Bootstrap::RequestValidator_>(), | 643 base::MakeUnique<typename mojom::Bootstrap::RequestValidator_>(), |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 619 | 654 |
| 620 // MojoBootstrap for the server process. You should create the instance | 655 // MojoBootstrap for the server process. You should create the instance |
| 621 // using MojoBootstrap::Create(). | 656 // using MojoBootstrap::Create(). |
| 622 class MojoServerBootstrap : public MojoBootstrap { | 657 class MojoServerBootstrap : public MojoBootstrap { |
| 623 public: | 658 public: |
| 624 MojoServerBootstrap(); | 659 MojoServerBootstrap(); |
| 625 | 660 |
| 626 private: | 661 private: |
| 627 // MojoBootstrap implementation. | 662 // MojoBootstrap implementation. |
| 628 void Connect() override; | 663 void Connect() override; |
| 664 | |
| 629 mojo::AssociatedGroup* GetAssociatedGroup() override { | 665 mojo::AssociatedGroup* GetAssociatedGroup() override { |
| 630 return bootstrap_.associated_group(); | 666 return bootstrap_.associated_group(); |
| 631 } | 667 } |
| 632 | 668 |
| 669 void SetProxyTaskRunner( | |
| 670 scoped_refptr<base::SingleThreadTaskRunner> task_runner) override { | |
| 671 bootstrap_.controller()->SetProxyTaskRunner(task_runner); | |
| 672 } | |
| 673 | |
| 633 void OnInitDone(int32_t peer_pid); | 674 void OnInitDone(int32_t peer_pid); |
| 634 | 675 |
| 635 BootstrapMasterProxy bootstrap_; | 676 BootstrapMasterProxy bootstrap_; |
| 636 IPC::mojom::ChannelAssociatedPtrInfo send_channel_; | 677 IPC::mojom::ChannelAssociatedPtrInfo send_channel_; |
| 637 IPC::mojom::ChannelAssociatedRequest receive_channel_request_; | 678 IPC::mojom::ChannelAssociatedRequest receive_channel_request_; |
| 638 | 679 |
| 639 DISALLOW_COPY_AND_ASSIGN(MojoServerBootstrap); | 680 DISALLOW_COPY_AND_ASSIGN(MojoServerBootstrap); |
| 640 }; | 681 }; |
| 641 | 682 |
| 642 MojoServerBootstrap::MojoServerBootstrap() = default; | 683 MojoServerBootstrap::MojoServerBootstrap() = default; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 681 | 722 |
| 682 // MojoBootstrap for client processes. You should create the instance | 723 // MojoBootstrap for client processes. You should create the instance |
| 683 // using MojoBootstrap::Create(). | 724 // using MojoBootstrap::Create(). |
| 684 class MojoClientBootstrap : public MojoBootstrap, public mojom::Bootstrap { | 725 class MojoClientBootstrap : public MojoBootstrap, public mojom::Bootstrap { |
| 685 public: | 726 public: |
| 686 MojoClientBootstrap(); | 727 MojoClientBootstrap(); |
| 687 | 728 |
| 688 private: | 729 private: |
| 689 // MojoBootstrap implementation. | 730 // MojoBootstrap implementation. |
| 690 void Connect() override; | 731 void Connect() override; |
| 732 | |
| 691 mojo::AssociatedGroup* GetAssociatedGroup() override { | 733 mojo::AssociatedGroup* GetAssociatedGroup() override { |
| 692 return binding_.associated_group(); | 734 return binding_.associated_group(); |
| 693 } | 735 } |
| 694 | 736 |
| 737 void SetProxyTaskRunner( | |
| 738 scoped_refptr<base::SingleThreadTaskRunner> task_runner) override { | |
| 739 binding_.controller()->SetProxyTaskRunner(task_runner); | |
| 740 } | |
| 741 | |
| 695 // mojom::Bootstrap implementation. | 742 // mojom::Bootstrap implementation. |
| 696 void Init(mojom::ChannelAssociatedRequest receive_channel, | 743 void Init(mojom::ChannelAssociatedRequest receive_channel, |
| 697 mojom::ChannelAssociatedPtrInfo send_channel, | 744 mojom::ChannelAssociatedPtrInfo send_channel, |
| 698 int32_t peer_pid, | 745 int32_t peer_pid, |
| 699 const InitCallback& callback) override; | 746 const InitCallback& callback) override; |
| 700 | 747 |
| 701 BootstrapMasterBinding binding_; | 748 BootstrapMasterBinding binding_; |
| 702 | 749 |
| 703 DISALLOW_COPY_AND_ASSIGN(MojoClientBootstrap); | 750 DISALLOW_COPY_AND_ASSIGN(MojoClientBootstrap); |
| 704 }; | 751 }; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 771 | 818 |
| 772 bool MojoBootstrap::HasFailed() const { | 819 bool MojoBootstrap::HasFailed() const { |
| 773 return state() == STATE_ERROR; | 820 return state() == STATE_ERROR; |
| 774 } | 821 } |
| 775 | 822 |
| 776 mojo::ScopedMessagePipeHandle MojoBootstrap::TakeHandle() { | 823 mojo::ScopedMessagePipeHandle MojoBootstrap::TakeHandle() { |
| 777 return std::move(handle_); | 824 return std::move(handle_); |
| 778 } | 825 } |
| 779 | 826 |
| 780 } // namespace IPC | 827 } // namespace IPC |
| OLD | NEW |