Commit 23ae8173 authored by viettrungluu's avatar viettrungluu Committed by Commit bot

Mojo: Have |ProxyMessagePipeEndpoint|s constructed with a |ChannelEndpoint|.

This eliminates the need for |ProxyMessagePipeEndpoint::Attach()|.

R=brettw@chromium.org

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

Cr-Commit-Position: refs/heads/master@{#296485}
parent faa99f8c
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
#include "mojo/system/core.h" #include "mojo/system/core.h"
#include "mojo/system/entrypoints.h" #include "mojo/system/entrypoints.h"
#include "mojo/system/message_in_transit.h" #include "mojo/system/message_in_transit.h"
#include "mojo/system/message_pipe.h"
#include "mojo/system/message_pipe_dispatcher.h" #include "mojo/system/message_pipe_dispatcher.h"
#include "mojo/system/platform_handle_dispatcher.h" #include "mojo/system/platform_handle_dispatcher.h"
#include "mojo/system/raw_channel.h" #include "mojo/system/raw_channel.h"
...@@ -42,7 +41,7 @@ namespace { ...@@ -42,7 +41,7 @@ namespace {
scoped_refptr<system::Channel> MakeChannel( scoped_refptr<system::Channel> MakeChannel(
system::Core* core, system::Core* core,
ScopedPlatformHandle platform_handle, ScopedPlatformHandle platform_handle,
scoped_refptr<system::MessagePipe> message_pipe) { scoped_refptr<system::ChannelEndpoint> channel_endpoint) {
DCHECK(platform_handle.is_valid()); DCHECK(platform_handle.is_valid());
// Create and initialize a |system::Channel|. // Create and initialize a |system::Channel|.
...@@ -58,9 +57,9 @@ scoped_refptr<system::Channel> MakeChannel( ...@@ -58,9 +57,9 @@ scoped_refptr<system::Channel> MakeChannel(
// Once |Init()| has succeeded, we have to return |channel| (since // Once |Init()| has succeeded, we have to return |channel| (since
// |Shutdown()| will have to be called on it). // |Shutdown()| will have to be called on it).
// Attach the message pipe endpoint. // Attach the endpoint.
system::MessageInTransit::EndpointId endpoint_id = channel->AttachEndpoint( system::MessageInTransit::EndpointId endpoint_id =
make_scoped_refptr(new system::ChannelEndpoint(message_pipe.get(), 1))); channel->AttachEndpoint(channel_endpoint);
if (endpoint_id == system::MessageInTransit::kInvalidEndpointId) { if (endpoint_id == system::MessageInTransit::kInvalidEndpointId) {
// This means that, e.g., the other endpoint of the message pipe was closed // This means that, e.g., the other endpoint of the message pipe was closed
// first. But it's not necessarily an error per se. // first. But it's not necessarily an error per se.
...@@ -83,11 +82,11 @@ void CreateChannelHelper( ...@@ -83,11 +82,11 @@ void CreateChannelHelper(
system::Core* core, system::Core* core,
ScopedPlatformHandle platform_handle, ScopedPlatformHandle platform_handle,
scoped_ptr<ChannelInfo> channel_info, scoped_ptr<ChannelInfo> channel_info,
scoped_refptr<system::MessagePipe> message_pipe, scoped_refptr<system::ChannelEndpoint> channel_endpoint,
DidCreateChannelCallback callback, DidCreateChannelCallback callback,
scoped_refptr<base::TaskRunner> callback_thread_task_runner) { scoped_refptr<base::TaskRunner> callback_thread_task_runner) {
channel_info->channel = channel_info->channel =
MakeChannel(core, platform_handle.Pass(), message_pipe); MakeChannel(core, platform_handle.Pass(), channel_endpoint);
// Hand the channel back to the embedder. // Hand the channel back to the embedder.
if (callback_thread_task_runner.get()) { if (callback_thread_task_runner.get()) {
...@@ -111,18 +110,18 @@ ScopedMessagePipeHandle CreateChannelOnIOThread( ...@@ -111,18 +110,18 @@ ScopedMessagePipeHandle CreateChannelOnIOThread(
DCHECK(platform_handle.is_valid()); DCHECK(platform_handle.is_valid());
DCHECK(channel_info); DCHECK(channel_info);
std::pair<scoped_refptr<system::MessagePipeDispatcher>, scoped_refptr<system::ChannelEndpoint> channel_endpoint;
scoped_refptr<system::MessagePipe> > remote_message_pipe = scoped_refptr<system::MessagePipeDispatcher> dispatcher =
system::MessagePipeDispatcher::CreateRemoteMessagePipe(); system::MessagePipeDispatcher::CreateRemoteMessagePipe(&channel_endpoint);
system::Core* core = system::entrypoints::GetCore(); system::Core* core = system::entrypoints::GetCore();
DCHECK(core); DCHECK(core);
ScopedMessagePipeHandle rv( ScopedMessagePipeHandle rv(
MessagePipeHandle(core->AddDispatcher(remote_message_pipe.first))); MessagePipeHandle(core->AddDispatcher(dispatcher)));
*channel_info = new ChannelInfo(); *channel_info = new ChannelInfo();
(*channel_info)->channel = (*channel_info)->channel =
MakeChannel(core, platform_handle.Pass(), remote_message_pipe.second); MakeChannel(core, platform_handle.Pass(), channel_endpoint);
return rv.Pass(); return rv.Pass();
} }
...@@ -134,14 +133,14 @@ ScopedMessagePipeHandle CreateChannel( ...@@ -134,14 +133,14 @@ ScopedMessagePipeHandle CreateChannel(
scoped_refptr<base::TaskRunner> callback_thread_task_runner) { scoped_refptr<base::TaskRunner> callback_thread_task_runner) {
DCHECK(platform_handle.is_valid()); DCHECK(platform_handle.is_valid());
std::pair<scoped_refptr<system::MessagePipeDispatcher>, scoped_refptr<system::ChannelEndpoint> channel_endpoint;
scoped_refptr<system::MessagePipe> > remote_message_pipe = scoped_refptr<system::MessagePipeDispatcher> dispatcher =
system::MessagePipeDispatcher::CreateRemoteMessagePipe(); system::MessagePipeDispatcher::CreateRemoteMessagePipe(&channel_endpoint);
system::Core* core = system::entrypoints::GetCore(); system::Core* core = system::entrypoints::GetCore();
DCHECK(core); DCHECK(core);
ScopedMessagePipeHandle rv( ScopedMessagePipeHandle rv(
MessagePipeHandle(core->AddDispatcher(remote_message_pipe.first))); MessagePipeHandle(core->AddDispatcher(dispatcher)));
scoped_ptr<ChannelInfo> channel_info(new ChannelInfo()); scoped_ptr<ChannelInfo> channel_info(new ChannelInfo());
channel_info->io_thread_task_runner = io_thread_task_runner; channel_info->io_thread_task_runner = io_thread_task_runner;
...@@ -152,7 +151,7 @@ ScopedMessagePipeHandle CreateChannel( ...@@ -152,7 +151,7 @@ ScopedMessagePipeHandle CreateChannel(
base::Unretained(core), base::Unretained(core),
base::Passed(&platform_handle), base::Passed(&platform_handle),
base::Passed(&channel_info), base::Passed(&channel_info),
remote_message_pipe.second, channel_endpoint,
callback, callback,
callback_thread_task_runner)); callback_thread_task_runner));
} else { } else {
......
...@@ -194,10 +194,12 @@ TEST_F(ChannelTest, CloseBeforeRun) { ...@@ -194,10 +194,12 @@ TEST_F(ChannelTest, CloseBeforeRun) {
base::Bind(&ChannelTest::InitChannelOnIOThread, base::Unretained(this))); base::Bind(&ChannelTest::InitChannelOnIOThread, base::Unretained(this)));
EXPECT_EQ(TRISTATE_TRUE, init_result()); EXPECT_EQ(TRISTATE_TRUE, init_result());
scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy()); scoped_refptr<ChannelEndpoint> channel_endpoint;
scoped_refptr<MessagePipe> mp(
MessagePipe::CreateLocalProxy(&channel_endpoint));
MessageInTransit::EndpointId local_id = channel()->AttachEndpoint( MessageInTransit::EndpointId local_id =
make_scoped_refptr(new ChannelEndpoint(mp.get(), 1))); channel()->AttachEndpoint(channel_endpoint);
EXPECT_EQ(Channel::kBootstrapEndpointId, local_id); EXPECT_EQ(Channel::kBootstrapEndpointId, local_id);
mp->Close(0); mp->Close(0);
...@@ -232,10 +234,12 @@ TEST_F(ChannelTest, ShutdownAfterAttach) { ...@@ -232,10 +234,12 @@ TEST_F(ChannelTest, ShutdownAfterAttach) {
base::Bind(&ChannelTest::InitChannelOnIOThread, base::Unretained(this))); base::Bind(&ChannelTest::InitChannelOnIOThread, base::Unretained(this)));
EXPECT_EQ(TRISTATE_TRUE, init_result()); EXPECT_EQ(TRISTATE_TRUE, init_result());
scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy()); scoped_refptr<ChannelEndpoint> channel_endpoint;
scoped_refptr<MessagePipe> mp(
MessagePipe::CreateLocalProxy(&channel_endpoint));
MessageInTransit::EndpointId local_id = channel()->AttachEndpoint( MessageInTransit::EndpointId local_id =
make_scoped_refptr(new ChannelEndpoint(mp.get(), 1))); channel()->AttachEndpoint(channel_endpoint);
EXPECT_EQ(Channel::kBootstrapEndpointId, local_id); EXPECT_EQ(Channel::kBootstrapEndpointId, local_id);
// TODO(vtl): Currently, we always "expect" a |RunMessagePipeEndpoint()| after // TODO(vtl): Currently, we always "expect" a |RunMessagePipeEndpoint()| after
...@@ -282,10 +286,12 @@ TEST_F(ChannelTest, WaitAfterAttachRunAndShutdown) { ...@@ -282,10 +286,12 @@ TEST_F(ChannelTest, WaitAfterAttachRunAndShutdown) {
base::Bind(&ChannelTest::InitChannelOnIOThread, base::Unretained(this))); base::Bind(&ChannelTest::InitChannelOnIOThread, base::Unretained(this)));
EXPECT_EQ(TRISTATE_TRUE, init_result()); EXPECT_EQ(TRISTATE_TRUE, init_result());
scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy()); scoped_refptr<ChannelEndpoint> channel_endpoint;
scoped_refptr<MessagePipe> mp(
MessagePipe::CreateLocalProxy(&channel_endpoint));
MessageInTransit::EndpointId local_id = channel()->AttachEndpoint( MessageInTransit::EndpointId local_id =
make_scoped_refptr(new ChannelEndpoint(mp.get(), 1))); channel()->AttachEndpoint(channel_endpoint);
EXPECT_EQ(Channel::kBootstrapEndpointId, local_id); EXPECT_EQ(Channel::kBootstrapEndpointId, local_id);
EXPECT_TRUE(channel()->RunMessagePipeEndpoint(local_id, EXPECT_TRUE(channel()->RunMessagePipeEndpoint(local_id,
......
...@@ -15,31 +15,36 @@ ...@@ -15,31 +15,36 @@
namespace mojo { namespace mojo {
namespace system { namespace system {
MessagePipe::MessagePipe(scoped_ptr<MessagePipeEndpoint> endpoint0,
scoped_ptr<MessagePipeEndpoint> endpoint1) {
endpoints_[0].reset(endpoint0.release());
endpoints_[1].reset(endpoint1.release());
}
// static // static
MessagePipe* MessagePipe::CreateLocalLocal() { MessagePipe* MessagePipe::CreateLocalLocal() {
return new MessagePipe( MessagePipe* message_pipe = new MessagePipe();
scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint), message_pipe->endpoints_[0].reset(new LocalMessagePipeEndpoint());
scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint)); message_pipe->endpoints_[1].reset(new LocalMessagePipeEndpoint());
return message_pipe;
} }
// static // static
MessagePipe* MessagePipe::CreateLocalProxy() { MessagePipe* MessagePipe::CreateLocalProxy(
return new MessagePipe( scoped_refptr<ChannelEndpoint>* channel_endpoint) {
scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint), DCHECK(!channel_endpoint->get()); // Not technically wrong, but unlikely.
scoped_ptr<MessagePipeEndpoint>(new ProxyMessagePipeEndpoint)); MessagePipe* message_pipe = new MessagePipe();
message_pipe->endpoints_[0].reset(new LocalMessagePipeEndpoint());
*channel_endpoint = new ChannelEndpoint(message_pipe, 1);
message_pipe->endpoints_[1].reset(
new ProxyMessagePipeEndpoint(channel_endpoint->get()));
return message_pipe;
} }
// static // static
MessagePipe* MessagePipe::CreateProxyLocal() { MessagePipe* MessagePipe::CreateProxyLocal(
return new MessagePipe( scoped_refptr<ChannelEndpoint>* channel_endpoint) {
scoped_ptr<MessagePipeEndpoint>(new ProxyMessagePipeEndpoint), DCHECK(!channel_endpoint->get()); // Not technically wrong, but unlikely.
scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint)); MessagePipe* message_pipe = new MessagePipe();
*channel_endpoint = new ChannelEndpoint(message_pipe, 0);
message_pipe->endpoints_[0].reset(
new ProxyMessagePipeEndpoint(channel_endpoint->get()));
message_pipe->endpoints_[1].reset(new LocalMessagePipeEndpoint());
return message_pipe;
} }
// static // static
...@@ -165,13 +170,16 @@ scoped_refptr<ChannelEndpoint> MessagePipe::ConvertLocalToProxy(unsigned port) { ...@@ -165,13 +170,16 @@ scoped_refptr<ChannelEndpoint> MessagePipe::ConvertLocalToProxy(unsigned port) {
<< "Direct message pipe passing across multiple channels not yet " << "Direct message pipe passing across multiple channels not yet "
"implemented; will proxy"; "implemented; will proxy";
scoped_refptr<ChannelEndpoint> channel_endpoint(
new ChannelEndpoint(this, port));
scoped_ptr<MessagePipeEndpoint> replacement_endpoint( scoped_ptr<MessagePipeEndpoint> replacement_endpoint(
new ProxyMessagePipeEndpoint( new ProxyMessagePipeEndpoint(
channel_endpoint.get(),
static_cast<LocalMessagePipeEndpoint*>(endpoints_[port].get()), static_cast<LocalMessagePipeEndpoint*>(endpoints_[port].get()),
is_peer_open)); is_peer_open));
endpoints_[port].swap(replacement_endpoint); endpoints_[port].swap(replacement_endpoint);
return make_scoped_refptr(new ChannelEndpoint(this, port)); return channel_endpoint;
} }
MojoResult MessagePipe::EnqueueMessage(unsigned port, MojoResult MessagePipe::EnqueueMessage(unsigned port,
...@@ -188,7 +196,6 @@ bool MessagePipe::Attach(unsigned port, ChannelEndpoint* channel_endpoint) { ...@@ -188,7 +196,6 @@ bool MessagePipe::Attach(unsigned port, ChannelEndpoint* channel_endpoint) {
return false; return false;
DCHECK_EQ(endpoints_[port]->GetType(), MessagePipeEndpoint::kTypeProxy); DCHECK_EQ(endpoints_[port]->GetType(), MessagePipeEndpoint::kTypeProxy);
endpoints_[port]->Attach(channel_endpoint);
return true; return true;
} }
...@@ -217,6 +224,9 @@ void MessagePipe::OnRemove(unsigned port) { ...@@ -217,6 +224,9 @@ void MessagePipe::OnRemove(unsigned port) {
endpoints_[port].reset(); endpoints_[port].reset();
} }
MessagePipe::MessagePipe() {
}
MessagePipe::~MessagePipe() { MessagePipe::~MessagePipe() {
// Owned by the dispatchers. The owning dispatchers should only release us via // Owned by the dispatchers. The owning dispatchers should only release us via
// their |Close()| method, which should inform us of being closed via our // their |Close()| method, which should inform us of being closed via our
......
...@@ -34,22 +34,23 @@ class Waiter; ...@@ -34,22 +34,23 @@ class Waiter;
class MOJO_SYSTEM_IMPL_EXPORT MessagePipe class MOJO_SYSTEM_IMPL_EXPORT MessagePipe
: public base::RefCountedThreadSafe<MessagePipe> { : public base::RefCountedThreadSafe<MessagePipe> {
public: public:
MessagePipe(scoped_ptr<MessagePipeEndpoint> endpoint0,
scoped_ptr<MessagePipeEndpoint> endpoint1);
// Creates a |MessagePipe| with two new |LocalMessagePipeEndpoint|s. // Creates a |MessagePipe| with two new |LocalMessagePipeEndpoint|s.
static MessagePipe* CreateLocalLocal(); static MessagePipe* CreateLocalLocal();
// Creates a |MessagePipe| with a |LocalMessagePipeEndpoint| on port 0 and a // Creates a |MessagePipe| with a |LocalMessagePipeEndpoint| on port 0 and a
// |ProxyMessagePipeEndpoint| on port 1. // |ProxyMessagePipeEndpoint| on port 1. |*channel_endpoint| is set to the
static MessagePipe* CreateLocalProxy(); // (newly-created) |ChannelEndpoint| for the latter.
static MessagePipe* CreateLocalProxy(
scoped_refptr<ChannelEndpoint>* channel_endpoint);
// Creates a |MessagePipe| with a |ProxyMessagePipeEndpoint| on port 0 and a // Creates a |MessagePipe| with a |ProxyMessagePipeEndpoint| on port 0 and a
// |LocalMessagePipeEndpoint| on port 1. // |LocalMessagePipeEndpoint| on port 1. |*channel_endpoint| is set to the
// (newly-created) |ChannelEndpoint| for the former.
// Note: This is really only needed in tests (outside of tests, this // Note: This is really only needed in tests (outside of tests, this
// configuration arises from a local message pipe having its port 0 // configuration arises from a local message pipe having its port 0
// "converted" using |ConvertLocalToProxy()|). // "converted" using |ConvertLocalToProxy()|).
static MessagePipe* CreateProxyLocal(); static MessagePipe* CreateProxyLocal(
scoped_refptr<ChannelEndpoint>* channel_endpoint);
// Gets the other port number (i.e., 0 -> 1, 1 -> 0). // Gets the other port number (i.e., 0 -> 1, 1 -> 0).
static unsigned GetPeerPort(unsigned port); static unsigned GetPeerPort(unsigned port);
...@@ -95,11 +96,14 @@ class MOJO_SYSTEM_IMPL_EXPORT MessagePipe ...@@ -95,11 +96,14 @@ class MOJO_SYSTEM_IMPL_EXPORT MessagePipe
scoped_ptr<MessageInTransit> message); scoped_ptr<MessageInTransit> message);
// These are used by |Channel|. // These are used by |Channel|.
// TODO(vtl): Remove |Attach()|.
bool Attach(unsigned port, ChannelEndpoint* channel_endpoint); bool Attach(unsigned port, ChannelEndpoint* channel_endpoint);
void Run(unsigned port); void Run(unsigned port);
void OnRemove(unsigned port); void OnRemove(unsigned port);
private: private:
MessagePipe();
friend class base::RefCountedThreadSafe<MessagePipe>; friend class base::RefCountedThreadSafe<MessagePipe>;
virtual ~MessagePipe(); virtual ~MessagePipe();
......
...@@ -83,14 +83,15 @@ Dispatcher::Type MessagePipeDispatcher::GetType() const { ...@@ -83,14 +83,15 @@ Dispatcher::Type MessagePipeDispatcher::GetType() const {
} }
// static // static
std::pair<scoped_refptr<MessagePipeDispatcher>, scoped_refptr<MessagePipe> > scoped_refptr<MessagePipeDispatcher>
MessagePipeDispatcher::CreateRemoteMessagePipe() { MessagePipeDispatcher::CreateRemoteMessagePipe(
scoped_refptr<MessagePipe> message_pipe(MessagePipe::CreateLocalProxy()); scoped_refptr<ChannelEndpoint>* channel_endpoint) {
scoped_refptr<MessagePipe> message_pipe(
MessagePipe::CreateLocalProxy(channel_endpoint));
scoped_refptr<MessagePipeDispatcher> dispatcher( scoped_refptr<MessagePipeDispatcher> dispatcher(
new MessagePipeDispatcher(MessagePipeDispatcher::kDefaultCreateOptions)); new MessagePipeDispatcher(MessagePipeDispatcher::kDefaultCreateOptions));
dispatcher->Init(message_pipe, 0); dispatcher->Init(message_pipe, 0);
return dispatcher;
return std::make_pair(dispatcher, message_pipe);
} }
// static // static
...@@ -103,8 +104,9 @@ scoped_refptr<MessagePipeDispatcher> MessagePipeDispatcher::Deserialize( ...@@ -103,8 +104,9 @@ scoped_refptr<MessagePipeDispatcher> MessagePipeDispatcher::Deserialize(
return scoped_refptr<MessagePipeDispatcher>(); return scoped_refptr<MessagePipeDispatcher>();
} }
std::pair<scoped_refptr<MessagePipeDispatcher>, scoped_refptr<MessagePipe> > scoped_refptr<ChannelEndpoint> channel_endpoint;
remote_message_pipe = CreateRemoteMessagePipe(); scoped_refptr<MessagePipeDispatcher> dispatcher =
CreateRemoteMessagePipe(&channel_endpoint);
MessageInTransit::EndpointId remote_id = MessageInTransit::EndpointId remote_id =
static_cast<const SerializedMessagePipeDispatcher*>(source)->endpoint_id; static_cast<const SerializedMessagePipeDispatcher*>(source)->endpoint_id;
...@@ -117,8 +119,7 @@ scoped_refptr<MessagePipeDispatcher> MessagePipeDispatcher::Deserialize( ...@@ -117,8 +119,7 @@ scoped_refptr<MessagePipeDispatcher> MessagePipeDispatcher::Deserialize(
return scoped_refptr<MessagePipeDispatcher>(); return scoped_refptr<MessagePipeDispatcher>();
} }
MessageInTransit::EndpointId local_id = MessageInTransit::EndpointId local_id =
channel->AttachEndpoint(make_scoped_refptr( channel->AttachEndpoint(channel_endpoint);
new ChannelEndpoint(remote_message_pipe.second.get(), 1)));
if (local_id == MessageInTransit::kInvalidEndpointId) { if (local_id == MessageInTransit::kInvalidEndpointId) {
LOG(ERROR) << "Failed to deserialize message pipe dispatcher (failed to " LOG(ERROR) << "Failed to deserialize message pipe dispatcher (failed to "
"attach; remote ID = " << remote_id << ")"; "attach; remote ID = " << remote_id << ")";
...@@ -135,7 +136,7 @@ scoped_refptr<MessagePipeDispatcher> MessagePipeDispatcher::Deserialize( ...@@ -135,7 +136,7 @@ scoped_refptr<MessagePipeDispatcher> MessagePipeDispatcher::Deserialize(
// TODO(vtl): FIXME -- Need some error handling here. // TODO(vtl): FIXME -- Need some error handling here.
channel->RunRemoteMessagePipeEndpoint(local_id, remote_id); channel->RunRemoteMessagePipeEndpoint(local_id, remote_id);
return remote_message_pipe.first; return dispatcher;
} }
MessagePipeDispatcher::~MessagePipeDispatcher() { MessagePipeDispatcher::~MessagePipeDispatcher() {
......
...@@ -5,8 +5,6 @@ ...@@ -5,8 +5,6 @@
#ifndef MOJO_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_ #ifndef MOJO_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_
#define MOJO_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_ #define MOJO_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_
#include <utility>
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "mojo/system/dispatcher.h" #include "mojo/system/dispatcher.h"
...@@ -16,6 +14,7 @@ ...@@ -16,6 +14,7 @@
namespace mojo { namespace mojo {
namespace system { namespace system {
class ChannelEndpoint;
class MessagePipe; class MessagePipe;
class MessagePipeDispatcherTransport; class MessagePipeDispatcherTransport;
...@@ -51,9 +50,8 @@ class MOJO_SYSTEM_IMPL_EXPORT MessagePipeDispatcher : public Dispatcher { ...@@ -51,9 +50,8 @@ class MOJO_SYSTEM_IMPL_EXPORT MessagePipeDispatcher : public Dispatcher {
// the message pipe, port 0). // the message pipe, port 0).
// TODO(vtl): This currently uses |kDefaultCreateOptions|, which is okay since // TODO(vtl): This currently uses |kDefaultCreateOptions|, which is okay since
// there aren't any options, but eventually options should be plumbed through. // there aren't any options, but eventually options should be plumbed through.
static std::pair<scoped_refptr<MessagePipeDispatcher>, static scoped_refptr<MessagePipeDispatcher> CreateRemoteMessagePipe(
scoped_refptr<MessagePipe> > scoped_refptr<ChannelEndpoint>* channel_endpoint);
CreateRemoteMessagePipe();
// The "opposite" of |SerializeAndClose()|. (Typically this is called by // The "opposite" of |SerializeAndClose()|. (Typically this is called by
// |Dispatcher::Deserialize()|.) // |Dispatcher::Deserialize()|.)
......
...@@ -110,10 +110,9 @@ MOJO_MULTIPROCESS_TEST_CHILD_MAIN(PingPongClient) { ...@@ -110,10 +110,9 @@ MOJO_MULTIPROCESS_TEST_CHILD_MAIN(PingPongClient) {
embedder::ScopedPlatformHandle client_platform_handle = embedder::ScopedPlatformHandle client_platform_handle =
mojo::test::MultiprocessTestHelper::client_platform_handle.Pass(); mojo::test::MultiprocessTestHelper::client_platform_handle.Pass();
CHECK(client_platform_handle.is_valid()); CHECK(client_platform_handle.is_valid());
scoped_refptr<MessagePipe> mp(new MessagePipe( scoped_refptr<ChannelEndpoint> ep;
scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint()), scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep));
scoped_ptr<MessagePipeEndpoint>(new ProxyMessagePipeEndpoint()))); channel_thread.Start(client_platform_handle.Pass(), ep);
channel_thread.Start(client_platform_handle.Pass(), mp);
std::string buffer(1000000, '\0'); std::string buffer(1000000, '\0');
int rv = 0; int rv = 0;
...@@ -158,10 +157,9 @@ MOJO_MULTIPROCESS_TEST_CHILD_MAIN(PingPongClient) { ...@@ -158,10 +157,9 @@ MOJO_MULTIPROCESS_TEST_CHILD_MAIN(PingPongClient) {
TEST_F(MultiprocessMessagePipePerfTest, PingPong) { TEST_F(MultiprocessMessagePipePerfTest, PingPong) {
helper()->StartChild("PingPongClient"); helper()->StartChild("PingPongClient");
scoped_refptr<MessagePipe> mp(new MessagePipe( scoped_refptr<ChannelEndpoint> ep;
scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint()), scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep));
scoped_ptr<MessagePipeEndpoint>(new ProxyMessagePipeEndpoint()))); Init(ep);
Init(mp);
// This values are set to align with one at ipc_pertests.cc for comparison. // This values are set to align with one at ipc_pertests.cc for comparison.
const size_t kMsgSize[5] = {12, 144, 1728, 20736, 248832}; const size_t kMsgSize[5] = {12, 144, 1728, 20736, 248832};
......
...@@ -6,7 +6,9 @@ ...@@ -6,7 +6,9 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/threading/platform_thread.h" // For |Sleep()|. #include "base/threading/platform_thread.h" // For |Sleep()|.
#include "mojo/system/channel.h"
#include "mojo/system/channel_endpoint.h" #include "mojo/system/channel_endpoint.h"
#include "mojo/system/message_pipe.h"
#include "mojo/system/waiter.h" #include "mojo/system/waiter.h"
namespace mojo { namespace mojo {
...@@ -40,14 +42,14 @@ ChannelThread::~ChannelThread() { ...@@ -40,14 +42,14 @@ ChannelThread::~ChannelThread() {
} }
void ChannelThread::Start(embedder::ScopedPlatformHandle platform_handle, void ChannelThread::Start(embedder::ScopedPlatformHandle platform_handle,
scoped_refptr<MessagePipe> message_pipe) { scoped_refptr<ChannelEndpoint> channel_endpoint) {
test_io_thread_.Start(); test_io_thread_.Start();
test_io_thread_.PostTaskAndWait( test_io_thread_.PostTaskAndWait(
FROM_HERE, FROM_HERE,
base::Bind(&ChannelThread::InitChannelOnIOThread, base::Bind(&ChannelThread::InitChannelOnIOThread,
base::Unretained(this), base::Unretained(this),
base::Passed(&platform_handle), base::Passed(&platform_handle),
message_pipe)); channel_endpoint));
} }
void ChannelThread::Stop() { void ChannelThread::Stop() {
...@@ -68,7 +70,7 @@ void ChannelThread::Stop() { ...@@ -68,7 +70,7 @@ void ChannelThread::Stop() {
void ChannelThread::InitChannelOnIOThread( void ChannelThread::InitChannelOnIOThread(
embedder::ScopedPlatformHandle platform_handle, embedder::ScopedPlatformHandle platform_handle,
scoped_refptr<MessagePipe> message_pipe) { scoped_refptr<ChannelEndpoint> channel_endpoint) {
CHECK_EQ(base::MessageLoop::current(), test_io_thread_.message_loop()); CHECK_EQ(base::MessageLoop::current(), test_io_thread_.message_loop());
CHECK(platform_handle.is_valid()); CHECK(platform_handle.is_valid());
...@@ -83,8 +85,7 @@ void ChannelThread::InitChannelOnIOThread( ...@@ -83,8 +85,7 @@ void ChannelThread::InitChannelOnIOThread(
// receive/process messages (which it can do as soon as it's hooked up to // receive/process messages (which it can do as soon as it's hooked up to
// the IO thread message loop, and that message loop runs) before the // the IO thread message loop, and that message loop runs) before the
// message pipe endpoint is attached. // message pipe endpoint is attached.
CHECK_EQ(channel_->AttachEndpoint( CHECK_EQ(channel_->AttachEndpoint(channel_endpoint),
make_scoped_refptr(new ChannelEndpoint(message_pipe.get(), 1))),
Channel::kBootstrapEndpointId); Channel::kBootstrapEndpointId);
CHECK(channel_->RunMessagePipeEndpoint(Channel::kBootstrapEndpointId, CHECK(channel_->RunMessagePipeEndpoint(Channel::kBootstrapEndpointId,
Channel::kBootstrapEndpointId)); Channel::kBootstrapEndpointId));
...@@ -104,8 +105,8 @@ MultiprocessMessagePipeTestBase::MultiprocessMessagePipeTestBase() ...@@ -104,8 +105,8 @@ MultiprocessMessagePipeTestBase::MultiprocessMessagePipeTestBase()
MultiprocessMessagePipeTestBase::~MultiprocessMessagePipeTestBase() { MultiprocessMessagePipeTestBase::~MultiprocessMessagePipeTestBase() {
} }
void MultiprocessMessagePipeTestBase::Init(scoped_refptr<MessagePipe> mp) { void MultiprocessMessagePipeTestBase::Init(scoped_refptr<ChannelEndpoint> ep) {
channel_thread_.Start(helper_.server_platform_handle.Pass(), mp); channel_thread_.Start(helper_.server_platform_handle.Pass(), ep);
} }
#endif #endif
......
...@@ -9,11 +9,15 @@ ...@@ -9,11 +9,15 @@
#include "mojo/common/test/multiprocess_test_helper.h" #include "mojo/common/test/multiprocess_test_helper.h"
#include "mojo/embedder/simple_platform_support.h" #include "mojo/embedder/simple_platform_support.h"
#include "mojo/system/channel.h" #include "mojo/system/channel.h"
#include "mojo/system/message_pipe.h"
#include "mojo/system/test_utils.h" #include "mojo/system/test_utils.h"
namespace mojo { namespace mojo {
namespace system { namespace system {
class Channel;
class ChannelEndpoint;
class MessagePipe;
namespace test { namespace test {
MojoResult WaitIfNecessary(scoped_refptr<MessagePipe> mp, MojoResult WaitIfNecessary(scoped_refptr<MessagePipe> mp,
...@@ -26,12 +30,12 @@ class ChannelThread { ...@@ -26,12 +30,12 @@ class ChannelThread {
~ChannelThread(); ~ChannelThread();
void Start(embedder::ScopedPlatformHandle platform_handle, void Start(embedder::ScopedPlatformHandle platform_handle,
scoped_refptr<MessagePipe> message_pipe); scoped_refptr<ChannelEndpoint> channel_endpoint);
void Stop(); void Stop();
private: private:
void InitChannelOnIOThread(embedder::ScopedPlatformHandle platform_handle, void InitChannelOnIOThread(embedder::ScopedPlatformHandle platform_handle,
scoped_refptr<MessagePipe> message_pipe); scoped_refptr<ChannelEndpoint> channel_endpoint);
void ShutdownChannelOnIOThread(); void ShutdownChannelOnIOThread();
embedder::PlatformSupport* const platform_support_; embedder::PlatformSupport* const platform_support_;
...@@ -48,7 +52,7 @@ class MultiprocessMessagePipeTestBase : public testing::Test { ...@@ -48,7 +52,7 @@ class MultiprocessMessagePipeTestBase : public testing::Test {
virtual ~MultiprocessMessagePipeTestBase(); virtual ~MultiprocessMessagePipeTestBase();
protected: protected:
void Init(scoped_refptr<MessagePipe> mp); void Init(scoped_refptr<ChannelEndpoint> ep);
embedder::PlatformSupport* platform_support() { return &platform_support_; } embedder::PlatformSupport* platform_support() { return &platform_support_; }
mojo::test::MultiprocessTestHelper* helper() { return &helper_; } mojo::test::MultiprocessTestHelper* helper() { return &helper_; }
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
#include "base/location.h" #include "base/location.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/macros.h" #include "base/macros.h"
#include "build/build_config.h" // TODO(vtl): Remove this. #include "build/build_config.h" // TODO(vtl): Remove this.
#include "mojo/common/test/test_utils.h" #include "mojo/common/test/test_utils.h"
#include "mojo/embedder/platform_shared_buffer.h" #include "mojo/embedder/platform_shared_buffer.h"
#include "mojo/embedder/scoped_platform_handle.h" #include "mojo/embedder/scoped_platform_handle.h"
...@@ -48,8 +48,9 @@ MOJO_MULTIPROCESS_TEST_CHILD_MAIN(EchoEcho) { ...@@ -48,8 +48,9 @@ MOJO_MULTIPROCESS_TEST_CHILD_MAIN(EchoEcho) {
embedder::ScopedPlatformHandle client_platform_handle = embedder::ScopedPlatformHandle client_platform_handle =
mojo::test::MultiprocessTestHelper::client_platform_handle.Pass(); mojo::test::MultiprocessTestHelper::client_platform_handle.Pass();
CHECK(client_platform_handle.is_valid()); CHECK(client_platform_handle.is_valid());
scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy()); scoped_refptr<ChannelEndpoint> ep;
channel_thread.Start(client_platform_handle.Pass(), mp); scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep));
channel_thread.Start(client_platform_handle.Pass(), ep);
const std::string quitquitquit("quitquitquit"); const std::string quitquitquit("quitquitquit");
int rv = 0; int rv = 0;
...@@ -103,8 +104,9 @@ MOJO_MULTIPROCESS_TEST_CHILD_MAIN(EchoEcho) { ...@@ -103,8 +104,9 @@ MOJO_MULTIPROCESS_TEST_CHILD_MAIN(EchoEcho) {
TEST_F(MultiprocessMessagePipeTest, Basic) { TEST_F(MultiprocessMessagePipeTest, Basic) {
helper()->StartChild("EchoEcho"); helper()->StartChild("EchoEcho");
scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy()); scoped_refptr<ChannelEndpoint> ep;
Init(mp); scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep));
Init(ep);
std::string hello("hello"); std::string hello("hello");
EXPECT_EQ(MOJO_RESULT_OK, EXPECT_EQ(MOJO_RESULT_OK,
...@@ -147,8 +149,9 @@ TEST_F(MultiprocessMessagePipeTest, Basic) { ...@@ -147,8 +149,9 @@ TEST_F(MultiprocessMessagePipeTest, Basic) {
TEST_F(MultiprocessMessagePipeTest, QueueMessages) { TEST_F(MultiprocessMessagePipeTest, QueueMessages) {
helper()->StartChild("EchoEcho"); helper()->StartChild("EchoEcho");
scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy()); scoped_refptr<ChannelEndpoint> ep;
Init(mp); scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep));
Init(ep);
static const size_t kNumMessages = 1001; static const size_t kNumMessages = 1001;
for (size_t i = 0; i < kNumMessages; i++) { for (size_t i = 0; i < kNumMessages; i++) {
...@@ -213,8 +216,9 @@ MOJO_MULTIPROCESS_TEST_CHILD_MAIN(CheckSharedBuffer) { ...@@ -213,8 +216,9 @@ MOJO_MULTIPROCESS_TEST_CHILD_MAIN(CheckSharedBuffer) {
embedder::ScopedPlatformHandle client_platform_handle = embedder::ScopedPlatformHandle client_platform_handle =
mojo::test::MultiprocessTestHelper::client_platform_handle.Pass(); mojo::test::MultiprocessTestHelper::client_platform_handle.Pass();
CHECK(client_platform_handle.is_valid()); CHECK(client_platform_handle.is_valid());
scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy()); scoped_refptr<ChannelEndpoint> ep;
channel_thread.Start(client_platform_handle.Pass(), mp); scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep));
channel_thread.Start(client_platform_handle.Pass(), ep);
// Wait for the first message from our parent. // Wait for the first message from our parent.
HandleSignalsState hss; HandleSignalsState hss;
...@@ -312,8 +316,9 @@ MOJO_MULTIPROCESS_TEST_CHILD_MAIN(CheckSharedBuffer) { ...@@ -312,8 +316,9 @@ MOJO_MULTIPROCESS_TEST_CHILD_MAIN(CheckSharedBuffer) {
TEST_F(MultiprocessMessagePipeTest, MAYBE_SharedBufferPassing) { TEST_F(MultiprocessMessagePipeTest, MAYBE_SharedBufferPassing) {
helper()->StartChild("CheckSharedBuffer"); helper()->StartChild("CheckSharedBuffer");
scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy()); scoped_refptr<ChannelEndpoint> ep;
Init(mp); scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep));
Init(ep);
// Make a shared buffer. // Make a shared buffer.
scoped_refptr<SharedBufferDispatcher> dispatcher; scoped_refptr<SharedBufferDispatcher> dispatcher;
...@@ -407,8 +412,9 @@ MOJO_MULTIPROCESS_TEST_CHILD_MAIN(CheckPlatformHandleFile) { ...@@ -407,8 +412,9 @@ MOJO_MULTIPROCESS_TEST_CHILD_MAIN(CheckPlatformHandleFile) {
embedder::ScopedPlatformHandle client_platform_handle = embedder::ScopedPlatformHandle client_platform_handle =
mojo::test::MultiprocessTestHelper::client_platform_handle.Pass(); mojo::test::MultiprocessTestHelper::client_platform_handle.Pass();
CHECK(client_platform_handle.is_valid()); CHECK(client_platform_handle.is_valid());
scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy()); scoped_refptr<ChannelEndpoint> ep;
channel_thread.Start(client_platform_handle.Pass(), mp); scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep));
channel_thread.Start(client_platform_handle.Pass(), ep);
HandleSignalsState hss; HandleSignalsState hss;
CHECK_EQ(test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss), CHECK_EQ(test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss),
...@@ -465,8 +471,9 @@ TEST_F(MultiprocessMessagePipeTest, MAYBE_PlatformHandlePassing) { ...@@ -465,8 +471,9 @@ TEST_F(MultiprocessMessagePipeTest, MAYBE_PlatformHandlePassing) {
helper()->StartChild("CheckPlatformHandleFile"); helper()->StartChild("CheckPlatformHandleFile");
scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy()); scoped_refptr<ChannelEndpoint> ep;
Init(mp); scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep));
Init(ep);
base::FilePath unused; base::FilePath unused;
base::ScopedFILE fp( base::ScopedFILE fp(
......
...@@ -14,14 +14,20 @@ ...@@ -14,14 +14,20 @@
namespace mojo { namespace mojo {
namespace system { namespace system {
ProxyMessagePipeEndpoint::ProxyMessagePipeEndpoint() ProxyMessagePipeEndpoint::ProxyMessagePipeEndpoint(
: is_running_(false), is_peer_open_(true) { ChannelEndpoint* channel_endpoint)
: channel_endpoint_(channel_endpoint),
is_running_(false),
is_peer_open_(true) {
} }
ProxyMessagePipeEndpoint::ProxyMessagePipeEndpoint( ProxyMessagePipeEndpoint::ProxyMessagePipeEndpoint(
ChannelEndpoint* channel_endpoint,
LocalMessagePipeEndpoint* local_message_pipe_endpoint, LocalMessagePipeEndpoint* local_message_pipe_endpoint,
bool is_peer_open) bool is_peer_open)
: is_running_(false), is_peer_open_(is_peer_open) { : channel_endpoint_(channel_endpoint),
is_running_(false),
is_peer_open_(is_peer_open) {
paused_message_queue_.Swap(local_message_pipe_endpoint->message_queue()); paused_message_queue_.Swap(local_message_pipe_endpoint->message_queue());
local_message_pipe_endpoint->Close(); local_message_pipe_endpoint->Close();
} }
...@@ -72,12 +78,6 @@ void ProxyMessagePipeEndpoint::EnqueueMessage( ...@@ -72,12 +78,6 @@ void ProxyMessagePipeEndpoint::EnqueueMessage(
} }
} }
void ProxyMessagePipeEndpoint::Attach(ChannelEndpoint* channel_endpoint) {
DCHECK(channel_endpoint);
DCHECK(!is_attached());
channel_endpoint_ = channel_endpoint;
}
bool ProxyMessagePipeEndpoint::Run() { bool ProxyMessagePipeEndpoint::Run() {
// Assertions about current state: // Assertions about current state:
DCHECK(is_attached()); DCHECK(is_attached());
......
...@@ -37,12 +37,13 @@ class MessagePipe; ...@@ -37,12 +37,13 @@ class MessagePipe;
class MOJO_SYSTEM_IMPL_EXPORT ProxyMessagePipeEndpoint class MOJO_SYSTEM_IMPL_EXPORT ProxyMessagePipeEndpoint
: public MessagePipeEndpoint { : public MessagePipeEndpoint {
public: public:
ProxyMessagePipeEndpoint(); explicit ProxyMessagePipeEndpoint(ChannelEndpoint* channel_endpoint);
// Constructs a |ProxyMessagePipeEndpoint| that replaces the given // Constructs a |ProxyMessagePipeEndpoint| that replaces the given
// |LocalMessagePipeEndpoint| (which this constructor will close), taking its // |LocalMessagePipeEndpoint| (which this constructor will close), taking its
// message queue's contents. This is done when transferring a message pipe // message queue's contents. This is done when transferring a message pipe
// handle over a remote message pipe. // handle over a remote message pipe.
ProxyMessagePipeEndpoint( ProxyMessagePipeEndpoint(
ChannelEndpoint* channel_endpoint,
LocalMessagePipeEndpoint* local_message_pipe_endpoint, LocalMessagePipeEndpoint* local_message_pipe_endpoint,
bool is_peer_open); bool is_peer_open);
virtual ~ProxyMessagePipeEndpoint(); virtual ~ProxyMessagePipeEndpoint();
...@@ -51,7 +52,6 @@ class MOJO_SYSTEM_IMPL_EXPORT ProxyMessagePipeEndpoint ...@@ -51,7 +52,6 @@ class MOJO_SYSTEM_IMPL_EXPORT ProxyMessagePipeEndpoint
virtual Type GetType() const OVERRIDE; virtual Type GetType() const OVERRIDE;
virtual bool OnPeerClose() OVERRIDE; virtual bool OnPeerClose() OVERRIDE;
virtual void EnqueueMessage(scoped_ptr<MessageInTransit> message) OVERRIDE; virtual void EnqueueMessage(scoped_ptr<MessageInTransit> message) OVERRIDE;
virtual void Attach(ChannelEndpoint* channel_endpoint) OVERRIDE;
virtual bool Run() OVERRIDE; virtual bool Run() OVERRIDE;
virtual void OnRemove() OVERRIDE; virtual void OnRemove() OVERRIDE;
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment