Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / socket.io-client / components / learnboost-engine.io-client / lib / transports / flashsocket.js
1
2 /**
3  * Module dependencies.
4  */
5
6 var WS = require('./websocket')
7   , util = require('../util')
8   , debug = require('debug')('engine.io-client:flashsocket');
9
10 /**
11  * Module exports.
12  */
13
14 module.exports = FlashWS;
15
16 /**
17  * Obfuscated key for Blue Coat.
18  */
19
20 var xobject = global[['Active'].concat('Object').join('X')];
21
22 /**
23  * FlashWS constructor.
24  *
25  * @api public
26  */
27
28 function FlashWS (options) {
29   WS.call(this, options);
30   this.flashPath = options.flashPath;
31   this.policyPort = options.policyPort;
32 };
33
34 /**
35  * Inherits from WebSocket.
36  */
37
38 util.inherits(FlashWS, WS);
39
40 /**
41  * Transport name.
42  *
43  * @api public
44  */
45
46 FlashWS.prototype.name = 'flashsocket';
47
48 /**
49  * Opens the transport.
50  *
51  * @api public
52  */
53
54 FlashWS.prototype.doOpen = function () {
55   if (!this.check()) {
56     // let the probe timeout
57     return;
58   }
59
60   // instrument websocketjs logging
61   function log (type) {
62     return function(){
63       var str = Array.prototype.join.call(arguments, ' ');
64       debug('[websocketjs %s] %s', type, str);
65     };
66   };
67
68   WEB_SOCKET_LOGGER = { log: log('debug'), error: log('error') };
69   WEB_SOCKET_SUPPRESS_CROSS_DOMAIN_SWF_ERROR = true;
70   WEB_SOCKET_DISABLE_AUTO_INITIALIZATION = true;
71
72   if ('undefined' == typeof WEB_SOCKET_SWF_LOCATION) {
73     WEB_SOCKET_SWF_LOCATION = this.flashPath + 'WebSocketMainInsecure.swf';
74   }
75
76   // dependencies
77   var deps = [this.flashPath + 'web_socket.js'];
78
79   if ('undefined' == typeof swfobject) {
80     deps.unshift(this.flashPath + 'swfobject.js');
81   }
82
83   var self = this;
84
85   load(deps, function () {
86     self.ready(function () {
87       WebSocket.__addTask(function () {
88         WS.prototype.doOpen.call(self);
89       });
90     });
91   });
92 };
93
94 /**
95  * Override to prevent closing uninitialized flashsocket.
96  *
97  * @api private
98  */
99
100 FlashWS.prototype.doClose = function () {
101   if (!this.socket) return;
102   var self = this;
103   WebSocket.__addTask(function() {
104     WS.prototype.doClose.call(self);
105   });
106 };
107
108 /**
109  * Writes to the Flash socket.
110  *
111  * @api private
112  */
113
114 FlashWS.prototype.write = function() {
115   var self = this, args = arguments;
116   WebSocket.__addTask(function () {
117     WS.prototype.write.apply(self, args);
118   });
119 };
120
121 /**
122  * Called upon dependencies are loaded.
123  *
124  * @api private
125  */
126
127 FlashWS.prototype.ready = function (fn) {
128   if (typeof WebSocket == 'undefined' ||
129     !('__initialize' in WebSocket) || !swfobject) {
130     return;
131   }
132
133   if (swfobject.getFlashPlayerVersion().major < 10) {
134     return;
135   }
136
137   function init () {
138     // Only start downloading the swf file when the checked that this browser
139     // actually supports it
140     if (!FlashWS.loaded) {
141       if (843 != self.policyPort) {
142         WebSocket.loadFlashPolicyFile('xmlsocket://' + self.host + ':' + self.policyPort);
143       }
144
145       WebSocket.__initialize();
146       FlashWS.loaded = true;
147     }
148
149     fn.call(self);
150   }
151
152   var self = this;
153   if (document.body) {
154     return init();
155   }
156
157   util.load(init);
158 };
159
160 /**
161  * Feature detection for flashsocket.
162  *
163  * @return {Boolean} whether this transport is available.
164  * @api public
165  */
166
167 FlashWS.prototype.check = function () {
168   if ('undefined' != typeof process) {
169     return false;
170   }
171
172   if (typeof WebSocket != 'undefined' && !('__initialize' in WebSocket)) {
173     return false;
174   }
175
176   if (xobject) {
177     var control = null;
178     try {
179       control = new xobject('ShockwaveFlash.ShockwaveFlash');
180     } catch (e) { }
181     if (control) {
182       return true;
183     }
184   } else {
185     for (var i = 0, l = navigator.plugins.length; i < l; i++) {
186       for (var j = 0, m = navigator.plugins[i].length; j < m; j++) {
187         if (navigator.plugins[i][j].description == 'Shockwave Flash') {
188           return true;
189         }
190       }
191     }
192   }
193
194   return false;
195 };
196
197 /**
198  * Lazy loading of scripts.
199  * Based on $script by Dustin Diaz - MIT
200  */
201
202 var scripts = {};
203
204 /**
205  * Injects a script. Keeps tracked of injected ones.
206  *
207  * @param {String} path
208  * @param {Function} callback
209  * @api private
210  */
211
212 function create (path, fn) {
213   if (scripts[path]) return fn();
214
215   var el = document.createElement('script');
216   var loaded = false;
217
218   debug('loading "%s"', path);
219   el.onload = el.onreadystatechange = function () {
220     if (loaded || scripts[path]) return;
221     var rs = el.readyState;
222     if (!rs || 'loaded' == rs || 'complete' == rs) {
223       debug('loaded "%s"', path);
224       el.onload = el.onreadystatechange = null;
225       loaded = true;
226       scripts[path] = true;
227       fn();
228     }
229   };
230
231   el.async = 1;
232   el.src = path;
233
234   var head = document.getElementsByTagName('head')[0];
235   head.insertBefore(el, head.firstChild);
236 };
237
238 /**
239  * Loads scripts and fires a callback.
240  *
241  * @param {Array} paths
242  * @param {Function} callback
243  */
244
245 function load (arr, fn) {
246   function process (i) {
247     if (!arr[i]) return fn();
248     create(arr[i], function () {
249       process(++i);
250     });
251   };
252
253   process(0);
254 };