Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/compat.js

Issue 10214006: Added JS-based XMLHttpRequest implementation (Closed)
Patch Set: Created April 15, 2013, 11:36 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | test/WebRequest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of the Adblock Plus extension, 2 * This file is part of the Adblock Plus extension,
3 * Copyright (C) 2006-2012 Eyeo GmbH 3 * Copyright (C) 2006-2012 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 // TODO: These need to be defined properly 18 // TODO: These need to be defined properly
19 var window = this; 19 var window = this;
20 function XMLHttpRequest() {};
21 function requestFileSystem() {}; 20 function requestFileSystem() {};
22 21
23 // 22 //
24 // Module framework stuff 23 // Module framework stuff
25 // 24 //
26 25
27 function require(module) 26 function require(module)
28 { 27 {
29 return require.scopes[module]; 28 return require.scopes[module];
30 } 29 }
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 catch(e) 270 catch(e)
272 { 271 {
273 Cu.reportError(e); 272 Cu.reportError(e);
274 } 273 }
275 me.scheduleTimeout(); 274 me.scheduleTimeout();
276 }, this.delay); 275 }, this.delay);
277 } 276 }
278 }; 277 };
279 278
280 // 279 //
281 // Add a channel property to XMLHttpRequest, Synchronizer needs it 280 // Fake XMLHttpRequest implementation
282 // 281 //
283 282
284 XMLHttpRequest.prototype.channel = 283 function XMLHttpRequest()
285 { 284 {
286 status: -1, 285 this._requestHeaders = {};
287 notificationCallbacks: {}, 286 this._loadHandlers = [];
288 loadFlags: 0, 287 this._errorHandlers = [];
289 INHIBIT_CACHING: 0, 288 };
290 VALIDATE_ALWAYS: 0, 289 XMLHttpRequest.prototype =
291 QueryInterface: function() 290 {
291 _url: null,
292 _requestHeaders: null,
293 _responseHeaders: null,
294 _loadHandlers: null,
295 _errorHandlers: null,
296 onload: null,
297 onerror: null,
298 status: 0,
299 readyState: 0,
300 responseText: null,
301
302 addEventListener: function(eventName, handler, capture)
292 { 303 {
293 return this; 304 var list;
305 if (eventName == "load")
306 list = this._loadHandlers;
307 else if (eventName == "error")
308 list = this._errorHandlers;
309 else
310 throw new Error("Event type " + eventName + " not supported");
311
312 if (list.indexOf(handler) < 0)
313 list.push(handler);
314 },
315
316 removeEventListener: function(eventName, handler, capture)
317 {
318 var list;
319 if (eventName == "load")
320 list = this._loadHandlers;
321 else if (eventName == "error")
322 list = this._errorHandlers;
323 else
324 throw new Error("Event type " + eventName + " not supported");
325
326 var index = list.indexOf(handler);
327 if (index >= 0)
328 list.splice(index, 1);
329 },
330
331 open: function(method, url, async, user, password)
332 {
333 if (method != "GET")
334 throw new Error("Only GET requests are currently supported");
335 if (typeof async != "undefined" && !async)
336 throw new Error("Sync requests are not supported");
337 if (typeof user != "undefined" || typeof password != "undefined")
338 throw new Error("User authentification is not supported");
339 if (this.readyState != 0)
340 throw new Error("Already opened");
341
342 this.readyState = 1;
343 this._url = url;
344 },
345
346 send: function(data)
347 {
348 if (this.readyState != 1)
349 throw new Error("XMLHttpRequest.send() is being called before XMLHttpReque st.open()");
350 if (typeof data != "undefined" && data)
351 throw new Error("Sending data to server is not supported");
352
353 this.readyState = 3;
354 window._webRequest.GET(this._url, this._requestHeaders, function(result)
355 {
356 this.channel.status = result.status;
357 this.status = result.responseStatus;
358 this.responseText = result.responseText;
359 this._responseHeaders = result.responseHeaders;
360 this.readyState = 4;
361
362 // Notify event listeners
363 const NS_OK = 0;
364 var eventName = (this.channel.status == NS_OK ? "load" : "error");
365 var event = {type: eventName};
366
367 if (this["on" + eventName])
368 this.onload.call(this, event);
369
370 var list = this["_" + eventName + "Handlers"];
371 for (var i = 0; i < list.length; i++)
372 list[i].call(this, event);
373 }.bind(this));
374 },
375
376 overrideMimeType: function(mime)
377 {
378 },
379
380 setRequestHeader: function(name, value)
381 {
382 if (this.readyState > 1)
383 throw new Error("Cannot set request header after sending");
384
385 this._requestHeaders[name] = value;
386 },
387
388 getResponseHeader: function(name)
389 {
390 name = name.toLowerCase();
391 if (!this._responseHeaders || !this._responseHeaders.hasOwnProperty(name))
392 return null;
393 else
394 return this._responseHeaders[name];
395 },
396
397 channel:
398 {
399 status: -1,
400 notificationCallbacks: {},
401 loadFlags: 0,
402 INHIBIT_CACHING: 0,
403 VALIDATE_ALWAYS: 0,
404 QueryInterface: function()
405 {
406 return this;
407 }
294 } 408 }
295 }; 409 };
OLDNEW
« no previous file with comments | « no previous file | test/WebRequest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld