| 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/bindings/lib/connector.h" | 5 #include "mojo/public/bindings/lib/connector.h" |
| 6 | 6 |
| 7 #include <assert.h> | 7 #include <assert.h> |
| 8 #include <stdlib.h> | 8 #include <stdlib.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 void Connector::OnHandleReady(Callback* callback, MojoResult result) { | 48 void Connector::OnHandleReady(Callback* callback, MojoResult result) { |
| 49 if (callback == &read_callback_) | 49 if (callback == &read_callback_) |
| 50 ReadMore(); | 50 ReadMore(); |
| 51 if (callback == &write_callback_) | 51 if (callback == &write_callback_) |
| 52 WriteMore(); | 52 WriteMore(); |
| 53 } | 53 } |
| 54 | 54 |
| 55 void Connector::WaitToReadMore() { | 55 void Connector::WaitToReadMore() { |
| 56 read_callback_.SetOwnerToNotify(this); | 56 read_callback_.SetOwnerToNotify(this); |
| 57 read_callback_.SetAsyncWaitID( | 57 read_callback_.SetAsyncWaitID( |
| 58 BindingsSupport::Get()->AsyncWait(message_pipe_, | 58 BindingsSupport::Get()->AsyncWait(message_pipe_.get(), |
| 59 MOJO_WAIT_FLAG_READABLE, | 59 MOJO_WAIT_FLAG_READABLE, |
| 60 &read_callback_)); | 60 &read_callback_)); |
| 61 } | 61 } |
| 62 | 62 |
| 63 void Connector::WaitToWriteMore() { | 63 void Connector::WaitToWriteMore() { |
| 64 write_callback_.SetOwnerToNotify(this); | 64 write_callback_.SetOwnerToNotify(this); |
| 65 write_callback_.SetAsyncWaitID( | 65 write_callback_.SetAsyncWaitID( |
| 66 BindingsSupport::Get()->AsyncWait(message_pipe_, | 66 BindingsSupport::Get()->AsyncWait(message_pipe_.get(), |
| 67 MOJO_WAIT_FLAG_WRITABLE, | 67 MOJO_WAIT_FLAG_WRITABLE, |
| 68 &write_callback_)); | 68 &write_callback_)); |
| 69 } | 69 } |
| 70 | 70 |
| 71 void Connector::ReadMore() { | 71 void Connector::ReadMore() { |
| 72 for (;;) { | 72 for (;;) { |
| 73 MojoResult rv; | 73 MojoResult rv; |
| 74 | 74 |
| 75 uint32_t num_bytes = 0, num_handles = 0; | 75 uint32_t num_bytes = 0, num_handles = 0; |
| 76 rv = ReadMessageRaw(message_pipe_, | 76 rv = ReadMessageRaw(message_pipe_.get(), |
| 77 NULL, | 77 NULL, |
| 78 &num_bytes, | 78 &num_bytes, |
| 79 NULL, | 79 NULL, |
| 80 &num_handles, | 80 &num_handles, |
| 81 MOJO_READ_MESSAGE_FLAG_NONE); | 81 MOJO_READ_MESSAGE_FLAG_NONE); |
| 82 if (rv == MOJO_RESULT_NOT_FOUND) { | 82 if (rv == MOJO_RESULT_NOT_FOUND) { |
| 83 WaitToReadMore(); | 83 WaitToReadMore(); |
| 84 break; | 84 break; |
| 85 } | 85 } |
| 86 if (rv != MOJO_RESULT_RESOURCE_EXHAUSTED) { | 86 if (rv != MOJO_RESULT_RESOURCE_EXHAUSTED) { |
| 87 error_ = true; | 87 error_ = true; |
| 88 break; | 88 break; |
| 89 } | 89 } |
| 90 | 90 |
| 91 Message message; | 91 Message message; |
| 92 message.data = static_cast<MessageData*>(malloc(num_bytes)); | 92 message.data = static_cast<MessageData*>(malloc(num_bytes)); |
| 93 message.handles.resize(num_handles); | 93 message.handles.resize(num_handles); |
| 94 | 94 |
| 95 rv = ReadMessageRaw(message_pipe_, | 95 rv = ReadMessageRaw(message_pipe_.get(), |
| 96 message.data, | 96 message.data, |
| 97 &num_bytes, | 97 &num_bytes, |
| 98 message.handles.empty() ? NULL : | 98 message.handles.empty() ? NULL : |
| 99 reinterpret_cast<MojoHandle*>(&message.handles[0]), | 99 reinterpret_cast<MojoHandle*>(&message.handles[0]), |
| 100 &num_handles, | 100 &num_handles, |
| 101 MOJO_READ_MESSAGE_FLAG_NONE); | 101 MOJO_READ_MESSAGE_FLAG_NONE); |
| 102 if (rv != MOJO_RESULT_OK) { | 102 if (rv != MOJO_RESULT_OK) { |
| 103 error_ = true; | 103 error_ = true; |
| 104 break; | 104 break; |
| 105 } | 105 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 122 } | 122 } |
| 123 | 123 |
| 124 void Connector::WriteOne(Message* message, bool* wait_to_write) { | 124 void Connector::WriteOne(Message* message, bool* wait_to_write) { |
| 125 // TODO(darin): WriteMessageRaw will eventually start generating an error that | 125 // TODO(darin): WriteMessageRaw will eventually start generating an error that |
| 126 // it cannot accept more data. In that case, we'll need to wait on the pipe | 126 // it cannot accept more data. In that case, we'll need to wait on the pipe |
| 127 // to determine when we can try writing again. This flag will be set to true | 127 // to determine when we can try writing again. This flag will be set to true |
| 128 // in that case. | 128 // in that case. |
| 129 *wait_to_write = false; | 129 *wait_to_write = false; |
| 130 | 130 |
| 131 MojoResult rv = WriteMessageRaw( | 131 MojoResult rv = WriteMessageRaw( |
| 132 message_pipe_, | 132 message_pipe_.get(), |
| 133 message->data, | 133 message->data, |
| 134 message->data->header.num_bytes, | 134 message->data->header.num_bytes, |
| 135 message->handles.empty() ? NULL : | 135 message->handles.empty() ? NULL : |
| 136 reinterpret_cast<const MojoHandle*>(&message->handles[0]), | 136 reinterpret_cast<const MojoHandle*>(&message->handles[0]), |
| 137 static_cast<uint32_t>(message->handles.size()), | 137 static_cast<uint32_t>(message->handles.size()), |
| 138 MOJO_WRITE_MESSAGE_FLAG_NONE); | 138 MOJO_WRITE_MESSAGE_FLAG_NONE); |
| 139 if (rv == MOJO_RESULT_OK) { | 139 if (rv == MOJO_RESULT_OK) { |
| 140 // The handles were successfully transferred, so we don't need the message | 140 // The handles were successfully transferred, so we don't need the message |
| 141 // to track their lifetime any longer. | 141 // to track their lifetime any longer. |
| 142 message->handles.clear(); | 142 message->handles.clear(); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 167 } | 167 } |
| 168 | 168 |
| 169 void Connector::Callback::OnHandleReady(MojoResult result) { | 169 void Connector::Callback::OnHandleReady(MojoResult result) { |
| 170 assert(owner_); | 170 assert(owner_); |
| 171 Connector* owner = NULL; | 171 Connector* owner = NULL; |
| 172 std::swap(owner, owner_); | 172 std::swap(owner, owner_); |
| 173 owner->OnHandleReady(this, result); | 173 owner->OnHandleReady(this, result); |
| 174 } | 174 } |
| 175 | 175 |
| 176 } // namespace mojo | 176 } // namespace mojo |
| OLD | NEW |