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_ = remoting.modalDialogFactory.createInputDialog(
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;
37 /** @private {remoting.SessionLogger} */
42 remoting.It2MeActivity.prototype.dispose = function() {
43 base.dispose(this.desktopActivity_);
44 this.desktopActivity_ = null;
47 remoting.It2MeActivity.prototype.start = function() {
50 this.logger_ = this.createLogger_();
51 this.logger_.logSessionStateChange(
52 remoting.ChromotingEvent.SessionState.STARTED,
53 remoting.ChromotingEvent.ConnectionError.NONE);
55 this.desktopActivity_ = new remoting.DesktopRemotingActivity(this);
57 this.accessCodeDialog_.show().then(function(/** string */ accessCode) {
58 that.desktopActivity_.getConnectingDialog().show();
59 return that.verifyAccessCode_(accessCode);
61 return remoting.HostListApi.getInstance().getSupportHost(that.hostId_);
62 }).then(function(/** remoting.Host */ host) {
64 }).catch(remoting.Error.handler(function(/** remoting.Error */ error) {
65 if (error.hasTag(remoting.Error.Tag.CANCELLED)) {
66 remoting.setMode(remoting.AppMode.HOME);
68 that.showErrorMessage_(error);
75 * @return {!remoting.SessionLogger}
78 remoting.It2MeActivity.prototype.createLogger_ = function() {
79 var Event = remoting.ChromotingEvent;
80 var logger = remoting.SessionLogger.createForClient();
81 logger.setEntryPoint(Event.SessionEntryPoint.CONNECT_BUTTON);
82 logger.setLogEntryMode(Event.Mode.IT2ME);
86 remoting.It2MeActivity.prototype.stop = function() {
87 if (this.desktopActivity_) {
88 this.desktopActivity_.stop();
93 * @param {!remoting.Error} error
95 remoting.It2MeActivity.prototype.onConnectionFailed = function(error) {
96 this.showErrorMessage_(error);
97 base.dispose(this.desktopActivity_);
98 this.desktopActivity_ = null;
102 * @param {!remoting.ConnectionInfo} connectionInfo
104 remoting.It2MeActivity.prototype.onConnected = function(connectionInfo) {
105 this.accessCodeDialog_.inputField().value = '';
108 remoting.It2MeActivity.prototype.onDisconnected = function(error) {
109 if (error.isNone()) {
110 this.showFinishDialog_(remoting.AppMode.CLIENT_SESSION_FINISHED_IT2ME);
112 this.showErrorMessage_(error);
115 base.dispose(this.desktopActivity_);
116 this.desktopActivity_ = null;
120 * @param {!remoting.Error} error
123 remoting.It2MeActivity.prototype.showErrorMessage_ = function(error) {
124 var errorDiv = document.getElementById('connect-error-message');
125 l10n.localizeElementFromTag(errorDiv, error.getTag());
126 this.showFinishDialog_(remoting.AppMode.CLIENT_CONNECT_FAILED_IT2ME);
129 /** @return {remoting.DesktopRemotingActivity} */
130 remoting.It2MeActivity.prototype.getDesktopActivityForTesting = function() {
131 return this.desktopActivity_;
135 * @param {remoting.AppMode} mode
138 remoting.It2MeActivity.prototype.showFinishDialog_ = function(mode) {
139 var finishDialog = new remoting.MessageDialog(
141 document.getElementById('client-finished-it2me-button'));
142 finishDialog.show().then(function() {
143 remoting.setMode(remoting.AppMode.HOME);
148 * @param {string} accessCode
149 * @return {Promise} Promise that resolves if the access code is valid.
152 remoting.It2MeActivity.prototype.verifyAccessCode_ = function(accessCode) {
153 var normalizedAccessCode = accessCode.replace(/\s/g, '');
154 if (normalizedAccessCode.length !== ACCESS_CODE_LENGTH) {
155 return Promise.reject(new remoting.Error(
156 remoting.Error.Tag.INVALID_ACCESS_CODE));
159 this.hostId_ = normalizedAccessCode.substring(0, SUPPORT_ID_LENGTH);
160 this.passCode_ = normalizedAccessCode;
162 return Promise.resolve();
166 * @param {remoting.Host} host
169 remoting.It2MeActivity.prototype.connect_ = function(host) {
170 this.desktopActivity_.start(
171 host, new remoting.CredentialsProvider({accessCode: this.passCode_}),