| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "content/common/mojo/channel_init.h" | 5 #include "content/common/mojo/channel_init.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/task_runner.h" | 15 #include "base/task_runner.h" |
| 16 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
| 17 #include "mojo/edk/embedder/embedder.h" | 17 #include "mojo/edk/embedder/embedder.h" |
| 18 #include "third_party/mojo/src/mojo/edk/embedder/embedder.h" | |
| 19 | 18 |
| 20 namespace content { | 19 namespace content { |
| 21 | 20 |
| 22 namespace { | 21 namespace { |
| 23 | 22 |
| 24 void CallMessagePipeCallbackOnThread( | 23 void CallMessagePipeCallbackOnThread( |
| 25 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& callback, | 24 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& callback, |
| 26 scoped_refptr<base::TaskRunner> task_runner, | 25 scoped_refptr<base::TaskRunner> task_runner, |
| 27 mojo::ScopedMessagePipeHandle pipe) { | 26 mojo::ScopedMessagePipeHandle pipe) { |
| 28 task_runner->PostTask(FROM_HERE, base::Bind(callback, base::Passed(&pipe))); | 27 task_runner->PostTask(FROM_HERE, base::Bind(callback, base::Passed(&pipe))); |
| 29 } | 28 } |
| 30 | 29 |
| 31 } // namespace | 30 } // namespace |
| 32 | 31 |
| 33 ChannelInit::ChannelInit() : channel_info_(nullptr), weak_factory_(this) {} | 32 ChannelInit::ChannelInit() : weak_factory_(this) {} |
| 34 | 33 |
| 35 ChannelInit::~ChannelInit() { | 34 ChannelInit::~ChannelInit() {} |
| 36 if (channel_info_) | |
| 37 mojo::embedder::DestroyChannel(channel_info_, | |
| 38 base::Bind(&base::DoNothing), nullptr); | |
| 39 } | |
| 40 | 35 |
| 41 void ChannelInit::Init( | 36 void ChannelInit::Init( |
| 42 base::PlatformFile file, | 37 base::PlatformFile file, |
| 43 scoped_refptr<base::TaskRunner> io_thread_task_runner, | 38 scoped_refptr<base::TaskRunner> io_thread_task_runner, |
| 44 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& callback) { | 39 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& callback) { |
| 45 scoped_ptr<IPC::ScopedIPCSupport> ipc_support( | 40 scoped_ptr<IPC::ScopedIPCSupport> ipc_support( |
| 46 new IPC::ScopedIPCSupport(io_thread_task_runner)); | 41 new IPC::ScopedIPCSupport(io_thread_task_runner)); |
| 47 if (base::CommandLine::ForCurrentProcess()->HasSwitch("use-new-edk")) { | 42 mojo::edk::CreateMessagePipe( |
| 48 mojo::edk::CreateMessagePipe( | 43 mojo::edk::ScopedPlatformHandle(mojo::edk::PlatformHandle(file)), |
| 49 mojo::edk::ScopedPlatformHandle(mojo::edk::PlatformHandle(file)), | 44 base::Bind(&CallMessagePipeCallbackOnThread, |
| 50 base::Bind(&CallMessagePipeCallbackOnThread, | 45 base::Bind(&ChannelInit::OnCreateMessagePipe, |
| 51 base::Bind(&ChannelInit::OnCreateMessagePipe, | 46 weak_factory_.GetWeakPtr(), |
| 52 weak_factory_.GetWeakPtr(), | 47 base::Passed(&ipc_support), callback), |
| 53 base::Passed(&ipc_support), | 48 base::ThreadTaskRunnerHandle::Get())); |
| 54 callback), | |
| 55 base::ThreadTaskRunnerHandle::Get())); | |
| 56 } else { | |
| 57 mojo::ScopedMessagePipeHandle message_pipe = mojo::embedder::CreateChannel( | |
| 58 mojo::embedder::ScopedPlatformHandle( | |
| 59 mojo::embedder::PlatformHandle(file)), | |
| 60 base::Bind(&ChannelInit::OnCreatedChannel, weak_factory_.GetWeakPtr(), | |
| 61 base::Passed(&ipc_support)), | |
| 62 base::ThreadTaskRunnerHandle::Get()); | |
| 63 callback.Run(std::move(message_pipe)); | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 void ChannelInit::WillDestroySoon() { | |
| 68 if (channel_info_) | |
| 69 mojo::embedder::WillDestroyChannelSoon(channel_info_); | |
| 70 } | |
| 71 | |
| 72 // static | |
| 73 void ChannelInit::OnCreatedChannel( | |
| 74 base::WeakPtr<ChannelInit> self, | |
| 75 scoped_ptr<IPC::ScopedIPCSupport> ipc_support, | |
| 76 mojo::embedder::ChannelInfo* channel) { | |
| 77 // If |self| was already destroyed, shut the channel down. | |
| 78 if (!self) { | |
| 79 mojo::embedder::DestroyChannel(channel, | |
| 80 base::Bind(&base::DoNothing), nullptr); | |
| 81 return; | |
| 82 } | |
| 83 | |
| 84 DCHECK(!self->channel_info_); | |
| 85 self->channel_info_ = channel; | |
| 86 self->ipc_support_ = std::move(ipc_support); | |
| 87 } | 49 } |
| 88 | 50 |
| 89 void ChannelInit::OnCreateMessagePipe( | 51 void ChannelInit::OnCreateMessagePipe( |
| 90 scoped_ptr<IPC::ScopedIPCSupport> ipc_support, | 52 scoped_ptr<IPC::ScopedIPCSupport> ipc_support, |
| 91 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& callback, | 53 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& callback, |
| 92 mojo::ScopedMessagePipeHandle pipe) { | 54 mojo::ScopedMessagePipeHandle pipe) { |
| 93 ipc_support_ = std::move(ipc_support); | 55 ipc_support_ = std::move(ipc_support); |
| 94 callback.Run(std::move(pipe)); | 56 callback.Run(std::move(pipe)); |
| 95 } | 57 } |
| 96 | 58 |
| 97 } // namespace content | 59 } // namespace content |
| OLD | NEW |