| 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(); | |
| 27 } | 23 } |
| 28 | 24 |
| 29 void Connector::SetIncomingReceiver(MessageReceiver* receiver) { | 25 void Connector::SetIncomingReceiver(MessageReceiver* receiver) { |
| 30 assert(!incoming_receiver_); | 26 assert(!incoming_receiver_); |
| 31 incoming_receiver_ = receiver; | 27 incoming_receiver_ = receiver; |
| 32 if (incoming_receiver_) | 28 if (incoming_receiver_) |
| 33 WaitToReadMore(); | 29 WaitToReadMore(); |
| 34 } | 30 } |
| 35 | 31 |
| 36 bool Connector::Accept(Message* message) { | 32 bool Connector::Accept(Message* message) { |
| 37 if (error_) | 33 if (error_) |
| 38 return false; | 34 return false; |
| 39 | 35 |
| 40 write_queue_.Push(message); | 36 write_queue_.Push(message); |
| 41 WriteMore(); | 37 WriteMore(); |
| 42 return !error_; | 38 return !error_; |
| 43 } | 39 } |
| 44 | 40 |
| 45 void Connector::OnHandleReady(Callback* callback, MojoResult result) { | 41 void Connector::OnHandleReady(Callback* callback, MojoResult result) { |
| 46 if (callback == &read_callback_) | 42 if (callback == &read_callback_) |
| 47 ReadMore(); | 43 ReadMore(); |
| 48 if (callback == &write_callback_) | 44 if (callback == &write_callback_) |
| 49 WriteMore(); | 45 WriteMore(); |
| 50 } | 46 } |
| 51 | 47 |
| 52 void Connector::WaitToReadMore() { | 48 void Connector::WaitToReadMore() { |
| 53 read_callback_.SetOwnerToNotify(this); | 49 read_callback_.SetOwnerToNotify(this); |
| 54 | 50 read_callback_.SetAsyncWaitID( |
| 55 bool ok = BindingsSupport::Get()->AsyncWait(message_pipe_, | 51 BindingsSupport::Get()->AsyncWait(message_pipe_, |
| 56 MOJO_WAIT_FLAG_READABLE, | 52 MOJO_WAIT_FLAG_READABLE, |
| 57 MOJO_DEADLINE_INDEFINITE, | 53 &read_callback_)); |
| 58 &read_callback_); | |
| 59 if (!ok) | |
| 60 error_ = true; | |
| 61 } | 54 } |
| 62 | 55 |
| 63 void Connector::WaitToWriteMore() { | 56 void Connector::WaitToWriteMore() { |
| 64 write_callback_.SetOwnerToNotify(this); | 57 write_callback_.SetOwnerToNotify(this); |
| 65 | 58 write_callback_.SetAsyncWaitID( |
| 66 bool ok = BindingsSupport::Get()->AsyncWait(message_pipe_, | 59 BindingsSupport::Get()->AsyncWait(message_pipe_, |
| 67 MOJO_WAIT_FLAG_WRITABLE, | 60 MOJO_WAIT_FLAG_WRITABLE, |
| 68 MOJO_DEADLINE_INDEFINITE, | 61 &write_callback_)); |
| 69 &write_callback_); | |
| 70 if (!ok) | |
| 71 error_ = true; | |
| 72 } | 62 } |
| 73 | 63 |
| 74 void Connector::ReadMore() { | 64 void Connector::ReadMore() { |
| 75 for (;;) { | 65 for (;;) { |
| 76 MojoResult rv; | 66 MojoResult rv; |
| 77 | 67 |
| 78 uint32_t num_bytes = 0, num_handles = 0; | 68 uint32_t num_bytes = 0, num_handles = 0; |
| 79 rv = ReadMessage(message_pipe_, | 69 rv = ReadMessage(message_pipe_, |
| 80 NULL, | 70 NULL, |
| 81 &num_bytes, | 71 &num_bytes, |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 } | 118 } |
| 129 | 119 |
| 130 error_ = true; | 120 error_ = true; |
| 131 break; | 121 break; |
| 132 } | 122 } |
| 133 } | 123 } |
| 134 | 124 |
| 135 // ---------------------------------------------------------------------------- | 125 // ---------------------------------------------------------------------------- |
| 136 | 126 |
| 137 Connector::Callback::Callback() | 127 Connector::Callback::Callback() |
| 138 : owner_(NULL) { | 128 : owner_(NULL), |
| 129 async_wait_id_(0) { |
| 139 } | 130 } |
| 140 | 131 |
| 141 void Connector::Callback::Cancel() { | 132 Connector::Callback::~Callback() { |
| 142 owner_ = NULL; | 133 if (owner_) |
| 143 BindingsSupport::Get()->CancelWait(this); | 134 BindingsSupport::Get()->CancelWait(async_wait_id_); |
| 144 } | 135 } |
| 145 | 136 |
| 146 void Connector::Callback::SetOwnerToNotify(Connector* owner) { | 137 void Connector::Callback::SetOwnerToNotify(Connector* owner) { |
| 147 assert(!owner_); | 138 assert(!owner_); |
| 148 owner_ = owner; | 139 owner_ = owner; |
| 149 } | 140 } |
| 150 | 141 |
| 151 bool Connector::Callback::IsPending() const { | 142 void Connector::Callback::SetAsyncWaitID(BindingsSupport::AsyncWaitID id) { |
| 152 return owner_ != NULL; | 143 async_wait_id_ = id; |
| 153 } | 144 } |
| 154 | 145 |
| 155 void Connector::Callback::OnHandleReady(MojoResult result) { | 146 void Connector::Callback::OnHandleReady(MojoResult result) { |
| 156 assert(owner_); | 147 assert(owner_); |
| 157 Connector* owner = NULL; | 148 Connector* owner = NULL; |
| 158 std::swap(owner, owner_); | 149 std::swap(owner, owner_); |
| 159 owner->OnHandleReady(this, result); | 150 owner->OnHandleReady(this, result); |
| 160 } | 151 } |
| 161 | 152 |
| 162 } // namespace mojo | 153 } // namespace mojo |
| OLD | NEW |