summaryrefslogtreecommitdiffstats
path: root/remoting/webapp/crd/js/host_screen.js
blob: b0b61b99b6030c361ac2a1fc438d00c97a3f79f2 (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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// Copyright (c) 2012 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
 * Functions related to the 'host screen' for Chromoting.
 */

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

(function(){

'use strict';

/** @type {remoting.HostSession} */
var hostSession_ = null;

/**
 * @type {boolean} Whether or not the last share was cancelled by the user.
 *     This controls what screen is shown when the host signals completion.
 */
var lastShareWasCancelled_ = false;

/**
 * @type {remoting.LogToServer} Logging instance for IT2Me host connection
 *     status.
 */
var it2meLogger = null;

/**
 * Start a host session. This is the main entry point for the host screen,
 * called directly from the onclick action of a button on the home screen.
 * It first verifies that the native host components are installed and asks
 * to install them if necessary.
 */
remoting.tryShare = function() {
  ensureIT2MeLogger_();
  it2meLogger.setSessionId();
  it2meLogger.logClientSessionStateChange(
      remoting.ClientSession.State.INITIALIZING, remoting.Error.none(), null);

  /** @type {remoting.It2MeHostFacade} */
  var hostFacade = new remoting.It2MeHostFacade();

  /** @type {remoting.HostInstallDialog} */
  var hostInstallDialog = null;

  var tryInitializeFacade = function() {
    hostFacade.initialize(onFacadeInitialized, onFacadeInitializationFailed);
  };

  var onFacadeInitialized = function () {
    // Host already installed.
    remoting.startHostUsingFacade_(hostFacade);
  };

  var onFacadeInitializationFailed = function() {
    // If we failed to initialize the dispatcher then prompt the user to install
    // the host manually.
    var hasHostDialog = (hostInstallDialog !== null);  /** jscompile hack */
    if (!hasHostDialog) {
      hostInstallDialog = new remoting.HostInstallDialog();
      hostInstallDialog.show(tryInitializeFacade, showShareError_);
    } else {
      hostInstallDialog.tryAgain();
    }
  };

  tryInitializeFacade();
};

/**
 * @param {remoting.It2MeHostFacade} hostFacade An initialized It2MeHostFacade.
 */
remoting.startHostUsingFacade_ = function(hostFacade) {
  console.log('Attempting to share...');
  it2meLogger.setHostVersion(hostFacade.getHostVersion());
  remoting.identity.getToken().then(
    remoting.tryShareWithToken_.bind(null, hostFacade),
    remoting.Error.handler(showShareError_));
};

/**
 * @param {remoting.It2MeHostFacade} hostFacade An initialized
 *     It2MeHostFacade.
 * @param {string} token The OAuth access token.
 * @private
 */
remoting.tryShareWithToken_ = function(hostFacade, token) {
  lastShareWasCancelled_ = false;
  onNatTraversalPolicyChanged_(true);  // Hide warning by default.
  remoting.setMode(remoting.AppMode.HOST_WAITING_FOR_CODE);
  it2meLogger.logClientSessionStateChange(
      remoting.ClientSession.State.CONNECTING, remoting.Error.none(), null);
  document.getElementById('cancel-share-button').disabled = false;
  disableTimeoutCountdown_();

  console.assert(hostSession_ === null, '|hostSession_| already exists.');
  hostSession_ = new remoting.HostSession();
  remoting.identity.getEmail().then(
      function(/** string */ email) {
        hostSession_.connect(
            hostFacade, email, token, onHostStateChanged_,
            onNatTraversalPolicyChanged_, logDebugInfo_, it2meConnectFailed_);
      });
};

/**
 * Callback for the host plugin to notify the web app of state changes.
 * @param {remoting.HostSession.State} state The new state of the plugin.
 * @return {void} Nothing.
 */
function onHostStateChanged_(state) {
  if (state == remoting.HostSession.State.STARTING) {
    // Nothing to do here.
    console.log('Host state: STARTING');

  } else if (state == remoting.HostSession.State.REQUESTED_ACCESS_CODE) {
    // Nothing to do here.
    console.log('Host state: REQUESTED_ACCESS_CODE');

  } else if (state == remoting.HostSession.State.RECEIVED_ACCESS_CODE) {
    console.log('Host state: RECEIVED_ACCESS_CODE');
    var accessCode = hostSession_.getAccessCode();
    var accessCodeDisplay = document.getElementById('access-code-display');
    accessCodeDisplay.innerText = '';
    // Display the access code in groups of four digits for readability.
    var kDigitsPerGroup = 4;
    for (var i = 0; i < accessCode.length; i += kDigitsPerGroup) {
      var nextFourDigits = document.createElement('span');
      nextFourDigits.className = 'access-code-digit-group';
      nextFourDigits.innerText = accessCode.substring(i, i + kDigitsPerGroup);
      accessCodeDisplay.appendChild(nextFourDigits);
    }
    accessCodeExpiresIn_ = hostSession_.getAccessCodeLifetime();
    if (accessCodeExpiresIn_ > 0) {  // Check it hasn't expired.
      accessCodeTimerId_ = setInterval(decrementAccessCodeTimeout_, 1000);
      timerRunning_ = true;
      updateAccessCodeTimeoutElement_();
      updateTimeoutStyles_();
      remoting.setMode(remoting.AppMode.HOST_WAITING_FOR_CONNECTION);
    } else {
      // This can only happen if the cloud tells us that the code lifetime is
      // <= 0s, which shouldn't happen so we don't care how clean this UX is.
      console.error('Access code already invalid on receipt!');
      remoting.cancelShare();
    }

  } else if (state == remoting.HostSession.State.CONNECTED) {
    console.log('Host state: CONNECTED');
    var element = document.getElementById('host-shared-message');
    var client = hostSession_.getClient();
    l10n.localizeElement(element, client);
    remoting.setMode(remoting.AppMode.HOST_SHARED);
    disableTimeoutCountdown_();

  } else if (state == remoting.HostSession.State.DISCONNECTING) {
    console.log('Host state: DISCONNECTING');

  } else if (state == remoting.HostSession.State.DISCONNECTED) {
    console.log('Host state: DISCONNECTED');
    if (remoting.currentMode != remoting.AppMode.HOST_SHARE_FAILED) {
      // If an error is being displayed, then the plugin should not be able to
      // hide it by setting the state. Errors must be dismissed by the user
      // clicking OK, which puts the app into mode HOME.
      if (lastShareWasCancelled_) {
        remoting.setMode(remoting.AppMode.HOME);
      } else {
        remoting.setMode(remoting.AppMode.HOST_SHARE_FINISHED);
      }
    }
    cleanUp();
  } else if (state == remoting.HostSession.State.ERROR) {
    // The processing of this message is identical to that of the "error"
    // message (see it2me_host_facade.js); it is included only to support
    // old native components that send errors as a host state message.
    // TODO(jamiewalch): Remove this once there are sufficiently few old
    //     installations deployed.
    console.error('Host state: ERROR');
    showShareError_(remoting.Error.unexpected());
  } else if (state == remoting.HostSession.State.INVALID_DOMAIN_ERROR) {
    console.error('Host state: INVALID_DOMAIN_ERROR');
    showShareError_(new remoting.Error(remoting.Error.Tag.INVALID_HOST_DOMAIN));
  } else {
    console.error('Unknown state -> ' + state);
  }
}

/**
 * This is the callback that the host plugin invokes to indicate that there
 * is additional debug log info to display.
 * @param {string} msg The message (which will not be localized) to be logged.
 */
function logDebugInfo_(msg) {
  console.log('plugin: ' + msg);
}

/**
 * Show a host-side error message.
 *
 * @param {!remoting.Error} error The error to be localized and displayed.
 * @return {void} Nothing.
 */
function showShareError_(error) {
  if (error.hasTag(remoting.Error.Tag.CANCELLED)) {
    remoting.setMode(remoting.AppMode.HOME);
    it2meLogger.logClientSessionStateChange(
        remoting.ClientSession.State.CONNECTION_CANCELED,
        remoting.Error.none(),
        null);
  } else {
    var errorDiv = document.getElementById('host-plugin-error');
    l10n.localizeElementFromTag(errorDiv, error.getTag());
    console.error('Sharing error: ' + error.toString());
    remoting.setMode(remoting.AppMode.HOST_SHARE_FAILED);
    it2meLogger.logClientSessionStateChange(
        remoting.ClientSession.State.FAILED, error, null);
  }

  cleanUp();
}

/**
 * Show a sharing error with error code UNEXPECTED .
 *
 * @return {void} Nothing.
 */
function it2meConnectFailed_() {
  showShareError_(remoting.Error.unexpected());
}

function cleanUp() {
  base.dispose(hostSession_);
  hostSession_ = null;
}

/**
 * Cancel an active or pending it2me share operation.
 *
 * @return {void} Nothing.
 */
remoting.cancelShare = function() {
  document.getElementById('cancel-share-button').disabled = true;
  console.log('Canceling share...');
  remoting.lastShareWasCancelled = true;
  try {
    hostSession_.disconnect();
    it2meLogger.logClientSessionStateChange(
        remoting.ClientSession.State.CONNECTION_CANCELED,
        remoting.Error.none(),
        null);
  } catch (/** @type {*} */ error) {
    console.error('Error disconnecting: ' + error +
                  '. The host probably crashed.');
    // TODO(jamiewalch): Clean this up. We should have a class representing
    // the host plugin, like we do for the client, which should handle crash
    // reporting and it should use a more detailed error message than the
    // default 'generic' one. See crbug.com/94624
    showShareError_(remoting.Error.unexpected());
  }
  disableTimeoutCountdown_();
};

/**
 * @type {boolean} Whether or not the access code timeout countdown is running.
 */
var timerRunning_ = false;

/**
 * @type {number} The id of the access code expiry countdown timer.
 */
var accessCodeTimerId_ = 0;

/**
 * @type {number} The number of seconds until the access code expires.
 */
var accessCodeExpiresIn_ = 0;

/**
 * The timer callback function
 * @return {void} Nothing.
 */
function decrementAccessCodeTimeout_() {
  --accessCodeExpiresIn_;
  updateAccessCodeTimeoutElement_();
}

/**
 * Stop the access code timeout countdown if it is running.
 * @return {void} Nothing.
 */
function disableTimeoutCountdown_() {
  if (timerRunning_) {
    clearInterval(accessCodeTimerId_);
    timerRunning_ = false;
    updateTimeoutStyles_();
  }
}

/**
 * Constants controlling the access code timer countdown display.
 */
var ACCESS_CODE_TIMER_DISPLAY_THRESHOLD_ = 30;
var ACCESS_CODE_RED_THRESHOLD_ = 10;

/**
 * Show/hide or restyle various elements, depending on the remaining countdown
 * and timer state.
 *
 * @return {boolean} True if the timeout is in progress, false if it has
 * expired.
 */
function updateTimeoutStyles_() {
  if (timerRunning_) {
    if (accessCodeExpiresIn_ <= 0) {
      remoting.cancelShare();
      return false;
    }
    var accessCode = document.getElementById('access-code-display');
    if (accessCodeExpiresIn_ <= ACCESS_CODE_RED_THRESHOLD_) {
      accessCode.classList.add('expiring');
    } else {
      accessCode.classList.remove('expiring');
    }
  }
  document.getElementById('access-code-countdown').hidden =
      (accessCodeExpiresIn_ > ACCESS_CODE_TIMER_DISPLAY_THRESHOLD_) ||
      !timerRunning_;
  return true;
}

/**
 * Update the text and appearance of the access code timeout element to
 * reflect the time remaining.
 * @return {void} Nothing.
 */
function updateAccessCodeTimeoutElement_() {
  var pad = (accessCodeExpiresIn_ < 10) ? '0:0' : '0:';
  l10n.localizeElement(document.getElementById('seconds-remaining'),
                       pad + accessCodeExpiresIn_);
  if (!updateTimeoutStyles_()) {
    disableTimeoutCountdown_();
  }
}

/**
 * Callback to show or hide the NAT traversal warning when the policy changes.
 * @param {boolean} enabled True if NAT traversal is enabled.
 * @return {void} Nothing.
 */
function onNatTraversalPolicyChanged_(enabled) {
  var natBox = document.getElementById('nat-box');
  if (enabled) {
    natBox.classList.add('traversal-enabled');
  } else {
    natBox.classList.remove('traversal-enabled');
  }
}

/**
 * Create an IT2Me LogToServer instance if one does not already exist.
 */
function ensureIT2MeLogger_() {
  if (it2meLogger) {
    return;
  }

  var xmppConnection = new remoting.XmppConnection();
  var tokenPromise = remoting.identity.getToken();
  var emailPromise = remoting.identity.getEmail();
  tokenPromise.then(function(/** string */ token) {
    emailPromise.then(function(/** string */ email) {
      xmppConnection.connect(remoting.settings.XMPP_SERVER, email, token);
    });
  });

  var bufferedSignalStrategy =
      new remoting.BufferedSignalStrategy(xmppConnection);
  it2meLogger = new remoting.LogToServer(bufferedSignalStrategy, true);
  it2meLogger.setLogEntryMode(remoting.ChromotingEvent.Mode.IT2ME);
}

})();