Bug 897516 - Implement a separate cookie jar for safebrowsing - rekey part. r=mmc
authorChristoph Kerschbaumer <mozilla@christophkerschbaumer.com>
Mon, 19 Aug 2013 12:05:40 -0700
changeset 149561 51616efd18394ac41f2f046ec003a028f938eada
parent 149560 61817a92297cb5ee0c73f956fa2f7761bc34b4e6
child 149562 35c6e515634caf5a25b3b3cc690c4fdae92af5fd
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersmmc
bugs897516
milestone27.0a1
Bug 897516 - Implement a separate cookie jar for safebrowsing - rekey part. r=mmc
toolkit/components/url-classifier/content/xml-fetcher.js
--- a/toolkit/components/url-classifier/content/xml-fetcher.js
+++ b/toolkit/components/url-classifier/content/xml-fetcher.js
@@ -3,19 +3,16 @@
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 // A simple class that encapsulates a request. You'll notice the
 // style here is different from the rest of the extension; that's
 // because this was re-used from really old code we had. At some
 // point it might be nice to replace this with something better
 // (e.g., something that has explicit onerror handler, ability
 // to set headers, and so on).
-//
-// The only interesting thing here is its ability to strip cookies
-// from the request.
 
 /**
  * Because we might be in a component, we can't just assume that
  * XMLHttpRequest exists. So we use this tiny factory function to wrap the
  * XPCOM version.
  *
  * @return XMLHttpRequest object
  */
@@ -29,25 +26,30 @@ function PROT_NewXMLHttpRequest() {
   return request;
 }
 
 /**
  * A helper class that does HTTP GETs and calls back a function with
  * the content it receives. Asynchronous, so uses a closure for the
  * callback.
  *
- * @param opt_stripCookies Boolean indicating whether we should strip
- *                         cookies from this request
- * 
+ * Note, that XMLFetcher is only used for SafeBrowsing, therefore
+ * we inherit from nsILoadContext, so we can use the callbacks on the
+ * channel to separate the safebrowsing cookie based on a reserved
+ * appId.
  * @constructor
  */
-function PROT_XMLFetcher(opt_stripCookies) {
+function PROT_XMLFetcher() {
   this.debugZone = "xmlfetcher";
   this._request = PROT_NewXMLHttpRequest();
-  this._stripCookies = !!opt_stripCookies;
+  // implements nsILoadContext
+  this.appId = Ci.nsIScriptSecurityManager.SAFEBROWSING_APP_ID;
+  this.isInBrowserElement = false;
+  this.usePrivateBrowsing = false;
+  this.isContent = false;
 }
 
 PROT_XMLFetcher.prototype = {
   /**
    * Function that will be called back upon fetch completion.
    */
   _callback: null,
   
@@ -61,19 +63,16 @@ PROT_XMLFetcher.prototype = {
   get: function(page, callback) {
     this._request.abort();                // abort() is asynchronous, so
     this._request = PROT_NewXMLHttpRequest();
     this._callback = callback;
     var asynchronous = true;
     this._request.open("GET", page, asynchronous);
     this._request.channel.notificationCallbacks = this;
 
-    if (this._stripCookies)
-      new PROT_CookieStripper(this._request.channel);
-
     // Create a closure
     var self = this;
     this._request.addEventListener("readystatechange", function() {
       self.readyStateChange(self);
     }, false);
 
     this._request.send(null);
   },
@@ -112,78 +111,12 @@ PROT_XMLFetcher.prototype = {
       fetcher._callback(responseText, status);
   },
 
   // nsIInterfaceRequestor
   getInterface: function(iid) {
     return this.QueryInterface(iid);
   },
 
-  QueryInterface: function(iid) {
-    if (!iid.equals(Components.interfaces.nsIInterfaceRequestor) &&
-        !iid.equals(Components.interfaces.nsISupports))
-      throw Components.results.NS_ERROR_NO_INTERFACE;
-    return this;
-  }
+  QueryInterface: XPCOMUtils.generateQI([Ci.nsIInterfaceRequestor,
+                                         Ci.nsISupports,
+                                         Ci.nsILoadContext])
 };
-
-
-/**
- * This class knows how to strip cookies from an HTTP request. It
- * listens for http-on-modify-request, and modifies the request
- * accordingly. We can't do this using xmlhttprequest.setHeader() or
- * nsIChannel.setRequestHeader() before send()ing because the cookie
- * service is called after send().
- * 
- * @param channel nsIChannel in which the request is happening
- * @constructor
- */
-function PROT_CookieStripper(channel) {
-  this.debugZone = "cookiestripper";
-  this.topic_ = "http-on-modify-request";
-  this.channel_ = channel;
-
-  var Cc = Components.classes;
-  var Ci = Components.interfaces;
-  this.observerService_ = Cc["@mozilla.org/observer-service;1"]
-                          .getService(Ci.nsIObserverService);
-  this.observerService_.addObserver(this, this.topic_, false);
-
-  // If the request doesn't issue, don't hang around forever
-  var twentySeconds = 20 * 1000;
-  this.alarm_ = new G_Alarm(BindToObject(this.stopObserving, this), 
-                            twentySeconds);
-}
-
-/**
- * Invoked by the observerservice. See nsIObserve.
- */
-PROT_CookieStripper.prototype.observe = function(subject, topic, data) {
-  if (topic != this.topic_ || subject != this.channel_)
-    return;
-
-  G_Debug(this, "Stripping cookies for channel.");
-
-  this.channel_.QueryInterface(Components.interfaces.nsIHttpChannel);
-  this.channel_.setRequestHeader("Cookie", "", false /* replace, not add */);
-  this.alarm_.cancel();
-  this.stopObserving();
-}
-
-/**
- * Remove us from the observerservice
- */
-PROT_CookieStripper.prototype.stopObserving = function() {
-  G_Debug(this, "Removing observer");
-  this.observerService_.removeObserver(this, this.topic_);
-  this.channel_ = this.alarm_ = this.observerService_ = null;
-}
-
-/**
- * XPCOM cruft
- */
-PROT_CookieStripper.prototype.QueryInterface = function(iid) {
-  var Ci = Components.interfaces;
-  if (iid.equals(Ci.nsISupports) || iid.equals(Ci.nsIObserve))
-    return this;
-  throw Components.results.NS_ERROR_NO_INTERFACE;
-}
-