/
info.js
115 lines (103 loc) · 3.33 KB
/
info.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
/*
* ***** BEGIN LICENSE BLOCK *****
* Copyright (c) 2011-2012 VMware, Inc.
*
* For the license see COPYING.
* ***** END LICENSE BLOCK *****
*/
var InfoReceiver = function(base_url, AjaxObject) {
var that = this;
utils.delay(function(){that.doXhr(base_url, AjaxObject);});
};
InfoReceiver.prototype = new EventEmitter(['finish']);
InfoReceiver.prototype.doXhr = function(base_url, AjaxObject) {
var that = this;
var t0 = (new Date()).getTime();
var xo = new AjaxObject('GET', base_url + '/info');
var tref = utils.delay(8000,
function(){xo.ontimeout();});
xo.onfinish = function(status, text) {
clearTimeout(tref);
tref = null;
if (status === 200) {
var rtt = (new Date()).getTime() - t0;
var info = JSON.parse(text);
if (typeof info !== 'object') info = {};
that.emit('finish', info, rtt);
} else {
that.emit('finish');
}
};
xo.ontimeout = function() {
xo.close();
that.emit('finish');
};
};
var InfoReceiverIframe = function(base_url) {
var that = this;
var go = function() {
var ifr = new IframeTransport();
ifr.protocol = 'w-iframe-info-receiver';
var fun = function(r) {
if (typeof r === 'string' && r.substr(0,1) === 'm') {
var d = JSON.parse(r.substr(1));
var info = d[0], rtt = d[1];
that.emit('finish', info, rtt);
} else {
that.emit('finish');
}
ifr.doCleanup();
ifr = null;
};
var mock_ri = {
_options: {},
_didClose: fun,
_didMessage: fun
};
ifr.i_constructor(mock_ri, base_url, base_url);
}
if(!_document.body) {
utils.attachEvent('load', go);
} else {
go();
}
};
InfoReceiverIframe.prototype = new EventEmitter(['finish']);
var InfoReceiverFake = function() {
// It may not be possible to do cross domain AJAX to get the info
// data, for example for IE7. But we want to run JSONP, so let's
// fake the response, with rtt=2s (rto=6s).
var that = this;
utils.delay(function() {
that.emit('finish', {}, 2000);
});
};
InfoReceiverFake.prototype = new EventEmitter(['finish']);
var createInfoReceiver = function(base_url) {
if (utils.isSameOriginUrl(base_url)) {
// If, for some reason, we have SockJS locally - there's no
// need to start up the complex machinery. Just use ajax.
return new InfoReceiver(base_url, utils.XHRLocalObject);
}
switch (utils.isXHRCorsCapable()) {
case 1:
// XHRLocalObject -> no_credentials=true
return new InfoReceiver(base_url, utils.XHRLocalObject);
case 2:
return new InfoReceiver(base_url, utils.XDRObject);
case 3:
// Opera
return new InfoReceiverIframe(base_url);
default:
// IE 7
return new InfoReceiverFake();
};
};
var WInfoReceiverIframe = FacadeJS['w-iframe-info-receiver'] = function(ri, _trans_url, base_url) {
var ir = new InfoReceiver(base_url, utils.XHRLocalObject);
ir.onfinish = function(info, rtt) {
ri._didMessage('m'+JSON.stringify([info, rtt]));
ri._didClose();
}
};
WInfoReceiverIframe.prototype.doCleanup = function() {};