Skip to content

Commit

Permalink
Convert NaCl renderer-loader messages to mojo.
Browse files Browse the repository at this point in the history
BUG=577685

Review-Url: https://codereview.chromium.org/2525483002
Cr-Commit-Position: refs/heads/master@{#439770}
  • Loading branch information
sammc authored and Commit bot committed Dec 20, 2016
1 parent 574d1b6 commit eb52daa
Show file tree
Hide file tree
Showing 21 changed files with 157 additions and 181 deletions.
1 change: 1 addition & 0 deletions components/nacl/DEPS
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include_rules = [
"+content/public/common",
"+ipc",
"+mojo/public",
]
17 changes: 15 additions & 2 deletions components/nacl/common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# found in the LICENSE file.

import("//build/config/features.gni")
import("//mojo/public/tools/bindings/mojom.gni")

if (enable_nacl) {
# This is separate so it can be used by ../broker:nacl64.
Expand All @@ -15,8 +16,6 @@ if (enable_nacl) {
"nacl_messages.cc",
"nacl_messages.h",
"nacl_process_type.h",
"nacl_renderer_messages.cc",
"nacl_renderer_messages.h",
"nacl_sandbox_type.h",
"nacl_types.cc",
"nacl_types.h",
Expand All @@ -30,6 +29,7 @@ if (enable_nacl) {

deps = [
":minimal_content_dummy",
":nacl_error_code",
"//base",
"//base:base_static",
"//ipc",
Expand Down Expand Up @@ -66,6 +66,7 @@ if (enable_nacl) {

public_deps = [
":minimal",
":mojo_bindings",
":switches",
]

Expand Down Expand Up @@ -96,6 +97,18 @@ if (enable_nacl) {
"//base",
]
}

source_set("nacl_error_code") {
sources = [
"//native_client/src/trusted/service_runtime/nacl_error_code.h",
]
}

mojom("mojo_bindings") {
sources = [
"nacl.mojom",
]
}
}

# Depending on this allows targets to unconditionally include
Expand Down
4 changes: 4 additions & 0 deletions components/nacl/common/OWNERS
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
per-file *_messages*.h=set noparent
per-file *_messages*.h=file://ipc/SECURITY_OWNERS
per-file *.mojom=set noparent
per-file *.mojom=file://ipc/SECURITY_OWNERS
per-file *_param_traits*.*=set noparent
per-file *_param_traits*.*=file://ipc/SECURITY_OWNERS
26 changes: 26 additions & 0 deletions components/nacl/common/nacl.mojom
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2016 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.

module nacl.mojom;

[Native]
enum NaClErrorCode;

interface NaClRendererHost {
// This message must be synchronous to ensure that the exit status is sent
// from NaCl to the renderer before the NaCl process exits very soon after.
[Sync]
ReportExitStatus(int32 exit_status) => ();

// This message must be synchronous to ensure that the load status is sent
// from NaCl to the renderer before the NaCl process exits very soon after.
[Sync]
ReportLoadStatus(NaClErrorCode load_status) => ();

ProvideExitControl(NaClExitControl exit_control);
};

// When this interface is closed, it indicates that the NaCl loader process
// should exit.
interface NaClExitControl {};
15 changes: 15 additions & 0 deletions components/nacl/common/nacl.typemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2016 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.

mojom = "//components/nacl/common/nacl.mojom"
public_headers =
[ "//native_client/src/trusted/service_runtime/nacl_error_code.h" ]
traits_headers = [ "//components/nacl/common/nacl_types_param_traits.h" ]
deps = [
"//components/nacl/common:minimal",
"//components/nacl/common:nacl_error_code",
"//ipc",
]

type_mappings = [ "nacl.mojom.NaClErrorCode=::NaClErrorCode" ]
39 changes: 0 additions & 39 deletions components/nacl/common/nacl_renderer_messages.cc

This file was deleted.

23 changes: 0 additions & 23 deletions components/nacl/common/nacl_renderer_messages.h

This file was deleted.

3 changes: 3 additions & 0 deletions components/nacl/common/nacl_types_param_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@

#include "components/nacl/common/nacl_types.h"
#include "ipc/ipc_message_macros.h"
#include "native_client/src/trusted/service_runtime/nacl_error_code.h"

IPC_ENUM_TRAITS_MAX_VALUE(nacl::NaClAppProcessType,
nacl::kNumNaClProcessTypes - 1)

IPC_ENUM_TRAITS_MAX_VALUE(NaClErrorCode, NACL_ERROR_CODE_MAX)

#endif // COMPONENTS_NACL_COMMON_NACL_TYPES_PARAM_TRAITS_H_
2 changes: 2 additions & 0 deletions components/nacl/loader/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ source_set("minimal") {
":minimal_content_dummy",
"//base",
"//components/nacl/common:minimal",
"//components/nacl/common:mojo_bindings",
"//crypto",
"//ipc",
"//mojo/edk/system",
Expand Down Expand Up @@ -214,6 +215,7 @@ if (is_nacl_nonsfi) {
deps = [
":nacl_helper_nonsfi_sandbox",
"//base",
"//components/nacl/common:mojo_bindings",
"//components/nacl/common:switches",
"//components/tracing",
"//content",
Expand Down
19 changes: 9 additions & 10 deletions components/nacl/loader/nacl_listener.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
#include "base/single_thread_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "build/build_config.h"
#include "components/nacl/common/nacl.mojom.h"
#include "components/nacl/common/nacl_messages.h"
#include "components/nacl/common/nacl_renderer_messages.h"
#include "components/nacl/common/nacl_switches.h"
#include "components/nacl/loader/nacl_ipc_adapter.h"
#include "components/nacl/loader/nacl_validation_db.h"
Expand Down Expand Up @@ -76,9 +76,8 @@ void FatalLogHandler(const char* data, size_t bytes) {
}

void LoadStatusCallback(int load_status) {
g_listener->trusted_listener()->Send(
new NaClRendererMsg_ReportLoadStatus(
static_cast<NaClErrorCode>(load_status)));
g_listener->trusted_listener()->renderer_host()->ReportLoadStatus(
static_cast<NaClErrorCode>(load_status));
}

#if defined(OS_LINUX)
Expand Down Expand Up @@ -349,15 +348,15 @@ void NaClListener::OnStart(const nacl::NaClStartParams& params) {
base::Bind(&NaClListener::ResolveFileToken, base::Unretained(this)),
base::Bind(&NaClListener::OnOpenResource, base::Unretained(this)));

mojo::MessagePipe trusted_pipe;
trusted_listener_ =
new NaClTrustedListener(trusted_pipe.handle0.release(),
io_thread_.task_runner().get(), &shutdown_event_);
nacl::mojom::NaClRendererHostPtr renderer_host;
if (!Send(new NaClProcessHostMsg_PpapiChannelsCreated(
browser_handle, ppapi_renderer_handle, trusted_pipe.handle1.release(),
browser_handle, ppapi_renderer_handle,
GetProxy(&renderer_host).PassMessagePipe().release(),
manifest_service_handle)))
LOG(FATAL) << "Failed to send IPC channel handle to NaClProcessHost.";

trusted_listener_ = base::MakeUnique<NaClTrustedListener>(
std::move(renderer_host), io_thread_.task_runner().get());
struct NaClChromeMainArgs* args = NaClChromeMainArgsCreate();
if (args == NULL) {
LOG(FATAL) << "NaClChromeMainArgsCreate() failed";
Expand Down Expand Up @@ -446,7 +445,7 @@ void NaClListener::OnStart(const nacl::NaClStartParams& params) {
NaClExit(1);

// Report the plugin's exit status if the application started successfully.
trusted_listener_->Send(new NaClRendererMsg_ReportExitStatus(exit_status));
trusted_listener_->renderer_host()->ReportExitStatus(exit_status);
NaClExit(exit_status);
}

Expand Down
2 changes: 1 addition & 1 deletion components/nacl/loader/nacl_listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class NaClListener : public IPC::Listener {

std::unique_ptr<base::SharedMemory> crash_info_shmem_;

scoped_refptr<NaClTrustedListener> trusted_listener_;
std::unique_ptr<NaClTrustedListener> trusted_listener_;

ResolveFileTokenCallback resolved_cb_;

Expand Down
55 changes: 24 additions & 31 deletions components/nacl/loader/nacl_trusted_listener.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,45 @@

#include "base/single_thread_task_runner.h"
#include "build/build_config.h"
#include "ipc/message_filter.h"
#include "mojo/public/cpp/bindings/strong_binding.h"
#include "native_client/src/public/chrome_main.h"

namespace {

// The OnChannelError() event must be processed in a MessageFilter so it can
// be handled on the IO thread. The main thread used by NaClListener is busy
// in NaClChromeMainAppStart(), so it can't be used for servicing messages.
class EOFMessageFilter : public IPC::MessageFilter {
class NaClExitControlImpl : public nacl::mojom::NaClExitControl {
public:
void OnChannelError() override {
// The renderer process dropped its connection to this process (the NaCl
// loader process), either because the <embed> element was removed
// (perhaps implicitly if the tab was closed) or because the renderer
// crashed. The NaCl loader process should therefore exit.
~NaClExitControlImpl() override {
// If the binding disconnects, the renderer process dropped its connection
// to this process (the NaCl loader process), either because the <embed>
// element was removed (perhaps implicitly if the tab was closed) or because
// the renderer crashed. The NaCl loader process should therefore exit.
//
// For SFI NaCl, trusted code does this exit voluntarily, but untrusted
// code cannot disable it. However, for Non-SFI NaCl, the following exit
// call could be disabled by untrusted code.
NaClExit(0);
}
private:
~EOFMessageFilter() override {}
};

void CreateExitControl(nacl::mojom::NaClExitControlRequest request) {
mojo::MakeStrongBinding(base::MakeUnique<NaClExitControlImpl>(),
std::move(request));
}

NaClTrustedListener::NaClTrustedListener(
const IPC::ChannelHandle& handle,
base::SingleThreadTaskRunner* ipc_task_runner,
base::WaitableEvent* shutdown_event)
: channel_handle_(handle) {
channel_ =
IPC::SyncChannel::Create(handle, IPC::Channel::MODE_SERVER, this,
ipc_task_runner, true, /* create_channel_now */
shutdown_event);
channel_->AddFilter(new EOFMessageFilter());
}

NaClTrustedListener::~NaClTrustedListener() {
}
} // namespace

bool NaClTrustedListener::OnMessageReceived(const IPC::Message& msg) {
return false;
NaClTrustedListener::NaClTrustedListener(
nacl::mojom::NaClRendererHostPtr renderer_host,
base::SingleThreadTaskRunner* io_task_runner)
: renderer_host_(std::move(renderer_host)) {
nacl::mojom::NaClExitControlPtr exit_control;
// The exit control binding must run on the IO thread. The main thread used
// by NaClListener is busy in NaClChromeMainAppStart(), so it can't be used
// for servicing messages.
io_task_runner->PostTask(
FROM_HERE, base::Bind(&CreateExitControl,
base::Passed(mojo::GetProxy(&exit_control))));
renderer_host_->ProvideExitControl(std::move(exit_control));
}

bool NaClTrustedListener::Send(IPC::Message* msg) {
return channel_->Send(msg);
}
NaClTrustedListener::~NaClTrustedListener() = default;
26 changes: 9 additions & 17 deletions components/nacl/loader/nacl_trusted_listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,24 @@
#define COMPONENTS_NACL_LOADER_NACL_TRUSTED_LISTENER_H_

#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "ipc/ipc_channel_handle.h"
#include "ipc/ipc_listener.h"
#include "ipc/ipc_sync_channel.h"
#include "components/nacl/common/nacl.mojom.h"

namespace base {
class SingleThreadTaskRunner;
}

class NaClTrustedListener : public base::RefCounted<NaClTrustedListener>,
public IPC::Listener {
class NaClTrustedListener {
public:
NaClTrustedListener(const IPC::ChannelHandle& handle,
base::SingleThreadTaskRunner* ipc_task_runner,
base::WaitableEvent* shutdown_event);
NaClTrustedListener(nacl::mojom::NaClRendererHostPtr renderer_host,
base::SingleThreadTaskRunner* io_task_runner);
~NaClTrustedListener();

// Listener implementation.
bool OnMessageReceived(const IPC::Message& message) override;

bool Send(IPC::Message* msg);
nacl::mojom::NaClRendererHost* renderer_host() {
return renderer_host_.get();
}

private:
friend class base::RefCounted<NaClTrustedListener>;
~NaClTrustedListener() override;
IPC::ChannelHandle channel_handle_;
std::unique_ptr<IPC::SyncChannel> channel_;
nacl::mojom::NaClRendererHostPtr renderer_host_;

DISALLOW_COPY_AND_ASSIGN(NaClTrustedListener);
};
Expand Down
Loading

0 comments on commit eb52daa

Please sign in to comment.