Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / content / browser / mojo / mojo_application_host.cc
blobb1a9382f9adfd02e361b142e79b3f1731343699a
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 scoped_refptr<base::TaskRunner> io_task_runner;
67 if (io_task_runner_override_) {
68 io_task_runner = io_task_runner_override_;
69 } else {
70 io_task_runner =
71 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO)
72 ->task_runner();
75 mojo::ScopedMessagePipeHandle message_pipe = channel_init_.Init(
76 PlatformFileFromScopedPlatformHandle(channel_pair.PassServerHandle()),
77 io_task_runner);
78 if (!message_pipe.is_valid())
79 return false;
81 // Forward this to the client once we know its process handle.
82 client_handle_ = channel_pair.PassClientHandle();
84 application_setup_.reset(new ApplicationSetupImpl(
85 &service_registry_,
86 mojo::MakeRequest<ApplicationSetup>(message_pipe.Pass())));
87 return true;
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