| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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/lib/interface_endpoint_handle", [ | 5 define("mojo/public/js/lib/interface_endpoint_handle", [ |
| 6 "mojo/public/js/interface_types", | 6 "mojo/public/js/interface_types", |
| 7 "timer", | 7 "timer", |
| 8 ], function(types, timer) { | 8 ], function(types, timer) { |
| 9 | 9 |
| 10 var AssociationEvent = { | 10 var AssociationEvent = { |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 this.associationEventHandler_ = handler; | 85 this.associationEventHandler_ = handler; |
| 86 if (!this.pendingAssociation) { | 86 if (!this.pendingAssociation) { |
| 87 timer.createOneShot(0, this.runAssociationEventHandler.bind(this, | 87 timer.createOneShot(0, this.runAssociationEventHandler.bind(this, |
| 88 AssociationEvent.ASSOCIATED)); | 88 AssociationEvent.ASSOCIATED)); |
| 89 } else if (!this.peerState_) { | 89 } else if (!this.peerState_) { |
| 90 timer.createOneShot(0, this.runAssociationEventHandler.bind(this, | 90 timer.createOneShot(0, this.runAssociationEventHandler.bind(this, |
| 91 AssociationEvent.PEER_CLOSED_BEFORE_ASSOCIATION)); | 91 AssociationEvent.PEER_CLOSED_BEFORE_ASSOCIATION)); |
| 92 } | 92 } |
| 93 }; | 93 }; |
| 94 | 94 |
| 95 State.prototype.notifyAssociation = function(interfaceId, |
| 96 peerGroupController) { |
| 97 var cachedPeerState = this.peerState_; |
| 98 this.peerState_ = null; |
| 99 |
| 100 this.pendingAssociation = false; |
| 101 |
| 102 if (cachedPeerState) { |
| 103 cachedPeerState.onAssociated(interfaceId, peerGroupController); |
| 104 return true; |
| 105 } |
| 106 return false; |
| 107 }; |
| 108 |
| 95 State.prototype.onAssociated = function(interfaceId, | 109 State.prototype.onAssociated = function(interfaceId, |
| 96 associatedGroupController) { | 110 associatedGroupController) { |
| 97 if (!this.pendingAssociation) { | 111 if (!this.pendingAssociation) { |
| 98 return; | 112 return; |
| 99 } | 113 } |
| 100 | 114 |
| 101 this.pendingAssociation = false; | 115 this.pendingAssociation = false; |
| 102 this.peerState_ = null; | 116 this.peerState_ = null; |
| 103 this.interfaceId = interfaceId; | 117 this.interfaceId = interfaceId; |
| 104 this.associatedGroupController = associatedGroupController; | 118 this.associatedGroupController = associatedGroupController; |
| 105 this.runAssociationEventHandler(AssociationEvent.ASSOCIATED); | 119 this.runAssociationEventHandler(AssociationEvent.ASSOCIATED); |
| 106 }; | 120 }; |
| 107 | 121 |
| 108 State.prototype.onPeerClosedBeforeAssociation = function(disconnectReason) { | 122 State.prototype.onPeerClosedBeforeAssociation = function(disconnectReason) { |
| 109 if (!this.pendingAssociation) { | 123 if (!this.pendingAssociation) { |
| 110 return; | 124 return; |
| 111 } | 125 } |
| 112 | 126 |
| 113 this.peerState_ = null; | 127 this.peerState_ = null; |
| 114 this.disconnectReason = disconnectReason; | 128 this.disconnectReason = disconnectReason; |
| 115 | 129 |
| 116 this.runAssociationEventHandler( | 130 this.runAssociationEventHandler( |
| 117 AssociationEvent.PEER_CLOSED_BEFORE_ASSOCIATION); | 131 AssociationEvent.PEER_CLOSED_BEFORE_ASSOCIATION); |
| 118 }; | 132 }; |
| 119 | 133 |
| 134 function createPairPendingAssociation() { |
| 135 var handle0 = new InterfaceEndpointHandle(); |
| 136 var handle1 = new InterfaceEndpointHandle(); |
| 137 handle0.state_.initPendingState(handle1.state_); |
| 138 handle1.state_.initPendingState(handle0.state_); |
| 139 return {handle0: handle0, handle1: handle1}; |
| 140 } |
| 141 |
| 120 function InterfaceEndpointHandle(interfaceId, associatedGroupController) { | 142 function InterfaceEndpointHandle(interfaceId, associatedGroupController) { |
| 121 this.state_ = new State(interfaceId, associatedGroupController); | 143 this.state_ = new State(interfaceId, associatedGroupController); |
| 122 } | 144 } |
| 123 | 145 |
| 124 InterfaceEndpointHandle.prototype.isValid = function() { | 146 InterfaceEndpointHandle.prototype.isValid = function() { |
| 125 return this.state_.isValid(); | 147 return this.state_.isValid(); |
| 126 }; | 148 }; |
| 127 | 149 |
| 128 InterfaceEndpointHandle.prototype.pendingAssociation = function() { | 150 InterfaceEndpointHandle.prototype.pendingAssociation = function() { |
| 129 return this.state_.pendingAssociation; | 151 return this.state_.pendingAssociation; |
| 130 }; | 152 }; |
| 131 | 153 |
| 132 InterfaceEndpointHandle.prototype.id = function() { | 154 InterfaceEndpointHandle.prototype.id = function() { |
| 133 return this.state_.interfaceId; | 155 return this.state_.interfaceId; |
| 134 }; | 156 }; |
| 135 | 157 |
| 136 InterfaceEndpointHandle.prototype.groupController = function() { | 158 InterfaceEndpointHandle.prototype.groupController = function() { |
| 137 return this.state_.associatedGroupController; | 159 return this.state_.associatedGroupController; |
| 138 }; | 160 }; |
| 139 | 161 |
| 140 InterfaceEndpointHandle.prototype.disconnectReason = function() { | 162 InterfaceEndpointHandle.prototype.disconnectReason = function() { |
| 141 return this.state_.disconnectReason; | 163 return this.state_.disconnectReason; |
| 142 }; | 164 }; |
| 143 | 165 |
| 144 InterfaceEndpointHandle.prototype.setAssociationEventHandler = function( | 166 InterfaceEndpointHandle.prototype.setAssociationEventHandler = function( |
| 145 handler) { | 167 handler) { |
| 146 this.state_.setAssociationEventHandler(handler); | 168 this.state_.setAssociationEventHandler(handler); |
| 147 }; | 169 }; |
| 148 | 170 |
| 171 InterfaceEndpointHandle.prototype.notifyAssociation = function(interfaceId, |
| 172 peerGroupController) { |
| 173 return this.state_.notifyAssociation(interfaceId, peerGroupController); |
| 174 }; |
| 175 |
| 149 InterfaceEndpointHandle.prototype.reset = function(reason) { | 176 InterfaceEndpointHandle.prototype.reset = function(reason) { |
| 150 this.state_.close(reason); | 177 this.state_.close(reason); |
| 151 this.state_ = new State(); | 178 this.state_ = new State(); |
| 152 }; | 179 }; |
| 153 | 180 |
| 154 var exports = {}; | 181 var exports = {}; |
| 182 exports.AssociationEvent = AssociationEvent; |
| 155 exports.InterfaceEndpointHandle = InterfaceEndpointHandle; | 183 exports.InterfaceEndpointHandle = InterfaceEndpointHandle; |
| 184 exports.createPairPendingAssociation = createPairPendingAssociation; |
| 156 | 185 |
| 157 return exports; | 186 return exports; |
| 158 }); | 187 }); |
| OLD | NEW |