Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2794)

Unified Diff: ipc/mojo/ipc_channel_mojo.cc

Issue 1350023003: Add a Mojo EDK for Chrome that uses one OS pipe per message pipe. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ipc/mojo/ipc_channel_mojo.cc
diff --git a/ipc/mojo/ipc_channel_mojo.cc b/ipc/mojo/ipc_channel_mojo.cc
index ff20719dd7d7ff41bb8939aa297af23bca759968..1ddc7aecea686fcf9c9f7ef3074c6d7f80d54648 100644
--- a/ipc/mojo/ipc_channel_mojo.cc
+++ b/ipc/mojo/ipc_channel_mojo.cc
@@ -15,13 +15,22 @@
#include "ipc/mojo/client_channel.mojom.h"
#include "ipc/mojo/ipc_mojo_bootstrap.h"
#include "ipc/mojo/ipc_mojo_handle_attachment.h"
-#include "third_party/mojo/src/mojo/edk/embedder/embedder.h"
#include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
+#if defined(USE_CHROME_EDK)
+#include "mojo/edk/embedder/embedder.h"
+#else
+#include "third_party/mojo/src/mojo/edk/embedder/embedder.h"
+#endif
+
#if defined(OS_POSIX) && !defined(OS_NACL)
#include "ipc/ipc_platform_file_attachment_posix.h"
#endif
+// TODO(jam): do more tests on using channel on same thread if it supports it (
+// i.e. with USE_CHROME_EDK and Windows). Also see message_pipe_dispatcher.cc
+bool g_use_channel_on_io = true;
+
namespace IPC {
namespace {
@@ -55,66 +64,63 @@ class MojoChannelFactory : public ChannelFactory {
//------------------------------------------------------------------------------
-class ClientChannelMojo : public ChannelMojo, public ClientChannel {
+class ClientChannelMojo
+ : public ChannelMojo
+#if !defined(USE_CHROME_EDK)
+ , public ClientChannel
+#endif
+{
public:
ClientChannelMojo(scoped_refptr<base::TaskRunner> io_runner,
const ChannelHandle& handle,
Listener* listener,
- AttachmentBroker* broker);
- ~ClientChannelMojo() override;
+ AttachmentBroker* broker)
+ : ChannelMojo(io_runner, handle, Channel::MODE_CLIENT, listener, broker),
+#if !defined(USE_CHROME_EDK)
+ binding_(this),
+#endif
+ weak_factory_(this) {
+ }
+ ~ClientChannelMojo() override {}
// MojoBootstrap::Delegate implementation
- void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override;
+ void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle,
+ int32 peer_pid) override {
+#if defined(USE_CHROME_EDK)
+ InitMessageReader(
+ mojo::embedder::CreateMessagePipe(handle.Pass()), peer_pid);
+#else
+ CreateMessagingPipe(handle.Pass(), base::Bind(&ClientChannelMojo::BindPipe,
+ weak_factory_.GetWeakPtr()));
+#endif
+ }
+#if !defined(USE_CHROME_EDK)
// ClientChannel implementation
void Init(
mojo::ScopedMessagePipeHandle pipe,
int32_t peer_pid,
- const mojo::Callback<void(int32_t)>& callback) override;
+ const mojo::Callback<void(int32_t)>& callback) override {
+ InitMessageReader(pipe.Pass(), static_cast<base::ProcessId>(peer_pid));
+ callback.Run(GetSelfPID());
+ }
+#endif
private:
- void BindPipe(mojo::ScopedMessagePipeHandle handle);
- void OnConnectionError();
+#if !defined(USE_CHROME_EDK)
+ void BindPipe(mojo::ScopedMessagePipeHandle handle) {
+ // binding_.Bind(handle.Pass());
+ }
+ void OnConnectionError() {
+ listener()->OnChannelError();
+ }
mojo::Binding<ClientChannel> binding_;
+#endif
base::WeakPtrFactory<ClientChannelMojo> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(ClientChannelMojo);
};
-ClientChannelMojo::ClientChannelMojo(scoped_refptr<base::TaskRunner> io_runner,
- const ChannelHandle& handle,
- Listener* listener,
- AttachmentBroker* broker)
- : ChannelMojo(io_runner, handle, Channel::MODE_CLIENT, listener, broker),
- binding_(this),
- weak_factory_(this) {
-}
-
-ClientChannelMojo::~ClientChannelMojo() {
-}
-
-void ClientChannelMojo::OnPipeAvailable(
- mojo::embedder::ScopedPlatformHandle handle) {
- CreateMessagingPipe(handle.Pass(), base::Bind(&ClientChannelMojo::BindPipe,
- weak_factory_.GetWeakPtr()));
-}
-
-void ClientChannelMojo::Init(
- mojo::ScopedMessagePipeHandle pipe,
- int32_t peer_pid,
- const mojo::Callback<void(int32_t)>& callback) {
- InitMessageReader(pipe.Pass(), static_cast<base::ProcessId>(peer_pid));
- callback.Run(GetSelfPID());
-}
-
-void ClientChannelMojo::BindPipe(mojo::ScopedMessagePipeHandle handle) {
- binding_.Bind(handle.Pass());
-}
-
-void ClientChannelMojo::OnConnectionError() {
- listener()->OnChannelError();
-}
-
//------------------------------------------------------------------------------
class ServerChannelMojo : public ChannelMojo {
@@ -122,84 +128,81 @@ class ServerChannelMojo : public ChannelMojo {
ServerChannelMojo(scoped_refptr<base::TaskRunner> io_runner,
const ChannelHandle& handle,
Listener* listener,
- AttachmentBroker* broker);
- ~ServerChannelMojo() override;
+ AttachmentBroker* broker)
+ : ChannelMojo(io_runner, handle, Channel::MODE_SERVER, listener, broker),
+ weak_factory_(this) {
+ }
+ ~ServerChannelMojo() override {
+ Close();
+ }
// MojoBootstrap::Delegate implementation
- void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override;
+ void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle,
+ int32 peer_pid) override {
+#if defined(USE_CHROME_EDK)
+ message_pipe_ = mojo::embedder::CreateMessagePipe(handle.Pass());
+ if (!message_pipe_.is_valid()) {
+ LOG(WARNING) << "mojo::CreateMessagePipe failed: ";
+ listener()->OnChannelError();
+ return;
+ }
+ InitMessageReader(message_pipe_.Pass(), peer_pid);
+#else
+ mojo::ScopedMessagePipeHandle peer;
+ MojoResult create_result =
+ mojo::CreateMessagePipe(nullptr, &message_pipe_, &peer);
+ if (create_result != MOJO_RESULT_OK) {
+ LOG(WARNING) << "mojo::CreateMessagePipe failed: " << create_result;
+ listener()->OnChannelError();
+ return;
+ }
+ CreateMessagingPipe(
+ handle.Pass(),
+ base::Bind(&ServerChannelMojo::InitClientChannel,
+ weak_factory_.GetWeakPtr(), base::Passed(&peer)));
+#endif
+ }
// Channel override
- void Close() override;
+ void Close() override {
+#if !defined(USE_CHROME_EDK)
+ client_channel_.reset();
+#endif
+ message_pipe_.reset();
+ ChannelMojo::Close();
+ }
private:
+#if !defined(USE_CHROME_EDK)
void InitClientChannel(mojo::ScopedMessagePipeHandle peer_handle,
- mojo::ScopedMessagePipeHandle handle);
- void OnConnectionError();
+ mojo::ScopedMessagePipeHandle handle) {
+ client_channel_.Bind(
+ mojo::InterfacePtrInfo<ClientChannel>(handle.Pass(), 0u));
+ client_channel_.set_connection_error_handler(base::Bind(
+ &ServerChannelMojo::OnConnectionError, base::Unretained(this)));
+ client_channel_->Init(
+ peer_handle.Pass(), static_cast<int32_t>(GetSelfPID()),
+ base::Bind(&ServerChannelMojo::ClientChannelWasInitialized,
+ base::Unretained(this)));
+ }
+#endif
+ void OnConnectionError() {
+ listener()->OnChannelError();
+ }
+#if !defined(USE_CHROME_EDK)
// ClientChannelClient implementation
- void ClientChannelWasInitialized(int32_t peer_pid);
+ void ClientChannelWasInitialized(int32_t peer_pid) {
+ InitMessageReader(message_pipe_.Pass(), peer_pid);
+ }
mojo::InterfacePtr<ClientChannel> client_channel_;
+#endif
mojo::ScopedMessagePipeHandle message_pipe_;
base::WeakPtrFactory<ServerChannelMojo> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(ServerChannelMojo);
};
-ServerChannelMojo::ServerChannelMojo(scoped_refptr<base::TaskRunner> io_runner,
- const ChannelHandle& handle,
- Listener* listener,
- AttachmentBroker* broker)
- : ChannelMojo(io_runner, handle, Channel::MODE_SERVER, listener, broker),
- weak_factory_(this) {
-}
-
-ServerChannelMojo::~ServerChannelMojo() {
- Close();
-}
-
-void ServerChannelMojo::OnPipeAvailable(
- mojo::embedder::ScopedPlatformHandle handle) {
- mojo::ScopedMessagePipeHandle peer;
- MojoResult create_result =
- mojo::CreateMessagePipe(nullptr, &message_pipe_, &peer);
- if (create_result != MOJO_RESULT_OK) {
- LOG(WARNING) << "mojo::CreateMessagePipe failed: " << create_result;
- listener()->OnChannelError();
- return;
- }
- CreateMessagingPipe(
- handle.Pass(),
- base::Bind(&ServerChannelMojo::InitClientChannel,
- weak_factory_.GetWeakPtr(), base::Passed(&peer)));
-}
-
-void ServerChannelMojo::Close() {
- client_channel_.reset();
- message_pipe_.reset();
- ChannelMojo::Close();
-}
-
-void ServerChannelMojo::InitClientChannel(
- mojo::ScopedMessagePipeHandle peer_handle,
- mojo::ScopedMessagePipeHandle handle) {
- client_channel_.Bind(
- mojo::InterfacePtrInfo<ClientChannel>(handle.Pass(), 0u));
- client_channel_.set_connection_error_handler(base::Bind(
- &ServerChannelMojo::OnConnectionError, base::Unretained(this)));
- client_channel_->Init(
- peer_handle.Pass(), static_cast<int32_t>(GetSelfPID()),
- base::Bind(&ServerChannelMojo::ClientChannelWasInitialized,
- base::Unretained(this)));
-}
-
-void ServerChannelMojo::OnConnectionError() {
- listener()->OnChannelError();
-}
-
-void ServerChannelMojo::ClientChannelWasInitialized(int32_t peer_pid) {
- InitMessageReader(message_pipe_.Pass(), peer_pid);
-}
-
#if defined(OS_POSIX) && !defined(OS_NACL)
base::ScopedFD TakeOrDupFile(internal::PlatformFileAttachment* attachment) {
@@ -211,6 +214,7 @@ base::ScopedFD TakeOrDupFile(internal::PlatformFileAttachment* attachment) {
} // namespace
+#if !defined(USE_CHROME_EDK)
//------------------------------------------------------------------------------
ChannelMojo::ChannelInfoDeleter::ChannelInfoDeleter(
@@ -230,7 +234,7 @@ void ChannelMojo::ChannelInfoDeleter::operator()(
FROM_HERE, base::Bind(&mojo::embedder::DestroyChannelOnIOThread, ptr));
}
}
-
+#endif
//------------------------------------------------------------------------------
// static
@@ -286,13 +290,16 @@ ChannelMojo::ChannelMojo(scoped_refptr<base::TaskRunner> io_runner,
: listener_(listener),
peer_pid_(base::kNullProcessId),
io_runner_(io_runner),
+#if !defined(USE_CHROME_EDK)
channel_info_(nullptr, ChannelInfoDeleter(nullptr)),
+#endif
waiting_connect_(true),
weak_factory_(this) {
// Create MojoBootstrap after all members are set as it touches
// ChannelMojo from a different thread.
bootstrap_ = MojoBootstrap::Create(handle, mode, this, broker);
- if (io_runner == base::MessageLoop::current()->task_runner()) {
+ if (g_use_channel_on_io ||
+ io_runner == base::MessageLoop::current()->task_runner()) {
InitOnIOThread();
} else {
io_runner->PostTask(FROM_HERE, base::Bind(&ChannelMojo::InitOnIOThread,
@@ -309,12 +316,14 @@ void ChannelMojo::InitOnIOThread() {
new ScopedIPCSupport(base::MessageLoop::current()->task_runner()));
}
+#if !defined(USE_CHROME_EDK)
void ChannelMojo::CreateMessagingPipe(
mojo::embedder::ScopedPlatformHandle handle,
const CreateMessagingPipeCallback& callback) {
auto return_callback = base::Bind(&ChannelMojo::OnMessagingPipeCreated,
weak_factory_.GetWeakPtr(), callback);
- if (base::ThreadTaskRunnerHandle::Get() == io_runner_) {
+ if (g_use_channel_on_io ||
+ base::ThreadTaskRunnerHandle::Get() == io_runner_) {
CreateMessagingPipeOnIOThread(
handle.Pass(), base::ThreadTaskRunnerHandle::Get(), return_callback);
} else {
@@ -351,6 +360,7 @@ void ChannelMojo::OnMessagingPipeCreated(
channel_info, ChannelInfoDeleter(io_runner_));
callback.Run(handle.Pass());
}
+#endif
bool ChannelMojo::Connect() {
DCHECK(!message_reader_);
@@ -369,7 +379,9 @@ void ChannelMojo::Close() {
waiting_connect_ = false;
}
+#if !defined(USE_CHROME_EDK)
channel_info_.reset();
+#endif
ipc_support_.reset();
to_be_deleted.reset();
}

Powered by Google App Engine
This is Rietveld 408576698