Index: mojo/bindings/java/src/org/chromium/mojo/bindings/Message.java |
diff --git a/mojo/bindings/java/src/org/chromium/mojo/bindings/Message.java b/mojo/bindings/java/src/org/chromium/mojo/bindings/Message.java |
index 07bd2bd2e4697516242c73ef017cb10fbe4a198b..ee687070127fff4b5f6b896de6c61c493a6584d4 100644 |
--- a/mojo/bindings/java/src/org/chromium/mojo/bindings/Message.java |
+++ b/mojo/bindings/java/src/org/chromium/mojo/bindings/Message.java |
@@ -6,6 +6,8 @@ |
import org.chromium.mojo.system.Handle; |
import org.chromium.mojo.system.MessagePipeHandle; |
+import org.chromium.mojo.system.MessagePipeHandle.ReadMessageResult; |
+import org.chromium.mojo.system.MojoResult; |
import java.nio.ByteBuffer; |
import java.util.List; |
@@ -36,4 +38,26 @@ |
this.buffer = buffer; |
this.handles = handles; |
} |
+ |
+ /** |
+ * Read a message, and pass it to the given |MessageReceiver| if not null. If the |
+ * |MessageReceiver| is null, the message is lost. |
+ * |
+ * @param receiver The {@link MessageReceiver} that will receive the read {@link Message}. Can |
+ * be <code>null</code>, in which case the message is discarded. |
+ */ |
+ public static int readAndDispatchMessage(MessagePipeHandle handle, MessageReceiver receiver) { |
+ // TODO(qsr) Allow usage of a pool of pre-allocated buffer for performance. |
+ ReadMessageResult result = handle.readMessage(null, 0, MessagePipeHandle.ReadFlags.NONE); |
+ if (result.getMojoResult() != MojoResult.RESOURCE_EXHAUSTED) { |
+ return result.getMojoResult(); |
+ } |
+ ByteBuffer buffer = ByteBuffer.allocateDirect(result.getMessageSize()); |
+ result = handle.readMessage(buffer, result.getHandlesCount(), |
+ MessagePipeHandle.ReadFlags.NONE); |
+ if (receiver != null && result.getMojoResult() == MojoResult.OK) { |
+ receiver.accept(new Message(buffer, result.getHandles())); |
+ } |
+ return result.getMojoResult(); |
+ } |
} |