| 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 var remoting = remoting || {}; | 5 var remoting = remoting || {}; |
| 6 | 6 |
| 7 (function() { | 7 (function() { |
| 8 "use strict"; | 8 'use strict'; |
| 9 | 9 |
| 10 window.addEventListener('blur', pluginLostFocus_, false); | 10 window.addEventListener('blur', pluginLostFocus_, false); |
| 11 | 11 |
| 12 function pluginLostFocus_() { | 12 function pluginLostFocus_() { |
| 13 // If the plug loses input focus, release all keys as a precaution against | 13 // If the plug loses input focus, release all keys as a precaution against |
| 14 // leaving them 'stuck down' on the host. | 14 // leaving them 'stuck down' on the host. |
| 15 if (remoting.session && remoting.session.plugin) { | 15 if (remoting.session && remoting.session.plugin) { |
| 16 remoting.session.plugin.releaseAllKeys(); | 16 remoting.session.plugin.releaseAllKeys(); |
| 17 } | 17 } |
| 18 } | 18 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 44 var kDigitsPerGroup = 4; | 44 var kDigitsPerGroup = 4; |
| 45 | 45 |
| 46 function retrieveEmail_(access_token) { | 46 function retrieveEmail_(access_token) { |
| 47 var headers = { | 47 var headers = { |
| 48 'Authorization': 'OAuth ' + remoting.oauth2.getAccessToken() | 48 'Authorization': 'OAuth ' + remoting.oauth2.getAccessToken() |
| 49 }; | 49 }; |
| 50 | 50 |
| 51 var onResponse = function(xhr) { | 51 var onResponse = function(xhr) { |
| 52 if (xhr.status != 200) { | 52 if (xhr.status != 200) { |
| 53 // TODO(ajwong): Have a better way of showing an error. | 53 // TODO(ajwong): Have a better way of showing an error. |
| 54 window.alert("Unable to get e-mail"); | 54 window.alert('Unable to get e-mail'); |
| 55 return; | 55 return; |
| 56 } | 56 } |
| 57 | 57 |
| 58 // TODO(ajwong): See if we can't find a JSON endpoint. | 58 // TODO(ajwong): See if we can't find a JSON endpoint. |
| 59 setEmail(xhr.responseText.split('&')[0].split('=')[1]); | 59 setEmail(xhr.responseText.split('&')[0].split('=')[1]); |
| 60 }; | 60 }; |
| 61 | 61 |
| 62 // TODO(ajwong): Update to new v2 API. | 62 // TODO(ajwong): Update to new v2 API. |
| 63 remoting.xhr.get('https://www.googleapis.com/userinfo/email', | 63 remoting.xhr.get('https://www.googleapis.com/userinfo/email', |
| 64 onResponse, '', headers); | 64 onResponse, '', headers); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 } | 102 } |
| 103 updateControls_(disableControls); | 103 updateControls_(disableControls); |
| 104 } | 104 } |
| 105 | 105 |
| 106 function setEmail(value) { | 106 function setEmail(value) { |
| 107 window.localStorage.setItem(KEY_EMAIL_, value); | 107 window.localStorage.setItem(KEY_EMAIL_, value); |
| 108 updateAuthStatus_(); | 108 updateAuthStatus_(); |
| 109 } | 109 } |
| 110 | 110 |
| 111 /** | 111 /** |
| 112 * @return {string} | 112 * @return {string} The email address associated with the auth credentials. |
| 113 */ | 113 */ |
| 114 function getEmail() { | 114 function getEmail() { |
| 115 return window.localStorage.getItem(KEY_EMAIL_); | 115 return window.localStorage.getItem(KEY_EMAIL_); |
| 116 } | 116 } |
| 117 | 117 |
| 118 function exchangedCodeForToken_() { | 118 function exchangedCodeForToken_() { |
| 119 if (!remoting.oauth2.isAuthenticated()) { | 119 if (!remoting.oauth2.isAuthenticated()) { |
| 120 alert('Your OAuth2 token was invalid. Please try again.'); | 120 alert('Your OAuth2 token was invalid. Please try again.'); |
| 121 } | 121 } |
| 122 remoting.oauth2.callWithToken(function(token) { | 122 remoting.oauth2.callWithToken(function(token) { |
| 123 retrieveEmail_(token); | 123 retrieveEmail_(token); |
| 124 updateAuthStatus_(); | 124 updateAuthStatus_(); |
| 125 }); | 125 }); |
| 126 } | 126 } |
| 127 | 127 |
| 128 remoting.clearOAuth2 = function() { | 128 remoting.clearOAuth2 = function() { |
| 129 remoting.oauth2.clear(); | 129 remoting.oauth2.clear(); |
| 130 updateAuthStatus_(); | 130 updateAuthStatus_(); |
| 131 } | 131 } |
| 132 | 132 |
| 133 // Show the div with id |mode| and hide those with other ids in |modes|. | 133 // Show the div with id |mode| and hide those with other ids in |modes|. |
| 134 function setMode_(mode, modes) { | 134 function setMode_(mode, modes) { |
| 135 for (var i = 0; i < modes.length; ++i) { | 135 for (var i = 0; i < modes.length; ++i) { |
| 136 modes[i].hidden = (mode != modes[i].id); | 136 modes[i].hidden = (mode != modes[i].id); |
| 137 } | 137 } |
| 138 } | 138 } |
| 139 | 139 |
| 140 remoting.toggleDebugLog = function() { | 140 remoting.toggleDebugLog = function() { |
| 141 var debugLog = document.getElementById('debug-log'); | 141 var debugLog = document.getElementById('debug-log'); |
| 142 var toggleButton = document.getElementById('debug-log-toggle'); | 142 if (debugLog.hidden) { |
| 143 | 143 debugLog.hidden = false; |
| 144 if (!debugLog.style.display || debugLog.style.display == 'none') { | |
| 145 debugLog.style.display = 'block'; | |
| 146 toggleButton.value = 'Hide Debug Log'; | |
| 147 } else { | 144 } else { |
| 148 debugLog.style.display = 'none'; | 145 debugLog.hidden = true; |
| 149 toggleButton.value = 'Show Debug Log'; | |
| 150 } | 146 } |
| 151 } | 147 } |
| 152 | 148 |
| 153 remoting.init = function() { | 149 remoting.init = function() { |
| 154 // Create global objects. | 150 // Create global objects. |
| 155 remoting.oauth2 = new remoting.OAuth2(); | 151 remoting.oauth2 = new remoting.OAuth2(); |
| 156 remoting.debug = new remoting.DebugLog(document.getElementById('debug-log')); | 152 remoting.debug = |
| 153 new remoting.DebugLog(document.getElementById('debug-messages')); |
| 157 | 154 |
| 158 updateAuthStatus_(); | 155 updateAuthStatus_(); |
| 159 refreshEmail_(); | 156 refreshEmail_(); |
| 160 remoting.setHostMode('unshared'); | 157 remoting.setHostMode('unshared'); |
| 161 remoting.setClientMode('unconnected'); | 158 remoting.setClientMode('unconnected'); |
| 162 setGlobalMode(getAppStartupMode()); | 159 setGlobalMode(getAppStartupMode()); |
| 163 document.getElementById('loading-mode').hidden = true; | 160 document.getElementById('loading-mode').hidden = true; |
| 164 document.getElementById('choice-mode').hidden = false; | 161 document.getElementById('choice-mode').hidden = false; |
| 165 } | 162 } |
| 166 | 163 |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 * This is that callback that the host plugin invokes to indicate that there | 288 * This is that callback that the host plugin invokes to indicate that there |
| 292 * is additional debug log info to display. | 289 * is additional debug log info to display. |
| 293 */ | 290 */ |
| 294 function debugInfoCallback_(msg) { | 291 function debugInfoCallback_(msg) { |
| 295 remoting.debug.log('plugin: ' + msg); | 292 remoting.debug.log('plugin: ' + msg); |
| 296 } | 293 } |
| 297 | 294 |
| 298 function showShareError_(errorCode) { | 295 function showShareError_(errorCode) { |
| 299 var errorDiv = document.getElementById(errorCode); | 296 var errorDiv = document.getElementById(errorCode); |
| 300 errorDiv.style.display = 'block'; | 297 errorDiv.style.display = 'block'; |
| 301 remoting.debug.log("Sharing error: " + errorCode); | 298 remoting.debug.log('Sharing error: ' + errorCode); |
| 302 remoting.setHostMode('share-failed'); | 299 remoting.setHostMode('share-failed'); |
| 303 } | 300 } |
| 304 | 301 |
| 305 remoting.cancelShare = function() { | 302 remoting.cancelShare = function() { |
| 306 remoting.debug.log('Canceling share...'); | 303 remoting.debug.log('Canceling share...'); |
| 307 var plugin = document.getElementById(remoting.HOST_PLUGIN_ID); | 304 var plugin = document.getElementById(remoting.HOST_PLUGIN_ID); |
| 308 plugin.disconnect(); | 305 plugin.disconnect(); |
| 309 } | 306 } |
| 310 | 307 |
| 311 /** | 308 /** |
| 312 * Show a client message that stays on the screeen until the state changes. | 309 * Show a client message that stays on the screeen until the state changes. |
| 313 * | 310 * |
| 314 * @param {string} message The message to display. | 311 * @param {string} message The message to display. |
| 312 * @param {string} opt_host The host to display after the message. |
| 315 */ | 313 */ |
| 316 function setClientStateMessage(message) { | 314 function setClientStateMessage(message, opt_host) { |
| 317 var msg = document.getElementById('session-status-message'); | 315 document.getElementById('session-status-message').innerText = message; |
| 318 msg.innerText = message; | 316 opt_host = opt_host || ''; |
| 317 document.getElementById('connected-to').innerText = opt_host; |
| 319 } | 318 } |
| 320 | 319 |
| 321 function updateStatusBarStats() { | 320 function updateStatistics() { |
| 322 if (remoting.session.state != remoting.ClientSession.State.CONNECTED) | 321 if (remoting.session.state != remoting.ClientSession.State.CONNECTED) |
| 323 return; | 322 return; |
| 324 var stats = remoting.session.stats(); | 323 var stats = remoting.session.stats(); |
| 325 | 324 |
| 326 var units = ''; | 325 var units = ''; |
| 327 var videoBandwidth = stats['video_bandwidth']; | 326 var videoBandwidth = stats['video_bandwidth']; |
| 328 if (videoBandwidth < 1024) { | 327 if (videoBandwidth < 1024) { |
| 329 units = 'Bps'; | 328 units = 'Bps'; |
| 330 } else if (videoBandwidth < 1048576) { | 329 } else if (videoBandwidth < 1048576) { |
| 331 units = 'KiBps'; | 330 units = 'KiBps'; |
| 332 videoBandwidth = videoBandwidth / 1024; | 331 videoBandwidth = videoBandwidth / 1024; |
| 333 } else if (videoBandwidth < 1073741824) { | 332 } else if (videoBandwidth < 1073741824) { |
| 334 units = 'MiBps'; | 333 units = 'MiBps'; |
| 335 videoBandwidth = videoBandwidth / 1048576; | 334 videoBandwidth = videoBandwidth / 1048576; |
| 336 } else { | 335 } else { |
| 337 units = 'GiBps'; | 336 units = 'GiBps'; |
| 338 videoBandwidth = videoBandwidth / 1073741824; | 337 videoBandwidth = videoBandwidth / 1073741824; |
| 339 } | 338 } |
| 340 | 339 |
| 341 setClientStateMessage( | 340 var statistics = document.getElementById('statistics'); |
| 341 statistics.innerText = |
| 342 'Bandwidth: ' + videoBandwidth.toFixed(2) + units + | 342 'Bandwidth: ' + videoBandwidth.toFixed(2) + units + |
| 343 ', Capture: ' + stats['capture_latency'].toFixed(2) + 'ms' + | 343 ', Capture: ' + stats['capture_latency'].toFixed(2) + 'ms' + |
| 344 ', Encode: ' + stats['encode_latency'].toFixed(2) + 'ms' + | 344 ', Encode: ' + stats['encode_latency'].toFixed(2) + 'ms' + |
| 345 ', Decode: ' + stats['decode_latency'].toFixed(2) + 'ms' + | 345 ', Decode: ' + stats['decode_latency'].toFixed(2) + 'ms' + |
| 346 ', Render: ' + stats['render_latency'].toFixed(2) + 'ms' + | 346 ', Render: ' + stats['render_latency'].toFixed(2) + 'ms' + |
| 347 ', Latency: ' + stats['roundtrip_latency'].toFixed(2) + 'ms'); | 347 ', Latency: ' + stats['roundtrip_latency'].toFixed(2) + 'ms'; |
| 348 | 348 |
| 349 // Update the stats once per second. | 349 // Update the stats once per second. |
| 350 window.setTimeout(updateStatusBarStats, 1000); | 350 window.setTimeout(updateStatistics, 1000); |
| 351 } | 351 } |
| 352 | 352 |
| 353 function onClientStateChange_(state) { | 353 function onClientStateChange_(state) { |
| 354 if (state == remoting.ClientSession.State.UNKNOWN) { | 354 if (state == remoting.ClientSession.State.UNKNOWN) { |
| 355 setClientStateMessage('Unknown'); | 355 setClientStateMessage('Unknown'); |
| 356 } else if (state == remoting.ClientSession.State.CREATED) { | 356 } else if (state == remoting.ClientSession.State.CREATED) { |
| 357 setClientStateMessage('Created'); | 357 setClientStateMessage('Created'); |
| 358 } else if (state == remoting.ClientSession.State.BAD_PLUGIN_VERSION) { | 358 } else if (state == remoting.ClientSession.State.BAD_PLUGIN_VERSION) { |
| 359 setClientStateMessage('Incompatible Plugin Version'); | 359 setClientStateMessage('Incompatible Plugin Version'); |
| 360 } else if (state == remoting.ClientSession.State.UNKNOWN_PLUGIN_ERROR) { | 360 } else if (state == remoting.ClientSession.State.UNKNOWN_PLUGIN_ERROR) { |
| 361 setClientStateMessage('Unknown error with plugin.'); | 361 setClientStateMessage('Unknown error with plugin.'); |
| 362 } else if (state == remoting.ClientSession.State.CONNECTING) { | 362 } else if (state == remoting.ClientSession.State.CONNECTING) { |
| 363 setClientStateMessage('Connecting as ' + remoting.username); | 363 setClientStateMessage('Connecting as ' + remoting.username); |
| 364 } else if (state == remoting.ClientSession.State.INITIALIZING) { | 364 } else if (state == remoting.ClientSession.State.INITIALIZING) { |
| 365 setClientStateMessage('Initializing connection'); | 365 setClientStateMessage('Initializing connection'); |
| 366 } else if (state == remoting.ClientSession.State.CONNECTED) { | 366 } else if (state == remoting.ClientSession.State.CONNECTED) { |
| 367 updateStatusBarStats(); | 367 var split = remoting.hostJid.split('/'); |
| 368 var host = null; |
| 369 if (split.length == 2) { |
| 370 host = split[0]; |
| 371 } |
| 372 setClientStateMessage('Connected to', host); |
| 373 updateStatistics(); |
| 368 } else if (state == remoting.ClientSession.State.CLOSED) { | 374 } else if (state == remoting.ClientSession.State.CLOSED) { |
| 369 setClientStateMessage('Closed'); | 375 setClientStateMessage('Closed'); |
| 370 } else if (state == remoting.ClientSession.State.CONNECTION_FAILED) { | 376 } else if (state == remoting.ClientSession.State.CONNECTION_FAILED) { |
| 371 setClientStateMessage('Failed'); | 377 setClientStateMessage('Failed'); |
| 372 } else { | 378 } else { |
| 373 setClientStateMessage('Bad State!!'); | 379 setClientStateMessage('Bad State: ' + state); |
| 374 } | 380 } |
| 375 } | 381 } |
| 376 | 382 |
| 377 function startSession_() { | 383 function startSession_() { |
| 378 remoting.debug.log('Starting session...'); | 384 remoting.debug.log('Starting session...'); |
| 379 remoting.username = getEmail(); | 385 remoting.username = getEmail(); |
| 380 setGlobalMode(remoting.AppMode.IN_SESSION); | 386 setGlobalMode(remoting.AppMode.IN_SESSION); |
| 381 remoting.session = | 387 remoting.session = |
| 382 new remoting.ClientSession(remoting.hostJid, remoting.hostPublicKey, | 388 new remoting.ClientSession(remoting.hostJid, remoting.hostPublicKey, |
| 383 remoting.accessCode, getEmail(), | 389 remoting.accessCode, getEmail(), |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 document.getElementById('cancel-button').disabled = true; | 471 document.getElementById('cancel-button').disabled = true; |
| 466 if (remoting.currentMode == remoting.AppMode.HOST) { | 472 if (remoting.currentMode == remoting.AppMode.HOST) { |
| 467 remoting.cancelShare(); | 473 remoting.cancelShare(); |
| 468 } | 474 } |
| 469 } | 475 } |
| 470 | 476 |
| 471 /** | 477 /** |
| 472 * Changes the major-mode of the application (Eg., client or host). | 478 * Changes the major-mode of the application (Eg., client or host). |
| 473 * | 479 * |
| 474 * @param {remoting.AppMode} mode The mode to shift the application into. | 480 * @param {remoting.AppMode} mode The mode to shift the application into. |
| 475 * @return {void} | 481 * @return {void} Nothing. |
| 476 */ | 482 */ |
| 477 remoting.setAppMode = function(mode) { | 483 remoting.setAppMode = function(mode) { |
| 478 setGlobalMode(mode); | 484 setGlobalMode(mode); |
| 479 window.localStorage.setItem(KEY_APP_MODE_, mode); | 485 window.localStorage.setItem(KEY_APP_MODE_, mode); |
| 480 } | 486 } |
| 481 | 487 |
| 482 /** | 488 /** |
| 483 * Gets the major-mode that this application should start up in. | 489 * Gets the major-mode that this application should start up in. |
| 484 * | 490 * |
| 485 * @return {remoting.AppMode} | 491 * @return {remoting.AppMode} The mode (client or host) to start in. |
| 486 */ | 492 */ |
| 487 function getAppStartupMode() { | 493 function getAppStartupMode() { |
| 488 var mode = window.localStorage.getItem(KEY_APP_MODE_); | 494 var mode = window.localStorage.getItem(KEY_APP_MODE_); |
| 489 if (!mode) { | 495 if (!mode) { |
| 490 mode = remoting.AppMode.HOST; | 496 mode = remoting.AppMode.HOST; |
| 491 } | 497 } |
| 492 return mode; | 498 return mode; |
| 493 } | 499 } |
| 494 | 500 |
| 495 remoting.toggleScaleToFit = function() { | 501 remoting.toggleScaleToFit = function() { |
| 496 remoting.scaleToFit = !remoting.scaleToFit; | 502 remoting.scaleToFit = !remoting.scaleToFit; |
| 497 document.getElementById('scale-to-fit-toggle').value = | |
| 498 remoting.scaleToFit ? 'No scaling' : 'Scale to fit'; | |
| 499 remoting.session.toggleScaleToFit(remoting.scaleToFit); | 503 remoting.session.toggleScaleToFit(remoting.scaleToFit); |
| 500 } | 504 } |
| 501 | 505 |
| 502 }()); | 506 }()); |
| OLD | NEW |