Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / render_process_host_mojo_impl.cc
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/renderer_host/render_process_host_mojo_impl.h"
6
7 #include "base/platform_file.h"
8 #include "content/common/mojo/mojo_channel_init.h"
9 #include "content/common/mojo/mojo_messages.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/render_process_host.h"
12 #include "mojo/embedder/platform_channel_pair.h"
13
14 namespace content {
15
16 namespace {
17
18 base::PlatformFile PlatformFileFromScopedPlatformHandle(
19     mojo::embedder::ScopedPlatformHandle handle) {
20 #if defined(OS_POSIX)
21   return handle.release().fd;
22 #elif defined(OS_WIN)
23   return handle.release().handle;
24 #endif
25 }
26
27 }  // namespace
28
29 struct RenderProcessHostMojoImpl::PendingHandle {
30   PendingHandle() : view_routing_id(0) {}
31
32   int32 view_routing_id;
33   mojo::ScopedMessagePipeHandle handle;
34 };
35
36 RenderProcessHostMojoImpl::RenderProcessHostMojoImpl(RenderProcessHost* host)
37     : host_(host) {
38 }
39
40 RenderProcessHostMojoImpl::~RenderProcessHostMojoImpl() {
41 }
42
43 void RenderProcessHostMojoImpl::SetWebUIHandle(
44     int32 view_routing_id,
45     mojo::ScopedMessagePipeHandle handle) {
46   base::ProcessHandle process = host_->GetHandle();
47   if (process != base::kNullProcessHandle) {
48     CreateMojoChannel(process);  // Does nothing if already connected.
49     if (!render_process_mojo_.is_null()) {
50       render_process_mojo_->SetWebUIHandle(view_routing_id, handle.Pass());
51       return;
52     }
53   }
54
55   // Remember the request, we'll attempt to reconnect once the child process is
56   // launched.
57   pending_handle_.reset(new PendingHandle);
58   pending_handle_->view_routing_id = view_routing_id;
59   pending_handle_->handle = handle.Pass();
60 }
61
62 void RenderProcessHostMojoImpl::OnProcessLaunched() {
63   if (pending_handle_) {
64     scoped_ptr<PendingHandle> handle(pending_handle_.Pass());
65     SetWebUIHandle(handle->view_routing_id, handle->handle.Pass());
66   }
67 }
68
69
70 void RenderProcessHostMojoImpl::CreateMojoChannel(
71     base::ProcessHandle process_handle) {
72   if (mojo_channel_init_.get())
73     return;
74
75   mojo::embedder::PlatformChannelPair channel_pair;
76   mojo_channel_init_.reset(new MojoChannelInit);
77   mojo_channel_init_->Init(
78       PlatformFileFromScopedPlatformHandle(channel_pair.PassServerHandle()),
79       BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
80   if (!mojo_channel_init_->is_handle_valid())
81     return;
82   base::PlatformFile client_file =
83       PlatformFileFromScopedPlatformHandle(channel_pair.PassClientHandle());
84   host_->Send(new MojoMsg_ChannelCreated(
85                   IPC::GetFileHandleForProcess(client_file, process_handle,
86                                                true)));
87   ScopedRenderProcessMojoHandle render_process_handle(
88       RenderProcessMojoHandle(
89           mojo_channel_init_->bootstrap_message_pipe().release().value()));
90   render_process_mojo_.reset(render_process_handle.Pass(), this);
91 }
92
93 }  // namespace content