summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorqsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-07-01 08:56:54 +0000
committerqsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-07-01 08:56:54 +0000
commit7eed2ec6c797a1e1556ceb954cfe52f8876ef816 (patch)
treeeb34bfe702553a83e3a916f8a1c950a137a9f6a8
parentdd8fa9056d20c93c07ec3ff1ff19bec9f4a81921 (diff)
downloadchromium_src-7eed2ec6c797a1e1556ceb954cfe52f8876ef816.zip
chromium_src-7eed2ec6c797a1e1556ceb954cfe52f8876ef816.tar.gz
chromium_src-7eed2ec6c797a1e1556ceb954cfe52f8876ef816.tar.bz2
Add Message and MessageReceiver classes.
Those classes are utility class to handle mojo messages. R=viettrungluu@chromium.org,rmcilroy@chromium.org Review URL: https://codereview.chromium.org/311363003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@280785 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--mojo/android/javatests/src/org/chromium/mojo/TestUtils.java30
-rw-r--r--mojo/android/javatests/src/org/chromium/mojo/bindings/MessageTest.java123
-rw-r--r--mojo/bindings/java/src/org/chromium/mojo/bindings/Message.java63
-rw-r--r--mojo/bindings/java/src/org/chromium/mojo/bindings/MessageReceiver.java17
-rw-r--r--mojo/mojo_public.gypi7
5 files changed, 240 insertions, 0 deletions
diff --git a/mojo/android/javatests/src/org/chromium/mojo/TestUtils.java b/mojo/android/javatests/src/org/chromium/mojo/TestUtils.java
new file mode 100644
index 0000000..ad13ca9
--- /dev/null
+++ b/mojo/android/javatests/src/org/chromium/mojo/TestUtils.java
@@ -0,0 +1,30 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.mojo;
+
+import java.nio.ByteBuffer;
+import java.util.Random;
+
+/**
+ * Utilities methods for tests.
+ */
+public final class TestUtils {
+
+ private static final Random RANDOM = new Random();
+
+ /**
+ * Returns a new direct ByteBuffer of the given size with random (but reproducible) data.
+ */
+ public static ByteBuffer newRandomBuffer(int size) {
+ byte bytes[] = new byte[size];
+ RANDOM.setSeed(size);
+ RANDOM.nextBytes(bytes);
+ ByteBuffer data = ByteBuffer.allocateDirect(size);
+ data.put(bytes);
+ data.flip();
+ return data;
+ }
+
+}
diff --git a/mojo/android/javatests/src/org/chromium/mojo/bindings/MessageTest.java b/mojo/android/javatests/src/org/chromium/mojo/bindings/MessageTest.java
new file mode 100644
index 0000000..ee58979
--- /dev/null
+++ b/mojo/android/javatests/src/org/chromium/mojo/bindings/MessageTest.java
@@ -0,0 +1,123 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.mojo.bindings;
+
+import android.test.suitebuilder.annotation.SmallTest;
+
+import org.chromium.mojo.MojoTestCase;
+import org.chromium.mojo.TestUtils;
+import org.chromium.mojo.system.Core;
+import org.chromium.mojo.system.DataPipe;
+import org.chromium.mojo.system.Handle;
+import org.chromium.mojo.system.MessagePipeHandle;
+import org.chromium.mojo.system.MojoException;
+import org.chromium.mojo.system.MojoResult;
+import org.chromium.mojo.system.Pair;
+import org.chromium.mojo.system.impl.CoreImpl;
+
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Testing {@link Message}.
+ */
+public class MessageTest extends MojoTestCase {
+
+ static class RecordingMessageReceiver implements MessageReceiver {
+
+ public final List<Message> messages = new ArrayList<Message>();
+
+ /**
+ * @see MessageReceiver#accept(Message)
+ */
+ @Override
+ public boolean accept(Message message) {
+ messages.add(message);
+ return true;
+ }
+ }
+
+ private static final int DATA_SIZE = 1024;
+
+ private ByteBuffer mData;
+ private Pair<MessagePipeHandle, MessagePipeHandle> mHandles;
+ private List<Handle> mHandlesToSend = new ArrayList<Handle>();
+ private List<Handle> mHandlesToClose = new ArrayList<Handle>();
+ private RecordingMessageReceiver mMessageReceiver;
+
+ /**
+ * @see org.chromium.mojo.MojoTestCase#setUp()
+ */
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ Core core = CoreImpl.getInstance();
+ mData = TestUtils.newRandomBuffer(DATA_SIZE);
+ mMessageReceiver = new RecordingMessageReceiver();
+ mHandles = core.createMessagePipe();
+ Pair<DataPipe.ProducerHandle, DataPipe.ConsumerHandle> datapipe = core.createDataPipe(null);
+ mHandlesToSend.addAll(Arrays.asList(datapipe.first, datapipe.second));
+ mHandlesToClose.addAll(Arrays.asList(mHandles.first, mHandles.second));
+ mHandlesToClose.addAll(mHandlesToSend);
+ }
+
+ /**
+ * @see org.chromium.mojo.MojoTestCase#tearDown()
+ */
+ @Override
+ protected void tearDown() throws Exception {
+ for (Handle handle : mHandlesToClose) {
+ handle.close();
+ }
+ super.tearDown();
+ }
+
+ /**
+ * Testing {@link Message#readAndDispatchMessage(MessagePipeHandle, MessageReceiver)}
+ */
+ @SmallTest
+ public void testReadAndDispatchMessage() {
+ mHandles.first.writeMessage(mData, mHandlesToSend, MessagePipeHandle.WriteFlags.NONE);
+ assertEquals(MojoResult.OK,
+ Message.readAndDispatchMessage(mHandles.second, mMessageReceiver));
+ assertEquals(1, mMessageReceiver.messages.size());
+ Message message = mMessageReceiver.messages.get(0);
+ mHandlesToClose.addAll(message.handles);
+ assertEquals(mData, message.buffer);
+ assertEquals(2, message.handles.size());
+ for (Handle handle : message.handles) {
+ assertTrue(handle.isValid());
+ }
+ }
+
+ /**
+ * Testing {@link Message#readAndDispatchMessage(MessagePipeHandle, MessageReceiver)} with no
+ * message available.
+ */
+ @SmallTest
+ public void testReadAndDispatchMessageOnEmptyHandle() {
+ assertEquals(MojoResult.SHOULD_WAIT,
+ Message.readAndDispatchMessage(mHandles.second, mMessageReceiver));
+ assertEquals(0, mMessageReceiver.messages.size());
+ }
+
+ /**
+ * Testing {@link Message#readAndDispatchMessage(MessagePipeHandle, MessageReceiver)} on closed
+ * handle.
+ */
+ @SmallTest
+ public void testReadAndDispatchMessageOnClosedHandle() {
+ mHandles.first.close();
+ try {
+ Message.readAndDispatchMessage(mHandles.second, mMessageReceiver);
+ fail("MojoException should have been thrown");
+ } catch (MojoException expected) {
+ assertEquals(MojoResult.FAILED_PRECONDITION, expected.getMojoResult());
+ }
+ assertEquals(0, mMessageReceiver.messages.size());
+ }
+}
diff --git a/mojo/bindings/java/src/org/chromium/mojo/bindings/Message.java b/mojo/bindings/java/src/org/chromium/mojo/bindings/Message.java
new file mode 100644
index 0000000..f826a8e
--- /dev/null
+++ b/mojo/bindings/java/src/org/chromium/mojo/bindings/Message.java
@@ -0,0 +1,63 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.mojo.bindings;
+
+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;
+
+import javax.annotation.Nullable;
+
+/**
+ * A raw message to be sent/received from a {@link MessagePipeHandle}.
+ */
+public final class Message {
+
+ /**
+ * The data of the message.
+ */
+ public final ByteBuffer buffer;
+
+ /**
+ * The handles of the message.
+ */
+ public final List<? extends Handle> handles;
+
+ /**
+ * Constructor.
+ *
+ * @param buffer The buffer containing the bytes to send. This must be a direct buffer.
+ * @param handles The list of handles to send.
+ */
+ public Message(ByteBuffer buffer, List<? extends Handle> handles) {
+ assert buffer.isDirect();
+ 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.
+ */
+ public static int readAndDispatchMessage(MessagePipeHandle handle,
+ @Nullable 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();
+ }
+}
diff --git a/mojo/bindings/java/src/org/chromium/mojo/bindings/MessageReceiver.java b/mojo/bindings/java/src/org/chromium/mojo/bindings/MessageReceiver.java
new file mode 100644
index 0000000..1736bc6
--- /dev/null
+++ b/mojo/bindings/java/src/org/chromium/mojo/bindings/MessageReceiver.java
@@ -0,0 +1,17 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.mojo.bindings;
+
+/**
+ * A class which implements this interface can receive {@link Message} objects.
+ */
+public interface MessageReceiver {
+
+ /**
+ * Receive a {@link Message}. The {@link MessageReceiver} is allowed to mutable the message.
+ * Returns |true| if the message has been handled, |false| otherwise.
+ */
+ boolean accept(Message message);
+}
diff --git a/mojo/mojo_public.gypi b/mojo/mojo_public.gypi
index 8f3c9d9..6797821a4 100644
--- a/mojo/mojo_public.gypi
+++ b/mojo/mojo_public.gypi
@@ -426,6 +426,13 @@
'java_in_dir': 'public/java',
},
'includes': [ '../build/java.gypi' ],
+ 'conditions': [
+ ['android_webview_build==0', {
+ 'dependencies': [
+ '../third_party/jsr-305/jsr-305.gyp:jsr_305_javalib',
+ ],
+ }]
+ ],
},
{
'target_name': 'mojo_bindings_java',