Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / remoting / webapp / app_remoting / js / app_remoting_activity.js
blobf5d9bd37768069bfae99f42b2f970aa995f9c973
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 /**
9  * Type definition for the RunApplicationResponse returned by the API.
10  * @typedef {{
11  *   status: string,
12  *   hostJid: string,
13  *   authorizationCode: string,
14  *   sharedSecret: string,
15  *   host: {
16  *     applicationId: string,
17  *     hostId: string
18  *   }
19  * }}
20  */
21 remoting.AppHostResponse;
23 (function() {
25 'use strict';
27 /**
28  * @param {Array<string>} appCapabilities Array of application capabilities.
29  * @param {remoting.Application} app
30  *
31  * @constructor
32  * @implements {remoting.Activity}
33  */
34 remoting.AppRemotingActivity = function(appCapabilities, app) {
35   /** @private */
36   this.sessionFactory_ = new remoting.ClientSessionFactory(
37       document.querySelector('#client-container .client-plugin-container'),
38       appCapabilities);
40   /** @private {remoting.ClientSession} */
41   this.session_ = null;
43   /** @private {base.Disposables} */
44   this.connectedDisposables_ = null;
46   /** @private */
47   this.app_ = app;
50 remoting.AppRemotingActivity.prototype.dispose = function() {
51   this.cleanup_();
52   remoting.LoadingWindow.close();
55 remoting.AppRemotingActivity.prototype.start = function() {
56   remoting.LoadingWindow.show();
57   var that = this;
58   return remoting.identity.getToken().then(function(/** string */ token) {
59     return that.getAppHostInfo_(token);
60   }).then(function(/** !remoting.Xhr.Response */ response) {
61     that.onAppHostResponse_(response);
62   });
65 remoting.AppRemotingActivity.prototype.stop = function() {
66   if (this.session_) {
67     this.session_.disconnect(remoting.Error.none());
68   }
71 /** @private */
72 remoting.AppRemotingActivity.prototype.cleanup_ = function() {
73   base.dispose(this.connectedDisposables_);
74   this.connectedDisposables_ = null;
75   base.dispose(this.session_);
76   this.session_ = null;
79 /**
80  * @param {string} token
81  * @return {Promise<!remoting.Xhr.Response>}
82  * @private
83  */
84 remoting.AppRemotingActivity.prototype.getAppHostInfo_ = function(token) {
85   var url = remoting.settings.APP_REMOTING_API_BASE_URL + '/applications/' +
86             remoting.settings.getAppRemotingApplicationId() + '/run';
87   return new remoting.Xhr({
88     method: 'POST',
89     url: url,
90     oauthToken: token
91   }).start();
94 /**
95  * @param {!remoting.Xhr.Response} xhrResponse
96  * @private
97  */
98 remoting.AppRemotingActivity.prototype.onAppHostResponse_ =
99     function(xhrResponse) {
100   if (xhrResponse.status == 200) {
101     var response = /** @type {remoting.AppHostResponse} */
102         (base.jsonParseSafe(xhrResponse.getText()));
103     if (response &&
104         response.status &&
105         response.status == 'done' &&
106         response.hostJid &&
107         response.authorizationCode &&
108         response.sharedSecret &&
109         response.host &&
110         response.host.hostId) {
111       var hostJid = response.hostJid;
112       var host = new remoting.Host(response.host.hostId);
113       host.jabberId = hostJid;
114       host.authorizationCode = response.authorizationCode;
115       host.sharedSecret = response.sharedSecret;
117       remoting.setMode(remoting.AppMode.CLIENT_CONNECTING);
119       /**
120        * @param {string} tokenUrl Token-issue URL received from the host.
121        * @param {string} hostPublicKey Host public key (DER and Base64
122        *     encoded).
123        * @param {string} scope OAuth scope to request the token for.
124        * @param {function(string, string):void} onThirdPartyTokenFetched
125        *     Callback.
126        */
127       var fetchThirdPartyToken = function(
128           tokenUrl, hostPublicKey, scope, onThirdPartyTokenFetched) {
129         // Use the authentication tokens returned by the app-remoting server.
130         onThirdPartyTokenFetched(host['authorizationCode'],
131                                  host['sharedSecret']);
132       };
134       var credentialsProvider = new remoting.CredentialsProvider(
135               {fetchThirdPartyToken: fetchThirdPartyToken});
136       var that = this;
138       this.sessionFactory_.createSession(this).then(
139         function(/** remoting.ClientSession */ session) {
140           that.session_ = session;
141           session.logHostOfflineErrors(true);
142           session.getLogger().setLogEntryMode(
143               remoting.ServerLogEntry.VALUE_MODE_APP_REMOTING);
144           session.connect(host, credentialsProvider);
145       });
146     } else if (response && response.status == 'pending') {
147       this.onConnectionFailed(new remoting.Error(
148           remoting.Error.Tag.SERVICE_UNAVAILABLE));
149     }
150   } else {
151     console.error('Invalid "runApplication" response from server.');
152     // The orchestrator returns 403 if the user is not whitelisted to run the
153     // app, which gets translated to a generic error message, so pick something
154     // a bit more user-friendly.
155     var error = xhrResponse.status == 403 ?
156         new remoting.Error(remoting.Error.Tag.APP_NOT_AUTHORIZED) :
157         remoting.Error.fromHttpStatus(xhrResponse.status);
158     this.onConnectionFailed(error);
159   }
163  * @param {remoting.ConnectionInfo} connectionInfo
164  */
165 remoting.AppRemotingActivity.prototype.onConnected = function(connectionInfo) {
166   var connectedView = new remoting.AppConnectedView(
167       document.getElementById('client-container'), connectionInfo);
169   var idleDetector = new remoting.IdleDetector(
170       document.getElementById('idle-dialog'),
171       this.app_.getApplicationName(),
172       this.stop.bind(this));
174   // Map Cmd to Ctrl on Mac since hosts typically use Ctrl for keyboard
175   // shortcuts, but we want them to act as natively as possible.
176   if (remoting.platformIsMac()) {
177     connectionInfo.plugin().setRemapKeys('0x0700e3>0x0700e0,0x0700e7>0x0700e4');
178   }
180   // Drop the session after 30s of suspension as we cannot recover from a
181   // connectivity loss longer than 30s anyways.
182   this.session_.dropSessionOnSuspend(30 * 1000);
183   this.connectedDisposables_ =
184       new base.Disposables(idleDetector, connectedView);
188  * @param {remoting.Error} error
189  */
190 remoting.AppRemotingActivity.prototype.onDisconnected = function(error) {
191   if (error.isNone()) {
192     this.app_.quit();
193   } else {
194     this.onConnectionDropped_();
195   }
199  * @param {!remoting.Error} error
200  */
201 remoting.AppRemotingActivity.prototype.onConnectionFailed = function(error) {
202   remoting.LoadingWindow.close();
203   this.showErrorMessage_(error);
204   this.cleanup_();
207 /** @private */
208 remoting.AppRemotingActivity.prototype.onConnectionDropped_ = function() {
209   // Don't dispose the session here to keep the plugin alive so that we can show
210   // the last frame of the remote application window.
211   base.dispose(this.connectedDisposables_);
212   this.connectedDisposables_ = null;
214   var rootElement = /** @type {HTMLDialogElement} */ (
215       document.getElementById('connection-dropped-dialog'));
216   var dialog = new remoting.ConnectionDroppedDialog(rootElement);
217   var that = this;
218   dialog.show().then(function(){
219     dialog.dispose();
220     // Hide the windows of the remote application with setDesktopRects([])
221     // before tearing down the plugin.
222     remoting.windowShape.setDesktopRects([]);
223     that.cleanup_();
224     that.start();
225   }).catch(function(){
226     dialog.dispose();
227     chrome.app.window.current().close();
228   });
232  * @param {!remoting.Error} error The error to be localized and displayed.
233  * @private
234  */
235 remoting.AppRemotingActivity.prototype.showErrorMessage_ = function(error) {
236   console.error('Connection failed: ' + error.toString());
237   remoting.MessageWindow.showErrorMessage(
238       this.app_.getApplicationName(),
239       chrome.i18n.getMessage(error.getTag()));
242 })();