Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / remoting / webapp / crd / js / it2me_activity.js
blob875aed1be2d077a1a9f8870e1079cadda9688565
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   if (this.desktopActivity_) {
69     this.desktopActivity_.stop();
70   }
73 /**
74  * @param {!remoting.Error} error
75  */
76 remoting.It2MeActivity.prototype.onConnectionFailed = function(error) {
77   this.showErrorMessage_(error);
78   base.dispose(this.desktopActivity_);
79   this.desktopActivity_ = null;
82 /**
83  * @param {!remoting.ConnectionInfo} connectionInfo
84  */
85 remoting.It2MeActivity.prototype.onConnected = function(connectionInfo) {
86   this.accessCodeDialog_.inputField().value = '';
89 remoting.It2MeActivity.prototype.onDisconnected = function(error) {
90   if (error.isNone()) {
91     this.showFinishDialog_(remoting.AppMode.CLIENT_SESSION_FINISHED_IT2ME);
92   } else {
93     this.showErrorMessage_(error);
94   }
96   base.dispose(this.desktopActivity_);
97   this.desktopActivity_ = null;
101  * @param {!remoting.Error} error
102  * @private
103  */
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
117  * @private
118  */
119 remoting.It2MeActivity.prototype.showFinishDialog_ = function(mode) {
120   var finishDialog = new remoting.MessageDialog(
121       mode,
122       document.getElementById('client-finished-it2me-button'));
123   finishDialog.show().then(function() {
124     remoting.setMode(remoting.AppMode.HOME);
125   });
129  * @param {string} accessCode
130  * @return {Promise}  Promise that resolves if the access code is valid.
131  * @private
132  */
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));
138   }
140   this.hostId_ = normalizedAccessCode.substring(0, SUPPORT_ID_LENGTH);
141   this.passCode_ = normalizedAccessCode;
143   return Promise.resolve();
147  * @param {remoting.Host} host
148  * @private
149  */
150 remoting.It2MeActivity.prototype.connect_ = function(host) {
151   this.desktopActivity_.start(
152       host, new remoting.CredentialsProvider({ accessCode: this.passCode_ }));
155 })();