Chromium Code Reviews| 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..47cc32fe9db0b26f4493b9526d4bf65d2a558c86 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,22 @@ 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 : 0; |
|
qsr
2014/09/02 07:41:44
You have a constant instead of this 0 in BindingsH
ppi
2014/09/02 16:50:43
Done.
|
| + 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()) { |
|
ppi
2014/09/01 19:00:45
What do we do if the field is not nullable, v != n
qsr
2014/09/02 07:41:44
Non nullable for a handle means valid, so this cas
ppi
2014/09/02 16:50:43
Acknowledged.
|
| - encode(-1, offset); |
| + encodeInvalidHandle(offset, nullable); |
| } else { |
| encode(mEncoderState.handles.size(), offset); |
| mEncoderState.handles.add(v); |
| @@ -245,9 +246,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 +260,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 +269,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 +297,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 +321,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 +336,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 +347,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 +358,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 +369,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 +380,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 +391,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 +425,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); |
| } |