| 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 |
| 37 #if defined(USE_CHROME_EDK) |
| 38 mojo::edk::ScopedPlatformHandle server_pipe_; |
| 39 #else |
| 32 mojo::embedder::ScopedPlatformHandle server_pipe_; | 40 mojo::embedder::ScopedPlatformHandle server_pipe_; |
| 41 #endif |
| 33 bool connected_; | 42 bool connected_; |
| 43 int32_t peer_pid_; |
| 34 | 44 |
| 35 DISALLOW_COPY_AND_ASSIGN(MojoServerBootstrap); | 45 DISALLOW_COPY_AND_ASSIGN(MojoServerBootstrap); |
| 36 }; | 46 }; |
| 37 | 47 |
| 38 MojoServerBootstrap::MojoServerBootstrap() : connected_(false) { | 48 MojoServerBootstrap::MojoServerBootstrap() : connected_(false), peer_pid_(0) { |
| 39 } | 49 } |
| 40 | 50 |
| 41 void MojoServerBootstrap::SendClientPipe(int32_t peer_pid) { | 51 void MojoServerBootstrap::SendClientPipe(int32_t peer_pid) { |
| 42 DCHECK_EQ(state(), STATE_INITIALIZED); | 52 DCHECK_EQ(state(), STATE_INITIALIZED); |
| 43 DCHECK(connected_); | 53 DCHECK(connected_); |
| 44 | 54 |
| 55 #if defined(USE_CHROME_EDK) |
| 56 mojo::edk::PlatformChannelPair channel_pair; |
| 57 #else |
| 45 mojo::embedder::PlatformChannelPair channel_pair; | 58 mojo::embedder::PlatformChannelPair channel_pair; |
| 59 #endif |
| 46 server_pipe_ = channel_pair.PassServerHandle(); | 60 server_pipe_ = channel_pair.PassServerHandle(); |
| 47 | 61 |
| 48 base::Process peer_process = | 62 base::Process peer_process = |
| 49 #if defined(OS_WIN) | 63 #if defined(OS_WIN) |
| 50 base::Process::OpenWithAccess(peer_pid, PROCESS_DUP_HANDLE); | 64 base::Process::OpenWithAccess(peer_pid, PROCESS_DUP_HANDLE); |
| 51 #else | 65 #else |
| 52 base::Process::Open(peer_pid); | 66 base::Process::Open(peer_pid); |
| 53 #endif | 67 #endif |
| 54 PlatformFileForTransit client_pipe = GetFileHandleForProcess( | 68 PlatformFileForTransit client_pipe = GetFileHandleForProcess( |
| 55 #if defined(OS_POSIX) | 69 #if defined(OS_POSIX) |
| (...skipping 15 matching lines...) Expand all Loading... |
| 71 scoped_ptr<Message> message(new Message()); | 85 scoped_ptr<Message> message(new Message()); |
| 72 ParamTraits<PlatformFileForTransit>::Write(message.get(), client_pipe); | 86 ParamTraits<PlatformFileForTransit>::Write(message.get(), client_pipe); |
| 73 Send(message.release()); | 87 Send(message.release()); |
| 74 | 88 |
| 75 set_state(STATE_WAITING_ACK); | 89 set_state(STATE_WAITING_ACK); |
| 76 } | 90 } |
| 77 | 91 |
| 78 void MojoServerBootstrap::OnChannelConnected(int32_t peer_pid) { | 92 void MojoServerBootstrap::OnChannelConnected(int32_t peer_pid) { |
| 79 DCHECK_EQ(state(), STATE_INITIALIZED); | 93 DCHECK_EQ(state(), STATE_INITIALIZED); |
| 80 connected_ = true; | 94 connected_ = true; |
| 95 peer_pid_ = peer_pid; |
| 81 SendClientPipe(peer_pid); | 96 SendClientPipe(peer_pid); |
| 82 } | 97 } |
| 83 | 98 |
| 84 bool MojoServerBootstrap::OnMessageReceived(const Message&) { | 99 bool MojoServerBootstrap::OnMessageReceived(const Message&) { |
| 85 if (state() != STATE_WAITING_ACK) { | 100 if (state() != STATE_WAITING_ACK) { |
| 86 set_state(STATE_ERROR); | 101 set_state(STATE_ERROR); |
| 87 LOG(ERROR) << "Got inconsistent message from client."; | 102 LOG(ERROR) << "Got inconsistent message from client."; |
| 88 return false; | 103 return false; |
| 89 } | 104 } |
| 90 | 105 |
| 91 set_state(STATE_READY); | 106 set_state(STATE_READY); |
| 92 CHECK(server_pipe_.is_valid()); | 107 CHECK(server_pipe_.is_valid()); |
| 108 #if defined(USE_CHROME_EDK) |
| 93 delegate()->OnPipeAvailable( | 109 delegate()->OnPipeAvailable( |
| 94 mojo::embedder::ScopedPlatformHandle(server_pipe_.release())); | 110 mojo::edk::ScopedPlatformHandle(server_pipe_.release()), peer_pid_); |
| 111 #else |
| 112 delegate()->OnPipeAvailable( |
| 113 mojo::embedder::ScopedPlatformHandle(server_pipe_.release()), peer_pid_); |
| 114 #endif |
| 95 | 115 |
| 96 return true; | 116 return true; |
| 97 } | 117 } |
| 98 | 118 |
| 99 // MojoBootstrap for client processes. You should create the instance | 119 // MojoBootstrap for client processes. You should create the instance |
| 100 // using MojoBootstrap::Create(). | 120 // using MojoBootstrap::Create(). |
| 101 class MojoClientBootstrap : public MojoBootstrap { | 121 class MojoClientBootstrap : public MojoBootstrap { |
| 102 public: | 122 public: |
| 103 MojoClientBootstrap(); | 123 MojoClientBootstrap(); |
| 104 | 124 |
| 105 private: | 125 private: |
| 106 // Listener implementations | 126 // Listener implementations |
| 107 bool OnMessageReceived(const Message& message) override; | 127 bool OnMessageReceived(const Message& message) override; |
| 108 void OnChannelConnected(int32_t peer_pid) override; | 128 void OnChannelConnected(int32_t peer_pid) override; |
| 109 | 129 |
| 130 int32 peer_pid_; |
| 131 |
| 110 DISALLOW_COPY_AND_ASSIGN(MojoClientBootstrap); | 132 DISALLOW_COPY_AND_ASSIGN(MojoClientBootstrap); |
| 111 }; | 133 }; |
| 112 | 134 |
| 113 MojoClientBootstrap::MojoClientBootstrap() { | 135 MojoClientBootstrap::MojoClientBootstrap() : peer_pid_(0) { |
| 114 } | 136 } |
| 115 | 137 |
| 116 bool MojoClientBootstrap::OnMessageReceived(const Message& message) { | 138 bool MojoClientBootstrap::OnMessageReceived(const Message& message) { |
| 117 if (state() != STATE_INITIALIZED) { | 139 if (state() != STATE_INITIALIZED) { |
| 118 set_state(STATE_ERROR); | 140 set_state(STATE_ERROR); |
| 119 LOG(ERROR) << "Got inconsistent message from server."; | 141 LOG(ERROR) << "Got inconsistent message from server."; |
| 120 return false; | 142 return false; |
| 121 } | 143 } |
| 122 | 144 |
| 123 PlatformFileForTransit pipe; | 145 PlatformFileForTransit pipe; |
| 124 base::PickleIterator iter(message); | 146 base::PickleIterator iter(message); |
| 125 if (!ParamTraits<PlatformFileForTransit>::Read(&message, &iter, &pipe)) { | 147 if (!ParamTraits<PlatformFileForTransit>::Read(&message, &iter, &pipe)) { |
| 126 LOG(WARNING) << "Failed to read a file handle from bootstrap channel."; | 148 LOG(WARNING) << "Failed to read a file handle from bootstrap channel."; |
| 127 message.set_dispatch_error(); | 149 message.set_dispatch_error(); |
| 128 return false; | 150 return false; |
| 129 } | 151 } |
| 130 | 152 |
| 131 // Sends ACK back. | 153 // Sends ACK back. |
| 132 Send(new Message()); | 154 Send(new Message()); |
| 133 set_state(STATE_READY); | 155 set_state(STATE_READY); |
| 156 #if defined(USE_CHROME_EDK) |
| 157 delegate()->OnPipeAvailable( |
| 158 mojo::edk::ScopedPlatformHandle(mojo::edk::PlatformHandle( |
| 159 PlatformFileForTransitToPlatformFile(pipe))), peer_pid_); |
| 160 #else |
| 134 delegate()->OnPipeAvailable( | 161 delegate()->OnPipeAvailable( |
| 135 mojo::embedder::ScopedPlatformHandle(mojo::embedder::PlatformHandle( | 162 mojo::embedder::ScopedPlatformHandle(mojo::embedder::PlatformHandle( |
| 136 PlatformFileForTransitToPlatformFile(pipe)))); | 163 PlatformFileForTransitToPlatformFile(pipe))), peer_pid_); |
| 164 #endif |
| 137 | 165 |
| 138 return true; | 166 return true; |
| 139 } | 167 } |
| 140 | 168 |
| 141 void MojoClientBootstrap::OnChannelConnected(int32_t peer_pid) { | 169 void MojoClientBootstrap::OnChannelConnected(int32_t peer_pid) { |
| 170 peer_pid_ = peer_pid; |
| 142 } | 171 } |
| 143 | 172 |
| 144 } // namespace | 173 } // namespace |
| 145 | 174 |
| 146 // MojoBootstrap | 175 // MojoBootstrap |
| 147 | 176 |
| 148 // static | 177 // static |
| 149 scoped_ptr<MojoBootstrap> MojoBootstrap::Create(ChannelHandle handle, | 178 scoped_ptr<MojoBootstrap> MojoBootstrap::Create(ChannelHandle handle, |
| 150 Channel::Mode mode, | 179 Channel::Mode mode, |
| 151 Delegate* delegate, | 180 Delegate* delegate, |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 int MojoBootstrap::GetClientFileDescriptor() const { | 238 int MojoBootstrap::GetClientFileDescriptor() const { |
| 210 return channel_->GetClientFileDescriptor(); | 239 return channel_->GetClientFileDescriptor(); |
| 211 } | 240 } |
| 212 | 241 |
| 213 base::ScopedFD MojoBootstrap::TakeClientFileDescriptor() { | 242 base::ScopedFD MojoBootstrap::TakeClientFileDescriptor() { |
| 214 return channel_->TakeClientFileDescriptor(); | 243 return channel_->TakeClientFileDescriptor(); |
| 215 } | 244 } |
| 216 #endif // defined(OS_POSIX) && !defined(OS_NACL) | 245 #endif // defined(OS_POSIX) && !defined(OS_NACL) |
| 217 | 246 |
| 218 } // namespace IPC | 247 } // namespace IPC |
| OLD | NEW |