bug 709589 - Some engine manager cleanup found by SeaMonkey reviews - use Services.jsm, r=gavin
authorRobert Kaiser <kairo@kairo.at>
Thu, 19 Jan 2012 13:39:44 +0100
changeset 84892 84becc09d9044bc9e1db839271a4286b581bf3df
parent 84891 1fd3d62346865eceb3f29a147e2e380864d04d42
child 84893 79cb693afd546acaa7001479f6c55abdd4d735ad
push id21881
push usermbrubeck@mozilla.com
push dateThu, 19 Jan 2012 18:41:36 +0000
treeherdermozilla-central@e5e66f40c35b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgavin
bugs709589
milestone12.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 709589 - Some engine manager cleanup found by SeaMonkey reviews - use Services.jsm, r=gavin
browser/components/search/content/engineManager.js
browser/components/search/content/engineManager.xul
--- a/browser/components/search/content/engineManager.js
+++ b/browser/components/search/content/engineManager.js
@@ -33,40 +33,43 @@
 # use your version of this file under the terms of the MPL, indicate your
 # decision by deleting the provisions above and replace them with the notice
 # and other provisions required by the GPL or the LGPL. If you do not delete
 # the provisions above, a recipient may use your version of this file under
 # the terms of any one of the MPL, the GPL or the LGPL.
 #
 # ***** END LICENSE BLOCK *****
 
+Components.utils.import("resource://gre/modules/Services.jsm");
+
 const Ci = Components.interfaces;
 const Cc = Components.classes;
 
 const ENGINE_FLAVOR = "text/x-moz-search-engine";
 
 const BROWSER_SUGGEST_PREF = "browser.search.suggest.enabled";
 
 var gEngineView = null;
 
 var gEngineManagerDialog = {
   init: function engineManager_init() {
     gEngineView = new EngineView(new EngineStore());
 
-    var prefService = Cc["@mozilla.org/preferences-service;1"].
-                      getService(Ci.nsIPrefBranch);
-    var suggestEnabled = prefService.getBoolPref(BROWSER_SUGGEST_PREF);
+    var suggestEnabled = Services.prefs.getBoolPref(BROWSER_SUGGEST_PREF);
     document.getElementById("enableSuggest").checked = suggestEnabled;
 
     var tree = document.getElementById("engineList");
     tree.view = gEngineView;
 
-    var os = Cc["@mozilla.org/observer-service;1"].
-             getService(Ci.nsIObserverService);
-    os.addObserver(this, "browser-search-engine-modified", false);
+    Services.obs.addObserver(this, "browser-search-engine-modified", false);
+  },
+
+  destroy: function engineManager_destroy() {
+    // Remove the observer
+    Services.obs.removeObserver(this, "browser-search-engine-modified");
   },
 
   observe: function engineManager_observe(aEngine, aTopic, aVerb) {
     if (aTopic == "browser-search-engine-modified") {
       aEngine.QueryInterface(Ci.nsISearchEngine)
       switch (aVerb) {
       case "engine-added":
         gEngineView._engineStore.addEngine(aEngine);
@@ -80,37 +83,23 @@ var gEngineManagerDialog = {
         // Not relevant
         return;
       }
       gEngineView.invalidate();
     }
   },
 
   onOK: function engineManager_onOK() {
-    // Remove the observer
-    var os = Cc["@mozilla.org/observer-service;1"].
-             getService(Ci.nsIObserverService);
-    os.removeObserver(this, "browser-search-engine-modified");
-
     // Set the preference
     var newSuggestEnabled = document.getElementById("enableSuggest").checked;
-    var prefService = Cc["@mozilla.org/preferences-service;1"].
-                      getService(Ci.nsIPrefBranch);
-    prefService.setBoolPref(BROWSER_SUGGEST_PREF, newSuggestEnabled);
+    Services.prefs.setBoolPref(BROWSER_SUGGEST_PREF, newSuggestEnabled);
 
     // Commit the changes
     gEngineView._engineStore.commit();
   },
-  
-  onCancel: function engineManager_onCancel() {
-    // Remove the observer
-    var os = Cc["@mozilla.org/observer-service;1"].
-             getService(Ci.nsIObserverService);
-    os.removeObserver(this, "browser-search-engine-modified");
-  },
 
   onRestoreDefaults: function engineManager_onRestoreDefaults() {
     var num = gEngineView._engineStore.restoreDefaultEngines();
     gEngineView.rowCountChanged(0, num);
     gEngineView.invalidate();
   },
 
   showRestoreDefaults: function engineManager_showRestoreDefaults(val) {
@@ -151,24 +140,22 @@ var gEngineManagerDialog = {
     document.getElementById("engineList").focus();
   },
 
   editKeyword: function engineManager_editKeyword() {
     var selectedEngine = gEngineView.selectedEngine;
     if (!selectedEngine)
       return;
 
-    var prompt = Cc["@mozilla.org/embedcomp/prompt-service;1"].
-                 getService(Ci.nsIPromptService);
     var alias = { value: selectedEngine.alias };
     var strings = document.getElementById("engineManagerBundle");
     var title = strings.getString("editTitle");
     var msg = strings.getFormattedString("editMsg", [selectedEngine.name]);
 
-    while (prompt.prompt(window, title, msg, alias, null, { })) {
+    while (Services.prompt.prompt(window, title, msg, alias, null, { })) {
       var bduplicate = false;
       var eduplicate = false;
 
       if (alias.value != "") {
         try {
           let bmserv = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
                        getService(Ci.nsINavBookmarksService);
           if (bmserv.getURIForKeyword(alias.value))
@@ -188,17 +175,17 @@ var gEngineManagerDialog = {
 
       // Notify the user if they have chosen an existing engine/bookmark keyword
       if (eduplicate || bduplicate) {
         var dtitle = strings.getString("duplicateTitle");
         var bmsg = strings.getString("duplicateBookmarkMsg");
         var emsg = strings.getFormattedString("duplicateEngineMsg",
                                               [engine.name]);
 
-        prompt.alert(window, dtitle, (eduplicate) ? emsg : bmsg);
+        Services.prompt.alert(window, dtitle, eduplicate ? emsg : bmsg);
       } else {
         gEngineView._engineStore.changeEngine(selectedEngine, "alias",
                                               alias.value);
         gEngineView.invalidate();
         break;
       }
     }
   },
@@ -238,50 +225,44 @@ function EngineMoveOp(aEngineClone, aNew
     throw new Error("bad args to new EngineMoveOp!");
   this._engine = aEngineClone.originalEngine;
   this._newIndex = aNewIndex;
 }
 EngineMoveOp.prototype = {
   _engine: null,
   _newIndex: null,
   commit: function EMO_commit() {
-    var searchService = Cc["@mozilla.org/browser/search-service;1"].
-                        getService(Ci.nsIBrowserSearchService);
-    searchService.moveEngine(this._engine, this._newIndex);
+    Services.search.moveEngine(this._engine, this._newIndex);
   }
 }
 
 function EngineRemoveOp(aEngineClone) {
   if (!aEngineClone)
     throw new Error("bad args to new EngineRemoveOp!");
   this._engine = aEngineClone.originalEngine;
 }
 EngineRemoveOp.prototype = {
   _engine: null,
   commit: function ERO_commit() {
-    var searchService = Cc["@mozilla.org/browser/search-service;1"].
-                        getService(Ci.nsIBrowserSearchService);
-    searchService.removeEngine(this._engine);
+    Services.search.removeEngine(this._engine);
   }
 }
 
 function EngineUnhideOp(aEngineClone, aNewIndex) {
   if (!aEngineClone)
     throw new Error("bad args to new EngineUnhideOp!");
   this._engine = aEngineClone.originalEngine;
   this._newIndex = aNewIndex;
 }
 EngineUnhideOp.prototype = {
   _engine: null,
   _newIndex: null,
   commit: function EUO_commit() {
     this._engine.hidden = false;
-    var searchService = Cc["@mozilla.org/browser/search-service;1"].
-                        getService(Ci.nsIBrowserSearchService);
-    searchService.moveEngine(this._engine, this._newIndex);
+    Services.search.moveEngine(this._engine, this._newIndex);
   }
 }
 
 function EngineChangeOp(aEngineClone, aProp, aValue) {
   if (!aEngineClone)
     throw new Error("bad args to new EngineChangeOp!");
 
   this._engine = aEngineClone.originalEngine;
@@ -293,20 +274,18 @@ EngineChangeOp.prototype = {
   _prop: null,
   _newValue: null,
   commit: function ECO_commit() {
     this._engine[this._prop] = this._newValue;
   }
 }
 
 function EngineStore() {
-  var searchService = Cc["@mozilla.org/browser/search-service;1"].
-                      getService(Ci.nsIBrowserSearchService);
-  this._engines = searchService.getVisibleEngines().map(this._cloneEngine);
-  this._defaultEngines = searchService.getDefaultEngines().map(this._cloneEngine);
+  this._engines = Services.search.getVisibleEngines().map(this._cloneEngine);
+  this._defaultEngines = Services.search.getDefaultEngines().map(this._cloneEngine);
 
   this._ops = [];
 
   // check if we need to disable the restore defaults button
   var someHidden = this._defaultEngines.some(function (e) e.hidden);
   gEngineManagerDialog.showRestoreDefaults(someHidden);
 }
 EngineStore.prototype = {
@@ -343,27 +322,25 @@ EngineStore.prototype = {
   },
 
   // Callback for Array's some(). A thisObj must be passed to some()
   _isSameEngine: function ES_isSameEngine(aEngineClone) {
     return aEngineClone.originalEngine == this.originalEngine;
   },
 
   commit: function ES_commit() {
-    var searchService = Cc["@mozilla.org/browser/search-service;1"].
-                        getService(Ci.nsIBrowserSearchService);
-    var currentEngine = this._cloneEngine(searchService.currentEngine);
+    var currentEngine = this._cloneEngine(Services.search.currentEngine);
     for (var i = 0; i < this._ops.length; i++)
       this._ops[i].commit();
 
     // Restore currentEngine if it is a default engine that is still visible.
     // Needed if the user deletes currentEngine and then restores it.
     if (this._defaultEngines.some(this._isSameEngine, currentEngine) &&
         !currentEngine.originalEngine.hidden)
-      searchService.currentEngine = currentEngine.originalEngine;
+      Services.search.currentEngine = currentEngine.originalEngine;
   },
 
   addEngine: function ES_addEngine(aEngine) {
     this._engines.push(this._cloneEngine(aEngine));
   },
 
   moveEngine: function ES_moveEngine(aEngine, aNewIndex) {
     if (aNewIndex < 0 || aNewIndex > this._engines.length - 1)
--- a/browser/components/search/content/engineManager.xul
+++ b/browser/components/search/content/engineManager.xul
@@ -43,18 +43,18 @@
 <!DOCTYPE dialog SYSTEM "chrome://browser/locale/engineManager.dtd">
 
 <dialog id="engineManager"
         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
         buttons="accept,cancel,extra2"
         buttonlabelextra2="&restoreDefaults.label;"
         buttonaccesskeyextra2="&restoreDefaults.accesskey;"
         onload="gEngineManagerDialog.init();"
+        onunload="gEngineManagerDialog.destroy();"
         ondialogaccept="gEngineManagerDialog.onOK();"
-        ondialogcancel="gEngineManagerDialog.onCancel();"
         ondialogextra2="gEngineManagerDialog.onRestoreDefaults();"
         title="&engineManager.title;"
         style="&engineManager.style;"
         persist="screenX screenY width height"
         windowtype="Browser:SearchManager">
 
   <script type="application/javascript"
           src="chrome://browser/content/search/engineManager.js"/>