Bug 1399864 - Send If-Modified-Since header when polling for blocklist.xml changes, r=florian
authorMathieu Leplatre <mathieu@mozilla.com>
Wed, 31 Jan 2018 11:45:53 +0000
changeset 402052 8f9dcb9c62de9ef2ab82f42b7c5e5984d38bf55c
parent 402051 f8a8160484c854b2805bd995dfecf64d3b024d76
child 402053 b4a42010118a987173f6b82dfb569c743f795d4c
push id33361
push userncsoregi@mozilla.com
push dateThu, 01 Feb 2018 18:09:52 +0000
treeherdermozilla-central@946b943acc04 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersflorian
bugs1399864
milestone60.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
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.