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 19 matching lines...) Expand all Loading... |
30 assert(!incoming_receiver_); | 30 assert(!incoming_receiver_); |
31 incoming_receiver_ = receiver; | 31 incoming_receiver_ = receiver; |
32 if (incoming_receiver_) | 32 if (incoming_receiver_) |
33 WaitToReadMore(); | 33 WaitToReadMore(); |
34 } | 34 } |
35 | 35 |
36 bool Connector::Accept(Message* message) { | 36 bool Connector::Accept(Message* message) { |
37 if (error_) | 37 if (error_) |
38 return false; | 38 return false; |
39 | 39 |
40 write_queue_.Push(message); | 40 bool wait_to_write; |
41 WriteMore(); | 41 WriteOne(message, &wait_to_write); |
| 42 |
| 43 if (wait_to_write) { |
| 44 WaitToWriteMore(); |
| 45 if (!error_) |
| 46 write_queue_.Push(message); |
| 47 } |
| 48 |
42 return !error_; | 49 return !error_; |
43 } | 50 } |
44 | 51 |
45 void Connector::OnHandleReady(Callback* callback, MojoResult result) { | 52 void Connector::OnHandleReady(Callback* callback, MojoResult result) { |
46 if (callback == &read_callback_) | 53 if (callback == &read_callback_) |
47 ReadMore(); | 54 ReadMore(); |
48 if (callback == &write_callback_) | 55 if (callback == &write_callback_) |
49 WriteMore(); | 56 WriteMore(); |
50 } | 57 } |
51 | 58 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 if (rv != MOJO_RESULT_OK) { | 111 if (rv != MOJO_RESULT_OK) { |
105 error_ = true; | 112 error_ = true; |
106 break; | 113 break; |
107 } | 114 } |
108 | 115 |
109 incoming_receiver_->Accept(&message); | 116 incoming_receiver_->Accept(&message); |
110 } | 117 } |
111 } | 118 } |
112 | 119 |
113 void Connector::WriteMore() { | 120 void Connector::WriteMore() { |
114 while (!write_queue_.IsEmpty()) { | 121 while (!error_ && !write_queue_.IsEmpty()) { |
115 const Message* message = write_queue_.Peek(); | 122 Message* message = write_queue_.Peek(); |
116 | 123 |
117 MojoResult rv = WriteMessage(message_pipe_, | 124 bool wait_to_write; |
118 message->data, | 125 WriteOne(message, &wait_to_write); |
119 message->data->header.num_bytes, | 126 if (wait_to_write) |
120 message->handles.data(), | 127 break; |
121 message->handles.size(), | |
122 MOJO_WRITE_MESSAGE_FLAG_NONE); | |
123 if (rv == MOJO_RESULT_OK) { | |
124 // TODO(darin): Handles were successfully transferred, and so we need | |
125 // to take care not to Close them here. | |
126 write_queue_.Pop(); | |
127 continue; // Write another message. | |
128 } | |
129 | 128 |
130 error_ = true; | 129 write_queue_.Pop(); |
131 break; | |
132 } | 130 } |
133 } | 131 } |
134 | 132 |
| 133 void Connector::WriteOne(Message* message, bool* wait_to_write) { |
| 134 // TODO(darin): WriteMessage will eventually start generating an error that |
| 135 // it cannot accept more data. In that case, we'll need to wait on the pipe |
| 136 // to determine when we can try writing again. This flag will be set to true |
| 137 // in that case. |
| 138 *wait_to_write = false; |
| 139 |
| 140 MojoResult rv = WriteMessage(message_pipe_, |
| 141 message->data, |
| 142 message->data->header.num_bytes, |
| 143 message->handles.data(), |
| 144 static_cast<uint32_t>(message->handles.size()), |
| 145 MOJO_WRITE_MESSAGE_FLAG_NONE); |
| 146 if (rv == MOJO_RESULT_OK) { |
| 147 // The handles were successfully transferred, so we don't need the message |
| 148 // to track their lifetime any longer. |
| 149 message->handles.clear(); |
| 150 } else { |
| 151 error_ = true; |
| 152 } |
| 153 } |
| 154 |
135 // ---------------------------------------------------------------------------- | 155 // ---------------------------------------------------------------------------- |
136 | 156 |
137 Connector::Callback::Callback() | 157 Connector::Callback::Callback() |
138 : owner_(NULL) { | 158 : owner_(NULL) { |
139 } | 159 } |
140 | 160 |
141 void Connector::Callback::Cancel() { | 161 void Connector::Callback::Cancel() { |
142 owner_ = NULL; | 162 owner_ = NULL; |
143 BindingsSupport::Get()->CancelWait(this); | 163 BindingsSupport::Get()->CancelWait(this); |
144 } | 164 } |
145 | 165 |
146 void Connector::Callback::SetOwnerToNotify(Connector* owner) { | 166 void Connector::Callback::SetOwnerToNotify(Connector* owner) { |
147 assert(!owner_); | 167 assert(!owner_); |
148 owner_ = owner; | 168 owner_ = owner; |
149 } | 169 } |
150 | 170 |
151 bool Connector::Callback::IsPending() const { | 171 bool Connector::Callback::IsPending() const { |
152 return owner_ != NULL; | 172 return owner_ != NULL; |
153 } | 173 } |
154 | 174 |
155 void Connector::Callback::OnHandleReady(MojoResult result) { | 175 void Connector::Callback::OnHandleReady(MojoResult result) { |
156 assert(owner_); | 176 assert(owner_); |
157 Connector* owner = NULL; | 177 Connector* owner = NULL; |
158 std::swap(owner, owner_); | 178 std::swap(owner, owner_); |
159 owner->OnHandleReady(this, result); | 179 owner->OnHandleReady(this, result); |
160 } | 180 } |
161 | 181 |
162 } // namespace mojo | 182 } // namespace mojo |
OLD | NEW |