diff options
author | morrita <morrita@chromium.org> | 2015-04-01 16:37:18 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-04-01 23:38:00 +0000 |
commit | 2b85d21119ce534940ccd2ff57727cd4b428081c (patch) | |
tree | 2f6b48b9442c9c9cf5833bb6fba07415e041c5ef /ipc | |
parent | 32afefd3f43e4548902826b0705be74f31d58c5b (diff) | |
download | chromium_src-2b85d21119ce534940ccd2ff57727cd4b428081c.zip chromium_src-2b85d21119ce534940ccd2ff57727cd4b428081c.tar.gz chromium_src-2b85d21119ce534940ccd2ff57727cd4b428081c.tar.bz2 |
Implement IPC::ParamTraits<mojo::MessagePipeHandle>
This allows MessagePipeHandle to be an IPC message member.
Note that this assumes underlying channel being a ChannelMojo
and can be used in content/ only after ChannelMojo settles.
BUG=448190
TEST=IPCChannelMojoTest.ParamTrait*
R=viettrungluu@chromium.org
Committed: https://crrev.com/5656ae68841a3e3ddaac75cf2983d9052d71d0d5
Cr-Commit-Position: refs/heads/master@{#323265}
Review URL: https://codereview.chromium.org/1051443003
Cr-Commit-Position: refs/heads/master@{#323370}
Diffstat (limited to 'ipc')
-rw-r--r-- | ipc/mojo/BUILD.gn | 2 | ||||
-rw-r--r-- | ipc/mojo/ipc_channel_mojo_unittest.cc | 115 | ||||
-rw-r--r-- | ipc/mojo/ipc_mojo.gyp | 2 | ||||
-rw-r--r-- | ipc/mojo/ipc_mojo_param_traits.cc | 42 | ||||
-rw-r--r-- | ipc/mojo/ipc_mojo_param_traits.h | 30 |
5 files changed, 191 insertions, 0 deletions
diff --git a/ipc/mojo/BUILD.gn b/ipc/mojo/BUILD.gn index 6d2a9f8..50241fe 100644 --- a/ipc/mojo/BUILD.gn +++ b/ipc/mojo/BUILD.gn @@ -28,6 +28,8 @@ component("mojo") { "ipc_mojo_handle_attachment.h", "ipc_mojo_message_helper.cc", "ipc_mojo_message_helper.h", + "ipc_mojo_param_traits.cc", + "ipc_mojo_param_traits.h", "scoped_ipc_support.cc", "scoped_ipc_support.h", ] diff --git a/ipc/mojo/ipc_channel_mojo_unittest.cc b/ipc/mojo/ipc_channel_mojo_unittest.cc index 662e758..356d3b3 100644 --- a/ipc/mojo/ipc_channel_mojo_unittest.cc +++ b/ipc/mojo/ipc_channel_mojo_unittest.cc @@ -18,6 +18,7 @@ #include "ipc/mojo/ipc_channel_mojo_host.h" #include "ipc/mojo/ipc_mojo_handle_attachment.h" #include "ipc/mojo/ipc_mojo_message_helper.h" +#include "ipc/mojo/ipc_mojo_param_traits.h" #include "ipc/mojo/scoped_ipc_support.h" #if defined(OS_POSIX) @@ -442,6 +443,120 @@ MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestSendMessagePipeClient) { return 0; } +void ReadOK(mojo::MessagePipeHandle pipe) { + std::string should_be_ok("xx"); + uint32_t num_bytes = static_cast<uint32_t>(should_be_ok.size()); + CHECK_EQ(MOJO_RESULT_OK, + mojo::ReadMessageRaw(pipe, &should_be_ok[0], &num_bytes, nullptr, + nullptr, 0)); + EXPECT_EQ(should_be_ok, std::string("OK")); +} + +void WriteOK(mojo::MessagePipeHandle pipe) { + std::string ok("OK"); + CHECK_EQ(MOJO_RESULT_OK, + mojo::WriteMessageRaw(pipe, &ok[0], static_cast<uint32_t>(ok.size()), + nullptr, 0, 0)); +} + +class ListenerThatExpectsMessagePipeUsingParamTrait : public IPC::Listener { + public: + explicit ListenerThatExpectsMessagePipeUsingParamTrait(bool receiving_valid) + : sender_(NULL), receiving_valid_(receiving_valid) {} + + ~ListenerThatExpectsMessagePipeUsingParamTrait() override {} + + bool OnMessageReceived(const IPC::Message& message) override { + PickleIterator iter(message); + mojo::MessagePipeHandle handle; + EXPECT_TRUE(IPC::ParamTraits<mojo::MessagePipeHandle>::Read(&message, &iter, + &handle)); + EXPECT_EQ(handle.is_valid(), receiving_valid_); + if (receiving_valid_) { + ReadOK(handle); + MojoClose(handle.value()); + } + + base::MessageLoop::current()->Quit(); + ListenerThatExpectsOK::SendOK(sender_); + return true; + } + + void OnChannelError() override { NOTREACHED(); } + void set_sender(IPC::Sender* sender) { sender_ = sender; } + + private: + IPC::Sender* sender_; + bool receiving_valid_; +}; + +void ParamTraitMessagePipeClient(bool receiving_valid_handle, + const char* channel_name) { + ListenerThatExpectsMessagePipeUsingParamTrait listener( + receiving_valid_handle); + ChannelClient client(&listener, channel_name); + client.Connect(); + listener.set_sender(client.channel()); + + base::MessageLoop::current()->Run(); + + client.Close(); +} + +TEST_F(IPCChannelMojoTest, ParamTraitValidMessagePipe) { + InitWithMojo("ParamTraitValidMessagePipeClient"); + + ListenerThatExpectsOK listener; + CreateChannel(&listener); + ASSERT_TRUE(ConnectChannel()); + ASSERT_TRUE(StartClient()); + + TestingMessagePipe pipe; + + scoped_ptr<IPC::Message> message(new IPC::Message()); + IPC::ParamTraits<mojo::MessagePipeHandle>::Write(message.get(), + pipe.peer.release()); + WriteOK(pipe.self.get()); + + this->channel()->Send(message.release()); + base::MessageLoop::current()->Run(); + this->channel()->Close(); + + EXPECT_TRUE(WaitForClientShutdown()); + DestroyChannel(); +} + +MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ParamTraitValidMessagePipeClient) { + ParamTraitMessagePipeClient(true, "ParamTraitValidMessagePipeClient"); + return 0; +} + +TEST_F(IPCChannelMojoTest, ParamTraitInvalidMessagePipe) { + InitWithMojo("ParamTraitInvalidMessagePipeClient"); + + ListenerThatExpectsOK listener; + CreateChannel(&listener); + ASSERT_TRUE(ConnectChannel()); + ASSERT_TRUE(StartClient()); + + mojo::MessagePipeHandle invalid_handle; + scoped_ptr<IPC::Message> message(new IPC::Message()); + IPC::ParamTraits<mojo::MessagePipeHandle>::Write(message.get(), + invalid_handle); + + this->channel()->Send(message.release()); + base::MessageLoop::current()->Run(); + this->channel()->Close(); + + EXPECT_TRUE(WaitForClientShutdown()); + DestroyChannel(); +} + +MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ParamTraitInvalidMessagePipeClient) { + ParamTraitMessagePipeClient(false, "ParamTraitInvalidMessagePipeClient"); + return 0; +} + #if defined(OS_WIN) class IPCChannelMojoDeadHandleTest : public IPCChannelMojoTestBase { protected: diff --git a/ipc/mojo/ipc_mojo.gyp b/ipc/mojo/ipc_mojo.gyp index 44f1817..a392283 100644 --- a/ipc/mojo/ipc_mojo.gyp +++ b/ipc/mojo/ipc_mojo.gyp @@ -40,6 +40,8 @@ 'ipc_mojo_handle_attachment.h', 'ipc_mojo_message_helper.cc', 'ipc_mojo_message_helper.h', + 'ipc_mojo_param_traits.cc', + 'ipc_mojo_param_traits.h', 'ipc_message_pipe_reader.cc', 'ipc_message_pipe_reader.h', 'scoped_ipc_support.cc', diff --git a/ipc/mojo/ipc_mojo_param_traits.cc b/ipc/mojo/ipc_mojo_param_traits.cc new file mode 100644 index 0000000..159d9a7 --- /dev/null +++ b/ipc/mojo/ipc_mojo_param_traits.cc @@ -0,0 +1,42 @@ +// Copyright 2015 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 "ipc/mojo/ipc_mojo_param_traits.h" + +#include "ipc/ipc_message_utils.h" +#include "ipc/mojo/ipc_mojo_message_helper.h" + +namespace IPC { + +void ParamTraits<mojo::MessagePipeHandle>::Write(Message* m, + const param_type& p) { + WriteParam(m, p.is_valid()); + if (p.is_valid()) + MojoMessageHelper::WriteMessagePipeTo(m, mojo::ScopedMessagePipeHandle(p)); +} + +bool ParamTraits<mojo::MessagePipeHandle>::Read(const Message* m, + PickleIterator* iter, + param_type* r) { + bool is_valid; + if (!ReadParam(m, iter, &is_valid)) + return false; + if (!is_valid) + return true; + + mojo::ScopedMessagePipeHandle handle; + if (!MojoMessageHelper::ReadMessagePipeFrom(m, iter, &handle)) + return false; + *r = handle.release(); + return true; +} + +void ParamTraits<mojo::MessagePipeHandle>::Log(const param_type& p, + std::string* l) { + l->append("mojo::MessagePipeHandle("); + LogParam(p.value(), l); + l->append(")"); +} + +} // namespace IPC diff --git a/ipc/mojo/ipc_mojo_param_traits.h b/ipc/mojo/ipc_mojo_param_traits.h new file mode 100644 index 0000000..d12e44fc --- /dev/null +++ b/ipc/mojo/ipc_mojo_param_traits.h @@ -0,0 +1,30 @@ +// Copyright 2015 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 IPC_MOJO_IPC_MOJO_PARAM_TRAITS_H_ +#define IPC_MOJO_IPC_MOJO_PARAM_TRAITS_H_ + +#include <string> + +#include "ipc/ipc_export.h" +#include "ipc/ipc_param_traits.h" +#include "third_party/mojo/src/mojo/public/cpp/system/message_pipe.h" + +class PickleIterator; + +namespace IPC { + +class Message; + +template <> +struct IPC_MOJO_EXPORT ParamTraits<mojo::MessagePipeHandle> { + typedef mojo::MessagePipeHandle param_type; + static void Write(Message* m, const param_type& p); + static bool Read(const Message* m, PickleIterator* iter, param_type* r); + static void Log(const param_type& p, std::string* l); +}; + +} // namespace IPC + +#endif // IPC_MOJO_IPC_MOJO_PARAM_TRAITS_H_ |