Bug 1547041 - Update and tidy SeaMonkey application preferences - Part 3: use lazy service getters. r=frg
authorIan Neal <iann_cvs@blueyonder.co.uk>
Sat, 22 Jun 2019 11:04:33 +0200
changeset 35923 aedfbb684ce8128dff40ab4619599393a3e44edd
parent 35922 60e3e0e72160bfd8bf8c183428ab38eb11ac4a9b
child 35924 9bbc721229fddcf5f209bbc33f4449ce58211647
push id392
push userclokep@gmail.com
push dateMon, 02 Sep 2019 20:17:19 +0000
reviewersfrg
bugs1547041
Bug 1547041 - Update and tidy SeaMonkey application preferences - Part 3: use lazy service getters. r=frg
suite/components/pref/content/pref-applications.js
--- a/suite/components/pref/content/pref-applications.js
+++ b/suite/components/pref/content/pref-applications.js
@@ -1,38 +1,32 @@
 /* -*- Mode: Java; tab-width: 2; c-basic-offset: 2; -*-
  *
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 const {AppConstants} = ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
+const {ShellService} = ChromeUtils.import("resource:///modules/ShellService.jsm");
 
 function Startup()
 {
   gApplicationsPane.init();
 }
 
+XPCOMUtils.defineLazyServiceGetters(this, {
+  gCategoryManager: ["@mozilla.org/categorymanager;1", "nsICategoryManager"],
+  gHandlerService: ["@mozilla.org/uriloader/handler-service;1", "nsIHandlerService"],
+  gMIMEService: ["@mozilla.org/mime;1", "nsIMIMEService"],
+  gWebContentConverterService: ["@mozilla.org/embeddor.implemented/web-content-handler-registrar;1", "nsIWebContentConverterService"],
+});
+
 //****************************************************************************//
 // Constants & Enumeration Values
 
-// global services
-var handlerSvc = Cc["@mozilla.org/uriloader/handler-service;1"]
-                   .getService(Ci.nsIHandlerService);
-var categoryMgr = Cc["@mozilla.org/categorymanager;1"]
-                    .getService(Ci.nsICategoryManager);
-var mimeSvc = Cc["@mozilla.org/mime;1"]
-                .getService(Ci.nsIMIMEService);
-var converterSvc = Cc["@mozilla.org/embeddor.implemented/web-content-handler-registrar;1"]
-                     .getService(Ci.nsIWebContentConverterService);
-var shellSvc = null;
-if ("@mozilla.org/suite/shell-service;1" in Cc)
-  shellSvc = Cc["@mozilla.org/suite/shell-service;1"]
-               .getService(Ci.nsIShellService);
-
 const TYPE_MAYBE_FEED = "application/vnd.mozilla.maybe.feed";
 const TYPE_MAYBE_VIDEO_FEED = "application/vnd.mozilla.maybe.video.feed";
 const TYPE_MAYBE_AUDIO_FEED = "application/vnd.mozilla.maybe.audio.feed";
 
 const PREF_DISABLED_PLUGIN_TYPES = "plugin.disable_full_page_plugin_for_types";
 
 // Preferences that affect which entries to show in the list.
 const PREF_SHOW_PLUGINS_IN_LIST = "browser.download.show_plugins_in_list";
@@ -372,45 +366,45 @@ HandlerInfoWrapper.prototype = {
 
     if (!disabledPluginTypes.includes(this.type))
       disabledPluginTypes.push(this.type);
 
     Services.prefs.setCharPref(PREF_DISABLED_PLUGIN_TYPES,
                                disabledPluginTypes.join(","));
 
     // Update the category manager so existing browser windows update.
-    categoryMgr.deleteCategoryEntry("Gecko-Content-Viewers",
-                                          this.type,
-                                          false);
+    gCategoryManager.deleteCategoryEntry("Gecko-Content-Viewers",
+                                         this.type,
+                                         false);
   },
 
   enablePluginType() {
     var disabledPluginTypes = this._getDisabledPluginTypes();
 
     var type = this.type;
     disabledPluginTypes = disabledPluginTypes.filter(v => v != type);
 
     Services.prefs.setCharPref(PREF_DISABLED_PLUGIN_TYPES,
                                disabledPluginTypes.join(","));
 
     // Update the category manager so existing browser windows update.
-    categoryMgr.
-      addCategoryEntry("Gecko-Content-Viewers",
+    gCategoryManager.addCategoryEntry(
+                       "Gecko-Content-Viewers",
                        this.type,
                        "@mozilla.org/content/plugin/document-loader-factory;1",
                        false,
                        true);
   },
 
 
   //**************************************************************************//
   // Storage
 
   store() {
-    handlerSvc.store(this.wrappedHandlerInfo);
+    gHandlerService.store(this.wrappedHandlerInfo);
   },
 
 
   //**************************************************************************//
   // Icons
 
   get smallIcon() {
     return this._getIcon(16);
@@ -478,17 +472,17 @@ FeedHandlerInfo.prototype = {
           return getLocalHandlerApp(file);
 
         return null;
 
       case "web":
         var uri = document.getElementById(this._prefSelectedWeb).value;
         if (!uri)
           return null;
-        return converterSvc.getWebContentHandlerByURI(this.type, uri);
+        return gWebContentConverterService.getWebContentHandlerByURI(this.type, uri);
 
       case "messenger":
       default:
         // When the pref is set to messenger, we handle feeds internally,
         // we don't forward them to a local or web handler app, so there is
         // no preferred handler.
         return null;
     }
@@ -503,17 +497,17 @@ FeedHandlerInfo.prototype = {
       document.getElementById(this._prefSelectedWeb).value = aNewValue.uri;
       document.getElementById(this._prefSelectedReader).value = "web";
       // Make the web handler be the new "auto handler" for feeds.
       // Note: we don't have to unregister the auto handler when the user picks
       // a non-web handler (local app, RSS News & Blogs, etc.) because the service
       // only uses the "auto handler" when the selected reader is a web handler.
       // We also don't have to unregister it when the user turns on "always ask"
       // (i.e. preview in browser), since that also overrides the auto handler.
-      converterSvc.setAutoHandler(this.type, aNewValue);
+      gWebContentConverterService.setAutoHandler(this.type, aNewValue);
     }
   },
 
   _possibleApplicationHandlers: null,
 
   get possibleApplicationHandlers() {
     if (this._possibleApplicationHandlers)
       return this._possibleApplicationHandlers;
@@ -563,31 +557,31 @@ FeedHandlerInfo.prototype = {
     if (preferredAppFile) {
       let preferredApp = getLocalHandlerApp(preferredAppFile);
       let defaultApp = this._defaultApplicationHandler;
       if (!defaultApp || !defaultApp.equals(preferredApp))
         this._possibleApplicationHandlers.appendElement(preferredApp);
     }
 
     // Add the registered web handlers.  There can be any number of these.
-    var webHandlers = converterSvc.getContentHandlers(this.type);
+    var webHandlers = gWebContentConverterService.getContentHandlers(this.type);
     for (let webHandler of webHandlers)
       this._possibleApplicationHandlers.appendElement(webHandler);
 
     return this._possibleApplicationHandlers;
   },
 
   __defaultApplicationHandler: undefined,
   get _defaultApplicationHandler() {
     if (this.__defaultApplicationHandler !== undefined)
       return this.__defaultApplicationHandler;
 
     var defaultFeedReader = null;
     try {
-      defaultFeedReader = shellSvc.defaultFeedReader;
+      defaultFeedReader = ShellService.defaultFeedReader;
     }
     catch(ex) {
       // no default reader
     }
 
     if (defaultFeedReader) {
       let handlerApp = Cc["@mozilla.org/uriloader/local-handler-app;1"]
                          .createInstance(Ci.nsIHandlerApp);
@@ -601,17 +595,17 @@ FeedHandlerInfo.prototype = {
       this.__defaultApplicationHandler = null;
     }
 
     return this.__defaultApplicationHandler;
   },
 
   get hasDefaultHandler() {
     try {
-      if (shellSvc.defaultFeedReader)
+      if (ShellService.defaultFeedReader)
         return true;
     }
     catch(ex) {
       // no default reader
     }
 
     return false;
   },
@@ -720,17 +714,18 @@ FeedHandlerInfo.prototype = {
         if (preferredAppFile) {
           let preferredApp = getLocalHandlerApp(preferredAppFile);
           if (app.equals(preferredApp))
             pref.reset();
         }
       }
       else {
         app.QueryInterface(Ci.nsIWebContentHandlerInfo);
-        converterSvc.removeContentHandler(app.contentType, app.uri);
+        gWebContentConverterService.removeContentHandler(app.contentType,
+                                                         app.uri);
       }
     }
     this._possibleApplicationHandlers._removed = [];
   },
 
 
   //**************************************************************************//
   // Icons
@@ -997,32 +992,32 @@ var gApplicationsPane = {
                        .getService(Ci.nsIPluginHost);
     for (let pluginTag of pluginHost.getPluginTags()) {
       for (let type of pluginTag.getMimeTypes()) {
         let handlerInfoWrapper;
         if (type in this._handledTypes)
           handlerInfoWrapper = this._handledTypes[type];
         else {
           let wrappedHandlerInfo =
-            mimeSvc.getFromTypeAndExtension(type, null);
+            gMIMEService.getFromTypeAndExtension(type, null);
           handlerInfoWrapper = new HandlerInfoWrapper(type, wrappedHandlerInfo);
           handlerInfoWrapper.handledOnlyByPlugin = true;
           this._handledTypes[type] = handlerInfoWrapper;
         }
 
         handlerInfoWrapper.plugin = pluginHost.getPluginTagForType(type);
       }
     }
   },
 
   /**
    * Load the set of handlers defined by the application datastore.
    */
   _loadApplicationHandlers() {
-    var wrappedHandlerInfos = handlerSvc.enumerate();
+    var wrappedHandlerInfos = gHandlerService.enumerate();
     while (wrappedHandlerInfos.hasMoreElements()) {
       let wrappedHandlerInfo =
         wrappedHandlerInfos.getNext().QueryInterface(Ci.nsIHandlerInfo);
       let type = wrappedHandlerInfo.type;
 
       let handlerInfoWrapper;
       if (type in this._handledTypes)
         handlerInfoWrapper = this._handledTypes[type];
@@ -1415,17 +1410,17 @@ var gApplicationsPane = {
       menuPopup.appendChild(pluginMenuItem);
     }
 
     // Create a menu item for selecting a local application.
     let canOpenWithOtherApp = true;
     if (AppConstants.platform == "win") {
       // On Windows, selecting an application to open another application
       // would be meaningless so we special case executables.
-      let executableType = mimeSvc.getTypeFromExtension("exe");
+      let executableType = gMIMEService.getTypeFromExtension("exe");
       canOpenWithOtherApp = handlerInfo.type != executableType;
     }
     if (canOpenWithOtherApp)
     {
       let menuItem = document.createElement("menuitem");
       menuItem.setAttribute("class", "handler-action");
       menuItem.setAttribute("value", kActionChooseApp);
       let label = this._prefsBundle.getString("useOtherApp");
@@ -1631,18 +1626,19 @@ var gApplicationsPane = {
     }.bind(this);
 
     if (AppConstants.platform == "win") {
       let params = {};
       let handlerInfo = this._handledTypes[this._list.selectedItem.type];
 
       if (isFeedType(handlerInfo.type)) {
         // MIME info will be null, create a temp object.
-        params.mimeInfo = mimeSvc.getFromTypeAndExtension(handlerInfo.type,
-                                                   handlerInfo.primaryExtension);
+        params.mimeInfo =
+            gMIMEService.getFromTypeAndExtension(handlerInfo.type,
+                                                 handlerInfo.primaryExtension);
       } else {
         params.mimeInfo = handlerInfo.wrappedHandlerInfo;
       }
 
       params.title         = this._prefsBundle.getString("fpTitleChooseApp");
       params.description   = handlerInfo.description;
       params.filename      = null;
       params.handlerApp    = null;