-
Notifications
You must be signed in to change notification settings - Fork 8
/
client_session.js
842 lines (762 loc) · 27.4 KB
/
client_session.js
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
// 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
* Class handling creation and teardown of a remoting client session.
*
* The ClientSession class controls lifetime of the client plugin
* object and provides the plugin with the functionality it needs to
* establish connection. Specifically it:
* - Delivers incoming/outgoing signaling messages,
* - Adjusts plugin size and position when destop resolution changes,
*
* This class should not access the plugin directly, instead it should
* do it through ClientPlugin class which abstracts plugin version
* differences.
*/
'use strict';
/** @suppress {duplicate} */
var remoting = remoting || {};
/**
* Interval that determines how often the web-app should send a new access token
* to the host.
*
* @const
* @type {number}
*/
remoting.ACCESS_TOKEN_RESEND_INTERVAL_MS = 15 * 60 * 1000;
/**
* @param {remoting.Host} host The host to connect to.
* @param {remoting.SignalStrategy} signalStrategy Signal strategy.
* @param {HTMLElement} container Container element for the client view.
* @param {string} accessCode The IT2Me access code. Blank for Me2Me.
* @param {function(boolean, function(string): void): void} fetchPin
* Called by Me2Me connections when a PIN needs to be obtained
* interactively.
* @param {function(string, string, string,
* function(string, string): void): void}
* fetchThirdPartyToken Called by Me2Me connections when a third party
* authentication token must be obtained.
* @param {string} authenticationMethods Comma-separated list of
* authentication methods the client should attempt to use.
* @param {remoting.DesktopConnectedView.Mode} mode The mode of this connection.
* @param {string} clientPairingId For paired Me2Me connections, the
* pairing id for this client, as issued by the host.
* @param {string} clientPairedSecret For paired Me2Me connections, the
* paired secret for this client, as issued by the host.
* @param {string} defaultRemapKeys The default set of remap keys, to use
* when the client doesn't define any.
* @constructor
* @extends {base.EventSourceImpl}
*/
remoting.ClientSession = function(host, signalStrategy, container, accessCode,
fetchPin, fetchThirdPartyToken,
authenticationMethods, mode, clientPairingId,
clientPairedSecret, defaultRemapKeys) {
/** @private */
this.state_ = remoting.ClientSession.State.CREATED;
/** @private */
this.error_ = remoting.Error.NONE;
/** @private */
this.host_ = host;
/** @private */
this.accessCode_ = accessCode;
/** @private */
this.fetchPin_ = fetchPin;
/** @private */
this.fetchThirdPartyToken_ = fetchThirdPartyToken;
/** @private */
this.authenticationMethods_ = authenticationMethods;
/** @private */
this.clientPairingId_ = clientPairingId;
/** @private */
this.clientPairedSecret_ = clientPairedSecret;
/** @private */
this.uiHandler_ = new remoting.DesktopConnectedView(
this, container, this.host_, mode, defaultRemapKeys,
this.onPluginInitialized_.bind(this));
remoting.desktopConnectedView = this.uiHandler_;
/** @private */
this.sessionId_ = '';
/** @type {remoting.ClientPlugin}
* @private */
this.plugin_ = null;
/** @private */
this.hasReceivedFrame_ = false;
this.logToServer = new remoting.LogToServer(signalStrategy, mode);
/** @private */
this.signalStrategy_ = signalStrategy;
base.debug.assert(this.signalStrategy_.getState() ==
remoting.SignalStrategy.State.CONNECTED);
this.signalStrategy_.setIncomingStanzaCallback(
this.onIncomingMessage_.bind(this));
remoting.formatIq.setJids(this.signalStrategy_.getJid(), host.jabberId);
/**
* Allow host-offline error reporting to be suppressed in situations where it
* would not be useful, for example, when using a cached host JID.
*
* @type {boolean} @private
*/
this.logHostOfflineErrors_ = true;
/** @type {remoting.GnubbyAuthHandler} @private */
this.gnubbyAuthHandler_ = null;
/** @type {remoting.CastExtensionHandler} @private */
this.castExtensionHandler_ = null;
this.defineEvents(Object.keys(remoting.ClientSession.Events));
};
base.extend(remoting.ClientSession, base.EventSourceImpl);
/** @enum {string} */
remoting.ClientSession.Events = {
stateChanged: 'stateChanged',
videoChannelStateChanged: 'videoChannelStateChanged',
};
// Note that the positive values in both of these enums are copied directly
// from chromoting_scriptable_object.h and must be kept in sync. The negative
// values represent state transitions that occur within the web-app that have
// no corresponding plugin state transition.
/** @enum {number} */
remoting.ClientSession.State = {
CONNECTION_CANCELED: -3, // Connection closed (gracefully) before connecting.
CONNECTION_DROPPED: -2, // Succeeded, but subsequently closed with an error.
CREATED: -1,
UNKNOWN: 0,
CONNECTING: 1,
INITIALIZING: 2,
CONNECTED: 3,
CLOSED: 4,
FAILED: 5
};
/**
* @param {string} state The state name.
* @return {remoting.ClientSession.State} The session state enum value.
*/
remoting.ClientSession.State.fromString = function(state) {
if (!remoting.ClientSession.State.hasOwnProperty(state)) {
throw "Invalid ClientSession.State: " + state;
}
return remoting.ClientSession.State[state];
};
/**
@param {remoting.ClientSession.State} current
@param {remoting.ClientSession.State} previous
@constructor
*/
remoting.ClientSession.StateEvent = function(current, previous) {
/** @type {remoting.ClientSession.State} */
this.previous = previous
/** @type {remoting.ClientSession.State} */
this.current = current;
};
/** @enum {number} */
remoting.ClientSession.ConnectionError = {
UNKNOWN: -1,
NONE: 0,
HOST_IS_OFFLINE: 1,
SESSION_REJECTED: 2,
INCOMPATIBLE_PROTOCOL: 3,
NETWORK_FAILURE: 4,
HOST_OVERLOAD: 5
};
/**
* @param {string} error The connection error name.
* @return {remoting.ClientSession.ConnectionError} The connection error enum.
*/
remoting.ClientSession.ConnectionError.fromString = function(error) {
if (!remoting.ClientSession.ConnectionError.hasOwnProperty(error)) {
console.error('Unexpected ClientSession.ConnectionError string: ', error);
return remoting.ClientSession.ConnectionError.UNKNOWN;
}
return remoting.ClientSession.ConnectionError[error];
}
/**
* Type used for performance statistics collected by the plugin.
* @constructor
*/
remoting.ClientSession.PerfStats = function() {};
/** @type {number} */
remoting.ClientSession.PerfStats.prototype.videoBandwidth;
/** @type {number} */
remoting.ClientSession.PerfStats.prototype.videoFrameRate;
/** @type {number} */
remoting.ClientSession.PerfStats.prototype.captureLatency;
/** @type {number} */
remoting.ClientSession.PerfStats.prototype.encodeLatency;
/** @type {number} */
remoting.ClientSession.PerfStats.prototype.decodeLatency;
/** @type {number} */
remoting.ClientSession.PerfStats.prototype.renderLatency;
/** @type {number} */
remoting.ClientSession.PerfStats.prototype.roundtripLatency;
// Keys for connection statistics.
remoting.ClientSession.STATS_KEY_VIDEO_BANDWIDTH = 'videoBandwidth';
remoting.ClientSession.STATS_KEY_VIDEO_FRAME_RATE = 'videoFrameRate';
remoting.ClientSession.STATS_KEY_CAPTURE_LATENCY = 'captureLatency';
remoting.ClientSession.STATS_KEY_ENCODE_LATENCY = 'encodeLatency';
remoting.ClientSession.STATS_KEY_DECODE_LATENCY = 'decodeLatency';
remoting.ClientSession.STATS_KEY_RENDER_LATENCY = 'renderLatency';
remoting.ClientSession.STATS_KEY_ROUNDTRIP_LATENCY = 'roundtripLatency';
/**
* Set of capabilities for which hasCapability() can be used to test.
*
* @enum {string}
*/
remoting.ClientSession.Capability = {
// When enabled this capability causes the client to send its screen
// resolution to the host once connection has been established. See
// this.plugin_.notifyClientResolution().
SEND_INITIAL_RESOLUTION: 'sendInitialResolution',
// Let the host know that we're interested in knowing whether or not it
// rate limits desktop-resize requests.
// TODO(kelvinp): This has been supported since M-29. Currently we only have
// <1000 users on M-29 or below. Remove this and the capability on the host.
RATE_LIMIT_RESIZE_REQUESTS: 'rateLimitResizeRequests',
// Indicates that host/client supports Google Drive integration, and that the
// client should send to the host the OAuth tokens to be used by Google Drive
// on the host.
GOOGLE_DRIVE: "googleDrive",
// Indicates that the client supports the video frame-recording extension.
VIDEO_RECORDER: 'videoRecorder',
// Indicates that the client supports 'cast'ing the video stream to a
// cast-enabled device.
CAST: 'casting',
};
/**
* The set of capabilities negotiated between the client and host.
* @type {Array<string>}
* @private
*/
remoting.ClientSession.prototype.capabilities_ = null;
/**
* @param {remoting.ClientSession.Capability} capability The capability to test
* for.
* @return {boolean} True if the capability has been negotiated between
* the client and host.
*/
remoting.ClientSession.prototype.hasCapability = function(capability) {
if (this.capabilities_ == null)
return false;
return this.capabilities_.indexOf(capability) > -1;
};
/**
* Adds <embed> element to the UI container and readies the session object.
*
* @param {function(string, string):boolean} onExtensionMessage The handler for
* protocol extension messages. Returns true if a message is recognized;
* false otherwise.
* @param {Array<string>} requiredCapabilities A list of capabilities
* required by this application.
*/
remoting.ClientSession.prototype.createPluginAndConnect =
function(onExtensionMessage, requiredCapabilities) {
this.uiHandler_.createPluginAndConnect(onExtensionMessage,
requiredCapabilities);
};
/**
* @param {remoting.Error} error
* @param {remoting.ClientPlugin} plugin
*/
remoting.ClientSession.prototype.onPluginInitialized_ = function(
error, plugin) {
if (error != remoting.Error.NONE) {
this.resetWithError_(error);
}
this.plugin_ = plugin;
plugin.setOnOutgoingIqHandler(this.sendIq_.bind(this));
plugin.setOnDebugMessageHandler(this.onDebugMessage_.bind(this));
plugin.setConnectionStatusUpdateHandler(
this.onConnectionStatusUpdate_.bind(this));
plugin.setRouteChangedHandler(this.onRouteChanged_.bind(this));
plugin.setConnectionReadyHandler(this.onConnectionReady_.bind(this));
plugin.setCapabilitiesHandler(this.onSetCapabilities_.bind(this));
plugin.setGnubbyAuthHandler(
this.processGnubbyAuthMessage_.bind(this));
plugin.setCastExtensionHandler(
this.processCastExtensionMessage_.bind(this));
this.initiateConnection_();
};
/**
* @param {remoting.Error} error
*/
remoting.ClientSession.prototype.resetWithError_ = function(error) {
this.signalStrategy_.setIncomingStanzaCallback(null);
this.removePlugin();
this.error_ = error;
this.setState_(remoting.ClientSession.State.FAILED);
}
/**
* Deletes the <embed> element from the container, without sending a
* session_terminate request. This is to be called when the session was
* disconnected by the Host.
*
* @return {void} Nothing.
*/
remoting.ClientSession.prototype.removePlugin = function() {
this.uiHandler_.removePlugin();
this.plugin_ = null;
};
/**
* Disconnect the current session with a particular |error|. The session will
* raise a |stateChanged| event in response to it. The caller should then call
* |cleanup| to remove and destroy the <embed> element.
*
* @param {remoting.Error} error The reason for the disconnection. Use
* remoting.Error.NONE if there is no error.
* @return {void} Nothing.
*/
remoting.ClientSession.prototype.disconnect = function(error) {
var state = (error == remoting.Error.NONE) ?
remoting.ClientSession.State.CLOSED :
remoting.ClientSession.State.FAILED;
// The plugin won't send a state change notification, so we explicitly log
// the fact that the connection has closed.
this.logToServer.logClientSessionStateChange(state, error);
this.error_ = error;
this.setState_(state);
};
/**
* Deletes the <embed> element from the container and disconnects.
*
* @return {void} Nothing.
*/
remoting.ClientSession.prototype.cleanup = function() {
this.sendIq_(
'<cli:iq ' +
'to="' + this.host_.jabberId + '" ' +
'type="set" ' +
'id="session-terminate" ' +
'xmlns:cli="jabber:client">' +
'<jingle ' +
'xmlns="urn:xmpp:jingle:1" ' +
'action="session-terminate" ' +
'sid="' + this.sessionId_ + '">' +
'<reason><success/></reason>' +
'</jingle>' +
'</cli:iq>');
this.removePlugin();
};
/**
* @return {remoting.ClientSession.State} The current state.
*/
remoting.ClientSession.prototype.getState = function() {
return this.state_;
};
/**
* @return {remoting.Error} The current error code.
*/
remoting.ClientSession.prototype.getError = function() {
return this.error_;
};
/**
* Called when the client receives its first frame.
*
* @return {void} Nothing.
*/
remoting.ClientSession.prototype.onFirstFrameReceived = function() {
this.hasReceivedFrame_ = true;
};
/**
* @return {boolean} Whether the client has received a video buffer.
*/
remoting.ClientSession.prototype.hasReceivedFrame = function() {
return this.hasReceivedFrame_;
};
/**
* Sends a signaling message.
*
* @param {string} message XML string of IQ stanza to send to server.
* @return {void} Nothing.
* @private
*/
remoting.ClientSession.prototype.sendIq_ = function(message) {
// Extract the session id, so we can close the session later.
var parser = new DOMParser();
var iqNode = parser.parseFromString(message, 'text/xml').firstChild;
var jingleNode = iqNode.firstChild;
if (jingleNode) {
var action = jingleNode.getAttribute('action');
if (jingleNode.nodeName == 'jingle' && action == 'session-initiate') {
this.sessionId_ = jingleNode.getAttribute('sid');
}
}
console.log(remoting.timestamp(), remoting.formatIq.prettifySendIq(message));
if (this.signalStrategy_.getState() !=
remoting.SignalStrategy.State.CONNECTED) {
console.log("Message above is dropped because signaling is not connected.");
return;
}
this.signalStrategy_.sendMessage(message);
};
/**
* @param {string} msg
* @private
*/
remoting.ClientSession.prototype.onDebugMessage_ = function(msg) {
console.log('plugin: ' + msg.trimRight());
};
/**
* @param {Element} message
* @private
*/
remoting.ClientSession.prototype.onIncomingMessage_ = function(message) {
if (!this.plugin_) {
return;
}
var formatted = new XMLSerializer().serializeToString(message);
console.log(remoting.timestamp(),
remoting.formatIq.prettifyReceiveIq(formatted));
this.plugin_.onIncomingIq(formatted);
};
/**
* @private
*/
remoting.ClientSession.prototype.initiateConnection_ = function() {
/** @type {remoting.ClientSession} */
var that = this;
/** @param {string} sharedSecret Shared secret. */
function onSharedSecretReceived(sharedSecret) {
that.plugin_.connect(that.host_.jabberId, that.host_.publicKey,
that.signalStrategy_.getJid(), sharedSecret,
that.authenticationMethods_, that.host_.hostId,
that.clientPairingId_, that.clientPairedSecret_);
}
this.getSharedSecret_(onSharedSecretReceived);
};
/**
* Gets shared secret to be used for connection.
*
* @param {function(string)} callback Callback called with the shared secret.
* @return {void} Nothing.
* @private
*/
remoting.ClientSession.prototype.getSharedSecret_ = function(callback) {
/** @type remoting.ClientSession */
var that = this;
if (this.plugin_.hasFeature(remoting.ClientPlugin.Feature.THIRD_PARTY_AUTH)) {
/** @type{function(string, string, string): void} */
var fetchThirdPartyToken = function(tokenUrl, hostPublicKey, scope) {
that.fetchThirdPartyToken_(
tokenUrl, hostPublicKey, scope,
that.plugin_.onThirdPartyTokenFetched.bind(that.plugin_));
};
this.plugin_.setFetchThirdPartyTokenHandler(fetchThirdPartyToken);
}
if (this.accessCode_) {
// Shared secret was already supplied before connecting (It2Me case).
callback(this.accessCode_);
} else if (this.plugin_.hasFeature(
remoting.ClientPlugin.Feature.ASYNC_PIN)) {
// Plugin supports asynchronously asking for the PIN.
this.plugin_.useAsyncPinDialog();
/** @param {boolean} pairingSupported */
var fetchPin = function(pairingSupported) {
that.fetchPin_(pairingSupported,
that.plugin_.onPinFetched.bind(that.plugin_));
};
this.plugin_.setFetchPinHandler(fetchPin);
callback('');
} else {
// Clients that don't support asking for a PIN asynchronously also don't
// support pairing, so request the PIN now without offering to remember it.
this.fetchPin_(false, callback);
}
};
/**
* Callback that the plugin invokes to indicate that the connection
* status has changed.
*
* @param {number} status The plugin's status.
* @param {number} error The plugin's error state, if any.
* @private
*/
remoting.ClientSession.prototype.onConnectionStatusUpdate_ =
function(status, error) {
if (status == remoting.ClientSession.State.CONNECTED) {
this.uiHandler_.updateClientSessionUi_(this);
} else if (status == remoting.ClientSession.State.FAILED) {
switch (error) {
case remoting.ClientSession.ConnectionError.HOST_IS_OFFLINE:
this.error_ = remoting.Error.HOST_IS_OFFLINE;
break;
case remoting.ClientSession.ConnectionError.SESSION_REJECTED:
this.error_ = remoting.Error.INVALID_ACCESS_CODE;
break;
case remoting.ClientSession.ConnectionError.INCOMPATIBLE_PROTOCOL:
this.error_ = remoting.Error.INCOMPATIBLE_PROTOCOL;
break;
case remoting.ClientSession.ConnectionError.NETWORK_FAILURE:
this.error_ = remoting.Error.P2P_FAILURE;
break;
case remoting.ClientSession.ConnectionError.HOST_OVERLOAD:
this.error_ = remoting.Error.HOST_OVERLOAD;
break;
default:
this.error_ = remoting.Error.UNEXPECTED;
}
}
this.setState_(/** @type {remoting.ClientSession.State} */ (status));
};
/**
* Callback that the plugin invokes to indicate that the connection type for
* a channel has changed.
*
* @param {string} channel The channel name.
* @param {string} connectionType The new connection type.
* @private
*/
remoting.ClientSession.prototype.onRouteChanged_ =
function(channel, connectionType) {
console.log('plugin: Channel ' + channel + ' using ' +
connectionType + ' connection.');
this.logToServer.setConnectionType(connectionType);
};
/**
* Callback that the plugin invokes to indicate when the connection is
* ready.
*
* @param {boolean} ready True if the connection is ready.
* @private
*/
remoting.ClientSession.prototype.onConnectionReady_ = function(ready) {
// TODO(jamiewalch): Currently, the logic for determining whether or not the
// connection is available is based solely on whether or not any video frames
// have been received recently. which leads to poor UX on slow connections.
// Re-enable this once crbug.com/435315 has been fixed.
var ignoreVideoChannelState = true;
if (ignoreVideoChannelState) {
console.log('Video channel ' + (ready ? '' : 'not ') + 'ready.');
return;
}
this.uiHandler_.onConnectionReady(ready);
this.raiseEvent(remoting.ClientSession.Events.videoChannelStateChanged,
ready);
};
/**
* Called when the client-host capabilities negotiation is complete.
* TODO(kelvinp): Move this function out of ClientSession.
*
* @param {!Array<string>} capabilities The set of capabilities negotiated
* between the client and host.
* @return {void} Nothing.
* @private
*/
remoting.ClientSession.prototype.onSetCapabilities_ = function(capabilities) {
if (this.capabilities_ != null) {
console.error('onSetCapabilities_() is called more than once');
return;
}
this.capabilities_ = capabilities;
if (this.hasCapability(
remoting.ClientSession.Capability.SEND_INITIAL_RESOLUTION)) {
this.uiHandler_.notifyClientResolution_();
}
if (this.hasCapability(remoting.ClientSession.Capability.GOOGLE_DRIVE)) {
this.sendGoogleDriveAccessToken_();
}
if (this.hasCapability(
remoting.ClientSession.Capability.VIDEO_RECORDER)) {
this.uiHandler_.initVideoFrameRecorder();
}
};
/**
* @param {remoting.ClientSession.State} newState The new state for the session.
* @return {void} Nothing.
* @private
*/
remoting.ClientSession.prototype.setState_ = function(newState) {
var oldState = this.state_;
this.state_ = newState;
var state = this.state_;
if (oldState == remoting.ClientSession.State.CONNECTING) {
if (this.state_ == remoting.ClientSession.State.CLOSED) {
state = remoting.ClientSession.State.CONNECTION_CANCELED;
} else if (this.state_ == remoting.ClientSession.State.FAILED &&
this.error_ == remoting.Error.HOST_IS_OFFLINE &&
!this.logHostOfflineErrors_) {
// The application requested host-offline errors to be suppressed, for
// example, because this connection attempt is using a cached host JID.
console.log('Suppressing host-offline error.');
state = remoting.ClientSession.State.CONNECTION_CANCELED;
}
} else if (oldState == remoting.ClientSession.State.CONNECTED &&
this.state_ == remoting.ClientSession.State.FAILED) {
state = remoting.ClientSession.State.CONNECTION_DROPPED;
}
this.logToServer.logClientSessionStateChange(state, this.error_);
if (this.state_ == remoting.ClientSession.State.CONNECTED) {
this.createGnubbyAuthHandler_();
this.createCastExtensionHandler_();
}
this.raiseEvent(remoting.ClientSession.Events.stateChanged,
new remoting.ClientSession.StateEvent(newState, oldState)
);
};
/**
* Returns an associative array with a set of stats for this connection.
*
* @return {remoting.ClientSession.PerfStats} The connection statistics.
*/
remoting.ClientSession.prototype.getPerfStats = function() {
return this.plugin_.getPerfStats();
};
/**
* Logs statistics.
*
* @param {remoting.ClientSession.PerfStats} stats
*/
remoting.ClientSession.prototype.logStatistics = function(stats) {
this.logToServer.logStatistics(stats);
};
/**
* Enable or disable logging of connection errors due to a host being offline.
* For example, if attempting a connection using a cached JID, host-offline
* errors should not be logged because the JID will be refreshed and the
* connection retried.
*
* @param {boolean} enable True to log host-offline errors; false to suppress.
*/
remoting.ClientSession.prototype.logHostOfflineErrors = function(enable) {
this.logHostOfflineErrors_ = enable;
};
/**
* Request pairing with the host for PIN-less authentication.
*
* @param {string} clientName The human-readable name of the client.
* @param {function(string, string):void} onDone Callback to receive the
* client id and shared secret when they are available.
*/
remoting.ClientSession.prototype.requestPairing = function(clientName, onDone) {
if (this.plugin_) {
this.plugin_.requestPairing(clientName, onDone);
}
};
/**
* Sends a clipboard item to the host.
*
* @param {string} mimeType The MIME type of the clipboard item.
* @param {string} item The clipboard item.
*/
remoting.ClientSession.prototype.sendClipboardItem = function(mimeType, item) {
if (!this.plugin_)
return;
this.plugin_.sendClipboardItem(mimeType, item);
};
/**
* Sends an extension message to the host.
*
* @param {string} type The message type.
* @param {string} message The message payload.
*/
remoting.ClientSession.prototype.sendClientMessage = function(type, message) {
if (!this.plugin_)
return;
this.plugin_.sendClientMessage(type, message);
};
/**
* Send a gnubby-auth extension message to the host.
* @param {Object} data The gnubby-auth message data.
*/
remoting.ClientSession.prototype.sendGnubbyAuthMessage = function(data) {
if (!this.plugin_)
return;
this.plugin_.sendClientMessage('gnubby-auth', JSON.stringify(data));
};
/**
* Process a remote gnubby auth request.
* @param {string} data Remote gnubby request data.
* @private
*/
remoting.ClientSession.prototype.processGnubbyAuthMessage_ = function(data) {
if (this.gnubbyAuthHandler_) {
try {
this.gnubbyAuthHandler_.onMessage(data);
} catch (/** @type {*} */ err) {
console.error('Failed to process gnubby message: ', err);
}
} else {
console.error('Received unexpected gnubby message');
}
};
/**
* Create a gnubby auth handler and inform the host that gnubby auth is
* supported.
* @private
*/
remoting.ClientSession.prototype.createGnubbyAuthHandler_ = function() {
if (this.uiHandler_.getMode() == remoting.DesktopConnectedView.Mode.ME2ME) {
this.gnubbyAuthHandler_ = new remoting.GnubbyAuthHandler(this);
// TODO(psj): Move to more generic capabilities mechanism.
this.sendGnubbyAuthMessage({'type': 'control', 'option': 'auth-v1'});
}
};
/**
* Timer callback to send the access token to the host.
* @private
*/
remoting.ClientSession.prototype.sendGoogleDriveAccessToken_ = function() {
if (this.state_ != remoting.ClientSession.State.CONNECTED) {
return;
}
/** @type {remoting.ClientSession} */
var that = this;
/** @param {string} token */
var sendToken = function(token) {
remoting.clientSession.sendClientMessage('accessToken', token);
};
/** @param {remoting.Error} error */
var sendError = function(error) {
console.log('Failed to refresh access token: ' + error);
}
remoting.identity.callWithNewToken(sendToken, sendError);
window.setTimeout(this.sendGoogleDriveAccessToken_.bind(this),
remoting.ACCESS_TOKEN_RESEND_INTERVAL_MS);
};
/**
* Send a Cast extension message to the host.
* @param {Object} data The cast message data.
*/
remoting.ClientSession.prototype.sendCastExtensionMessage = function(data) {
if (!this.plugin_)
return;
this.plugin_.sendClientMessage('cast_message', JSON.stringify(data));
};
/**
* Process a remote Cast extension message from the host.
* @param {string} data Remote cast extension data message.
* @private
*/
remoting.ClientSession.prototype.processCastExtensionMessage_ = function(data) {
if (this.castExtensionHandler_) {
try {
this.castExtensionHandler_.onMessage(data);
} catch (/** @type {*} */ err) {
console.error('Failed to process cast message: ', err);
}
} else {
console.error('Received unexpected cast message');
}
};
/**
* Create a CastExtensionHandler and inform the host that cast extension
* is supported.
* @private
*/
remoting.ClientSession.prototype.createCastExtensionHandler_ = function() {
if (remoting.app.hasCapability(remoting.ClientSession.Capability.CAST) &&
this.uiHandler_.getMode() == remoting.DesktopConnectedView.Mode.ME2ME) {
this.castExtensionHandler_ = new remoting.CastExtensionHandler(this);
}
};
/**
* Handles protocol extension messages.
* @param {string} type Type of extension message.
* @param {Object} message The parsed extension message data.
* @return {boolean} True if the message was recognized, false otherwise.
*/
remoting.ClientSession.prototype.handleExtensionMessage =
function(type, message) {
if (this.uiHandler_.handleExtensionMessage(type, message)) {
return true;
}
return false;
};