Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(286)

Side by Side Diff: mojo/public/bindings/lib/connector.cc

Issue 54743003: Mojo: bindings connector (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove errant line from mojo.gyp Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « mojo/public/bindings/lib/connector.h ('k') | mojo/public/bindings/lib/message.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "mojo/public/bindings/lib/connector.h"
6
7 #include <assert.h>
8 #include <stdlib.h>
9
10 #include <algorithm>
11
12 namespace mojo {
13
14 // ----------------------------------------------------------------------------
15
16 Connector::Connector(Handle message_pipe)
17 : message_pipe_(message_pipe),
18 incoming_receiver_(NULL),
19 error_(false) {
20 }
21
22 Connector::~Connector() {
23 if (read_callback_.IsPending())
24 read_callback_.Cancel();
25 if (write_callback_.IsPending())
26 write_callback_.Cancel();
27 }
28
29 void Connector::SetIncomingReceiver(MessageReceiver* receiver) {
30 assert(!incoming_receiver_);
31 incoming_receiver_ = receiver;
32 if (incoming_receiver_)
33 WaitToReadMore();
34 }
35
36 bool Connector::Accept(Message* message) {
37 if (error_)
38 return false;
39
40 write_queue_.Push(message);
41 WriteMore();
42 return !error_;
43 }
44
45 void Connector::OnHandleReady(Callback* callback, MojoResult result) {
46 if (callback == &read_callback_)
47 ReadMore();
48 if (callback == &write_callback_)
49 WriteMore();
50 }
51
52 void Connector::WaitToReadMore() {
53 read_callback_.SetOwnerToNotify(this);
54
55 bool ok = BindingsSupport::Get()->AsyncWait(message_pipe_,
56 MOJO_WAIT_FLAG_READABLE,
57 MOJO_DEADLINE_INDEFINITE,
58 &read_callback_);
59 if (!ok)
60 error_ = true;
61 }
62
63 void Connector::WaitToWriteMore() {
64 write_callback_.SetOwnerToNotify(this);
65
66 bool ok = BindingsSupport::Get()->AsyncWait(message_pipe_,
67 MOJO_WAIT_FLAG_WRITABLE,
68 MOJO_DEADLINE_INDEFINITE,
69 &write_callback_);
70 if (!ok)
71 error_ = true;
72 }
73
74 void Connector::ReadMore() {
75 for (;;) {
76 MojoResult rv;
77
78 uint32_t num_bytes = 0, num_handles = 0;
79 rv = ReadMessage(message_pipe_,
80 NULL,
81 &num_bytes,
82 NULL,
83 &num_handles,
84 MOJO_READ_MESSAGE_FLAG_NONE);
85 if (rv == MOJO_RESULT_NOT_FOUND) {
86 WaitToReadMore();
87 break;
88 }
89 if (rv != MOJO_RESULT_RESOURCE_EXHAUSTED) {
90 error_ = true;
91 break;
92 }
93
94 Message message;
95 message.data = static_cast<MessageData*>(malloc(num_bytes));
96 message.handles.resize(num_handles);
97
98 rv = ReadMessage(message_pipe_,
99 message.data,
100 &num_bytes,
101 &message.handles[0],
102 &num_handles,
103 MOJO_READ_MESSAGE_FLAG_NONE);
104 if (rv != MOJO_RESULT_OK) {
105 error_ = true;
106 break;
107 }
108
109 incoming_receiver_->Accept(&message);
110 }
111 }
112
113 void Connector::WriteMore() {
114 while (!write_queue_.IsEmpty()) {
115 const Message* message = write_queue_.Peek();
116
117 MojoResult rv = WriteMessage(message_pipe_,
118 message->data,
119 message->data->header.num_bytes,
120 message->handles.data(),
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
130 error_ = true;
131 break;
132 }
133 }
134
135 // ----------------------------------------------------------------------------
136
137 Connector::Callback::Callback()
138 : owner_(NULL) {
139 }
140
141 void Connector::Callback::Cancel() {
142 owner_ = NULL;
143 BindingsSupport::Get()->CancelWait(this);
144 }
145
146 void Connector::Callback::SetOwnerToNotify(Connector* owner) {
147 assert(!owner_);
148 owner_ = owner;
149 }
150
151 bool Connector::Callback::IsPending() const {
152 return owner_ != NULL;
153 }
154
155 void Connector::Callback::OnHandleReady(MojoResult result) {
156 assert(owner_);
157 Connector* owner = NULL;
158 std::swap(owner, owner_);
159 owner->OnHandleReady(this, result);
160 }
161
162 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/bindings/lib/connector.h ('k') | mojo/public/bindings/lib/message.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698