| 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> |
| 11 | 11 |
| 12 namespace mojo { | 12 namespace mojo { |
| 13 | 13 |
| 14 // ---------------------------------------------------------------------------- | 14 // ---------------------------------------------------------------------------- |
| 15 | 15 |
| 16 Connector::Connector(Handle message_pipe) | 16 Connector::Connector(Handle message_pipe) |
| 17 : message_pipe_(message_pipe), | 17 : message_pipe_(message_pipe), |
| 18 incoming_receiver_(NULL), | 18 incoming_receiver_(NULL), |
| 19 error_(false) { | 19 error_(false) { |
| 20 } | 20 } |
| 21 | 21 |
| 22 Connector::~Connector() { | 22 Connector::~Connector() { |
| 23 if (read_callback_.IsPending()) |
| 24 read_callback_.Cancel(); |
| 25 if (write_callback_.IsPending()) |
| 26 write_callback_.Cancel(); |
| 23 } | 27 } |
| 24 | 28 |
| 25 void Connector::SetIncomingReceiver(MessageReceiver* receiver) { | 29 void Connector::SetIncomingReceiver(MessageReceiver* receiver) { |
| 26 assert(!incoming_receiver_); | 30 assert(!incoming_receiver_); |
| 27 incoming_receiver_ = receiver; | 31 incoming_receiver_ = receiver; |
| 28 if (incoming_receiver_) | 32 if (incoming_receiver_) |
| 29 WaitToReadMore(); | 33 WaitToReadMore(); |
| 30 } | 34 } |
| 31 | 35 |
| 32 bool Connector::Accept(Message* message) { | 36 bool Connector::Accept(Message* message) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 47 | 51 |
| 48 void Connector::OnHandleReady(Callback* callback, MojoResult result) { | 52 void Connector::OnHandleReady(Callback* callback, MojoResult result) { |
| 49 if (callback == &read_callback_) | 53 if (callback == &read_callback_) |
| 50 ReadMore(); | 54 ReadMore(); |
| 51 if (callback == &write_callback_) | 55 if (callback == &write_callback_) |
| 52 WriteMore(); | 56 WriteMore(); |
| 53 } | 57 } |
| 54 | 58 |
| 55 void Connector::WaitToReadMore() { | 59 void Connector::WaitToReadMore() { |
| 56 read_callback_.SetOwnerToNotify(this); | 60 read_callback_.SetOwnerToNotify(this); |
| 57 read_callback_.SetAsyncWaitID( | 61 |
| 58 BindingsSupport::Get()->AsyncWait(message_pipe_, | 62 bool ok = BindingsSupport::Get()->AsyncWait(message_pipe_, |
| 59 MOJO_WAIT_FLAG_READABLE, | 63 MOJO_WAIT_FLAG_READABLE, |
| 60 &read_callback_)); | 64 MOJO_DEADLINE_INDEFINITE, |
| 65 &read_callback_); |
| 66 if (!ok) |
| 67 error_ = true; |
| 61 } | 68 } |
| 62 | 69 |
| 63 void Connector::WaitToWriteMore() { | 70 void Connector::WaitToWriteMore() { |
| 64 write_callback_.SetOwnerToNotify(this); | 71 write_callback_.SetOwnerToNotify(this); |
| 65 write_callback_.SetAsyncWaitID( | 72 |
| 66 BindingsSupport::Get()->AsyncWait(message_pipe_, | 73 bool ok = BindingsSupport::Get()->AsyncWait(message_pipe_, |
| 67 MOJO_WAIT_FLAG_WRITABLE, | 74 MOJO_WAIT_FLAG_WRITABLE, |
| 68 &write_callback_)); | 75 MOJO_DEADLINE_INDEFINITE, |
| 76 &write_callback_); |
| 77 if (!ok) |
| 78 error_ = true; |
| 69 } | 79 } |
| 70 | 80 |
| 71 void Connector::ReadMore() { | 81 void Connector::ReadMore() { |
| 72 for (;;) { | 82 for (;;) { |
| 73 MojoResult rv; | 83 MojoResult rv; |
| 74 | 84 |
| 75 uint32_t num_bytes = 0, num_handles = 0; | 85 uint32_t num_bytes = 0, num_handles = 0; |
| 76 rv = ReadMessage(message_pipe_, | 86 rv = ReadMessage(message_pipe_, |
| 77 NULL, | 87 NULL, |
| 78 &num_bytes, | 88 &num_bytes, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 void Connector::WriteOne(Message* message, bool* wait_to_write) { | 133 void Connector::WriteOne(Message* message, bool* wait_to_write) { |
| 124 // TODO(darin): WriteMessage will eventually start generating an error that | 134 // TODO(darin): WriteMessage will eventually start generating an error that |
| 125 // it cannot accept more data. In that case, we'll need to wait on the pipe | 135 // it cannot accept more data. In that case, we'll need to wait on the pipe |
| 126 // to determine when we can try writing again. This flag will be set to true | 136 // to determine when we can try writing again. This flag will be set to true |
| 127 // in that case. | 137 // in that case. |
| 128 *wait_to_write = false; | 138 *wait_to_write = false; |
| 129 | 139 |
| 130 MojoResult rv = WriteMessage(message_pipe_, | 140 MojoResult rv = WriteMessage(message_pipe_, |
| 131 message->data, | 141 message->data, |
| 132 message->data->header.num_bytes, | 142 message->data->header.num_bytes, |
| 133 &message->handles[0], | 143 message->handles.data(), |
| 134 static_cast<uint32_t>(message->handles.size()), | 144 static_cast<uint32_t>(message->handles.size()), |
| 135 MOJO_WRITE_MESSAGE_FLAG_NONE); | 145 MOJO_WRITE_MESSAGE_FLAG_NONE); |
| 136 if (rv == MOJO_RESULT_OK) { | 146 if (rv == MOJO_RESULT_OK) { |
| 137 // The handles were successfully transferred, so we don't need the message | 147 // The handles were successfully transferred, so we don't need the message |
| 138 // to track their lifetime any longer. | 148 // to track their lifetime any longer. |
| 139 message->handles.clear(); | 149 message->handles.clear(); |
| 140 } else { | 150 } else { |
| 141 error_ = true; | 151 error_ = true; |
| 142 } | 152 } |
| 143 } | 153 } |
| 144 | 154 |
| 145 // ---------------------------------------------------------------------------- | 155 // ---------------------------------------------------------------------------- |
| 146 | 156 |
| 147 Connector::Callback::Callback() | 157 Connector::Callback::Callback() |
| 148 : owner_(NULL), | 158 : owner_(NULL) { |
| 149 async_wait_id_(0) { | |
| 150 } | 159 } |
| 151 | 160 |
| 152 Connector::Callback::~Callback() { | 161 void Connector::Callback::Cancel() { |
| 153 if (owner_) | 162 owner_ = NULL; |
| 154 BindingsSupport::Get()->CancelWait(async_wait_id_); | 163 BindingsSupport::Get()->CancelWait(this); |
| 155 } | 164 } |
| 156 | 165 |
| 157 void Connector::Callback::SetOwnerToNotify(Connector* owner) { | 166 void Connector::Callback::SetOwnerToNotify(Connector* owner) { |
| 158 assert(!owner_); | 167 assert(!owner_); |
| 159 owner_ = owner; | 168 owner_ = owner; |
| 160 } | 169 } |
| 161 | 170 |
| 162 void Connector::Callback::SetAsyncWaitID(BindingsSupport::AsyncWaitID id) { | 171 bool Connector::Callback::IsPending() const { |
| 163 async_wait_id_ = id; | 172 return owner_ != NULL; |
| 164 } | 173 } |
| 165 | 174 |
| 166 void Connector::Callback::OnHandleReady(MojoResult result) { | 175 void Connector::Callback::OnHandleReady(MojoResult result) { |
| 167 assert(owner_); | 176 assert(owner_); |
| 168 Connector* owner = NULL; | 177 Connector* owner = NULL; |
| 169 std::swap(owner, owner_); | 178 std::swap(owner, owner_); |
| 170 owner->OnHandleReady(this, result); | 179 owner->OnHandleReady(this, result); |
| 171 } | 180 } |
| 172 | 181 |
| 173 } // namespace mojo | 182 } // namespace mojo |
| OLD | NEW |