Chromium Code Reviews| Index: ipc/mojo/ipc_channel_mojo.cc |
| diff --git a/ipc/mojo/ipc_channel_mojo.cc b/ipc/mojo/ipc_channel_mojo.cc |
| index 086b420cc52071ef90c7b53cf77249714efd67be..dd6da3891f6a8c4e5d93b1bef61c719bd93df7c7 100644 |
| --- a/ipc/mojo/ipc_channel_mojo.cc |
| +++ b/ipc/mojo/ipc_channel_mojo.cc |
| @@ -9,6 +9,7 @@ |
| #include "base/lazy_instance.h" |
| #include "ipc/ipc_listener.h" |
| #include "ipc/mojo/ipc_channel_mojo_readers.h" |
| +#include "ipc/mojo/ipc_mojo_bootstrap.h" |
| #include "mojo/embedder/embedder.h" |
| #if defined(OS_POSIX) && !defined(OS_NACL) |
| @@ -41,8 +42,6 @@ class NullListener : public Listener { |
| } |
| }; |
| -base::LazyInstance<NullListener> g_null_listener = LAZY_INSTANCE_INITIALIZER; |
| - |
| class MojoChannelFactory : public ChannelFactory { |
| public: |
| MojoChannelFactory( |
| @@ -72,16 +71,79 @@ class MojoChannelFactory : public ChannelFactory { |
| scoped_refptr<base::TaskRunner> io_thread_task_runner_; |
| }; |
| -mojo::embedder::PlatformHandle ToPlatformHandle( |
| - const ChannelHandle& handle) { |
| -#if defined(OS_POSIX) && !defined(OS_NACL) |
| - return mojo::embedder::PlatformHandle(handle.socket.fd); |
| -#elif defined(OS_WIN) |
| - return mojo::embedder::PlatformHandle(handle.pipe.handle); |
| -#else |
| -#error "Unsupported Platform!" |
| -#endif |
| -} |
| +//------------------------------------------------------------------------------ |
| + |
| +// TODO(morrita): This should be built using higher-level Mojo construct |
| +// for clarity and extensibility. |
| +class HelloMessage { |
|
viettrungluu
2014/09/15 22:37:27
I don't think you need (or use) this anywhere?
Hajime Morrita
2014/09/15 23:51:33
Right. Removing.
|
| + public: |
| + static Pickle CreateRequest(int32 pid) { |
| + Pickle request; |
| + request.WriteString(kHelloRequestMagic); |
| + request.WriteInt(pid); |
| + return request; |
| + } |
| + |
| + static bool ReadRequest(Pickle& pickle, int32* pid) { |
| + PickleIterator iter(pickle); |
| + std::string hello; |
| + if (!iter.ReadString(&hello)) { |
| + DLOG(WARNING) << "Failed to Read magic string."; |
| + return false; |
| + } |
| + |
| + if (hello != kHelloRequestMagic) { |
| + DLOG(WARNING) << "Magic mismatch:" << hello; |
| + return false; |
| + } |
| + |
| + int read_pid; |
| + if (!iter.ReadInt(&read_pid)) { |
| + DLOG(WARNING) << "Failed to Read PID."; |
| + return false; |
| + } |
| + |
| + *pid = read_pid; |
| + return true; |
| + } |
| + |
| + static Pickle CreateResponse(int32 pid) { |
| + Pickle request; |
| + request.WriteString(kHelloResponseMagic); |
| + request.WriteInt(pid); |
| + return request; |
| + } |
| + |
| + static bool ReadResponse(Pickle& pickle, int32* pid) { |
| + PickleIterator iter(pickle); |
| + std::string hello; |
| + if (!iter.ReadString(&hello)) { |
| + DLOG(WARNING) << "Failed to read magic string."; |
| + return false; |
| + } |
| + |
| + if (hello != kHelloResponseMagic) { |
| + DLOG(WARNING) << "Magic mismatch:" << hello; |
| + return false; |
| + } |
| + |
| + int read_pid; |
| + if (!iter.ReadInt(&read_pid)) { |
| + DLOG(WARNING) << "Failed to read PID."; |
| + return false; |
| + } |
| + |
| + *pid = read_pid; |
| + return true; |
| + } |
| + |
| + private: |
| + static const char* kHelloRequestMagic; |
| + static const char* kHelloResponseMagic; |
| +}; |
| + |
| +const char* HelloMessage::kHelloRequestMagic = "MREQ"; |
| +const char* HelloMessage::kHelloResponseMagic = "MRES"; |
| } // namespace |
| @@ -112,22 +174,21 @@ scoped_ptr<ChannelFactory> ChannelMojo::CreateFactory( |
| io_thread_task_runner)).PassAs<ChannelFactory>(); |
| } |
| -ChannelMojo::ChannelMojo(const ChannelHandle& channel_handle, |
| +ChannelMojo::ChannelMojo(const ChannelHandle& handle, |
| Mode mode, |
| Listener* listener, |
| scoped_refptr<base::TaskRunner> io_thread_task_runner) |
| - : bootstrap_( |
| - Channel::Create(channel_handle, mode, g_null_listener.Pointer())), |
| - mode_(mode), |
| + : mode_(mode), |
| listener_(listener), |
| peer_pid_(base::kNullProcessId), |
| weak_factory_(this) { |
| if (base::MessageLoopProxy::current() == io_thread_task_runner.get()) { |
| - InitOnIOThread(); |
| + InitBootstrap(handle); |
| } else { |
| - io_thread_task_runner->PostTask(FROM_HERE, |
| - base::Bind(&ChannelMojo::InitOnIOThread, |
| - weak_factory_.GetWeakPtr())); |
| + io_thread_task_runner->PostTask( |
| + FROM_HERE, |
| + base::Bind( |
| + &ChannelMojo::InitBootstrap, weak_factory_.GetWeakPtr(), handle)); |
| } |
| } |
| @@ -135,13 +196,17 @@ ChannelMojo::~ChannelMojo() { |
| Close(); |
| } |
| -void ChannelMojo::InitOnIOThread() { |
| +void ChannelMojo::InitBootstrap(ChannelHandle handle) { |
| + DCHECK(base::MessageLoopForIO::IsCurrent()); |
| + bootstrap_ = MojoBootstrap::Create(handle, mode_, this); |
| +} |
| + |
| +void ChannelMojo::InitControlReader( |
| + mojo::embedder::ScopedPlatformHandle handle) { |
| + DCHECK(base::MessageLoopForIO::IsCurrent()); |
| mojo::embedder::ChannelInfo* channel_info; |
| mojo::ScopedMessagePipeHandle control_pipe = |
| - mojo::embedder::CreateChannelOnIOThread( |
| - mojo::embedder::ScopedPlatformHandle( |
| - ToPlatformHandle(bootstrap_->TakePipeHandle())), |
| - &channel_info); |
| + mojo::embedder::CreateChannelOnIOThread(handle.Pass(), &channel_info); |
| channel_info_.reset(channel_info); |
| switch (mode_) { |
| @@ -161,7 +226,8 @@ void ChannelMojo::InitOnIOThread() { |
| bool ChannelMojo::Connect() { |
| DCHECK(!message_reader_); |
| - return control_reader_->Connect(); |
| + DCHECK(!control_reader_); |
| + return bootstrap_->Connect(); |
| } |
| void ChannelMojo::Close() { |
| @@ -170,6 +236,15 @@ void ChannelMojo::Close() { |
| channel_info_.reset(); |
| } |
| +void ChannelMojo::OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) { |
| + InitControlReader(handle.Pass()); |
| + control_reader_->Connect(); |
| +} |
| + |
| +void ChannelMojo::OnBootstrapError() { |
| + listener_->OnChannelError(); |
| +} |
| + |
| void ChannelMojo::OnConnected(mojo::ScopedMessagePipeHandle pipe) { |
| message_reader_ = |
| make_scoped_ptr(new internal::MessageReader(pipe.Pass(), this)); |
| @@ -207,11 +282,16 @@ base::ProcessId ChannelMojo::GetPeerPID() const { |
| } |
| base::ProcessId ChannelMojo::GetSelfPID() const { |
| - return bootstrap_->GetSelfPID(); |
| + return base::GetCurrentProcId(); |
| } |
| ChannelHandle ChannelMojo::TakePipeHandle() { |
| - return bootstrap_->TakePipeHandle(); |
| + NOTREACHED(); |
| + return ChannelHandle(); |
| +} |
| + |
| +void ChannelMojo::OnClientLaunched(base::ProcessHandle handle) { |
| + bootstrap_->OnClientLaunched(handle); |
| } |
| void ChannelMojo::OnMessageReceived(Message& message) { |