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
|
// Copyright (c) 2011 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
* Session class that handles creation and teardown of a remoting session.
*
* This abstracts a <embed> element and controls the plugin which does the
* actual remoting work. There should be no UI code inside this class. It
* should be purely thought of as a controller of sorts.
*/
'use strict';
var remoting = remoting || {};
(function() {
/**
* @param {string} hostJid The jid of the host to connect to.
* @param {string} hostPublicKey The base64 encoded version of the host's
* public key.
* @param {string} accessCode The access code for the IT2Me connection.
* @param {string} email The username for the talk network.
* @param {function(remoting.ClientSession.State):void} onStateChange
* The callback to invoke when the session changes state.
* @constructor
*/
remoting.ClientSession = function(hostJid, hostPublicKey, accessCode, email,
onStateChange) {
this.state = remoting.ClientSession.CREATED;
this.hostJid = hostJid;
this.hostPublicKey = hostPublicKey;
this.accessCode = accessCode;
this.email = email;
this.clientJid = '';
this.onStateChange = onStateChange;
};
/** @enum {number} */
remoting.ClientSession.State = {
UNKNOWN: 0,
CREATED: 1,
BAD_PLUGIN_VERSION: 2,
UNKNOWN_PLUGIN_ERROR: 3,
CONNECTING: 4,
INITIALIZING: 5,
CONNECTED: 6,
CLOSED: 7,
CONNECTION_FAILED: 8
};
/**
* The current state of the session.
* @type {remoting.ClientSession.State}
*/
remoting.ClientSession.prototype.state = remoting.ClientSession.State.UNKNOWN;
/**
* Chromoting session API version (for this javascript).
* This is compared with the plugin API version to verify that they are
* compatible.
*
* @const
*/
remoting.ClientSession.prototype.API_VERSION_ = 2;
/**
* Server used to bridge into the Jabber network for establishing Jingle
* connections.
*
* @const
*/
remoting.ClientSession.prototype.HTTP_XMPP_PROXY_ =
'https://chromoting-httpxmpp-oauth2-dev.corp.google.com';
/**
* The oldest API version that we support.
* This will differ from the |API_VERSION_| if we maintain backward
* compatibility with older API versions.
*
* @const
*/
remoting.ClientSession.prototype.API_MIN_VERSION_ = 1;
/**
* The id of the client plugin
*
* @const
*/
remoting.ClientSession.prototype.PLUGIN_ID = 'session-client-plugin';
/**
* Callback to invoke when the state is changed.
*
* @type {function(remoting.ClientSession.State):void}
*/
remoting.ClientSession.prototype.onStateChange = null;
/**
* Adds <embed> element to |container| and readies the sesion object.
*
* @param {Element} container The element to add the plugin to.
* @param {string} oauth2AccessToken A valid OAuth2 access token.
* @return {void} Nothing.
*/
remoting.ClientSession.prototype.createPluginAndConnect =
function(container, oauth2AccessToken) {
this.plugin = document.createElement('embed');
this.plugin.id = this.PLUGIN_ID;
this.plugin.src = 'about://none';
this.plugin.type = 'pepper-application/x-chromoting';
container.appendChild(this.plugin);
if (!this.isPluginVersionSupported_(this.plugin)) {
// TODO(ajwong): Remove from parent.
delete this.plugin;
this.setState_(remoting.ClientSession.BAD_PLUGIN_VERSION);
return;
}
var that = this;
this.plugin.sendIq = function(msg) { that.sendIq_(msg); };
this.plugin.debugInfo = function(msg) {
remoting.debug.log('plugin: ' + msg);
};
// TODO(ajwong): Is it even worth having this class handle these events?
// Or would it be better to just allow users to pass in their own handlers
// and leave these blank by default?
this.plugin.connectionInfoUpdate = function() {
that.connectionInfoUpdateCallback();
};
this.plugin.desktopSizeUpdate = function() { that.onDesktopSizeChanged_(); };
// For IT2Me, we are pre-authorized so there is no login challenge.
this.plugin.loginChallenge = function() {};
// TODO(garykac): Clean exit if |connect| isn't a function.
if (typeof this.plugin.connect === 'function') {
this.registerConnection_(oauth2AccessToken);
} else {
remoting.debug.log('ERROR: remoting plugin not loaded');
this.setState_(remoting.ClientSession.UNKNOWN_PLUGIN_ERROR);
}
};
/**
* Deletes the <embed> element from the container and disconnects.
*
* @return {void} Nothing.
*/
remoting.ClientSession.prototype.disconnect = function() {
var plugin = document.getElementById(this.PLUGIN_ID);
if (plugin) {
plugin.parentNode.removeChild(plugin);
}
var parameters = {
'to': this.hostJid,
'payload_xml':
'<jingle xmlns="urn:xmpp:jingle:1"' +
' action="session-terminate"' +
' initiator="' + this.clientJid + '"' +
' sid="' + this.sessionId + '">' +
'<reason><success/></reason>' +
'</jingle>',
'id': 'session_terminate',
'type': 'set',
'host_jid': this.hostJid
};
this.sendIqWithParameters_(parameters);
}
/**
* Sends an IQ stanza via the http xmpp proxy.
*
* @param {string} msg XML string of IQ stanza to send to server.
* @return {void} Nothing.
*/
remoting.ClientSession.prototype.sendIq_ = function(msg) {
remoting.debug.log('Sending Iq: ' + msg);
// Extract the top level fields of the Iq packet.
// TODO(ajwong): Can the plugin just return these fields broken out.
var parser = new DOMParser();
var iqNode = parser.parseFromString(msg, 'text/xml').firstChild;
var jingleNode = iqNode.firstChild;
var serializer = new XMLSerializer();
var parameters = {
'to': iqNode.getAttribute('to') || "",
'payload_xml': serializer.serializeToString(jingleNode),
'id': iqNode.getAttribute('id') || '1',
'type': iqNode.getAttribute('type'),
'host_jid': this.hostJid
};
this.sendIqWithParameters_(parameters);
var action = jingleNode.getAttribute('action');
if (jingleNode.nodeName == 'jingle' && action == 'session-initiate') {
// The session id is needed in order to close the session later.
this.sessionId = jingleNode.getAttribute('sid');
}
};
/**
* Sends an IQ stanza via the http xmpp proxy.
*
* @param {(string|Object.<string>)} paramters Parameters to include.
* @return {void} Nothing.
*/
remoting.ClientSession.prototype.sendIqWithParameters_ = function(parameters) {
remoting.xhr.post(this.HTTP_XMPP_PROXY_ + '/sendIq', function(xhr) {},
parameters, {}, true);
}
/**
* Executes a poll loop on the server for more IQ packet to feed to the plugin.
*
* @return {void} Nothing.
*/
remoting.ClientSession.prototype.feedIq_ = function() {
var that = this;
var onIq = function(xhr) {
if (xhr.status == 200) {
remoting.debug.log('Receiving Iq: --' + xhr.responseText + '--');
that.plugin.onIq(xhr.responseText);
}
if (xhr.status == 200 || xhr.status == 204) {
// Poll again.
that.feedIq_();
} else {
remoting.debug.log('HttpXmpp gateway returned code: ' + xhr.status);
that.plugin.disconnect();
that.setState_(remoting.ClientSession.CONNECTION_FAILED);
}
}
remoting.xhr.get(this.HTTP_XMPP_PROXY_ + '/readIq', onIq,
{'host_jid': this.hostJid}, {}, true);
};
/**
* @param {Element} plugin The embed element for the plugin.
* @return {boolean} True if the plugin and web-app versions are compatible.
*/
remoting.ClientSession.prototype.isPluginVersionSupported_ = function(plugin) {
return this.API_VERSION_ >= plugin.apiMinVersion &&
plugin.apiVersion >= this.API_MIN_VERSION_;
};
/**
* Registers a new connection with the HttpXmpp proxy.
*
* @param {string} oauth2AccessToken A valid OAuth2 access token.
* @return {void} Nothing.
*/
remoting.ClientSession.prototype.registerConnection_ =
function(oauth2AccessToken) {
var parameters = {
'host_jid': this.hostJid,
'username': this.email,
'password': oauth2AccessToken
};
var that = this;
var onRegistered = function(xhr) {
if (xhr.status != 200) {
remoting.debug.log('FailedToConnect: --' + xhr.responseText +
'-- (status=' + xhr.status + ')');
that.setState_(remoting.ClientSession.CONNECTION_FAILED);
return;
}
remoting.debug.log('Receiving Iq: --' + xhr.responseText + '--');
that.clientJid = xhr.responseText;
// TODO(ajwong): Remove old version support.
if (that.plugin.apiVersion >= 2) {
that.plugin.connect(that.hostJid, that.hostPublicKey, that.clientJid,
that.accessCode);
} else {
that.plugin.connect(that.hostJid, that.clientJid, that.accessCode);
}
that.feedIq_();
};
remoting.xhr.post(this.HTTP_XMPP_PROXY_ + '/newConnection',
onRegistered, parameters, {}, true);
};
/**
* Callback that the plugin invokes to indicate that the connection
* status has changed.
*/
remoting.ClientSession.prototype.connectionInfoUpdateCallback = function() {
var state = this.plugin.status;
// TODO(ajwong): We're doing silly type translation here. Any way to avoid?
if (state == this.plugin.STATUS_UNKNOWN) {
this.setState_(remoting.ClientSession.State.UNKNOWN);
} else if (state == this.plugin.STATUS_CONNECTING) {
this.setState_(remoting.ClientSession.State.CONNECTING);
} else if (state == this.plugin.STATUS_INITIALIZING) {
this.setState_(remoting.ClientSession.State.INITIALIZING);
} else if (state == this.plugin.STATUS_CONNECTED) {
this.onDesktopSizeChanged_();
this.setState_(remoting.ClientSession.State.CONNECTED);
} else if (state == this.plugin.STATUS_CLOSED) {
this.setState_(remoting.ClientSession.State.CLOSED);
} else if (state == this.plugin.STATUS_FAILED) {
this.setState_(remoting.ClientSession.State.CONNECTION_FAILED);
}
};
/**
* @param {remoting.ClientSession.State} state The new state for the session.
* @return {void} Nothing.
*/
remoting.ClientSession.prototype.setState_ = function(state) {
this.state = state;
if (this.onStateChange) {
this.onStateChange(this.state);
}
};
/**
* This is a callback that gets called when the desktop size contained in the
* the plugin has changed.
*
* @return {void} Nothing.
*/
remoting.ClientSession.prototype.onDesktopSizeChanged_ = function() {
var width = this.plugin.desktopWidth;
var height = this.plugin.desktopHeight;
remoting.debug.log('desktop size changed: ' + width + 'x' + height);
this.plugin.width = width;
this.plugin.height = height;
};
/**
* Informs the plugin that it should scale itself.
*
* @param {boolean} shouldScale If the plugin should scale itself.
* @return {void} Nothing.
*/
remoting.ClientSession.prototype.toggleScaleToFit = function (shouldScale) {
if (shouldScale) {
remoting.debug.log('scale to fit is turned on.');
if (this.plugin.desktopWidth == 0 ||
this.plugin.desktopHeight == 0) {
remoting.debug.log('desktop size is not known yet.');
return;
}
// Make sure both width and height are multiples of two.
var width = window.innerWidth;
var height = window.innerHeight;
if (width % 2 == 1)
--width;
if (height % 2 == 1)
--height;
var scale = 1.0;
if (width < height)
scale = 1.0 * height / this.plugin.desktopHeight;
else
scale = 1.0 * width / this.plugin.desktopWidth;
if (scale > 1.0) {
remoting.debug.log('scale up is not supported');
return;
}
this.plugin.width = this.plugin.desktopWidth * scale;
this.plugin.height = this.plugin.desktopHeight * scale;
} else {
remoting.debug.log('scale to fit is turned off.');
this.plugin.width = this.plugin.desktopWidth;
this.plugin.height = this.plugin.desktopHeight;
}
remoting.debug.log('plugin size is now: ' +
this.plugin.width + " x " + this.plugin.height + '.');
this.plugin.setScaleToFit(shouldScale);
};
/**
* Returns an associative array with a set of stats for this connection.
*
* @return {Object} The connection statistics.
*/
remoting.ClientSession.prototype.stats = function() {
return {
'video_bandwidth': this.plugin.videoBandwidth,
'capture_latency': this.plugin.videoCaptureLatency,
'encode_latency': this.plugin.videoEncodeLatency,
'decode_latency': this.plugin.videoDecodeLatency,
'render_latency': this.plugin.videoRenderLatency,
'roundtrip_latency': this.plugin.roundTripLatency
};
};
}());
|