Skip to content

Commit

Permalink
Move Mojo channel initialization closer to IPC::Channel setup
Browse files Browse the repository at this point in the history
This CL introduces two new content classes:
- MojoApplicationHost encapsulates what's needed to host a Mojo App using Chrome IPC to bootstrap.
- MojoApplication represents what's needed to be a Mojo App using Chrome IPC to bootstrap.

The RenderProcess and RenderProcessHost interfaces are replaced with WebUISetup and WebUISetupClient interfaces. This way the interface is more specific to the service of setting up WebUI.

WebUISetupClient is empty and uninteresting. RenderProcessHostImpl no longer deals with WebUI setup. That is all done directly by RenderViewHostImpl by talking to the WebUISetup service.

Service names get defined in content/common/mojo/mojo_service_names.{h,cc}.

TBR=sky@chromium.org, tsepez@chromium.org
Originally reviewed at https://codereview.chromium.org/236813002/

Review URL: https://codereview.chromium.org/256403004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@265962 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
darin@chromium.org committed Apr 24, 2014
1 parent c0b5b76 commit f68c146
Show file tree
Hide file tree
Showing 41 changed files with 487 additions and 371 deletions.
6 changes: 3 additions & 3 deletions content/app/content_main_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "content/browser/browser_main.h"
#include "content/browser/gpu/gpu_process_host.h"
#include "content/browser/renderer_host/render_process_host_impl.h"
#include "content/browser/utility_process_host_impl.h"
#include "content/common/set_process_title.h"
#include "content/common/url_schemes.h"
#include "content/gpu/in_process_gpu_thread.h"
Expand Down Expand Up @@ -63,6 +60,9 @@

#if !defined(OS_IOS)
#include "content/app/mojo/mojo_init.h"
#include "content/browser/gpu/gpu_process_host.h"
#include "content/browser/renderer_host/render_process_host_impl.h"
#include "content/browser/utility_process_host_impl.h"
#include "content/public/plugin/content_plugin_client.h"
#include "content/public/renderer/content_renderer_client.h"
#include "content/public/utility/content_utility_client.h"
Expand Down
7 changes: 6 additions & 1 deletion content/browser/browser_main_loop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#include "content/browser/net/browser_online_state_observer.h"
#include "content/browser/plugin_service_impl.h"
#include "content/browser/renderer_host/media/media_stream_manager.h"
#include "content/browser/renderer_host/render_process_host_impl.h"
#include "content/browser/speech/speech_recognition_manager_impl.h"
#include "content/browser/startup_task_runner.h"
#include "content/browser/webui/content_web_ui_controller_factory.h"
Expand Down Expand Up @@ -70,6 +69,10 @@
#include "ui/aura/env.h"
#endif

#if !defined(OS_IOS)
#include "content/browser/renderer_host/render_process_host_impl.h"
#endif

#if defined(OS_ANDROID)
#include "base/android/jni_android.h"
#include "content/browser/android/browser_startup_controller.h"
Expand Down Expand Up @@ -718,8 +721,10 @@ void BrowserMainLoop::ShutdownThreadsAndCleanUp() {
base::Bind(base::IgnoreResult(&base::ThreadRestrictions::SetIOAllowed),
true));

#if !defined(OS_IOS)
if (RenderProcessHost::run_renderer_in_process())
RenderProcessHostImpl::ShutDownInProcessRenderer();
#endif

if (parts_) {
TRACE_EVENT0("shutdown",
Expand Down
65 changes: 65 additions & 0 deletions content/browser/mojo/mojo_application_host.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/mojo/mojo_application_host.h"

#include "content/common/mojo/mojo_messages.h"
#include "content/public/browser/browser_thread.h"
#include "ipc/ipc_sender.h"
#include "mojo/embedder/platform_channel_pair.h"

namespace content {
namespace {

base::PlatformFile PlatformFileFromScopedPlatformHandle(
mojo::embedder::ScopedPlatformHandle handle) {
#if defined(OS_POSIX)
return handle.release().fd;
#elif defined(OS_WIN)
return handle.release().handle;
#endif
}

} // namespace

MojoApplicationHost::MojoApplicationHost() : did_activate_(false) {
}

MojoApplicationHost::~MojoApplicationHost() {
}

bool MojoApplicationHost::Init() {
DCHECK(shell_client_.is_null()) << "Already initialized!";

mojo::embedder::PlatformChannelPair channel_pair;

mojo::ScopedMessagePipeHandle message_pipe = channel_init_.Init(
PlatformFileFromScopedPlatformHandle(channel_pair.PassServerHandle()),
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
if (!message_pipe.is_valid())
return false;

// Forward this to the client once we know its process handle.
client_handle_ = channel_pair.PassClientHandle();

// TODO(darin): Provide a Shell implementation
shell_client_.reset(
mojo::ScopedShellClientHandle::From(message_pipe.Pass()), NULL);

return true;
}

bool MojoApplicationHost::Activate(IPC::Sender* sender,
base::ProcessHandle process_handle) {
DCHECK(!did_activate_);
DCHECK(client_handle_.is_valid());

base::PlatformFile client_file =
PlatformFileFromScopedPlatformHandle(client_handle_.Pass());
did_activate_ = sender->Send(new MojoMsg_Activate(
IPC::GetFileHandleForProcess(client_file, process_handle, true)));
return did_activate_;
}

} // namespace content
51 changes: 51 additions & 0 deletions content/browser/mojo/mojo_application_host.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_MOJO_MOJO_APPLICATION_HOST_H_
#define CONTENT_BROWSER_MOJO_MOJO_APPLICATION_HOST_H_

#include "base/process/process_handle.h"
#include "mojo/common/channel_init.h"
#include "mojo/embedder/scoped_platform_handle.h"
#include "mojo/public/cpp/bindings/remote_ptr.h"
#include "mojo/public/interfaces/shell/shell.mojom.h"

namespace IPC {
class Sender;
}

namespace content {

// MojoApplicationHost represents the code needed on the browser side to setup
// a child process as a Mojo application via Chrome IPC. The child process
// should use MojoApplication to handle messages generated by an instance of
// MojoApplicationHost. MojoApplicationHost makes the mojo::ShellClient
// interface available so that child-provided services can be invoked.
class MojoApplicationHost {
public:
MojoApplicationHost();
~MojoApplicationHost();

// Two-phase initialization:
// 1- Init makes the shell_client() available synchronously.
// 2- Activate establishes the actual connection to the peer process.
bool Init();
bool Activate(IPC::Sender* sender, base::ProcessHandle process_handle);

bool did_activate() const { return did_activate_; }

mojo::ShellClient* shell_client() { return shell_client_.get(); }

private:
mojo::common::ChannelInit channel_init_;
mojo::embedder::ScopedPlatformHandle client_handle_;
mojo::RemotePtr<mojo::ShellClient> shell_client_;
bool did_activate_;

DISALLOW_COPY_AND_ASSIGN(MojoApplicationHost);
};

} // namespace content

#endif // CONTENT_BROWSER_MOJO_MOJO_APPLICATION_HOST_H_
2 changes: 1 addition & 1 deletion content/browser/renderer_data_memoizing_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

#include "base/bind.h"
#include "base/synchronization/lock.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_process_host_observer.h"

namespace content {
Expand Down
35 changes: 25 additions & 10 deletions content/browser/renderer_host/render_process_host_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
#include "content/browser/media/media_internals.h"
#include "content/browser/message_port_message_filter.h"
#include "content/browser/mime_registry_message_filter.h"
#include "content/browser/mojo/mojo_application_host.h"
#include "content/browser/plugin_service_impl.h"
#include "content/browser/profiler_message_filter.h"
#include "content/browser/push_messaging_message_filter.h"
Expand All @@ -86,7 +87,6 @@
#include "content/browser/renderer_host/pepper/pepper_message_filter.h"
#include "content/browser/renderer_host/pepper/pepper_renderer_connection.h"
#include "content/browser/renderer_host/render_message_filter.h"
#include "content/browser/renderer_host/render_process_host_mojo_impl.h"
#include "content/browser/renderer_host/render_view_host_delegate.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/browser/renderer_host/render_widget_helper.h"
Expand All @@ -112,6 +112,7 @@
#include "content/common/child_process_messages.h"
#include "content/common/content_switches_internal.h"
#include "content/common/gpu/gpu_messages.h"
#include "content/common/mojo/mojo_messages.h"
#include "content/common/resource_messages.h"
#include "content/common/view_messages.h"
#include "content/port/browser/render_widget_host_view_frame_subscriber.h"
Expand All @@ -137,6 +138,8 @@
#include "ipc/ipc_logging.h"
#include "ipc/ipc_switches.h"
#include "media/base/media_switches.h"
#include "mojo/common/common_type_converters.h"
#include "mojo/public/cpp/bindings/allocation_scope.h"
#include "net/url_request/url_request_context_getter.h"
#include "ppapi/shared_impl/ppapi_switches.h"
#include "third_party/skia/include/core/SkBitmap.h"
Expand Down Expand Up @@ -538,6 +541,10 @@ bool RenderProcessHostImpl::Init() {
BrowserThread::GetMessageLoopProxyForThread(
BrowserThread::IO).get()));

// Setup the Mojo channel.
mojo_application_host_.reset(new MojoApplicationHost());
mojo_application_host_->Init();

// Call the embedder first so that their IPC filters have priority.
GetContentClient()->browser()->RenderProcessWillLaunch(this);

Expand Down Expand Up @@ -1827,7 +1834,7 @@ void RenderProcessHostImpl::ProcessDied(bool already_dead) {
iter.Advance();
}

render_process_host_mojo_.reset();
mojo_application_host_.reset();

// It's possible that one of the calls out to the observers might have caused
// this object to be no longer needed.
Expand Down Expand Up @@ -1962,6 +1969,14 @@ void RenderProcessHostImpl::OnProcessLaunched() {
Source<RenderProcessHost>(this),
NotificationService::NoDetails());

// TODO(darin): This is blocked on security review. Un-commenting this will
// allow Mojo calls from all renderers.
#if 0
// Let the Mojo system get setup on the child process side before any other
// IPCs arrive. This way those may safely generate Mojo-related RPCs.
mojo_application_host_->Activate(this, GetHandle());
#endif

while (!queued_messages_.empty()) {
Send(queued_messages_.front());
queued_messages_.pop();
Expand All @@ -1971,9 +1986,6 @@ void RenderProcessHostImpl::OnProcessLaunched() {
if (WebRTCInternals::GetInstance()->aec_dump_enabled())
EnableAecDump(WebRTCInternals::GetInstance()->aec_dump_file_path());
#endif

if (render_process_host_mojo_.get())
render_process_host_mojo_->OnProcessLaunched();
}

scoped_refptr<AudioRendererHost>
Expand Down Expand Up @@ -2047,12 +2059,15 @@ void RenderProcessHostImpl::DecrementWorkerRefCount() {
Cleanup();
}

void RenderProcessHostImpl::SetWebUIHandle(
int32 view_routing_id,
void RenderProcessHostImpl::ConnectTo(
const base::StringPiece& service_name,
mojo::ScopedMessagePipeHandle handle) {
if (!render_process_host_mojo_)
render_process_host_mojo_.reset(new RenderProcessHostMojoImpl(this));
render_process_host_mojo_->SetWebUIHandle(view_routing_id, handle.Pass());
if (!mojo_application_host_->did_activate())
mojo_application_host_->Activate(this, GetHandle());

mojo::AllocationScope scope;
mojo_application_host_->shell_client()->AcceptConnection(service_name,
handle.Pass());
}

} // namespace content
15 changes: 10 additions & 5 deletions content/browser/renderer_host/render_process_host_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
#include "content/public/browser/render_process_host.h"
#include "ipc/ipc_channel_proxy.h"
#include "ipc/ipc_platform_file.h"
#include "mojo/public/cpp/system/core.h"
#include "mojo/embedder/scoped_platform_handle.h"
#include "mojo/public/cpp/bindings/remote_ptr.h"
#include "mojo/public/interfaces/shell/shell.mojom.h"

struct ViewHostMsg_CompositorSurfaceBuffersSwapped_Params;

Expand All @@ -40,6 +42,7 @@ class BrowserDemuxerAndroid;
class GeolocationDispatcherHost;
class GpuMessageFilter;
class MessagePortMessageFilter;
class MojoApplicationHost;
class PeerConnectionTrackerHost;
class RendererMainThread;
class RenderProcessHostMojoImpl;
Expand Down Expand Up @@ -237,8 +240,10 @@ class CONTENT_EXPORT RenderProcessHostImpl
void IncrementWorkerRefCount();
void DecrementWorkerRefCount();

void SetWebUIHandle(int32 view_routing_id,
mojo::ScopedMessagePipeHandle handle);
// Establish a connection to a renderer-provided service. See
// content/common/mojo/mojo_service_names.h for a list of services.
void ConnectTo(const base::StringPiece& service_name,
mojo::ScopedMessagePipeHandle handle);

protected:
// A proxy for our IPC::Channel that lives on the IO thread (see
Expand Down Expand Up @@ -303,6 +308,8 @@ class CONTENT_EXPORT RenderProcessHostImpl
void SendDisableAecDumpToRenderer();
#endif

scoped_ptr<MojoApplicationHost> mojo_application_host_;

// The registered IPC listener objects. When this list is empty, we should
// delete ourselves.
IDMap<IPC::Listener> listeners_;
Expand Down Expand Up @@ -416,8 +423,6 @@ class CONTENT_EXPORT RenderProcessHostImpl
// Records the time when the process starts surviving for workers for UMA.
base::TimeTicks survive_for_worker_start_time_;

scoped_ptr<RenderProcessHostMojoImpl> render_process_host_mojo_;

base::WeakPtrFactory<RenderProcessHostImpl> weak_factory_;

DISALLOW_COPY_AND_ASSIGN(RenderProcessHostImpl);
Expand Down
Loading

0 comments on commit f68c146

Please sign in to comment.