Bug 1399864 - Send If-Modified-Since header when polling for blocklist.xml changes, r?florian draft
authorMathieu Leplatre <mathieu@mozilla.com>
Wed, 31 Jan 2018 11:45:53 +0000
changeset 749405 3721c10647da3c5d533bdad27320861f0d4c8256
parent 749389 7b46ef2ae1412b15ed45e7d2367ca491344729f7
push id97385
push usergijskruitbosch@gmail.com
push dateWed, 31 Jan 2018 12:21:51 +0000
reviewersflorian
bugs1399864
milestone60.0a1
Bug 1399864 - Send If-Modified-Since header when polling for blocklist.xml changes, r?florian MozReview-Commit-ID: oAM9lDhtlt
toolkit/mozapps/extensions/nsBlocklistService.js
--- a/toolkit/mozapps/extensions/nsBlocklistService.js
+++ b/toolkit/mozapps/extensions/nsBlocklistService.js
@@ -42,16 +42,17 @@ ChromeUtils.defineModuleGetter(Blocklist
 const TOOLKIT_ID                      = "toolkit@mozilla.org";
 const KEY_PROFILEDIR                  = "ProfD";
 const KEY_APPDIR                      = "XCurProcD";
 const FILE_BLOCKLIST                  = "blocklist.xml";
 const PREF_BLOCKLIST_LASTUPDATETIME   = "app.update.lastUpdateTime.blocklist-background-update-timer";
 const PREF_BLOCKLIST_URL              = "extensions.blocklist.url";
 const PREF_BLOCKLIST_ITEM_URL         = "extensions.blocklist.itemURL";
 const PREF_BLOCKLIST_ENABLED          = "extensions.blocklist.enabled";
+const PREF_BLOCKLIST_LAST_MODIFIED    = "extensions.blocklist.lastModified";
 const PREF_BLOCKLIST_LEVEL            = "extensions.blocklist.level";
 const PREF_BLOCKLIST_PINGCOUNTTOTAL   = "extensions.blocklist.pingCountTotal";
 const PREF_BLOCKLIST_PINGCOUNTVERSION = "extensions.blocklist.pingCountVersion";
 const PREF_BLOCKLIST_SUPPRESSUI       = "extensions.blocklist.suppressUI";
 const PREF_BLOCKLIST_UPDATE_ENABLED   = "services.blocklist.update_enabled";
 const PREF_APP_DISTRIBUTION           = "distribution.id";
 const PREF_APP_DISTRIBUTION_VERSION   = "distribution.version";
 const PREF_EM_LOGGING_ENABLED         = "extensions.logging.enabled";
@@ -574,17 +575,25 @@ Blocklist.prototype = {
       return;
     }
 
     LOG("Blocklist::notify: Requesting " + uri.spec);
     let request = new ServiceRequest();
     request.open("GET", uri.spec, true);
     request.channel.notificationCallbacks = new gCertUtils.BadCertHandler();
     request.overrideMimeType("text/xml");
-    request.setRequestHeader("Cache-Control", "no-cache");
+
+    // The server will return a `304 Not Modified` response if the blocklist was
+    // not changed since last check.
+    const lastModified = Services.prefs.getCharPref(PREF_BLOCKLIST_LAST_MODIFIED, "");
+    if (lastModified) {
+      request.setRequestHeader("If-Modified-Since", lastModified);
+    } else {
+      request.setRequestHeader("Cache-Control", "no-cache");
+    }
 
     request.addEventListener("error", event => this.onXMLError(event));
     request.addEventListener("load", event => this.onXMLLoad(event));
     request.send(null);
 
     // When the blocklist loads we need to compare it to the current copy so
     // make sure we have loaded it.
     if (!this._isBlocklistLoaded())
@@ -602,23 +611,33 @@ Blocklist.prototype = {
   async onXMLLoad(aEvent) {
     let request = aEvent.target;
     try {
       gCertUtils.checkCert(request.channel);
     } catch (e) {
       LOG("Blocklist::onXMLLoad: " + e);
       return;
     }
+
+    if (request.status == 304) {
+      LOG("Blocklist::onXMLLoad: up to date.");
+      return;
+    }
+
     let responseXML = request.responseXML;
     if (!responseXML || responseXML.documentElement.namespaceURI == XMLURI_PARSE_ERROR ||
         (request.status != 200 && request.status != 0)) {
       LOG("Blocklist::onXMLLoad: there was an error during load");
       return;
     }
 
+    // Save current blocklist timestamp to pref.
+    const lastModified = request.getResponseHeader("Last-Modified") || "";
+    Services.prefs.setCharPref(PREF_BLOCKLIST_LAST_MODIFIED, lastModified);
+
     var oldAddonEntries = this._addonEntries;
     var oldPluginEntries = this._pluginEntries;
     this._addonEntries = [];
     this._gfxEntries = [];
     this._pluginEntries = [];
 
     this._loadBlocklistFromString(request.responseText);
     // We don't inform the users when the graphics blocklist changed at runtime.