OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 define("mojo/public/js/lib/interface_endpoint_handle", [ |
| 6 "mojo/public/js/interface_types", |
| 7 "timer", |
| 8 ], function(types, timer) { |
| 9 |
| 10 var AssociationEvent = { |
| 11 // The interface has been associated with a message pipe. |
| 12 ASSOCIATED: 'associated', |
| 13 // The peer of this object has been closed before association. |
| 14 PEER_CLOSED_BEFORE_ASSOCIATION: 'peer_closed_before_association' |
| 15 }; |
| 16 |
| 17 function State(interfaceId = types.kInvalidInterfaceId, |
| 18 associatedGroupController) { |
| 19 this.interfaceId = interfaceId; |
| 20 this.associatedGroupController = associatedGroupController; |
| 21 this.pendingAssociation = false; |
| 22 this.disconnectReason = null; |
| 23 this.peerState_ = null; |
| 24 this.associationEventHandler_ = undefined; |
| 25 } |
| 26 |
| 27 State.prototype.initPendingState = function(peer) { |
| 28 this.pendingAssociation = true; |
| 29 this.peerState_ = peer; |
| 30 }; |
| 31 |
| 32 State.prototype.isValid = function() { |
| 33 return this.pendingAssociation || |
| 34 types.isValidInterfaceId(this.interfaceId); |
| 35 }; |
| 36 |
| 37 State.prototype.close = function(disconnectReason) { |
| 38 if (this.associatedGroupController) { |
| 39 this.associatedGroupController.closeEndpointHandle(this.interfaceId, |
| 40 disconnectReason); |
| 41 } else if (this.peerState_) { |
| 42 this.peerState_.onPeerClosedBeforeAssociation(disconnectReason); |
| 43 } |
| 44 }; |
| 45 |
| 46 State.prototype.runAssociationEventHandler = function(associationEvent) { |
| 47 function run() { |
| 48 this.associationEventHandler_(associationEvent); |
| 49 this.associationEventHandler_ = undefined; |
| 50 } |
| 51 |
| 52 if (this.associationEventHandler_) { |
| 53 timer.createOneShot(0, run.bind(this)); |
| 54 } |
| 55 }; |
| 56 |
| 57 State.prototype.setAssociationEventHandler = function(handler) { |
| 58 if ((!this.pendingAssociation && |
| 59 !types.isValidInterfaceId(this.interfaceId)) || !handler) { |
| 60 return; |
| 61 } |
| 62 |
| 63 this.associationEventHandler_ = handler; |
| 64 if (!this.pendingAssociation) { |
| 65 this.runAssociationEventHandler(AssociationEvent.ASSOCIATED); |
| 66 } else if (!this.peerState_) { |
| 67 this.runAssociationEventHandler( |
| 68 AssociationEvent.PEER_CLOSED_BEFORE_ASSOCIATION); |
| 69 } |
| 70 }; |
| 71 |
| 72 State.prototype.onAssociated = function(interfaceId, |
| 73 associatedGroupController) { |
| 74 if (!this.pendingAssociation) { |
| 75 return; |
| 76 } |
| 77 |
| 78 this.pendingAssociation = false; |
| 79 this.peerState_ = null; |
| 80 this.interfaceId = interfaceId; |
| 81 this.associatedGroupController = associatedGroupController; |
| 82 this.runAssociationEventHandler(AssociationEvent.ASSOCIATED); |
| 83 }; |
| 84 |
| 85 State.prototype.onPeerClosedBeforeAssociation = function(disconnectReason) { |
| 86 if (!this.pendingAssociation) { |
| 87 return; |
| 88 } |
| 89 |
| 90 this.peerState_ = null; |
| 91 this.disconnectReason = disconnectReason; |
| 92 |
| 93 this.runAssociationEventHandler( |
| 94 AssociationEvent.PEER_CLOSED_BEFORE_ASSOCIATION); |
| 95 }; |
| 96 |
| 97 function InterfaceEndpointHandle(interfaceId, associatedGroupController) { |
| 98 this.state_ = new State(interfaceId, associatedGroupController); |
| 99 } |
| 100 |
| 101 InterfaceEndpointHandle.prototype.isValid = function() { |
| 102 return this.state_.isValid(); |
| 103 }; |
| 104 |
| 105 InterfaceEndpointHandle.prototype.pendingAssociation = function() { |
| 106 return this.state_.pendingAssociation; |
| 107 }; |
| 108 |
| 109 InterfaceEndpointHandle.prototype.id = function() { |
| 110 return this.state_.interfaceId; |
| 111 }; |
| 112 |
| 113 InterfaceEndpointHandle.prototype.groupController = function() { |
| 114 return this.state_.associatedGroupController; |
| 115 }; |
| 116 |
| 117 InterfaceEndpointHandle.prototype.disconnectReason = function() { |
| 118 return this.state_.disconnectReason; |
| 119 }; |
| 120 |
| 121 InterfaceEndpointHandle.prototype.setAssociationEventHandler = function( |
| 122 handler) { |
| 123 this.state_.setAssociationEventHandler(handler); |
| 124 }; |
| 125 |
| 126 InterfaceEndpointHandle.prototype.reset = function(reason) { |
| 127 this.state_.close(reason); |
| 128 this.state_ = new State(); |
| 129 }; |
| 130 |
| 131 var exports = {}; |
| 132 exports.InterfaceEndpointHandle = InterfaceEndpointHandle; |
| 133 |
| 134 return exports; |
| 135 }); |
OLD | NEW |