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 /** @suppress {duplicate} */ | 5 /** @suppress {duplicate} */ |
6 var remoting = remoting || {}; | 6 var remoting = remoting || {}; |
7 | 7 |
8 (function() { | 8 (function() { |
9 'use strict'; | 9 'use strict'; |
10 | 10 |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 } | 150 } |
151 | 151 |
152 remoting.init = function() { | 152 remoting.init = function() { |
153 l10n.localize(); | 153 l10n.localize(); |
154 var button = document.getElementById('toggle-scaling'); | 154 var button = document.getElementById('toggle-scaling'); |
155 button.title = chrome.i18n.getMessage(/*i18n-content*/'TOOLTIP_SCALING'); | 155 button.title = chrome.i18n.getMessage(/*i18n-content*/'TOOLTIP_SCALING'); |
156 // Create global objects. | 156 // Create global objects. |
157 remoting.oauth2 = new remoting.OAuth2(); | 157 remoting.oauth2 = new remoting.OAuth2(); |
158 remoting.debug = | 158 remoting.debug = |
159 new remoting.DebugLog(document.getElementById('debug-messages')); | 159 new remoting.DebugLog(document.getElementById('debug-messages')); |
160 /** @type {XMLHttpRequest} */ | |
161 remoting.supportHostsXhr = null; | |
160 | 162 |
161 refreshEmail_(); | 163 refreshEmail_(); |
162 var email = getEmail(); | 164 var email = getEmail(); |
163 if (email) { | 165 if (email) { |
164 document.getElementById('current-email').innerText = email; | 166 document.getElementById('current-email').innerText = email; |
165 } | 167 } |
166 | 168 |
167 remoting.setMode(getAppStartupMode()); | 169 remoting.setMode(getAppStartupMode()); |
168 if (isHostModeSupported()) { | 170 if (isHostModeSupported()) { |
169 var unsupported = document.getElementById('client-footer-text-cros'); | 171 var unsupported = document.getElementById('client-footer-text-cros'); |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
371 plugin.parentNode.removeChild(plugin); | 373 plugin.parentNode.removeChild(plugin); |
372 } else if (state == plugin.ERROR) { | 374 } else if (state == plugin.ERROR) { |
373 remoting.debug.log('Host plugin state: ERROR'); | 375 remoting.debug.log('Host plugin state: ERROR'); |
374 showShareError_(/*i18n-content*/'ERROR_GENERIC'); | 376 showShareError_(/*i18n-content*/'ERROR_GENERIC'); |
375 } else { | 377 } else { |
376 remoting.debug.log('Unknown state -> ' + state); | 378 remoting.debug.log('Unknown state -> ' + state); |
377 } | 379 } |
378 } | 380 } |
379 | 381 |
380 /** | 382 /** |
381 * This is the callback that the host plugin invokes to indicate that there | 383 * This is the callback that the host plugin invokes to indicate that there |
382 * is additional debug log info to display. | 384 * is additional debug log info to display. |
383 * @param {string} msg The message (which will not be localized) to be logged. | 385 * @param {string} msg The message (which will not be localized) to be logged. |
384 */ | 386 */ |
385 function debugInfoCallback_(msg) { | 387 function debugInfoCallback_(msg) { |
386 remoting.debug.log('plugin: ' + msg); | 388 remoting.debug.log('plugin: ' + msg); |
387 } | 389 } |
388 | 390 |
389 /** | 391 /** |
390 * Show a host-side error message. | 392 * Show a host-side error message. |
391 * | 393 * |
392 * @param {string} errorTag The error message to be localized and displayed. | 394 * @param {string} errorTag The error message to be localized and displayed. |
393 * @return {void} Nothing. | 395 * @return {void} Nothing. |
394 */ | 396 */ |
395 function showShareError_(errorTag) { | 397 function showShareError_(errorTag) { |
396 var errorDiv = document.getElementById('host-plugin-error'); | 398 var errorDiv = document.getElementById('host-plugin-error'); |
397 l10n.localizeElementFromTag(errorDiv, errorTag); | 399 l10n.localizeElementFromTag(errorDiv, errorTag); |
398 remoting.debug.log('Sharing error: ' + errorTag); | 400 remoting.debug.log('Sharing error: ' + errorTag); |
399 remoting.setMode(remoting.AppMode.HOST_SHARE_FAILED); | 401 remoting.setMode(remoting.AppMode.HOST_SHARE_FAILED); |
400 } | 402 } |
401 | 403 |
404 /** | |
405 * Cancel an active or pending share operation. | |
406 * | |
407 * @return {void} Nothing. | |
408 */ | |
402 remoting.cancelShare = function() { | 409 remoting.cancelShare = function() { |
403 remoting.debug.log('Canceling share...'); | 410 remoting.debug.log('Canceling share...'); |
404 remoting.lastShareWasCancelled = true; | 411 remoting.lastShareWasCancelled = true; |
405 var plugin = /** @type {remoting.HostPlugin} */ | 412 var plugin = /** @type {remoting.HostPlugin} */ |
406 document.getElementById(remoting.HOST_PLUGIN_ID); | 413 document.getElementById(remoting.HOST_PLUGIN_ID); |
407 try { | 414 try { |
408 plugin.disconnect(); | 415 plugin.disconnect(); |
409 } catch (error) { | 416 } catch (error) { |
410 remoting.debug.log('Error disconnecting: ' + error.description + | 417 remoting.debug.log('Error disconnecting: ' + error.description + |
411 '. The host plugin probably crashed.'); | 418 '. The host plugin probably crashed.'); |
412 // TODO(jamiewalch): Clean this up. We should have a class representing | 419 // TODO(jamiewalch): Clean this up. We should have a class representing |
413 // the host plugin, like we do for the client, which should handle crash | 420 // the host plugin, like we do for the client, which should handle crash |
414 // reporting and it should use a more detailed error message than the | 421 // reporting and it should use a more detailed error message than the |
415 // default 'generic' one. See crbug.com/94624 | 422 // default 'generic' one. See crbug.com/94624 |
416 showShareError_(/*i18n-content*/'ERROR_GENERIC'); | 423 showShareError_(/*i18n-content*/'ERROR_GENERIC'); |
417 } | 424 } |
418 disableTimeoutCountdown_(); | 425 disableTimeoutCountdown_(); |
419 } | 426 } |
420 | 427 |
428 /** | |
429 * Cancel an incomplete connect operation. | |
430 * | |
431 * @return {void} Nothing. | |
432 */ | |
433 remoting.cancelConnect = function() { | |
434 if (remoting.supportHostsXhr) { | |
435 remoting.supportHostsXhr.abort(); | |
436 remoting.supportHostsXhr = null; | |
437 } | |
438 if (remoting.session) { | |
439 remoting.session.removePlugin(); | |
440 remoting.session = null; | |
441 } | |
442 remoting.setMode(remoting.AppMode.HOME); | |
443 } | |
444 | |
421 function updateStatistics() { | 445 function updateStatistics() { |
422 if (!remoting.session) | 446 if (!remoting.session) |
423 return; | 447 return; |
424 if (remoting.session.state != remoting.ClientSession.State.CONNECTED) | 448 if (remoting.session.state != remoting.ClientSession.State.CONNECTED) |
425 return; | 449 return; |
426 var stats = remoting.session.stats(); | 450 var stats = remoting.session.stats(); |
427 | 451 |
428 var units = ''; | 452 var units = ''; |
429 var videoBandwidth = stats['video_bandwidth']; | 453 var videoBandwidth = stats['video_bandwidth']; |
430 if (videoBandwidth < 1024) { | 454 if (videoBandwidth < 1024) { |
(...skipping 22 matching lines...) Expand all Loading... | |
453 window.setTimeout(updateStatistics, 1000); | 477 window.setTimeout(updateStatistics, 1000); |
454 } | 478 } |
455 | 479 |
456 function showToolbarPreview_() { | 480 function showToolbarPreview_() { |
457 var toolbar = document.getElementById('session-toolbar'); | 481 var toolbar = document.getElementById('session-toolbar'); |
458 addClass(toolbar, 'toolbar-preview'); | 482 addClass(toolbar, 'toolbar-preview'); |
459 window.setTimeout(removeClass, 3000, toolbar, 'toolbar-preview'); | 483 window.setTimeout(removeClass, 3000, toolbar, 'toolbar-preview'); |
460 } | 484 } |
461 | 485 |
462 function onClientStateChange_(oldState) { | 486 function onClientStateChange_(oldState) { |
487 if (!remoting.session) { | |
488 // If the connection has been cancelled, then we no longer have a reference | |
489 // to the session object and should ignore any state changes. | |
490 return; | |
491 } | |
463 var state = remoting.session.state; | 492 var state = remoting.session.state; |
464 if (state == remoting.ClientSession.State.CREATED) { | 493 if (state == remoting.ClientSession.State.CREATED) { |
465 remoting.debug.log('Created plugin'); | 494 remoting.debug.log('Created plugin'); |
466 } else if (state == remoting.ClientSession.State.BAD_PLUGIN_VERSION) { | 495 } else if (state == remoting.ClientSession.State.BAD_PLUGIN_VERSION) { |
467 showConnectError_(remoting.ClientError.MISSING_PLUGIN); | 496 showConnectError_(remoting.ClientError.MISSING_PLUGIN); |
468 } else if (state == remoting.ClientSession.State.CONNECTING) { | 497 } else if (state == remoting.ClientSession.State.CONNECTING) { |
469 remoting.debug.log('Connecting as ' + remoting.username); | 498 remoting.debug.log('Connecting as ' + remoting.username); |
470 } else if (state == remoting.ClientSession.State.INITIALIZING) { | 499 } else if (state == remoting.ClientSession.State.INITIALIZING) { |
471 remoting.debug.log('Initializing connection'); | 500 remoting.debug.log('Initializing connection'); |
472 } else if (state == remoting.ClientSession.State.CONNECTED) { | 501 } else if (state == remoting.ClientSession.State.CONNECTED) { |
473 remoting.setMode(remoting.AppMode.IN_SESSION); | 502 if (remoting.session) { |
Wez
2011/10/14 18:44:02
We shouldn't even reach here if !remoting.session?
| |
474 recenterToolbar_(); | 503 remoting.setMode(remoting.AppMode.IN_SESSION); |
475 showToolbarPreview_(); | 504 recenterToolbar_(); |
476 updateStatistics(); | 505 showToolbarPreview_(); |
477 var accessCode = document.getElementById('access-code-entry'); | 506 updateStatistics(); |
478 accessCode.value = ''; | 507 } |
479 } else if (state == remoting.ClientSession.State.CLOSED) { | 508 } else if (state == remoting.ClientSession.State.CLOSED) { |
480 if (oldState == remoting.ClientSession.State.CONNECTED) { | 509 if (oldState == remoting.ClientSession.State.CONNECTED) { |
481 remoting.session.removePlugin(); | 510 remoting.session.removePlugin(); |
482 remoting.session = null; | 511 remoting.session = null; |
483 remoting.debug.log('Connection closed by host'); | 512 remoting.debug.log('Connection closed by host'); |
484 remoting.setMode(remoting.AppMode.CLIENT_SESSION_FINISHED); | 513 remoting.setMode(remoting.AppMode.CLIENT_SESSION_FINISHED); |
485 } else { | 514 } else { |
486 // The transition from CONNECTING to CLOSED state may happen | 515 // The transition from CONNECTING to CLOSED state may happen |
487 // only with older client plugins. Current version should go the | 516 // only with older client plugins. Current version should go the |
488 // FAILED state when connection fails. | 517 // FAILED state when connection fails. |
(...skipping 19 matching lines...) Expand all Loading... | |
508 } else { | 537 } else { |
509 remoting.debug.log('Unexpected client plugin state: ' + state); | 538 remoting.debug.log('Unexpected client plugin state: ' + state); |
510 // This should only happen if the web-app and client plugin get out of | 539 // This should only happen if the web-app and client plugin get out of |
511 // sync, and even then the version check should allow compatibility. | 540 // sync, and even then the version check should allow compatibility. |
512 showConnectError_(remoting.ClientError.MISSING_PLUGIN); | 541 showConnectError_(remoting.ClientError.MISSING_PLUGIN); |
513 } | 542 } |
514 } | 543 } |
515 | 544 |
516 function startSession_() { | 545 function startSession_() { |
517 remoting.debug.log('Starting session...'); | 546 remoting.debug.log('Starting session...'); |
547 var accessCode = document.getElementById('access-code-entry'); | |
548 accessCode.value = ''; // The code has been validated and won't work again. | |
Wez
2011/10/14 18:44:02
nit: Move this comment above the var accessCode li
| |
518 remoting.username = | 549 remoting.username = |
519 /** @type {string} email must be non-NULL to get here */ getEmail(); | 550 /** @type {string} email must be non-NULL to get here */ getEmail(); |
520 remoting.session = | 551 remoting.session = |
521 new remoting.ClientSession(remoting.hostJid, remoting.hostPublicKey, | 552 new remoting.ClientSession(remoting.hostJid, remoting.hostPublicKey, |
522 remoting.accessCode, remoting.username, | 553 remoting.accessCode, remoting.username, |
523 onClientStateChange_); | 554 onClientStateChange_); |
524 remoting.oauth2.callWithToken(function(token) { | 555 remoting.oauth2.callWithToken(function(token) { |
525 remoting.session.createPluginAndConnect( | 556 remoting.session.createPluginAndConnect( |
526 document.getElementById('session-mode'), | 557 document.getElementById('session-mode'), |
527 token); | 558 token); |
(...skipping 12 matching lines...) Expand all Loading... | |
540 var errorDiv = document.getElementById('connect-error-message'); | 571 var errorDiv = document.getElementById('connect-error-message'); |
541 l10n.localizeElementFromTag(errorDiv, /** @type {string} */ (errorTag)); | 572 l10n.localizeElementFromTag(errorDiv, /** @type {string} */ (errorTag)); |
542 remoting.accessCode = ''; | 573 remoting.accessCode = ''; |
543 if (remoting.session) { | 574 if (remoting.session) { |
544 remoting.session.disconnect(); | 575 remoting.session.disconnect(); |
545 remoting.session = null; | 576 remoting.session = null; |
546 } | 577 } |
547 remoting.setMode(remoting.AppMode.CLIENT_CONNECT_FAILED); | 578 remoting.setMode(remoting.AppMode.CLIENT_CONNECT_FAILED); |
548 } | 579 } |
549 | 580 |
581 /** | |
582 * @param {XMLHttpRequest} xhr The XMLHttpRequest object. | |
583 * @return {void} Nothing. | |
584 */ | |
550 function parseServerResponse_(xhr) { | 585 function parseServerResponse_(xhr) { |
586 remoting.supportHostsXhr = null; | |
Wez
2011/10/14 18:44:02
nit: Should we check that xhr == supportHostsXhr f
| |
551 remoting.debug.log('parseServerResponse: status = ' + xhr.status); | 587 remoting.debug.log('parseServerResponse: status = ' + xhr.status); |
552 if (xhr.status == 200) { | 588 if (xhr.status == 200) { |
553 var host = JSON.parse(xhr.responseText); | 589 var host = JSON.parse(xhr.responseText); |
554 if (host.data && host.data.jabberId) { | 590 if (host.data && host.data.jabberId) { |
555 remoting.hostJid = host.data.jabberId; | 591 remoting.hostJid = host.data.jabberId; |
556 remoting.hostPublicKey = host.data.publicKey; | 592 remoting.hostPublicKey = host.data.publicKey; |
557 var split = remoting.hostJid.split('/'); | 593 var split = remoting.hostJid.split('/'); |
558 document.getElementById('connected-to').innerText = split[0]; | 594 document.getElementById('connected-to').innerText = split[0]; |
559 startSession_(); | 595 startSession_(); |
560 return; | 596 return; |
(...skipping 14 matching lines...) Expand all Loading... | |
575 // Trim whitespace. | 611 // Trim whitespace. |
576 // TODO(sergeyu): Do we need to do any other normalization here? | 612 // TODO(sergeyu): Do we need to do any other normalization here? |
577 return accessCode.replace(/\s/g, ''); | 613 return accessCode.replace(/\s/g, ''); |
578 } | 614 } |
579 | 615 |
580 function resolveSupportId(supportId) { | 616 function resolveSupportId(supportId) { |
581 var headers = { | 617 var headers = { |
582 'Authorization': 'OAuth ' + remoting.oauth2.getAccessToken() | 618 'Authorization': 'OAuth ' + remoting.oauth2.getAccessToken() |
583 }; | 619 }; |
584 | 620 |
585 remoting.xhr.get( | 621 remoting.supportHostsXhr = remoting.xhr.get( |
586 'https://www.googleapis.com/chromoting/v1/support-hosts/' + | 622 'https://www.googleapis.com/chromoting/v1/support-hosts/' + |
587 encodeURIComponent(supportId), | 623 encodeURIComponent(supportId), |
588 parseServerResponse_, | 624 parseServerResponse_, |
589 '', | 625 '', |
590 headers); | 626 headers); |
591 } | 627 } |
592 | 628 |
593 remoting.tryConnect = function() { | 629 remoting.tryConnect = function() { |
630 document.getElementById('cancel-button').disabled = false; | |
594 if (remoting.oauth2.needsNewAccessToken()) { | 631 if (remoting.oauth2.needsNewAccessToken()) { |
595 remoting.oauth2.refreshAccessToken(function(xhr) { | 632 remoting.oauth2.refreshAccessToken(function(xhr) { |
596 if (remoting.oauth2.needsNewAccessToken()) { | 633 if (remoting.oauth2.needsNewAccessToken()) { |
597 // Failed to get access token | 634 // Failed to get access token |
598 remoting.debug.log('tryConnect: OAuth2 token fetch failed'); | 635 remoting.debug.log('tryConnect: OAuth2 token fetch failed'); |
599 showConnectError_(remoting.ClientError.OAUTH_FETCH_FAILED); | 636 showConnectError_(remoting.ClientError.OAUTH_FETCH_FAILED); |
600 return; | 637 return; |
601 } | 638 } |
602 remoting.tryConnectWithAccessToken(); | 639 remoting.tryConnectWithAccessToken(); |
603 }); | 640 }); |
(...skipping 24 matching lines...) Expand all Loading... | |
628 showConnectError_(remoting.ClientError.INVALID_ACCESS_CODE); | 665 showConnectError_(remoting.ClientError.INVALID_ACCESS_CODE); |
629 } else { | 666 } else { |
630 var supportId = remoting.accessCode.substring(0, kSupportIdLen); | 667 var supportId = remoting.accessCode.substring(0, kSupportIdLen); |
631 remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); | 668 remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); |
632 resolveSupportId(supportId); | 669 resolveSupportId(supportId); |
633 } | 670 } |
634 } | 671 } |
635 | 672 |
636 remoting.cancelPendingOperation = function() { | 673 remoting.cancelPendingOperation = function() { |
637 document.getElementById('cancel-button').disabled = true; | 674 document.getElementById('cancel-button').disabled = true; |
638 if (remoting.getMajorMode() == remoting.AppMode.HOST) { | 675 switch (remoting.getMajorMode()) { |
639 remoting.cancelShare(); | 676 case remoting.AppMode.HOST: |
677 remoting.cancelShare(); | |
678 break; | |
679 case remoting.AppMode.CLIENT: | |
680 remoting.cancelConnect(); | |
681 break; | |
Wez
2011/10/14 18:44:02
nit: This might actually read better as an if...el
| |
640 } | 682 } |
641 } | 683 } |
642 | 684 |
643 /** | 685 /** |
644 * Gets the major-mode that this application should start up in. | 686 * Gets the major-mode that this application should start up in. |
645 * | 687 * |
646 * @return {remoting.AppMode} The mode to start in. | 688 * @return {remoting.AppMode} The mode to start in. |
647 */ | 689 */ |
648 function getAppStartupMode() { | 690 function getAppStartupMode() { |
649 if (!remoting.oauth2.isAuthenticated()) { | 691 if (!remoting.oauth2.isAuthenticated()) { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
733 } | 775 } |
734 } | 776 } |
735 | 777 |
736 function recenterToolbar_() { | 778 function recenterToolbar_() { |
737 var toolbar = document.getElementById('session-toolbar'); | 779 var toolbar = document.getElementById('session-toolbar'); |
738 var toolbarX = (window.innerWidth - toolbar.clientWidth) / 2; | 780 var toolbarX = (window.innerWidth - toolbar.clientWidth) / 2; |
739 toolbar.style['left'] = toolbarX + 'px'; | 781 toolbar.style['left'] = toolbarX + 'px'; |
740 } | 782 } |
741 | 783 |
742 }()); | 784 }()); |
OLD | NEW |