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

Side by Side Diff: mojo/bindings/java/src/org/chromium/mojo/bindings/Connector.java

Issue 371603003: Adding a router class to handle messages that expect responses. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix findbugs issue Created 6 years, 5 months 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 package org.chromium.mojo.bindings; 5 package org.chromium.mojo.bindings;
6 6
7 import org.chromium.mojo.system.AsyncWaiter; 7 import org.chromium.mojo.system.AsyncWaiter;
8 import org.chromium.mojo.system.Core; 8 import org.chromium.mojo.system.Core;
9 import org.chromium.mojo.system.MessagePipeHandle; 9 import org.chromium.mojo.system.MessagePipeHandle;
10 import org.chromium.mojo.system.MojoException; 10 import org.chromium.mojo.system.MojoException;
11 import org.chromium.mojo.system.MojoResult; 11 import org.chromium.mojo.system.MojoResult;
12 12
13 /** 13 /**
14 * A {@link Connector} owns a {@link MessagePipeHandle} and will send any receiv ed messages to the 14 * A {@link Connector} owns a {@link MessagePipeHandle} and will send any receiv ed messages to the
15 * registered {@link MessageReceiver}. It also acts as a {@link MessageReceiver} and will send any 15 * registered {@link MessageReceiver}. It also acts as a {@link MessageReceiver} and will send any
16 * message through the handle. 16 * message through the handle.
17 * <p> 17 * <p>
18 * The method |start| must be called before the {@link Connector} will start lis tening to incoming 18 * The method |start| must be called before the {@link Connector} will start lis tening to incoming
19 * messages. 19 * messages.
20 */ 20 */
21 public class Connector implements MessageReceiver, HandleOwner<MessagePipeHandle > { 21 public class Connector implements MessageReceiver, HandleOwner<MessagePipeHandle > {
22 22
23 /** 23 /**
24 * An {@link ErrorHandler} is notified of error happening while using the me ssage pipe.
25 */
26 interface ErrorHandler {
27 public void onError(MojoException e);
28 }
29
30 /**
31 * The callback that is notified when the state of the owned handle changes. 24 * The callback that is notified when the state of the owned handle changes.
32 */ 25 */
33 private final AsyncWaiterCallback mAsyncWaiterCallback = new AsyncWaiterCall back(); 26 private final AsyncWaiterCallback mAsyncWaiterCallback = new AsyncWaiterCall back();
34 27
35 /** 28 /**
36 * The owned message pipe. 29 * The owned message pipe.
37 */ 30 */
38 private final MessagePipeHandle mMessagePipeHandle; 31 private final MessagePipeHandle mMessagePipeHandle;
39 32
40 /** 33 /**
41 * A waiter which is notified when a new message is available on the owned m essage pipe. 34 * A waiter which is notified when a new message is available on the owned m essage pipe.
42 */ 35 */
43 private final AsyncWaiter mAsyncWaiter; 36 private final AsyncWaiter mAsyncWaiter;
44 37
45 /** 38 /**
46 * The {@link MessageReceiver} to which received messages are sent. 39 * The {@link MessageReceiver} to which received messages are sent.
47 */ 40 */
48 private MessageReceiver mIncomingMessageReceiver; 41 private MessageReceiver mIncomingMessageReceiver;
49 42
50 /** 43 /**
51 * The Cancellable for the current wait. Is |null| when not currently waitin g for new messages. 44 * The Cancellable for the current wait. Is |null| when not currently waitin g for new messages.
52 */ 45 */
53 private AsyncWaiter.Cancellable mCancellable; 46 private AsyncWaiter.Cancellable mCancellable;
54 47
55 /** 48 /**
56 * The error handler to notify of errors. 49 * The error handler to notify of errors.
57 */ 50 */
58 private ErrorHandler mErrorHandler; 51 private ConnectionErrorHandler mErrorHandler;
59 52
60 /** 53 /**
61 * Create a new connector over a |messagePipeHandle|. The created connector will use the default 54 * Create a new connector over a |messagePipeHandle|. The created connector will use the default
62 * {@link AsyncWaiter} from the {@link Core} implementation of |messagePipeH andle|. 55 * {@link AsyncWaiter} from the {@link Core} implementation of |messagePipeH andle|.
63 */ 56 */
64 public Connector(MessagePipeHandle messagePipeHandle) { 57 public Connector(MessagePipeHandle messagePipeHandle) {
65 this(messagePipeHandle, getDefaultAsyncWaiterForMessagePipe(messagePipeH andle)); 58 this(messagePipeHandle, BindingsHelper.getDefaultAsyncWaiterForHandle(me ssagePipeHandle));
66 } 59 }
67 60
68 /** 61 /**
69 * Create a new connector over a |messagePipeHandle| using the given {@link AsyncWaiter} to get 62 * Create a new connector over a |messagePipeHandle| using the given {@link AsyncWaiter} to get
70 * notified of changes on the handle. 63 * notified of changes on the handle.
71 */ 64 */
72 public Connector(MessagePipeHandle messagePipeHandle, AsyncWaiter asyncWaite r) { 65 public Connector(MessagePipeHandle messagePipeHandle, AsyncWaiter asyncWaite r) {
73 mCancellable = null; 66 mCancellable = null;
74 mMessagePipeHandle = messagePipeHandle; 67 mMessagePipeHandle = messagePipeHandle;
75 mAsyncWaiter = asyncWaiter; 68 mAsyncWaiter = asyncWaiter;
76 } 69 }
77 70
78 /** 71 /**
79 * Set the {@link MessageReceiver} that will receive message from the owned message pipe. 72 * Set the {@link MessageReceiver} that will receive message from the owned message pipe.
80 */ 73 */
81 public void setIncomingMessageReceiver(MessageReceiver incomingMessageReceiv er) { 74 public void setIncomingMessageReceiver(MessageReceiver incomingMessageReceiv er) {
82 mIncomingMessageReceiver = incomingMessageReceiver; 75 mIncomingMessageReceiver = incomingMessageReceiver;
83 } 76 }
84 77
85 /** 78 /**
86 * Set the {@link ErrorHandler} that will be notified of errors on the owned message pipe. 79 * Set the {@link ConnectionErrorHandler} that will be notified of errors on the owned message
80 * pipe.
87 */ 81 */
88 public void setErrorHandler(ErrorHandler errorHandler) { 82 public void setErrorHandler(ConnectionErrorHandler errorHandler) {
89 mErrorHandler = errorHandler; 83 mErrorHandler = errorHandler;
90 } 84 }
91 85
92 /** 86 /**
93 * Start listening for incoming messages. 87 * Start listening for incoming messages.
94 */ 88 */
95 public void start() { 89 public void start() {
96 assert mCancellable == null; 90 assert mCancellable == null;
97 registerAsyncWaiterForRead(); 91 registerAsyncWaiterForRead();
98 } 92 }
99 93
100 /** 94 /**
101 * @see MessageReceiver#accept(Message) 95 * @see MessageReceiver#accept(MessageWithHeader)
102 */ 96 */
103 @Override 97 @Override
104 public boolean accept(Message message) { 98 public boolean accept(MessageWithHeader message) {
105 try { 99 try {
106 mMessagePipeHandle.writeMessage(message.buffer, message.handles, 100 mMessagePipeHandle.writeMessage(message.getMessage().buffer,
107 MessagePipeHandle.WriteFlags.NONE); 101 message.getMessage().handles, MessagePipeHandle.WriteFlags.N ONE);
108 return true; 102 return true;
109 } catch (MojoException e) { 103 } catch (MojoException e) {
110 onError(e); 104 onError(e);
111 return false; 105 return false;
112 } 106 }
113 } 107 }
114 108
115 /** 109 /**
116 * Pass the owned handle of the connector. After this, the connector is disc onnected. It cannot 110 * Pass the owned handle of the connector. After this, the connector is disc onnected. It cannot
117 * accept new message and it isn't listening to the handle anymore. 111 * accept new message and it isn't listening to the handle anymore.
118 * 112 *
119 * @see org.chromium.mojo.bindings.HandleOwner#passHandle() 113 * @see org.chromium.mojo.bindings.HandleOwner#passHandle()
120 */ 114 */
121 @Override 115 @Override
122 public MessagePipeHandle passHandle() { 116 public MessagePipeHandle passHandle() {
123 cancelIfActive(); 117 cancelIfActive();
124 return mMessagePipeHandle.pass(); 118 return mMessagePipeHandle.pass();
125 } 119 }
126 120
127 /** 121 /**
128 * @see java.io.Closeable#close() 122 * @see java.io.Closeable#close()
129 */ 123 */
130 @Override 124 @Override
131 public void close() { 125 public void close() {
132 cancelIfActive(); 126 cancelIfActive();
133 mMessagePipeHandle.close(); 127 mMessagePipeHandle.close();
134 } 128 }
135 129
136 private static AsyncWaiter getDefaultAsyncWaiterForMessagePipe(
137 MessagePipeHandle messagePipeHandle) {
138 if (messagePipeHandle.getCore() != null) {
139 return messagePipeHandle.getCore().getDefaultAsyncWaiter();
140 } else {
141 return null;
142 }
143 }
144
145 private class AsyncWaiterCallback implements AsyncWaiter.Callback { 130 private class AsyncWaiterCallback implements AsyncWaiter.Callback {
146 131
147 /** 132 /**
148 * @see org.chromium.mojo.system.AsyncWaiter.Callback#onResult(int) 133 * @see org.chromium.mojo.system.AsyncWaiter.Callback#onResult(int)
149 */ 134 */
150 @Override 135 @Override
151 public void onResult(int result) { 136 public void onResult(int result) {
152 Connector.this.onAsyncWaiterResult(result); 137 Connector.this.onAsyncWaiterResult(result);
153 } 138 }
154 139
(...skipping 16 matching lines...) Expand all
171 readOutstandingMessages(); 156 readOutstandingMessages();
172 } else { 157 } else {
173 onError(new MojoException(result)); 158 onError(new MojoException(result));
174 } 159 }
175 } 160 }
176 161
177 private void onError(MojoException exception) { 162 private void onError(MojoException exception) {
178 mCancellable = null; 163 mCancellable = null;
179 close(); 164 close();
180 if (mErrorHandler != null) { 165 if (mErrorHandler != null) {
181 mErrorHandler.onError(exception); 166 mErrorHandler.onConnectionError(exception);
182 } 167 }
183 } 168 }
184 169
185 /** 170 /**
186 * Register to be called back when a new message is available on the owned m essage pipe. 171 * Register to be called back when a new message is available on the owned m essage pipe.
187 */ 172 */
188 private void registerAsyncWaiterForRead() { 173 private void registerAsyncWaiterForRead() {
189 assert mCancellable == null; 174 assert mCancellable == null;
190 if (mAsyncWaiter != null) { 175 if (mAsyncWaiter != null) {
191 mCancellable = mAsyncWaiter.asyncWait(mMessagePipeHandle, Core.Handl eSignals.READABLE, 176 mCancellable = mAsyncWaiter.asyncWait(mMessagePipeHandle, Core.Handl eSignals.READABLE,
192 Core.DEADLINE_INFINITE, mAsyncWaiterCallback); 177 Core.DEADLINE_INFINITE, mAsyncWaiterCallback);
193 } else { 178 } else {
194 onError(new MojoException(MojoResult.INVALID_ARGUMENT)); 179 onError(new MojoException(MojoResult.INVALID_ARGUMENT));
195 } 180 }
196 } 181 }
197 182
198 /** 183 /**
199 * Read all available messages on the owned message pipe. 184 * Read all available messages on the owned message pipe.
200 */ 185 */
201 private void readOutstandingMessages() { 186 private void readOutstandingMessages() {
202 int result; 187 int result;
203 do { 188 do {
204 try { 189 try {
205 result = Message.readAndDispatchMessage(mMessagePipeHandle, 190 result = MessageWithHeader.readAndDispatchMessage(mMessagePipeHa ndle,
206 mIncomingMessageReceiver); 191 mIncomingMessageReceiver);
207 } catch (MojoException e) { 192 } catch (MojoException e) {
208 onError(e); 193 onError(e);
209 return; 194 return;
210 } 195 }
211 } while (result == MojoResult.OK); 196 } while (result == MojoResult.OK);
212 if (result == MojoResult.SHOULD_WAIT) { 197 if (result == MojoResult.SHOULD_WAIT) {
213 registerAsyncWaiterForRead(); 198 registerAsyncWaiterForRead();
214 } else { 199 } else {
215 onError(new MojoException(result)); 200 onError(new MojoException(result));
216 } 201 }
217 } 202 }
218 203
219 private void cancelIfActive() { 204 private void cancelIfActive() {
220 if (mCancellable != null) { 205 if (mCancellable != null) {
221 mCancellable.cancel(); 206 mCancellable.cancel();
222 mCancellable = null; 207 mCancellable = null;
223 } 208 }
224 } 209 }
225
226 } 210 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698