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
32 * @implements {remoting.Activity}
34 remoting.AppRemotingActivity = function(appCapabilities, app) {
36 this.sessionFactory_ = new remoting.ClientSessionFactory(
37 document.querySelector('#client-container .client-plugin-container'),
40 /** @private {remoting.ClientSession} */
43 /** @private {base.Disposables} */
44 this.connectedDisposables_ = null;
50 remoting.AppRemotingActivity.prototype.dispose = function() {
52 remoting.LoadingWindow.close();
55 remoting.AppRemotingActivity.prototype.start = function() {
56 remoting.LoadingWindow.show();
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);
65 remoting.AppRemotingActivity.prototype.stop = function() {
67 this.session_.disconnect(remoting.Error.none());
72 remoting.AppRemotingActivity.prototype.cleanup_ = function() {
73 base.dispose(this.connectedDisposables_);
74 this.connectedDisposables_ = null;
75 base.dispose(this.session_);
80 * @param {string} token
81 * @return {Promise<!remoting.Xhr.Response>}
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({
95 * @param {!remoting.Xhr.Response} xhrResponse
98 remoting.AppRemotingActivity.prototype.onAppHostResponse_ =
99 function(xhrResponse) {
100 if (xhrResponse.status == 200) {
101 var response = /** @type {remoting.AppHostResponse} */
102 (base.jsonParseSafe(xhrResponse.getText()));
105 response.status == 'done' &&
107 response.authorizationCode &&
108 response.sharedSecret &&
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);
120 * @param {string} tokenUrl Token-issue URL received from the host.
121 * @param {string} hostPublicKey Host public key (DER and Base64
123 * @param {string} scope OAuth scope to request the token for.
124 * @param {function(string, string):void} onThirdPartyTokenFetched
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']);
134 var credentialsProvider = new remoting.CredentialsProvider(
135 {fetchThirdPartyToken: fetchThirdPartyToken});
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);
146 } else if (response && response.status == 'pending') {
147 this.onConnectionFailed(new remoting.Error(
148 remoting.Error.Tag.SERVICE_UNAVAILABLE));
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);
163 * @param {remoting.ConnectionInfo} connectionInfo
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');
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
190 remoting.AppRemotingActivity.prototype.onDisconnected = function(error) {
191 if (error.isNone()) {
194 this.onConnectionDropped_();
199 * @param {!remoting.Error} error
201 remoting.AppRemotingActivity.prototype.onConnectionFailed = function(error) {
202 remoting.LoadingWindow.close();
203 this.showErrorMessage_(error);
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);
218 dialog.show().then(function(){
220 // Hide the windows of the remote application with setDesktopRects([])
221 // before tearing down the plugin.
222 remoting.windowShape.setDesktopRects([]);
227 chrome.app.window.current().close();
232 * @param {!remoting.Error} error The error to be localized and displayed.
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()));