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, associatedGroupController) { | |
18 if (interfaceId === undefined) { | |
19 interfaceId = types.kInvalidInterfaceId | |
yzshen1
2017/03/24 21:20:13
trailing ";"
wangjimmy
2017/03/27 16:51:28
Done.
| |
20 } | |
21 | |
22 this.interfaceId = interfaceId; | |
23 this.associatedGroupController = associatedGroupController; | |
24 this.pendingAssociation = false; | |
25 this.disconnectReason = null; | |
26 this.peerState_ = null; | |
27 this.associationEventHandler_ = undefined; | |
28 } | |
29 | |
30 State.prototype.initPendingState = function(peer) { | |
31 this.pendingAssociation = true; | |
32 this.peerState_ = peer; | |
33 }; | |
34 | |
35 State.prototype.isValid = function() { | |
36 return this.pendingAssociation || | |
37 types.isValidInterfaceId(this.interfaceId); | |
38 }; | |
39 | |
40 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.
| |
41 if (this.associatedGroupController) { | |
42 this.associatedGroupController.closeEndpointHandle(this.interfaceId, | |
43 disconnectReason); | |
44 } else if (this.peerState_) { | |
45 this.peerState_.onPeerClosedBeforeAssociation(disconnectReason); | |
46 } | |
47 }; | |
48 | |
49 State.prototype.runAssociationEventHandler = function(associationEvent) { | |
50 function run() { | |
51 this.associationEventHandler_(associationEvent); | |
52 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
| |
53 } | |
54 | |
55 if (this.associationEventHandler_) { | |
56 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.
| |
57 } | |
58 }; | |
59 | |
60 State.prototype.setAssociationEventHandler = function(handler) { | |
61 if ((!this.pendingAssociation && | |
62 !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.
| |
63 return; | |
64 } | |
65 | |
66 this.associationEventHandler_ = handler; | |
67 if (!this.pendingAssociation) { | |
68 this.runAssociationEventHandler(AssociationEvent.ASSOCIATED); | |
69 } else if (!this.peerState_) { | |
70 this.runAssociationEventHandler( | |
71 AssociationEvent.PEER_CLOSED_BEFORE_ASSOCIATION); | |
72 } | |
73 }; | |
74 | |
75 State.prototype.onAssociated = function(interfaceId, | |
76 associatedGroupController) { | |
77 if (!this.pendingAssociation) { | |
78 return; | |
79 } | |
80 | |
81 this.pendingAssociation = false; | |
82 this.peerState_ = null; | |
83 this.interfaceId = interfaceId; | |
84 this.associatedGroupController = associatedGroupController; | |
85 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.
| |
86 }; | |
87 | |
88 State.prototype.onPeerClosedBeforeAssociation = function(disconnectReason) { | |
89 if (!this.pendingAssociation) { | |
90 return; | |
91 } | |
92 | |
93 this.peerState_ = null; | |
94 this.disconnectReason = disconnectReason; | |
95 | |
96 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.
| |
97 AssociationEvent.PEER_CLOSED_BEFORE_ASSOCIATION); | |
98 }; | |
99 | |
100 function InterfaceEndpointHandle(interfaceId, associatedGroupController) { | |
101 this.state_ = new State(interfaceId, associatedGroupController); | |
102 } | |
103 | |
104 InterfaceEndpointHandle.prototype.isValid = function() { | |
105 return this.state_.isValid(); | |
106 }; | |
107 | |
108 InterfaceEndpointHandle.prototype.pendingAssociation = function() { | |
109 return this.state_.pendingAssociation; | |
110 }; | |
111 | |
112 InterfaceEndpointHandle.prototype.id = function() { | |
113 return this.state_.interfaceId; | |
114 }; | |
115 | |
116 InterfaceEndpointHandle.prototype.groupController = function() { | |
117 return this.state_.associatedGroupController; | |
118 }; | |
119 | |
120 InterfaceEndpointHandle.prototype.disconnectReason = function() { | |
121 return this.state_.disconnectReason; | |
122 }; | |
123 | |
124 InterfaceEndpointHandle.prototype.setAssociationEventHandler = function( | |
125 handler) { | |
126 this.state_.setAssociationEventHandler(handler); | |
127 }; | |
128 | |
129 InterfaceEndpointHandle.prototype.reset = function(reason) { | |
130 this.state_.close(reason); | |
131 this.state_ = new State(); | |
132 }; | |
133 | |
134 var exports = {}; | |
135 exports.InterfaceEndpointHandle = InterfaceEndpointHandle; | |
136 | |
137 return exports; | |
138 }); | |
OLD | NEW |