Add remoting and PPAPI tests to GN build
[chromium-blink-merge.git] / content / browser / mojo / mojo_application_host.cc
blob883945f92badedb8198ec0bc0ad776fc89acc62b
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"
12 namespace content {
13 namespace {
15 base::PlatformFile PlatformFileFromScopedPlatformHandle(
16 mojo::embedder::ScopedPlatformHandle handle) {
17 #if defined(OS_POSIX)
18 return handle.release().fd;
19 #elif defined(OS_WIN)
20 return handle.release().handle;
21 #endif
24 class ApplicationSetupImpl : public ApplicationSetup {
25 public:
26 ApplicationSetupImpl(ServiceRegistryImpl* service_registry,
27 mojo::InterfaceRequest<ApplicationSetup> request)
28 : binding_(this, request.Pass()),
29 service_registry_(service_registry) {
32 ~ApplicationSetupImpl() override {
35 private:
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_;
48 } // namespace
50 MojoApplicationHost::MojoApplicationHost()
51 : did_activate_(false) {
52 #if defined(OS_ANDROID)
53 service_registry_android_.reset(
54 new ServiceRegistryAndroid(&service_registry_));
55 #endif
58 MojoApplicationHost::~MojoApplicationHost() {
61 bool MojoApplicationHost::Init() {
62 DCHECK(!client_handle_.is_valid()) << "Already initialized!";
64 mojo::embedder::PlatformChannelPair channel_pair;
66 mojo::ScopedMessagePipeHandle message_pipe = channel_init_.Init(
67 PlatformFileFromScopedPlatformHandle(channel_pair.PassServerHandle()),
68 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
69 if (!message_pipe.is_valid())
70 return false;
72 // Forward this to the client once we know its process handle.
73 client_handle_ = channel_pair.PassClientHandle();
75 application_setup_.reset(new ApplicationSetupImpl(
76 &service_registry_,
77 mojo::MakeRequest<ApplicationSetup>(message_pipe.Pass())));
78 return true;
81 void MojoApplicationHost::Activate(IPC::Sender* sender,
82 base::ProcessHandle process_handle) {
83 DCHECK(!did_activate_);
84 DCHECK(client_handle_.is_valid());
86 base::PlatformFile client_file =
87 PlatformFileFromScopedPlatformHandle(client_handle_.Pass());
88 did_activate_ = sender->Send(new MojoMsg_Activate(
89 IPC::GetFileHandleForProcess(client_file, process_handle, true)));
92 void MojoApplicationHost::WillDestroySoon() {
93 channel_init_.WillDestroySoon();
96 } // namespace content