Roll src/third_party/WebKit 3529d49:06e8485 (svn 202554:202555)
[chromium-blink-merge.git] / remoting / webapp / app_remoting / js / app_remoting_activity.js
blobb1930bad33481371339bf13c3d4ab1c19f104a45
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  * @param {remoting.WindowShape} windowShape
31  * @param {string} subscriptionToken
32  * @param {base.WindowMessageDispatcher} windowMessageDispatcher
33  *
34  * @constructor
35  * @implements {remoting.Activity}
36  */
37 remoting.AppRemotingActivity = function(appCapabilities, app, windowShape,
38                                         subscriptionToken,
39                                         windowMessageDispatcher) {
40   /** @private */
41   this.sessionFactory_ = new remoting.ClientSessionFactory(
42       document.querySelector('#client-container .client-plugin-container'),
43       appCapabilities);
45   /** @private {remoting.ClientSession} */
46   this.session_ = null;
48   /** @private {base.Disposables} */
49   this.connectedDisposables_ = null;
51   /** @private */
52   this.app_ = app;
54   /** @private */
55   this.windowShape_ = windowShape;
57   /** @private */
58   this.subscriptionToken_ = subscriptionToken;
60   /** @private {base.WindowMessageDispatcher} */
61   this.windowMessageDispatcher_ = windowMessageDispatcher;
64 remoting.AppRemotingActivity.prototype.dispose = function() {
65   this.cleanup_();
66   remoting.LoadingWindow.close();
69 remoting.AppRemotingActivity.prototype.start = function() {
70   remoting.LoadingWindow.show();
71   var that = this;
72   return remoting.identity.getToken().then(function(/** string */ token) {
73     return that.getAppHostInfo_(token);
74   }).then(function(/** !remoting.Xhr.Response */ response) {
75     that.onAppHostResponse_(response);
76   }).catch(function(/** !remoting.Error */ error) {
77     that.onConnectionFailed(error);
78   });
81 remoting.AppRemotingActivity.prototype.stop = function() {
82   if (this.session_) {
83     this.session_.disconnect(remoting.Error.none());
84   }
87 /** @private */
88 remoting.AppRemotingActivity.prototype.cleanup_ = function() {
89   base.dispose(this.connectedDisposables_);
90   this.connectedDisposables_ = null;
91   base.dispose(this.session_);
92   this.session_ = null;
95 /**
96  * @param {string} token
97  * @return {Promise<!remoting.Xhr.Response>}
98  * @private
99  */
100 remoting.AppRemotingActivity.prototype.getAppHostInfo_ = function(token) {
101   var url = remoting.settings.APP_REMOTING_API_BASE_URL + '/applications/' +
102             this.app_.getApplicationId() + '/run';
103   // TODO(kelvinp): Passes |this.subscriptionToken_| to the XHR.
104   return new remoting.AutoRetryXhr({
105     method: 'POST',
106     url: url,
107     oauthToken: token
108   }).start();
112  * @param {!remoting.Xhr.Response} xhrResponse
113  * @private
114  */
115 remoting.AppRemotingActivity.prototype.onAppHostResponse_ =
116     function(xhrResponse) {
117   if (xhrResponse.status == 200) {
118     var response = /** @type {remoting.AppHostResponse} */
119         (base.jsonParseSafe(xhrResponse.getText()));
120     if (response &&
121         response.status &&
122         response.status == 'done' &&
123         response.hostJid &&
124         response.authorizationCode &&
125         response.sharedSecret &&
126         response.host &&
127         response.host.hostId) {
128       var hostJid = response.hostJid;
129       var host = new remoting.Host(response.host.hostId);
130       host.jabberId = hostJid;
131       host.authorizationCode = response.authorizationCode;
132       host.sharedSecret = response.sharedSecret;
134       remoting.setMode(remoting.AppMode.CLIENT_CONNECTING);
136       /**
137        * @param {string} tokenUrl Token-issue URL received from the host.
138        * @param {string} hostPublicKey Host public key (DER and Base64
139        *     encoded).
140        * @param {string} scope OAuth scope to request the token for.
141        * @param {function(string, string):void} onThirdPartyTokenFetched
142        *     Callback.
143        */
144       var fetchThirdPartyToken = function(
145           tokenUrl, hostPublicKey, scope, onThirdPartyTokenFetched) {
146         // Use the authentication tokens returned by the app-remoting server.
147         onThirdPartyTokenFetched(host['authorizationCode'],
148                                  host['sharedSecret']);
149       };
151       var credentialsProvider = new remoting.CredentialsProvider(
152               {fetchThirdPartyToken: fetchThirdPartyToken});
153       var that = this;
154       var logger = remoting.SessionLogger.createForClient();
156       this.sessionFactory_.createSession(this, logger).then(
157         function(/** remoting.ClientSession */ session) {
158           that.session_ = session;
159           logger.setLogEntryMode(remoting.ChromotingEvent.Mode.LGAPP);
160           session.connect(host, credentialsProvider);
161       });
162     } else if (response && response.status == 'pending') {
163       this.onConnectionFailed(new remoting.Error(
164           remoting.Error.Tag.SERVICE_UNAVAILABLE));
165     }
166   } else {
167     console.error('Invalid "runApplication" response from server.');
168     // The orchestrator returns 403 if the user is not whitelisted to run the
169     // app, which gets translated to a generic error message, so pick something
170     // a bit more user-friendly.
171     var error = xhrResponse.status == 403 ?
172         new remoting.Error(remoting.Error.Tag.APP_NOT_AUTHORIZED) :
173         remoting.Error.fromHttpStatus(xhrResponse.status);
174     this.onConnectionFailed(error);
175   }
179  * @param {remoting.ConnectionInfo} connectionInfo
180  */
181 remoting.AppRemotingActivity.prototype.onConnected = function(connectionInfo) {
182   var connectedView = new remoting.AppConnectedView(
183       document.getElementById('client-container'),
184       this.windowShape_, connectionInfo, this.windowMessageDispatcher_);
186   var idleDetector = new remoting.IdleDetector(
187       document.getElementById('idle-dialog'),
188       this.windowShape_,
189       this.app_.getApplicationName(),
190       this.stop.bind(this));
192   // Map Cmd to Ctrl on Mac since hosts typically use Ctrl for keyboard
193   // shortcuts, but we want them to act as natively as possible.
194   if (remoting.platformIsMac()) {
195     connectionInfo.plugin().setRemapKeys({
196       0x0700e3: 0x0700e0,
197       0x0700e7: 0x0700e4
198     });
199   }
201   // Drop the session after 30s of suspension as we cannot recover from a
202   // connectivity loss longer than 30s anyways.
203   this.session_.dropSessionOnSuspend(30 * 1000);
204   this.connectedDisposables_ =
205       new base.Disposables(idleDetector, connectedView);
209  * @param {remoting.Error} error
210  */
211 remoting.AppRemotingActivity.prototype.onDisconnected = function(error) {
212   if (error.isNone()) {
213     this.app_.quit();
214   } else {
215     this.onConnectionDropped_();
216   }
220  * @param {!remoting.Error} error
221  */
222 remoting.AppRemotingActivity.prototype.onConnectionFailed = function(error) {
223   remoting.LoadingWindow.close();
224   this.showErrorMessage_(error);
225   this.cleanup_();
228 /** @private */
229 remoting.AppRemotingActivity.prototype.onConnectionDropped_ = function() {
230   // Don't dispose the session here to keep the plugin alive so that we can show
231   // the last frame of the remote application window.
232   base.dispose(this.connectedDisposables_);
233   this.connectedDisposables_ = null;
235   if (base.isOnline()) {
236     this.reconnect_();
237     return;
238   }
240   var rootElement = /** @type {HTMLDialogElement} */ (
241       document.getElementById('connection-dropped-dialog'));
242   var dialog =
243       new remoting.ConnectionDroppedDialog(rootElement, this.windowShape_);
244   var that = this;
245   dialog.show().then(function(){
246     dialog.dispose();
247     that.reconnect_();
248   }).catch(function(){
249     dialog.dispose();
250     that.app_.quit();
251   });
254 /** @private */
255 remoting.AppRemotingActivity.prototype.reconnect_ = function() {
256   // Hide the windows of the remote application with setDesktopRects([])
257   // before tearing down the plugin.
258   this.windowShape_.setDesktopRects([]);
259   this.cleanup_();
260   this.start();
264  * @param {!remoting.Error} error The error to be localized and displayed.
265  * @private
266  */
267 remoting.AppRemotingActivity.prototype.showErrorMessage_ = function(error) {
268   console.error('Connection failed: ' + error.toString());
269   remoting.MessageWindow.showErrorMessage(
270       this.app_.getApplicationName(),
271       chrome.i18n.getMessage(error.getTag()));
274 })();