| 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 namespace internal { | 13 namespace internal { |
| 14 | 14 |
| 15 // ---------------------------------------------------------------------------- | 15 // ---------------------------------------------------------------------------- |
| 16 | 16 |
| 17 Connector::Connector(ScopedMessagePipeHandle message_pipe) | 17 Connector::Connector(ScopedMessagePipeHandle message_pipe, |
| 18 : message_pipe_(message_pipe.Pass()), | 18 MojoAsyncWaiter* waiter) |
| 19 : waiter_(waiter), |
| 20 message_pipe_(message_pipe.Pass()), |
| 19 incoming_receiver_(NULL), | 21 incoming_receiver_(NULL), |
| 20 error_(false) { | 22 error_(false) { |
| 21 } | 23 } |
| 22 | 24 |
| 23 Connector::~Connector() { | 25 Connector::~Connector() { |
| 24 } | 26 } |
| 25 | 27 |
| 26 void Connector::SetIncomingReceiver(MessageReceiver* receiver) { | 28 void Connector::SetIncomingReceiver(MessageReceiver* receiver) { |
| 27 assert(!incoming_receiver_); | 29 assert(!incoming_receiver_); |
| 28 incoming_receiver_ = receiver; | 30 incoming_receiver_ = receiver; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 47 } | 49 } |
| 48 | 50 |
| 49 void Connector::OnHandleReady(Callback* callback, MojoResult result) { | 51 void Connector::OnHandleReady(Callback* callback, MojoResult result) { |
| 50 if (callback == &read_callback_) | 52 if (callback == &read_callback_) |
| 51 ReadMore(); | 53 ReadMore(); |
| 52 if (callback == &write_callback_) | 54 if (callback == &write_callback_) |
| 53 WriteMore(); | 55 WriteMore(); |
| 54 } | 56 } |
| 55 | 57 |
| 56 void Connector::WaitToReadMore() { | 58 void Connector::WaitToReadMore() { |
| 57 read_callback_.SetOwnerToNotify(this); | 59 CallAsyncWait(MOJO_WAIT_FLAG_READABLE, &read_callback_); |
| 58 read_callback_.SetAsyncWaitID( | |
| 59 BindingsSupport::Get()->AsyncWait(message_pipe_.get(), | |
| 60 MOJO_WAIT_FLAG_READABLE, | |
| 61 &read_callback_)); | |
| 62 } | 60 } |
| 63 | 61 |
| 64 void Connector::WaitToWriteMore() { | 62 void Connector::WaitToWriteMore() { |
| 65 write_callback_.SetOwnerToNotify(this); | 63 CallAsyncWait(MOJO_WAIT_FLAG_WRITABLE, &write_callback_); |
| 66 write_callback_.SetAsyncWaitID( | 64 } |
| 67 BindingsSupport::Get()->AsyncWait(message_pipe_.get(), | 65 |
| 68 MOJO_WAIT_FLAG_WRITABLE, | 66 void Connector::CallAsyncWait(MojoWaitFlags flags, Callback* callback) { |
| 69 &write_callback_)); | 67 callback->SetOwnerToNotify(this); |
| 68 callback->SetAsyncWaitID( |
| 69 waiter_->AsyncWait(waiter_, |
| 70 message_pipe_.get().value(), |
| 71 MOJO_WAIT_FLAG_READABLE, |
| 72 MOJO_DEADLINE_INDEFINITE, |
| 73 &Callback::OnHandleReady, |
| 74 callback)); |
| 75 } |
| 76 |
| 77 void Connector::CallCancelWait(MojoAsyncWaitID async_wait_id) { |
| 78 waiter_->CancelWait(waiter_, async_wait_id); |
| 70 } | 79 } |
| 71 | 80 |
| 72 void Connector::ReadMore() { | 81 void Connector::ReadMore() { |
| 73 for (;;) { | 82 for (;;) { |
| 74 MojoResult rv; | 83 MojoResult rv; |
| 75 | 84 |
| 76 uint32_t num_bytes = 0, num_handles = 0; | 85 uint32_t num_bytes = 0, num_handles = 0; |
| 77 rv = ReadMessageRaw(message_pipe_.get(), | 86 rv = ReadMessageRaw(message_pipe_.get(), |
| 78 NULL, | 87 NULL, |
| 79 &num_bytes, | 88 &num_bytes, |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 | 157 |
| 149 // ---------------------------------------------------------------------------- | 158 // ---------------------------------------------------------------------------- |
| 150 | 159 |
| 151 Connector::Callback::Callback() | 160 Connector::Callback::Callback() |
| 152 : owner_(NULL), | 161 : owner_(NULL), |
| 153 async_wait_id_(0) { | 162 async_wait_id_(0) { |
| 154 } | 163 } |
| 155 | 164 |
| 156 Connector::Callback::~Callback() { | 165 Connector::Callback::~Callback() { |
| 157 if (owner_) | 166 if (owner_) |
| 158 BindingsSupport::Get()->CancelWait(async_wait_id_); | 167 owner_->CallCancelWait(async_wait_id_); |
| 159 } | 168 } |
| 160 | 169 |
| 161 void Connector::Callback::SetOwnerToNotify(Connector* owner) { | 170 void Connector::Callback::SetOwnerToNotify(Connector* owner) { |
| 162 assert(!owner_); | 171 assert(!owner_); |
| 163 owner_ = owner; | 172 owner_ = owner; |
| 164 } | 173 } |
| 165 | 174 |
| 166 void Connector::Callback::SetAsyncWaitID(BindingsSupport::AsyncWaitID id) { | 175 void Connector::Callback::SetAsyncWaitID(MojoAsyncWaitID id) { |
| 167 async_wait_id_ = id; | 176 async_wait_id_ = id; |
| 168 } | 177 } |
| 169 | 178 |
| 170 void Connector::Callback::OnHandleReady(MojoResult result) { | 179 // static |
| 171 assert(owner_); | 180 void Connector::Callback::OnHandleReady(void* closure, MojoResult result) { |
| 181 Callback* self = static_cast<Callback*>(closure); |
| 182 |
| 183 // Reset |owner_| to indicate that we are no longer in the waiting state. |
| 184 |
| 185 assert(self->owner_); |
| 172 Connector* owner = NULL; | 186 Connector* owner = NULL; |
| 173 std::swap(owner, owner_); | 187 std::swap(owner, self->owner_); |
| 174 owner->OnHandleReady(this, result); | 188 owner->OnHandleReady(self, result); |
| 175 } | 189 } |
| 176 | 190 |
| 177 } // namespace internal | 191 } // namespace internal |
| 178 } // namespace mojo | 192 } // namespace mojo |
| OLD | NEW |