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: Unbitrotted patch Created April 16, 2013, 3:31 p.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 20
22 // 21 //
23 // Module framework stuff 22 // Module framework stuff
24 // 23 //
25 24
26 function require(module) 25 function require(module)
27 { 26 {
28 return require.scopes[module]; 27 return require.scopes[module];
29 } 28 }
30 require.scopes = {__proto__: null}; 29 require.scopes = {__proto__: null};
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 catch(e) 269 catch(e)
271 { 270 {
272 Cu.reportError(e); 271 Cu.reportError(e);
273 } 272 }
274 me.scheduleTimeout(); 273 me.scheduleTimeout();
275 }, this.delay); 274 }, this.delay);
276 } 275 }
277 }; 276 };
278 277
279 // 278 //
280 // Add a channel property to XMLHttpRequest, Synchronizer needs it 279 // Fake XMLHttpRequest implementation
281 // 280 //
282 281
283 XMLHttpRequest.prototype.channel = 282 function XMLHttpRequest()
284 { 283 {
285 status: -1, 284 this._requestHeaders = {};
286 notificationCallbacks: {}, 285 this._loadHandlers = [];
287 loadFlags: 0, 286 this._errorHandlers = [];
288 INHIBIT_CACHING: 0, 287 };
289 VALIDATE_ALWAYS: 0, 288 XMLHttpRequest.prototype =
290 QueryInterface: function() 289 {
290 _url: null,
291 _requestHeaders: null,
292 _responseHeaders: null,
293 _loadHandlers: null,
294 _errorHandlers: null,
295 onload: null,
296 onerror: null,
297 status: 0,
298 readyState: 0,
299 responseText: null,
300
301 addEventListener: function(eventName, handler, capture)
291 { 302 {
292 return this; 303 var list;
304 if (eventName == "load")
305 list = this._loadHandlers;
306 else if (eventName == "error")
307 list = this._errorHandlers;
308 else
309 throw new Error("Event type " + eventName + " not supported");
310
311 if (list.indexOf(handler) < 0)
312 list.push(handler);
313 },
314
315 removeEventListener: function(eventName, handler, capture)
316 {
317 var list;
318 if (eventName == "load")
319 list = this._loadHandlers;
320 else if (eventName == "error")
321 list = this._errorHandlers;
322 else
323 throw new Error("Event type " + eventName + " not supported");
324
325 var index = list.indexOf(handler);
326 if (index >= 0)
327 list.splice(index, 1);
328 },
329
330 open: function(method, url, async, user, password)
331 {
332 if (method != "GET")
333 throw new Error("Only GET requests are currently supported");
334 if (typeof async != "undefined" && !async)
335 throw new Error("Sync requests are not supported");
336 if (typeof user != "undefined" || typeof password != "undefined")
337 throw new Error("User authentification is not supported");
338 if (this.readyState != 0)
339 throw new Error("Already opened");
340
341 this.readyState = 1;
342 this._url = url;
343 },
344
345 send: function(data)
346 {
347 if (this.readyState != 1)
348 throw new Error("XMLHttpRequest.send() is being called before XMLHttpReque st.open()");
349 if (typeof data != "undefined" && data)
350 throw new Error("Sending data to server is not supported");
351
352 this.readyState = 3;
353 window._webRequest.GET(this._url, this._requestHeaders, function(result)
354 {
355 this.channel.status = result.status;
356 this.status = result.responseStatus;
357 this.responseText = result.responseText;
358 this._responseHeaders = result.responseHeaders;
359 this.readyState = 4;
360
361 // Notify event listeners
362 const NS_OK = 0;
363 var eventName = (this.channel.status == NS_OK ? "load" : "error");
364 var event = {type: eventName};
365
366 if (this["on" + eventName])
367 this.onload.call(this, event);
368
369 var list = this["_" + eventName + "Handlers"];
370 for (var i = 0; i < list.length; i++)
371 list[i].call(this, event);
372 }.bind(this));
373 },
374
375 overrideMimeType: function(mime)
376 {
377 },
378
379 setRequestHeader: function(name, value)
380 {
381 if (this.readyState > 1)
382 throw new Error("Cannot set request header after sending");
383
384 this._requestHeaders[name] = value;
385 },
386
387 getResponseHeader: function(name)
388 {
389 name = name.toLowerCase();
390 if (!this._responseHeaders || !this._responseHeaders.hasOwnProperty(name))
391 return null;
392 else
393 return this._responseHeaders[name];
394 },
395
396 channel:
397 {
398 status: -1,
399 notificationCallbacks: {},
400 loadFlags: 0,
401 INHIBIT_CACHING: 0,
402 VALIDATE_ALWAYS: 0,
403 QueryInterface: function()
404 {
405 return this;
406 }
293 } 407 }
294 }; 408 };
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