| Index: mojo/public/java/bindings/src/org/chromium/mojo/bindings/Encoder.java
|
| diff --git a/mojo/public/java/bindings/src/org/chromium/mojo/bindings/Encoder.java b/mojo/public/java/bindings/src/org/chromium/mojo/bindings/Encoder.java
|
| index b13c6fb6a1e910e3db32a308aa4b0823825d9fcc..dbe7d9865b82d9eb8a3205630252b64a529e4add 100644
|
| --- a/mojo/public/java/bindings/src/org/chromium/mojo/bindings/Encoder.java
|
| +++ b/mojo/public/java/bindings/src/org/chromium/mojo/bindings/Encoder.java
|
| @@ -209,9 +209,9 @@ public class Encoder {
|
| /**
|
| * Encode a {@link Struct} at the given offset.
|
| */
|
| - public void encode(Struct v, int offset) {
|
| + public void encode(Struct v, int offset, boolean nullable) {
|
| if (v == null) {
|
| - encodeNullPointer(offset);
|
| + encodeNullPointer(offset, nullable);
|
| return;
|
| }
|
| encodePointerToNextUnclaimedData(offset);
|
| @@ -221,21 +221,23 @@ public class Encoder {
|
| /**
|
| * Encodes a String.
|
| */
|
| - public void encode(String v, int offset) {
|
| + public void encode(String v, int offset, boolean nullable) {
|
| if (v == null) {
|
| - encodeNullPointer(offset);
|
| + encodeNullPointer(offset, nullable);
|
| return;
|
| }
|
| - encode(v.getBytes(
|
| - Charset.forName("utf8")), offset, BindingsHelper.UNSPECIFIED_ARRAY_LENGTH);
|
| + final int arrayNullability = nullable ?
|
| + BindingsHelper.ARRAY_NULLABLE : BindingsHelper.NOTHING_NULLABLE;
|
| + encode(v.getBytes(Charset.forName("utf8")), offset, arrayNullability,
|
| + BindingsHelper.UNSPECIFIED_ARRAY_LENGTH);
|
| }
|
|
|
| /**
|
| * Encodes a {@link Handle}.
|
| */
|
| - public void encode(Handle v, int offset) {
|
| + public void encode(Handle v, int offset, boolean nullable) {
|
| if (v == null || !v.isValid()) {
|
| - encode(-1, offset);
|
| + encodeInvalidHandle(offset, nullable);
|
| } else {
|
| encode(mEncoderState.handles.size(), offset);
|
| mEncoderState.handles.add(v);
|
| @@ -245,9 +247,10 @@ public class Encoder {
|
| /**
|
| * Encode an {@link Interface}.
|
| */
|
| - public <T extends Interface> void encode(T v, int offset, Interface.Manager<T, ?> manager) {
|
| + public <T extends Interface> void encode(T v, int offset, boolean nullable,
|
| + Interface.Manager<T, ?> manager) {
|
| if (v == null) {
|
| - encode(-1, offset);
|
| + encodeInvalidHandle(offset, nullable);
|
| return;
|
| }
|
| if (mEncoderState.core == null) {
|
| @@ -258,7 +261,8 @@ public class Encoder {
|
| if (v instanceof Interface.AbstractProxy) {
|
| Interface.AbstractProxy proxy = (Interface.AbstractProxy) v;
|
| if (proxy.getMessageReceiver() instanceof HandleOwner) {
|
| - encode(((HandleOwner<?>) proxy.getMessageReceiver()).passHandle(), offset);
|
| + encode(((HandleOwner<?>) proxy.getMessageReceiver()).passHandle(), offset,
|
| + nullable);
|
| return;
|
| }
|
| // If the proxy is not over a message pipe, the default case applies.
|
| @@ -266,22 +270,22 @@ public class Encoder {
|
| Pair<MessagePipeHandle, MessagePipeHandle> handles =
|
| mEncoderState.core.createMessagePipe(null);
|
| manager.bind(v, handles.first);
|
| - encode(handles.second, offset);
|
| + encode(handles.second, offset, nullable);
|
| }
|
|
|
| /**
|
| * Encode an {@link InterfaceRequest}.
|
| */
|
| - public <I extends Interface> void encode(InterfaceRequest<I> v, int offset) {
|
| + public <I extends Interface> void encode(InterfaceRequest<I> v, int offset, boolean nullable) {
|
| if (v == null) {
|
| - encode(-1, offset);
|
| + encodeInvalidHandle(offset, nullable);
|
| return;
|
| }
|
| if (mEncoderState.core == null) {
|
| throw new UnsupportedOperationException(
|
| "The encoder has been created without a Core. It can't encode an interface.");
|
| }
|
| - encode(v.passHandle(), offset);
|
| + encode(v.passHandle(), offset, nullable);
|
| }
|
|
|
| /**
|
| @@ -294,9 +298,9 @@ public class Encoder {
|
| /**
|
| * Encodes an array of booleans.
|
| */
|
| - public void encode(boolean[] v, int offset, int expectedLength) {
|
| + public void encode(boolean[] v, int offset, int arrayNullability, int expectedLength) {
|
| if (v == null) {
|
| - encodeNullPointer(offset);
|
| + encodeNullPointer(offset, BindingsHelper.isArrayNullable(arrayNullability));
|
| return;
|
| }
|
| if (expectedLength != BindingsHelper.UNSPECIFIED_ARRAY_LENGTH &&
|
| @@ -318,9 +322,9 @@ public class Encoder {
|
| /**
|
| * Encodes an array of bytes.
|
| */
|
| - public void encode(byte[] v, int offset, int expectedLength) {
|
| + public void encode(byte[] v, int offset, int arrayNullability, int expectedLength) {
|
| if (v == null) {
|
| - encodeNullPointer(offset);
|
| + encodeNullPointer(offset, BindingsHelper.isArrayNullable(arrayNullability));
|
| return;
|
| }
|
| if (expectedLength != BindingsHelper.UNSPECIFIED_ARRAY_LENGTH &&
|
| @@ -333,9 +337,9 @@ public class Encoder {
|
| /**
|
| * Encodes an array of shorts.
|
| */
|
| - public void encode(short[] v, int offset, int expectedLength) {
|
| + public void encode(short[] v, int offset, int arrayNullability, int expectedLength) {
|
| if (v == null) {
|
| - encodeNullPointer(offset);
|
| + encodeNullPointer(offset, BindingsHelper.isArrayNullable(arrayNullability));
|
| return;
|
| }
|
| encoderForArray(2, v.length, offset, expectedLength).append(v);
|
| @@ -344,9 +348,9 @@ public class Encoder {
|
| /**
|
| * Encodes an array of ints.
|
| */
|
| - public void encode(int[] v, int offset, int expectedLength) {
|
| + public void encode(int[] v, int offset, int arrayNullability, int expectedLength) {
|
| if (v == null) {
|
| - encodeNullPointer(offset);
|
| + encodeNullPointer(offset, BindingsHelper.isArrayNullable(arrayNullability));
|
| return;
|
| }
|
| encoderForArray(4, v.length, offset, expectedLength).append(v);
|
| @@ -355,9 +359,9 @@ public class Encoder {
|
| /**
|
| * Encodes an array of floats.
|
| */
|
| - public void encode(float[] v, int offset, int expectedLength) {
|
| + public void encode(float[] v, int offset, int arrayNullability, int expectedLength) {
|
| if (v == null) {
|
| - encodeNullPointer(offset);
|
| + encodeNullPointer(offset, BindingsHelper.isArrayNullable(arrayNullability));
|
| return;
|
| }
|
| encoderForArray(4, v.length, offset, expectedLength).append(v);
|
| @@ -366,9 +370,9 @@ public class Encoder {
|
| /**
|
| * Encodes an array of longs.
|
| */
|
| - public void encode(long[] v, int offset, int expectedLength) {
|
| + public void encode(long[] v, int offset, int arrayNullability, int expectedLength) {
|
| if (v == null) {
|
| - encodeNullPointer(offset);
|
| + encodeNullPointer(offset, BindingsHelper.isArrayNullable(arrayNullability));
|
| return;
|
| }
|
| encoderForArray(8, v.length, offset, expectedLength).append(v);
|
| @@ -377,9 +381,9 @@ public class Encoder {
|
| /**
|
| * Encodes an array of doubles.
|
| */
|
| - public void encode(double[] v, int offset, int expectedLength) {
|
| + public void encode(double[] v, int offset, int arrayNullability, int expectedLength) {
|
| if (v == null) {
|
| - encodeNullPointer(offset);
|
| + encodeNullPointer(offset, BindingsHelper.isArrayNullable(arrayNullability));
|
| return;
|
| }
|
| encoderForArray(8, v.length, offset, expectedLength).append(v);
|
| @@ -388,32 +392,33 @@ public class Encoder {
|
| /**
|
| * Encodes an array of {@link Handle}.
|
| */
|
| - public void encode(Handle[] v, int offset, int expectedLength) {
|
| + public void encode(Handle[] v, int offset, int arrayNullability, int expectedLength) {
|
| if (v == null) {
|
| - encodeNullPointer(offset);
|
| + encodeNullPointer(offset, BindingsHelper.isArrayNullable(arrayNullability));
|
| return;
|
| }
|
| Encoder e = encoderForArray(
|
| BindingsHelper.SERIALIZED_HANDLE_SIZE, v.length, offset, expectedLength);
|
| for (int i = 0; i < v.length; ++i) {
|
| - e.encode(v[i], DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SIZE * i);
|
| + e.encode(v[i], DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SIZE * i,
|
| + BindingsHelper.isElementNullable(arrayNullability));
|
| }
|
| }
|
|
|
| /**
|
| * Encodes an array of {@link Interface}.
|
| */
|
| - public <T extends Interface> void encode(T[] v, int offset, int expectedLength,
|
| - Interface.Manager<T, ?> manager) {
|
| + public <T extends Interface> void encode(T[] v, int offset, int arrayNullability,
|
| + int expectedLength, Interface.Manager<T, ?> manager) {
|
| if (v == null) {
|
| - encodeNullPointer(offset);
|
| + encodeNullPointer(offset, BindingsHelper.isArrayNullable(arrayNullability));
|
| return;
|
| }
|
| Encoder e = encoderForArray(
|
| BindingsHelper.SERIALIZED_HANDLE_SIZE, v.length, offset, expectedLength);
|
| for (int i = 0; i < v.length; ++i) {
|
| e.encode(v[i], DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SIZE * i,
|
| - manager);
|
| + BindingsHelper.isElementNullable(arrayNullability), manager);
|
| }
|
| }
|
|
|
| @@ -421,25 +426,42 @@ public class Encoder {
|
| * Encodes an array of {@link InterfaceRequest}.
|
| */
|
| public <I extends Interface> void encode(InterfaceRequest<I>[] v, int offset,
|
| - int expectedLength) {
|
| + int arrayNullability, int expectedLength) {
|
| if (v == null) {
|
| - encodeNullPointer(offset);
|
| + encodeNullPointer(offset, BindingsHelper.isArrayNullable(arrayNullability));
|
| return;
|
| }
|
| Encoder e = encoderForArray(
|
| BindingsHelper.SERIALIZED_HANDLE_SIZE, v.length, offset, expectedLength);
|
| for (int i = 0; i < v.length; ++i) {
|
| - e.encode(v[i], DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SIZE * i);
|
| + e.encode(v[i], DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SIZE * i,
|
| + BindingsHelper.isElementNullable(arrayNullability));
|
| }
|
| }
|
|
|
| /**
|
| - * Encode a <code>null</code> pointer.
|
| + * Encodes a <code>null</code> pointer iff the object is nullable, raises an exception
|
| + * otherwise.
|
| */
|
| - public void encodeNullPointer(int offset) {
|
| + public void encodeNullPointer(int offset, boolean nullable) {
|
| + if (!nullable) {
|
| + throw new SerializationException(
|
| + "Trying to encode a null pointer for a non-nullable type.");
|
| + }
|
| mEncoderState.byteBuffer.putLong(mBaseOffset + offset, 0);
|
| }
|
|
|
| + /**
|
| + * Encodes an invalid handle iff the object is nullable, raises an exception otherwise.
|
| + */
|
| + public void encodeInvalidHandle(int offset, boolean nullable) {
|
| + if (!nullable) {
|
| + throw new SerializationException(
|
| + "Trying to encode an invalid handle for a non-nullable type.");
|
| + }
|
| + mEncoderState.byteBuffer.putInt(mBaseOffset + offset, -1);
|
| + }
|
| +
|
| private void encodePointerToNextUnclaimedData(int offset) {
|
| encode((long) mEncoderState.dataEnd - (mBaseOffset + offset), offset);
|
| }
|
|
|