summaryrefslogtreecommitdiffstats
path: root/mojo/common
diff options
context:
space:
mode:
authorviettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-11-25 18:36:36 +0000
committerviettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-11-25 18:36:36 +0000
commitb8ce93e3b21bf54f8fe59554d31cdc51a9ad7c95 (patch)
treeea4cd6137b63eb0d20de9b9fe309950e6f05edbf /mojo/common
parent593cb299832f74720e26e42f6ff5ba51ee6ea5c0 (diff)
downloadchromium_src-b8ce93e3b21bf54f8fe59554d31cdc51a9ad7c95.zip
chromium_src-b8ce93e3b21bf54f8fe59554d31cdc51a9ad7c95.tar.gz
chromium_src-b8ce93e3b21bf54f8fe59554d31cdc51a9ad7c95.tar.bz2
Mojo: Remove scoped_message_pipe.*.
ScopedMessagePipe is now mostly unneeded with ScopedMessagePipeHandle (and I can't imagine many "real" uses for it, except in tests). Begin converting stuff in mojo/common over to the new C++ wrappers -- though there's lots more to do. R=sky@chromium.org, sky Review URL: https://codereview.chromium.org/83073007 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@237107 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo/common')
-rw-r--r--mojo/common/handle_watcher.h2
-rw-r--r--mojo/common/handle_watcher_unittest.cc119
-rw-r--r--mojo/common/message_pump_mojo.cc25
-rw-r--r--mojo/common/message_pump_mojo.h2
-rw-r--r--mojo/common/scoped_message_pipe.cc25
-rw-r--r--mojo/common/scoped_message_pipe.h35
6 files changed, 78 insertions, 130 deletions
diff --git a/mojo/common/handle_watcher.h b/mojo/common/handle_watcher.h
index a6a73ea..2d5eb2a 100644
--- a/mojo/common/handle_watcher.h
+++ b/mojo/common/handle_watcher.h
@@ -9,7 +9,7 @@
#include "base/callback_forward.h"
#include "base/memory/scoped_ptr.h"
#include "mojo/common/mojo_common_export.h"
-#include "mojo/public/system/core.h"
+#include "mojo/public/system/core_cpp.h"
namespace base {
class Thread;
diff --git a/mojo/common/handle_watcher_unittest.cc b/mojo/common/handle_watcher_unittest.cc
index a10585a..c73cbb1 100644
--- a/mojo/common/handle_watcher_unittest.cc
+++ b/mojo/common/handle_watcher_unittest.cc
@@ -8,23 +8,31 @@
#include "base/bind.h"
#include "base/run_loop.h"
#include "base/test/simple_test_tick_clock.h"
-#include "mojo/common/scoped_message_pipe.h"
+#include "mojo/public/system/core_cpp.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace mojo {
namespace common {
namespace test {
-MojoResult WriteToHandle(MojoHandle handle) {
- return MojoWriteMessage(handle, NULL, 0, NULL, 0,
- MOJO_WRITE_MESSAGE_FLAG_NONE);
+struct MessagePipe {
+ MessagePipe() { CreateMessagePipe(&handle_0, &handle_1); }
+ ~MessagePipe() {}
+
+ ScopedMessagePipeHandle handle_0;
+ ScopedMessagePipeHandle handle_1;
+
+ DISALLOW_COPY_AND_ASSIGN(MessagePipe);
+};
+
+MojoResult WriteToHandle(const MessagePipeHandle& handle) {
+ return WriteMessageRaw(handle, NULL, 0, NULL, 0,
+ MOJO_WRITE_MESSAGE_FLAG_NONE);
}
-MojoResult ReadFromHandle(MojoHandle handle) {
- uint32_t num_bytes = 0;
- uint32_t num_handles = 0;
- return MojoReadMessage(handle, NULL, &num_bytes, NULL, &num_handles,
- MOJO_READ_MESSAGE_FLAG_MAY_DISCARD);
+MojoResult ReadFromHandle(const MessagePipeHandle& handle) {
+ return ReadMessageRaw(handle, NULL, NULL, NULL, NULL,
+ MOJO_READ_MESSAGE_FLAG_MAY_DISCARD);
}
void RunUntilIdle() {
@@ -71,14 +79,15 @@ class CallbackHelper {
return base::Bind(&CallbackHelper::OnCallback, weak_factory_.GetWeakPtr());
}
- void Start(HandleWatcher* watcher, MojoHandle handle) {
+ void Start(HandleWatcher* watcher, const MessagePipeHandle& handle) {
StartWithCallback(watcher, handle, GetCallback());
}
- void StartWithCallback(HandleWatcher* watcher, MojoHandle handle,
+ void StartWithCallback(HandleWatcher* watcher,
+ const MessagePipeHandle& handle,
const base::Callback<void(MojoResult)>& callback) {
- watcher->Start(handle, MOJO_WAIT_FLAG_READABLE, MOJO_DEADLINE_INDEFINITE,
- callback);
+ watcher->Start(handle.value(), MOJO_WAIT_FLAG_READABLE,
+ MOJO_DEADLINE_INDEFINITE, callback);
}
private:
@@ -122,14 +131,14 @@ class HandleWatcherTest : public testing::Test {
// Trivial test case with a single handle to watch.
TEST_F(HandleWatcherTest, SingleHandler) {
- ScopedMessagePipe test_pipe;
- ASSERT_NE(MOJO_HANDLE_INVALID, test_pipe.handle_0());
+ MessagePipe test_pipe;
+ ASSERT_TRUE(test_pipe.handle_0.is_valid());
CallbackHelper callback_helper;
HandleWatcher watcher;
- callback_helper.Start(&watcher, test_pipe.handle_0());
+ callback_helper.Start(&watcher, test_pipe.handle_0);
RunUntilIdle();
EXPECT_FALSE(callback_helper.got_callback());
- EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe.handle_1()));
+ EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe.handle_1));
callback_helper.RunUntilGotCallback();
EXPECT_TRUE(callback_helper.got_callback());
}
@@ -137,39 +146,39 @@ TEST_F(HandleWatcherTest, SingleHandler) {
// Creates three handles and notfies them in reverse order ensuring each one is
// notified appropriately.
TEST_F(HandleWatcherTest, ThreeHandles) {
- ScopedMessagePipe test_pipe1;
- ScopedMessagePipe test_pipe2;
- ScopedMessagePipe test_pipe3;
+ MessagePipe test_pipe1;
+ MessagePipe test_pipe2;
+ MessagePipe test_pipe3;
CallbackHelper callback_helper1;
CallbackHelper callback_helper2;
CallbackHelper callback_helper3;
- ASSERT_NE(MOJO_HANDLE_INVALID, test_pipe1.handle_0());
- ASSERT_NE(MOJO_HANDLE_INVALID, test_pipe2.handle_0());
- ASSERT_NE(MOJO_HANDLE_INVALID, test_pipe3.handle_0());
+ ASSERT_TRUE(test_pipe1.handle_0.is_valid());
+ ASSERT_TRUE(test_pipe2.handle_0.is_valid());
+ ASSERT_TRUE(test_pipe3.handle_0.is_valid());
HandleWatcher watcher1;
- callback_helper1.Start(&watcher1, test_pipe1.handle_0());
+ callback_helper1.Start(&watcher1, test_pipe1.handle_0);
RunUntilIdle();
EXPECT_FALSE(callback_helper1.got_callback());
EXPECT_FALSE(callback_helper2.got_callback());
EXPECT_FALSE(callback_helper3.got_callback());
HandleWatcher watcher2;
- callback_helper2.Start(&watcher2, test_pipe2.handle_0());
+ callback_helper2.Start(&watcher2, test_pipe2.handle_0);
RunUntilIdle();
EXPECT_FALSE(callback_helper1.got_callback());
EXPECT_FALSE(callback_helper2.got_callback());
EXPECT_FALSE(callback_helper3.got_callback());
HandleWatcher watcher3;
- callback_helper3.Start(&watcher3, test_pipe3.handle_0());
+ callback_helper3.Start(&watcher3, test_pipe3.handle_0);
RunUntilIdle();
EXPECT_FALSE(callback_helper1.got_callback());
EXPECT_FALSE(callback_helper2.got_callback());
EXPECT_FALSE(callback_helper3.got_callback());
// Write to 3 and make sure it's notified.
- EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe3.handle_1()));
+ EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe3.handle_1));
callback_helper3.RunUntilGotCallback();
EXPECT_FALSE(callback_helper1.got_callback());
EXPECT_FALSE(callback_helper2.got_callback());
@@ -178,8 +187,8 @@ TEST_F(HandleWatcherTest, ThreeHandles) {
// Write to 1 and 3. Only 1 should be notified since 3 was is no longer
// running.
- EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe1.handle_1()));
- EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe3.handle_1()));
+ EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe1.handle_1));
+ EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe3.handle_1));
callback_helper1.RunUntilGotCallback();
EXPECT_TRUE(callback_helper1.got_callback());
EXPECT_FALSE(callback_helper2.got_callback());
@@ -187,8 +196,8 @@ TEST_F(HandleWatcherTest, ThreeHandles) {
callback_helper1.clear_callback();
// Write to 1 and 2. Only 2 should be notified (since 1 was already notified).
- EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe1.handle_1()));
- EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe2.handle_1()));
+ EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe1.handle_1));
+ EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe2.handle_1));
callback_helper2.RunUntilGotCallback();
EXPECT_FALSE(callback_helper1.got_callback());
EXPECT_TRUE(callback_helper2.got_callback());
@@ -197,48 +206,48 @@ TEST_F(HandleWatcherTest, ThreeHandles) {
// Verifies Start() invoked a second time works.
TEST_F(HandleWatcherTest, Restart) {
- ScopedMessagePipe test_pipe1;
- ScopedMessagePipe test_pipe2;
+ MessagePipe test_pipe1;
+ MessagePipe test_pipe2;
CallbackHelper callback_helper1;
CallbackHelper callback_helper2;
- ASSERT_NE(MOJO_HANDLE_INVALID, test_pipe1.handle_0());
- ASSERT_NE(MOJO_HANDLE_INVALID, test_pipe2.handle_0());
+ ASSERT_TRUE(test_pipe1.handle_0.is_valid());
+ ASSERT_TRUE(test_pipe2.handle_0.is_valid());
HandleWatcher watcher1;
- callback_helper1.Start(&watcher1, test_pipe1.handle_0());
+ callback_helper1.Start(&watcher1, test_pipe1.handle_0);
RunUntilIdle();
EXPECT_FALSE(callback_helper1.got_callback());
EXPECT_FALSE(callback_helper2.got_callback());
HandleWatcher watcher2;
- callback_helper2.Start(&watcher2, test_pipe2.handle_0());
+ callback_helper2.Start(&watcher2, test_pipe2.handle_0);
RunUntilIdle();
EXPECT_FALSE(callback_helper1.got_callback());
EXPECT_FALSE(callback_helper2.got_callback());
// Write to 1 and make sure it's notified.
- EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe1.handle_1()));
+ EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe1.handle_1));
callback_helper1.RunUntilGotCallback();
EXPECT_TRUE(callback_helper1.got_callback());
EXPECT_FALSE(callback_helper2.got_callback());
callback_helper1.clear_callback();
- EXPECT_EQ(MOJO_RESULT_OK, ReadFromHandle(test_pipe1.handle_0()));
+ EXPECT_EQ(MOJO_RESULT_OK, ReadFromHandle(test_pipe1.handle_0));
// Write to 2 and make sure it's notified.
- EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe2.handle_1()));
+ EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe2.handle_1));
callback_helper2.RunUntilGotCallback();
EXPECT_FALSE(callback_helper1.got_callback());
EXPECT_TRUE(callback_helper2.got_callback());
callback_helper2.clear_callback();
// Listen on 1 again.
- callback_helper1.Start(&watcher1, test_pipe1.handle_0());
+ callback_helper1.Start(&watcher1, test_pipe1.handle_0);
RunUntilIdle();
EXPECT_FALSE(callback_helper1.got_callback());
EXPECT_FALSE(callback_helper2.got_callback());
// Write to 1 and make sure it's notified.
- EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe1.handle_1()));
+ EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe1.handle_1));
callback_helper1.RunUntilGotCallback();
EXPECT_TRUE(callback_helper1.got_callback());
EXPECT_FALSE(callback_helper2.got_callback());
@@ -248,19 +257,19 @@ TEST_F(HandleWatcherTest, Restart) {
TEST_F(HandleWatcherTest, Deadline) {
InstallTickClock();
- ScopedMessagePipe test_pipe1;
- ScopedMessagePipe test_pipe2;
- ScopedMessagePipe test_pipe3;
+ MessagePipe test_pipe1;
+ MessagePipe test_pipe2;
+ MessagePipe test_pipe3;
CallbackHelper callback_helper1;
CallbackHelper callback_helper2;
CallbackHelper callback_helper3;
- ASSERT_NE(MOJO_HANDLE_INVALID, test_pipe1.handle_0());
- ASSERT_NE(MOJO_HANDLE_INVALID, test_pipe2.handle_0());
- ASSERT_NE(MOJO_HANDLE_INVALID, test_pipe3.handle_0());
+ ASSERT_TRUE(test_pipe1.handle_0.is_valid());
+ ASSERT_TRUE(test_pipe2.handle_0.is_valid());
+ ASSERT_TRUE(test_pipe3.handle_0.is_valid());
// Add a watcher with an infinite timeout.
HandleWatcher watcher1;
- callback_helper1.Start(&watcher1, test_pipe1.handle_0());
+ callback_helper1.Start(&watcher1, test_pipe1.handle_0);
RunUntilIdle();
EXPECT_FALSE(callback_helper1.got_callback());
EXPECT_FALSE(callback_helper2.got_callback());
@@ -268,8 +277,8 @@ TEST_F(HandleWatcherTest, Deadline) {
// Add another watcher wth a timeout of 500 microseconds.
HandleWatcher watcher2;
- watcher2.Start(test_pipe2.handle_0(), MOJO_WAIT_FLAG_READABLE, 500,
- callback_helper2.GetCallback());
+ watcher2.Start(test_pipe2.handle_0.get().value(), MOJO_WAIT_FLAG_READABLE,
+ 500, callback_helper2.GetCallback());
RunUntilIdle();
EXPECT_FALSE(callback_helper1.got_callback());
EXPECT_FALSE(callback_helper2.got_callback());
@@ -280,7 +289,7 @@ TEST_F(HandleWatcherTest, Deadline) {
tick_clock_.Advance(base::TimeDelta::FromMicroseconds(501));
HandleWatcher watcher3;
- callback_helper3.Start(&watcher3, test_pipe3.handle_0());
+ callback_helper3.Start(&watcher3, test_pipe3.handle_0);
callback_helper2.RunUntilGotCallback();
EXPECT_FALSE(callback_helper1.got_callback());
@@ -289,15 +298,15 @@ TEST_F(HandleWatcherTest, Deadline) {
}
TEST_F(HandleWatcherTest, DeleteInCallback) {
- ScopedMessagePipe test_pipe;
+ MessagePipe test_pipe;
CallbackHelper callback_helper;
HandleWatcher* watcher = new HandleWatcher();
- callback_helper.StartWithCallback(watcher, test_pipe.handle_1(),
+ callback_helper.StartWithCallback(watcher, test_pipe.handle_1,
base::Bind(&DeleteWatcherAndForwardResult,
watcher,
callback_helper.GetCallback()));
- WriteToHandle(test_pipe.handle_0());
+ WriteToHandle(test_pipe.handle_0);
callback_helper.RunUntilGotCallback();
EXPECT_TRUE(callback_helper.got_callback());
}
diff --git a/mojo/common/message_pump_mojo.cc b/mojo/common/message_pump_mojo.cc
index 64e1396..d6ebfda 100644
--- a/mojo/common/message_pump_mojo.cc
+++ b/mojo/common/message_pump_mojo.cc
@@ -10,7 +10,6 @@
#include "base/logging.h"
#include "base/time/time.h"
#include "mojo/common/message_pump_mojo_handler.h"
-#include "mojo/common/scoped_message_pipe.h"
namespace mojo {
namespace common {
@@ -24,15 +23,15 @@ struct MessagePumpMojo::WaitState {
struct MessagePumpMojo::RunState {
public:
- RunState() : should_quit(false) {}
-
- MojoHandle read_handle() const { return control_pipe.handle_0(); }
- MojoHandle write_handle() const { return control_pipe.handle_1(); }
+ RunState() : should_quit(false) {
+ CreateMessagePipe(&read_handle, &write_handle);
+ }
base::TimeTicks delayed_work_time;
// Used to wake up WaitForWork().
- ScopedMessagePipe control_pipe;
+ ScopedMessagePipeHandle read_handle;
+ ScopedMessagePipeHandle write_handle;
bool should_quit;
};
@@ -67,8 +66,8 @@ void MessagePumpMojo::Run(Delegate* delegate) {
RunState* old_state = run_state_;
RunState run_state;
// TODO: better deal with error handling.
- CHECK_NE(run_state.control_pipe.handle_0(), MOJO_HANDLE_INVALID);
- CHECK_NE(run_state.control_pipe.handle_1(), MOJO_HANDLE_INVALID);
+ CHECK(run_state.read_handle.is_valid());
+ CHECK(run_state.write_handle.is_valid());
run_state_ = &run_state;
bool more_work_is_plausible = true;
for (;;) {
@@ -129,8 +128,8 @@ void MessagePumpMojo::DoInternalWork(bool block) {
if (result == 0) {
// Control pipe was written to.
uint32_t num_bytes = 0;
- MojoReadMessage(run_state_->read_handle(), NULL, &num_bytes, NULL, 0,
- MOJO_READ_MESSAGE_FLAG_MAY_DISCARD);
+ ReadMessageRaw(run_state_->read_handle, NULL, &num_bytes, NULL, NULL,
+ MOJO_READ_MESSAGE_FLAG_MAY_DISCARD);
} else if (result > 0) {
const size_t index = static_cast<size_t>(result);
DCHECK(handlers_.find(wait_state.handles[index]) != handlers_.end());
@@ -191,13 +190,13 @@ void MessagePumpMojo::SignalControlPipe() {
return;
// TODO(sky): deal with error?
- MojoWriteMessage(run_state_->write_handle(), NULL, 0, NULL, 0,
- MOJO_WRITE_MESSAGE_FLAG_NONE);
+ WriteMessageRaw(run_state_->write_handle, NULL, 0, NULL, 0,
+ MOJO_WRITE_MESSAGE_FLAG_NONE);
}
MessagePumpMojo::WaitState MessagePumpMojo::GetWaitState() const {
WaitState wait_state;
- wait_state.handles.push_back(run_state_->read_handle());
+ wait_state.handles.push_back(run_state_->read_handle.get().value());
wait_state.wait_flags.push_back(MOJO_WAIT_FLAG_READABLE);
for (HandleToHandler::const_iterator i = handlers_.begin();
diff --git a/mojo/common/message_pump_mojo.h b/mojo/common/message_pump_mojo.h
index 3d12eee..6db82d4 100644
--- a/mojo/common/message_pump_mojo.h
+++ b/mojo/common/message_pump_mojo.h
@@ -10,7 +10,7 @@
#include "base/message_loop/message_pump.h"
#include "base/time/time.h"
#include "mojo/common/mojo_common_export.h"
-#include "mojo/public/system/core.h"
+#include "mojo/public/system/core_cpp.h"
namespace mojo {
namespace common {
diff --git a/mojo/common/scoped_message_pipe.cc b/mojo/common/scoped_message_pipe.cc
deleted file mode 100644
index cee13bb..0000000
--- a/mojo/common/scoped_message_pipe.cc
+++ /dev/null
@@ -1,25 +0,0 @@
-// Copyright 2013 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 "mojo/common/scoped_message_pipe.h"
-
-namespace mojo {
-namespace common {
-
-ScopedMessagePipe::ScopedMessagePipe()
- : handle_0_(MOJO_HANDLE_INVALID),
- handle_1_(MOJO_HANDLE_INVALID) {
- MojoCreateMessagePipe(&handle_0_, &handle_1_);
-}
-
-ScopedMessagePipe::~ScopedMessagePipe() {
- if (handle_0_ != MOJO_HANDLE_INVALID)
- MojoClose(handle_0_);
-
- if (handle_1_ != MOJO_HANDLE_INVALID)
- MojoClose(handle_1_);
-}
-
-} // namespace common
-} // namespace mojo
diff --git a/mojo/common/scoped_message_pipe.h b/mojo/common/scoped_message_pipe.h
deleted file mode 100644
index 476efde..0000000
--- a/mojo/common/scoped_message_pipe.h
+++ /dev/null
@@ -1,35 +0,0 @@
-// Copyright 2013 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 MOJO_COMMON_SCOPED_MESSAGE_PIPE_H_
-#define MOJO_COMMON_SCOPED_MESSAGE_PIPE_H_
-
-#include "base/basictypes.h"
-#include "mojo/common/mojo_common_export.h"
-#include "mojo/public/system/core.h"
-
-namespace mojo {
-namespace common {
-
-// Simple scoper that creates a message pipe in constructor and closes it in
-// destructor. Test for success by comparing handles with MOJO_HANDLE_INVALID.
-class MOJO_COMMON_EXPORT ScopedMessagePipe {
- public:
- ScopedMessagePipe();
- ~ScopedMessagePipe();
-
- MojoHandle handle_0() const { return handle_0_; }
- MojoHandle handle_1() const { return handle_1_; }
-
- private:
- MojoHandle handle_0_;
- MojoHandle handle_1_;
-
- DISALLOW_COPY_AND_ASSIGN(ScopedMessagePipe);
-};
-
-} // namespace common
-} // namespace mojo
-
-#endif // MOJO_COMMON_SCOPED_MESSAGE_PIPE_H_