Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 * Functions related to the 'host screen' for Chromoting. | 7 * Functions related to the 'host screen' for Chromoting. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 /** @suppress {duplicate} */ | 10 /** @suppress {duplicate} */ |
| 11 var remoting = remoting || {}; | 11 var remoting = remoting || {}; |
| 12 | 12 |
| 13 (function(){ | 13 (function(){ |
| 14 | 14 |
| 15 'use strict'; | 15 'use strict'; |
| 16 | 16 |
| 17 /** @type {remoting.HostSession} */ | 17 /** @type {remoting.HostSession} */ |
| 18 var hostSession_ = null; | 18 var hostSession_ = null; |
| 19 | 19 |
| 20 /** | 20 /** |
| 21 * @type {boolean} Whether or not the last share was cancelled by the user. | 21 * @type {boolean} Whether or not the last share was cancelled by the user. |
| 22 * This controls what screen is shown when the host signals completion. | 22 * This controls what screen is shown when the host signals completion. |
| 23 */ | 23 */ |
| 24 var lastShareWasCancelled_ = false; | 24 var lastShareWasCancelled_ = false; |
| 25 | 25 |
| 26 /** | 26 /** |
| 27 * @type {remoting.LogToServer} Logging instance for IT2Me host connection | |
| 28 * status. | |
| 29 */ | |
| 30 var it2meLogger = null; | |
| 31 | |
| 32 /** | |
| 27 * Start a host session. This is the main entry point for the host screen, | 33 * Start a host session. This is the main entry point for the host screen, |
| 28 * called directly from the onclick action of a button on the home screen. | 34 * called directly from the onclick action of a button on the home screen. |
| 29 * It first verifies that the native host components are installed and asks | 35 * It first verifies that the native host components are installed and asks |
| 30 * to install them if necessary. | 36 * to install them if necessary. |
| 31 */ | 37 */ |
| 32 remoting.tryShare = function() { | 38 remoting.tryShare = function() { |
| 39 ensureIT2MeLogger_().then(tryShareWithLogger_); | |
| 40 }; | |
| 41 | |
| 42 function tryShareWithLogger_() { | |
| 43 it2meLogger.setSessionId(); | |
|
Jamie
2015/06/10 00:38:29
This forces a fresh session id each time the user
| |
| 44 it2meLogger.logClientSessionStateChange( | |
| 45 remoting.ClientSession.State.INITIALIZING, | |
| 46 remoting.Error.none()); | |
| 47 | |
| 33 /** @type {remoting.It2MeHostFacade} */ | 48 /** @type {remoting.It2MeHostFacade} */ |
| 34 var hostFacade = new remoting.It2MeHostFacade(); | 49 var hostFacade = new remoting.It2MeHostFacade(); |
| 35 | 50 |
| 36 /** @type {remoting.HostInstallDialog} */ | 51 /** @type {remoting.HostInstallDialog} */ |
| 37 var hostInstallDialog = null; | 52 var hostInstallDialog = null; |
| 38 | 53 |
| 39 var tryInitializeFacade = function() { | 54 var tryInitializeFacade = function() { |
| 40 hostFacade.initialize(onFacadeInitialized, onFacadeInitializationFailed); | 55 hostFacade.initialize(onFacadeInitialized, onFacadeInitializationFailed); |
| 41 }; | 56 }; |
| 42 | 57 |
| 43 var onFacadeInitialized = function () { | 58 var onFacadeInitialized = function () { |
| 44 // Host already installed. | 59 // Host already installed. |
| 45 remoting.startHostUsingFacade_(hostFacade); | 60 remoting.startHostUsingFacade_(hostFacade); |
| 46 }; | 61 }; |
| 47 | 62 |
| 48 var onFacadeInitializationFailed = function() { | 63 var onFacadeInitializationFailed = function() { |
| 49 // If we failed to initialize the dispatcher then prompt the user to install | 64 // If we failed to initialize the dispatcher then prompt the user to install |
| 50 // the host manually. | 65 // the host manually. |
| 51 var hasHostDialog = (hostInstallDialog !== null); /** jscompile hack */ | 66 var hasHostDialog = (hostInstallDialog !== null); /** jscompile hack */ |
| 52 if (!hasHostDialog) { | 67 if (!hasHostDialog) { |
| 53 hostInstallDialog = new remoting.HostInstallDialog(); | 68 hostInstallDialog = new remoting.HostInstallDialog(); |
| 54 hostInstallDialog.show(tryInitializeFacade, onInstallError); | 69 hostInstallDialog.show(tryInitializeFacade, showShareError_); |
| 55 } else { | 70 } else { |
| 56 hostInstallDialog.tryAgain(); | 71 hostInstallDialog.tryAgain(); |
| 57 } | 72 } |
| 58 }; | 73 }; |
| 59 | 74 |
| 60 /** @param {!remoting.Error} error */ | |
| 61 var onInstallError = function(error) { | |
| 62 if (error.hasTag(remoting.Error.Tag.CANCELLED)) { | |
| 63 remoting.setMode(remoting.AppMode.HOME); | |
| 64 } else { | |
| 65 showShareError_(error); | |
| 66 } | |
| 67 }; | |
| 68 | |
| 69 tryInitializeFacade(); | 75 tryInitializeFacade(); |
| 70 }; | 76 }; |
| 71 | 77 |
| 72 /** | 78 /** |
| 73 * @param {remoting.It2MeHostFacade} hostFacade An initialized It2MeHostFacade. | 79 * @param {remoting.It2MeHostFacade} hostFacade An initialized It2MeHostFacade. |
| 74 */ | 80 */ |
| 75 remoting.startHostUsingFacade_ = function(hostFacade) { | 81 remoting.startHostUsingFacade_ = function(hostFacade) { |
| 76 console.log('Attempting to share...'); | 82 console.log('Attempting to share...'); |
| 77 remoting.identity.getToken().then( | 83 setHostVersion_() |
| 78 remoting.tryShareWithToken_.bind(null, hostFacade), | 84 .then(remoting.identity.getToken.bind(remoting.identity)) |
| 79 remoting.Error.handler(remoting.showErrorMessage)); | 85 .then(remoting.tryShareWithToken_.bind(null, hostFacade), |
| 86 remoting.Error.handler(showShareError_)); | |
| 80 } | 87 } |
| 81 | 88 |
| 82 /** | 89 /** |
| 83 * @param {remoting.It2MeHostFacade} hostFacade An initialized | 90 * @param {remoting.It2MeHostFacade} hostFacade An initialized |
| 84 * It2MeHostFacade. | 91 * It2MeHostFacade. |
| 85 * @param {string} token The OAuth access token. | 92 * @param {string} token The OAuth access token. |
| 86 * @private | 93 * @private |
| 87 */ | 94 */ |
| 88 remoting.tryShareWithToken_ = function(hostFacade, token) { | 95 remoting.tryShareWithToken_ = function(hostFacade, token) { |
| 89 lastShareWasCancelled_ = false; | 96 lastShareWasCancelled_ = false; |
| 90 onNatTraversalPolicyChanged_(true); // Hide warning by default. | 97 onNatTraversalPolicyChanged_(true); // Hide warning by default. |
| 91 remoting.setMode(remoting.AppMode.HOST_WAITING_FOR_CODE); | 98 remoting.setMode(remoting.AppMode.HOST_WAITING_FOR_CODE); |
| 99 it2meLogger.logClientSessionStateChange( | |
| 100 remoting.ClientSession.State.CONNECTING, | |
| 101 remoting.Error.none()); | |
| 92 document.getElementById('cancel-share-button').disabled = false; | 102 document.getElementById('cancel-share-button').disabled = false; |
| 93 disableTimeoutCountdown_(); | 103 disableTimeoutCountdown_(); |
| 94 | 104 |
| 95 base.debug.assert(hostSession_ === null); | 105 base.debug.assert(hostSession_ === null); |
| 96 hostSession_ = new remoting.HostSession(); | 106 hostSession_ = new remoting.HostSession(); |
| 97 remoting.identity.getEmail().then( | 107 remoting.identity.getEmail().then( |
| 98 function(/** string */ email) { | 108 function(/** string */ email) { |
| 99 hostSession_.connect( | 109 hostSession_.connect( |
| 100 hostFacade, email, token, onHostStateChanged_, | 110 hostFacade, email, token, onHostStateChanged_, |
| 101 onNatTraversalPolicyChanged_, logDebugInfo_, it2meConnectFailed_); | 111 onNatTraversalPolicyChanged_, logDebugInfo_, it2meConnectFailed_); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 187 console.log('plugin: ' + msg); | 197 console.log('plugin: ' + msg); |
| 188 } | 198 } |
| 189 | 199 |
| 190 /** | 200 /** |
| 191 * Show a host-side error message. | 201 * Show a host-side error message. |
| 192 * | 202 * |
| 193 * @param {!remoting.Error} error The error to be localized and displayed. | 203 * @param {!remoting.Error} error The error to be localized and displayed. |
| 194 * @return {void} Nothing. | 204 * @return {void} Nothing. |
| 195 */ | 205 */ |
| 196 function showShareError_(error) { | 206 function showShareError_(error) { |
| 197 var errorDiv = document.getElementById('host-plugin-error'); | 207 if (error.hasTag(remoting.Error.Tag.CANCELLED)) { |
| 198 l10n.localizeElementFromTag(errorDiv, error.getTag()); | 208 remoting.setMode(remoting.AppMode.HOME); |
| 199 console.error('Sharing error: ' + error.toString()); | 209 it2meLogger.logClientSessionStateChange( |
| 200 remoting.setMode(remoting.AppMode.HOST_SHARE_FAILED); | 210 remoting.ClientSession.State.CONNECTION_CANCELED, |
| 211 remoting.Error.none()); | |
| 212 } else { | |
| 213 var errorDiv = document.getElementById('host-plugin-error'); | |
| 214 l10n.localizeElementFromTag(errorDiv, error.getTag()); | |
| 215 console.error('Sharing error: ' + error.toString()); | |
| 216 remoting.setMode(remoting.AppMode.HOST_SHARE_FAILED); | |
| 217 it2meLogger.logClientSessionStateChange( | |
| 218 remoting.ClientSession.State.FAILED, | |
| 219 error); | |
| 220 } | |
| 221 | |
| 201 cleanUp(); | 222 cleanUp(); |
| 202 } | 223 } |
| 203 | 224 |
| 204 /** | 225 /** |
| 205 * Show a sharing error with error code UNEXPECTED . | 226 * Show a sharing error with error code UNEXPECTED . |
| 206 * | 227 * |
| 207 * @return {void} Nothing. | 228 * @return {void} Nothing. |
| 208 */ | 229 */ |
| 209 function it2meConnectFailed_() { | 230 function it2meConnectFailed_() { |
| 210 // TODO (weitaosu): Instruct the user to install the native messaging host. | 231 // TODO (weitaosu): Instruct the user to install the native messaging host. |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 223 * Cancel an active or pending it2me share operation. | 244 * Cancel an active or pending it2me share operation. |
| 224 * | 245 * |
| 225 * @return {void} Nothing. | 246 * @return {void} Nothing. |
| 226 */ | 247 */ |
| 227 remoting.cancelShare = function() { | 248 remoting.cancelShare = function() { |
| 228 document.getElementById('cancel-share-button').disabled = true; | 249 document.getElementById('cancel-share-button').disabled = true; |
| 229 console.log('Canceling share...'); | 250 console.log('Canceling share...'); |
| 230 remoting.lastShareWasCancelled = true; | 251 remoting.lastShareWasCancelled = true; |
| 231 try { | 252 try { |
| 232 hostSession_.disconnect(); | 253 hostSession_.disconnect(); |
| 254 it2meLogger.logClientSessionStateChange( | |
| 255 remoting.ClientSession.State.CONNECTION_CANCELED, | |
| 256 remoting.Error.none()); | |
| 233 } catch (/** @type {*} */ error) { | 257 } catch (/** @type {*} */ error) { |
| 234 console.error('Error disconnecting: ' + error + | 258 console.error('Error disconnecting: ' + error + |
| 235 '. The host probably crashed.'); | 259 '. The host probably crashed.'); |
| 236 // TODO(jamiewalch): Clean this up. We should have a class representing | 260 // TODO(jamiewalch): Clean this up. We should have a class representing |
| 237 // the host plugin, like we do for the client, which should handle crash | 261 // the host plugin, like we do for the client, which should handle crash |
| 238 // reporting and it should use a more detailed error message than the | 262 // reporting and it should use a more detailed error message than the |
| 239 // default 'generic' one. See crbug.com/94624 | 263 // default 'generic' one. See crbug.com/94624 |
| 240 showShareError_(remoting.Error.unexpected()); | 264 showShareError_(remoting.Error.unexpected()); |
| 241 } | 265 } |
| 242 disableTimeoutCountdown_(); | 266 disableTimeoutCountdown_(); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 331 */ | 355 */ |
| 332 function onNatTraversalPolicyChanged_(enabled) { | 356 function onNatTraversalPolicyChanged_(enabled) { |
| 333 var natBox = document.getElementById('nat-box'); | 357 var natBox = document.getElementById('nat-box'); |
| 334 if (enabled) { | 358 if (enabled) { |
| 335 natBox.classList.add('traversal-enabled'); | 359 natBox.classList.add('traversal-enabled'); |
| 336 } else { | 360 } else { |
| 337 natBox.classList.remove('traversal-enabled'); | 361 natBox.classList.remove('traversal-enabled'); |
| 338 } | 362 } |
| 339 } | 363 } |
| 340 | 364 |
| 365 /** | |
| 366 * Create an IT2Me LogToServer instance if one does not already exist. | |
| 367 * | |
| 368 * @return {Promise} Promise that resolves when the host version (if available), | |
| 369 * has been set on the logger instance. | |
| 370 */ | |
| 371 function ensureIT2MeLogger_() { | |
| 372 if (it2meLogger) { | |
| 373 return Promise.resolve(); | |
| 374 } | |
| 375 | |
| 376 var xmppConnection = new remoting.XmppConnection(); | |
| 377 var tokenPromise = remoting.identity.getToken(); | |
| 378 var emailPromise = remoting.identity.getEmail(); | |
| 379 tokenPromise.then(function(/** string */ token) { | |
| 380 emailPromise.then(function(/** string */ email) { | |
| 381 xmppConnection.connect(remoting.settings.XMPP_SERVER, email, token); | |
| 382 }); | |
| 383 }); | |
| 384 | |
| 385 var bufferedSignalStrategy = | |
| 386 new remoting.BufferedSignalStrategy(xmppConnection); | |
| 387 it2meLogger = new remoting.LogToServer(bufferedSignalStrategy, true); | |
| 388 it2meLogger.setLogEntryMode(remoting.ServerLogEntry.VALUE_MODE_IT2ME); | |
| 389 | |
| 390 return setHostVersion_(); | |
| 391 }; | |
| 392 | |
| 393 /** | |
| 394 * @return {Promise} Promise that resolves when the host version (if available), | |
| 395 * has been set on the logger instance. | |
| 396 */ | |
| 397 function setHostVersion_() { | |
| 398 return remoting.hostController.getLocalHostVersion().then( | |
| 399 function(/** string */ version) { | |
| 400 it2meLogger.setHostVersion(version); | |
| 401 }, | |
| 402 function() { | |
|
kelvinp
2015/06/10 18:00:45
use .catch for readability
return remoting.hostCon
Jamie
2015/06/10 18:14:44
Done.
| |
| 403 return Promise.resolve(); | |
| 404 }); | |
| 405 }; | |
| 406 | |
| 341 })(); | 407 })(); |
| OLD | NEW |