Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / remoting / webapp / crd / js / it2me_activity.js
blobc334ea337c3cea9cd47d254c5d0e5c435c5fad30
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 || {};
8 (function() {
10 'use strict';
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;
17 /**
18  * @constructor
19  * @implements {remoting.Activity}
20  */
21 remoting.It2MeActivity = function() {
22   /** @private */
23   this.hostId_ = '';
24   /** @private */
25   this.passCode_ = '';
27   var form = document.getElementById('access-code-form');
28   /** @private */
29   this.accessCodeDialog_ = remoting.modalDialogFactory.createInputDialog(
30     remoting.AppMode.CLIENT_UNCONNECTED,
31     form,
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} */
38   this.logger_ = null;
42 remoting.It2MeActivity.prototype.dispose = function() {
43   base.dispose(this.desktopActivity_);
44   this.desktopActivity_ = null;
47 remoting.It2MeActivity.prototype.start = function() {
48   var that = this;
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);
60   }).then(function() {
61     return remoting.HostListApi.getInstance().getSupportHost(that.hostId_);
62   }).then(function(/** remoting.Host */ host) {
63     that.connect_(host);
64   }).catch(remoting.Error.handler(function(/** remoting.Error */ error) {
65     if (error.hasTag(remoting.Error.Tag.CANCELLED)) {
66       remoting.setMode(remoting.AppMode.HOME);
67     } else {
68       that.showErrorMessage_(error);
69     }
70   }));
74 /**
75  * @return {!remoting.SessionLogger}
76  * @private
77  */
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);
83   return logger;
86 remoting.It2MeActivity.prototype.stop = function() {
87   if (this.desktopActivity_) {
88     this.desktopActivity_.stop();
89   }
92 /**
93  * @param {!remoting.Error} error
94  */
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
103  */
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);
111   } else {
112     this.showErrorMessage_(error);
113   }
115   base.dispose(this.desktopActivity_);
116   this.desktopActivity_ = null;
120  * @param {!remoting.Error} error
121  * @private
122  */
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
136  * @private
137  */
138 remoting.It2MeActivity.prototype.showFinishDialog_ = function(mode) {
139   var finishDialog = new remoting.MessageDialog(
140       mode,
141       document.getElementById('client-finished-it2me-button'));
142   finishDialog.show().then(function() {
143     remoting.setMode(remoting.AppMode.HOME);
144   });
148  * @param {string} accessCode
149  * @return {Promise}  Promise that resolves if the access code is valid.
150  * @private
151  */
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));
157   }
159   this.hostId_ = normalizedAccessCode.substring(0, SUPPORT_ID_LENGTH);
160   this.passCode_ = normalizedAccessCode;
162   return Promise.resolve();
166  * @param {remoting.Host} host
167  * @private
168  */
169 remoting.It2MeActivity.prototype.connect_ = function(host) {
170   this.desktopActivity_.start(
171       host, new remoting.CredentialsProvider({accessCode: this.passCode_}),
172       this.logger_);
175 })();