Skip to content

Commit

Permalink
Use unique URLs rather than qualifiers to uniquify RenderProcesses.
Browse files Browse the repository at this point in the history
This is so code in the browser has something tangible to call ConnectToApplication() on. When a RenderProcessHost creates a new renderer, it creates a URL of the style exe:chrome_renderer#, where # is the RenderProcess ID. It stores this string on the RenderProcessHost's SupportsUserData. Call content/browser/mojo/mojo_shell_client_host.h's GetMojoApplicationInstanceURL() with a particular RPH to get the URL to connect to it.

R=jam@chromium.org
http://crbug.com/558009

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

Cr-Commit-Position: refs/heads/master@{#360604}
  • Loading branch information
ben authored and Commit bot committed Nov 19, 2015
1 parent 84061ed commit 2c95144
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 24 deletions.
45 changes: 40 additions & 5 deletions content/browser/mojo/mojo_shell_client_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/strings/stringprintf.h"
#include "base/thread_task_runner_handle.h"
#include "content/browser/mojo/mojo_shell_client_host.h"
#include "content/common/mojo/mojo_messages.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/common/mojo_shell_connection.h"
#include "ipc/ipc_sender.h"
#include "mojo/application/public/cpp/application_impl.h"
Expand All @@ -16,6 +18,9 @@

namespace content {
namespace {

const char kMojoShellInstanceURL[] = "mojo_shell_instance_url";

void DidCreateChannel(mojo::embedder::ChannelInfo* info) {}

base::PlatformFile PlatformFileFromScopedPlatformHandle(
Expand All @@ -27,11 +32,30 @@ base::PlatformFile PlatformFileFromScopedPlatformHandle(
#endif
}

class InstanceURL : public base::SupportsUserData::Data {
public:
InstanceURL(const std::string& instance_url) : instance_url_(instance_url) {}
~InstanceURL() override {}

std::string get() const { return instance_url_; }

private:
std::string instance_url_;

DISALLOW_COPY_AND_ASSIGN(InstanceURL);
};

void SetMojoApplicationInstanceURL(RenderProcessHost* render_process_host,
const std::string& instance_url) {
render_process_host->SetUserData(kMojoShellInstanceURL,
new InstanceURL(instance_url));
}

} // namespace

void RegisterChildWithExternalShell(int child_process_id,
base::ProcessHandle process_handle,
IPC::Sender* sender) {
RenderProcessHost* render_process_host) {
// Some process types get created before the main message loop.
if (!MojoShellConnection::Get())
return;
Expand All @@ -57,16 +81,27 @@ void RegisterChildWithExternalShell(int child_process_id,
// specification is best determined (not here, this is a common
// chokepoint for all process types) and how to wire it through.
// http://crbug.com/555393
std::string url =
base::StringPrintf("exe:chrome_renderer%d", child_process_id);
application_manager->CreateInstanceForHandle(
mojo::ScopedHandle(mojo::Handle(handle.release().value())),
"exe:chrome_renderer", // See above about how this string is meaningless.
base::IntToString(child_process_id));
mojo::ScopedHandle(mojo::Handle(handle.release().value())), url);

// Send the other end to the child via Chrome IPC.
base::PlatformFile client_file = PlatformFileFromScopedPlatformHandle(
platform_channel_pair.PassClientHandle());
sender->Send(new MojoMsg_BindExternalMojoShellHandle(
render_process_host->Send(new MojoMsg_BindExternalMojoShellHandle(
IPC::GetFileHandleForProcess(client_file, process_handle, true)));

// Store the URL on the RPH so client code can access it later via
// GetMojoApplicationInstanceURL().
SetMojoApplicationInstanceURL(render_process_host, url);
}

std::string GetMojoApplicationInstanceURL(
RenderProcessHost* render_process_host) {
InstanceURL* instance_url = static_cast<InstanceURL*>(
render_process_host->GetUserData(kMojoShellInstanceURL));
return instance_url ? instance_url->get() : std::string();
}

} // namespace content
16 changes: 11 additions & 5 deletions content/browser/mojo/mojo_shell_client_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@
#ifndef CONTENT_BROWSER_MOJO_MOJO_SHELL_CLIENT_HOST_H_
#define CONTENT_BROWSER_MOJO_MOJO_SHELL_CLIENT_HOST_H_

#include "base/process/process_handle.h"
#include <string>

namespace IPC {
class Sender;
}
#include "base/process/process_handle.h"

namespace content {

class RenderProcessHost;

// Creates a communication channel between the external Mojo shell and the
// child. The server handle of this channel is shared with the external shell
// via Mojo IPC and the client handle is shared with the child via Chrome IPC.
// |child_process_id| is used to uniquify the child in the external shell's
// instance map.
void RegisterChildWithExternalShell(int child_process_id,
base::ProcessHandle process_handle,
IPC::Sender* sender);
RenderProcessHost* render_process_host);

// Returns the URL associated with an instance corresponding to the renderer
// process in the external shell. This URL can be passed to
// ConnectToApplication() to open a new connection to this renderer.
std::string GetMojoApplicationInstanceURL(
RenderProcessHost* render_process_host);

} // namespace content

Expand Down
5 changes: 2 additions & 3 deletions mojo/shell/application_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,14 @@ ApplicationInstance* ApplicationManager::GetApplicationInstance(
}

void ApplicationManager::CreateInstanceForHandle(ScopedHandle channel,
const GURL& url,
const std::string& qualifier) {
const GURL& url) {
// Instances created by others are considered unique, and thus have no
// identity. As such they cannot be connected to by anyone else, and so we
// never call ConnectToClient().
// TODO(beng): GetPermissiveCapabilityFilter() here obviously cannot make it
// to production. See note in application_manager.mojom.
// http://crbug.com/555392
Identity target_id(url, qualifier, GetPermissiveCapabilityFilter());
Identity target_id(url, std::string(), GetPermissiveCapabilityFilter());
InterfaceRequest<Application> application_request =
CreateInstance(target_id, base::Closure(), nullptr);
NativeRunner* runner =
Expand Down
4 changes: 1 addition & 3 deletions mojo/shell/application_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ class ApplicationManager {

ApplicationInstance* GetApplicationInstance(const Identity& identity) const;

void CreateInstanceForHandle(ScopedHandle channel,
const GURL& url,
const std::string& qualifier);
void CreateInstanceForHandle(ScopedHandle channel, const GURL& url);

private:
using IdentityToInstanceMap = std::map<Identity, ApplicationInstance*>;
Expand Down
2 changes: 1 addition & 1 deletion mojo/shell/application_manager.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ interface ApplicationManager {
// otherwise child processes registered with the shell will be
// able to request any application/service.
// http://crbug.com/555392
CreateInstanceForHandle(handle channel, string url, string qualifier);
CreateInstanceForHandle(handle channel, string url);
};
8 changes: 3 additions & 5 deletions mojo/shell/shell_application_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ void ShellApplicationDelegate::Create(
bindings_.AddBinding(this, request.Pass());
}

void ShellApplicationDelegate::CreateInstanceForHandle(
ScopedHandle channel,
const String& url,
const String& qualifier) {
manager_->CreateInstanceForHandle(channel.Pass(), GURL(url), qualifier);
void ShellApplicationDelegate::CreateInstanceForHandle(ScopedHandle channel,
const String& url) {
manager_->CreateInstanceForHandle(channel.Pass(), GURL(url));
}

} // namespace shell
Expand Down
3 changes: 1 addition & 2 deletions mojo/shell/shell_application_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ class ShellApplicationDelegate

// Overridden from mojom::ApplicationManager:
void CreateInstanceForHandle(ScopedHandle channel,
const String& url,
const String& qualifier) override;
const String& url) override;

mojo::shell::ApplicationManager* manager_;

Expand Down

0 comments on commit 2c95144

Please sign in to comment.