| OLD | NEW |
| 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 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 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 Loading... |
| 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.WaitF
lags.READABLE, | 176 mCancellable = mAsyncWaiter.asyncWait(mMessagePipeHandle, Core.WaitF
lags.READABLE, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 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 | 210 } |
| 226 } | |
| OLD | NEW |