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 || {};
9 * Type definition for the RunApplicationResponse returned by the API.
13 * authorizationCode: string,
14 * sharedSecret: string,
16 * applicationId: string,
21 remoting.AppHostResponse;
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
35 * @implements {remoting.Activity}
37 remoting.AppRemotingActivity = function(appCapabilities, app, windowShape,
39 windowMessageDispatcher) {
41 this.sessionFactory_ = new remoting.ClientSessionFactory(
42 document.querySelector('#client-container .client-plugin-container'),
45 /** @private {remoting.ClientSession} */
48 /** @private {base.Disposables} */
49 this.connectedDisposables_ = null;
55 this.windowShape_ = windowShape;
58 this.subscriptionToken_ = subscriptionToken;
60 /** @private {base.WindowMessageDispatcher} */
61 this.windowMessageDispatcher_ = windowMessageDispatcher;
64 remoting.AppRemotingActivity.prototype.dispose = function() {
66 remoting.LoadingWindow.close();
69 remoting.AppRemotingActivity.prototype.start = function() {
70 remoting.LoadingWindow.show();
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);
81 remoting.AppRemotingActivity.prototype.stop = function() {
83 this.session_.disconnect(remoting.Error.none());
88 remoting.AppRemotingActivity.prototype.cleanup_ = function() {
89 base.dispose(this.connectedDisposables_);
90 this.connectedDisposables_ = null;
91 base.dispose(this.session_);
96 * @param {string} token
97 * @return {Promise<!remoting.Xhr.Response>}
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({
112 * @param {!remoting.Xhr.Response} xhrResponse
115 remoting.AppRemotingActivity.prototype.onAppHostResponse_ =
116 function(xhrResponse) {
117 if (xhrResponse.status == 200) {
118 var response = /** @type {remoting.AppHostResponse} */
119 (base.jsonParseSafe(xhrResponse.getText()));
122 response.status == 'done' &&
124 response.authorizationCode &&
125 response.sharedSecret &&
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);
137 * @param {string} tokenUrl Token-issue URL received from the host.
138 * @param {string} hostPublicKey Host public key (DER and Base64
140 * @param {string} scope OAuth scope to request the token for.
141 * @param {function(string, string):void} onThirdPartyTokenFetched
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']);
151 var credentialsProvider = new remoting.CredentialsProvider(
152 {fetchThirdPartyToken: fetchThirdPartyToken});
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);
162 } else if (response && response.status == 'pending') {
163 this.onConnectionFailed(new remoting.Error(
164 remoting.Error.Tag.SERVICE_UNAVAILABLE));
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);
179 * @param {remoting.ConnectionInfo} connectionInfo
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'),
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({
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
211 remoting.AppRemotingActivity.prototype.onDisconnected = function(error) {
212 if (error.isNone()) {
215 this.onConnectionDropped_();
220 * @param {!remoting.Error} error
222 remoting.AppRemotingActivity.prototype.onConnectionFailed = function(error) {
223 remoting.LoadingWindow.close();
224 this.showErrorMessage_(error);
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()) {
240 var rootElement = /** @type {HTMLDialogElement} */ (
241 document.getElementById('connection-dropped-dialog'));
243 new remoting.ConnectionDroppedDialog(rootElement, this.windowShape_);
245 dialog.show().then(function(){
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([]);
264 * @param {!remoting.Error} error The error to be localized and displayed.
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()));