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 81cbc9871e756207cc046272f375ce74183c2d7d..91ba791473da873abd923a924eff88b3bb3ad53a 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 |
| @@ -5,7 +5,6 @@ |
| package org.chromium.mojo.bindings; |
| import org.chromium.mojo.bindings.Interface.Proxy; |
| -import org.chromium.mojo.bindings.Struct.DataHeader; |
| import org.chromium.mojo.system.DataPipe; |
| import org.chromium.mojo.system.Handle; |
| import org.chromium.mojo.system.InvalidHandle; |
| @@ -114,11 +113,42 @@ public class Decoder { |
| } |
| /** |
| - * Deserializes a {@link DataHeader} at the given offset. |
| + * Deserializes a {@link DataHeader} at the current position. |
| */ |
| public DataHeader readDataHeader() { |
| + DataHeader result = readDataHeaderAtOffset(0); |
| + // Claim the remaining memory. |
| + mValidator.claimMemory(mBaseOffset + DataHeader.HEADER_SIZE, mBaseOffset + result.size); |
| + return result; |
| + } |
| + |
| + /** |
| + * Deserializes a {@link DataHeader} for an union at the given offset. |
| + */ |
| + public DataHeader readDataHeaderForUnion(int offset) { |
| + DataHeader result = readDataHeaderAtOffset(offset); |
| + if (result.size == 0) { |
| + if (result.elementsOrVersion != 0) { |
|
azani
2015/05/19 21:13:23
The design doc doesn't specify an error when the t
qsr
2015/05/20 09:22:36
That's not correct though. 0 is a valid value for
|
| + throw new DeserializationException( |
| + "Unexpected tag for a null union. Expecting 0, found: " |
| + + result.elementsOrVersion); |
| + } |
| + } else if (result.size != BindingsHelper.UNION_SIZE) { |
| + throw new DeserializationException( |
| + "Unexpected size of an union. The size must be 0 for a null union, and 16 for " |
|
azani
2015/05/19 21:13:23
nit: "or 16 for..."
qsr
2015/05/20 09:22:36
Done.
|
| + + "a non-null union."); |
| + } |
| + mValidator.claimMemory(mBaseOffset + offset + DataHeader.HEADER_SIZE, |
| + mBaseOffset + offset + BindingsHelper.UNION_SIZE); |
| + return result; |
| + } |
| + |
| + /** |
| + * Deserializes a {@link DataHeader} at the given offset. |
| + */ |
| + private DataHeader readDataHeaderAtOffset(int offset) { |
| // Claim the memory for the header. |
| - mValidator.claimMemory(mBaseOffset, mBaseOffset + DataHeader.HEADER_SIZE); |
| + mValidator.claimMemory(mBaseOffset + offset, mBaseOffset + offset + DataHeader.HEADER_SIZE); |
| int size = readInt(DataHeader.SIZE_OFFSET); |
| int elementsOrVersion = readInt(DataHeader.ELEMENTS_OR_VERSION_OFFSET); |
| if (size < 0) { |
| @@ -130,10 +160,7 @@ public class Decoder { |
| "Negative elements or version. Unsigned integers are not valid for java."); |
| } |
| - // Claim the remaining memory. |
| - mValidator.claimMemory(mBaseOffset + DataHeader.HEADER_SIZE, mBaseOffset + size); |
| - DataHeader res = new DataHeader(size, elementsOrVersion); |
| - return res; |
| + return new DataHeader(size, elementsOrVersion); |
| } |
| public DataHeader readAndValidateDataHeader(DataHeader[] versionArray) { |
| @@ -163,10 +190,18 @@ public class Decoder { |
| /** |
| * Deserializes a {@link DataHeader} at the given offset and checks if it is correct for an |
| - * array where element have the given size. |
| + * array where elements are pointers. |
| */ |
| public DataHeader readDataHeaderForPointerArray(int expectedLength) { |
| - return readDataHeaderForArray(8, expectedLength); |
| + return readDataHeaderForArray(BindingsHelper.POINTER_SIZE, expectedLength); |
| + } |
| + |
| + /** |
| + * Deserializes a {@link DataHeader} at the given offset and checks if it is correct for an |
| + * array where elements are unions. |
| + */ |
| + public DataHeader readDataHeaderForUnionArray(int expectedLength) { |
| + return readDataHeaderForArray(BindingsHelper.UNION_SIZE, expectedLength); |
| } |
| /** |