Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / remoting / webapp / crd / js / it2me_activity.js
blobb10e85f86f0639364e361435e927860920c0b010
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_ = new remoting.InputDialog(
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;
39 remoting.It2MeActivity.prototype.dispose = function() {
40   base.dispose(this.desktopActivity_);
41   this.desktopActivity_ = null;
44 remoting.It2MeActivity.prototype.start = function() {
45   var that = this;
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);
52   }).then(function() {
53     return remoting.HostListApi.getInstance().getSupportHost(that.hostId_);
54   }).then(function(/** remoting.Host */ host) {
55     that.connect_(host);
56   }).catch(remoting.Error.handler(function(/** remoting.Error */ error) {
57     if (error.hasTag(remoting.Error.Tag.CANCELLED)) {
58       remoting.setMode(remoting.AppMode.HOME);
59     } else {
60       var errorDiv = document.getElementById('connect-error-message');
61       l10n.localizeElementFromTag(errorDiv, error.getTag());
62       remoting.setMode(remoting.AppMode.CLIENT_CONNECT_FAILED_IT2ME);
63     }
64   }));
67 remoting.It2MeActivity.prototype.stop = function() {
68   this.desktopActivity_.stop();
71 /**
72  * @param {!remoting.Error} error
73  */
74 remoting.It2MeActivity.prototype.onConnectionFailed = function(error) {
75   this.showErrorMessage_(error);
76   base.dispose(this.desktopActivity_);
77   this.desktopActivity_ = null;
80 /**
81  * @param {!remoting.ConnectionInfo} connectionInfo
82  */
83 remoting.It2MeActivity.prototype.onConnected = function(connectionInfo) {
84   this.accessCodeDialog_.inputField().value = '';
87 remoting.It2MeActivity.prototype.onDisconnected = function(error) {
88   if (error.isNone()) {
89     this.showFinishDialog_(remoting.AppMode.CLIENT_SESSION_FINISHED_IT2ME);
90   } else {
91     this.showErrorMessage_(error);
92   }
94   base.dispose(this.desktopActivity_);
95   this.desktopActivity_ = null;
98 /**
99  * @param {!remoting.Error} error
100  * @private
101  */
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
115  * @private
116  */
117 remoting.It2MeActivity.prototype.showFinishDialog_ = function(mode) {
118   var finishDialog = new remoting.MessageDialog(
119       mode,
120       document.getElementById('client-finished-it2me-button'));
121   finishDialog.show().then(function() {
122     remoting.setMode(remoting.AppMode.HOME);
123   });
127  * @param {string} accessCode
128  * @return {Promise}  Promise that resolves if the access code is valid.
129  * @private
130  */
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));
136   }
138   this.hostId_ = normalizedAccessCode.substring(0, SUPPORT_ID_LENGTH);
139   this.passCode_ = normalizedAccessCode;
141   return Promise.resolve();
145  * @param {remoting.Host} host
146  * @private
147  */
148 remoting.It2MeActivity.prototype.connect_ = function(host) {
149   this.desktopActivity_.start(
150       host, new remoting.CredentialsProvider({ accessCode: this.passCode_ }));
153 })();