Chromium Code Reviews| Index: mojo/public/js/codec.js |
| diff --git a/mojo/public/js/codec.js b/mojo/public/js/codec.js |
| index b78aac230eba14394d4b071dfcfc4be74e4c0659..e0554cd8c812f3e821f4e4dde0c4e198a0dcc1c7 100644 |
| --- a/mojo/public/js/codec.js |
| +++ b/mojo/public/js/codec.js |
| @@ -43,9 +43,10 @@ define("mojo/public/js/codec", [ |
| // Decoder ------------------------------------------------------------------ |
| - function Decoder(buffer, handles, base) { |
| + function Decoder(buffer, handles, base, associatedEndpointHandles) { |
|
yzshen1
2017/04/19 21:11:34
nit: it may make sense to re-order the parameters
wangjimmy
2017/04/20 15:36:44
Done.
|
| this.buffer = buffer; |
| this.handles = handles; |
| + this.associatedEndpointHandles = associatedEndpointHandles; |
| this.base = base; |
| this.next = base; |
| } |
| @@ -129,13 +130,22 @@ define("mojo/public/js/codec", [ |
| }; |
| Decoder.prototype.decodeAndCreateDecoder = function(pointer) { |
| - return new Decoder(this.buffer, this.handles, pointer); |
| + return new Decoder(this.buffer, this.handles, pointer, |
| + this.associatedEndpointHandles); |
| }; |
| Decoder.prototype.decodeHandle = function() { |
| return this.handles[this.readUint32()] || null; |
| }; |
| + Decoder.prototype.decodeAssociatedEndpointHandle = function() { |
| + if (this.associatedEndpointHandles) { |
|
yzshen1
2017/04/19 21:11:34
Under what circumstances decodeAssociatedEndpointH
wangjimmy
2017/04/20 15:36:43
Yes. Before a new Decoder could be created without
|
| + var index = this.readUint32(); |
| + return this.associatedEndpointHandles[index] || null; |
| + } |
| + return null; |
| + }; |
| + |
| Decoder.prototype.decodeString = function() { |
| var numberOfBytes = this.readUint32(); |
| var numberOfElements = this.readUint32(); |
| @@ -214,9 +224,10 @@ define("mojo/public/js/codec", [ |
| // Encoder ------------------------------------------------------------------ |
| - function Encoder(buffer, handles, base) { |
| + function Encoder(buffer, handles, base, associatedEndpointHandles) { |
|
yzshen1
2017/04/19 21:11:34
nit: it may make sense to re-order the parameters
wangjimmy
2017/04/20 15:36:43
Done.
|
| this.buffer = buffer; |
| this.handles = handles; |
| + this.associatedEndpointHandles = associatedEndpointHandles; |
| this.base = base; |
| this.next = base; |
| } |
| @@ -303,7 +314,8 @@ define("mojo/public/js/codec", [ |
| Encoder.prototype.createAndEncodeEncoder = function(size) { |
| var pointer = this.buffer.alloc(align(size)); |
| this.encodePointer(pointer); |
| - return new Encoder(this.buffer, this.handles, pointer); |
| + return new Encoder(this.buffer, this.handles, pointer, |
| + this.associatedEndpointHandles); |
| }; |
| Encoder.prototype.encodeHandle = function(handle) { |
| @@ -315,6 +327,15 @@ define("mojo/public/js/codec", [ |
| } |
| }; |
| + Encoder.prototype.encodeAssociatedEndpointHandle = function(endpointHandle) { |
| + if (this.associatedEndpointHandles && endpointHandle) { |
|
yzshen1
2017/04/19 21:11:34
Under what circumstances we would want to encode a
wangjimmy
2017/04/20 15:36:43
Removed. It shouldn't happen. I had a check becaus
|
| + this.associatedEndpointHandles.push(endpointHandle); |
| + this.writeUint32(this.associatedEndpointHandles.length - 1); |
| + } else { |
| + this.writeUint32(kEncodedInvalidHandleValue); |
| + } |
| + }; |
| + |
| Encoder.prototype.encodeString = function(val) { |
| var base = this.next + kArrayHeaderSize; |
| var numberOfElements = unicode.encodeUtf8String( |
| @@ -436,9 +457,14 @@ define("mojo/public/js/codec", [ |
| var kMessageExpectsResponse = 1 << 0; |
| var kMessageIsResponse = 1 << 1; |
| - function Message(buffer, handles) { |
| + function Message(buffer, handles, associatedEndpointHandles) { |
| + if (associatedEndpointHandles === undefined) { |
| + associatedEndpointHandles = []; |
| + } |
| + |
| this.buffer = buffer; |
| this.handles = handles; |
| + this.associatedEndpointHandles = associatedEndpointHandles; |
| } |
| Message.prototype.getHeaderNumBytes = function() { |
| @@ -489,6 +515,63 @@ define("mojo/public/js/codec", [ |
| this.buffer.setUint32(kMessageInterfaceIdOffset, interfaceId); |
| }; |
| + Message.prototype.setPayloadInterfaceIds = function(payloadInterfaceIds) { |
|
yzshen1
2017/04/19 21:11:34
Is this supposed to be a private method? If yes pl
wangjimmy
2017/04/20 15:36:43
Done.
|
| + if (this.getHeaderVersion() < 2) { |
| + throw new Error( |
| + "Version of message does not support payload interface ids."); |
| + } |
| + |
| + var decoder = new Decoder(this.buffer, this.handles, |
| + kMessageV2HeaderSize-8); |
|
yzshen1
2017/04/19 21:11:34
I thought you have defined a constant for this off
wangjimmy
2017/04/20 15:36:43
Done. Used it in new patch. Thanks for the catch.
|
| + var payloadInterfaceIdsOffset = decoder.decodePointer(); |
| + var encoder = new Encoder(this.buffer, this.handles, |
| + payloadInterfaceIdsOffset); |
| + encoder.encodeArray(Uint32, payloadInterfaceIds); |
| + }; |
| + |
| + Message.prototype.serializeAssociatedEndpointHandles = function( |
| + associatedGroupController) { |
| + if (this.associatedEndpointHandles.length > 0) { |
| + if (this.getHeaderVersion() < 2) { |
| + throw new Error(); |
|
yzshen1
2017/04/19 21:11:34
Please give a description.
wangjimmy
2017/04/20 15:36:43
Done.
|
| + } |
| + |
| + var data = []; |
| + for (var i = 0; i < this.associatedEndpointHandles.length; i++) { |
| + var handle = this.associatedEndpointHandles[i]; |
| + data.push(associatedGroupController.associateInterface(handle)); |
| + } |
| + this.associatedEndpointHandles = []; |
| + this.setPayloadInterfaceIds(data); |
| + } |
| + }; |
| + |
| + Message.prototype.deserializeAssociatedEndpointHandles = function( |
| + associatedGroupController) { |
| + if (this.getHeaderVersion() < 2) { |
| + return true; |
| + } |
| + |
| + this.associatedEndpointHandles = []; |
| + var ids = this.getPayloadInterfaceIds(); |
| + |
| + var result = true; |
| + for (var i = 0; i < ids.length; i++) { |
| + var handle = associatedGroupController.createLocalEndpointHandle(ids[i]); |
| + if (types.isValidInterfaceId(ids[i]) && !handle.isValid()) { |
| + // |ids[i]| itself is valid but handle creation failed. In that case, |
| + // mark deserialization as failed but continue to deserialize the |
| + // rest of handles. |
| + result = false; |
| + } |
| + this.associatedEndpointHandles.push(handle); |
| + ids[i] = types.kInvalidInterfaceId; |
| + } |
| + |
| + this.setPayloadInterfaceIds(ids); |
| + return result; |
| + }; |
| + |
| // MessageBuilder ----------------------------------------------------------- |
| @@ -552,10 +635,65 @@ define("mojo/public/js/codec", [ |
| MessageWithRequestIDBuilder.prototype.constructor = |
| MessageWithRequestIDBuilder; |
| + // MessageV2 ----------------------------------------------- |
| + |
| + function MessageV2Builder(messageName, payloadSize, flags, requestID) { |
| + // Currently, we don't compute the payload size correctly ahead of time. |
| + // Instead, we resize the buffer at the end. |
| + var numberOfBytes = kMessageV2HeaderSize + payloadSize; |
| + this.buffer = new buffer.Buffer(numberOfBytes); |
| + this.handles = []; |
| + |
| + this.payload = null; |
| + this.associatedEndpointHandles = []; |
| + |
| + this.encoder = this.createEncoder(kMessageV2HeaderSize); |
| + this.encoder.writeUint32(kMessageV2HeaderSize); |
| + this.encoder.writeUint32(2); // version. |
| + // Gets set to an appropriate interfaceId for the endpoint by the IEC. |
|
yzshen1
2017/04/19 21:11:34
Please change IEC to the full name.
wangjimmy
2017/04/20 15:36:43
Done.
|
| + this.encoder.writeUint32(0); // interface ID.. |
|
yzshen1
2017/04/19 21:11:34
Please remove a redundant '.'
wangjimmy
2017/04/20 15:36:43
Done.
|
| + this.encoder.writeUint32(messageName); |
| + this.encoder.writeUint32(flags); |
| + this.encoder.writeUint32(0); // padding. |
| + this.encoder.writeUint64(requestID); |
| + } |
| + |
| + MessageV2Builder.prototype.createEncoder = function(size) { |
| + var pointer = this.buffer.alloc(size); |
| + return new Encoder(this.buffer, this.handles, pointer, |
| + this.associatedEndpointHandles); |
| + }; |
| + |
| + MessageV2Builder.prototype.setPayload = function(cls, val) { |
| + this.payload = {cls: cls, val: val}; |
| + }; |
| + |
| + MessageV2Builder.prototype.finish = function() { |
| + if (!this.payload) { |
| + throw new Error(); |
|
yzshen1
2017/04/19 21:11:34
Please add a description.
wangjimmy
2017/04/20 15:36:43
Done.
|
| + } |
| + |
| + this.encoder.encodeStructPointer(this.payload.cls, this.payload.val); |
| + this.encoder.encodeArrayPointer(Uint32, |
| + new Array(this.associatedEndpointHandles.length)); |
| + |
| + this.buffer.trim(); |
| + var message = new Message(this.buffer, this.handles, |
| + this.associatedEndpointHandles); |
| + this.buffer = null; |
| + this.handles = null; |
| + this.encoder = null; |
| + this.payload = null; |
| + this.associatedEndpointHandles = null; |
| + |
| + return message; |
| + }; |
| + |
| // MessageReader ------------------------------------------------------------ |
| function MessageReader(message) { |
| - this.decoder = new Decoder(message.buffer, message.handles, 0); |
| + this.decoder = new Decoder(message.buffer, message.handles, 0, |
| + message.associatedEndpointHandles); |
| var messageHeaderSize = this.decoder.readUint32(); |
| this.payloadSize = message.buffer.byteLength - messageHeaderSize; |
| var version = this.decoder.readUint32(); |
| @@ -858,12 +996,31 @@ define("mojo/public/js/codec", [ |
| AssociatedInterfacePtrInfo.prototype.encodedSize = 8; |
| + AssociatedInterfacePtrInfo.decode = function(decoder) { |
| + return new types.AssociatedInterfacePtrInfo( |
| + decoder.decodeAssociatedEndpointHandle(), decoder.readUint32()); |
| + }; |
| + |
| + AssociatedInterfacePtrInfo.encode = function(encoder, val) { |
| + var associatedinterfacePtrInfo = |
| + val ? val : new types.AssociatedInterfacePtrInfo(null, 0); |
| + encoder.encodeAssociatedEndpointHandle( |
| + associatedinterfacePtrInfo.interfaceEndpointHandle); |
| + encoder.writeUint32(associatedinterfacePtrInfo.version); |
| + }; |
| + |
| function NullableAssociatedInterfacePtrInfo() { |
| } |
| NullableAssociatedInterfacePtrInfo.encodedSize = |
| AssociatedInterfacePtrInfo.encodedSize; |
| + NullableAssociatedInterfacePtrInfo.decode = |
| + AssociatedInterfacePtrInfo.decode; |
| + |
| + NullableAssociatedInterfacePtrInfo.encode = |
| + AssociatedInterfacePtrInfo.encode; |
| + |
| function InterfaceRequest() { |
| } |
| @@ -889,6 +1046,16 @@ define("mojo/public/js/codec", [ |
| function AssociatedInterfaceRequest() { |
| } |
| + AssociatedInterfaceRequest.decode = function(decoder) { |
| + var handle = decoder.decodeAssociatedEndpointHandle(); |
| + return new types.AssociatedInterfaceRequest(handle); |
| + }; |
| + |
| + AssociatedInterfaceRequest.encode = function(encoder, val) { |
| + encoder.encodeAssociatedEndpointHandle( |
| + val ? val.interfaceEndpointHandle : null); |
| + }; |
| + |
| AssociatedInterfaceRequest.encodedSize = 4; |
| function NullableAssociatedInterfaceRequest() { |
| @@ -897,6 +1064,12 @@ define("mojo/public/js/codec", [ |
| NullableAssociatedInterfaceRequest.encodedSize = |
| AssociatedInterfaceRequest.encodedSize; |
| + NullableAssociatedInterfaceRequest.decode = |
| + AssociatedInterfaceRequest.decode; |
| + |
| + NullableAssociatedInterfaceRequest.encode = |
| + AssociatedInterfaceRequest.encode; |
| + |
| function MapOf(keyClass, valueClass) { |
| this.keyClass = keyClass; |
| this.valueClass = valueClass; |
| @@ -923,6 +1096,7 @@ define("mojo/public/js/codec", [ |
| exports.isAligned = isAligned; |
| exports.Message = Message; |
| exports.MessageBuilder = MessageBuilder; |
| + exports.MessageV2Builder = MessageV2Builder; |
| exports.MessageWithRequestIDBuilder = MessageWithRequestIDBuilder; |
| exports.MessageReader = MessageReader; |
| exports.kArrayHeaderSize = kArrayHeaderSize; |