OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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 /** | |
6 * @fileoverview | |
7 * | |
8 * It2MeHelperChannel relays messages between Hangouts and Chrome Remote Desktop | |
9 * (webapp) for the helper (the Hangouts participant who is giving remote | |
10 * assistance). | |
11 * | |
12 * It runs in the background page and contains two chrome.runtime.Port objects, | |
13 * respresenting connections to the webapp and hangout, respectively. | |
14 * | |
15 * Connection is always initiated from Hangouts. | |
16 * | |
17 * Hangout It2MeHelperChannel Chrome Remote Desktop | |
18 * |-----runtime.connect() ------>| | | |
19 * |------connect message-------->| | | |
20 * | |-------appLauncher.launch()---->| | |
21 * | |<------runtime.connect()------- | | |
22 * | |<-----sessionStateChanged------ | | |
23 * |<----sessionStateChanged------| | | |
24 * | |
25 * Disconnection can be initiated from either side: | |
26 * 1. In the normal flow initiated from hangout | |
27 * Hangout It2MeHelperChannel Chrome Remote Desktop | |
28 * |-----disconnect message------>| | | |
29 * |<-sessionStateChanged(CLOSED)-| | | |
30 * | |-----appLauncher.close()------>| | |
31 * | |
32 * 2. In the normal flow initiated from webapp | |
33 * Hangout It2MeHelperChannel Chrome Remote Desktop | |
34 * | |<-sessionStateChanged(CLOSED)--| | |
35 * | |<--------port.disconnect()-----| | |
36 * |<--------port.disconnect()----| | | |
37 * | |
38 * 2. If hangout crashes | |
39 * Hangout It2MeHelperChannel Chrome Remote Desktop | |
40 * |---------port.disconnect()--->| | | |
41 * | |--------port.disconnect()----->| | |
42 * | |------appLauncher.close()----->| | |
43 * | |
44 * 3. If webapp crashes | |
45 * Hangout It2MeHelperChannel Chrome Remote Desktop | |
46 * | |<-------port.disconnect()------| | |
47 * |<-sessionStateChanged(FAILED)-| | | |
48 * |<--------port.disconnect()----| | | |
49 */ | |
50 | |
51 'use strict'; | |
52 | |
53 /** @suppress {duplicate} */ | |
54 var remoting = remoting || {}; | |
55 | |
56 /** | |
57 * @param {remoting.AppLauncher} appLauncher | |
58 * @param {chrome.runtime.Port} hangoutPort | |
59 * Represents an active connection to Hangouts. | |
Jamie
2014/08/13 01:44:52
For consistency, param descriptions should start o
kelvinp
2014/08/13 23:44:10
Done.
| |
60 * @param {function(*)} onDisconnectCallback | |
61 * Callback to notify when the connection is torn down. IT2MeService uses | |
62 * this callback to dispose of the channel object. | |
63 * @constructor | |
64 */ | |
65 remoting.It2MeHelperChannel = | |
66 function(appLauncher, hangoutPort, onDisconnectCallback) { | |
67 | |
68 /** | |
69 * @type {remoting.AppLauncher} | |
70 * @private | |
71 */ | |
72 this.appLauncher_ = appLauncher; | |
73 | |
74 /** | |
75 * @type {chrome.runtime.Port} | |
76 * @private | |
77 */ | |
78 this.hangoutPort_ = hangoutPort; | |
79 | |
80 /** | |
81 * @type {chrome.runtime.Port} | |
82 * @private | |
83 */ | |
84 this.webappPort_ = null; | |
85 | |
86 /** | |
87 * @type {string} | |
88 * @private | |
89 */ | |
90 this.instanceId_ = ''; | |
91 | |
92 /** | |
93 * @type {remoting.ClientSession.State} | |
94 * @private | |
95 */ | |
96 this.sessionState_ = remoting.ClientSession.State.CONNECTING; | |
97 this.onDisconnectCallback_ = onDisconnectCallback; | |
Jamie
2014/08/13 01:44:52
Unless it fits more logically here, I think it wou
kelvinp
2014/08/13 23:44:10
Done.
Jamie
2014/08/14 00:25:06
You haven't moved it to be with the other paramete
| |
98 | |
99 this.onWebappMessageRef_ = this.onWebappMessage_.bind(this); | |
100 this.onWebappDisconnectRef_ = this.onWebappDisconnect_.bind(this); | |
101 this.onHangoutMessageRef_ = this.onHangoutMessage_.bind(this); | |
102 this.onHangoutDisconnectRef_ = this.onHangoutDisconnect_.bind(this); | |
103 }; | |
104 | |
105 /** @enum {string} */ | |
106 remoting.It2MeHelperChannel.HangoutMessageTypes = { | |
107 CONNECT: 'connect', | |
108 DISCONNECT: 'disconnect' | |
109 }; | |
110 | |
111 /** @enum {string} */ | |
112 remoting.It2MeHelperChannel.WebappMessageTypes = { | |
113 SESSION_STATE_CHANGED: 'sessionStateChanged' | |
114 }; | |
115 | |
116 remoting.It2MeHelperChannel.prototype.init = function() { | |
117 this.hangoutPort_.onMessage.addListener(this.onHangoutMessageRef_); | |
118 this.hangoutPort_.onDisconnect.addListener(this.onHangoutDisconnectRef_); | |
119 }; | |
120 | |
121 /** @return {string} */ | |
122 remoting.It2MeHelperChannel.prototype.instanceId = function() { | |
123 return this.instanceId_; | |
124 }; | |
125 | |
126 /** | |
127 * @param {{method:string, data:Object.<string,*>}} message | |
128 * @return {boolean} whether the message is handled or not. | |
Jamie
2014/08/13 01:44:52
@private, or no underscore, here and elsewhere.
kelvinp
2014/08/13 23:44:10
Done.
| |
129 */ | |
130 remoting.It2MeHelperChannel.prototype.onHangoutMessage_ = function(message) { | |
131 try { | |
132 var MessageTypes = remoting.It2MeHelperChannel.HangoutMessageTypes; | |
133 switch (message.method) { | |
134 case MessageTypes.CONNECT: | |
135 this.launchWebapp_(message); | |
136 return true; | |
137 case MessageTypes.DISCONNECT: | |
138 this.closeWebapp_(message); | |
139 return true; | |
140 } | |
141 } catch(e) { | |
142 var error = /** @type {Error} */ e; | |
143 console.error(error); | |
144 this.hangoutPort_.postMessage({ | |
145 method: message.method + 'Response', | |
146 error: error.message | |
147 }); | |
148 } | |
149 return false; | |
150 }; | |
151 | |
152 /** | |
153 * Disconnect the existing connection to the helpee. | |
154 * | |
155 * @param {{method:string, data:Object.<string,*>}} message | |
156 */ | |
157 remoting.It2MeHelperChannel.prototype.closeWebapp_ = | |
158 function(message) { | |
159 // TODO(kepoon): Closing the v2 app current doesn't disconnect the IT2me | |
Jamie
2014/08/13 01:44:52
s/current/currently/
Jamie
2014/08/13 01:44:52
Wrong user id :)
kelvinp
2014/08/13 23:44:09
Done.
kelvinp
2014/08/13 23:44:10
Done.
| |
160 // session crbug.com/402137, so send an explicit notification to the hangout. | |
Jamie
2014/08/13 01:44:53
Parentheses around the bug.
kelvinp
2014/08/13 23:44:09
Done.
| |
161 this.sessionState_ = remoting.ClientSession.State.CLOSED; | |
162 this.hangoutPort_.postMessage({ | |
163 method: 'sessionStateChanged', | |
164 state: this.sessionState_ | |
165 }); | |
166 this.appLauncher_.close(this.instanceId_); | |
Jamie
2014/08/13 01:44:52
I'm not sure it makes much difference, but if the
kelvinp
2014/08/13 23:44:09
this.appLauncher_.close will disconnect the port a
| |
167 }; | |
168 | |
169 /** | |
170 * Launches the web app. | |
171 * @param {{method:string, data:Object.<string,*>}} message | |
Jamie
2014/08/13 01:44:52
Blank line between description and annotations.
kelvinp
2014/08/13 23:44:10
Done.
| |
172 */ | |
173 remoting.It2MeHelperChannel.prototype.launchWebapp_ = | |
174 function(message) { | |
175 var accessCode = getStringAttr(message, 'accessCode'); | |
176 if (!accessCode) { | |
177 throw new Error('Access code is missing'); | |
178 } | |
179 | |
180 // Launch the webapp. | |
181 this.appLauncher_.launch({ | |
182 mode: 'hangout', | |
183 accessCode: accessCode | |
184 }).then( | |
185 /** | |
186 * @this {remoting.It2MeHelperChannel} | |
187 * @param {string} instanceId | |
188 */ | |
189 function(instanceId){ | |
190 this.instanceId_ = String(instanceId); | |
Jamie
2014/08/13 01:44:52
Do you need String here?
kelvinp
2014/08/13 23:44:09
Done.
| |
191 }.bind(this)); | |
Jamie
2014/08/13 01:44:52
Closing brace should be aligned with 'function'.
kelvinp
2014/08/13 23:44:09
Done.
| |
192 }; | |
193 | |
194 remoting.It2MeHelperChannel.prototype.onHangoutDisconnect_ = | |
195 function() { | |
Jamie
2014/08/13 01:44:52
No need for line break I don't think.
kelvinp
2014/08/13 23:44:09
Done.
| |
196 this.appLauncher_.close(this.instanceId_); | |
Jamie
2014/08/13 01:44:52
In the non-error case, this is going to result in
kelvinp
2014/08/13 23:44:10
This won't need to two calls to close. The discon
| |
197 this.unhookPorts_(); | |
198 }; | |
199 | |
200 /** | |
201 * @param {chrome.runtime.Port} port | |
202 * @param {string} id | |
Jamie
2014/08/13 01:44:53
I think these parameters would benefit from docume
kelvinp
2014/08/13 23:44:09
Done.
| |
203 */ | |
204 remoting.It2MeHelperChannel.prototype.onWebappConnect = function(port, id) { | |
205 base.debug.assert(id === this.instanceId_); | |
206 base.debug.assert(this.hangoutPort_ !== null); | |
207 | |
208 // Hook listeners. | |
209 port.onMessage.addListener(this.onWebappMessageRef_); | |
210 port.onDisconnect.addListener(this.onWebappDisconnectRef_); | |
211 this.webappPort_ = port; | |
212 }; | |
213 | |
214 /** @param {chrome.runtime.Port} port */ | |
215 remoting.It2MeHelperChannel.prototype.onWebappDisconnect_ = function(port) { | |
216 // If the port got disconnected while the session is still connected, | |
Jamie
2014/08/13 01:44:52
Please specify which port.
kelvinp
2014/08/13 23:44:09
Done.
| |
217 // treat it as an error. | |
218 var States = remoting.ClientSession.State; | |
219 if (this.sessionState_ === States.CONNECTING || | |
220 this.sessionState_ === States.CONNECTED) { | |
221 this.sessionState_ = States.FAILED; | |
222 this.hangoutPort_.postMessage({ | |
223 method: 'sessionStateChanged', | |
224 state: this.sessionState_ | |
225 }); | |
226 } | |
227 this.unhookPorts_(); | |
228 }; | |
229 | |
230 /** | |
231 * @param {{method:string, data:Object.<string,*>}} message | |
232 */ | |
233 remoting.It2MeHelperChannel.prototype.onWebappMessage_ = function(message) { | |
234 try { | |
235 console.log('It2MeHelperChannel id:=' + this.instanceId_ + | |
kelvinp
2014/08/13 00:08:48
This statement will only log to the console of bac
Jamie
2014/08/13 01:44:52
As long as the protocol isn't too chatty, that's f
| |
236 ' incoming message method:=' + message.method); | |
237 var MessageTypes = remoting.It2MeHelperChannel.WebappMessageTypes; | |
238 switch (message.method) { | |
239 case MessageTypes.SESSION_STATE_CHANGED: | |
240 var state = getNumberAttr(message, 'state'); | |
241 this.sessionState_ = | |
242 /** @type {remoting.ClientSession.State} */ state; | |
243 this.hangoutPort_.postMessage(message); | |
244 return true; | |
245 } | |
246 throw new Error('Unknown message method:=' + message.method); | |
Jamie
2014/08/13 01:44:52
s/:=/: /
kelvinp
2014/08/13 23:44:09
Done.
| |
247 } catch(e) { | |
248 var error = /** @type {Error} */ e; | |
249 console.error(error); | |
250 this.webappPort_.postMessage({ | |
251 method: message.method + 'Response', | |
252 error: error.message | |
253 }); | |
254 } | |
255 return false; | |
256 }; | |
257 | |
258 remoting.It2MeHelperChannel.prototype.unhookPorts_ = function() { | |
259 if (this.webappPort_) { | |
260 this.webappPort_.onMessage.removeListener(this.onWebappMessageRef_); | |
261 this.webappPort_.onDisconnect.removeListener(this.onWebappDisconnectRef_); | |
262 this.webappPort_.disconnect(); | |
263 this.webappPort_ = null; | |
264 } | |
265 | |
266 if (this.hangoutPort_) { | |
267 this.hangoutPort_.onMessage.removeListener(this.onHangoutMessageRef_); | |
268 this.hangoutPort_.onDisconnect.removeListener(this.onHangoutDisconnectRef_); | |
269 this.hangoutPort_.disconnect(); | |
270 this.hangoutPort_ = null; | |
271 } | |
272 | |
273 if (this.onDisconnectCallback_) { | |
274 this.onDisconnectCallback_(this); | |
275 this.onDisconnectCallback_ = null; | |
276 } | |
277 }; | |
OLD | NEW |