Bug 795158 - Switch to Services.jsm: /editor/ui/: Services.io. r=mconley, r=IanN
authorSebastian Hengst <archaeopteryx@coole-files.de>
Tue, 05 Feb 2013 00:28:55 +0100
changeset 14799 8a508a9ee4856e4c7f539d66634017529a2b5c14
parent 14798 65556fa263e6d432d6fff56124b25bd66a5aafe0
child 14800 e7f920afc8bd218f51fe5e49b66c95258b9663e7
push id867
push userbugzilla@standard8.plus.com
push dateMon, 01 Apr 2013 20:44:27 +0000
treeherdercomm-beta@797726b8d244 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmconley, IanN
bugs795158
Bug 795158 - Switch to Services.jsm: /editor/ui/: Services.io. r=mconley, r=IanN
editor/ui/composer/content/ComposerCommands.js
editor/ui/composer/content/editorApplicationOverlay.js
editor/ui/composer/content/editorUtilities.js
editor/ui/composer/content/publishprefs.js
editor/ui/dialogs/content/EdImageOverlay.js
--- a/editor/ui/composer/content/ComposerCommands.js
+++ b/editor/ui/composer/content/ComposerCommands.js
@@ -743,25 +743,24 @@ function GetSuggestedFileName(aDocumentU
   var extension = GetExtensionBasedOnMimeType(aMIMEType);
   if (extension)
     extension = "." + extension;
 
   // check for existing file name we can use
   if (aDocumentURLString && !IsUrlAboutBlank(aDocumentURLString))
   {
     try {
-      var ioService = GetIOService();
-      var docURI = ioService.newURI(aDocumentURLString,
+      let docURI = Services.io.newURI(aDocumentURLString,
         GetCurrentEditor().documentCharacterSet, null);
       docURI = docURI.QueryInterface(Components.interfaces.nsIURL);
 
       // grab the file name
-      var url = validateFileName(decodeURIComponent(docURI.fileBaseName));
+      let url = validateFileName(decodeURIComponent(docURI.fileBaseName));
       if (url)
-        return url+extension;
+        return url + extension;
     } catch(e) {}
   } 
 
   // Check if there is a title we can use to generate a valid filename,
   // if we can't, use the default filename.
   var title = validateFileName(GetDocumentTitle()) ||
               GetString("untitledDefaultFilename");
   return title + extension;
@@ -800,22 +799,21 @@ function PromptForSaveLocation(aDoSaveAs
   // now let's actually set the filepicker's suggested filename
   var suggestedFileName = GetSuggestedFileName(aDocumentURLString, aMIMEType);
   if (suggestedFileName)
     fp.defaultString = suggestedFileName;
 
   // set the file picker's current directory
   // assuming we have information needed (like prior saved location)
   try {
-    var ioService = GetIOService();
     var fileHandler = GetFileProtocolHandler();
     
     var isLocalFile = true;
     try {
-      var docURI = ioService.newURI(aDocumentURLString, GetCurrentEditor().documentCharacterSet, null);
+      let docURI = Services.io.newURI(aDocumentURLString, GetCurrentEditor().documentCharacterSet, null);
       isLocalFile = docURI.schemeIs("file");
     }
     catch (e) {}
 
     var parentLocation = null;
     if (isLocalFile)
     {
       var fileLocation = fileHandler.getFileFromURLSpec(aDocumentURLString); // this asserts if url is not local
@@ -1178,17 +1176,17 @@ var gEditorOutputProgressListener =
 
         if (gPersistObj.result == 0)
         {
           // All files are finished and publishing succeeded (some images may have failed)
           try {
             // Make a new docURI from the "browse location" in case "publish location" was FTP
             // We need to set document uri before notifying listeners
             var docUrl = GetDocUrlFromPublishData(gPublishData);
-            SetDocumentURI(GetIOService().newURI(docUrl, editor.documentCharacterSet, null));
+            SetDocumentURI(Services.io.newURI(docUrl, editor.documentCharacterSet, null));
 
             UpdateWindowTitle();
 
             // this should cause notification to listeners that doc has changed
             editor.resetModificationCount();
 
             // Set UI based on whether we're editing a remote or local url
             SetSaveAndPublishUI(urlstring);
@@ -1679,25 +1677,23 @@ function SaveDocument(aSaveAs, aSaveCopy
         doUpdateURI = true;
     } catch (e) {
        Components.utils.reportError(e);
        return false; 
     }
   } // mustShowFileDialog
 
   var success = true;
-  var ioService;
   try {
     // if somehow we didn't get a local file but we did get a uri, 
     // attempt to create the localfile if it's a "file" url
     var docURI;
     if (!tempLocalFile)
     {
-      ioService = GetIOService();
-      docURI = ioService.newURI(urlstring, editor.documentCharacterSet, null);
+      docURI = Services.io.newURI(urlstring, editor.documentCharacterSet, null);
       
       if (docURI.schemeIs("file"))
       {
         var fileHandler = GetFileProtocolHandler();
         tempLocalFile = fileHandler.getFileFromURLSpec(urlstring).QueryInterface(Components.interfaces.nsILocalFile);
       }
     }
 
@@ -1729,18 +1725,17 @@ function SaveDocument(aSaveAs, aSaveCopy
             relatedFilesDir = tempLocalFile.parent;
         }
         else
         {
           var lastSlash = urlstring.lastIndexOf("\/");
           if (lastSlash != -1)
           {
             var relatedFilesDirString = urlstring.slice(0, lastSlash + 1);  // include last slash
-            ioService = GetIOService();
-            relatedFilesDir = ioService.newURI(relatedFilesDirString, editor.documentCharacterSet, null);
+            relatedFilesDir = Services.io.newURI(relatedFilesDirString, editor.documentCharacterSet, null);
           }
         }
       } catch(e) { relatedFilesDir = null; }
     }
 
     var destinationLocation;
     if (tempLocalFile)
       destinationLocation = tempLocalFile;
@@ -1947,18 +1942,17 @@ function CreateURIFromPublishData(publis
   var URI;
   try {
     var spec = publishData.publishUrl;
     if (doDocUri)
       spec += FormatDirForPublishing(publishData.docDir) + publishData.filename; 
     else
       spec += FormatDirForPublishing(publishData.otherDir);
 
-    var ioService = GetIOService();
-    URI = ioService.newURI(spec, GetCurrentEditor().documentCharacterSet, null);
+    URI = Services.io.newURI(spec, GetCurrentEditor().documentCharacterSet, null);
 
     if (publishData.username)
       URI.username = publishData.username;
     if (publishData.password)
       URI.password = publishData.password;
   }
   catch (e) {}
 
--- a/editor/ui/composer/content/editorApplicationOverlay.js
+++ b/editor/ui/composer/content/editorApplicationOverlay.js
@@ -113,19 +113,17 @@ function editPage(url, aFileType)
       NewEditorWindow(url, charsetArg);
 
   } catch(e) {}
 }
 
 function createURI(urlstring)
 {
   try {
-    var ioserv = Components.classes["@mozilla.org/network/io-service;1"]
-               .getService(Components.interfaces.nsIIOService);
-    return ioserv.newURI(urlstring, null, null);
+    return Services.io.newURI(urlstring, null, null);
   } catch (e) {}
 
   return null;
 }
 
 function CheckOpenWindowForURIMatch(uri, win)
 {
   try {
--- a/editor/ui/composer/content/editorUtilities.js
+++ b/editor/ui/composer/content/editorUtilities.js
@@ -18,17 +18,16 @@ const kOutputEncodeHTMLEntities = Compon
 const kOutputEncodeLatin1Entities = Components.interfaces.nsIDocumentEncoder.OutputEncodeLatin1Entities;
 const kOutputEncodeW3CEntities = Components.interfaces.nsIDocumentEncoder.OutputEncodeW3CEntities;
 const kOutputFormatted = Components.interfaces.nsIDocumentEncoder.OutputFormatted;
 const kOutputLFLineBreak = Components.interfaces.nsIDocumentEncoder.OutputLFLineBreak;
 const kOutputSelectionOnly = Components.interfaces.nsIDocumentEncoder.OutputSelectionOnly;
 const kOutputWrap = Components.interfaces.nsIDocumentEncoder.OutputWrap;
 
 var gStringBundle;
-var gIOService;
 var gFilePickerDirectory;
 
 var gOS = "";
 const gWin = "Win";
 const gUNIX = "UNIX";
 const gMac = "Mac";
 
 const kWebComposerWindowID = "editorWindow";
@@ -443,31 +442,19 @@ function SetElementEnabled(element, doEn
   else
   {
     dump("Element  not found in SetElementEnabled\n");
   }
 }
 
 /************* Services / Prefs ***************/
 
-function GetIOService()
-{
-  if (gIOService)
-    return gIOService;
-
-  gIOService = Components.classes["@mozilla.org/network/io-service;1"]
-               .getService(Components.interfaces.nsIIOService);
-
-  return gIOService;
-}
-
 function GetFileProtocolHandler()
 {
-  var ios = GetIOService();
-  var handler = ios.getProtocolHandler("file");
+  let handler = Services.io.getProtocolHandler("file");
   return handler.QueryInterface(Components.interfaces.nsIFileProtocolHandler);
 }
 
 function GetStringPref(name)
 {
   try {
     return Services.prefs.getComplexValue(name, Components.interfaces.nsISupportsString).data;
   } catch (e) {}
@@ -582,31 +569,27 @@ function MakeRelativeUrl(url)
     return inputUrl;
 
   var urlScheme = GetScheme(inputUrl);
 
   // Do nothing if not the same scheme or url is already relativized
   if (docScheme != urlScheme)
     return inputUrl;
 
-  var IOService = GetIOService();
-  if (!IOService)
-    return inputUrl;
-
   // Host must be the same
   var docHost = GetHost(docUrl);
   var urlHost = GetHost(inputUrl);
   if (docHost != urlHost)
     return inputUrl;
 
 
   // Get just the file path part of the urls
   // XXX Should we use GetCurrentEditor().documentCharacterSet for 2nd param ?
-  var docPath = IOService.newURI(docUrl, GetCurrentEditor().documentCharacterSet, null).path;
-  var urlPath = IOService.newURI(inputUrl, GetCurrentEditor().documentCharacterSet, null).path;
+  let docPath = Services.io.newURI(docUrl, GetCurrentEditor().documentCharacterSet, null).path;
+  let urlPath = Services.io.newURI(inputUrl, GetCurrentEditor().documentCharacterSet, null).path;
 
   // We only return "urlPath", so we can convert
   //  the entire docPath for case-insensitive comparisons
   var os = GetOS();
   var doCaseInsensitive = (docScheme == "file" && os == gWin);
   if (doCaseInsensitive)
     docPath = docPath.toLowerCase();
 
@@ -689,40 +672,36 @@ function MakeRelativeUrl(url)
     urlPath = "../" + urlPath;
     nextDocSlash = docPath.indexOf("\/", nextDocSlash+1);
   }
   return urlPath;
 }
 
 function MakeAbsoluteUrl(url)
 {
-  var resultUrl = TrimString(url);
+  let resultUrl = TrimString(url);
   if (!resultUrl)
     return resultUrl;
 
   // Check if URL is already absolute, i.e., it has a scheme
-  var urlScheme = GetScheme(resultUrl);
+  let urlScheme = GetScheme(resultUrl);
 
   if (urlScheme)
     return resultUrl;
 
-  var docUrl = GetDocumentBaseUrl();
-  var docScheme = GetScheme(docUrl);
+  let docUrl = GetDocumentBaseUrl();
+  let docScheme = GetScheme(docUrl);
 
   // Can't relativize if no doc scheme (page hasn't been saved)
   if (!docScheme)
     return resultUrl;
 
-  var  IOService = GetIOService();
-  if (!IOService)
-    return resultUrl;
-  
   // Make a URI object to use its "resolve" method
-  var absoluteUrl = resultUrl;
-  var docUri = IOService.newURI(docUrl, GetCurrentEditor().documentCharacterSet, null);
+  let absoluteUrl = resultUrl;
+  let docUri = Services.io.newURI(docUrl, GetCurrentEditor().documentCharacterSet, null);
 
   try {
     absoluteUrl = docUri.resolve(resultUrl);
     // This is deprecated and buggy! 
     // If used, we must make it a path for the parent directory (remove filename)
     //absoluteUrl = IOService.resolveRelativePath(resultUrl, docUrl);
   } catch (e) {}
 
@@ -766,79 +745,63 @@ function GetDocumentUrl()
 // Extract the scheme (e.g., 'file', 'http') from a URL string
 function GetScheme(urlspec)
 {
   var resultUrl = TrimString(urlspec);
   // Unsaved document URL has no acceptable scheme yet
   if (!resultUrl || IsUrlAboutBlank(resultUrl))
     return "";
 
-  var IOService = GetIOService();
-  if (!IOService)
-    return "";
-
   var scheme = "";
   try {
     // This fails if there's no scheme
-    scheme = IOService.extractScheme(resultUrl);
+    scheme = Services.io.extractScheme(resultUrl);
   } catch (e) {}
 
   return scheme ? scheme.toLowerCase() : "";
 }
 
 function GetHost(urlspec)
 {
   if (!urlspec)
     return "";
 
-  var IOService = GetIOService();
-  if (!IOService)
-    return "";
-
   var host = "";
   try {
-    host = IOService.newURI(urlspec, null, null).host;
+    host = Services.io.newURI(urlspec, null, null).host;
    } catch (e) {}
 
   return host;
 }
 
 function GetUsername(urlspec)
 {
   if (!urlspec)
     return "";
 
-  var IOService = GetIOService();
-  if (!IOService)
-    return "";
-
   var username = "";
   try {
-    username = IOService.newURI(urlspec, null, null).username;
+    username = Services.io.newURI(urlspec, null, null).username;
   } catch (e) {}
 
   return username;
 }
 
 function GetFilename(urlspec)
 {
   if (!urlspec || IsUrlAboutBlank(urlspec))
     return "";
 
-  var IOService = GetIOService();
-  if (!IOService)
-    return "";
-
   var filename;
 
   try {
-    var uri = IOService.newURI(urlspec, null, null);
+    let uri = Services.io.newURI(urlspec, null, null);
     if (uri)
     {
-      var url = uri.QueryInterface(Components.interfaces.nsIURL);
+      let url = uri.QueryInterface(Components.interfaces.nsIURL);
       if (url)
         filename = url.fileName;
     }
   } catch (e) {}
 
   return filename ? filename : "";
 }
 
@@ -856,31 +819,27 @@ function StripUsernamePassword(urlspec, 
   if (passwordObj)
     passwordObj.value = "";
 
   // "@" must exist else we will never detect username or password
   var atIndex = urlspec.indexOf("@");
   if (atIndex > 0)
   {
     try {
-      var IOService = GetIOService();
-      if (!IOService)
-        return urlspec;
-
-      var uri = IOService.newURI(urlspec, null, null);
-      var username = uri.username;
-      var password = uri.password;
+      let uri = Services.io.newURI(urlspec, null, null);
+      let username = uri.username;
+      let password = uri.password;
 
       if (usernameObj && username)
         usernameObj.value = username;
       if (passwordObj && password)
         passwordObj.value = password;
       if (username)
       {
-        var usernameStart = urlspec.indexOf(username);
+        let usernameStart = urlspec.indexOf(username);
         if (usernameStart != -1)
           return urlspec.slice(0, usernameStart) + urlspec.slice(atIndex+1);
       }
     } catch (e) {}
   }
   return urlspec;
 }
 
@@ -893,28 +852,24 @@ function StripPassword(urlspec, password
   if (passwordObj)
     passwordObj.value = "";
 
   // "@" must exist else we will never detect password
   var atIndex = urlspec.indexOf("@");
   if (atIndex > 0)
   {
     try {
-      var IOService = GetIOService();
-      if (!IOService)
-        return urlspec;
-
-      var password = IOService.newURI(urlspec, null, null).password;
+      let password = Services.io.newURI(urlspec, null, null).password;
 
       if (passwordObj && password)
         passwordObj.value = password;
       if (password)
       {
         // Find last ":" before "@"
-        var colon = urlspec.lastIndexOf(":", atIndex);
+        let colon = urlspec.lastIndexOf(":", atIndex);
         if (colon != -1)
         {
           // Include the "@"
           return urlspec.slice(0, colon) + urlspec.slice(atIndex);
         }
       }
     } catch (e) {}
   }
@@ -941,18 +896,17 @@ function StripUsernamePasswordFromURI(ur
 }
 
 function InsertUsernameIntoUrl(urlspec, username)
 {
   if (!urlspec || !username)
     return urlspec;
 
   try {
-    var ioService = GetIOService();
-    var URI = ioService.newURI(urlspec, GetCurrentEditor().documentCharacterSet, null);
+    let URI = Services.io.newURI(urlspec, GetCurrentEditor().documentCharacterSet, null);
     URI.username = username;
     return URI.spec;
   } catch (e) {}
 
   return urlspec;
 }
 
 function GetOS()
--- a/editor/ui/composer/content/publishprefs.js
+++ b/editor/ui/composer/content/publishprefs.js
@@ -874,19 +874,17 @@ function SavePassword(publishData)
   return false;
 }
 
 function GetUrlForPasswordManager(publishData)
 {
   if (!publishData || !publishData.publishUrl)
     return false;
 
-  var url = Components.classes["@mozilla.org/network/io-service;1"]
-                      .getService(Components.interfaces.nsIIOService)
-                      .newURI(publishData.publishUrl, null, null);
+  let url = Services.io.newURI(publishData.publishUrl, null, null);
 
   if (url.scheme == "ftp" && publishData.username)
     // Include username in the URL so we can handle multiple users per server
     // in the password manager
     url = url.scheme + "://" + publishData.username + "@" + url.hostPort;
   else
     url = url.scheme + "://" + url.hostPort;
 
--- a/editor/ui/dialogs/content/EdImageOverlay.js
+++ b/editor/ui/dialogs/content/EdImageOverlay.js
@@ -1,18 +1,18 @@
 /* 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/. */
 
 /*
- Note: We encourage non-empty alt text for images inserted into a page. 
+ Note: We encourage non-empty alt text for images inserted into a page.
  When there's no alt text, we always write 'alt=""' as the attribute, since "alt" is a required attribute.
  We allow users to not have alt text by checking a "Don't use alterate text" radio button,
  and we don't accept spaces as valid alt text. A space used to be required to avoid the error message
- if user didn't enter alt text, but is unnecessary now that we no longer annoy the user 
+ if user didn't enter alt text, but is unnecessary now that we no longer annoy the user
  with the error dialog if alt="" is present on an img element.
  We trim all spaces at the beginning and end of user's alt text
 */
 
 var gInsertNewImage = true;
 var gDoAltTextError = false;
 var gConstrainOn = false;
 // Note used in current version, but these are set correctly
@@ -283,44 +283,40 @@ function LoadPreviewImage()
   var imageSrc = TrimString(gDialog.srcInput.value);
   if (!imageSrc)
     return;
 
   try {
     // Remove the image URL from image cache so it loads fresh
     //  (if we don't do this, loads after the first will always use image cache
     //   and we won't see image edit changes or be able to get actual width and height)
-    
-    var IOService = GetIOService();
-    if (IOService)
+
+    // We must have an absolute URL to preview it or remove it from the cache
+    imageSrc = MakeAbsoluteUrl(imageSrc);
+
+    if (GetScheme(imageSrc))
     {
-      // We must have an absolute URL to preview it or remove it from the cache
-      imageSrc = MakeAbsoluteUrl(imageSrc);
-
-      if (GetScheme(imageSrc))
+      let uri = Services.io.newURI(imageSrc, null, null);
+      if (uri)
       {
-        var uri = IOService.newURI(imageSrc, null, null);
-        if (uri)
-        {
-          var imgCacheService = Components.classes["@mozilla.org/image/cache;1"].getService();
-          var imgCache = imgCacheService.QueryInterface(Components.interfaces.imgICache);
+        let imgCacheService = Components.classes["@mozilla.org/image/cache;1"].getService();
+        let imgCache = imgCacheService.QueryInterface(Components.interfaces.imgICache);
 
-          // This returns error if image wasn't in the cache; ignore that
-          imgCache.removeEntry(uri);
-        }
+        // This returns error if image wasn't in the cache; ignore that
+        imgCache.removeEntry(uri);
       }
     }
   } catch(e) {}
 
   if (gDialog.PreviewImage)
     removeEventListener("load", PreviewImageLoaded, true);
 
   if (gDialog.ImageHolder.firstChild)
     gDialog.ImageHolder.removeChild(gDialog.ImageHolder.firstChild);
-    
+
   gDialog.PreviewImage = document.createElementNS("http://www.w3.org/1999/xhtml", "html:img");
   if (gDialog.PreviewImage)
   {
     // set the src before appending to the document -- see bug 198435 for why
     // this is needed.
     // XXXbz that bug is long-since fixed.  Is this still needed?
     gDialog.PreviewImage.addEventListener("load", PreviewImageLoaded, true);
     gDialog.PreviewImage.src = imageSrc;
@@ -511,22 +507,22 @@ function ValidateImage()
 
   var width = "";
   var height = "";
 
   gValidateTab = gDialog.tabDimensions;
   if (!gDialog.actualSizeRadio.selected)
   {
     // Get user values for width and height
-    width = ValidateNumber(gDialog.widthInput, gDialog.widthUnitsMenulist, 1, gMaxPixels, 
+    width = ValidateNumber(gDialog.widthInput, gDialog.widthUnitsMenulist, 1, gMaxPixels,
                            globalElement, "width", false, true);
     if (gValidationError)
       return false;
 
-    height = ValidateNumber(gDialog.heightInput, gDialog.heightUnitsMenulist, 1, gMaxPixels, 
+    height = ValidateNumber(gDialog.heightInput, gDialog.heightUnitsMenulist, 1, gMaxPixels,
                             globalElement, "height", false, true);
     if (gValidationError)
       return false;
   }
 
   // We always set the width and height attributes, even if same as actual.
   //  This speeds up layout of pages since sizes are known before image is loaded
   if (!width)
@@ -539,33 +535,33 @@ function ValidateImage()
   var srcChanged = (src != gOriginalSrc);
   if (width)
     editor.setAttributeOrEquivalent(globalElement, "width", width, true);
   else if (srcChanged)
     editor.removeAttributeOrEquivalent(globalElement, "width", true);
 
   if (height)
     editor.setAttributeOrEquivalent(globalElement, "height", height, true);
-  else if (srcChanged) 
+  else if (srcChanged)
     editor.removeAttributeOrEquivalent(globalElement, "height", true);
 
   // spacing attributes
   gValidateTab = gDialog.tabBorder;
-  ValidateNumber(gDialog.imagelrInput, null, 0, gMaxPixels, 
+  ValidateNumber(gDialog.imagelrInput, null, 0, gMaxPixels,
                  globalElement, "hspace", false, true, true);
   if (gValidationError)
     return false;
 
-  ValidateNumber(gDialog.imagetbInput, null, 0, gMaxPixels, 
+  ValidateNumber(gDialog.imagetbInput, null, 0, gMaxPixels,
                  globalElement, "vspace", false, true);
   if (gValidationError)
     return false;
 
   // note this is deprecated and should be converted to stylesheets
-  ValidateNumber(gDialog.border, null, 0, gMaxPixels, 
+  ValidateNumber(gDialog.border, null, 0, gMaxPixels,
                  globalElement, "border", false, true);
   if (gValidationError)
     return false;
 
   // Default or setting "bottom" means don't set the attribute
   // Note that the attributes "left" and "right" are opposite
   //  of what we use in the UI, which describes where the TEXT wraps,
   //  not the image location (which is what the HTML describes)