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