| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 'client screen' for Chromoting. | 7 * Functions related to the 'client screen' for Chromoting. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 'use strict'; | 10 'use strict'; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 * @type {string} The normalized access code. | 29 * @type {string} The normalized access code. |
| 30 */ | 30 */ |
| 31 remoting.accessCode = ''; | 31 remoting.accessCode = ''; |
| 32 | 32 |
| 33 /** | 33 /** |
| 34 * @type {string} The host's JID, returned by the server. | 34 * @type {string} The host's JID, returned by the server. |
| 35 */ | 35 */ |
| 36 remoting.hostJid = ''; | 36 remoting.hostJid = ''; |
| 37 | 37 |
| 38 /** | 38 /** |
| 39 * @type {string} For Me2Me connections, the id of the current host, used when |
| 40 * (re-)connecting, as the JID may have changed. |
| 41 */ |
| 42 remoting.hostId = ''; |
| 43 |
| 44 /** |
| 39 * @type {string} The host's public key, returned by the server. | 45 * @type {string} The host's public key, returned by the server. |
| 40 */ | 46 */ |
| 41 remoting.hostPublicKey = ''; | 47 remoting.hostPublicKey = ''; |
| 42 | 48 |
| 43 /** | 49 /** |
| 44 * @type {XMLHttpRequest} The XHR object corresponding to the current | 50 * @type {XMLHttpRequest} The XHR object corresponding to the current |
| 45 * support-hosts request, if there is one outstanding. | 51 * support-hosts request, if there is one outstanding. |
| 46 */ | 52 */ |
| 47 remoting.supportHostsXhr_ = null; | 53 remoting.supportHostsXhr_ = null; |
| 48 | 54 |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 // only with older client plugins. Current version should go the | 247 // only with older client plugins. Current version should go the |
| 242 // FAILED state when connection fails. | 248 // FAILED state when connection fails. |
| 243 showConnectError_(remoting.Error.INVALID_ACCESS_CODE); | 249 showConnectError_(remoting.Error.INVALID_ACCESS_CODE); |
| 244 } | 250 } |
| 245 | 251 |
| 246 } else if (newState == remoting.ClientSession.State.CONNECTION_FAILED) { | 252 } else if (newState == remoting.ClientSession.State.CONNECTION_FAILED) { |
| 247 remoting.debug.log('Client plugin reported connection failed: ' + | 253 remoting.debug.log('Client plugin reported connection failed: ' + |
| 248 remoting.clientSession.error); | 254 remoting.clientSession.error); |
| 249 if (remoting.clientSession.error == | 255 if (remoting.clientSession.error == |
| 250 remoting.ClientSession.ConnectionError.HOST_IS_OFFLINE) { | 256 remoting.ClientSession.ConnectionError.HOST_IS_OFFLINE) { |
| 251 showConnectError_(remoting.Error.HOST_IS_OFFLINE); | 257 retryConnectOrReportOffline_(); |
| 252 } else if (remoting.clientSession.error == | 258 } else if (remoting.clientSession.error == |
| 253 remoting.ClientSession.ConnectionError.SESSION_REJECTED) { | 259 remoting.ClientSession.ConnectionError.SESSION_REJECTED) { |
| 254 showConnectError_(remoting.Error.INVALID_ACCESS_CODE); | 260 showConnectError_(remoting.Error.INVALID_ACCESS_CODE); |
| 255 } else if (remoting.clientSession.error == | 261 } else if (remoting.clientSession.error == |
| 256 remoting.ClientSession.ConnectionError.INCOMPATIBLE_PROTOCOL) { | 262 remoting.ClientSession.ConnectionError.INCOMPATIBLE_PROTOCOL) { |
| 257 showConnectError_(remoting.Error.INCOMPATIBLE_PROTOCOL); | 263 showConnectError_(remoting.Error.INCOMPATIBLE_PROTOCOL); |
| 258 } else if (remoting.clientSession.error == | 264 } else if (remoting.clientSession.error == |
| 259 remoting.ClientSession.ConnectionError.NETWORK_FAILURE) { | 265 remoting.ClientSession.ConnectionError.NETWORK_FAILURE) { |
| 260 showConnectError_(remoting.Error.GENERIC); | 266 showConnectError_(remoting.Error.GENERIC); |
| 261 } else { | 267 } else { |
| 262 showConnectError_(remoting.Error.GENERIC); | 268 showConnectError_(remoting.Error.GENERIC); |
| 263 } | 269 } |
| 264 | 270 |
| 265 } else { | 271 } else { |
| 266 remoting.debug.log('Unexpected client plugin state: ' + newState); | 272 remoting.debug.log('Unexpected client plugin state: ' + newState); |
| 267 // This should only happen if the web-app and client plugin get out of | 273 // This should only happen if the web-app and client plugin get out of |
| 268 // sync, and even then the version check should allow compatibility. | 274 // sync, and even then the version check should allow compatibility. |
| 269 showConnectError_(remoting.Error.MISSING_PLUGIN); | 275 showConnectError_(remoting.Error.MISSING_PLUGIN); |
| 270 } | 276 } |
| 271 } | 277 } |
| 272 | 278 |
| 273 /** | 279 /** |
| 280 * If we have a hostId to retry, try refreshing it and connecting again. If not, |
| 281 * then show the 'host offline' error message. |
| 282 * |
| 283 * @return {void} Nothing. |
| 284 */ |
| 285 function retryConnectOrReportOffline_() { |
| 286 if (remoting.hostId) { |
| 287 console.log('Connection failed. Retrying.'); |
| 288 /** @param {boolean} success True if the refresh was successful. */ |
| 289 var onDone = function(success) { |
| 290 if (success) { |
| 291 remoting.connectHost(remoting.hostId, false); |
| 292 } else { |
| 293 showConnectError_(remoting.Error.HOST_IS_OFFLINE); |
| 294 } |
| 295 }; |
| 296 remoting.hostList.refresh(onDone); |
| 297 } else { |
| 298 console.log('Connection failed. Not retrying.'); |
| 299 showConnectError_(remoting.Error.HOST_IS_OFFLINE); |
| 300 } |
| 301 } |
| 302 |
| 303 /** |
| 274 * Create the client session object and initiate the connection. | 304 * Create the client session object and initiate the connection. |
| 275 * | 305 * |
| 276 * @return {void} Nothing. | 306 * @return {void} Nothing. |
| 277 */ | 307 */ |
| 278 function startSession_() { | 308 function startSession_() { |
| 279 remoting.debug.log('Starting session...'); | 309 remoting.debug.log('Starting session...'); |
| 280 var accessCode = document.getElementById('access-code-entry'); | 310 var accessCode = document.getElementById('access-code-entry'); |
| 281 accessCode.value = ''; // The code has been validated and won't work again. | 311 accessCode.value = ''; // The code has been validated and won't work again. |
| 282 remoting.clientSession = | 312 remoting.clientSession = |
| 283 new remoting.ClientSession( | 313 new remoting.ClientSession( |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 } | 419 } |
| 390 var stats = remoting.clientSession.stats(); | 420 var stats = remoting.clientSession.stats(); |
| 391 remoting.debug.updateStatistics(stats); | 421 remoting.debug.updateStatistics(stats); |
| 392 remoting.clientSession.logStatistics(stats); | 422 remoting.clientSession.logStatistics(stats); |
| 393 // Update the stats once per second. | 423 // Update the stats once per second. |
| 394 window.setTimeout(updateStatistics_, 1000); | 424 window.setTimeout(updateStatistics_, 1000); |
| 395 } | 425 } |
| 396 | 426 |
| 397 | 427 |
| 398 /** | 428 /** |
| 399 * Start a connection to the specified host, using the stored details. | 429 * Start a connection to the specified host, using the cached details. |
| 400 * | 430 * |
| 401 * @param {string} hostJid The jabber Id of the host. | 431 * @param {string} hostId The unique id of the host. |
| 402 * @param {string} hostPublicKey The public key of the host. | 432 * @param {boolean} retryIfOffline If true and the host can't be contacted, |
| 403 * @param {string} hostName The name of the host. | 433 * refresh the host list and try again. This allows bookmarked hosts to |
| 434 * work even if they reregister with Talk and get a different Jid. |
| 404 * @return {void} Nothing. | 435 * @return {void} Nothing. |
| 405 */ | 436 */ |
| 406 remoting.connectHost = function(hostJid, hostPublicKey, hostName) { | 437 remoting.connectHost = function(hostId, retryIfOffline) { |
| 407 // TODO(jamiewalch): Instead of passing the jid in the URL, cache it in local | |
| 408 // storage so that host bookmarks can be implemented efficiently. | |
| 409 remoting.hostJid = hostJid; | |
| 410 remoting.hostPublicKey = hostPublicKey; | |
| 411 document.getElementById('connected-to').innerText = hostName; | |
| 412 document.title = document.title + ': ' + hostName; | |
| 413 | |
| 414 remoting.debug.log('Connecting to host...'); | 438 remoting.debug.log('Connecting to host...'); |
| 415 remoting.currentConnectionType = remoting.ConnectionType.Me2Me; | 439 remoting.currentConnectionType = remoting.ConnectionType.Me2Me; |
| 440 |
| 441 // Storing the hostId indicates that it should be retried on failure. |
| 442 remoting.hostId = retryIfOffline ? hostId : ''; |
| 416 remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); | 443 remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); |
| 417 | 444 |
| 445 var host = remoting.hostList.getHostForId(hostId); |
| 446 if (!host) { |
| 447 retryConnectOrReportOffline_(); |
| 448 return; |
| 449 } |
| 450 remoting.hostJid = host.jabberId; |
| 451 remoting.hostPublicKey = host.publicKey; |
| 452 document.getElementById('connected-to').innerText = host.hostName; |
| 453 document.title = document.title + ': ' + host.hostName; |
| 454 |
| 418 if (!remoting.wcsLoader) { | 455 if (!remoting.wcsLoader) { |
| 419 remoting.wcsLoader = new remoting.WcsLoader(); | 456 remoting.wcsLoader = new remoting.WcsLoader(); |
| 420 } | 457 } |
| 421 /** @param {function(string):void} setToken The callback function. */ | 458 /** @param {function(string):void} setToken The callback function. */ |
| 422 var callWithToken = function(setToken) { | 459 var callWithToken = function(setToken) { |
| 423 remoting.oauth2.callWithToken(setToken); | 460 remoting.oauth2.callWithToken(setToken); |
| 424 }; | 461 }; |
| 425 remoting.wcsLoader.startAsync(callWithToken, remoting.connectHostWithWcs); | 462 remoting.wcsLoader.startAsync(callWithToken, remoting.connectHostWithWcs); |
| 426 } | 463 } |
| 427 | 464 |
| 428 /** | 465 /** |
| 429 * Continue making the connection to a host, once WCS has initialized. | 466 * Continue making the connection to a host, once WCS has initialized. |
| 430 * | 467 * |
| 431 * @return {void} Nothing. | 468 * @return {void} Nothing. |
| 432 */ | 469 */ |
| 433 remoting.connectHostWithWcs = function() { | 470 remoting.connectHostWithWcs = function() { |
| 434 remoting.clientSession = | 471 remoting.clientSession = |
| 435 new remoting.ClientSession( | 472 new remoting.ClientSession( |
| 436 remoting.hostJid, remoting.hostPublicKey, | 473 remoting.hostJid, remoting.hostPublicKey, |
| 437 '', /** @type {string} */ (remoting.oauth2.getCachedEmail()), | 474 '', /** @type {string} */ (remoting.oauth2.getCachedEmail()), |
| 438 onClientStateChange_); | 475 onClientStateChange_); |
| 439 /** @param {string} token The auth token. */ | 476 /** @param {string} token The auth token. */ |
| 440 var createPluginAndConnect = function(token) { | 477 var createPluginAndConnect = function(token) { |
| 441 remoting.clientSession.createPluginAndConnect( | 478 remoting.clientSession.createPluginAndConnect( |
| 442 document.getElementById('session-mode'), | 479 document.getElementById('session-mode'), |
| 443 token); | 480 token); |
| 444 }; | 481 }; |
| 445 | 482 |
| 446 remoting.oauth2.callWithToken(createPluginAndConnect); | 483 remoting.oauth2.callWithToken(createPluginAndConnect); |
| 447 } | 484 } |
| 448 | 485 |
| 449 }()); | 486 }()); |
| OLD | NEW |