1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
|
// Copyright 2018 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.
/**
* Owns a single message pipe handle and facilitates message sending and routing
* on behalf of all the pipe's local Endpoints.
*/
mojo.internal.interfaceSupport.Router = class {
/**
* @param {!MojoHandle} pipe
* @param {boolean} setNamespaceBit
* @public
*/
constructor(pipe, setNamespaceBit) {
/** @const {!MojoHandle} */
this.pipe_ = pipe;
/** @const {!mojo.internal.interfaceSupport.HandleReader} */
this.reader_ = new mojo.internal.interfaceSupport.HandleReader(pipe);
this.reader_.onRead = this.onMessageReceived_.bind(this);
this.reader_.onError = this.onError_.bind(this);
/** @const {!Map<number, !mojo.internal.interfaceSupport.Endpoint>} */
this.endpoints_ = new Map();
/** @private {number} */
this.nextInterfaceId_ = 1;
/** @const {number} */
this.interfaceIdNamespace_ =
setNamespaceBit ? mojo.internal.kInterfaceNamespaceBit : 0;
/** @const {!mojo.internal.interfaceSupport.PipeControlMessageHandler} */
this.pipeControlHandler_ =
new mojo.internal.interfaceSupport.PipeControlMessageHandler(
this, this.onPeerEndpointClosed_.bind(this));
}
/** @return {!MojoHandle} */
get pipe() {
return this.pipe_;
}
/** @return {number} */
generateInterfaceId() {
return (this.nextInterfaceId_++ | this.interfaceIdNamespace_) >>> 0;
}
/**
* @param {!mojo.internal.interfaceSupport.Endpoint} endpoint
* @param {number} interfaceId
*/
addEndpoint(endpoint, interfaceId) {
if (interfaceId === 0) {
this.reader_.start();
}
console.assert(
this.isReading(), 'adding a secondary endpoint with no primary');
this.endpoints_.set(interfaceId, endpoint);
}
/** @param {number} interfaceId */
removeEndpoint(interfaceId) {
this.endpoints_.delete(interfaceId);
if (interfaceId === 0) {
this.reader_.stop();
}
}
close() {
console.assert(
this.endpoints_.size === 0,
'closing primary endpoint with secondary endpoints still bound');
this.reader_.stopAndCloseHandle();
}
/** @param {number} interfaceId */
closeEndpoint(interfaceId) {
this.removeEndpoint(interfaceId);
if (interfaceId === 0) {
this.close();
} else {
this.pipeControlHandler_.notifyEndpointClosed(interfaceId);
}
}
/** @return {boolean} */
isReading() {
return !this.reader_.isStopped();
}
/** @param {!mojo.internal.Message} message */
send(message) {
this.pipe_.writeMessage(message.buffer, message.handles);
}
/**
* @param {!ArrayBuffer} buffer
* @param {!Array<MojoHandle>} handles
*/
onMessageReceived_(buffer, handles) {
if (buffer.byteLength < mojo.internal.kMessageV0HeaderSize) {
console.error('Rejecting undersized message');
this.onError_();
return;
}
const header = mojo.internal.deserializeMessageHeader(new DataView(buffer));
if (this.pipeControlHandler_.maybeHandleMessage(header, buffer)) {
return;
}
const endpoint = this.endpoints_.get(header.interfaceId);
if (!endpoint) {
console.error(
`Received message for unknown endpoint ${header.interfaceId}`);
return;
}
endpoint.onMessageReceived(header, buffer, handles);
}
onError_() {
for (const endpoint of this.endpoints_.values()) {
endpoint.onError();
}
this.endpoints_.clear();
}
/** @param {number} id */
onPeerEndpointClosed_(id) {
const endpoint = this.endpoints_.get(id);
if (endpoint) {
endpoint.onError();
}
}
};
/**
* Something which can receive notifications from an Endpoint; generally this is
* the Endpoint's owner.
* @interface
*/
mojo.internal.interfaceSupport.EndpointClient = class {
/**
* @param {!mojo.internal.interfaceSupport.Endpoint} endpoint
* @param {!mojo.internal.MessageHeader} header
* @param {!ArrayBuffer} buffer
* @param {!Array<MojoHandle>} handles
*/
onMessageReceived(endpoint, header, buffer, handles) {}
/**
* @param {!mojo.internal.interfaceSupport.Endpoint} endpoint
* @param {string=} reason
*/
onError(endpoint, reason = undefined) {}
};
/**
* Encapsulates a single interface endpoint on a multiplexed Router object. This
* may be the primary (possibly only) endpoint on a pipe, or a secondary
* associated interface endpoint.
*/
mojo.internal.interfaceSupport.Endpoint = class {
/**
* @param {mojo.internal.interfaceSupport.Router=} router
* @param {number=} interfaceId
*/
constructor(router = null, interfaceId = 0) {
/** @private {mojo.internal.interfaceSupport.Router} */
this.router_ = router;
/** @private {number} */
this.interfaceId_ = interfaceId;
/** @private {mojo.internal.interfaceSupport.ControlMessageHandler} */
this.controlMessageHandler_ =
new mojo.internal.interfaceSupport.ControlMessageHandler(this);
/** @private {mojo.internal.interfaceSupport.EndpointClient} */
this.client_ = null;
/** @private {number} */
this.nextRequestId_ = 0;
/** @private {mojo.internal.interfaceSupport.Endpoint} */
this.localPeer_ = null;
}
/**
* @return {{
* endpoint0: !mojo.internal.interfaceSupport.Endpoint,
* endpoint1: !mojo.internal.interfaceSupport.Endpoint,
* }}
*/
static createAssociatedPair() {
const endpoint0 = new mojo.internal.interfaceSupport.Endpoint();
const endpoint1 = new mojo.internal.interfaceSupport.Endpoint();
endpoint1.localPeer_ = endpoint0;
endpoint0.localPeer_ = endpoint1;
return {endpoint0, endpoint1};
}
/** @return {mojo.internal.interfaceSupport.Router} */
get router() {
return this.router_;
}
/** @return {boolean} */
isPrimary() {
return this.router_ !== null && this.interfaceId_ === 0;
}
/** @return {!MojoHandle} */
releasePipe() {
console.assert(this.isPrimary(), 'secondary endpoint cannot release pipe');
return this.router_.pipe;
}
/** @return {boolean} */
get isPendingAssociation() {
return this.localPeer_ !== null;
}
/**
* @param {string} interfaceName
* @param {string} scope
*/
bindInBrowser(interfaceName, scope) {
console.assert(
this.isPrimary() && !this.router_.isReading(),
'endpoint is either associated or already bound');
Mojo.bindInterface(interfaceName, this.router_.pipe, scope);
}
/**
* @param {!mojo.internal.interfaceSupport.Endpoint} endpoint
* @return {number}
*/
associatePeerOfOutgoingEndpoint(endpoint) {
console.assert(this.router_, 'cannot associate with unbound endpoint');
const peer = endpoint.localPeer_;
endpoint.localPeer_ = peer.localPeer_ = null;
const id = this.router_.generateInterfaceId();
peer.router_ = this.router_;
peer.interfaceId_ = id;
if (peer.client_) {
this.router_.addEndpoint(peer, id);
}
return id;
}
/** @return {number} */
generateRequestId() {
const id = this.nextRequestId_++;
if (this.nextRequestId_ > 0xffffffff) {
this.nextRequestId_ = 0;
}
return id;
}
/**
* @param {number} ordinal
* @param {number} requestId
* @param {number} flags
* @param {!mojo.internal.MojomType} paramStruct
* @param {!Object} value
*/
send(ordinal, requestId, flags, paramStruct, value) {
const message = new mojo.internal.Message(
this, this.interfaceId_, flags, ordinal, requestId,
/** @type {!mojo.internal.StructSpec} */ (paramStruct.$.structSpec),
value);
console.assert(
this.router_, 'cannot send message on unassociated unbound endpoint');
this.router_.send(message);
}
/** @param {mojo.internal.interfaceSupport.EndpointClient} client */
start(client) {
console.assert(!this.client_, 'endpoint already started');
this.client_ = client;
if (this.router_) {
this.router_.addEndpoint(this, this.interfaceId_);
}
}
/** @return {boolean} */
get isStarted() {
return this.client_ !== null;
}
stop() {
if (this.router_) {
this.router_.removeEndpoint(this.interfaceId_);
}
this.client_ = null;
this.controlMessageHandler_ = null;
}
close() {
if (this.router_) {
this.router.closeEndpoint(this.interfaceId_);
}
this.client_ = null;
this.controlMessageHandler_ = null;
}
async flushForTesting() {
return this.controlMessageHandler_.sendRunMessage({'flushForTesting': {}});
}
/**
* @param {!mojo.internal.MessageHeader} header
* @param {!ArrayBuffer} buffer
* @param {!Array<MojoHandle>} handles
*/
onMessageReceived(header, buffer, handles) {
console.assert(this.client_, 'endpoint has no client');
const handled =
this.controlMessageHandler_.maybeHandleControlMessage(header, buffer);
if (handled) {
return;
}
this.client_.onMessageReceived(this, header, buffer, handles);
}
onError() {
if (this.client_) {
this.client_.onError(this);
}
}
};
/**
* Creates a new Endpoint wrapping a given pipe handle.
*
* @param {!MojoHandle|!mojo.internal.interfaceSupport.Endpoint} pipeOrEndpoint
* @param {boolean=} setNamespaceBit
* @return {!mojo.internal.interfaceSupport.Endpoint}
*/
mojo.internal.interfaceSupport.createEndpoint = function(
pipeOrEndpoint, setNamespaceBit = false) {
if (pipeOrEndpoint.constructor.name != 'MojoHandle') {
return /** @type {!mojo.internal.interfaceSupport.Endpoint} */(
pipeOrEndpoint);
}
return new mojo.internal.interfaceSupport.Endpoint(
new mojo.internal.interfaceSupport.Router(
/** @type {!MojoHandle} */(pipeOrEndpoint), setNamespaceBit),
0);
};
/**
* Returns its input if given an existing Endpoint. If given a pipe handle,
* creates a new Endpoint to own it and returns that. This is a helper for
* generated PendingReceiver constructors since they can accept either type as
* input.
*
* @param {!MojoHandle|!mojo.internal.interfaceSupport.Endpoint} handle
* @return {!mojo.internal.interfaceSupport.Endpoint}
* @export
*/
mojo.internal.interfaceSupport.getEndpointForReceiver = function(handle) {
return mojo.internal.interfaceSupport.createEndpoint(handle);
};
/**
* @param {!mojo.internal.interfaceSupport.Endpoint} endpoint
* @param {string} interfaceName
* @param {string} scope
* @export
*/
mojo.internal.interfaceSupport.bind = function(endpoint, interfaceName, scope) {
endpoint.bindInBrowser(interfaceName, scope);
};
mojo.internal.interfaceSupport.PipeControlMessageHandler = class {
/**
* @param {!mojo.internal.interfaceSupport.Router} router
* @param {function(number)} onDisconnect
*/
constructor(router, onDisconnect) {
/** @const {!mojo.internal.interfaceSupport.Router} */
this.router_ = router;
/** @const {function(number)} */
this.onDisconnect_ = onDisconnect;
}
/**
* @param {!mojo.pipeControl.RunOrClosePipeInput} input
*/
send(input) {
const spec = /** @type {!mojo.internal.StructSpec} */ (
mojo.pipeControl.RunOrClosePipeMessageParamsSpec.$.$.structSpec);
const message = new mojo.internal.Message(
null, 0xffffffff, 0, mojo.pipeControl.RUN_OR_CLOSE_PIPE_MESSAGE_ID, 0,
/** @type {!mojo.internal.StructSpec} */
(mojo.pipeControl.RunOrClosePipeMessageParamsSpec.$.$.structSpec),
{'input': input});
this.router_.send(message);
}
/**
* @param {!mojo.internal.MessageHeader} header
* @param {!ArrayBuffer} buffer
* @return {boolean}
*/
maybeHandleMessage(header, buffer) {
if (header.ordinal !== mojo.pipeControl.RUN_OR_CLOSE_PIPE_MESSAGE_ID) {
return false;
}
const data = new DataView(buffer, header.headerSize);
const decoder = new mojo.internal.Decoder(data, []);
const spec = /** @type {!mojo.internal.StructSpec} */ (
mojo.pipeControl.RunOrClosePipeMessageParamsSpec.$.$.structSpec);
const input = decoder.decodeStructInline(spec)['input'];
if (input.hasOwnProperty('peerAssociatedEndpointClosedEvent')) {
this.onDisconnect_(input['peerAssociatedEndpointClosedEvent']['id']);
return true;
}
return true;
}
/**@param {number} interfaceId */
notifyEndpointClosed(interfaceId) {
this.send({'peerAssociatedEndpointClosedEvent': {'id': interfaceId}});
}
};
/**
* Handles incoming interface control messages on an interface endpoint.
*/
mojo.internal.interfaceSupport.ControlMessageHandler = class {
/** @param {!mojo.internal.interfaceSupport.Endpoint} endpoint */
constructor(endpoint) {
/** @private {!mojo.internal.interfaceSupport.Endpoint} */
this.endpoint_ = endpoint;
/** @private {!Map<number, function()>} */
this.pendingFlushResolvers_ = new Map;
}
sendRunMessage(input) {
const requestId = this.endpoint_.generateRequestId();
return new Promise(resolve => {
this.endpoint_.send(
mojo.interfaceControl.RUN_MESSAGE_ID, requestId,
mojo.internal.kMessageFlagExpectsResponse,
mojo.interfaceControl.RunMessageParamsSpec.$, {'input': input});
this.pendingFlushResolvers_.set(requestId, resolve);
});
}
maybeHandleControlMessage(header, buffer) {
if (header.ordinal === mojo.interfaceControl.RUN_MESSAGE_ID) {
const data = new DataView(buffer, header.headerSize);
const decoder = new mojo.internal.Decoder(data, []);
if (header.flags & mojo.internal.kMessageFlagExpectsResponse)
return this.handleRunRequest_(header.requestId, decoder);
else
return this.handleRunResponse_(header.requestId, decoder);
}
return false;
}
handleRunRequest_(requestId, decoder) {
const input = decoder.decodeStructInline(
mojo.interfaceControl.RunMessageParamsSpec.$.$.structSpec)['input'];
if (input.hasOwnProperty('flushForTesting')) {
this.endpoint_.send(
mojo.interfaceControl.RUN_MESSAGE_ID, requestId,
mojo.internal.kMessageFlagIsResponse,
mojo.interfaceControl.RunResponseMessageParamsSpec.$,
{'output': null});
return true;
}
return false;
}
handleRunResponse_(requestId, decoder) {
const resolver = this.pendingFlushResolvers_.get(requestId);
if (!resolver)
return false;
resolver();
return true;
}
};
/**
* Captures metadata about a request which was sent by a remote, for which a
* response is expected.
*
* @typedef {{
* requestId: number,
* ordinal: number,
* responseStruct: !mojo.internal.MojomType,
* resolve: !Function,
* reject: !Function,
* }}
*/
mojo.internal.interfaceSupport.PendingResponse;
/**
* Exposed by endpoints to allow observation of remote peer closure. Any number
* of listeners may be registered on a ConnectionErrorEventRouter, and the
* router will dispatch at most one event in its lifetime, whenever its endpoint
* detects peer closure.
* @export
*/
mojo.internal.interfaceSupport.ConnectionErrorEventRouter = class {
/** @public */
constructor() {
/** @type {!Map<number, !Function>} */
this.listeners = new Map;
/** @private {number} */
this.nextListenerId_ = 0;
}
/**
* @param {!Function} listener
* @return {number} An ID which can be given to removeListener() to remove
* this listener.
* @export
*/
addListener(listener) {
const id = ++this.nextListenerId_;
this.listeners.set(id, listener);
return id;
}
/**
* @param {number} id An ID returned by a prior call to addListener.
* @return {boolean} True iff the identified listener was found and removed.
* @export
*/
removeListener(id) {
return this.listeners.delete(id);
}
/**
* Notifies all listeners of a connection error.
*/
dispatchErrorEvent() {
for (const listener of this.listeners.values())
listener();
}
};
/**
* @interface
* @export
*/
mojo.internal.interfaceSupport.PendingReceiver = class {
/**
* @return {!mojo.internal.interfaceSupport.Endpoint}
* @export
*/
get handle() {}
};
/**
* Generic helper used to implement all generated remote classes. Knows how to
* serialize requests and deserialize their replies, both according to
* declarative message structure specs.
*
* TODO(crbug.com/1012109): Use a bounded generic type instead of
* mojo.internal.interfaceSupport.PendingReceiver.
* @implements {mojo.internal.interfaceSupport.EndpointClient}
* @export
*/
mojo.internal.interfaceSupport.InterfaceRemoteBase = class {
/**
* @param {!function(new:mojo.internal.interfaceSupport.PendingReceiver,
* !mojo.internal.interfaceSupport.Endpoint)} requestType
* @param {MojoHandle|mojo.internal.interfaceSupport.Endpoint=} handle
* The pipe or endpoint handle to use as a remote endpoint. If omitted,
* this object must be bound with bindHandle before it can be used to send
* messages.
* @public
*/
constructor(requestType, handle = undefined) {
/** @private {mojo.internal.interfaceSupport.Endpoint} */
this.endpoint_ = null;
/**
* @private {!function(new:mojo.internal.interfaceSupport.PendingReceiver,
* !mojo.internal.interfaceSupport.Endpoint)}
*/
this.requestType_ = requestType;
/**
* @private {!Map<number, !mojo.internal.interfaceSupport.PendingResponse>}
*/
this.pendingResponses_ = new Map;
/** @const {!mojo.internal.interfaceSupport.ConnectionErrorEventRouter} */
this.connectionErrorEventRouter_ =
new mojo.internal.interfaceSupport.ConnectionErrorEventRouter;
if (handle) {
this.bindHandle(handle);
}
}
/** @return {mojo.internal.interfaceSupport.Endpoint} */
get endpoint() {
return this.endpoint_;
}
/**
* @return {!mojo.internal.interfaceSupport.PendingReceiver}
*/
bindNewPipeAndPassReceiver() {
let {handle0, handle1} = Mojo.createMessagePipe();
this.bindHandle(handle0);
return new this.requestType_(
mojo.internal.interfaceSupport.createEndpoint(handle1));
}
/**
* @param {!MojoHandle|!mojo.internal.interfaceSupport.Endpoint} handle
* @export
*/
bindHandle(handle) {
console.assert(!this.endpoint_, 'already bound');
handle = mojo.internal.interfaceSupport.createEndpoint(
handle, /* setNamespaceBit */ true);
this.endpoint_ = handle;
this.endpoint_.start(this);
this.pendingResponses_ = new Map;
}
/** @export */
associateAndPassReceiver() {
console.assert(!this.endpoint_, 'cannot associate when already bound');
const {endpoint0, endpoint1} =
mojo.internal.interfaceSupport.Endpoint.createAssociatedPair();
this.bindHandle(endpoint0);
return new this.requestType_(endpoint1);
}
/**
* @return {?mojo.internal.interfaceSupport.Endpoint}
* @export
*/
unbind() {
if (!this.endpoint_) {
return null;
}
const endpoint = this.endpoint_;
this.endpoint_ = null;
endpoint.stop();
return endpoint;
}
/** @export */
close() {
this.cleanupAndFlushPendingResponses_('Message pipe closed.');
if (this.endpoint_) {
this.endpoint_.close();
}
this.endpoint_ = null;
}
/**
* @return {!mojo.internal.interfaceSupport.ConnectionErrorEventRouter}
* @export
*/
getConnectionErrorEventRouter() {
return this.connectionErrorEventRouter_;
}
/**
* @param {number} ordinal
* @param {!mojo.internal.MojomType} paramStruct
* @param {?mojo.internal.MojomType} maybeResponseStruct
* @param {!Array} args
* @return {!Promise}
* @export
*/
sendMessage(ordinal, paramStruct, maybeResponseStruct, args) {
// The pipe has already been closed, so just drop the message.
if (maybeResponseStruct && (!this.endpoint_ || !this.endpoint_.isStarted)) {
return Promise.reject(new Error('The pipe has already been closed.'));
}
const value = {};
paramStruct.$.structSpec.fields.forEach(
(field, index) => value[field.name] = args[index]);
const requestId = this.endpoint_.generateRequestId();
this.endpoint_.send(
ordinal, requestId,
maybeResponseStruct ? mojo.internal.kMessageFlagExpectsResponse : 0,
paramStruct, value);
if (!maybeResponseStruct) {
return Promise.resolve();
}
const responseStruct =
/** @type {!mojo.internal.MojomType} */ (maybeResponseStruct);
return new Promise((resolve, reject) => {
this.pendingResponses_.set(
requestId, {requestId, ordinal, responseStruct, resolve, reject});
});
}
/**
* @return {!Promise}
* @export
*/
flushForTesting() {
return this.endpoint_.flushForTesting();
}
/** @override */
onMessageReceived(endpoint, header, buffer, handles) {
if (!(header.flags & mojo.internal.kMessageFlagIsResponse) ||
header.flags & mojo.internal.kMessageFlagExpectsResponse) {
return this.onError(endpoint, 'Received unexpected request message');
}
const pendingResponse = this.pendingResponses_.get(header.requestId);
this.pendingResponses_.delete(header.requestId);
if (!pendingResponse)
return this.onError(endpoint, 'Received unexpected response message');
const decoder = new mojo.internal.Decoder(
new DataView(buffer, header.headerSize), handles, {endpoint});
const responseValue = decoder.decodeStructInline(
/** @type {!mojo.internal.StructSpec} */ (
pendingResponse.responseStruct.$.structSpec));
if (!responseValue)
return this.onError(endpoint, 'Received malformed response message');
if (header.ordinal !== pendingResponse.ordinal)
return this.onError(endpoint, 'Received malformed response message');
pendingResponse.resolve(responseValue);
}
/** @override */
onError(endpoint, reason = undefined) {
this.cleanupAndFlushPendingResponses_(reason);
this.connectionErrorEventRouter_.dispatchErrorEvent();
}
/**
* @param {string=} reason
* @private
*/
cleanupAndFlushPendingResponses_(reason = undefined) {
if (this.endpoint_) {
this.endpoint_.stop();
}
for (const id of this.pendingResponses_.keys()) {
this.pendingResponses_.get(id).reject(new Error(reason));
}
this.pendingResponses_ = new Map;
}
};
/**
* Wrapper around mojo.internal.interfaceSupport.InterfaceRemoteBase that
* exposes the subset of InterfaceRemoteBase's method that users are allowed
* to use.
* @template T
* @export
*/
mojo.internal.interfaceSupport.InterfaceRemoteBaseWrapper = class {
/**
* @param {!mojo.internal.interfaceSupport.InterfaceRemoteBase<T>} remote
* @public
*/
constructor(remote) {
/** @private {!mojo.internal.interfaceSupport.InterfaceRemoteBase<T>} */
this.remote_ = remote;
}
/**
* @return {!T}
* @export
*/
bindNewPipeAndPassReceiver() {
return this.remote_.bindNewPipeAndPassReceiver();
}
/**
* @return {!T}
* @export
*/
associateAndPassReceiver() {
return this.remote_.associateAndPassReceiver();
}
/**
* @return {boolean}
* @export
*/
isBound() {
return this.remote_.endpoint_ !== null;
}
/** @export */
close() {
this.remote_.close();
}
/**
* @return {!Promise}
* @export
*/
flushForTesting() {
return this.remote_.flushForTesting();
}
}
/**
* Helper used by generated EventRouter types to dispatch incoming interface
* messages as Event-like things.
* @export
*/
mojo.internal.interfaceSupport.CallbackRouter = class {
constructor() {
/** @type {!Map<number, !Function>} */
this.removeCallbacks = new Map;
/** @private {number} */
this.nextListenerId_ = 0;
}
/** @return {number} */
getNextId() {
return ++this.nextListenerId_;
}
/**
* @param {number} id An ID returned by a prior call to addListener.
* @return {boolean} True iff the identified listener was found and removed.
* @export
*/
removeListener(id) {
this.removeCallbacks.get(id)();
return this.removeCallbacks.delete(id);
}
};
/**
* Helper used by generated CallbackRouter types to dispatch incoming interface
* messages to listeners.
* @export
*/
mojo.internal.interfaceSupport.InterfaceCallbackReceiver = class {
/**
* @public
* @param {!mojo.internal.interfaceSupport.CallbackRouter} callbackRouter
*/
constructor(callbackRouter) {
/** @private {!Map<number, !Function>} */
this.listeners_ = new Map;
/** @private {!mojo.internal.interfaceSupport.CallbackRouter} */
this.callbackRouter_ = callbackRouter;
}
/**
* @param {!Function} listener
* @return {number} A unique ID for the added listener.
* @export
*/
addListener(listener) {
const id = this.callbackRouter_.getNextId();
this.listeners_.set(id, listener);
this.callbackRouter_.removeCallbacks.set(id, () => {
return this.listeners_.delete(id);
});
return id;
}
/**
* @param {boolean} expectsResponse
* @return {!Function}
* @export
*/
createReceiverHandler(expectsResponse) {
if (expectsResponse)
return this.dispatchWithResponse_.bind(this);
return this.dispatch_.bind(this);
}
/**
* @param {...*} varArgs
* @private
*/
dispatch_(varArgs) {
const args = Array.from(arguments);
this.listeners_.forEach(listener => listener.apply(null, args));
}
/**
* @param {...*} varArgs
* @return {?Object}
* @private
*/
dispatchWithResponse_(varArgs) {
const args = Array.from(arguments);
const returnValues = Array.from(this.listeners_.values())
.map(listener => listener.apply(null, args));
let returnValue;
for (const value of returnValues) {
if (value === undefined)
continue;
if (returnValue !== undefined)
throw new Error('Multiple listeners attempted to reply to a message');
returnValue = value;
}
return returnValue;
}
};
/**
* Wraps message handlers attached to an InterfaceReceiver.
*
* @typedef {{
* paramStruct: !mojo.internal.MojomType,
* responseStruct: ?mojo.internal.MojomType,
* handler: !Function,
* }}
*/
mojo.internal.interfaceSupport.MessageHandler;
/**
* Generic helper that listens for incoming request messages on one or more
* endpoints of the same interface type, dispatching them to registered
* handlers. Handlers are registered against a specific ordinal message number.
*
* @template T
* @implements {mojo.internal.interfaceSupport.EndpointClient}
* @export
*/
mojo.internal.interfaceSupport.InterfaceReceiverHelperInternal = class {
/**
* @param {!function(new:T,
* (!MojoHandle|!mojo.internal.interfaceSupport.Endpoint)=)} remoteType
* @public
*/
constructor(remoteType) {
/** @private {!Set<!mojo.internal.interfaceSupport.Endpoint>} endpoints */
this.endpoints_ = new Set();
/**
* @private {!function(new:T,
* (!MojoHandle|!mojo.internal.interfaceSupport.Endpoint)=)}
*/
this.remoteType_ = remoteType;
/**
* @private {!Map<number, !mojo.internal.interfaceSupport.MessageHandler>}
*/
this.messageHandlers_ = new Map;
/** @const {!mojo.internal.interfaceSupport.ConnectionErrorEventRouter} */
this.connectionErrorEventRouter_ =
new mojo.internal.interfaceSupport.ConnectionErrorEventRouter;
}
/**
* @param {number} ordinal
* @param {!mojo.internal.MojomType} paramStruct
* @param {?mojo.internal.MojomType} responseStruct
* @param {!Function} handler
* @export
*/
registerHandler(ordinal, paramStruct, responseStruct, handler) {
this.messageHandlers_.set(ordinal, {paramStruct, responseStruct, handler});
}
/**
* @param {!MojoHandle|!mojo.internal.interfaceSupport.Endpoint} handle
* @export
*/
bindHandle(handle) {
handle = mojo.internal.interfaceSupport.createEndpoint(handle);
this.endpoints_.add(handle);
handle.start(this);
}
/**
* @return {!T}
* @export
*/
bindNewPipeAndPassRemote() {
let remote = new this.remoteType_();
this.bindHandle(remote.$.bindNewPipeAndPassReceiver().handle);
return remote;
}
/**
* @return {!T}
* @export
*/
associateAndPassRemote() {
const {endpoint0, endpoint1} =
mojo.internal.interfaceSupport.Endpoint.createAssociatedPair();
this.bindHandle(endpoint0);
return new this.remoteType_(endpoint1);
}
/** @export */
closeBindings() {
for (const endpoint of this.endpoints_) {
endpoint.close();
}
this.endpoints_.clear();
}
/**
* @return {!mojo.internal.interfaceSupport.ConnectionErrorEventRouter}
* @export
*/
getConnectionErrorEventRouter() {
return this.connectionErrorEventRouter_;
}
/**
* @return {!Promise}
* @export
*/
async flush() {
for (let endpoint of this.endpoints_) {
await endpoint.flushForTesting();
}
}
/** @override */
onMessageReceived(endpoint, header, buffer, handles) {
if (header.flags & mojo.internal.kMessageFlagIsResponse)
throw new Error('Received unexpected response on interface receiver');
const handler = this.messageHandlers_.get(header.ordinal);
if (!handler)
throw new Error('Received unknown message');
const decoder = new mojo.internal.Decoder(
new DataView(buffer, header.headerSize), handles, {endpoint});
const request = decoder.decodeStructInline(
/** @type {!mojo.internal.StructSpec} */ (
handler.paramStruct.$.structSpec));
if (!request)
throw new Error('Received malformed message');
let result = handler.handler.apply(
null,
handler.paramStruct.$.structSpec.fields.map(
field => request[field.name]));
// If the message expects a response, the handler must return either a
// well-formed response object, or a Promise that will eventually yield one.
if (handler.responseStruct) {
if (result === undefined) {
this.onError(endpoint);
throw new Error(
'Message expects a reply but its handler did not provide one.');
}
if (typeof result != 'object' || result.constructor.name != 'Promise') {
result = Promise.resolve(result);
}
result
.then(value => {
endpoint.send(
header.ordinal, header.requestId,
mojo.internal.kMessageFlagIsResponse,
/** @type {!mojo.internal.MojomType} */
(handler.responseStruct), value);
})
.catch(() => {
// If the handler rejects, that means it didn't like the request's
// contents for whatever reason. We close the binding to prevent
// further messages from being received from that client.
this.onError(endpoint);
});
}
}
/** @override */
onError(endpoint, reason = undefined) {
this.endpoints_.delete(endpoint);
endpoint.close();
this.connectionErrorEventRouter_.dispatchErrorEvent();
}
};
/**
* Generic helper used to perform operations related to the interface pipe e.g.
* bind the pipe, close it, flush it for testing, etc. Wraps
* mojo.internal.interfaceSupport.InterfaceReceiverHelperInternal and exposes a
* subset of methods that meant to be used by users of a receiver class.
*
* @template T
* @export
*/
mojo.internal.interfaceSupport.InterfaceReceiverHelper = class {
/**
* @param {!mojo.internal.interfaceSupport.InterfaceReceiverHelperInternal<T>}
* helper_internal
* @public
*/
constructor(helper_internal) {
/**
* @private {!mojo.internal.interfaceSupport.InterfaceReceiverHelperInternal<T>}
*/
this.helper_internal_ = helper_internal;
}
/**
* Binds a new handle to this object. Messages which arrive on the handle will
* be read and dispatched to this object.
*
* @param {!MojoHandle|!mojo.internal.interfaceSupport.Endpoint} handle
* @export
*/
bindHandle(handle) {
this.helper_internal_.bindHandle(handle);
}
/**
* @return {!T}
* @export
*/
bindNewPipeAndPassRemote() {
return this.helper_internal_.bindNewPipeAndPassRemote();
}
/**
* @return {!T}
* @export
*/
associateAndPassRemote() {
return this.helper_internal_.associateAndPassRemote();
}
/** @export */
close() {
this.helper_internal_.closeBindings();
}
/**
* @return {!Promise}
* @export
*/
flush() {
return this.helper_internal_.flush();
}
}
/**
* Watches a MojoHandle for readability or peer closure, forwarding either event
* to one of two callbacks on the reader. Used by both InterfaceRemoteBase and
* InterfaceReceiverHelperInternal to watch for incoming messages.
*/
mojo.internal.interfaceSupport.HandleReader = class {
/**
* @param {!MojoHandle} handle
* @private
*/
constructor(handle) {
/** @private {!MojoHandle} */
this.handle_ = handle;
/** @public {?function(!ArrayBuffer, !Array<MojoHandle>)} */
this.onRead = null;
/** @public {!Function} */
this.onError = () => {};
/** @public {?MojoWatcher} */
this.watcher_ = null;
}
isStopped() {
return this.watcher_ === null;
}
start() {
this.watcher_ = this.handle_.watch({readable: true}, this.read_.bind(this));
}
stop() {
if (!this.watcher_) {
return;
}
this.watcher_.cancel();
this.watcher_ = null;
}
stopAndCloseHandle() {
if (this.watcher_) {
this.stop();
}
this.handle_.close();
}
/** @private */
read_(result) {
for (;;) {
if (!this.watcher_)
return;
const read = this.handle_.readMessage();
// No messages available.
if (read.result == Mojo.RESULT_SHOULD_WAIT)
return;
// Remote endpoint has been closed *and* no messages available.
if (read.result == Mojo.RESULT_FAILED_PRECONDITION) {
this.onError();
return;
}
// Something terrible happened.
if (read.result != Mojo.RESULT_OK)
throw new Error('Unexpected error on HandleReader: ' + read.result);
this.onRead(read.buffer, read.handles);
}
}
};
|