Chromium Code Reviews| Index: mojo/public/java/bindings/src/org/chromium/mojo/bindings/Decoder.java |
| diff --git a/mojo/public/java/bindings/src/org/chromium/mojo/bindings/Decoder.java b/mojo/public/java/bindings/src/org/chromium/mojo/bindings/Decoder.java |
| index 02b5c9f129e93e410046a17c60c9045f1a0dc15a..483338c4d609a6de69097aca7fbdb5ece5a7d32a 100644 |
| --- a/mojo/public/java/bindings/src/org/chromium/mojo/bindings/Decoder.java |
| +++ b/mojo/public/java/bindings/src/org/chromium/mojo/bindings/Decoder.java |
| @@ -41,10 +41,16 @@ public class Decoder { |
| private final long mMaxMemory; |
| /** |
| + * The number of handles in the message. |
| + */ |
| + private final long mNumberOfHandles; |
| + |
| + /** |
| * Constructor. |
| */ |
| - Validator(long maxMemory) { |
| + Validator(long maxMemory, int numberOfHandles) { |
| mMaxMemory = maxMemory; |
| + mNumberOfHandles = numberOfHandles; |
| } |
| public void claimHandle(int handle) { |
| @@ -52,6 +58,9 @@ public class Decoder { |
| throw new DeserializationException( |
| "Trying to access handle out of order."); |
| } |
| + if (handle >= mNumberOfHandles) { |
| + throw new DeserializationException("Trying to access non present handle."); |
| + } |
| mMinNextClaimedHandle = handle + 1; |
| } |
| @@ -93,7 +102,7 @@ public class Decoder { |
| * @param message The message to decode. |
| */ |
| public Decoder(Message message) { |
| - this(message, new Validator(message.buffer.limit()), 0); |
| + this(message, new Validator(message.buffer.limit(), message.handles.size()), 0); |
| } |
| private Decoder(Message message, Validator validator, int baseOffset) { |
| @@ -319,44 +328,53 @@ public class Decoder { |
| * Deserializes a |ConsumerHandle| at the given offset. |
| */ |
| public DataPipe.ConsumerHandle readConsumerHandle(int offset) { |
| - return readHandle(offset).toUntypedHandle().toDataPipeConsumerHandle(); |
| + return readUntypedHandle(offset).toDataPipeConsumerHandle(); |
| } |
| /** |
| * Deserializes a |ProducerHandle| at the given offset. |
| */ |
| public DataPipe.ProducerHandle readProducerHandle(int offset) { |
| - return readHandle(offset).toUntypedHandle().toDataPipeProducerHandle(); |
| + return readUntypedHandle(offset).toDataPipeProducerHandle(); |
| } |
| /** |
| * Deserializes a |MessagePipeHandle| at the given offset. |
| */ |
| public MessagePipeHandle readMessagePipeHandle(int offset) { |
| - return readHandle(offset).toUntypedHandle().toMessagePipeHandle(); |
| + return readUntypedHandle(offset).toMessagePipeHandle(); |
| } |
| /** |
| * Deserializes a |SharedBufferHandle| at the given offset. |
| */ |
| public SharedBufferHandle readSharedBufferHandle(int offset) { |
| - return readHandle(offset).toUntypedHandle().toSharedBufferHandle(); |
| + return readUntypedHandle(offset).toSharedBufferHandle(); |
| } |
| /** |
| - * Deserializes a |ServiceHandle| at the given offset. |
| + * Deserializes an interface at the given offset. |
|
rmcilroy
2014/08/21 14:43:52
remove extra space
qsr
2014/08/22 08:11:18
Done.
|
| + * |
| + * @return a proxy to the service. |
| */ |
| - public <S extends Interface> S readServiceInterface(int offset, Object builder) { |
| - // TODO(qsr): To be implemented when interfaces proxy and stubs are implemented. |
| - throw new UnsupportedOperationException("Unimplemented operation"); |
| + public <S extends Interface> S readServiceInterface(int offset, |
| + Interface.Builder<S, ? extends S> builder) { |
| + MessagePipeHandle handle = readMessagePipeHandle(offset); |
| + if (!handle.isValid()) { |
| + return null; |
| + } |
| + return builder.connect(handle); |
| } |
| /** |
| * Deserializes a |InterfaceRequest| at the given offset. |
| */ |
| - public <S extends Interface> InterfaceRequest<S> readInterfaceRequest(int offset) { |
| - // TODO(qsr): To be implemented when interfaces proxy and stubs are implemented. |
| - throw new UnsupportedOperationException("Unimplemented operation"); |
| + public <P extends Interface.Proxy> InterfaceRequest<P> readInterfaceRequest(int offset) { |
| + MessagePipeHandle handle = readMessagePipeHandle(offset); |
| + if (handle == null) { |
| + return null; |
| + } |
| + return new InterfaceRequest<P>(handle); |
| } |
| /** |
| @@ -388,6 +406,23 @@ public class Decoder { |
| } |
| /** |
| + * Deserializes an array of |UntypedHandle| at the given offset. |
| + */ |
| + public UntypedHandle[] readUntypedHandles(int offset) { |
| + Decoder d = readPointer(offset); |
| + if (d == null) { |
| + return null; |
| + } |
| + DataHeader si = d.readDataHeader(); |
| + UntypedHandle[] result = new UntypedHandle[si.numFields]; |
| + for (int i = 0; i < result.length; ++i) { |
| + result[i] = d.readUntypedHandle( |
| + DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SIZE * i); |
| + } |
| + return result; |
| + } |
| + |
| + /** |
| * Deserializes an array of |ConsumerHandle| at the given offset. |
| */ |
| public DataPipe.ConsumerHandle[] readConsumerHandles(int offset) { |
| @@ -461,22 +496,32 @@ public class Decoder { |
| /** |
| * Deserializes an array of |ServiceHandle| at the given offset. |
| */ |
| - public <S extends Interface> S[] readServiceInterfaces(int offset, Object builder) { |
| - // TODO(qsr): To be implemented when interfaces proxy and stubs are implemented. |
| - throw new UnsupportedOperationException("Unimplemented operation"); |
| + public <S extends Interface> S[] readServiceInterfaces(int offset, |
| + Interface.Builder<S, ? extends S> builder) { |
| + Decoder d = readPointer(offset); |
| + if (d == null) { |
| + return null; |
| + } |
| + DataHeader si = d.readDataHeader(); |
| + S[] result = builder.newArray(si.numFields); |
| + for (int i = 0; i < result.length; ++i) { |
| + result[i] = d.readServiceInterface( |
| + DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SIZE * i, builder); |
| + } |
| + return result; |
| } |
| /** |
| * Deserializes an array of |InterfaceRequest| at the given offset. |
| */ |
| - public <S extends Interface> InterfaceRequest<S>[] readInterfaceRequests(int offset) { |
| + public <P extends Interface.Proxy> InterfaceRequest<P>[] readInterfaceRequests(int offset) { |
| Decoder d = readPointer(offset); |
| if (d == null) { |
| return null; |
| } |
| DataHeader si = d.readDataHeader(); |
| @SuppressWarnings("unchecked") |
| - InterfaceRequest<S>[] result = new InterfaceRequest[si.numFields]; |
| + InterfaceRequest<P>[] result = new InterfaceRequest[si.numFields]; |
| for (int i = 0; i < result.length; ++i) { |
| result[i] = d.readInterfaceRequest( |
| DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SIZE * i); |