Bug 802668 - Use Service.jsm in toolkit/content/contentAreaUtils.js. r=gavin
authorOHZEKI Tetsuharu <saneyuki.s.snyk@gmail.com>
Thu, 18 Oct 2012 15:44:36 +0100
changeset 110803 389e0c33ddeffc66ca0ca4f7ad014e10f5614cac
parent 110802 50083503d2e066d8bac8005c870b4fc945917ef0
child 110804 f5bbdc2a1b3d01aaf1332f68352ce2273c862293
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersgavin
bugs802668
milestone19.0a1
Bug 802668 - Use Service.jsm in toolkit/content/contentAreaUtils.js. r=gavin
toolkit/content/contentAreaUtils.js
--- a/toolkit/content/contentAreaUtils.js
+++ b/toolkit/content/contentAreaUtils.js
@@ -1,29 +1,27 @@
 # -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; 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/.
 
+Components.utils.import("resource://gre/modules/Services.jsm");
 Components.utils.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
 
 var ContentAreaUtils = {
+
+  // this is for backwards compatibility.
   get ioService() {
-    delete this.ioService;
-    return this.ioService =
-      Components.classes["@mozilla.org/network/io-service;1"]
-                .getService(Components.interfaces.nsIIOService);
+    return Services.io;
   },
 
   get stringBundle() {
     delete this.stringBundle;
     return this.stringBundle =
-      Components.classes["@mozilla.org/intl/stringbundle;1"]
-                .getService(Components.interfaces.nsIStringBundleService)
-                .createBundle("chrome://global/locale/contentAreaCommands.properties");
+      Services.strings.createBundle("chrome://global/locale/contentAreaCommands.properties");
   }
 }
 
 /**
  * urlSecurityCheck: JavaScript wrapper for checkLoadURIWithPrincipal
  * and checkLoadURIStrWithPrincipal.
  * If |aPrincipal| is not allowed to link to |aURL|, this function throws with
  * an error message.
@@ -34,22 +32,20 @@ var ContentAreaUtils = {
  * @param aPrincipal
  *        The principal of the document from which aURL came.
  * @param aFlags
  *        Flags to be passed to checkLoadURIStr. If undefined,
  *        nsIScriptSecurityManager.STANDARD will be passed.
  */
 function urlSecurityCheck(aURL, aPrincipal, aFlags)
 {
-  const nsIScriptSecurityManager =
-    Components.interfaces.nsIScriptSecurityManager;
-  var secMan = Components.classes["@mozilla.org/scriptsecuritymanager;1"]
-                         .getService(nsIScriptSecurityManager);
-  if (aFlags === undefined)
-    aFlags = nsIScriptSecurityManager.STANDARD;
+  var secMan = Services.scriptSecurityManager;
+  if (aFlags === undefined) {
+    aFlags = secMan.STANDARD;
+  }
 
   try {
     if (aURL instanceof Components.interfaces.nsIURI)
       secMan.checkLoadURIWithPrincipal(aPrincipal, aURL, aFlags);
     else
       secMan.checkLoadURIStrWithPrincipal(aPrincipal, aURL, aFlags);
   } catch (e) {
     let principalStr = "";
@@ -540,28 +536,26 @@ function initFileInfo(aFI, aURL, aURLCha
  *         was not displayed; false if they dismissed the picker.
  */
 function getTargetFile(aFpP, /* optional */ aSkipPrompt, /* optional */ aRelatedURI)
 {
   if (!getTargetFile.gDownloadLastDir)
     Components.utils.import("resource://gre/modules/DownloadLastDir.jsm", getTargetFile);
   var gDownloadLastDir = getTargetFile.gDownloadLastDir;
 
-  var prefs = getPrefsBrowserDownload("browser.download.");
+  var prefs = Services.prefs.getBranch("browser.download.");
   var useDownloadDir = prefs.getBoolPref("useDownloadDir");
   const nsILocalFile = Components.interfaces.nsILocalFile;
 
   if (!aSkipPrompt)
     useDownloadDir = false;
 
   // Default to the user's default downloads directory configured
   // through download prefs.
-  var dlMgr = Components.classes["@mozilla.org/download-manager;1"]
-                        .getService(Components.interfaces.nsIDownloadManager);
-  var dir = dlMgr.userDownloadsDirectory;
+  var dir = Services.downloads.userDownloadsDirectory;
   var dirExists = dir && dir.exists();
 
   if (useDownloadDir && dirExists) {
     dir.append(getNormalizedLeafName(aFpP.fileInfo.fileName,
                                      aFpP.fileInfo.fileExt));
     aFpP.file = uniqueFile(dir);
     return true;
   }
@@ -577,19 +571,17 @@ function getTargetFile(aFpP, /* optional
     if (lastDir.exists()) {
       dir = lastDir;
       dirExists = true;
     }
   } catch(e) {}
 
   if (!dirExists) {
     // Default to desktop.
-    var fileLocator = Components.classes["@mozilla.org/file/directory_service;1"]
-                                .getService(Components.interfaces.nsIProperties);
-    dir = fileLocator.get("Desk", nsILocalFile);
+    dir = Services.dirsvc.get("Desk", nsILocalFile);
   }
 
   var fp = makeFilePicker();
   var titleKey = aFpP.fpTitleKey || "SaveLinkTitle";
   fp.init(window, ContentAreaUtils.stringBundle.GetStringFromName(titleKey),
           Components.interfaces.nsIFilePicker.modeSave);
 
   fp.displayDirectory = dir;
@@ -615,17 +607,17 @@ function getTargetFile(aFpP, /* optional
   if (aFpP.saveMode != SAVEMODE_FILEONLY)
     prefs.setIntPref("save_converter_index", fp.filterIndex);
 
   // Do not store the last save directory as a pref inside the private browsing mode
   var directory = fp.file.parent.QueryInterface(nsILocalFile);
   gDownloadLastDir.setFile(aRelatedURI, directory);
 
   fp.file.leafName = validateFileName(fp.file.leafName);
-  
+
   aFpP.saveAsType = fp.filterIndex;
   aFpP.file = fp.file;
   aFpP.fileURL = fp.fileURL;
   return true;
 }
 
 // Since we're automatically downloading, we don't get the file picker's
 // logic to check for existing files, so we need to do that here.
@@ -753,46 +745,38 @@ function getPostData(aDocument)
                  .QueryInterface(Components.interfaces.nsISHEntry);
     return sessionHistoryEntry.postData;
   }
   catch (e) {
   }
   return null;
 }
 
-// Get the preferences branch ("browser.download." for normal 'save' mode)...
-function getPrefsBrowserDownload(branch)
-{
-  const prefSvcContractID = "@mozilla.org/preferences-service;1";
-  const prefSvcIID = Components.interfaces.nsIPrefService;                              
-  return Components.classes[prefSvcContractID].getService(prefSvcIID).getBranch(branch);
-}
-
 function makeWebBrowserPersist()
 {
   const persistContractID = "@mozilla.org/embedding/browser/nsWebBrowserPersist;1";
   const persistIID = Components.interfaces.nsIWebBrowserPersist;
   return Components.classes[persistContractID].createInstance(persistIID);
 }
 
 /**
  * Constructs a new URI, using nsIIOService.
  * @param aURL The URI spec.
  * @param aOriginCharset The charset of the URI.
  * @param aBaseURI Base URI to resolve aURL, or null.
  * @return an nsIURI object based on aURL.
  */
 function makeURI(aURL, aOriginCharset, aBaseURI)
 {
-  return ContentAreaUtils.ioService.newURI(aURL, aOriginCharset, aBaseURI);
+  return Services.io.newURI(aURL, aOriginCharset, aBaseURI);
 }
 
 function makeFileURI(aFile)
 {
-  return ContentAreaUtils.ioService.newFileURI(aFile);
+  return Services.io.newFileURI(aFile);
 }
 
 function makeFilePicker()
 {
   const fpContractID = "@mozilla.org/filepicker;1";
   const fpIID = Components.interfaces.nsIFilePicker;
   return Components.classes[fpContractID].createInstance(fpIID);
 }
@@ -1083,31 +1067,28 @@ function openURL(aURL)
   var protocolSvc = Components.classes["@mozilla.org/uriloader/external-protocol-service;1"]
                               .getService(Components.interfaces.nsIExternalProtocolService);
 
   if (!protocolSvc.isExposedProtocol(uri.scheme)) {
     // If we're not a browser, use the external protocol service to load the URI.
     protocolSvc.loadUrl(uri);
   }
   else {
-    var wmSvc = Components.classes["@mozilla.org/appshell/window-mediator;1"]
-                          .getService(Components.interfaces.nsIWindowMediator);
-    var recentWindow = wmSvc.getMostRecentWindow("navigator:browser");
+    var recentWindow = Services.wm.getMostRecentWindow("navigator:browser");
     if (recentWindow) {
       var win = recentWindow.browserDOMWindow.openURI(uri, null,
                                                       recentWindow.browserDOMWindow.OPEN_DEFAULTWINDOW,
                                                       recentWindow.browserDOMWindow.OPEN_NEW);
       win.focus();
       return;
     }
 
     var loadgroup = Components.classes["@mozilla.org/network/load-group;1"]
                               .createInstance(Components.interfaces.nsILoadGroup);
-    var appstartup = Components.classes["@mozilla.org/toolkit/app-startup;1"]
-                               .getService(Components.interfaces.nsIAppStartup);
+    var appstartup = Services.startup;
 
     var loadListener = {
       onStartRequest: function ll_start(aRequest, aContext) {
         appstartup.enterLastWindowClosingSurvivalArea();
       },
       onStopRequest: function ll_stop(aRequest, aContext, aStatusCode) {
         appstartup.exitLastWindowClosingSurvivalArea();
       },
@@ -1132,14 +1113,14 @@ function openURL(aURL)
         if (iid.equals(Components.interfaces.nsIURIContentListener))
           return this;
         if (iid.equals(Components.interfaces.nsILoadGroup))
           return loadgroup;
         throw Components.results.NS_ERROR_NO_INTERFACE;
       }
     }
 
-    var channel = ContentAreaUtils.ioService.newChannelFromURI(uri);
+    var channel = Services.io.newChannelFromURI(uri);
     var uriLoader = Components.classes["@mozilla.org/uriloader;1"]
                               .getService(Components.interfaces.nsIURILoader);
     uriLoader.openURI(channel, true, uriListener);
   }
 }