| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 * Connect set-up state machine for Me2Me and IT2Me | 7 * Connect set-up state machine for Me2Me and IT2Me |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 'use strict'; | 10 'use strict'; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 * once the connector has invoked its onOk callback. | 24 * once the connector has invoked its onOk callback. |
| 25 * TODO(garykac): Have this owned by someone instead of being global. | 25 * TODO(garykac): Have this owned by someone instead of being global. |
| 26 */ | 26 */ |
| 27 remoting.desktopConnectedView = null; | 27 remoting.desktopConnectedView = null; |
| 28 | 28 |
| 29 /** | 29 /** |
| 30 * @param {HTMLElement} clientContainer Container element for the client view. | 30 * @param {HTMLElement} clientContainer Container element for the client view. |
| 31 * @param {function(remoting.ConnectionInfo):void} onConnected Callback on | 31 * @param {function(remoting.ConnectionInfo):void} onConnected Callback on |
| 32 * success. | 32 * success. |
| 33 * @param {function(!remoting.Error):void} onError Callback on error. | 33 * @param {function(!remoting.Error):void} onError Callback on error. |
| 34 * @param {function(string, string):boolean} appProtocolExtensionHandler The | |
| 35 * handler for the application's protocol extension messages. Returns true | |
| 36 * if a message is recognized; false otherwise. | |
| 37 * @param {function(!remoting.Error):void} onConnectionFailed Callback for when | 34 * @param {function(!remoting.Error):void} onConnectionFailed Callback for when |
| 38 * the connection fails. | 35 * the connection fails. |
| 39 * @param {Array<string>} requiredCapabilities Connector capabilities | 36 * @param {Array<string>} requiredCapabilities Connector capabilities |
| 40 * required by this application. | 37 * required by this application. |
| 41 * @param {string} defaultRemapKeys The default set of key mappings for the | 38 * @param {string} defaultRemapKeys The default set of key mappings for the |
| 42 * client session to use. | 39 * client session to use. |
| 43 * @constructor | 40 * @constructor |
| 44 * @implements {remoting.SessionConnector} | 41 * @implements {remoting.SessionConnector} |
| 45 */ | 42 */ |
| 46 remoting.SessionConnectorImpl = function(clientContainer, onConnected, onError, | 43 remoting.SessionConnectorImpl = function(clientContainer, onConnected, onError, |
| 47 appProtocolExtensionHandler, | |
| 48 onConnectionFailed, | 44 onConnectionFailed, |
| 49 requiredCapabilities, | 45 requiredCapabilities, |
| 50 defaultRemapKeys) { | 46 defaultRemapKeys) { |
| 51 /** @private {HTMLElement} */ | 47 /** @private {HTMLElement} */ |
| 52 this.clientContainer_ = clientContainer; | 48 this.clientContainer_ = clientContainer; |
| 53 | 49 |
| 54 /** @private {function(remoting.ConnectionInfo):void} */ | 50 /** @private {function(remoting.ConnectionInfo):void} */ |
| 55 this.onConnected_ = onConnected; | 51 this.onConnected_ = onConnected; |
| 56 | 52 |
| 57 /** @private {function(!remoting.Error):void} */ | 53 /** @private {function(!remoting.Error):void} */ |
| 58 this.onError_ = onError; | 54 this.onError_ = onError; |
| 59 | 55 |
| 60 /** @private {function(string, string):boolean} */ | |
| 61 this.appProtocolExtensionHandler_ = appProtocolExtensionHandler; | |
| 62 | |
| 63 /** @private {function(!remoting.Error):void} */ | 56 /** @private {function(!remoting.Error):void} */ |
| 64 this.onConnectionFailed_ = onConnectionFailed; | 57 this.onConnectionFailed_ = onConnectionFailed; |
| 65 | 58 |
| 66 /** @private {Array<string>} */ | 59 /** @private {Array<string>} */ |
| 67 this.requiredCapabilities_ = requiredCapabilities; | 60 this.requiredCapabilities_ = requiredCapabilities; |
| 68 | 61 |
| 69 /** @private {string} */ | 62 /** @private {string} */ |
| 70 this.defaultRemapKeys_ = defaultRemapKeys; | 63 this.defaultRemapKeys_ = defaultRemapKeys; |
| 71 | 64 |
| 72 /** @private {remoting.DesktopConnectedView.Mode} */ | 65 /** @private {remoting.DesktopConnectedView.Mode} */ |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 this.connectedView_ = null; | 104 this.connectedView_ = null; |
| 112 | 105 |
| 113 /** @private {XMLHttpRequest} */ | 106 /** @private {XMLHttpRequest} */ |
| 114 this.pendingXhr_ = null; | 107 this.pendingXhr_ = null; |
| 115 | 108 |
| 116 /** @private {remoting.CredentialsProvider} */ | 109 /** @private {remoting.CredentialsProvider} */ |
| 117 this.credentialsProvider_ = null; | 110 this.credentialsProvider_ = null; |
| 118 | 111 |
| 119 /** @private {Object<string,remoting.ProtocolExtension>} */ | 112 /** @private {Object<string,remoting.ProtocolExtension>} */ |
| 120 this.protocolExtensions_ = {}; | 113 this.protocolExtensions_ = {}; |
| 114 |
| 115 /** |
| 116 * True once a session has been created and we've started the extensions. |
| 117 * This is used to immediately start any extensions that are registered |
| 118 * after the CONNECTED state change. |
| 119 * @private {boolean} |
| 120 */ |
| 121 this.protocolExtensionsStarted_ = false; |
| 121 }; | 122 }; |
| 122 | 123 |
| 123 /** | 124 /** |
| 124 * Initiate a Me2Me connection. | 125 * Initiate a Me2Me connection. |
| 125 * | 126 * |
| 126 * This doesn't report host-offline errors because the connection will | 127 * This doesn't report host-offline errors because the connection will |
| 127 * be retried and retryConnectMe2Me is responsible for reporting these errors. | 128 * be retried and retryConnectMe2Me is responsible for reporting these errors. |
| 128 * | 129 * |
| 129 * @param {remoting.Host} host The Me2Me host to which to connect. | 130 * @param {remoting.Host} host The Me2Me host to which to connect. |
| 130 * @param {function(boolean, function(string):void):void} fetchPin Function to | 131 * @param {function(boolean, function(string):void):void} fetchPin Function to |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 | 399 |
| 399 base.dispose(this.plugin_); | 400 base.dispose(this.plugin_); |
| 400 this.plugin_ = null; | 401 this.plugin_ = null; |
| 401 }; | 402 }; |
| 402 | 403 |
| 403 /** | 404 /** |
| 404 * @param {remoting.ProtocolExtension} extension | 405 * @param {remoting.ProtocolExtension} extension |
| 405 */ | 406 */ |
| 406 remoting.SessionConnectorImpl.prototype.registerProtocolExtension = | 407 remoting.SessionConnectorImpl.prototype.registerProtocolExtension = |
| 407 function(extension) { | 408 function(extension) { |
| 408 var type = extension.getType(); | 409 var types = extension.getExtensionTypes(); |
| 409 if (type in this.protocolExtensions_) { | 410 |
| 410 console.error( | 411 // Make sure we don't have an extension of that type already registered. |
| 411 'Attempt to register multiple extensions with the same type: ', type); | 412 for (var i=0, len=types.length; i < len; i++) { |
| 412 return; | 413 if (types[i] in this.protocolExtensions_) { |
| 414 console.error( |
| 415 'Attempt to register multiple extensions of the same type: ', type); |
| 416 return; |
| 417 } |
| 413 } | 418 } |
| 414 this.protocolExtensions_[type] = extension; | 419 |
| 420 for (var i=0, len=types.length; i < len; i++) { |
| 421 var type = types[i]; |
| 422 this.protocolExtensions_[type] = extension; |
| 423 if (this.protocolExtensionsStarted_) { |
| 424 this.startProtocolExtension_(type); |
| 425 } |
| 426 } |
| 415 }; | 427 }; |
| 416 | 428 |
| 417 /** @private */ | 429 /** @private */ |
| 418 remoting.SessionConnectorImpl.prototype.initProtocolExtensions_ = function() { | 430 remoting.SessionConnectorImpl.prototype.initProtocolExtensions_ = function() { |
| 431 base.debug.assert(!this.protocolExtensionsStarted_); |
| 419 for (var type in this.protocolExtensions_) { | 432 for (var type in this.protocolExtensions_) { |
| 420 /** @type {remoting.ProtocolExtension} */ | 433 this.startProtocolExtension_(type); |
| 421 var extension = this.protocolExtensions_[type]; | |
| 422 extension.start(this.plugin_.sendClientMessage.bind(this.plugin_)); | |
| 423 } | 434 } |
| 435 this.protocolExtensionsStarted_ = true; |
| 424 }; | 436 }; |
| 425 | 437 |
| 426 /** | 438 /** |
| 439 * @param {string} type |
| 440 * @private |
| 441 */ |
| 442 remoting.SessionConnectorImpl.prototype.startProtocolExtension_ = |
| 443 function(type) { |
| 444 var extension = this.protocolExtensions_[type]; |
| 445 extension.startExtension(this.plugin_.sendClientMessage.bind(this.plugin_)); |
| 446 }; |
| 447 |
| 448 /** |
| 427 * Called when an extension message needs to be handled. | 449 * Called when an extension message needs to be handled. |
| 428 * | 450 * |
| 429 * @param {string} type The type of the extension message. | 451 * @param {string} type The type of the extension message. |
| 430 * @param {string} data The payload of the extension message. | 452 * @param {string} data The payload of the extension message. |
| 431 * @return {boolean} Return true if the extension message was recognized. | 453 * @return {boolean} Return true if the extension message was recognized. |
| 432 * @private | 454 * @private |
| 433 */ | 455 */ |
| 434 remoting.SessionConnectorImpl.prototype.onProtocolExtensionMessage_ = | 456 remoting.SessionConnectorImpl.prototype.onProtocolExtensionMessage_ = |
| 435 function(type, data) { | 457 function(type, data) { |
| 436 if (type == 'test-echo-reply') { | 458 if (type == 'test-echo-reply') { |
| 437 console.log('Got echo reply: ' + data); | 459 console.log('Got echo reply: ' + data); |
| 438 return true; | 460 return true; |
| 439 } | 461 } |
| 440 for (var type in this.protocolExtensions_) { | 462 |
| 463 var message = base.jsonParseSafe(data); |
| 464 if (typeof message != 'object') { |
| 465 console.error('Error parsing extension json data: ' + data); |
| 466 return false; |
| 467 } |
| 468 |
| 469 if (type in this.protocolExtensions_) { |
| 441 /** @type {remoting.ProtocolExtension} */ | 470 /** @type {remoting.ProtocolExtension} */ |
| 442 var extension = this.protocolExtensions_[type]; | 471 var extension = this.protocolExtensions_[type]; |
| 443 if (type == extension.getType()) { | 472 var handled = false; |
| 444 try { | 473 try { |
| 445 extension.onMessage(data); | 474 handled = extension.onExtensionMessage(type, message); |
| 446 } catch (/** @type {*} */ err) { | 475 } catch (/** @type {*} */ err) { |
| 447 console.error('Failed to process protocol extension ', type, | 476 console.error('Failed to process protocol extension ' + type + |
| 448 ' message: ', err); | 477 ' message: ' + err); |
| 449 } | 478 } |
| 479 if (handled) { |
| 450 return true; | 480 return true; |
| 451 } | 481 } |
| 452 } | 482 } |
| 453 return this.appProtocolExtensionHandler_(type, data); | 483 |
| 484 if (remoting.desktopConnectedView) { |
| 485 return remoting.desktopConnectedView.handleExtensionMessage(type, message); |
| 486 } |
| 487 |
| 488 return false; |
| 454 }; | 489 }; |
| 455 | 490 |
| 456 /** | 491 /** |
| 457 * Handle a change in the state of the client session prior to successful | 492 * Handle a change in the state of the client session prior to successful |
| 458 * connection (after connection, this class no longer handles state change | 493 * connection (after connection, this class no longer handles state change |
| 459 * events). Errors that occur while connecting either trigger a reconnect | 494 * events). Errors that occur while connecting either trigger a reconnect |
| 460 * or notify the onError handler. | 495 * or notify the onError handler. |
| 461 * | 496 * |
| 462 * @param {remoting.ClientSession.StateEvent=} event | 497 * @param {remoting.ClientSession.StateEvent=} event |
| 463 * @return {void} Nothing. | 498 * @return {void} Nothing. |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 535 * @constructor | 570 * @constructor |
| 536 * @implements {remoting.SessionConnectorFactory} | 571 * @implements {remoting.SessionConnectorFactory} |
| 537 */ | 572 */ |
| 538 remoting.DefaultSessionConnectorFactory = function() {}; | 573 remoting.DefaultSessionConnectorFactory = function() {}; |
| 539 | 574 |
| 540 /** | 575 /** |
| 541 * @param {HTMLElement} clientContainer Container element for the client view. | 576 * @param {HTMLElement} clientContainer Container element for the client view. |
| 542 * @param {function(remoting.ConnectionInfo):void} onConnected Callback on | 577 * @param {function(remoting.ConnectionInfo):void} onConnected Callback on |
| 543 * success. | 578 * success. |
| 544 * @param {function(!remoting.Error):void} onError Callback on error. | 579 * @param {function(!remoting.Error):void} onError Callback on error. |
| 545 * @param {function(string, string):boolean} appProtocolExtensionHandler The | |
| 546 * handler for the application's protocol extension messages. Returns true | |
| 547 * if a message is recognized; false otherwise. | |
| 548 * @param {function(!remoting.Error):void} onConnectionFailed Callback for when | 580 * @param {function(!remoting.Error):void} onConnectionFailed Callback for when |
| 549 * the connection fails. | 581 * the connection fails. |
| 550 * @param {Array<string>} requiredCapabilities Connector capabilities | 582 * @param {Array<string>} requiredCapabilities Connector capabilities |
| 551 * required by this application. | 583 * required by this application. |
| 552 * @param {string} defaultRemapKeys The default set of key mappings to use | 584 * @param {string} defaultRemapKeys The default set of key mappings to use |
| 553 * in the client session. | 585 * in the client session. |
| 554 * @return {remoting.SessionConnector} | 586 * @return {remoting.SessionConnector} |
| 555 */ | 587 */ |
| 556 remoting.DefaultSessionConnectorFactory.prototype.createConnector = | 588 remoting.DefaultSessionConnectorFactory.prototype.createConnector = |
| 557 function(clientContainer, onConnected, onError, | 589 function(clientContainer, onConnected, onError, |
| 558 appProtocolExtensionHandler, | |
| 559 onConnectionFailed, requiredCapabilities, defaultRemapKeys) { | 590 onConnectionFailed, requiredCapabilities, defaultRemapKeys) { |
| 560 return new remoting.SessionConnectorImpl(clientContainer, onConnected, | 591 return new remoting.SessionConnectorImpl(clientContainer, onConnected, |
| 561 onError, | 592 onError, |
| 562 appProtocolExtensionHandler, | |
| 563 onConnectionFailed, | 593 onConnectionFailed, |
| 564 requiredCapabilities, | 594 requiredCapabilities, |
| 565 defaultRemapKeys); | 595 defaultRemapKeys); |
| 566 }; | 596 }; |
| OLD | NEW |