| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 define("mojo/public/js/codec", [ | 5 define("mojo/public/js/codec", [ |
| 6 "mojo/public/js/buffer", | 6 "mojo/public/js/buffer", |
| 7 "mojo/public/js/interface_types", | 7 "mojo/public/js/interface_types", |
| 8 "mojo/public/js/lib/interface_endpoint_handle", | 8 "mojo/public/js/lib/interface_endpoint_handle", |
| 9 "mojo/public/js/unicode", | 9 "mojo/public/js/unicode", |
| 10 ], function(buffer, types, interfaceEndpointHandle, unicode) { | 10 ], function(buffer, types, interfaceEndpointHandle, unicode) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 var kMessageV2HeaderSize = 48; | 36 var kMessageV2HeaderSize = 48; |
| 37 var kMapStructPayloadSize = 16; | 37 var kMapStructPayloadSize = 16; |
| 38 | 38 |
| 39 var kStructHeaderNumBytesOffset = 0; | 39 var kStructHeaderNumBytesOffset = 0; |
| 40 var kStructHeaderVersionOffset = 4; | 40 var kStructHeaderVersionOffset = 4; |
| 41 | 41 |
| 42 var kEncodedInvalidHandleValue = 0xFFFFFFFF; | 42 var kEncodedInvalidHandleValue = 0xFFFFFFFF; |
| 43 | 43 |
| 44 // Decoder ------------------------------------------------------------------ | 44 // Decoder ------------------------------------------------------------------ |
| 45 | 45 |
| 46 function Decoder(buffer, handles, base) { | 46 function Decoder(buffer, handles, associatedEndpointHandles, base) { |
| 47 this.buffer = buffer; | 47 this.buffer = buffer; |
| 48 this.handles = handles; | 48 this.handles = handles; |
| 49 this.associatedEndpointHandles = associatedEndpointHandles; |
| 49 this.base = base; | 50 this.base = base; |
| 50 this.next = base; | 51 this.next = base; |
| 51 } | 52 } |
| 52 | 53 |
| 53 Decoder.prototype.align = function() { | 54 Decoder.prototype.align = function() { |
| 54 this.next = align(this.next); | 55 this.next = align(this.next); |
| 55 }; | 56 }; |
| 56 | 57 |
| 57 Decoder.prototype.skip = function(offset) { | 58 Decoder.prototype.skip = function(offset) { |
| 58 this.next += offset; | 59 this.next += offset; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 // TODO(abarth): To correctly decode a pointer, we need to know the real | 123 // TODO(abarth): To correctly decode a pointer, we need to know the real |
| 123 // base address of the array buffer. | 124 // base address of the array buffer. |
| 124 var offsetPointer = this.next; | 125 var offsetPointer = this.next; |
| 125 var offset = this.readUint64(); | 126 var offset = this.readUint64(); |
| 126 if (!offset) | 127 if (!offset) |
| 127 return 0; | 128 return 0; |
| 128 return offsetPointer + offset; | 129 return offsetPointer + offset; |
| 129 }; | 130 }; |
| 130 | 131 |
| 131 Decoder.prototype.decodeAndCreateDecoder = function(pointer) { | 132 Decoder.prototype.decodeAndCreateDecoder = function(pointer) { |
| 132 return new Decoder(this.buffer, this.handles, pointer); | 133 return new Decoder(this.buffer, this.handles, |
| 134 this.associatedEndpointHandles, pointer); |
| 133 }; | 135 }; |
| 134 | 136 |
| 135 Decoder.prototype.decodeHandle = function() { | 137 Decoder.prototype.decodeHandle = function() { |
| 136 return this.handles[this.readUint32()] || null; | 138 return this.handles[this.readUint32()] || null; |
| 137 }; | 139 }; |
| 138 | 140 |
| 141 Decoder.prototype.decodeAssociatedEndpointHandle = function() { |
| 142 return this.associatedEndpointHandles[this.readUint32()] || null; |
| 143 }; |
| 144 |
| 139 Decoder.prototype.decodeString = function() { | 145 Decoder.prototype.decodeString = function() { |
| 140 var numberOfBytes = this.readUint32(); | 146 var numberOfBytes = this.readUint32(); |
| 141 var numberOfElements = this.readUint32(); | 147 var numberOfElements = this.readUint32(); |
| 142 var base = this.next; | 148 var base = this.next; |
| 143 this.next += numberOfElements; | 149 this.next += numberOfElements; |
| 144 return unicode.decodeUtf8String( | 150 return unicode.decodeUtf8String( |
| 145 new Uint8Array(this.buffer.arrayBuffer, base, numberOfElements)); | 151 new Uint8Array(this.buffer.arrayBuffer, base, numberOfElements)); |
| 146 }; | 152 }; |
| 147 | 153 |
| 148 Decoder.prototype.decodeArray = function(cls) { | 154 Decoder.prototype.decodeArray = function(cls) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 var pointer = this.decodePointer(); | 213 var pointer = this.decodePointer(); |
| 208 if (!pointer) { | 214 if (!pointer) { |
| 209 return null; | 215 return null; |
| 210 } | 216 } |
| 211 var decoder = this.decodeAndCreateDecoder(pointer); | 217 var decoder = this.decodeAndCreateDecoder(pointer); |
| 212 return decoder.decodeMap(keyClass, valueClass); | 218 return decoder.decodeMap(keyClass, valueClass); |
| 213 }; | 219 }; |
| 214 | 220 |
| 215 // Encoder ------------------------------------------------------------------ | 221 // Encoder ------------------------------------------------------------------ |
| 216 | 222 |
| 217 function Encoder(buffer, handles, base) { | 223 function Encoder(buffer, handles, associatedEndpointHandles, base) { |
| 218 this.buffer = buffer; | 224 this.buffer = buffer; |
| 219 this.handles = handles; | 225 this.handles = handles; |
| 226 this.associatedEndpointHandles = associatedEndpointHandles; |
| 220 this.base = base; | 227 this.base = base; |
| 221 this.next = base; | 228 this.next = base; |
| 222 } | 229 } |
| 223 | 230 |
| 224 Encoder.prototype.align = function() { | 231 Encoder.prototype.align = function() { |
| 225 this.next = align(this.next); | 232 this.next = align(this.next); |
| 226 }; | 233 }; |
| 227 | 234 |
| 228 Encoder.prototype.skip = function(offset) { | 235 Encoder.prototype.skip = function(offset) { |
| 229 this.next += offset; | 236 this.next += offset; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 return this.writeUint64(0); | 303 return this.writeUint64(0); |
| 297 // TODO(abarth): To correctly encode a pointer, we need to know the real | 304 // TODO(abarth): To correctly encode a pointer, we need to know the real |
| 298 // base address of the array buffer. | 305 // base address of the array buffer. |
| 299 var offset = pointer - this.next; | 306 var offset = pointer - this.next; |
| 300 this.writeUint64(offset); | 307 this.writeUint64(offset); |
| 301 }; | 308 }; |
| 302 | 309 |
| 303 Encoder.prototype.createAndEncodeEncoder = function(size) { | 310 Encoder.prototype.createAndEncodeEncoder = function(size) { |
| 304 var pointer = this.buffer.alloc(align(size)); | 311 var pointer = this.buffer.alloc(align(size)); |
| 305 this.encodePointer(pointer); | 312 this.encodePointer(pointer); |
| 306 return new Encoder(this.buffer, this.handles, pointer); | 313 return new Encoder(this.buffer, this.handles, |
| 314 this.associatedEndpointHandles, pointer); |
| 307 }; | 315 }; |
| 308 | 316 |
| 309 Encoder.prototype.encodeHandle = function(handle) { | 317 Encoder.prototype.encodeHandle = function(handle) { |
| 310 if (handle) { | 318 if (handle) { |
| 311 this.handles.push(handle); | 319 this.handles.push(handle); |
| 312 this.writeUint32(this.handles.length - 1); | 320 this.writeUint32(this.handles.length - 1); |
| 313 } else { | 321 } else { |
| 314 this.writeUint32(kEncodedInvalidHandleValue); | 322 this.writeUint32(kEncodedInvalidHandleValue); |
| 315 } | 323 } |
| 316 }; | 324 }; |
| 317 | 325 |
| 326 Encoder.prototype.encodeAssociatedEndpointHandle = function(endpointHandle) { |
| 327 if (endpointHandle) { |
| 328 this.associatedEndpointHandles.push(endpointHandle); |
| 329 this.writeUint32(this.associatedEndpointHandles.length - 1); |
| 330 } else { |
| 331 this.writeUint32(kEncodedInvalidHandleValue); |
| 332 } |
| 333 }; |
| 334 |
| 318 Encoder.prototype.encodeString = function(val) { | 335 Encoder.prototype.encodeString = function(val) { |
| 319 var base = this.next + kArrayHeaderSize; | 336 var base = this.next + kArrayHeaderSize; |
| 320 var numberOfElements = unicode.encodeUtf8String( | 337 var numberOfElements = unicode.encodeUtf8String( |
| 321 val, new Uint8Array(this.buffer.arrayBuffer, base)); | 338 val, new Uint8Array(this.buffer.arrayBuffer, base)); |
| 322 var numberOfBytes = kArrayHeaderSize + numberOfElements; | 339 var numberOfBytes = kArrayHeaderSize + numberOfElements; |
| 323 this.writeUint32(numberOfBytes); | 340 this.writeUint32(numberOfBytes); |
| 324 this.writeUint32(numberOfElements); | 341 this.writeUint32(numberOfElements); |
| 325 this.next += numberOfElements; | 342 this.next += numberOfElements; |
| 326 }; | 343 }; |
| 327 | 344 |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 | 446 |
| 430 var kMessageInterfaceIdOffset = kStructHeaderSize; | 447 var kMessageInterfaceIdOffset = kStructHeaderSize; |
| 431 var kMessageNameOffset = kMessageInterfaceIdOffset + 4; | 448 var kMessageNameOffset = kMessageInterfaceIdOffset + 4; |
| 432 var kMessageFlagsOffset = kMessageNameOffset + 4; | 449 var kMessageFlagsOffset = kMessageNameOffset + 4; |
| 433 var kMessageRequestIDOffset = kMessageFlagsOffset + 8; | 450 var kMessageRequestIDOffset = kMessageFlagsOffset + 8; |
| 434 var kMessagePayloadInterfaceIdsPointerOffset = kMessageV2HeaderSize - 8; | 451 var kMessagePayloadInterfaceIdsPointerOffset = kMessageV2HeaderSize - 8; |
| 435 | 452 |
| 436 var kMessageExpectsResponse = 1 << 0; | 453 var kMessageExpectsResponse = 1 << 0; |
| 437 var kMessageIsResponse = 1 << 1; | 454 var kMessageIsResponse = 1 << 1; |
| 438 | 455 |
| 439 function Message(buffer, handles) { | 456 function Message(buffer, handles, associatedEndpointHandles) { |
| 457 if (associatedEndpointHandles === undefined) { |
| 458 associatedEndpointHandles = []; |
| 459 } |
| 460 |
| 440 this.buffer = buffer; | 461 this.buffer = buffer; |
| 441 this.handles = handles; | 462 this.handles = handles; |
| 463 this.associatedEndpointHandles = associatedEndpointHandles; |
| 442 } | 464 } |
| 443 | 465 |
| 444 Message.prototype.getHeaderNumBytes = function() { | 466 Message.prototype.getHeaderNumBytes = function() { |
| 445 return this.buffer.getUint32(kStructHeaderNumBytesOffset); | 467 return this.buffer.getUint32(kStructHeaderNumBytesOffset); |
| 446 }; | 468 }; |
| 447 | 469 |
| 448 Message.prototype.getHeaderVersion = function() { | 470 Message.prototype.getHeaderVersion = function() { |
| 449 return this.buffer.getUint32(kStructHeaderVersionOffset); | 471 return this.buffer.getUint32(kStructHeaderVersionOffset); |
| 450 }; | 472 }; |
| 451 | 473 |
| 452 Message.prototype.getName = function() { | 474 Message.prototype.getName = function() { |
| 453 return this.buffer.getUint32(kMessageNameOffset); | 475 return this.buffer.getUint32(kMessageNameOffset); |
| 454 }; | 476 }; |
| 455 | 477 |
| 456 Message.prototype.getFlags = function() { | 478 Message.prototype.getFlags = function() { |
| 457 return this.buffer.getUint32(kMessageFlagsOffset); | 479 return this.buffer.getUint32(kMessageFlagsOffset); |
| 458 }; | 480 }; |
| 459 | 481 |
| 460 Message.prototype.getInterfaceId = function() { | 482 Message.prototype.getInterfaceId = function() { |
| 461 return this.buffer.getUint32(kMessageInterfaceIdOffset); | 483 return this.buffer.getUint32(kMessageInterfaceIdOffset); |
| 462 }; | 484 }; |
| 463 | 485 |
| 464 Message.prototype.getPayloadInterfaceIds = function() { | 486 Message.prototype.getPayloadInterfaceIds = function() { |
| 465 if (this.getHeaderVersion() < 2) { | 487 if (this.getHeaderVersion() < 2) { |
| 466 return null; | 488 return null; |
| 467 } | 489 } |
| 468 | 490 |
| 469 var decoder = new Decoder(this.buffer, this.handles, | 491 var decoder = new Decoder(this.buffer, this.handles, |
| 492 this.associatedEndpointHandles, |
| 470 kMessagePayloadInterfaceIdsPointerOffset); | 493 kMessagePayloadInterfaceIdsPointerOffset); |
| 471 var payloadInterfaceIds = decoder.decodeArrayPointer(Uint32); | 494 var payloadInterfaceIds = decoder.decodeArrayPointer(Uint32); |
| 472 return payloadInterfaceIds; | 495 return payloadInterfaceIds; |
| 473 }; | 496 }; |
| 474 | 497 |
| 475 Message.prototype.isResponse = function() { | 498 Message.prototype.isResponse = function() { |
| 476 return (this.getFlags() & kMessageIsResponse) != 0; | 499 return (this.getFlags() & kMessageIsResponse) != 0; |
| 477 }; | 500 }; |
| 478 | 501 |
| 479 Message.prototype.expectsResponse = function() { | 502 Message.prototype.expectsResponse = function() { |
| 480 return (this.getFlags() & kMessageExpectsResponse) != 0; | 503 return (this.getFlags() & kMessageExpectsResponse) != 0; |
| 481 }; | 504 }; |
| 482 | 505 |
| 483 Message.prototype.setRequestID = function(requestID) { | 506 Message.prototype.setRequestID = function(requestID) { |
| 484 // TODO(darin): Verify that space was reserved for this field! | 507 // TODO(darin): Verify that space was reserved for this field! |
| 485 this.buffer.setUint64(kMessageRequestIDOffset, requestID); | 508 this.buffer.setUint64(kMessageRequestIDOffset, requestID); |
| 486 }; | 509 }; |
| 487 | 510 |
| 488 Message.prototype.setInterfaceId = function(interfaceId) { | 511 Message.prototype.setInterfaceId = function(interfaceId) { |
| 489 this.buffer.setUint32(kMessageInterfaceIdOffset, interfaceId); | 512 this.buffer.setUint32(kMessageInterfaceIdOffset, interfaceId); |
| 490 }; | 513 }; |
| 491 | 514 |
| 515 Message.prototype.setPayloadInterfaceIds_ = function(payloadInterfaceIds) { |
| 516 if (this.getHeaderVersion() < 2) { |
| 517 throw new Error( |
| 518 "Version of message does not support payload interface ids"); |
| 519 } |
| 492 | 520 |
| 493 // MessageBuilder ----------------------------------------------------------- | 521 var decoder = new Decoder(this.buffer, this.handles, |
| 522 this.associatedEndpointHandles, |
| 523 kMessagePayloadInterfaceIdsPointerOffset); |
| 524 var payloadInterfaceIdsOffset = decoder.decodePointer(); |
| 525 var encoder = new Encoder(this.buffer, this.handles, |
| 526 this.associatedEndpointHandles, |
| 527 payloadInterfaceIdsOffset); |
| 528 encoder.encodeArray(Uint32, payloadInterfaceIds); |
| 529 }; |
| 494 | 530 |
| 495 function MessageBuilder(messageName, payloadSize) { | 531 Message.prototype.serializeAssociatedEndpointHandles = function( |
| 532 associatedGroupController) { |
| 533 if (this.associatedEndpointHandles.length > 0) { |
| 534 if (this.getHeaderVersion() < 2) { |
| 535 throw new Error( |
| 536 "Version of message does not support associated endpoint handles"); |
| 537 } |
| 538 |
| 539 var data = []; |
| 540 for (var i = 0; i < this.associatedEndpointHandles.length; i++) { |
| 541 var handle = this.associatedEndpointHandles[i]; |
| 542 data.push(associatedGroupController.associateInterface(handle)); |
| 543 } |
| 544 this.associatedEndpointHandles = []; |
| 545 this.setPayloadInterfaceIds_(data); |
| 546 } |
| 547 }; |
| 548 |
| 549 Message.prototype.deserializeAssociatedEndpointHandles = function( |
| 550 associatedGroupController) { |
| 551 if (this.getHeaderVersion() < 2) { |
| 552 return true; |
| 553 } |
| 554 |
| 555 this.associatedEndpointHandles = []; |
| 556 var ids = this.getPayloadInterfaceIds(); |
| 557 |
| 558 var result = true; |
| 559 for (var i = 0; i < ids.length; i++) { |
| 560 var handle = associatedGroupController.createLocalEndpointHandle(ids[i]); |
| 561 if (types.isValidInterfaceId(ids[i]) && !handle.isValid()) { |
| 562 // |ids[i]| itself is valid but handle creation failed. In that case, |
| 563 // mark deserialization as failed but continue to deserialize the |
| 564 // rest of handles. |
| 565 result = false; |
| 566 } |
| 567 this.associatedEndpointHandles.push(handle); |
| 568 ids[i] = types.kInvalidInterfaceId; |
| 569 } |
| 570 |
| 571 this.setPayloadInterfaceIds_(ids); |
| 572 return result; |
| 573 }; |
| 574 |
| 575 |
| 576 // MessageV0Builder --------------------------------------------------------- |
| 577 |
| 578 function MessageV0Builder(messageName, payloadSize) { |
| 496 // Currently, we don't compute the payload size correctly ahead of time. | 579 // Currently, we don't compute the payload size correctly ahead of time. |
| 497 // Instead, we resize the buffer at the end. | 580 // Instead, we resize the buffer at the end. |
| 498 var numberOfBytes = kMessageV0HeaderSize + payloadSize; | 581 var numberOfBytes = kMessageV0HeaderSize + payloadSize; |
| 499 this.buffer = new buffer.Buffer(numberOfBytes); | 582 this.buffer = new buffer.Buffer(numberOfBytes); |
| 500 this.handles = []; | 583 this.handles = []; |
| 501 var encoder = this.createEncoder(kMessageV0HeaderSize); | 584 var encoder = this.createEncoder(kMessageV0HeaderSize); |
| 502 encoder.writeUint32(kMessageV0HeaderSize); | 585 encoder.writeUint32(kMessageV0HeaderSize); |
| 503 encoder.writeUint32(0); // version. | 586 encoder.writeUint32(0); // version. |
| 504 encoder.writeUint32(0); // interface ID. | 587 encoder.writeUint32(0); // interface ID. |
| 505 encoder.writeUint32(messageName); | 588 encoder.writeUint32(messageName); |
| 506 encoder.writeUint32(0); // flags. | 589 encoder.writeUint32(0); // flags. |
| 507 encoder.writeUint32(0); // padding. | 590 encoder.writeUint32(0); // padding. |
| 508 } | 591 } |
| 509 | 592 |
| 510 MessageBuilder.prototype.createEncoder = function(size) { | 593 MessageV0Builder.prototype.createEncoder = function(size) { |
| 511 var pointer = this.buffer.alloc(size); | 594 var pointer = this.buffer.alloc(size); |
| 512 return new Encoder(this.buffer, this.handles, pointer); | 595 return new Encoder(this.buffer, this.handles, [], pointer); |
| 513 }; | 596 }; |
| 514 | 597 |
| 515 MessageBuilder.prototype.encodeStruct = function(cls, val) { | 598 MessageV0Builder.prototype.encodeStruct = function(cls, val) { |
| 516 cls.encode(this.createEncoder(cls.encodedSize), val); | 599 cls.encode(this.createEncoder(cls.encodedSize), val); |
| 517 }; | 600 }; |
| 518 | 601 |
| 519 MessageBuilder.prototype.finish = function() { | 602 MessageV0Builder.prototype.finish = function() { |
| 520 // TODO(abarth): Rather than resizing the buffer at the end, we could | 603 // TODO(abarth): Rather than resizing the buffer at the end, we could |
| 521 // compute the size we need ahead of time, like we do in C++. | 604 // compute the size we need ahead of time, like we do in C++. |
| 522 this.buffer.trim(); | 605 this.buffer.trim(); |
| 523 var message = new Message(this.buffer, this.handles); | 606 var message = new Message(this.buffer, this.handles); |
| 524 this.buffer = null; | 607 this.buffer = null; |
| 525 this.handles = null; | 608 this.handles = null; |
| 526 this.encoder = null; | 609 this.encoder = null; |
| 527 return message; | 610 return message; |
| 528 }; | 611 }; |
| 529 | 612 |
| 530 // MessageWithRequestIDBuilder ----------------------------------------------- | 613 // MessageV1Builder ----------------------------------------------- |
| 531 | 614 |
| 532 function MessageWithRequestIDBuilder(messageName, payloadSize, flags, | 615 function MessageV1Builder(messageName, payloadSize, flags, |
| 533 requestID) { | 616 requestID) { |
| 534 // Currently, we don't compute the payload size correctly ahead of time. | 617 // Currently, we don't compute the payload size correctly ahead of time. |
| 535 // Instead, we resize the buffer at the end. | 618 // Instead, we resize the buffer at the end. |
| 536 var numberOfBytes = kMessageV1HeaderSize + payloadSize; | 619 var numberOfBytes = kMessageV1HeaderSize + payloadSize; |
| 537 this.buffer = new buffer.Buffer(numberOfBytes); | 620 this.buffer = new buffer.Buffer(numberOfBytes); |
| 538 this.handles = []; | 621 this.handles = []; |
| 539 var encoder = this.createEncoder(kMessageV1HeaderSize); | 622 var encoder = this.createEncoder(kMessageV1HeaderSize); |
| 540 encoder.writeUint32(kMessageV1HeaderSize); | 623 encoder.writeUint32(kMessageV1HeaderSize); |
| 541 encoder.writeUint32(1); // version. | 624 encoder.writeUint32(1); // version. |
| 542 encoder.writeUint32(0); // interface ID. | 625 encoder.writeUint32(0); // interface ID. |
| 543 encoder.writeUint32(messageName); | 626 encoder.writeUint32(messageName); |
| 544 encoder.writeUint32(flags); | 627 encoder.writeUint32(flags); |
| 545 encoder.writeUint32(0); // padding. | 628 encoder.writeUint32(0); // padding. |
| 546 encoder.writeUint64(requestID); | 629 encoder.writeUint64(requestID); |
| 547 } | 630 } |
| 548 | 631 |
| 549 MessageWithRequestIDBuilder.prototype = | 632 MessageV1Builder.prototype = |
| 550 Object.create(MessageBuilder.prototype); | 633 Object.create(MessageV0Builder.prototype); |
| 551 | 634 |
| 552 MessageWithRequestIDBuilder.prototype.constructor = | 635 MessageV1Builder.prototype.constructor = |
| 553 MessageWithRequestIDBuilder; | 636 MessageV1Builder; |
| 637 |
| 638 // MessageV2 ----------------------------------------------- |
| 639 |
| 640 function MessageV2Builder(messageName, payloadSize, flags, requestID) { |
| 641 // Currently, we don't compute the payload size correctly ahead of time. |
| 642 // Instead, we resize the buffer at the end. |
| 643 var numberOfBytes = kMessageV2HeaderSize + payloadSize; |
| 644 this.buffer = new buffer.Buffer(numberOfBytes); |
| 645 this.handles = []; |
| 646 |
| 647 this.payload = null; |
| 648 this.associatedEndpointHandles = []; |
| 649 |
| 650 this.encoder = this.createEncoder(kMessageV2HeaderSize); |
| 651 this.encoder.writeUint32(kMessageV2HeaderSize); |
| 652 this.encoder.writeUint32(2); // version. |
| 653 // Gets set to an appropriate interfaceId for the endpoint by the Router. |
| 654 this.encoder.writeUint32(0); // interface ID. |
| 655 this.encoder.writeUint32(messageName); |
| 656 this.encoder.writeUint32(flags); |
| 657 this.encoder.writeUint32(0); // padding. |
| 658 this.encoder.writeUint64(requestID); |
| 659 } |
| 660 |
| 661 MessageV2Builder.prototype.createEncoder = function(size) { |
| 662 var pointer = this.buffer.alloc(size); |
| 663 return new Encoder(this.buffer, this.handles, |
| 664 this.associatedEndpointHandles, pointer); |
| 665 }; |
| 666 |
| 667 MessageV2Builder.prototype.setPayload = function(cls, val) { |
| 668 this.payload = {cls: cls, val: val}; |
| 669 }; |
| 670 |
| 671 MessageV2Builder.prototype.finish = function() { |
| 672 if (!this.payload) { |
| 673 throw new Error("Payload needs to be set before calling finish"); |
| 674 } |
| 675 |
| 676 this.encoder.encodeStructPointer(this.payload.cls, this.payload.val); |
| 677 this.encoder.encodeArrayPointer(Uint32, |
| 678 new Array(this.associatedEndpointHandles.length)); |
| 679 |
| 680 this.buffer.trim(); |
| 681 var message = new Message(this.buffer, this.handles, |
| 682 this.associatedEndpointHandles); |
| 683 this.buffer = null; |
| 684 this.handles = null; |
| 685 this.encoder = null; |
| 686 this.payload = null; |
| 687 this.associatedEndpointHandles = null; |
| 688 |
| 689 return message; |
| 690 }; |
| 554 | 691 |
| 555 // MessageReader ------------------------------------------------------------ | 692 // MessageReader ------------------------------------------------------------ |
| 556 | 693 |
| 557 function MessageReader(message) { | 694 function MessageReader(message) { |
| 558 this.decoder = new Decoder(message.buffer, message.handles, 0); | 695 this.decoder = new Decoder(message.buffer, message.handles, |
| 696 message.associatedEndpointHandles, 0); |
| 559 var messageHeaderSize = this.decoder.readUint32(); | 697 var messageHeaderSize = this.decoder.readUint32(); |
| 560 this.payloadSize = message.buffer.byteLength - messageHeaderSize; | 698 this.payloadSize = message.buffer.byteLength - messageHeaderSize; |
| 561 var version = this.decoder.readUint32(); | 699 var version = this.decoder.readUint32(); |
| 562 var interface_id = this.decoder.readUint32(); | 700 var interface_id = this.decoder.readUint32(); |
| 563 this.messageName = this.decoder.readUint32(); | 701 this.messageName = this.decoder.readUint32(); |
| 564 this.flags = this.decoder.readUint32(); | 702 this.flags = this.decoder.readUint32(); |
| 565 // Skip the padding. | 703 // Skip the padding. |
| 566 this.decoder.skip(4); | 704 this.decoder.skip(4); |
| 567 if (version >= 1) | 705 if (version >= 1) |
| 568 this.requestID = this.decoder.readUint64(); | 706 this.requestID = this.decoder.readUint64(); |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 851 Interface.call(this, cls); | 989 Interface.call(this, cls); |
| 852 } | 990 } |
| 853 | 991 |
| 854 NullableInterface.prototype = Object.create(Interface.prototype); | 992 NullableInterface.prototype = Object.create(Interface.prototype); |
| 855 | 993 |
| 856 function AssociatedInterfacePtrInfo() { | 994 function AssociatedInterfacePtrInfo() { |
| 857 } | 995 } |
| 858 | 996 |
| 859 AssociatedInterfacePtrInfo.prototype.encodedSize = 8; | 997 AssociatedInterfacePtrInfo.prototype.encodedSize = 8; |
| 860 | 998 |
| 999 AssociatedInterfacePtrInfo.decode = function(decoder) { |
| 1000 return new types.AssociatedInterfacePtrInfo( |
| 1001 decoder.decodeAssociatedEndpointHandle(), decoder.readUint32()); |
| 1002 }; |
| 1003 |
| 1004 AssociatedInterfacePtrInfo.encode = function(encoder, val) { |
| 1005 var associatedinterfacePtrInfo = |
| 1006 val ? val : new types.AssociatedInterfacePtrInfo(null, 0); |
| 1007 encoder.encodeAssociatedEndpointHandle( |
| 1008 associatedinterfacePtrInfo.interfaceEndpointHandle); |
| 1009 encoder.writeUint32(associatedinterfacePtrInfo.version); |
| 1010 }; |
| 1011 |
| 861 function NullableAssociatedInterfacePtrInfo() { | 1012 function NullableAssociatedInterfacePtrInfo() { |
| 862 } | 1013 } |
| 863 | 1014 |
| 864 NullableAssociatedInterfacePtrInfo.encodedSize = | 1015 NullableAssociatedInterfacePtrInfo.encodedSize = |
| 865 AssociatedInterfacePtrInfo.encodedSize; | 1016 AssociatedInterfacePtrInfo.encodedSize; |
| 866 | 1017 |
| 1018 NullableAssociatedInterfacePtrInfo.decode = |
| 1019 AssociatedInterfacePtrInfo.decode; |
| 1020 |
| 1021 NullableAssociatedInterfacePtrInfo.encode = |
| 1022 AssociatedInterfacePtrInfo.encode; |
| 1023 |
| 867 function InterfaceRequest() { | 1024 function InterfaceRequest() { |
| 868 } | 1025 } |
| 869 | 1026 |
| 870 InterfaceRequest.encodedSize = 4; | 1027 InterfaceRequest.encodedSize = 4; |
| 871 | 1028 |
| 872 InterfaceRequest.decode = function(decoder) { | 1029 InterfaceRequest.decode = function(decoder) { |
| 873 return new types.InterfaceRequest(decoder.decodeHandle()); | 1030 return new types.InterfaceRequest(decoder.decodeHandle()); |
| 874 }; | 1031 }; |
| 875 | 1032 |
| 876 InterfaceRequest.encode = function(encoder, val) { | 1033 InterfaceRequest.encode = function(encoder, val) { |
| 877 encoder.encodeHandle(val ? val.handle : null); | 1034 encoder.encodeHandle(val ? val.handle : null); |
| 878 }; | 1035 }; |
| 879 | 1036 |
| 880 function NullableInterfaceRequest() { | 1037 function NullableInterfaceRequest() { |
| 881 } | 1038 } |
| 882 | 1039 |
| 883 NullableInterfaceRequest.encodedSize = InterfaceRequest.encodedSize; | 1040 NullableInterfaceRequest.encodedSize = InterfaceRequest.encodedSize; |
| 884 | 1041 |
| 885 NullableInterfaceRequest.decode = InterfaceRequest.decode; | 1042 NullableInterfaceRequest.decode = InterfaceRequest.decode; |
| 886 | 1043 |
| 887 NullableInterfaceRequest.encode = InterfaceRequest.encode; | 1044 NullableInterfaceRequest.encode = InterfaceRequest.encode; |
| 888 | 1045 |
| 889 function AssociatedInterfaceRequest() { | 1046 function AssociatedInterfaceRequest() { |
| 890 } | 1047 } |
| 891 | 1048 |
| 1049 AssociatedInterfaceRequest.decode = function(decoder) { |
| 1050 var handle = decoder.decodeAssociatedEndpointHandle(); |
| 1051 return new types.AssociatedInterfaceRequest(handle); |
| 1052 }; |
| 1053 |
| 1054 AssociatedInterfaceRequest.encode = function(encoder, val) { |
| 1055 encoder.encodeAssociatedEndpointHandle( |
| 1056 val ? val.interfaceEndpointHandle : null); |
| 1057 }; |
| 1058 |
| 892 AssociatedInterfaceRequest.encodedSize = 4; | 1059 AssociatedInterfaceRequest.encodedSize = 4; |
| 893 | 1060 |
| 894 function NullableAssociatedInterfaceRequest() { | 1061 function NullableAssociatedInterfaceRequest() { |
| 895 } | 1062 } |
| 896 | 1063 |
| 897 NullableAssociatedInterfaceRequest.encodedSize = | 1064 NullableAssociatedInterfaceRequest.encodedSize = |
| 898 AssociatedInterfaceRequest.encodedSize; | 1065 AssociatedInterfaceRequest.encodedSize; |
| 899 | 1066 |
| 1067 NullableAssociatedInterfaceRequest.decode = |
| 1068 AssociatedInterfaceRequest.decode; |
| 1069 |
| 1070 NullableAssociatedInterfaceRequest.encode = |
| 1071 AssociatedInterfaceRequest.encode; |
| 1072 |
| 900 function MapOf(keyClass, valueClass) { | 1073 function MapOf(keyClass, valueClass) { |
| 901 this.keyClass = keyClass; | 1074 this.keyClass = keyClass; |
| 902 this.valueClass = valueClass; | 1075 this.valueClass = valueClass; |
| 903 } | 1076 } |
| 904 | 1077 |
| 905 MapOf.prototype.encodedSize = 8; | 1078 MapOf.prototype.encodedSize = 8; |
| 906 | 1079 |
| 907 MapOf.prototype.decode = function(decoder) { | 1080 MapOf.prototype.decode = function(decoder) { |
| 908 return decoder.decodeMapPointer(this.keyClass, this.valueClass); | 1081 return decoder.decodeMapPointer(this.keyClass, this.valueClass); |
| 909 }; | 1082 }; |
| 910 | 1083 |
| 911 MapOf.prototype.encode = function(encoder, val) { | 1084 MapOf.prototype.encode = function(encoder, val) { |
| 912 encoder.encodeMapPointer(this.keyClass, this.valueClass, val); | 1085 encoder.encodeMapPointer(this.keyClass, this.valueClass, val); |
| 913 }; | 1086 }; |
| 914 | 1087 |
| 915 function NullableMapOf(keyClass, valueClass) { | 1088 function NullableMapOf(keyClass, valueClass) { |
| 916 MapOf.call(this, keyClass, valueClass); | 1089 MapOf.call(this, keyClass, valueClass); |
| 917 } | 1090 } |
| 918 | 1091 |
| 919 NullableMapOf.prototype = Object.create(MapOf.prototype); | 1092 NullableMapOf.prototype = Object.create(MapOf.prototype); |
| 920 | 1093 |
| 921 var exports = {}; | 1094 var exports = {}; |
| 922 exports.align = align; | 1095 exports.align = align; |
| 923 exports.isAligned = isAligned; | 1096 exports.isAligned = isAligned; |
| 924 exports.Message = Message; | 1097 exports.Message = Message; |
| 925 exports.MessageBuilder = MessageBuilder; | 1098 exports.MessageV0Builder = MessageV0Builder; |
| 926 exports.MessageWithRequestIDBuilder = MessageWithRequestIDBuilder; | 1099 exports.MessageV1Builder = MessageV1Builder; |
| 1100 exports.MessageV2Builder = MessageV2Builder; |
| 927 exports.MessageReader = MessageReader; | 1101 exports.MessageReader = MessageReader; |
| 928 exports.kArrayHeaderSize = kArrayHeaderSize; | 1102 exports.kArrayHeaderSize = kArrayHeaderSize; |
| 929 exports.kMapStructPayloadSize = kMapStructPayloadSize; | 1103 exports.kMapStructPayloadSize = kMapStructPayloadSize; |
| 930 exports.kStructHeaderSize = kStructHeaderSize; | 1104 exports.kStructHeaderSize = kStructHeaderSize; |
| 931 exports.kEncodedInvalidHandleValue = kEncodedInvalidHandleValue; | 1105 exports.kEncodedInvalidHandleValue = kEncodedInvalidHandleValue; |
| 932 exports.kMessageV0HeaderSize = kMessageV0HeaderSize; | 1106 exports.kMessageV0HeaderSize = kMessageV0HeaderSize; |
| 933 exports.kMessageV1HeaderSize = kMessageV1HeaderSize; | 1107 exports.kMessageV1HeaderSize = kMessageV1HeaderSize; |
| 934 exports.kMessageV2HeaderSize = kMessageV2HeaderSize; | 1108 exports.kMessageV2HeaderSize = kMessageV2HeaderSize; |
| 935 exports.kMessagePayloadInterfaceIdsPointerOffset = | 1109 exports.kMessagePayloadInterfaceIdsPointerOffset = |
| 936 kMessagePayloadInterfaceIdsPointerOffset; | 1110 kMessagePayloadInterfaceIdsPointerOffset; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 963 exports.AssociatedInterfacePtrInfo = AssociatedInterfacePtrInfo; | 1137 exports.AssociatedInterfacePtrInfo = AssociatedInterfacePtrInfo; |
| 964 exports.NullableAssociatedInterfacePtrInfo = | 1138 exports.NullableAssociatedInterfacePtrInfo = |
| 965 NullableAssociatedInterfacePtrInfo; | 1139 NullableAssociatedInterfacePtrInfo; |
| 966 exports.AssociatedInterfaceRequest = AssociatedInterfaceRequest; | 1140 exports.AssociatedInterfaceRequest = AssociatedInterfaceRequest; |
| 967 exports.NullableAssociatedInterfaceRequest = | 1141 exports.NullableAssociatedInterfaceRequest = |
| 968 NullableAssociatedInterfaceRequest; | 1142 NullableAssociatedInterfaceRequest; |
| 969 exports.MapOf = MapOf; | 1143 exports.MapOf = MapOf; |
| 970 exports.NullableMapOf = NullableMapOf; | 1144 exports.NullableMapOf = NullableMapOf; |
| 971 return exports; | 1145 return exports; |
| 972 }); | 1146 }); |
| OLD | NEW |