| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/public/cpp/test_support/test_utils.h" | 5 #include "mojo/public/cpp/test_support/test_utils.h" |
| 6 | 6 |
| 7 #include "mojo/public/cpp/system/core.h" | 7 #include "mojo/public/cpp/system/handle.h" |
| 8 #include "mojo/public/cpp/test_support/test_support.h" | 8 #include "mojo/public/cpp/system/message_pipe.h" |
| 9 | 9 |
| 10 namespace mojo { | 10 namespace mojo { |
| 11 namespace test { | 11 namespace test { |
| 12 | 12 |
| 13 bool WriteTextMessage(const MessagePipeHandle& handle, | 13 bool WriteTextMessage(const MessagePipeHandle& handle, |
| 14 const std::string& text) { | 14 const std::string& text) { |
| 15 MojoResult rv = WriteMessageRaw(handle, | 15 MojoResult rv = |
| 16 text.data(), | 16 WriteMessageRaw(handle, text.data(), static_cast<uint32_t>(text.size()), |
| 17 static_cast<uint32_t>(text.size()), | 17 nullptr, 0u, MOJO_WRITE_MESSAGE_FLAG_NONE); |
| 18 nullptr, | |
| 19 0, | |
| 20 MOJO_WRITE_MESSAGE_FLAG_NONE); | |
| 21 return rv == MOJO_RESULT_OK; | 18 return rv == MOJO_RESULT_OK; |
| 22 } | 19 } |
| 23 | 20 |
| 24 bool ReadTextMessage(const MessagePipeHandle& handle, std::string* text) { | 21 bool ReadTextMessage(const MessagePipeHandle& handle, std::string* text) { |
| 25 MojoResult rv; | 22 MojoResult rv; |
| 26 bool did_wait = false; | 23 bool did_wait = false; |
| 27 | 24 |
| 28 uint32_t num_bytes = 0, num_handles = 0; | 25 uint32_t num_bytes = 0u; |
| 26 uint32_t num_handles = 0u; |
| 29 for (;;) { | 27 for (;;) { |
| 30 rv = ReadMessageRaw(handle, | 28 rv = ReadMessageRaw(handle, nullptr, &num_bytes, nullptr, &num_handles, |
| 31 nullptr, | |
| 32 &num_bytes, | |
| 33 nullptr, | |
| 34 &num_handles, | |
| 35 MOJO_READ_MESSAGE_FLAG_NONE); | 29 MOJO_READ_MESSAGE_FLAG_NONE); |
| 36 if (rv == MOJO_RESULT_SHOULD_WAIT) { | 30 if (rv == MOJO_RESULT_SHOULD_WAIT) { |
| 37 if (did_wait) { | 31 if (did_wait) { |
| 38 assert(false); // Looping endlessly!? | 32 assert(false); // Looping endlessly!? |
| 39 return false; | 33 return false; |
| 40 } | 34 } |
| 41 rv = Wait(handle, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE, | 35 rv = Wait(handle, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE, |
| 42 nullptr); | 36 nullptr); |
| 43 if (rv != MOJO_RESULT_OK) | 37 if (rv != MOJO_RESULT_OK) |
| 44 return false; | 38 return false; |
| 45 did_wait = true; | 39 did_wait = true; |
| 46 } else { | 40 } else { |
| 47 assert(!num_handles); | 41 assert(!num_handles); |
| 48 break; | 42 break; |
| 49 } | 43 } |
| 50 } | 44 } |
| 51 | 45 |
| 52 text->resize(num_bytes); | 46 text->resize(num_bytes); |
| 53 rv = ReadMessageRaw(handle, | 47 rv = ReadMessageRaw(handle, &text->at(0), &num_bytes, nullptr, &num_handles, |
| 54 &text->at(0), | |
| 55 &num_bytes, | |
| 56 nullptr, | |
| 57 &num_handles, | |
| 58 MOJO_READ_MESSAGE_FLAG_NONE); | 48 MOJO_READ_MESSAGE_FLAG_NONE); |
| 59 return rv == MOJO_RESULT_OK; | 49 return rv == MOJO_RESULT_OK; |
| 60 } | 50 } |
| 61 | 51 |
| 62 bool DiscardMessage(const MessagePipeHandle& handle) { | 52 bool DiscardMessage(const MessagePipeHandle& handle) { |
| 63 MojoResult rv = ReadMessageRaw(handle, | 53 MojoResult rv = ReadMessageRaw(handle, nullptr, nullptr, nullptr, nullptr, |
| 64 nullptr, | |
| 65 nullptr, | |
| 66 nullptr, | |
| 67 nullptr, | |
| 68 MOJO_READ_MESSAGE_FLAG_MAY_DISCARD); | 54 MOJO_READ_MESSAGE_FLAG_MAY_DISCARD); |
| 69 return rv == MOJO_RESULT_OK; | 55 return rv == MOJO_RESULT_OK; |
| 70 } | 56 } |
| 71 | 57 |
| 72 } // namespace test | 58 } // namespace test |
| 73 } // namespace mojo | 59 } // namespace mojo |
| OLD | NEW |