Update SplitString calls to new form
[chromium-blink-merge.git] / remoting / webapp / app_remoting / js / app_remoting_activity.js
blobd54a5c7641de55174e21c70cf82020dd1d162022
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 * }}
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
34 * @constructor
35 * @implements {remoting.Activity}
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());
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
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
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);
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.
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});
153 var that = this;
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));
167 } else {
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'),
189 this.windowShape_,
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({
197 0x0700e3: 0x0700e0,
198 0x0700e7: 0x0700e4
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()) {
214 this.app_.quit();
215 } else {
216 this.onConnectionDropped_();
221 * @param {!remoting.Error} error
223 remoting.AppRemotingActivity.prototype.onConnectionFailed = function(error) {
224 remoting.LoadingWindow.close();
225 this.showErrorMessage_(error);
226 this.cleanup_();
229 /** @private */
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()) {
237 this.reconnect_();
238 return;
241 var rootElement = /** @type {HTMLDialogElement} */ (
242 document.getElementById('connection-dropped-dialog'));
243 var dialog =
244 new remoting.ConnectionDroppedDialog(rootElement, this.windowShape_);
245 var that = this;
246 dialog.show().then(function(){
247 dialog.dispose();
248 that.reconnect_();
249 }).catch(function(){
250 dialog.dispose();
251 that.app_.quit();
255 /** @private */
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([]);
260 this.cleanup_();
261 this.start();
265 * @param {!remoting.Error} error The error to be localized and displayed.
266 * @private
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()));
275 })();