Bug 520342 - Consolidate some getService calls. r=dietrich
authorRyan Flint <rflint@ryanflint.com>
Sat, 03 Oct 2009 17:48:43 -0400
changeset 33433 41038fb51fb5ab6a1c59bf1c3cda0cf99c6f58b6
parent 33432 2b4ab8c8f64f9d030c05192670642ea2b8b531c2
child 33434 5b2d368cfc9f83f4f83e910788178b4ec8915523
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdietrich
bugs520342
milestone1.9.3a1pre
Bug 520342 - Consolidate some getService calls. r=dietrich
browser/components/microsummaries/src/nsMicrosummaryService.js
--- a/browser/components/microsummaries/src/nsMicrosummaryService.js
+++ b/browser/components/microsummaries/src/nsMicrosummaryService.js
@@ -64,18 +64,28 @@ const ANNO_MICSUM_GEN_URI    = "microsum
 const ANNO_MICSUM_EXPIRATION = "microsummary/expiration";
 const ANNO_STATIC_TITLE      = "bookmarks/staticTitle";
 const ANNO_CONTENT_TYPE      = "bookmarks/contentType";
 
 const MAX_SUMMARY_LENGTH = 4096;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 
+__defineGetter__("NetUtil", function() {
+  delete this.NetUtil;
+  Cu.import("resource://gre/modules/NetUtil.jsm");
+  return NetUtil;
+});
+
+XPCOMUtils.defineLazyServiceGetter(this, "gObsSvc",
+                                   "@mozilla.org/observer-service;1",
+                                   "nsIObserverService");
+
 function MicrosummaryService() {
-  this._obs.addObserver(this, "xpcom-shutdown", true);
+  gObsSvc.addObserver(this, "xpcom-shutdown", true);
   this._ans.addObserver(this, false);
 
   Cc["@mozilla.org/preferences-service;1"].
     getService(Ci.nsIPrefService).
     getBranch("browser.microsummary.").
     QueryInterface(Ci.nsIPrefBranch2).
     addObserver("", this, true);
 
@@ -94,42 +104,16 @@ MicrosummaryService.prototype = {
 
   // Annotation Service
   get _ans() {
     var svc = Cc["@mozilla.org/browser/annotation-service;1"].
               getService(Ci.nsIAnnotationService);
     this.__defineGetter__("_ans", function() svc);
     return this._ans;
   },
- 
-  // IO Service
-  get _ios() {
-    var svc = Cc["@mozilla.org/network/io-service;1"].
-              getService(Ci.nsIIOService);
-    this.__defineGetter__("_ios", function() svc);
-    return this._ios;
-  },
-
-  // Observer Service
-  get _obs() {
-    var svc = Cc["@mozilla.org/observer-service;1"].
-              getService(Ci.nsIObserverService);
-    this.__defineGetter__("_obs", function() svc);
-    return this._obs;
-  },
-
-  /**
-   * Make a URI from a spec.
-   * @param   spec
-   *          The string spec of the URI.
-   * @returns An nsIURI object.
-   */
-  _uri: function MSS__uri(spec) {
-    return this._ios.newURI(spec, null, null);
-  },
 
   // Directory Locator
   __dirs: null,
   get _dirs() {
     if (!this.__dirs)
       this.__dirs = Cc["@mozilla.org/file/directory_service;1"].
                     getService(Ci.nsIProperties);
     return this.__dirs;
@@ -197,17 +181,17 @@ MicrosummaryService.prototype = {
       notify: function(timer) { this._svc._updateMicrosummaries() }
     };
     this._timer.initWithCallback(callback,
                                  CHECK_INTERVAL,
                                  this._timer.TYPE_REPEATING_SLACK);
   },
   
   _destroy: function MSS__destroy() {
-    this._obs.removeObserver(this, "xpcom-shutdown", true);
+    gObsSvc.removeObserver(this, "xpcom-shutdown", true);
     this._ans.removeObserver(this);
     this._timer.cancel();
     this._timer = null;
   },
 
   _updateMicrosummaries: function MSS__updateMicrosummaries() {
     var bookmarks = this._bookmarks;
 
@@ -258,17 +242,17 @@ MicrosummaryService.prototype = {
 
     // Update if the microsummary differs from the current title.
     if (!title || title != microsummary.content) {
       this._bms.setItemTitle(bookmarkID, microsummary.content);
       var subject = new LiveTitleNotificationSubject(bookmarkID, microsummary);
       LOG("updated live title for " + bookmarkIdentity +
           " from '" + (title == null ? "<no live title>" : title) +
           "' to '" + microsummary.content + "'");
-      this._obs.notifyObservers(subject, "microsummary-livetitle-updated", title);
+      gObsSvc.notifyObservers(subject, "microsummary-livetitle-updated", title);
     }
     else {
       LOG("didn't update live title for " + bookmarkIdentity + "; it hasn't changed");
     }
 
     // Whether or not the title itself has changed, we still save any changes
     // to the update interval, since the interval represents how long to wait
     // before checking again for updates, and that can vary across updates,
@@ -320,17 +304,17 @@ MicrosummaryService.prototype = {
   /**
    * Load a local generator from a file into the cache.
    * 
    * @param   file
    *          nsIFile object pointing to file from which to load generator
    * 
    */
   _cacheLocalGeneratorFile: function MSS__cacheLocalGeneratorFile(file) {
-    var uri = this._ios.newFileURI(file);
+    var uri = NetUtil.ioService.newFileURI(file);
 
     var t = this;
     var callback =
       function MSS_cacheLocalGeneratorCallback(resource) {
         try     { t._handleLocalGenerator(resource) }
         finally { resource.destroy() }
       };
 
@@ -428,28 +412,29 @@ MicrosummaryService.prototype = {
     else {
       // This generator is not already installed.  Save it as a new file.
       topic = "microsummary-generator-installed";
       var generatorName = rootNode.getAttribute("name");
       var fileName = sanitizeName(generatorName) + ".xml";
       var file = this._dirs.get("UsrMicsumGens", Ci.nsIFile);
       file.append(fileName);
       file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, PERMS_FILE);
-      generator = new MicrosummaryGenerator(null, this._ios.newFileURI(file));
+      generator = new MicrosummaryGenerator(null,
+                                            NetUtil.ioService.newFileURI(file));
       this._localGenerators[generatorID] = generator;
     }
  
     // Initialize (or reinitialize) the generator from its XML definition,
     // the save the definition to the generator's file.
     generator.initFromXML(xmlDefinition);
     generator.saveXMLToFile(xmlDefinition);
 
     LOG("installed generator " + generatorID);
 
-    this._obs.notifyObservers(generator, topic, null);
+    gObsSvc.notifyObservers(generator, topic, null);
 
     return generator;
   },
 
   /**
    * Get the set of microsummaries available for a given page.  The set
    * might change after this method returns, since this method will trigger
    * an asynchronous load of the page in question (if it isn't already loaded)
@@ -604,18 +589,18 @@ MicrosummaryService.prototype = {
    *          if the bookmark does not have a current microsummary
    *
    */
   getMicrosummary: function MSS_getMicrosummary(bookmarkID) {
     if (!this.hasMicrosummary(bookmarkID))
       return null;
 
     var pageURI = this._bms.getBookmarkURI(bookmarkID);
-    var generatorURI = this._uri(this._ans.getItemAnnotation(bookmarkID,
-                                                             ANNO_MICSUM_GEN_URI));
+    var generatorURI = NetUtil.newURI(this._ans.getItemAnnotation(bookmarkID,
+                                                                  ANNO_MICSUM_GEN_URI));
     var generator = this.getGenerator(generatorURI);
 
     return new Microsummary(pageURI, generator);
   },
 
   /**
    * Get a microsummary for a given page URI and generator URI.
    *
@@ -736,18 +721,18 @@ MicrosummaryService.prototype = {
    */
   refreshMicrosummary: function MSS_refreshMicrosummary(bookmarkID) {
     if (!this.hasMicrosummary(bookmarkID))
       throw "bookmark " + bookmarkID + " does not have a microsummary";
 
     var pageURI = this._bms.getBookmarkURI(bookmarkID);
     if (!pageURI)
       throw("can't get URL for bookmark with ID " + bookmarkID);
-    var generatorURI = this._uri(this._ans.getItemAnnotation(bookmarkID,
-                                                             ANNO_MICSUM_GEN_URI));
+    var generatorURI = NetUtil.newURI(this._ans.getItemAnnotation(bookmarkID,
+                                                                  ANNO_MICSUM_GEN_URI));
 
     var generator = this._localGenerators[generatorURI.spec] ||
                     new MicrosummaryGenerator(generatorURI);
 
     var microsummary = new Microsummary(pageURI, generator);
 
     // A microsummary observer that calls the microsummary service
     // to update the datastore when the microsummary finishes loading.
@@ -835,35 +820,16 @@ Microsummary.prototype = {
   __mss: null,
   get _mss() {
     if (!this.__mss)
       this.__mss = Cc["@mozilla.org/microsummary/service;1"].
                    getService(Ci.nsIMicrosummaryService);
     return this.__mss;
   },
 
-  // IO Service
-  __ios: null,
-  get _ios() {
-    if (!this.__ios)
-      this.__ios = Cc["@mozilla.org/network/io-service;1"].
-                   getService(Ci.nsIIOService);
-    return this.__ios;
-  },
-
-  /**
-   * Make a URI from a spec.
-   * @param   spec
-   *          The string spec of the URI.
-   * @returns An nsIURI object.
-   */
-  _uri: function MSS__uri(spec) {
-    return this._ios.newURI(spec, null, null);
-  },
-
   // nsISupports
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIMicrosummary]),
 
   // nsIMicrosummary
   get content() {
     // If we have everything we need to generate the content, generate it.
     if (!this._content &&
         this.generator.loaded &&
@@ -1056,17 +1022,17 @@ Microsummary.prototype = {
       function MS_missingGeneratorErrorCallback(resource) {
         try     { t._handleMissingGeneratorError(resource) }
         finally { resource.destroy() }
       };
 
     try {
       // Extract the URI from which the generator was originally installed.
       var sourceURL = this.generator.uri.path.replace(/^source:/, "");
-      var sourceURI = this._uri(sourceURL);
+      var sourceURI = NetUtil.newURI(sourceURL);
 
       var resource = new MicrosummaryResource(sourceURI);
       resource.load(loadCallback, errorCallback);
     }
     catch(ex) {
       Cu.reportError(ex);
       this._handleMissingGeneratorError();
     }
@@ -1148,26 +1114,16 @@ function MicrosummaryGenerator(aURI, aLo
   this._name = aName || null;
   this._loaded = false;
   this._rules = [];
   this._template = null;
   this._content = null;
 }
 
 MicrosummaryGenerator.prototype = {
-
-  // IO Service
-  __ios: null,
-  get _ios() {
-    if (!this.__ios)
-      this.__ios = Cc["@mozilla.org/network/io-service;1"].
-                   getService(Ci.nsIIOService);
-    return this.__ios;
-  },
-
   // nsISupports
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIMicrosummaryGenerator]),
 
   // nsIMicrosummaryGenerator
 
   // Normally this is just the URL from which we download the generator,
   // but for generators stored in the app or profile generators directory
   // it's the value of the generator tag's "uri" attribute (or its local URI
@@ -1262,17 +1218,17 @@ MicrosummaryGenerator.prototype = {
     if (!generatorNode)
       throw Cr.NS_ERROR_FAILURE;
 
     this._name = generatorNode.getAttribute("name");
 
     // We have to retrieve the URI from local generators via the "uri" attribute
     // of its generator tag.
     if (this.localURI && generatorNode.hasAttribute("uri"))
-      this._uri = this._ios.newURI(generatorNode.getAttribute("uri"), null, null);
+      this._uri = NetUtil.newURI(generatorNode.getAttribute("uri"), null, null);
 
     function getFirstChildByTagName(tagName, parentNode, namespace) {
       var nodeList = parentNode.getElementsByTagNameNS(namespace, tagName);
       for (var i = 0; i < nodeList.length; i++) {
         // Make sure that the node is a direct descendent of the generator node
         if (nodeList[i].parentNode == parentNode)
           return nodeList[i];
       }
@@ -1422,17 +1378,17 @@ MicrosummaryGenerator.prototype = {
   },
 
   update: function MSD_update() {
     // Update this generator if it was downloaded from a remote source and has
     // been modified since we last downloaded it.
     var genURI = this.uri;
     if (genURI && /^urn:source:/i.test(genURI.spec)) {
       let genURL = genURI.spec.replace(/^urn:source:/, "");
-      genURI = this._ios.newURI(genURL, null, null);
+      genURI = NetUtil.newURI(genURL, null, null);
     }
 
     // Only continue if we have a valid remote URI
     if (!genURI || !/^https?/.test(genURI.scheme)) {
       LOG("generator did not have valid URI; skipping update: " + genURI.spec);
       return;
     }
 
@@ -1483,19 +1439,17 @@ MicrosummaryGenerator.prototype = {
     var generatorID = this.uri.spec;
     resource.content.documentElement.setAttribute("uri", generatorID);
 
     // Reinitialize this generator with the newly downloaded XML and save to disk.
     this.initFromXML(resource.content);
     this.saveXMLToFile(resource.content);
 
     // Let observers know we've updated this generator
-    var obs = Cc["@mozilla.org/observer-service;1"].
-              getService(Ci.nsIObserverService);
-    obs.notifyObservers(this, "microsummary-generator-updated", null);
+    gObsSvc.notifyObservers(this, "microsummary-generator-updated", null);
   }
 };
 
 
 
 
 
 // Microsummary sets are collections of microsummaries.  They allow callers
@@ -1504,25 +1458,16 @@ MicrosummaryGenerator.prototype = {
 // the set instead of each individual microsummary.
 
 function MicrosummarySet() {
   this._observers = [];
   this._elements = [];
 }
 
 MicrosummarySet.prototype = {
-  // IO Service
-  __ios: null,
-  get _ios() {
-    if (!this.__ios)
-      this.__ios = Cc["@mozilla.org/network/io-service;1"].
-                   getService(Ci.nsIIOService);
-    return this.__ios;
-  },
-
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIMicrosummarySet,
                                          Ci.nsIMicrosummaryObserver]),
 
   // nsIMicrosummaryObserver
 
   onContentLoaded: function MSSet_onContentLoaded(microsummary) {
     for ( var i = 0; i < this._observers.length; i++ )
       this._observers[i].onContentLoaded(microsummary);
@@ -1586,19 +1531,18 @@ MicrosummarySet.prototype = {
 
 
       // Look for a TITLE attribute to give the generator a nice name in the UI.
       var linkTitle = link.getAttribute("title");
 
 
       // Unlike the "href" attribute, the "href" property contains
       // an absolute URI spec, so we use it here to create the URI.
-      var generatorURI = this._ios.newURI(link.href,
-                                          resource.content.characterSet,
-                                          null);
+      var generatorURI = NetUtil.newURI(link.href, resource.content.characterSet,
+                                        null);
 
       if (!/^https?$/i.test(generatorURI.scheme)) {
         LOG("can't load generator " + generatorURI.spec + " from page " +
             resource.uri.spec);
         continue;
       }
 
       var generator = new MicrosummaryGenerator(generatorURI, null, linkTitle);
@@ -1733,25 +1677,16 @@ function MicrosummaryResource(uri) {
   this._loadCallback = null;
   // A function to call if we get an error while loading/parsing the resource.
   this._errorCallback = null;
   // A hidden iframe to parse HTML content.
   this._iframe = null;
 }
 
 MicrosummaryResource.prototype = {
-  // IO Service
-  __ios: null,
-  get _ios() {
-    if (!this.__ios)
-      this.__ios = Cc["@mozilla.org/network/io-service;1"].
-                   getService(Ci.nsIIOService);
-    return this.__ios;
-  },
-
   get uri() {
     return this._uri;
   },
 
   get content() {
     return this._content;
   },