| Index: mojo/bindings/java/src/org/chromium/mojo/bindings/MessageHeader.java
|
| diff --git a/mojo/bindings/java/src/org/chromium/mojo/bindings/MessageHeader.java b/mojo/bindings/java/src/org/chromium/mojo/bindings/MessageHeader.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..5d637ad846a82c8a74bea4119564db53d35a282b
|
| --- /dev/null
|
| +++ b/mojo/bindings/java/src/org/chromium/mojo/bindings/MessageHeader.java
|
| @@ -0,0 +1,177 @@
|
| +// 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.bindings.Struct.DataHeader;
|
| +
|
| +/**
|
| + * Header information for a message.
|
| + */
|
| +public class MessageHeader {
|
| +
|
| + /**
|
| + * The struct header of a message header that doesn't represent a method with return values.
|
| + */
|
| + private static final DataHeader SIMPLE_MESSAGE_STRUCT_INFO = new DataHeader(16, 2);
|
| +
|
| + /**
|
| + * The struct header of a message header that represents a method with return values.
|
| + */
|
| + private static final DataHeader MESSAGE_WITH_REQUEST_ID_STRUCT_INFO = new DataHeader(24, 3);
|
| +
|
| + /**
|
| + * Flag for a header of a message that expected a response.
|
| + */
|
| + public static final int MESSAGE_EXPECTS_RESPONSE_FLAG = 1 << 0;
|
| +
|
| + /**
|
| + * Flag for a header of a message that is a response.
|
| + */
|
| + public static final int MESSAGE_IS_RESPONSE_FLAG = 1 << 1;
|
| +
|
| + /**
|
| + * return whether a message with the given flags must have a request Id.
|
| + */
|
| + public static boolean mustHaveRequestId(int flags) {
|
| + return (flags & (MESSAGE_EXPECTS_RESPONSE_FLAG | MESSAGE_IS_RESPONSE_FLAG)) != 0;
|
| + }
|
| +
|
| + private final DataHeader mDataHeader;
|
| + private final int mType;
|
| + private final int mFlags;
|
| + private long mRequestId;
|
| +
|
| + /**
|
| + * Constructor, parsing the header from a message.
|
| + */
|
| + public MessageHeader(Message message) {
|
| + Decoder decoder = new Decoder(message);
|
| + mDataHeader = decoder.readDataHeader();
|
| + if (mDataHeader.numFields < 2) {
|
| + throw new DeserializationException(
|
| + "Incorrect number of fields, expecting at least 2, but got: "
|
| + + mDataHeader.numFields);
|
| + }
|
| + if (mDataHeader.size < SIMPLE_MESSAGE_STRUCT_INFO.size) {
|
| + throw new DeserializationException(
|
| + "Incorrect message size, expecting at least " + SIMPLE_MESSAGE_STRUCT_INFO.size
|
| + + ", but got: " + mDataHeader.size);
|
| + }
|
| + mType = decoder.readInt(8);
|
| + mFlags = decoder.readInt(12);
|
| + if (mustHaveRequestId(mFlags)) {
|
| + if (mDataHeader.size < MESSAGE_WITH_REQUEST_ID_STRUCT_INFO.size) {
|
| + throw new DeserializationException("Incorrect message size, expecting at least "
|
| + + MESSAGE_WITH_REQUEST_ID_STRUCT_INFO.size + ", but got: "
|
| + + mDataHeader.size);
|
| +
|
| + }
|
| + mRequestId = decoder.readLong(16);
|
| + } else {
|
| + mRequestId = 0;
|
| + }
|
| + }
|
| +
|
| + /**
|
| + * Constructor for the header of a message not having a response.
|
| + */
|
| + public MessageHeader(int type, int flags) {
|
| + assert !mustHaveRequestId(flags);
|
| + mDataHeader = SIMPLE_MESSAGE_STRUCT_INFO;
|
| + mType = type;
|
| + mFlags = flags;
|
| + mRequestId = 0;
|
| + }
|
| +
|
| + /**
|
| + * Constructor for the header of a message not having response or the response of a message.
|
| + */
|
| + public MessageHeader(int type, int flags, long requestId) {
|
| + assert mustHaveRequestId(flags);
|
| + mDataHeader = MESSAGE_WITH_REQUEST_ID_STRUCT_INFO;
|
| + mType = type;
|
| + mFlags = flags;
|
| + mRequestId = requestId;
|
| + }
|
| +
|
| + public int getSize() {
|
| + return mDataHeader.size;
|
| + }
|
| +
|
| + /**
|
| + * Returns the type of the message.
|
| + */
|
| + public int getType() {
|
| + return mType;
|
| + }
|
| +
|
| + /**
|
| + * Returns the flags associated to the message.
|
| + */
|
| + public int getFlags() {
|
| + return mFlags;
|
| + }
|
| +
|
| + /**
|
| + * Returns if the message has the given flag.
|
| + */
|
| + public boolean hasFlag(int flag) {
|
| + return (mFlags & flag) == flag;
|
| + }
|
| +
|
| + /**
|
| + * Returns if the message has a request id.
|
| + */
|
| + public boolean hasRequestId() {
|
| + return mustHaveRequestId(mFlags);
|
| + }
|
| +
|
| + /**
|
| + * Return the request id for the message. Must only be called if the message has a request id.
|
| + */
|
| + public long getRequestId() {
|
| + assert hasRequestId();
|
| + return mRequestId;
|
| + }
|
| +
|
| + /**
|
| + * Encode the header.
|
| + */
|
| + public void encode(Encoder encoder) {
|
| + encoder.encode(mDataHeader);
|
| + encoder.encode(getType(), 8);
|
| + encoder.encode(getFlags(), 12);
|
| + if (mustHaveRequestId(getFlags())) {
|
| + encoder.encode(getRequestId(), 16);
|
| + }
|
| + }
|
| +
|
| + /**
|
| + * Returns if the header has the expected flags. Only consider flags this class knows about to
|
| + * allow working with future version of the header format.
|
| + */
|
| + public boolean validateHeader(int expectedFlags) {
|
| + int knownFlags = getFlags() & (MESSAGE_EXPECTS_RESPONSE_FLAG | MESSAGE_IS_RESPONSE_FLAG);
|
| + return knownFlags == expectedFlags;
|
| + }
|
| +
|
| + /**
|
| + * Returns if the header has the expected type and flags. Only consider flags this class knows
|
| + * about to allow working with future version of the header format.
|
| + */
|
| + public boolean validateHeader(int expectedType, int expectedFlags) {
|
| + return getType() == expectedType && validateHeader(expectedFlags);
|
| + }
|
| +
|
| + /**
|
| + * Set the given request id on the given message. Should only be called if the message requires
|
| + * a request id.
|
| + */
|
| + public static void setRequestId(Message message, long requestId) {
|
| + assert new MessageHeader(message).hasRequestId();
|
| + message.buffer.putLong(16, requestId);
|
| + }
|
| +
|
| +}
|
|
|