summaryrefslogtreecommitdiffstats
path: root/remoting/webapp/app_remoting/js/app_remoting.js
blob: c0630c792a5ba6dc9773c9a35f16212f0a2697bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview
 * This class implements the functionality that is specific to application
 * remoting ("AppRemoting" or AR).
 */

'use strict';

/** @suppress {duplicate} */
var remoting = remoting || {};

/**
 * @param {Array<string>} appCapabilities Array of application capabilities.
 * @constructor
 * @implements {remoting.ApplicationInterface}
 * @implements {remoting.ProtocolExtension}
 * @extends {remoting.Application}
 */
remoting.AppRemoting = function(appCapabilities) {
  base.inherits(this, remoting.Application, appCapabilities);

  /** @private {remoting.ApplicationContextMenu} */
  this.contextMenu_ = null;

  /** @private {remoting.KeyboardLayoutsMenu} */
  this.keyboardLayoutsMenu_ = null;

  /** @private {remoting.WindowActivationMenu} */
  this.windowActivationMenu_ = null;

  /** @private {remoting.AppConnectedView} */
  this.connectedView_ = null;
};

/**
 * Type definition for the RunApplicationResponse returned by the API.
 *
 * @constructor
 * @private
 */
remoting.AppRemoting.AppHostResponse = function() {
  /** @type {string} */
  this.status = '';
  /** @type {string} */
  this.hostJid = '';
  /** @type {string} */
  this.authorizationCode = '';
  /** @type {string} */
  this.sharedSecret = '';

  this.host = {
    /** @type {string} */
    applicationId: '',

    /** @type {string} */
    hostId: ''};
};

/**
 * @return {string} Application product name to be used in UI.
 * @override {remoting.ApplicationInterface}
 */
remoting.AppRemoting.prototype.getApplicationName = function() {
  var manifest = chrome.runtime.getManifest();
  return manifest.name;
};

/**
 * @param {!remoting.Error} error The failure reason.
 * @override {remoting.ApplicationInterface}
 */
remoting.AppRemoting.prototype.signInFailed_ = function(error) {
  this.onError_(error);
};

/**
 * @override {remoting.ApplicationInterface}
 */
remoting.AppRemoting.prototype.initApplication_ = function() {
  // TODO(jamiewalch): Remove ClientSession's dependency on remoting.fullscreen
  // so that this is no longer required.
  remoting.fullscreen = new remoting.FullscreenAppsV2();

  var restoreHostWindows = function() {
    if (remoting.clientSession) {
      remoting.clientSession.sendClientMessage('restoreAllWindows', '');
    }
  };
  chrome.app.window.current().onRestored.addListener(restoreHostWindows);

  remoting.windowShape.updateClientWindowShape();

  // Initialize the context menus.
  if (remoting.platformIsChromeOS()) {
    var adapter = new remoting.ContextMenuChrome();
  } else {
    var adapter = new remoting.ContextMenuDom(
        document.getElementById('context-menu'));
  }
  this.contextMenu_ = new remoting.ApplicationContextMenu(adapter);
  this.keyboardLayoutsMenu_ = new remoting.KeyboardLayoutsMenu(adapter);
  this.windowActivationMenu_ = new remoting.WindowActivationMenu(adapter);
};

/**
 * @param {string} token An OAuth access token.
 * @override {remoting.ApplicationInterface}
 */
remoting.AppRemoting.prototype.startApplication_ = function(token) {
  remoting.LoadingWindow.show();

  /** @type {remoting.AppRemoting} */
  var that = this;

  /** @param {!remoting.Xhr.Response} xhrResponse */
  var parseAppHostResponse = function(xhrResponse) {
    if (xhrResponse.status == 200) {
      var response = /** @type {remoting.AppRemoting.AppHostResponse} */
          (base.jsonParseSafe(xhrResponse.getText()));
      if (response &&
          response.status &&
          response.status == 'done' &&
          response.hostJid &&
          response.authorizationCode &&
          response.sharedSecret &&
          response.host &&
          response.host.hostId) {
        var hostJid = response.hostJid;
        that.contextMenu_.setHostId(response.host.hostId);
        var host = new remoting.Host(response.host.hostId);
        host.jabberId = hostJid;
        host.authorizationCode = response.authorizationCode;
        host.sharedSecret = response.sharedSecret;

        remoting.setMode(remoting.AppMode.CLIENT_CONNECTING);

        var idleDetector = new remoting.IdleDetector(
            document.getElementById('idle-dialog'),
            remoting.app.disconnect.bind(remoting.app));

        /**
         * @param {string} tokenUrl Token-issue URL received from the host.
         * @param {string} hostPublicKey Host public key (DER and Base64
         *     encoded).
         * @param {string} scope OAuth scope to request the token for.
         * @param {function(string, string):void} onThirdPartyTokenFetched
         *     Callback.
         */
        var fetchThirdPartyToken = function(
            tokenUrl, hostPublicKey, scope, onThirdPartyTokenFetched) {
          // Use the authentication tokens returned by the app-remoting server.
          onThirdPartyTokenFetched(host['authorizationCode'],
                                   host['sharedSecret']);
        };

        that.sessionConnector_.connectMe2App(host, fetchThirdPartyToken);
      } else if (response && response.status == 'pending') {
        that.onError_(new remoting.Error(
            remoting.Error.Tag.SERVICE_UNAVAILABLE));
      }
    } else {
      console.error('Invalid "runApplication" response from server.');
      // TODO(garykac) Start using remoting.Error.fromHttpStatus once it has
      // been updated to properly report 'unknown' errors (rather than
      // reporting them as AUTHENTICATION_FAILED).
      if (xhrResponse.status == 0) {
        that.onError_(new remoting.Error(
            remoting.Error.Tag.NETWORK_FAILURE));
      } else if (xhrResponse.status == 401) {
        that.onError_(new remoting.Error(
            remoting.Error.Tag.AUTHENTICATION_FAILED));
      } else if (xhrResponse.status == 403) {
        that.onError_(new remoting.Error(
            remoting.Error.Tag.APP_NOT_AUTHORIZED));
      } else if (xhrResponse.status == 502 || xhrResponse.status == 503) {
        that.onError_(new remoting.Error(
            remoting.Error.Tag.SERVICE_UNAVAILABLE));
      } else {
        that.onError_(remoting.Error.unexpected());
      }
    }
  };

  new remoting.Xhr({
    method: 'POST',
    url: that.runApplicationUrl_(),
    oauthToken: token
  }).start().then(parseAppHostResponse);
};

/**
 * @override {remoting.ApplicationInterface}
 */
remoting.AppRemoting.prototype.exitApplication_ = function() {
  remoting.LoadingWindow.close();
  this.closeMainWindow_();
};

/**
 * @param {remoting.ConnectionInfo} connectionInfo
 * @override {remoting.ApplicationInterface}
 */
remoting.AppRemoting.prototype.onConnected_ = function(connectionInfo) {
  this.initSession_(connectionInfo);

  remoting.identity.getUserInfo().then(
      function(userInfo) {
        remoting.clientSession.sendClientMessage(
            'setUserDisplayInfo',
            JSON.stringify({fullName: userInfo.name}));
      });

  connectionInfo.plugin().extensions().register(this);

  this.connectedView_ = new remoting.AppConnectedView(
      document.getElementById('client-container'), connectionInfo);

  // Map Cmd to Ctrl on Mac since hosts typically use Ctrl for keyboard
  // shortcuts, but we want them to act as natively as possible.
  if (remoting.platformIsMac()) {
    connectionInfo.plugin().setRemapKeys('0x0700e3>0x0700e0,0x0700e7>0x0700e4');
  }
};

/**
 * @override {remoting.ApplicationInterface}
 */
remoting.AppRemoting.prototype.onDisconnected_ = function() {
  base.dispose(this.connectedView_);
  this.connectedView_ = null;

  chrome.app.window.current().close();
};

/**
 * @param {!remoting.Error} error
 * @override {remoting.ApplicationInterface}
 */
remoting.AppRemoting.prototype.onConnectionFailed_ = function(error) {
  this.onError_(error);
};

/**
 * @param {!remoting.Error} error The error to be localized and displayed.
 * @override {remoting.ApplicationInterface}
 */
remoting.AppRemoting.prototype.onError_ = function(error) {
  console.error('Connection failed: ' + error.toString());
  remoting.LoadingWindow.close();
  remoting.MessageWindow.showErrorMessage(
      chrome.i18n.getMessage(/*i18n-content*/'CONNECTION_FAILED'),
      chrome.i18n.getMessage(error.getTag()));
};


/**
 * @return {Array<string>}
 * @override {remoting.ProtocolExtension}
 */
remoting.AppRemoting.prototype.getExtensionTypes = function() {
  return ['openURL', 'onWindowRemoved', 'onWindowAdded',
          'onAllWindowsMinimized', 'setKeyboardLayouts', 'pingResponse'];
};

/**
 * @param {function(string,string)} sendMessageToHost Callback to send a message
 *     to the host.
 * @override {remoting.ProtocolExtension}
 */
remoting.AppRemoting.prototype.startExtension = function(sendMessageToHost) {
};

/**
 * @param {string} type The message type.
 * @param {Object} message The parsed extension message data.
 * @override {remoting.ProtocolExtension}
 */
remoting.AppRemoting.prototype.onExtensionMessage = function(type, message) {
  switch (type) {

    case 'openURL':
      // URL requests from the hosted app are untrusted, so disallow anything
      // other than HTTP or HTTPS.
      var url = base.getStringAttr(message, 'url');
      if (url.indexOf('http:') != 0 && url.indexOf('https:') != 0) {
        console.error('Bad URL: ' + url);
      } else {
        window.open(url);
      }
      return true;

    case 'onWindowRemoved':
      var id = base.getNumberAttr(message, 'id');
      this.windowActivationMenu_.remove(id);
      return true;

    case 'onWindowAdded':
      var id = base.getNumberAttr(message, 'id');
      var title = base.getStringAttr(message, 'title');
      this.windowActivationMenu_.add(id, title);
      return true;

    case 'onAllWindowsMinimized':
      chrome.app.window.current().minimize();
      return true;

    case 'setKeyboardLayouts':
      var supportedLayouts = base.getArrayAttr(message, 'supportedLayouts');
      var currentLayout = base.getStringAttr(message, 'currentLayout');
      console.log('Current host keyboard layout: ' + currentLayout);
      console.log('Supported host keyboard layouts: ' + supportedLayouts);
      this.keyboardLayoutsMenu_.setLayouts(supportedLayouts, currentLayout);
      return true;

    case 'pingResponse':
      var then = base.getNumberAttr(message, 'timestamp');
      var now = new Date().getTime();
      this.contextMenu_.updateConnectionRTT(now - then);
      return true;
  }

  return false;
};

/**
 * @return {string}
 * @private
 */
remoting.AppRemoting.prototype.runApplicationUrl_ = function() {
  return remoting.settings.APP_REMOTING_API_BASE_URL + '/applications/' +
      remoting.settings.getAppRemotingApplicationId() + '/run';
};