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.
4
5#include "content/browser/mojo/mojo_application_host.h"
6
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/embedder/platform_channel_pair.h"
11
12namespace content {
13namespace {
14
15base::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
22}
23
24}  // namespace
25
26MojoApplicationHost::MojoApplicationHost() : did_activate_(false) {
27}
28
29MojoApplicationHost::~MojoApplicationHost() {
30}
31
32bool MojoApplicationHost::Init() {
33  DCHECK(!child_service_provider_.get()) << "Already initialized!";
34
35  mojo::embedder::PlatformChannelPair channel_pair;
36
37  mojo::ScopedMessagePipeHandle message_pipe = channel_init_.Init(
38      PlatformFileFromScopedPlatformHandle(channel_pair.PassServerHandle()),
39      BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
40  if (!message_pipe.is_valid())
41    return false;
42
43  // Forward this to the client once we know its process handle.
44  client_handle_ = channel_pair.PassClientHandle();
45
46  child_service_provider_.reset(
47      BindToPipe(new ServiceProviderImpl(), message_pipe.Pass()));
48  return true;
49}
50
51bool MojoApplicationHost::Activate(IPC::Sender* sender,
52                                   base::ProcessHandle process_handle) {
53  DCHECK(!did_activate_);
54  DCHECK(client_handle_.is_valid());
55
56  base::PlatformFile client_file =
57      PlatformFileFromScopedPlatformHandle(client_handle_.Pass());
58  did_activate_ = sender->Send(new MojoMsg_Activate(
59      IPC::GetFileHandleForProcess(client_file, process_handle, true)));
60  return did_activate_;
61}
62
63void MojoApplicationHost::ServiceProviderImpl::ConnectToService(
64    const mojo::String& service_url,
65    const mojo::String& service_name,
66    mojo::ScopedMessagePipeHandle handle,
67    const mojo::String& requestor_url) {
68  // TODO(darin): Provide something meaningful here.
69}
70
71}  // namespace content
72