home *** CD-ROM | disk | FTP | other *** search
/ Minami 83 / MINAMI83.iso / Extra / DivXInstaller.exe / $PLUGINSDIR / GoogleToolbarFirefox.msi / xpi / amulet-jslib / xml-fetcher.js < prev   
Text File  |  2006-08-07  |  3KB  |  80 lines

  1. function PROT_XMLHttpRequest() {
  2. var Cc = Components.classes;
  3. var Ci = Components.interfaces;
  4. var request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
  5. .createInstance(Ci.nsIXMLHttpRequest);
  6. request.QueryInterface(Ci.nsIJSXMLHttpRequest);
  7. return request;
  8. }
  9. function PROT_XMLFetcher(opt_stripCookies) {
  10. this.debugZone = "xmlfetcher";
  11. this._request = new PROT_XMLHttpRequest();
  12. this._stripCookies = !!opt_stripCookies;
  13. }
  14. PROT_XMLFetcher.prototype = {
  15. _callback: null,
  16. get: function(page, callback) {
  17. this._request.abort();                // abort() is asynchronous, so
  18. this._request = new PROT_XMLHttpRequest();
  19. this._callback = callback;
  20. var asynchronous = true;
  21. this._request.open("GET", page, asynchronous);
  22. if (this._stripCookies)
  23. new PROT_CookieStripper(this._request.channel);
  24. var self = this;
  25. this._request.onreadystatechange = function() {
  26. self.readyStateChange(self);
  27. }
  28. this._request.send(null);
  29. },
  30. readyStateChange: function(fetcher) {
  31. if (fetcher._request.readyState != 4) // TODO: check status code 200
  32. return;
  33. var responseText = null;
  34. try {
  35. G_Debug(this, "xml fetch status code: \"" +
  36. fetcher._request.status + "\"");
  37. var responseText = fetcher._request.responseText;
  38. } catch(e) {
  39. G_Debug(this, "Caught exception trying to read xmlhttprequest " +
  40. "status/response.");
  41. G_Debug(this, e);
  42. }
  43. if (fetcher._callback)
  44. fetcher._callback(responseText);
  45. }
  46. };
  47. function PROT_CookieStripper(channel) {
  48. this.debugZone = "cookiestripper";
  49. this.topic_ = "http-on-modify-request";
  50. this.channel_ = channel;
  51. var Cc = Components.classes;
  52. var Ci = Components.interfaces;
  53. this.observerService_ = Cc["@mozilla.org/observer-service;1"]
  54. .getService(Ci.nsIObserverService);
  55. this.observerService_.addObserver(this, this.topic_, false);
  56. var twentySeconds = 20 * 1000;
  57. this.alarm_ = new G_Alarm(BindToObject(this.stopObserving, this),
  58. twentySeconds);
  59. }
  60. PROT_CookieStripper.prototype.observe = function(subject, topic, data) {
  61. if (topic != this.topic_ || subject != this.channel_)
  62. return;
  63. G_Debug(this, "Stripping cookies for channel.");
  64. this.channel_.QueryInterface(Components.interfaces.nsIHttpChannel);
  65. this.channel_.setRequestHeader("Cookie", "", false /* replace, not add */);
  66. this.alarm_.cancel();
  67. this.stopObserving();
  68. }
  69. PROT_CookieStripper.prototype.stopObserving = function() {
  70. G_Debug(this, "Removing observer");
  71. this.observerService_.removeObserver(this, this.topic_);
  72. this.channel_ = this.alarm_ = this.observerService_ = null;
  73. }
  74. PROT_CookieStripper.prototype.QueryInterface = function(iid) {
  75. var Ci = Components.interfaces;
  76. if (iid.equals(Ci.nsISupports) || iid.equals(Ci.nsIObserve))
  77. return this;
  78. throw Components.results.NS_ERROR_NO_INTERFACE;
  79. }
  80.