Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.mojo.bindings; | |
| 6 | |
| 7 import org.chromium.mojo.bindings.Struct.DataHeader; | |
| 8 | |
| 9 /** | |
| 10 * Header information for a message. | |
| 11 */ | |
| 12 public class MessageHeader { | |
| 13 | |
| 14 private static final int SIMPLE_MESSAGE_SIZE = 16; | |
| 15 private static final int MESSAGE_WITH_REQUEST_ID_SIZE = 24; | |
|
rmcilroy
2014/07/10 19:03:58
Group common fields and insert newlines to seperat
qsr
2014/07/11 11:42:08
Done.
| |
| 16 private static final int SIMPLE_MESSAGE_NUM_FIELDS = 2; | |
| 17 private static final int MESSAGE_WITH_REQUEST_ID_NUM_FIELDS = 3; | |
| 18 private static final DataHeader SIMPLE_MESSAGE_STRUCT_INFO = new DataHeader( SIMPLE_MESSAGE_SIZE, | |
|
rmcilroy
2014/07/10 19:03:58
nit - newline at "new" instead of SIMPLE_MESSAGE_N
qsr
2014/07/11 11:42:08
Done.
| |
| 19 SIMPLE_MESSAGE_NUM_FIELDS); | |
| 20 private static final DataHeader MESSAGE_WITH_REQUEST_ID_STRUCT_INFO = new Da taHeader( | |
|
rmcilroy
2014/07/10 19:03:58
ditto
qsr
2014/07/11 11:42:08
Done.
| |
| 21 MESSAGE_WITH_REQUEST_ID_SIZE, MESSAGE_WITH_REQUEST_ID_NUM_FIELDS); | |
| 22 private static final int TYPE_OFFSET = 8; | |
| 23 private static final int FLAGS_OFFSET = 12; | |
| 24 private static final int REQUEST_ID_OFFSET = 16; | |
|
rmcilroy
2014/07/10 19:03:58
newline
qsr
2014/07/11 11:42:08
Done.
| |
| 25 /** | |
| 26 * Flag for a header of a message that expected a response. | |
| 27 */ | |
| 28 public static final int MESSAGE_EXPECTS_RESPONSE_FLAG = 1 << 0; | |
| 29 | |
| 30 /** | |
| 31 * Flag for a header of a message that is a response. | |
| 32 */ | |
| 33 public static final int MESSAGE_IS_RESPONSE_FLAG = 1 << 1; | |
| 34 | |
| 35 private final DataHeader mDataHeader; | |
| 36 private final int mType; | |
| 37 private final int mFlags; | |
| 38 private long mRequestId; | |
| 39 | |
| 40 /** | |
| 41 * Constructor for the header of a message not having a response. | |
|
rmcilroy
2014/07/10 19:03:58
/not having/which does not have
qsr
2014/07/11 11:42:08
Done.
| |
| 42 */ | |
| 43 public MessageHeader(int type, int flags) { | |
| 44 assert !mustHaveRequestId(flags); | |
| 45 mDataHeader = SIMPLE_MESSAGE_STRUCT_INFO; | |
| 46 mType = type; | |
| 47 mFlags = flags; | |
| 48 mRequestId = 0; | |
| 49 } | |
| 50 | |
| 51 /** | |
| 52 * Constructor for the header of a message having a response or being itself a response. | |
|
rmcilroy
2014/07/10 19:03:58
ditto
qsr
2014/07/11 11:42:07
Done.
| |
| 53 */ | |
| 54 public MessageHeader(int type, int flags, long requestId) { | |
| 55 assert mustHaveRequestId(flags); | |
| 56 mDataHeader = MESSAGE_WITH_REQUEST_ID_STRUCT_INFO; | |
| 57 mType = type; | |
| 58 mFlags = flags; | |
| 59 mRequestId = requestId; | |
| 60 } | |
| 61 | |
| 62 /** | |
| 63 * Constructor, parsing the header from a message. Should only be used by {@ link Message} | |
| 64 * itself. | |
| 65 */ | |
| 66 MessageHeader(Message message) { | |
| 67 Decoder decoder = new Decoder(message); | |
| 68 mDataHeader = decoder.readDataHeader(); | |
| 69 if (mDataHeader.numFields < SIMPLE_MESSAGE_NUM_FIELDS) { | |
| 70 throw new DeserializationException( | |
| 71 "Incorrect number of fields, expecting at least " + SIMPLE_M ESSAGE_NUM_FIELDS | |
| 72 + ", but got: " + mDataHeader.numFields); | |
| 73 } | |
| 74 if (mDataHeader.size < SIMPLE_MESSAGE_STRUCT_INFO.size) { | |
| 75 throw new DeserializationException( | |
| 76 "Incorrect message size, expecting at least " + SIMPLE_MESSA GE_STRUCT_INFO.size | |
| 77 + ", but got: " + mDataHeader.size); | |
| 78 } | |
| 79 if (mDataHeader.numFields == 2 && mDataHeader.size != SIMPLE_MESSAGE_STR UCT_INFO.size) { | |
|
rmcilroy
2014/07/10 19:03:58
/s/2/SIMPLE_MESSAGE_NUM_FIELDS (and below for "3")
qsr
2014/07/11 11:42:07
Done.
| |
| 80 throw new DeserializationException( | |
| 81 "Incorrect message size, expecting " + SIMPLE_MESSAGE_STRUCT _INFO.size | |
|
rmcilroy
2014/07/10 19:03:58
Mention that you got a message with 2 fields and u
qsr
2014/07/11 11:42:08
Done.
| |
| 82 + ", but got: " + mDataHeader.size); | |
| 83 } | |
| 84 if (mDataHeader.numFields == 3 | |
| 85 && mDataHeader.size != MESSAGE_WITH_REQUEST_ID_STRUCT_INFO.size) { | |
| 86 throw new DeserializationException( | |
| 87 "Incorrect message size, expecting " + MESSAGE_WITH_REQUEST_ ID_STRUCT_INFO.size | |
| 88 + ", but got: " + mDataHeader.size); | |
| 89 } | |
|
rmcilroy
2014/07/10 19:03:58
I think this would be clearer if you factor out th
qsr
2014/07/11 11:42:08
Done.
| |
| 90 mType = decoder.readInt(TYPE_OFFSET); | |
| 91 mFlags = decoder.readInt(FLAGS_OFFSET); | |
| 92 if (mustHaveRequestId(mFlags)) { | |
| 93 if (mDataHeader.size < MESSAGE_WITH_REQUEST_ID_STRUCT_INFO.size) { | |
| 94 throw new DeserializationException("Incorrect message size, expe cting at least " | |
|
rmcilroy
2014/07/10 19:03:58
Also mention something in the message about why yo
qsr
2014/07/11 11:42:08
Done.
| |
| 95 + MESSAGE_WITH_REQUEST_ID_STRUCT_INFO.size + ", but got: " | |
| 96 + mDataHeader.size); | |
| 97 | |
| 98 } | |
| 99 mRequestId = decoder.readLong(REQUEST_ID_OFFSET); | |
| 100 } else { | |
| 101 mRequestId = 0; | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 public int getSize() { | |
| 106 return mDataHeader.size; | |
| 107 } | |
| 108 | |
| 109 /** | |
| 110 * Returns the type of the message. | |
| 111 */ | |
| 112 public int getType() { | |
| 113 return mType; | |
| 114 } | |
| 115 | |
| 116 /** | |
| 117 * Returns the flags associated to the message. | |
| 118 */ | |
| 119 public int getFlags() { | |
| 120 return mFlags; | |
| 121 } | |
| 122 | |
| 123 /** | |
| 124 * Returns if the message has the given flag. | |
| 125 */ | |
| 126 public boolean hasFlag(int flag) { | |
| 127 return (mFlags & flag) == flag; | |
| 128 } | |
| 129 | |
| 130 /** | |
| 131 * Returns if the message has a request id. | |
| 132 */ | |
| 133 public boolean hasRequestId() { | |
| 134 return mustHaveRequestId(mFlags); | |
| 135 } | |
| 136 | |
| 137 /** | |
| 138 * Return the request id for the message. Must only be called if the message has a request id. | |
| 139 */ | |
| 140 public long getRequestId() { | |
| 141 assert hasRequestId(); | |
| 142 return mRequestId; | |
| 143 } | |
| 144 | |
| 145 /** | |
| 146 * Encode the header. | |
| 147 */ | |
| 148 public void encode(Encoder encoder) { | |
| 149 encoder.encode(mDataHeader); | |
| 150 encoder.encode(getType(), TYPE_OFFSET); | |
| 151 encoder.encode(getFlags(), FLAGS_OFFSET); | |
| 152 if (hasRequestId()) { | |
| 153 encoder.encode(getRequestId(), REQUEST_ID_OFFSET); | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 /** | |
| 158 * Returns if the header has the expected flags. Only consider flags this cl ass knows about to | |
|
rmcilroy
2014/07/10 19:03:58
Returns true if...
/s/consider/considers
qsr
2014/07/11 11:42:08
Done.
| |
| 159 * allow working with future version of the header format. | |
|
rmcilroy
2014/07/10 19:03:58
/s/to allow working/in order to allow this class t
qsr
2014/07/11 11:42:08
Done.
| |
| 160 */ | |
| 161 public boolean validateHeader(int expectedFlags) { | |
| 162 int knownFlags = getFlags() & (MESSAGE_EXPECTS_RESPONSE_FLAG | MESSAGE_I S_RESPONSE_FLAG); | |
| 163 return knownFlags == expectedFlags; | |
| 164 } | |
| 165 | |
| 166 /** | |
| 167 * Returns if the header has the expected type and flags. Only consider flag s this class knows | |
| 168 * about to allow working with future version of the header format. | |
|
rmcilroy
2014/07/10 19:03:58
same changes as validateHeader above
qsr
2014/07/11 11:42:08
Done.
| |
| 169 */ | |
| 170 public boolean validateHeader(int expectedType, int expectedFlags) { | |
| 171 return getType() == expectedType && validateHeader(expectedFlags); | |
| 172 } | |
| 173 | |
| 174 /** | |
| 175 * Set the given request id on the given message. Should only be called if t he message requires | |
| 176 * a request id. | |
| 177 */ | |
| 178 public static void setRequestId(Message message, long requestId) { | |
| 179 assert message.getHeader().hasRequestId(); | |
| 180 message.buffer.putLong(REQUEST_ID_OFFSET, requestId); | |
| 181 // If the message has a cached header, it is now invalid. | |
| 182 message.resetHeader(); | |
| 183 } | |
| 184 | |
| 185 /** | |
| 186 * return whether a message with the given flags must have a request Id. | |
|
rmcilroy
2014/07/10 19:03:58
/s/return/Returns
qsr
2014/07/11 11:42:08
Done.
| |
| 187 */ | |
| 188 private static boolean mustHaveRequestId(int flags) { | |
| 189 return (flags & (MESSAGE_EXPECTS_RESPONSE_FLAG | MESSAGE_IS_RESPONSE_FLA G)) != 0; | |
| 190 } | |
| 191 } | |
| OLD | NEW |