summaryrefslogtreecommitdiffstats
path: root/remoting/webapp/crd/js/desktop_remoting.js
blob: 10e35c5437115d8287fb776eef0c7bbde13a54ce (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
337
338
339
340
341
342
343
344
// 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 desktop
 * remoting ("Chromoting" or CRD).
 */

'use strict';

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

/**
 * @param {remoting.Application} app The main app that owns this delegate.
 * @constructor
 * @implements {remoting.Application.Delegate}
 */
remoting.DesktopRemoting = function(app) {
  /**
   * TODO(garykac): Remove this reference to the Application. It's only
   * needed to get the current mode when reporting errors. So we should be
   * able to refactor and remove this reference cycle.
   *
   * @private {remoting.Application}
   */
  this.app_ = app;
  app.setDelegate(this);

  /**
   * Whether to refresh the JID and retry the connection if the current JID
   * is offline.
   *
   * @private {boolean}
   */
  this.refreshHostJidIfOffline_ = true;
};

/**
 * Initialize the application and register all event handlers. After this
 * is called, the app is running and waiting for user events.
 *
 * @return {void} Nothing.
 */
remoting.DesktopRemoting.prototype.init = function() {
  remoting.initElementEventHandlers();

  if (base.isAppsV2()) {
    remoting.windowFrame = new remoting.WindowFrame(
        document.getElementById('title-bar'));
    remoting.optionsMenu = remoting.windowFrame.createOptionsMenu();

    var START_FULLSCREEN = 'start-fullscreen';
    remoting.fullscreen = new remoting.FullscreenAppsV2();
    remoting.fullscreen.addListener(function(isFullscreen) {
      chrome.storage.local.set({START_FULLSCREEN: isFullscreen});
    });
    // TODO(jamiewalch): This should be handled by the background page when the
    // window is created, but due to crbug.com/51587 it needs to be done here.
    // Remove this hack once that bug is fixed.
    chrome.storage.local.get(
        START_FULLSCREEN,
        /** @param {Object} values */
        function(values) {
          if (values[START_FULLSCREEN]) {
            remoting.fullscreen.activate(true);
          }
        }
    );

  } else {
    remoting.fullscreen = new remoting.FullscreenAppsV1();
    remoting.toolbar = new remoting.Toolbar(
        document.getElementById('session-toolbar'));
    remoting.optionsMenu = remoting.toolbar.createOptionsMenu();

    window.addEventListener('beforeunload', remoting.promptClose, false);
    window.addEventListener('unload',
                            remoting.app.disconnect.bind(remoting.app), false);
  }

  remoting.initHostlist_();

  var homeFeedback = new remoting.MenuButton(
      document.getElementById('help-feedback-main'));
  var toolbarFeedback = new remoting.MenuButton(
      document.getElementById('help-feedback-toolbar'));
  remoting.manageHelpAndFeedback(
      document.getElementById('title-bar'));
  remoting.manageHelpAndFeedback(
      document.getElementById('help-feedback-toolbar'));
  remoting.manageHelpAndFeedback(
      document.getElementById('help-feedback-main'));

  remoting.windowShape.updateClientWindowShape();

  remoting.showOrHideIT2MeUi();
  remoting.showOrHideMe2MeUi();

  // For Apps v1, check the tab type to warn the user if they are not getting
  // the best keyboard experience.
  if (!base.isAppsV2() && !remoting.platformIsMac()) {
    /** @param {boolean} isWindowed */
    var onIsWindowed = function(isWindowed) {
      if (!isWindowed) {
        document.getElementById('startup-mode-box-me2me').hidden = false;
        document.getElementById('startup-mode-box-it2me').hidden = false;
      }
    };
    isWindowed_(onIsWindowed);
  }

  remoting.ClientPlugin.factory.preloadPlugin();
}

/**
 * Start the application. Once start() is called, the delegate can assume that
 * the user has consented to all permissions specified in the manifest.
 *
 * @param {remoting.SessionConnector} connector
 * @param {string} token An OAuth access token. The delegate should not cache
 *     this token, but can assume that it will remain valid during application
 *     start-up.
 */
remoting.DesktopRemoting.prototype.start = function(connector, token) {
  remoting.identity.getEmail().then(
      function(/** string */ email) {
        document.getElementById('current-email').innerText = email;
        document.getElementById('get-started-it2me').disabled = false;
        document.getElementById('get-started-me2me').disabled = false;
      });
};

/**
 * Report an authentication error to the user. This is called in lieu of start()
 * if the user cannot be authenticated or if they decline the app permissions.
 *
 * @param {!remoting.Error} error The failure reason.
 */
remoting.DesktopRemoting.prototype.signInFailed = function(error) {
  remoting.showErrorMessage(error);
};

/**
 * @return {string} Application product name to be used in UI.
 */
remoting.DesktopRemoting.prototype.getApplicationName = function() {
  return chrome.i18n.getMessage(/*i18n-content*/'PRODUCT_NAME');
};

/**
 * @return {string} The default remap keys for the current platform.
 */
remoting.DesktopRemoting.prototype.getDefaultRemapKeys = function() {
  var remapKeys = '';
  // By default, under ChromeOS, remap the right Control key to the right
  // Win / Cmd key.
  if (remoting.platformIsChromeOS()) {
    remapKeys = '0x0700e4>0x0700e7';
  }
  return remapKeys;
};

/**
 * Called when a new session has been connected.
 *
 * @param {remoting.ClientSession} clientSession
 * @return {void} Nothing.
 */
remoting.DesktopRemoting.prototype.handleConnected = function(clientSession) {
  // Set the text on the buttons shown under the error message so that they are
  // easy to understand in the case where a successful connection failed, as
  // opposed to the case where a connection never succeeded.
  // TODO(garykac): Investigate to see if these need to be reverted to their
  // original values in the onDisconnected method.
  var button1 = document.getElementById('client-reconnect-button');
  l10n.localizeElementFromTag(button1, /*i18n-content*/'RECONNECT');
  button1.removeAttribute('autofocus');
  var button2 = document.getElementById('client-finished-me2me-button');
  l10n.localizeElementFromTag(button2, /*i18n-content*/'OK');
  button2.setAttribute('autofocus', 'autofocus');

  // Reset the refresh flag so that the next connection will retry if needed.
  this.refreshHostJidIfOffline_ = true;

  document.getElementById('access-code-entry').value = '';
  remoting.setMode(remoting.AppMode.IN_SESSION);
  if (!base.isAppsV2()) {
    remoting.toolbar.center();
    remoting.toolbar.preview();
  }

  if (remoting.desktopConnectedView.getMode() ==
        remoting.DesktopConnectedView.Mode.ME2ME) {
    var sessionConnector = remoting.app.getSessionConnector();
    if (remoting.app.hasCapability(remoting.ClientSession.Capability.CAST)) {
      sessionConnector.registerProtocolExtension(
          new remoting.CastExtensionHandler());
    }
    sessionConnector.registerProtocolExtension(
        new remoting.GnubbyAuthHandler());
  }

  if (remoting.pairingRequested) {
    /**
     * @param {string} clientId
     * @param {string} sharedSecret
     */
    var onPairingComplete = function(clientId, sharedSecret) {
      var pairingInfo = {
        pairingInfo: {
          clientId: clientId,
          sharedSecret: sharedSecret
        }
      };
      var connector = remoting.app.getSessionConnector();
      remoting.HostSettings.save(connector.getHostId(), pairingInfo);
      connector.updatePairingInfo(clientId, sharedSecret);
    };
    // Use the platform name as a proxy for the local computer name.
    // TODO(jamiewalch): Use a descriptive name for the local computer, for
    // example, its Chrome Sync name.
    var clientName = '';
    if (remoting.platformIsMac()) {
      clientName = 'Mac';
    } else if (remoting.platformIsWindows()) {
      clientName = 'Windows';
    } else if (remoting.platformIsChromeOS()) {
      clientName = 'ChromeOS';
    } else if (remoting.platformIsLinux()) {
      clientName = 'Linux';
    } else {
      console.log('Unrecognized client platform. Using navigator.platform.');
      clientName = navigator.platform;
    }
    clientSession.requestPairing(clientName, onPairingComplete);
  }
};

/**
 * Called when the current session has been disconnected.
 *
 * @return {void} Nothing.
 */
remoting.DesktopRemoting.prototype.handleDisconnected = function() {
  if (remoting.desktopConnectedView.getMode() ==
      remoting.DesktopConnectedView.Mode.IT2ME) {
    remoting.setMode(remoting.AppMode.CLIENT_SESSION_FINISHED_IT2ME);
  } else {
    remoting.setMode(remoting.AppMode.CLIENT_SESSION_FINISHED_ME2ME);
  }
};

/**
 * Called when the current session's connection has failed.
 *
 * @param {remoting.SessionConnector} connector
 * @param {!remoting.Error} error
 * @return {void} Nothing.
 */
remoting.DesktopRemoting.prototype.handleConnectionFailed = function(
    connector, error) {
  /** @type {remoting.DesktopRemoting} */
  var that = this;

  /** @param {boolean} success */
  var onHostListRefresh = function(success) {
    if (success) {
      var host = remoting.hostList.getHostForId(connector.getHostId());
      if (host) {
        connector.retryConnectMe2Me(host);
        return;
      }
    }
    that.handleError(error);
  };

  if (error.tag == remoting.Error.Tag.HOST_IS_OFFLINE &&
      that.refreshHostJidIfOffline_) {
    that.refreshHostJidIfOffline_ = false;
    // The plugin will be re-created when the host finished refreshing
    remoting.hostList.refresh(onHostListRefresh);
  } else {
    this.handleError(error);
  }
};

/**
 * Called when the current session has reached the point where the host has
 * started streaming video frames to the client.
 *
 * @return {void} Nothing.
 */
remoting.DesktopRemoting.prototype.handleVideoStreamingStarted = function() {
};

/**
 * @param {string} type The type of the extension message.
 * @param {Object} message The parsed extension message data.
 * @return {boolean} Return true if the extension message was recognized.
 */
remoting.DesktopRemoting.prototype.handleExtensionMessage = function(
    type, message) {
  return false;
};

/**
 * Called when an error needs to be displayed to the user.
 *
 * @param {!remoting.Error} error The error to be localized and displayed.
 * @return {void} Nothing.
 */
remoting.DesktopRemoting.prototype.handleError = function(error) {
  console.error('Connection failed:' + error.tag);
  remoting.accessCode = '';

  if (error.tag === remoting.Error.Tag.AUTHENTICATION_FAILED) {
    remoting.setMode(remoting.AppMode.HOME);
    remoting.handleAuthFailureAndRelaunch();
    return;
  }

  // Reset the refresh flag so that the next connection will retry if needed.
  this.refreshHostJidIfOffline_ = true;

  var errorDiv = document.getElementById('connect-error-message');
  l10n.localizeElementFromTag(errorDiv, error.tag);

  var mode = remoting.clientSession ? remoting.desktopConnectedView.getMode()
      : this.app_.getSessionConnector().getConnectionMode();
  if (mode == remoting.DesktopConnectedView.Mode.IT2ME) {
    remoting.setMode(remoting.AppMode.CLIENT_CONNECT_FAILED_IT2ME);
  } else {
    remoting.setMode(remoting.AppMode.CLIENT_CONNECT_FAILED_ME2ME);
  }
};

/**
 * No cleanup required for desktop remoting.
 */
remoting.DesktopRemoting.prototype.handleExit = function() {
};