| OLD | NEW |
| (Empty) |
| 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 | |
| 5 #include "ipc/mojo/ipc_mojo_bootstrap.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/callback.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/memory/ptr_util.h" | |
| 14 #include "base/process/process_handle.h" | |
| 15 #include "build/build_config.h" | |
| 16 #include "ipc/ipc_message_utils.h" | |
| 17 #include "ipc/ipc_platform_file.h" | |
| 18 #include "mojo/public/cpp/bindings/binding.h" | |
| 19 | |
| 20 namespace IPC { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 // MojoBootstrap for the server process. You should create the instance | |
| 25 // using MojoBootstrap::Create(). | |
| 26 class MojoServerBootstrap : public MojoBootstrap { | |
| 27 public: | |
| 28 MojoServerBootstrap(); | |
| 29 | |
| 30 private: | |
| 31 // MojoBootstrap implementation. | |
| 32 void Connect() override; | |
| 33 | |
| 34 void OnInitDone(int32_t peer_pid); | |
| 35 | |
| 36 mojom::BootstrapPtr bootstrap_; | |
| 37 IPC::mojom::ChannelAssociatedPtrInfo send_channel_; | |
| 38 IPC::mojom::ChannelAssociatedRequest receive_channel_request_; | |
| 39 | |
| 40 DISALLOW_COPY_AND_ASSIGN(MojoServerBootstrap); | |
| 41 }; | |
| 42 | |
| 43 MojoServerBootstrap::MojoServerBootstrap() = default; | |
| 44 | |
| 45 void MojoServerBootstrap::Connect() { | |
| 46 DCHECK_EQ(state(), STATE_INITIALIZED); | |
| 47 | |
| 48 bootstrap_.Bind(mojom::BootstrapPtrInfo(TakeHandle(), 0)); | |
| 49 bootstrap_.set_connection_error_handler( | |
| 50 base::Bind(&MojoServerBootstrap::Fail, base::Unretained(this))); | |
| 51 | |
| 52 IPC::mojom::ChannelAssociatedRequest send_channel_request; | |
| 53 IPC::mojom::ChannelAssociatedPtrInfo receive_channel; | |
| 54 | |
| 55 bootstrap_.associated_group()->CreateAssociatedInterface( | |
| 56 mojo::AssociatedGroup::WILL_PASS_REQUEST, &send_channel_, | |
| 57 &send_channel_request); | |
| 58 bootstrap_.associated_group()->CreateAssociatedInterface( | |
| 59 mojo::AssociatedGroup::WILL_PASS_PTR, &receive_channel, | |
| 60 &receive_channel_request_); | |
| 61 | |
| 62 bootstrap_->Init( | |
| 63 std::move(send_channel_request), std::move(receive_channel), | |
| 64 GetSelfPID(), | |
| 65 base::Bind(&MojoServerBootstrap::OnInitDone, base::Unretained(this))); | |
| 66 | |
| 67 set_state(STATE_WAITING_ACK); | |
| 68 } | |
| 69 | |
| 70 void MojoServerBootstrap::OnInitDone(int32_t peer_pid) { | |
| 71 if (state() != STATE_WAITING_ACK) { | |
| 72 set_state(STATE_ERROR); | |
| 73 LOG(ERROR) << "Got inconsistent message from client."; | |
| 74 return; | |
| 75 } | |
| 76 | |
| 77 set_state(STATE_READY); | |
| 78 bootstrap_.set_connection_error_handler(base::Closure()); | |
| 79 delegate()->OnPipesAvailable(std::move(send_channel_), | |
| 80 std::move(receive_channel_request_), peer_pid); | |
| 81 } | |
| 82 | |
| 83 // MojoBootstrap for client processes. You should create the instance | |
| 84 // using MojoBootstrap::Create(). | |
| 85 class MojoClientBootstrap : public MojoBootstrap, public mojom::Bootstrap { | |
| 86 public: | |
| 87 MojoClientBootstrap(); | |
| 88 | |
| 89 private: | |
| 90 // MojoBootstrap implementation. | |
| 91 void Connect() override; | |
| 92 | |
| 93 // mojom::Bootstrap implementation. | |
| 94 void Init(mojom::ChannelAssociatedRequest receive_channel, | |
| 95 mojom::ChannelAssociatedPtrInfo send_channel, | |
| 96 int32_t peer_pid, | |
| 97 const InitCallback& callback) override; | |
| 98 | |
| 99 mojo::Binding<mojom::Bootstrap> binding_; | |
| 100 | |
| 101 DISALLOW_COPY_AND_ASSIGN(MojoClientBootstrap); | |
| 102 }; | |
| 103 | |
| 104 MojoClientBootstrap::MojoClientBootstrap() : binding_(this) {} | |
| 105 | |
| 106 void MojoClientBootstrap::Connect() { | |
| 107 binding_.Bind(TakeHandle()); | |
| 108 binding_.set_connection_error_handler( | |
| 109 base::Bind(&MojoClientBootstrap::Fail, base::Unretained(this))); | |
| 110 } | |
| 111 | |
| 112 void MojoClientBootstrap::Init(mojom::ChannelAssociatedRequest receive_channel, | |
| 113 mojom::ChannelAssociatedPtrInfo send_channel, | |
| 114 int32_t peer_pid, | |
| 115 const InitCallback& callback) { | |
| 116 callback.Run(GetSelfPID()); | |
| 117 set_state(STATE_READY); | |
| 118 binding_.set_connection_error_handler(base::Closure()); | |
| 119 delegate()->OnPipesAvailable(std::move(send_channel), | |
| 120 std::move(receive_channel), peer_pid); | |
| 121 } | |
| 122 | |
| 123 } // namespace | |
| 124 | |
| 125 // MojoBootstrap | |
| 126 | |
| 127 // static | |
| 128 std::unique_ptr<MojoBootstrap> MojoBootstrap::Create( | |
| 129 mojo::ScopedMessagePipeHandle handle, | |
| 130 Channel::Mode mode, | |
| 131 Delegate* delegate) { | |
| 132 CHECK(mode == Channel::MODE_CLIENT || mode == Channel::MODE_SERVER); | |
| 133 std::unique_ptr<MojoBootstrap> self = | |
| 134 mode == Channel::MODE_CLIENT | |
| 135 ? std::unique_ptr<MojoBootstrap>(new MojoClientBootstrap) | |
| 136 : std::unique_ptr<MojoBootstrap>(new MojoServerBootstrap); | |
| 137 | |
| 138 self->Init(std::move(handle), delegate); | |
| 139 return self; | |
| 140 } | |
| 141 | |
| 142 MojoBootstrap::MojoBootstrap() : delegate_(NULL), state_(STATE_INITIALIZED) { | |
| 143 } | |
| 144 | |
| 145 MojoBootstrap::~MojoBootstrap() {} | |
| 146 | |
| 147 void MojoBootstrap::Init(mojo::ScopedMessagePipeHandle handle, | |
| 148 Delegate* delegate) { | |
| 149 handle_ = std::move(handle); | |
| 150 delegate_ = delegate; | |
| 151 } | |
| 152 | |
| 153 base::ProcessId MojoBootstrap::GetSelfPID() const { | |
| 154 #if defined(OS_LINUX) | |
| 155 if (int global_pid = Channel::GetGlobalPid()) | |
| 156 return global_pid; | |
| 157 #endif // OS_LINUX | |
| 158 #if defined(OS_NACL) | |
| 159 return -1; | |
| 160 #else | |
| 161 return base::GetCurrentProcId(); | |
| 162 #endif // defined(OS_NACL) | |
| 163 } | |
| 164 | |
| 165 void MojoBootstrap::Fail() { | |
| 166 set_state(STATE_ERROR); | |
| 167 delegate()->OnBootstrapError(); | |
| 168 } | |
| 169 | |
| 170 bool MojoBootstrap::HasFailed() const { | |
| 171 return state() == STATE_ERROR; | |
| 172 } | |
| 173 | |
| 174 mojo::ScopedMessagePipeHandle MojoBootstrap::TakeHandle() { | |
| 175 return std::move(handle_); | |
| 176 } | |
| 177 | |
| 178 } // namespace IPC | |
| OLD | NEW |