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 this.desktopActivity_.stop();
72 * @param {!remoting.Error} error
74 remoting.It2MeActivity.prototype.onConnectionFailed = function(error) {
75 this.showErrorMessage_(error);
76 base.dispose(this.desktopActivity_);
77 this.desktopActivity_ = null;
81 * @param {!remoting.ConnectionInfo} connectionInfo
83 remoting.It2MeActivity.prototype.onConnected = function(connectionInfo) {
84 this.accessCodeDialog_.inputField().value = '';
87 remoting.It2MeActivity.prototype.onDisconnected = function(error) {
89 this.showFinishDialog_(remoting.AppMode.CLIENT_SESSION_FINISHED_IT2ME);
91 this.showErrorMessage_(error);
94 base.dispose(this.desktopActivity_);
95 this.desktopActivity_ = null;
99 * @param {!remoting.Error} error
102 remoting.It2MeActivity.prototype.showErrorMessage_ = function(error) {
103 var errorDiv = document.getElementById('connect-error-message');
104 l10n.localizeElementFromTag(errorDiv, error.getTag());
105 this.showFinishDialog_(remoting.AppMode.CLIENT_CONNECT_FAILED_IT2ME);
108 /** @return {remoting.DesktopRemotingActivity} */
109 remoting.It2MeActivity.prototype.getDesktopActivityForTesting = function() {
110 return this.desktopActivity_;
114 * @param {remoting.AppMode} mode
117 remoting.It2MeActivity.prototype.showFinishDialog_ = function(mode) {
118 var finishDialog = new remoting.MessageDialog(
120 document.getElementById('client-finished-it2me-button'));
121 finishDialog.show().then(function() {
122 remoting.setMode(remoting.AppMode.HOME);
127 * @param {string} accessCode
128 * @return {Promise} Promise that resolves if the access code is valid.
131 remoting.It2MeActivity.prototype.verifyAccessCode_ = function(accessCode) {
132 var normalizedAccessCode = accessCode.replace(/\s/g, '');
133 if (normalizedAccessCode.length !== ACCESS_CODE_LENGTH) {
134 return Promise.reject(new remoting.Error(
135 remoting.Error.Tag.INVALID_ACCESS_CODE));
138 this.hostId_ = normalizedAccessCode.substring(0, SUPPORT_ID_LENGTH);
139 this.passCode_ = normalizedAccessCode;
141 return Promise.resolve();
145 * @param {remoting.Host} host
148 remoting.It2MeActivity.prototype.connect_ = function(host) {
149 this.desktopActivity_.start(
150 host, new remoting.CredentialsProvider({ accessCode: this.passCode_ }));