Chromium Code Reviews| Index: mojo/public/js/lib/interface_endpoint_handle.js |
| diff --git a/mojo/public/js/lib/interface_endpoint_handle.js b/mojo/public/js/lib/interface_endpoint_handle.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3e3d7ffc240087bf55b1cc3562d7c7e21d10ae10 |
| --- /dev/null |
| +++ b/mojo/public/js/lib/interface_endpoint_handle.js |
| @@ -0,0 +1,138 @@ |
| +// Copyright 2017 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. |
| + |
| +define("mojo/public/js/lib/interface_endpoint_handle", [ |
| + "mojo/public/js/interface_types", |
| + "timer", |
| +], function(types, timer) { |
| + |
| + var AssociationEvent = { |
| + // The interface has been associated with a message pipe. |
| + ASSOCIATED: 'associated', |
| + // The peer of this object has been closed before association. |
| + PEER_CLOSED_BEFORE_ASSOCIATION: 'peer_closed_before_association' |
| + }; |
| + |
| + function State(interfaceId, associatedGroupController) { |
| + if (interfaceId === undefined) { |
| + interfaceId = types.kInvalidInterfaceId |
|
yzshen1
2017/03/24 21:20:13
trailing ";"
wangjimmy
2017/03/27 16:51:28
Done.
|
| + } |
| + |
| + this.interfaceId = interfaceId; |
| + this.associatedGroupController = associatedGroupController; |
| + this.pendingAssociation = false; |
| + this.disconnectReason = null; |
| + this.peerState_ = null; |
| + this.associationEventHandler_ = undefined; |
| + } |
| + |
| + State.prototype.initPendingState = function(peer) { |
| + this.pendingAssociation = true; |
| + this.peerState_ = peer; |
| + }; |
| + |
| + State.prototype.isValid = function() { |
| + return this.pendingAssociation || |
| + types.isValidInterfaceId(this.interfaceId); |
| + }; |
| + |
| + State.prototype.close = function(disconnectReason) { |
|
yzshen1
2017/03/24 21:20:13
Please also reset associationEventHandler_, interf
wangjimmy
2017/03/27 16:51:28
Done.
|
| + if (this.associatedGroupController) { |
| + this.associatedGroupController.closeEndpointHandle(this.interfaceId, |
| + disconnectReason); |
| + } else if (this.peerState_) { |
| + this.peerState_.onPeerClosedBeforeAssociation(disconnectReason); |
| + } |
| + }; |
| + |
| + State.prototype.runAssociationEventHandler = function(associationEvent) { |
| + function run() { |
| + this.associationEventHandler_(associationEvent); |
| + this.associationEventHandler_ = undefined; |
|
yzshen1
2017/03/24 21:20:13
Although it shouldn't matter in this particular ca
wangjimmy
2017/03/27 16:51:28
Done, changed it. Since you're right a callback co
|
| + } |
| + |
| + if (this.associationEventHandler_) { |
| + timer.createOneShot(0, run.bind(this)); |
|
yzshen1
2017/03/24 21:20:13
At the time when run() is run, this.asssociationEv
wangjimmy
2017/03/27 16:51:28
Done.
|
| + } |
| + }; |
| + |
| + State.prototype.setAssociationEventHandler = function(handler) { |
| + if ((!this.pendingAssociation && |
| + !types.isValidInterfaceId(this.interfaceId)) || !handler) { |
|
yzshen1
2017/03/24 21:20:13
If handler is null, you need to reset this.associa
wangjimmy
2017/03/27 16:51:28
Done.
|
| + return; |
| + } |
| + |
| + this.associationEventHandler_ = handler; |
| + if (!this.pendingAssociation) { |
| + this.runAssociationEventHandler(AssociationEvent.ASSOCIATED); |
| + } else if (!this.peerState_) { |
| + this.runAssociationEventHandler( |
| + AssociationEvent.PEER_CLOSED_BEFORE_ASSOCIATION); |
| + } |
| + }; |
| + |
| + State.prototype.onAssociated = function(interfaceId, |
| + associatedGroupController) { |
| + if (!this.pendingAssociation) { |
| + return; |
| + } |
| + |
| + this.pendingAssociation = false; |
| + this.peerState_ = null; |
| + this.interfaceId = interfaceId; |
| + this.associatedGroupController = associatedGroupController; |
| + this.runAssociationEventHandler(AssociationEvent.ASSOCIATED); |
|
yzshen1
2017/03/24 21:20:13
We have to run the handler immediately instead of
wangjimmy
2017/03/27 16:51:28
Done.
|
| + }; |
| + |
| + State.prototype.onPeerClosedBeforeAssociation = function(disconnectReason) { |
| + if (!this.pendingAssociation) { |
| + return; |
| + } |
| + |
| + this.peerState_ = null; |
| + this.disconnectReason = disconnectReason; |
| + |
| + this.runAssociationEventHandler( |
|
yzshen1
2017/03/24 21:20:13
Run the handler immediately instead of delaying it
wangjimmy
2017/03/27 16:51:28
Done.
|
| + AssociationEvent.PEER_CLOSED_BEFORE_ASSOCIATION); |
| + }; |
| + |
| + function InterfaceEndpointHandle(interfaceId, associatedGroupController) { |
| + this.state_ = new State(interfaceId, associatedGroupController); |
| + } |
| + |
| + InterfaceEndpointHandle.prototype.isValid = function() { |
| + return this.state_.isValid(); |
| + }; |
| + |
| + InterfaceEndpointHandle.prototype.pendingAssociation = function() { |
| + return this.state_.pendingAssociation; |
| + }; |
| + |
| + InterfaceEndpointHandle.prototype.id = function() { |
| + return this.state_.interfaceId; |
| + }; |
| + |
| + InterfaceEndpointHandle.prototype.groupController = function() { |
| + return this.state_.associatedGroupController; |
| + }; |
| + |
| + InterfaceEndpointHandle.prototype.disconnectReason = function() { |
| + return this.state_.disconnectReason; |
| + }; |
| + |
| + InterfaceEndpointHandle.prototype.setAssociationEventHandler = function( |
| + handler) { |
| + this.state_.setAssociationEventHandler(handler); |
| + }; |
| + |
| + InterfaceEndpointHandle.prototype.reset = function(reason) { |
| + this.state_.close(reason); |
| + this.state_ = new State(); |
| + }; |
| + |
| + var exports = {}; |
| + exports.InterfaceEndpointHandle = InterfaceEndpointHandle; |
| + |
| + return exports; |
| +}); |