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 "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
;
26 MojoApplicationHost::MojoApplicationHost() : did_activate_(false) {
27 #if defined(OS_ANDROID)
28 service_registry_android_
.reset(
29 new ServiceRegistryAndroid(&service_registry_
));
33 MojoApplicationHost::~MojoApplicationHost() {
36 bool MojoApplicationHost::Init() {
37 DCHECK(!client_handle_
.is_valid()) << "Already initialized!";
39 mojo::embedder::PlatformChannelPair channel_pair
;
41 mojo::ScopedMessagePipeHandle message_pipe
= channel_init_
.Init(
42 PlatformFileFromScopedPlatformHandle(channel_pair
.PassServerHandle()),
43 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
));
44 if (!message_pipe
.is_valid())
47 // Forward this to the client once we know its process handle.
48 client_handle_
= channel_pair
.PassClientHandle();
50 service_registry_
.BindRemoteServiceProvider(message_pipe
.Pass());
54 void MojoApplicationHost::Activate(IPC::Sender
* sender
,
55 base::ProcessHandle process_handle
) {
56 DCHECK(!did_activate_
);
57 DCHECK(client_handle_
.is_valid());
59 base::PlatformFile client_file
=
60 PlatformFileFromScopedPlatformHandle(client_handle_
.Pass());
61 did_activate_
= sender
->Send(new MojoMsg_Activate(
62 IPC::GetFileHandleForProcess(client_file
, process_handle
, true)));
65 void MojoApplicationHost::WillDestroySoon() {
66 channel_init_
.WillDestroySoon();
69 } // namespace content