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
});
155 this.sessionFactory_
.createSession(this).then(
156 function(/** remoting.ClientSession */ session
) {
157 that
.session_
= session
;
158 session
.logHostOfflineErrors(true);
159 session
.getLogger().setLogEntryMode(
160 remoting
.ChromotingEvent
.Mode
.LGAPP
);
161 session
.connect(host
, credentialsProvider
);
163 } else if (response
&& response
.status
== 'pending') {
164 this.onConnectionFailed(new remoting
.Error(
165 remoting
.Error
.Tag
.SERVICE_UNAVAILABLE
));
168 console
.error('Invalid "runApplication" response from server.');
169 // The orchestrator returns 403 if the user is not whitelisted to run the
170 // app, which gets translated to a generic error message, so pick something
171 // a bit more user-friendly.
172 var error
= xhrResponse
.status
== 403 ?
173 new remoting
.Error(remoting
.Error
.Tag
.APP_NOT_AUTHORIZED
) :
174 remoting
.Error
.fromHttpStatus(xhrResponse
.status
);
175 this.onConnectionFailed(error
);
180 * @param {remoting.ConnectionInfo} connectionInfo
182 remoting
.AppRemotingActivity
.prototype.onConnected = function(connectionInfo
) {
183 var connectedView
= new remoting
.AppConnectedView(
184 document
.getElementById('client-container'),
185 this.windowShape_
, connectionInfo
, this.windowMessageDispatcher_
);
187 var idleDetector
= new remoting
.IdleDetector(
188 document
.getElementById('idle-dialog'),
190 this.app_
.getApplicationName(),
191 this.stop
.bind(this));
193 // Map Cmd to Ctrl on Mac since hosts typically use Ctrl for keyboard
194 // shortcuts, but we want them to act as natively as possible.
195 if (remoting
.platformIsMac()) {
196 connectionInfo
.plugin().setRemapKeys({
202 // Drop the session after 30s of suspension as we cannot recover from a
203 // connectivity loss longer than 30s anyways.
204 this.session_
.dropSessionOnSuspend(30 * 1000);
205 this.connectedDisposables_
=
206 new base
.Disposables(idleDetector
, connectedView
);
210 * @param {remoting.Error} error
212 remoting
.AppRemotingActivity
.prototype.onDisconnected = function(error
) {
213 if (error
.isNone()) {
216 this.onConnectionDropped_();
221 * @param {!remoting.Error} error
223 remoting
.AppRemotingActivity
.prototype.onConnectionFailed = function(error
) {
224 remoting
.LoadingWindow
.close();
225 this.showErrorMessage_(error
);
230 remoting
.AppRemotingActivity
.prototype.onConnectionDropped_ = function() {
231 // Don't dispose the session here to keep the plugin alive so that we can show
232 // the last frame of the remote application window.
233 base
.dispose(this.connectedDisposables_
);
234 this.connectedDisposables_
= null;
236 if (base
.isOnline()) {
241 var rootElement
= /** @type {HTMLDialogElement} */ (
242 document
.getElementById('connection-dropped-dialog'));
244 new remoting
.ConnectionDroppedDialog(rootElement
, this.windowShape_
);
246 dialog
.show().then(function(){
256 remoting
.AppRemotingActivity
.prototype.reconnect_ = function() {
257 // Hide the windows of the remote application with setDesktopRects([])
258 // before tearing down the plugin.
259 this.windowShape_
.setDesktopRects([]);
265 * @param {!remoting.Error} error The error to be localized and displayed.
268 remoting
.AppRemotingActivity
.prototype.showErrorMessage_ = function(error
) {
269 console
.error('Connection failed: ' + error
.toString());
270 remoting
.MessageWindow
.showErrorMessage(
271 this.app_
.getApplicationName(),
272 chrome
.i18n
.getMessage(error
.getTag()));