Bug 1497909 - Properly remove preview when pdf is disabled. r=Gijs
authorMichael Kaply <mozilla@kaply.com>
Wed, 13 Nov 2019 17:16:41 +0000
changeset 501773 291ef4ef1773d3a6cf138975185cdef75010e76b
parent 501772 2f0634afb2f364a2a82afbb03d8d89d99bab1f61
child 501774 80ecfe77072130f16fe643a588ae534c8819cc8a
push id114172
push userdluca@mozilla.com
push dateTue, 19 Nov 2019 11:31:10 +0000
treeherdermozilla-inbound@b5c5ba07d3db [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersGijs
bugs1497909
milestone72.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 1497909 - Properly remove preview when pdf is disabled. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D14777
browser/extensions/pdfjs/content/PdfJs.jsm
--- a/browser/extensions/pdfjs/content/PdfJs.jsm
+++ b/browser/extensions/pdfjs/content/PdfJs.jsm
@@ -34,16 +34,22 @@ const { Services } = ChromeUtils.import(
 
 var Svc = {};
 XPCOMUtils.defineLazyServiceGetter(
   Svc,
   "mime",
   "@mozilla.org/mime;1",
   "nsIMIMEService"
 );
+XPCOMUtils.defineLazyServiceGetter(
+  Svc,
+  "handlerService",
+  "@mozilla.org/uriloader/handler-service;1",
+  "nsIHandlerService"
+);
 ChromeUtils.defineModuleGetter(
   this,
   "PdfjsChromeUtils",
   "resource://pdf.js/PdfjsChromeUtils.jsm"
 );
 ChromeUtils.defineModuleGetter(
   this,
   "PdfJsDefaultPreferences",
@@ -94,17 +100,19 @@ var PdfJs = {
   },
 
   initPrefs: function initPrefs() {
     if (this._initialized) {
       return;
     }
     this._initialized = true;
 
-    if (!Services.prefs.getBoolPref(PREF_DISABLED, true)) {
+    if (Services.prefs.getBoolPref(PREF_DISABLED, false)) {
+      this._unbecomeHandler();
+    } else {
       this._migrate();
     }
 
     // Listen for when pdf.js is completely disabled or a different pdf handler
     // is chosen.
     Services.prefs.addObserver(PREF_DISABLED, this);
     Services.obs.addObserver(this, TOPIC_PDFJS_HANDLER_CHANGED);
 
@@ -136,33 +144,50 @@ var PdfJs = {
     Services.prefs.setIntPref(PREF_MIGRATION_VERSION, VERSION);
   },
 
   _becomeHandler: function _becomeHandler() {
     let handlerInfo = Svc.mime.getFromTypeAndExtension(PDF_CONTENT_TYPE, "pdf");
     let prefs = Services.prefs;
     if (
       handlerInfo.preferredAction !== Ci.nsIHandlerInfo.handleInternally &&
-      handlerInfo.preferredAction !== false
+      handlerInfo.alwaysAskBeforeHandling !== false
     ) {
       // Store the previous settings of preferredAction and
       // alwaysAskBeforeHandling in case we need to revert them in a hotfix that
       // would turn pdf.js off.
       prefs.setIntPref(PREF_PREVIOUS_ACTION, handlerInfo.preferredAction);
       prefs.setBoolPref(PREF_PREVIOUS_ASK, handlerInfo.alwaysAskBeforeHandling);
     }
 
-    let handlerService = Cc[
-      "@mozilla.org/uriloader/handler-service;1"
-    ].getService(Ci.nsIHandlerService);
-
     // Change and save mime handler settings.
     handlerInfo.alwaysAskBeforeHandling = false;
     handlerInfo.preferredAction = Ci.nsIHandlerInfo.handleInternally;
-    handlerService.store(handlerInfo);
+    Svc.handlerService.store(handlerInfo);
+  },
+
+  _unbecomeHandler: function _unbecomeHandler() {
+    let handlerInfo = Svc.mime.getFromTypeAndExtension(PDF_CONTENT_TYPE, "pdf");
+    if (handlerInfo.preferredAction === Ci.nsIHandlerInfo.handleInternally) {
+      // If PDFJS is disabled, but we're still marked to handleInternally,
+      // either put it back to what it was, or remove it.
+      if (Services.prefs.prefHasUserValue(PREF_PREVIOUS_ACTION)) {
+        handlerInfo.preferredAction = Services.prefs.getIntPref(
+          PREF_PREVIOUS_ACTION
+        );
+        handlerInfo.alwaysAskBeforeHandling = Services.prefs.getBoolPref(
+          PREF_PREVIOUS_ASK
+        );
+        Svc.handlerService.store(handlerInfo);
+      } else {
+        Svc.handlerService.remove(handlerInfo);
+        // Clear migration pref so the handler comes back if reenabled
+        Services.prefs.clearIntPref(PREF_MIGRATION_VERSION);
+      }
+    }
   },
 
   _isEnabled: function _isEnabled() {
     let { processType, PROCESS_TYPE_DEFAULT } = Services.appinfo;
     if (processType !== PROCESS_TYPE_DEFAULT) {
       throw new Error(
         "isEnabled should only get called in the parent process."
       );