Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 /** | 5 /** |
| 6 * @fileoverview | 6 * @fileoverview |
| 7 * | 7 * |
| 8 * It2MeHelperChannel relays messages between Hangouts and Chrome Remote Desktop | 8 * It2MeHelperChannel relays messages between Hangouts and Chrome Remote Desktop |
| 9 * (webapp) for the helper (the Hangouts participant who is giving remote | 9 * (webapp) for the helper (the Hangouts participant who is giving remote |
| 10 * assistance). | 10 * assistance). |
| 11 * | 11 * |
| 12 * It runs in the background page and contains two chrome.runtime.Port objects, | 12 * It runs in the background page and contains two chrome.runtime.Port objects, |
| 13 * respresenting connections to the webapp and hangout, respectively. | 13 * representing connections to the webapp and hangout, respectively. |
| 14 * | 14 * |
| 15 * Connection is always initiated from Hangouts. | 15 * Connection is always initiated from Hangouts by calling |
| 16 * var port = chrome.runtime.connect({name:'it2me.helper.hangout'}, extId). | |
| 17 * port.postMessage('hello') | |
| 18 * If the webapp is not installed, |port.onDisconnect| will fire. | |
| 19 * If the webapp is installed, Hangouts will receive a hello response with the | |
| 20 * list of supported features. | |
| 16 * | 21 * |
| 17 * Hangout It2MeHelperChannel Chrome Remote Desktop | 22 * Hangout It2MeHelperChannel Chrome Remote Desktop |
| 18 * |-----runtime.connect() ------>| | | 23 * |-----runtime.connect() ------>| | |
| 19 * |------connect message-------->| | | 24 * |--------hello message-------->| | |
| 25 * | |<-----helloResponse message-----| | |
| 26 * |-------connect message------->| | | |
| 20 * | |-------appLauncher.launch()---->| | 27 * | |-------appLauncher.launch()---->| |
| 21 * | |<------runtime.connect()------- | | 28 * | |<------runtime.connect()------- | |
| 22 * | |<-----sessionStateChanged------ | | 29 * | |<-----sessionStateChanged------ | |
| 23 * |<----sessionStateChanged------| | | 30 * |<----sessionStateChanged------| | |
| 24 * | 31 * |
| 25 * Disconnection can be initiated from either side: | 32 * Disconnection can be initiated from either side: |
| 26 * 1. In the normal flow initiated from hangout | 33 * 1. In the normal flow initiated from hangout |
| 27 * Hangout It2MeHelperChannel Chrome Remote Desktop | 34 * Hangout It2MeHelperChannel Chrome Remote Desktop |
| 28 * |-----disconnect message------>| | | 35 * |-----disconnect message------>| | |
| 29 * |<-sessionStateChanged(CLOSED)-| | | 36 * |<-sessionStateChanged(CLOSED)-| | |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 102 this.onDisconnectCallback_ = onDisconnectCallback; | 109 this.onDisconnectCallback_ = onDisconnectCallback; |
| 103 | 110 |
| 104 this.onWebappMessageRef_ = this.onWebappMessage_.bind(this); | 111 this.onWebappMessageRef_ = this.onWebappMessage_.bind(this); |
| 105 this.onWebappDisconnectRef_ = this.onWebappDisconnect_.bind(this); | 112 this.onWebappDisconnectRef_ = this.onWebappDisconnect_.bind(this); |
| 106 this.onHangoutMessageRef_ = this.onHangoutMessage_.bind(this); | 113 this.onHangoutMessageRef_ = this.onHangoutMessage_.bind(this); |
| 107 this.onHangoutDisconnectRef_ = this.onHangoutDisconnect_.bind(this); | 114 this.onHangoutDisconnectRef_ = this.onHangoutDisconnect_.bind(this); |
| 108 }; | 115 }; |
| 109 | 116 |
| 110 /** @enum {string} */ | 117 /** @enum {string} */ |
| 111 remoting.It2MeHelperChannel.HangoutMessageTypes = { | 118 remoting.It2MeHelperChannel.HangoutMessageTypes = { |
| 119 HELLO: 'hello', | |
| 120 HELLO_RESPONSE: 'helloResponse', | |
| 112 CONNECT: 'connect', | 121 CONNECT: 'connect', |
| 113 DISCONNECT: 'disconnect' | 122 DISCONNECT: 'disconnect', |
| 123 SESSION_STATE_CHANGED: 'sessionStateChanged', | |
| 124 ERROR: 'error' | |
| 125 }; | |
| 126 | |
| 127 /** @enum {string} */ | |
| 128 remoting.It2MeHelperChannel.Features = { | |
| 129 REMOTE_ASSISTANCE: 'remoteAssistance' | |
| 114 }; | 130 }; |
| 115 | 131 |
| 116 /** @enum {string} */ | 132 /** @enum {string} */ |
| 117 remoting.It2MeHelperChannel.WebappMessageTypes = { | 133 remoting.It2MeHelperChannel.WebappMessageTypes = { |
| 118 SESSION_STATE_CHANGED: 'sessionStateChanged' | 134 SESSION_STATE_CHANGED: 'sessionStateChanged', |
| 135 ERROR: 'error' | |
| 119 }; | 136 }; |
| 120 | 137 |
| 121 remoting.It2MeHelperChannel.prototype.init = function() { | 138 remoting.It2MeHelperChannel.prototype.init = function() { |
| 122 this.hangoutPort_.onMessage.addListener(this.onHangoutMessageRef_); | 139 this.hangoutPort_.onMessage.addListener(this.onHangoutMessageRef_); |
| 123 this.hangoutPort_.onDisconnect.addListener(this.onHangoutDisconnectRef_); | 140 this.hangoutPort_.onDisconnect.addListener(this.onHangoutDisconnectRef_); |
| 124 }; | 141 }; |
| 125 | 142 |
| 126 /** @return {string} */ | 143 /** @return {string} */ |
| 127 remoting.It2MeHelperChannel.prototype.instanceId = function() { | 144 remoting.It2MeHelperChannel.prototype.instanceId = function() { |
| 128 return this.instanceId_; | 145 return this.instanceId_; |
| 129 }; | 146 }; |
| 130 | 147 |
| 131 /** | 148 /** |
| 132 * @param {{method:string, data:Object.<string,*>}} message | 149 * @param {{method:string, data:Object.<string,*>}} message |
| 133 * @return {boolean} whether the message is handled or not. | 150 * @return {boolean} whether the message is handled or not. |
| 134 * @private | 151 * @private |
| 135 */ | 152 */ |
| 136 remoting.It2MeHelperChannel.prototype.onHangoutMessage_ = function(message) { | 153 remoting.It2MeHelperChannel.prototype.onHangoutMessage_ = function(message) { |
| 137 try { | 154 try { |
| 138 var MessageTypes = remoting.It2MeHelperChannel.HangoutMessageTypes; | 155 var MessageTypes = remoting.It2MeHelperChannel.HangoutMessageTypes; |
| 139 switch (message.method) { | 156 switch (message.method) { |
| 140 case MessageTypes.CONNECT: | 157 case MessageTypes.CONNECT: |
| 141 this.launchWebapp_(message); | 158 this.launchWebapp_(message); |
| 142 return true; | 159 return true; |
| 143 case MessageTypes.DISCONNECT: | 160 case MessageTypes.DISCONNECT: |
| 144 this.closeWebapp_(message); | 161 this.closeWebapp_(message); |
| 145 return true; | 162 return true; |
| 163 case MessageTypes.HELLO: | |
| 164 this.hangoutPort_.postMessage({ | |
| 165 method: MessageTypes.HELLO_RESPONSE, | |
| 166 supportedFeatures: base.values(remoting.It2MeHelperChannel.Features) | |
| 167 }); | |
| 168 return true; | |
| 146 } | 169 } |
| 170 throw new Error('Unknown message method=' + message.method); | |
| 147 } catch(e) { | 171 } catch(e) { |
| 148 var error = /** @type {Error} */ e; | 172 var error = /** @type {Error} */ e; |
| 149 console.error(error); | 173 console.error(error); |
| 150 this.hangoutPort_.postMessage({ | 174 this.hangoutPort_.postMessage({ |
| 151 method: message.method + 'Response', | 175 method: MessageTypes.ERROR, |
|
Jamie
2014/08/15 21:57:31
My comment about appending "Response" was just in
kelvinp
2014/08/15 23:25:01
I will make the change in error in later CL's
| |
| 152 error: error.message | 176 error: error.message |
| 153 }); | 177 }); |
| 154 } | 178 } |
| 155 return false; | 179 return false; |
| 156 }; | 180 }; |
| 157 | 181 |
| 158 /** | 182 /** |
| 159 * Disconnect the existing connection to the helpee. | 183 * Disconnect the existing connection to the helpee. |
| 160 * | 184 * |
| 161 * @param {{method:string, data:Object.<string,*>}} message | 185 * @param {{method:string, data:Object.<string,*>}} message |
| 162 * @private | 186 * @private |
| 163 */ | 187 */ |
| 164 remoting.It2MeHelperChannel.prototype.closeWebapp_ = | 188 remoting.It2MeHelperChannel.prototype.closeWebapp_ = |
| 165 function(message) { | 189 function(message) { |
| 166 // TODO(kelvinp): Closing the v2 app currently doesn't disconnect the IT2me | 190 // TODO(kelvinp): Closing the v2 app currently doesn't disconnect the IT2me |
| 167 // session (crbug.com/402137), so send an explicit notification to Hangouts. | 191 // session (crbug.com/402137), so send an explicit notification to Hangouts. |
| 192 var MessageTypes = remoting.It2MeHelperChannel.HangoutMessageTypes; | |
| 168 this.sessionState_ = remoting.ClientSession.State.CLOSED; | 193 this.sessionState_ = remoting.ClientSession.State.CLOSED; |
| 169 this.hangoutPort_.postMessage({ | 194 this.hangoutPort_.postMessage({ |
| 170 method: 'sessionStateChanged', | 195 method: MessageTypes.SESSION_STATE_CHANGED, |
| 171 state: this.sessionState_ | 196 state: this.sessionState_ |
| 172 }); | 197 }); |
| 173 this.appLauncher_.close(this.instanceId_); | 198 this.appLauncher_.close(this.instanceId_); |
| 174 }; | 199 }; |
| 175 | 200 |
| 176 /** | 201 /** |
| 177 * Launches the web app. | 202 * Launches the web app. |
| 178 * | 203 * |
| 179 * @param {{method:string, data:Object.<string,*>}} message | 204 * @param {{method:string, data:Object.<string,*>}} message |
| 180 * @private | 205 * @private |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 221 port.onMessage.addListener(this.onWebappMessageRef_); | 246 port.onMessage.addListener(this.onWebappMessageRef_); |
| 222 port.onDisconnect.addListener(this.onWebappDisconnectRef_); | 247 port.onDisconnect.addListener(this.onWebappDisconnectRef_); |
| 223 this.webappPort_ = port; | 248 this.webappPort_ = port; |
| 224 }; | 249 }; |
| 225 | 250 |
| 226 /** @param {chrome.runtime.Port} port The webapp port. */ | 251 /** @param {chrome.runtime.Port} port The webapp port. */ |
| 227 remoting.It2MeHelperChannel.prototype.onWebappDisconnect_ = function(port) { | 252 remoting.It2MeHelperChannel.prototype.onWebappDisconnect_ = function(port) { |
| 228 // If the webapp port got disconnected while the session is still connected, | 253 // If the webapp port got disconnected while the session is still connected, |
| 229 // treat it as an error. | 254 // treat it as an error. |
| 230 var States = remoting.ClientSession.State; | 255 var States = remoting.ClientSession.State; |
| 256 var HangoutMessageTypes = remoting.It2MeHelperChannel.HangoutMessageTypes; | |
| 231 if (this.sessionState_ === States.CONNECTING || | 257 if (this.sessionState_ === States.CONNECTING || |
| 232 this.sessionState_ === States.CONNECTED) { | 258 this.sessionState_ === States.CONNECTED) { |
| 233 this.sessionState_ = States.FAILED; | 259 this.sessionState_ = States.FAILED; |
| 234 this.hangoutPort_.postMessage({ | 260 this.hangoutPort_.postMessage({ |
| 235 method: 'sessionStateChanged', | 261 method: HangoutMessageTypes.SESSION_STATE_CHANGED, |
| 236 state: this.sessionState_ | 262 state: this.sessionState_ |
| 237 }); | 263 }); |
| 238 } | 264 } |
| 239 this.unhookPorts_(); | 265 this.unhookPorts_(); |
| 240 }; | 266 }; |
| 241 | 267 |
| 242 /** | 268 /** |
| 243 * @param {{method:string, data:Object.<string,*>}} message | 269 * @param {{method:string, data:Object.<string,*>}} message |
| 244 * @private | 270 * @private |
| 245 */ | 271 */ |
| 246 remoting.It2MeHelperChannel.prototype.onWebappMessage_ = function(message) { | 272 remoting.It2MeHelperChannel.prototype.onWebappMessage_ = function(message) { |
| 247 try { | 273 try { |
| 248 console.log('It2MeHelperChannel id=' + this.instanceId_ + | 274 console.log('It2MeHelperChannel id=' + this.instanceId_ + |
| 249 ' incoming message method=' + message.method); | 275 ' incoming message method=' + message.method); |
| 250 var MessageTypes = remoting.It2MeHelperChannel.WebappMessageTypes; | 276 var WebappMessageTypes = remoting.It2MeHelperChannel.WebappMessageTypes; |
| 277 var HangoutMessageTypes = remoting.It2MeHelperChannel.HangoutMessageTypes; | |
| 251 switch (message.method) { | 278 switch (message.method) { |
| 252 case MessageTypes.SESSION_STATE_CHANGED: | 279 case WebappMessageTypes.SESSION_STATE_CHANGED: |
| 253 var state = getNumberAttr(message, 'state'); | 280 var state = getNumberAttr(message, 'state'); |
| 254 this.sessionState_ = | 281 this.sessionState_ = /** @type {remoting.ClientSession.State} */ state; |
| 255 /** @type {remoting.ClientSession.State} */ state; | 282 this.hangoutPort_.postMessage({ |
| 256 this.hangoutPort_.postMessage(message); | 283 method: HangoutMessageTypes.SESSION_STATE_CHANGED, |
| 284 state: state | |
| 285 }); | |
|
Jamie
2014/08/15 21:57:31
As above, I like this change, but it doesn't reall
| |
| 257 return true; | 286 return true; |
| 258 } | 287 } |
| 259 throw new Error('Unknown message method=' + message.method); | 288 throw new Error('Unknown message method=' + message.method); |
| 260 } catch(e) { | 289 } catch(e) { |
| 261 var error = /** @type {Error} */ e; | 290 var error = /** @type {Error} */ e; |
| 262 console.error(error); | 291 console.error(error); |
| 263 this.webappPort_.postMessage({ | 292 this.webappPort_.postMessage({ |
| 264 method: message.method + 'Response', | 293 method: WebappMessageTypes.ERROR, |
| 265 error: error.message | 294 error: error.message |
| 266 }); | 295 }); |
| 267 } | 296 } |
| 268 return false; | 297 return false; |
| 269 }; | 298 }; |
| 270 | 299 |
| 271 remoting.It2MeHelperChannel.prototype.unhookPorts_ = function() { | 300 remoting.It2MeHelperChannel.prototype.unhookPorts_ = function() { |
| 272 if (this.webappPort_) { | 301 if (this.webappPort_) { |
| 273 this.webappPort_.onMessage.removeListener(this.onWebappMessageRef_); | 302 this.webappPort_.onMessage.removeListener(this.onWebappMessageRef_); |
| 274 this.webappPort_.onDisconnect.removeListener(this.onWebappDisconnectRef_); | 303 this.webappPort_.onDisconnect.removeListener(this.onWebappDisconnectRef_); |
| 275 this.webappPort_.disconnect(); | 304 this.webappPort_.disconnect(); |
| 276 this.webappPort_ = null; | 305 this.webappPort_ = null; |
| 277 } | 306 } |
| 278 | 307 |
| 279 if (this.hangoutPort_) { | 308 if (this.hangoutPort_) { |
| 280 this.hangoutPort_.onMessage.removeListener(this.onHangoutMessageRef_); | 309 this.hangoutPort_.onMessage.removeListener(this.onHangoutMessageRef_); |
| 281 this.hangoutPort_.onDisconnect.removeListener(this.onHangoutDisconnectRef_); | 310 this.hangoutPort_.onDisconnect.removeListener(this.onHangoutDisconnectRef_); |
| 282 this.hangoutPort_.disconnect(); | 311 this.hangoutPort_.disconnect(); |
| 283 this.hangoutPort_ = null; | 312 this.hangoutPort_ = null; |
| 284 } | 313 } |
| 285 | 314 |
| 286 if (this.onDisconnectCallback_) { | 315 if (this.onDisconnectCallback_) { |
| 287 this.onDisconnectCallback_(this); | 316 this.onDisconnectCallback_(this); |
| 288 this.onDisconnectCallback_ = null; | 317 this.onDisconnectCallback_ = null; |
| 289 } | 318 } |
| 290 }; | 319 }; |
| OLD | NEW |