1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 /** @suppress {duplicate} */
6 var remoting = remoting || {};
12 // Length of the various components of the access code in number of digits.
13 var SUPPORT_ID_LENGTH = 7;
14 var HOST_SECRET_LENGTH = 5;
15 var ACCESS_CODE_LENGTH = SUPPORT_ID_LENGTH + HOST_SECRET_LENGTH;
19 * @implements {remoting.Activity}
21 remoting.It2MeActivity = function() {
27 var form = document.getElementById('access-code-form');
29 this.accessCodeDialog_ = new remoting.InputDialog(
30 remoting.AppMode.CLIENT_UNCONNECTED,
32 form.querySelector('#access-code-entry'),
33 form.querySelector('#cancel-access-code-button'));
35 /** @private {remoting.DesktopRemotingActivity} */
36 this.desktopActivity_ = null;
39 remoting.It2MeActivity.prototype.dispose = function() {
40 base.dispose(this.desktopActivity_);
41 this.desktopActivity_ = null;
44 remoting.It2MeActivity.prototype.start = function() {
47 this.desktopActivity_ = new remoting.DesktopRemotingActivity(this);
49 this.accessCodeDialog_.show().then(function(/** string */ accessCode) {
50 that.desktopActivity_.getConnectingDialog().show();
51 return that.verifyAccessCode_(accessCode);
53 return remoting.HostListApi.getInstance().getSupportHost(that.hostId_);
54 }).then(function(/** remoting.Host */ host) {
56 }).catch(remoting.Error.handler(function(/** remoting.Error */ error) {
57 if (error.hasTag(remoting.Error.Tag.CANCELLED)) {
58 remoting.setMode(remoting.AppMode.HOME);
60 var errorDiv = document.getElementById('connect-error-message');
61 l10n.localizeElementFromTag(errorDiv, error.getTag());
62 remoting.setMode(remoting.AppMode.CLIENT_CONNECT_FAILED_IT2ME);
67 remoting.It2MeActivity.prototype.stop = function() {
68 if (this.desktopActivity_) {
69 this.desktopActivity_.stop();
74 * @param {!remoting.Error} error
76 remoting.It2MeActivity.prototype.onConnectionFailed = function(error) {
77 this.showErrorMessage_(error);
78 base.dispose(this.desktopActivity_);
79 this.desktopActivity_ = null;
83 * @param {!remoting.ConnectionInfo} connectionInfo
85 remoting.It2MeActivity.prototype.onConnected = function(connectionInfo) {
86 this.accessCodeDialog_.inputField().value = '';
89 remoting.It2MeActivity.prototype.onDisconnected = function(error) {
91 this.showFinishDialog_(remoting.AppMode.CLIENT_SESSION_FINISHED_IT2ME);
93 this.showErrorMessage_(error);
96 base.dispose(this.desktopActivity_);
97 this.desktopActivity_ = null;
101 * @param {!remoting.Error} error
104 remoting.It2MeActivity.prototype.showErrorMessage_ = function(error) {
105 var errorDiv = document.getElementById('connect-error-message');
106 l10n.localizeElementFromTag(errorDiv, error.getTag());
107 this.showFinishDialog_(remoting.AppMode.CLIENT_CONNECT_FAILED_IT2ME);
110 /** @return {remoting.DesktopRemotingActivity} */
111 remoting.It2MeActivity.prototype.getDesktopActivityForTesting = function() {
112 return this.desktopActivity_;
116 * @param {remoting.AppMode} mode
119 remoting.It2MeActivity.prototype.showFinishDialog_ = function(mode) {
120 var finishDialog = new remoting.MessageDialog(
122 document.getElementById('client-finished-it2me-button'));
123 finishDialog.show().then(function() {
124 remoting.setMode(remoting.AppMode.HOME);
129 * @param {string} accessCode
130 * @return {Promise} Promise that resolves if the access code is valid.
133 remoting.It2MeActivity.prototype.verifyAccessCode_ = function(accessCode) {
134 var normalizedAccessCode = accessCode.replace(/\s/g, '');
135 if (normalizedAccessCode.length !== ACCESS_CODE_LENGTH) {
136 return Promise.reject(new remoting.Error(
137 remoting.Error.Tag.INVALID_ACCESS_CODE));
140 this.hostId_ = normalizedAccessCode.substring(0, SUPPORT_ID_LENGTH);
141 this.passCode_ = normalizedAccessCode;
143 return Promise.resolve();
147 * @param {remoting.Host} host
150 remoting.It2MeActivity.prototype.connect_ = function(host) {
151 this.desktopActivity_.start(
152 host, new remoting.CredentialsProvider({ accessCode: this.passCode_ }));