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