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
3 // found in the LICENSE file.
5 #include "content/browser/mojo/mojo_application_host.h"
7 #include "content/common/mojo/mojo_messages.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "ipc/ipc_sender.h"
10 #include "third_party/mojo/src/mojo/edk/embedder/platform_channel_pair.h"
15 base::PlatformFile
PlatformFileFromScopedPlatformHandle(
16 mojo::embedder::ScopedPlatformHandle handle
) {
18 return handle
.release().fd
;
20 return handle
.release().handle
;
24 class ApplicationSetupImpl
: public ApplicationSetup
{
26 ApplicationSetupImpl(ServiceRegistryImpl
* service_registry
,
27 mojo::InterfaceRequest
<ApplicationSetup
> request
)
28 : binding_(this, request
.Pass()),
29 service_registry_(service_registry
) {
32 ~ApplicationSetupImpl() override
{
36 // ApplicationSetup implementation.
37 void ExchangeServiceProviders(
38 mojo::InterfaceRequest
<mojo::ServiceProvider
> services
,
39 mojo::ServiceProviderPtr exposed_services
) override
{
40 service_registry_
->Bind(services
.Pass());
41 service_registry_
->BindRemoteServiceProvider(exposed_services
.Pass());
44 mojo::Binding
<ApplicationSetup
> binding_
;
45 ServiceRegistryImpl
* service_registry_
;
50 MojoApplicationHost::MojoApplicationHost()
51 : did_activate_(false) {
52 #if defined(OS_ANDROID)
53 service_registry_android_
.reset(
54 new ServiceRegistryAndroid(&service_registry_
));
58 MojoApplicationHost::~MojoApplicationHost() {
61 bool MojoApplicationHost::Init() {
62 DCHECK(!client_handle_
.is_valid()) << "Already initialized!";
64 mojo::embedder::PlatformChannelPair channel_pair
;
66 scoped_refptr
<base::TaskRunner
> io_task_runner
;
67 if (io_task_runner_override_
) {
68 io_task_runner
= io_task_runner_override_
;
71 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO
)
75 mojo::ScopedMessagePipeHandle message_pipe
= channel_init_
.Init(
76 PlatformFileFromScopedPlatformHandle(channel_pair
.PassServerHandle()),
78 if (!message_pipe
.is_valid())
81 // Forward this to the client once we know its process handle.
82 client_handle_
= channel_pair
.PassClientHandle();
84 application_setup_
.reset(new ApplicationSetupImpl(
86 mojo::MakeRequest
<ApplicationSetup
>(message_pipe
.Pass())));
90 void MojoApplicationHost::Activate(IPC::Sender
* sender
,
91 base::ProcessHandle process_handle
) {
92 DCHECK(!did_activate_
);
93 DCHECK(client_handle_
.is_valid());
95 base::PlatformFile client_file
=
96 PlatformFileFromScopedPlatformHandle(client_handle_
.Pass());
97 did_activate_
= sender
->Send(new MojoMsg_Activate(
98 IPC::GetFileHandleForProcess(client_file
, process_handle
, true)));
101 void MojoApplicationHost::WillDestroySoon() {
102 channel_init_
.WillDestroySoon();
105 void MojoApplicationHost::ShutdownOnIOThread() {
106 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
107 channel_init_
.ShutdownOnIOThread();
110 void MojoApplicationHost::OverrideIOTaskRunnerForTest(
111 scoped_refptr
<base::TaskRunner
> io_task_runner
) {
112 io_task_runner_override_
= io_task_runner
;
115 } // namespace content