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 relay messages between the hangout web page (hangout) and | |
Jamie
2014/08/12 02:24:57
s/hangout/Hangouts/
Jamie
2014/08/12 02:24:58
s/relay/relays/
| |
9 * the chrome remote desktop (webapp) for the helper (The person who is giving | |
Jamie
2014/08/12 02:24:58
s/The/the/
Jamie
2014/08/12 02:24:58
Chrome Remote Desktop (capitalization).
kelvinp
2014/08/12 21:42:39
Done.
kelvinp
2014/08/12 21:42:40
Done.
| |
10 * remoting assistance). | |
Jamie
2014/08/12 02:24:58
s/remoting/remote/
kelvinp
2014/08/12 21:42:40
Done.
| |
11 * | |
12 * It contains two port object, with each representing a two-way connection | |
13 * between the background script and the webapp/hangout. | |
Jamie
2014/08/12 02:24:58
I think this whole paragraph can be more succinctl
| |
14 * | |
15 * Connection is always initiated from Hangout. | |
Jamie
2014/08/12 02:24:58
s/Hangout/the hangout/
kelvinp
2014/08/12 21:42:39
Done.
| |
16 * | |
17 * Hangout It2MeHelperChannel | |
18 * |-----runtime.connect() ----->| | |
19 * |------connect message------->|---------launch----> Chrome remote desktop | |
Jamie
2014/08/12 02:24:58
Even though CRD is not running until this step, I
kelvinp
2014/08/12 21:42:41
Done.
| |
20 * | |<------runtime.connect()-------| | |
21 * |<----sessionStateChanged-----|<-----sessionStateChanged------| | |
22 * | | | | |
23 * | |
24 * Disconnection can be initiated from either side: | |
25 * 1. In the normal flow initiated from hangout | |
26 * a) hangout ---- ------ disconnect message ---------> It2MeHelperChannel | |
27 * b) hangout <---------- sessionStateChanged(CLOSED) - It2MeHelperChannel | |
28 * c) It2MeHelperChannel- window.close() -----------> Webapp | |
Jamie
2014/08/12 02:24:58
Can these diagrams be made to fit the layout above
kelvinp
2014/08/12 21:42:40
Done.
| |
29 * | |
30 * 2. In the normal flow initiated from webapp | |
31 * a) webapp ------------ sessionStateChanged(CLOSED)-> It2MeHelperChannel | |
32 * b) webapp ------------ port.disconnect() ----------> It2MeHelperChannel | |
33 * c) It2MeHelperChannel- port.disconnect() ----------> hangout | |
34 * | |
35 * 2. If hangout crashes | |
36 * a) hangout ------------> port.disconnect()-----> It2MeHelperChannel | |
37 * b) It2MeHelperChannel -> port.disconnect()-----> webapp | |
38 * b) It2MeHelperChannel -> window.close()--------> webapp | |
39 * | |
40 * 3. If webapp crashes | |
41 * a) webapp -------------> port.disconnect()-----------> It2MeHelperChannel | |
42 * b) It2MeHelperChannel -> sessionStateChanged(FAILED)-> hangout | |
43 * b) It2MeHelperChannel -> port.disconnect()-----------> hangout | |
44 * | |
Jamie
2014/08/12 02:24:57
Nit: Unnecessary blank line.
kelvinp
2014/08/12 21:42:39
Done.
| |
45 */ | |
46 | |
47 'use strict'; | |
48 | |
49 /** @suppress {duplicate} */ | |
50 var remoting = remoting || {}; | |
51 | |
52 /** | |
53 * @param {remoting.AppLauncher} appLauncher | |
54 * @param {chrome.runtime.Port} hangoutPort | |
Jamie
2014/08/12 02:24:57
Please add a description for this parameter.
kelvinp
2014/08/12 21:42:40
Done.
| |
55 * @param {function(*)} onDisconnectCallback | |
56 * Callback to notify when the | |
Jamie
2014/08/12 02:24:58
Formatting.
kelvinp
2014/08/12 21:42:39
Done.
| |
57 * connection is torn down. IT2MeServices uses this callback to dispose the | |
Jamie
2014/08/12 02:24:57
s/Services/Service/
Jamie
2014/08/12 02:24:58
s/dispose/dispose of/
kelvinp
2014/08/12 21:42:40
Done.
| |
58 * channel object. | |
59 * @constructor | |
60 */ | |
61 remoting.It2MeHelperChannel = | |
62 function(appLauncher, hangoutPort, onDisconnectCallback) { | |
63 | |
64 /** | |
65 * @private | |
Jamie
2014/08/12 02:24:57
Nit: @private should come after the type (for cons
kelvinp
2014/08/12 21:42:39
Done.
| |
66 * @type {remoting.AppLauncher} | |
67 */ | |
68 this.appLauncher_ = appLauncher; | |
69 | |
70 /** | |
71 * @private | |
72 * @type {chrome.runtime.Port} | |
73 */ | |
74 this.hangoutPort_ = hangoutPort; | |
75 | |
76 /** | |
77 * @private | |
78 * @type {chrome.runtime.Port} | |
79 */ | |
80 this.webappPort_ = null; | |
81 | |
82 /** | |
83 * @private | |
84 * @type {string} | |
85 */ | |
86 this.instanceId_ = ""; | |
Jamie
2014/08/12 02:24:57
Nit: Single-quotes for strings.
| |
87 | |
88 /** | |
89 * @private | |
90 * @type {remoting.ClientSession.State} | |
91 */ | |
92 this.sessionState_ = remoting.ClientSession.State.CONNECTING; | |
93 | |
94 this.onWebappMessageRef_ = this.onWebappMessage_.bind(this); | |
95 this.onWebappDisconnectRef_ = this.onWebappDisconnect_.bind(this); | |
96 this.onHangoutMessageRef_ = this.onHangoutMessage_.bind(this); | |
97 this.onHangoutDisconnectRef_ = this.onHangoutDisconnect_.bind(this); | |
98 this.onDisconnectCallback_ = onDisconnectCallback; | |
Jamie
2014/08/12 02:24:57
I think it's clearer to save all the ctor paramete
kelvinp
2014/08/12 21:42:39
Done.
| |
99 }; | |
100 | |
101 remoting.It2MeHelperChannel.HangoutMessageTypes = { | |
Jamie
2014/08/12 02:24:58
Add @enum annotation?
kelvinp
2014/08/12 21:42:40
Done.
| |
102 CONNECT: 'connect', | |
103 DISCONNECT: 'disconnect' | |
104 }; | |
105 | |
106 remoting.It2MeHelperChannel.WebappMessageTypes = { | |
107 SESSION_STATE_CHANGED: 'sessionStateChanged' | |
108 }; | |
109 | |
110 remoting.It2MeHelperChannel.prototype.init = function() { | |
Jamie
2014/08/12 02:24:57
Please add some doc comments to this and other met
| |
111 this.hangoutPort_.onMessage.addListener(this.onHangoutMessageRef_); | |
112 this.hangoutPort_.onDisconnect.addListener(this.onHangoutDisconnectRef_); | |
113 }; | |
114 | |
115 /** @return {string} */ | |
116 remoting.It2MeHelperChannel.prototype.instanceId = function() { | |
117 return this.instanceId_; | |
118 }; | |
119 | |
120 /** | |
121 * @param {chrome.runtime.Port} port | |
122 * @param {Object=} opt_value | |
123 * @param {string=} opt_error | |
124 */ | |
125 remoting.It2MeHelperChannel.prototype.sendResponse_ = | |
126 function(port, opt_value, opt_error) { | |
127 var errorMessage = (opt_error) ? opt_error : chrome.runtime.lastError.message; | |
128 port.postMessage({'value': opt_value, 'error': errorMessage}); | |
129 }; | |
130 | |
131 /** | |
132 * @param {{method:string, data:Object.<string,*>}} message | |
133 * @return {boolean} whether the message is handled or not. | |
134 */ | |
135 remoting.It2MeHelperChannel.prototype.onHangoutMessage_ = function(message) { | |
136 try { | |
137 var MessageTypes = remoting.It2MeHelperChannel.HangoutMessageTypes; | |
138 switch (message.method) { | |
139 case MessageTypes.CONNECT: | |
140 this.launchWebapp_(message); | |
141 return true; | |
142 case MessageTypes.DISCONNECT: | |
143 this.closeWebapp_(message); | |
144 return true; | |
145 } | |
146 } catch(e) { | |
147 var error = /** @type {Error} */ e; | |
148 console.error(error); | |
149 this.sendResponse_(this.hangoutPort_, null, error.message); | |
150 } | |
151 return false; | |
152 }; | |
153 | |
154 /** | |
155 * Disconnect the existing connection to the helpee. | |
156 * @param {{method:string, data:Object.<string,*>}} message | |
157 */ | |
158 remoting.It2MeHelperChannel.prototype.closeWebapp_ = | |
159 function(message) { | |
160 // TODO(kepoon): Closing the v2 app current doesn't disconnect the IT2me | |
161 // session crbug.com/402137. | |
Jamie
2014/08/12 02:24:58
It's not clear what this comment is trying to say.
kelvinp
2014/08/12 21:42:40
Done.
| |
162 this.sessionState_ = remoting.ClientSession.State.CLOSED; | |
163 this.hangoutPort_.postMessage({ | |
164 method: 'sessionStateChanged', | |
165 state: this.sessionState_ | |
166 }); | |
167 this.appLauncher_.close(this.instanceId_); | |
168 }; | |
169 | |
170 /** | |
171 * @param {{method:string, data:Object.<string,*>}} message | |
172 */ | |
173 remoting.It2MeHelperChannel.prototype.launchWebapp_ = | |
174 function(message) { | |
175 var accessCode = getStringAttr(message, 'accessCode'); | |
176 if (!accessCode) { | |
177 throw new Error('AccessCode is missing'); | |
Jamie
2014/08/12 02:24:58
s/Access/access/
kelvinp
2014/08/12 21:42:39
Done.
| |
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_ = instanceId; | |
191 }.bind(this)); | |
192 }; | |
193 | |
194 remoting.It2MeHelperChannel.prototype.onHangoutDisconnect_ = | |
195 function() { | |
196 // Runtime port do not fire disconnect events synchronously so | |
Jamie
2014/08/12 02:24:57
s/Runtime port do/chrome.runtime.Port does/
kelvinp
2014/08/12 21:42:41
Done.
| |
197 // appLauncher_.close won't invoke onWebappDiscconect | |
Jamie
2014/08/12 02:24:57
s/Discconect/Disconnect/
Jamie
2014/08/12 02:24:58
I don't understand what this comment is trying to
kelvinp
2014/08/12 21:42:39
Done.
kelvinp
2014/08/12 21:42:40
Comments removed
| |
198 this.appLauncher_.close(this.instanceId_); | |
199 this.unhookPorts_(); | |
200 }; | |
201 | |
202 /** | |
203 * @param {chrome.runtime.Port} port | |
204 * @param {string} id | |
205 */ | |
206 remoting.It2MeHelperChannel.prototype.onWebappConnect = function(port, id) { | |
207 base.debug.assert(id === this.instanceId_); | |
208 base.debug.assert(Boolean(this.hangoutPort_)); | |
Jamie
2014/08/12 02:24:57
I think "this.hangoutPort_ != null" would be clear
kelvinp
2014/08/12 21:42:39
Done.
| |
209 | |
210 // Hook listeners. | |
211 port.onMessage.addListener(this.onWebappMessageRef_); | |
212 port.onDisconnect.addListener(this.onWebappDisconnectRef_); | |
213 this.webappPort_ = port; | |
214 }; | |
215 | |
216 /** @param {chrome.runtime.Port} port */ | |
217 remoting.It2MeHelperChannel.prototype.onWebappDisconnect_ = function(port) { | |
218 // If the port got disconnected while the session is still connected, | |
219 // treat it as an error. | |
220 var States = remoting.ClientSession.State; | |
221 if (this.sessionState_ === States.CONNECTING || | |
222 this.sessionState_ === States.CONNECTED) { | |
223 this.sessionState_ = States.FAILED; | |
224 this.hangoutPort_.postMessage({ | |
225 method: 'sessionStateChanged', | |
226 state: this.sessionState_ | |
227 }); | |
228 } | |
229 this.unhookPorts_(); | |
230 }; | |
231 | |
232 /** | |
233 * @param {{method:string, data:Object.<string,*>}} message | |
234 */ | |
235 remoting.It2MeHelperChannel.prototype.onWebappMessage_ = function(message) { | |
236 try { | |
237 console.log('It2MeHelperChannel id:=' + this.instanceId_ + | |
238 ' incoming message method:=' + message.method); | |
Jamie
2014/08/12 02:24:58
Can this be removed? Console spam makes it harder
kelvinp
2014/08/12 21:42:40
This will only log to the console of the backgroun
| |
239 var MessageTypes = remoting.It2MeHelperChannel.WebappMessageTypes; | |
240 switch (message.method) { | |
241 case MessageTypes.SESSION_STATE_CHANGED: | |
242 var state = getNumberAttr(message, 'state'); | |
243 this.sessionState_ = | |
244 /** @type {remoting.ClientSession.State} */ state; | |
245 this.hangoutPort_.postMessage(message); | |
246 return true; | |
247 } | |
248 throw new Error('Unknown messages.'); | |
Jamie
2014/08/12 02:24:57
s/messages/message/
Also, include message.method
kelvinp
2014/08/12 21:42:38
Done.
| |
249 } catch(e) { | |
250 var error = /** @type {Error} */ e; | |
251 console.error(error); | |
252 this.sendResponse_(this.hangoutPort_, null, error.message); | |
253 } | |
254 return false; | |
255 }; | |
256 | |
257 | |
Jamie
2014/08/12 02:24:57
Nit: Unnecessary blank line.
kelvinp
2014/08/12 21:42:39
Done.
| |
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 |