| 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/mojo/ipc_mojo_bootstrap.h" | 5 #include "ipc/mojo/ipc_mojo_bootstrap.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/process/process_handle.h" | 10 #include "base/process/process_handle.h" |
| 11 #include "ipc/ipc_message_utils.h" | 11 #include "ipc/ipc_message_utils.h" |
| 12 #include "ipc/ipc_platform_file.h" | 12 #include "ipc/ipc_platform_file.h" |
| 13 |
| 14 #if defined(USE_CHROME_EDK) |
| 15 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 16 #else |
| 13 #include "third_party/mojo/src/mojo/edk/embedder/platform_channel_pair.h" | 17 #include "third_party/mojo/src/mojo/edk/embedder/platform_channel_pair.h" |
| 18 #endif |
| 14 | 19 |
| 15 namespace IPC { | 20 namespace IPC { |
| 16 | 21 |
| 17 namespace { | 22 namespace { |
| 18 | 23 |
| 19 // MojoBootstrap for the server process. You should create the instance | 24 // MojoBootstrap for the server process. You should create the instance |
| 20 // using MojoBootstrap::Create(). | 25 // using MojoBootstrap::Create(). |
| 21 class MojoServerBootstrap : public MojoBootstrap { | 26 class MojoServerBootstrap : public MojoBootstrap { |
| 22 public: | 27 public: |
| 23 MojoServerBootstrap(); | 28 MojoServerBootstrap(); |
| 24 | 29 |
| 25 private: | 30 private: |
| 26 void SendClientPipe(int32_t peer_pid); | 31 void SendClientPipe(int32_t peer_pid); |
| 27 | 32 |
| 28 // Listener implementations | 33 // Listener implementations |
| 29 bool OnMessageReceived(const Message& message) override; | 34 bool OnMessageReceived(const Message& message) override; |
| 30 void OnChannelConnected(int32_t peer_pid) override; | 35 void OnChannelConnected(int32_t peer_pid) override; |
| 31 | 36 |
| 32 mojo::embedder::ScopedPlatformHandle server_pipe_; | 37 mojo::embedder::ScopedPlatformHandle server_pipe_; |
| 33 bool connected_; | 38 bool connected_; |
| 39 int32_t peer_pid_; |
| 34 | 40 |
| 35 DISALLOW_COPY_AND_ASSIGN(MojoServerBootstrap); | 41 DISALLOW_COPY_AND_ASSIGN(MojoServerBootstrap); |
| 36 }; | 42 }; |
| 37 | 43 |
| 38 MojoServerBootstrap::MojoServerBootstrap() : connected_(false) { | 44 MojoServerBootstrap::MojoServerBootstrap() : connected_(false), peer_pid_(0) { |
| 39 } | 45 } |
| 40 | 46 |
| 41 void MojoServerBootstrap::SendClientPipe(int32_t peer_pid) { | 47 void MojoServerBootstrap::SendClientPipe(int32_t peer_pid) { |
| 42 DCHECK_EQ(state(), STATE_INITIALIZED); | 48 DCHECK_EQ(state(), STATE_INITIALIZED); |
| 43 DCHECK(connected_); | 49 DCHECK(connected_); |
| 44 | 50 |
| 45 mojo::embedder::PlatformChannelPair channel_pair; | 51 mojo::embedder::PlatformChannelPair channel_pair; |
| 46 server_pipe_ = channel_pair.PassServerHandle(); | 52 server_pipe_ = channel_pair.PassServerHandle(); |
| 47 | 53 |
| 48 base::Process peer_process = | 54 base::Process peer_process = |
| (...skipping 22 matching lines...) Expand all Loading... |
| 71 scoped_ptr<Message> message(new Message()); | 77 scoped_ptr<Message> message(new Message()); |
| 72 ParamTraits<PlatformFileForTransit>::Write(message.get(), client_pipe); | 78 ParamTraits<PlatformFileForTransit>::Write(message.get(), client_pipe); |
| 73 Send(message.release()); | 79 Send(message.release()); |
| 74 | 80 |
| 75 set_state(STATE_WAITING_ACK); | 81 set_state(STATE_WAITING_ACK); |
| 76 } | 82 } |
| 77 | 83 |
| 78 void MojoServerBootstrap::OnChannelConnected(int32_t peer_pid) { | 84 void MojoServerBootstrap::OnChannelConnected(int32_t peer_pid) { |
| 79 DCHECK_EQ(state(), STATE_INITIALIZED); | 85 DCHECK_EQ(state(), STATE_INITIALIZED); |
| 80 connected_ = true; | 86 connected_ = true; |
| 87 peer_pid_ = peer_pid; |
| 81 SendClientPipe(peer_pid); | 88 SendClientPipe(peer_pid); |
| 82 } | 89 } |
| 83 | 90 |
| 84 bool MojoServerBootstrap::OnMessageReceived(const Message&) { | 91 bool MojoServerBootstrap::OnMessageReceived(const Message&) { |
| 85 if (state() != STATE_WAITING_ACK) { | 92 if (state() != STATE_WAITING_ACK) { |
| 86 set_state(STATE_ERROR); | 93 set_state(STATE_ERROR); |
| 87 LOG(ERROR) << "Got inconsistent message from client."; | 94 LOG(ERROR) << "Got inconsistent message from client."; |
| 88 return false; | 95 return false; |
| 89 } | 96 } |
| 90 | 97 |
| 91 set_state(STATE_READY); | 98 set_state(STATE_READY); |
| 92 CHECK(server_pipe_.is_valid()); | 99 CHECK(server_pipe_.is_valid()); |
| 93 delegate()->OnPipeAvailable( | 100 delegate()->OnPipeAvailable( |
| 94 mojo::embedder::ScopedPlatformHandle(server_pipe_.release())); | 101 mojo::embedder::ScopedPlatformHandle(server_pipe_.release()), peer_pid_); |
| 95 | 102 |
| 96 return true; | 103 return true; |
| 97 } | 104 } |
| 98 | 105 |
| 99 // MojoBootstrap for client processes. You should create the instance | 106 // MojoBootstrap for client processes. You should create the instance |
| 100 // using MojoBootstrap::Create(). | 107 // using MojoBootstrap::Create(). |
| 101 class MojoClientBootstrap : public MojoBootstrap { | 108 class MojoClientBootstrap : public MojoBootstrap { |
| 102 public: | 109 public: |
| 103 MojoClientBootstrap(); | 110 MojoClientBootstrap(); |
| 104 | 111 |
| 105 private: | 112 private: |
| 106 // Listener implementations | 113 // Listener implementations |
| 107 bool OnMessageReceived(const Message& message) override; | 114 bool OnMessageReceived(const Message& message) override; |
| 108 void OnChannelConnected(int32_t peer_pid) override; | 115 void OnChannelConnected(int32_t peer_pid) override; |
| 109 | 116 |
| 117 int32 peer_pid_; |
| 118 |
| 110 DISALLOW_COPY_AND_ASSIGN(MojoClientBootstrap); | 119 DISALLOW_COPY_AND_ASSIGN(MojoClientBootstrap); |
| 111 }; | 120 }; |
| 112 | 121 |
| 113 MojoClientBootstrap::MojoClientBootstrap() { | 122 MojoClientBootstrap::MojoClientBootstrap() : peer_pid_(0) { |
| 114 } | 123 } |
| 115 | 124 |
| 116 bool MojoClientBootstrap::OnMessageReceived(const Message& message) { | 125 bool MojoClientBootstrap::OnMessageReceived(const Message& message) { |
| 117 if (state() != STATE_INITIALIZED) { | 126 if (state() != STATE_INITIALIZED) { |
| 118 set_state(STATE_ERROR); | 127 set_state(STATE_ERROR); |
| 119 LOG(ERROR) << "Got inconsistent message from server."; | 128 LOG(ERROR) << "Got inconsistent message from server."; |
| 120 return false; | 129 return false; |
| 121 } | 130 } |
| 122 | 131 |
| 123 PlatformFileForTransit pipe; | 132 PlatformFileForTransit pipe; |
| 124 base::PickleIterator iter(message); | 133 base::PickleIterator iter(message); |
| 125 if (!ParamTraits<PlatformFileForTransit>::Read(&message, &iter, &pipe)) { | 134 if (!ParamTraits<PlatformFileForTransit>::Read(&message, &iter, &pipe)) { |
| 126 LOG(WARNING) << "Failed to read a file handle from bootstrap channel."; | 135 LOG(WARNING) << "Failed to read a file handle from bootstrap channel."; |
| 127 message.set_dispatch_error(); | 136 message.set_dispatch_error(); |
| 128 return false; | 137 return false; |
| 129 } | 138 } |
| 130 | 139 |
| 131 // Sends ACK back. | 140 // Sends ACK back. |
| 132 Send(new Message()); | 141 Send(new Message()); |
| 133 set_state(STATE_READY); | 142 set_state(STATE_READY); |
| 134 delegate()->OnPipeAvailable( | 143 delegate()->OnPipeAvailable( |
| 135 mojo::embedder::ScopedPlatformHandle(mojo::embedder::PlatformHandle( | 144 mojo::embedder::ScopedPlatformHandle(mojo::embedder::PlatformHandle( |
| 136 PlatformFileForTransitToPlatformFile(pipe)))); | 145 PlatformFileForTransitToPlatformFile(pipe))), peer_pid_); |
| 137 | 146 |
| 138 return true; | 147 return true; |
| 139 } | 148 } |
| 140 | 149 |
| 141 void MojoClientBootstrap::OnChannelConnected(int32_t peer_pid) { | 150 void MojoClientBootstrap::OnChannelConnected(int32_t peer_pid) { |
| 151 peer_pid_ = peer_pid; |
| 142 } | 152 } |
| 143 | 153 |
| 144 } // namespace | 154 } // namespace |
| 145 | 155 |
| 146 // MojoBootstrap | 156 // MojoBootstrap |
| 147 | 157 |
| 148 // static | 158 // static |
| 149 scoped_ptr<MojoBootstrap> MojoBootstrap::Create(ChannelHandle handle, | 159 scoped_ptr<MojoBootstrap> MojoBootstrap::Create(ChannelHandle handle, |
| 150 Channel::Mode mode, | 160 Channel::Mode mode, |
| 151 Delegate* delegate, | 161 Delegate* delegate, |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 int MojoBootstrap::GetClientFileDescriptor() const { | 219 int MojoBootstrap::GetClientFileDescriptor() const { |
| 210 return channel_->GetClientFileDescriptor(); | 220 return channel_->GetClientFileDescriptor(); |
| 211 } | 221 } |
| 212 | 222 |
| 213 base::ScopedFD MojoBootstrap::TakeClientFileDescriptor() { | 223 base::ScopedFD MojoBootstrap::TakeClientFileDescriptor() { |
| 214 return channel_->TakeClientFileDescriptor(); | 224 return channel_->TakeClientFileDescriptor(); |
| 215 } | 225 } |
| 216 #endif // defined(OS_POSIX) && !defined(OS_NACL) | 226 #endif // defined(OS_POSIX) && !defined(OS_NACL) |
| 217 | 227 |
| 218 } // namespace IPC | 228 } // namespace IPC |
| OLD | NEW |